2 * sun4i-ss.h - hardware cryptographic accelerator for Allwinner A20 SoC
4 * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
6 * Support AES cipher with 128,192,256 bits keysize.
7 * Support MD5 and SHA1 hash algorithms.
10 * You could find the datasheet in Documentation/arm/sunxi/README
12 * Licensed under the GPL-2.
15 #include <linux/clk.h>
16 #include <linux/crypto.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
22 #include <crypto/scatterwalk.h>
23 #include <linux/scatterlist.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <crypto/md5.h>
27 #include <crypto/skcipher.h>
28 #include <crypto/sha.h>
29 #include <crypto/hash.h>
30 #include <crypto/internal/hash.h>
31 #include <crypto/internal/skcipher.h>
32 #include <crypto/aes.h>
33 #include <crypto/des.h>
34 #include <crypto/internal/rng.h>
59 #define SS_RXFIFO 0x200
60 #define SS_TXFIFO 0x204
62 /* SS_CTL configuration values */
64 /* PRNG generator mode - bit 15 */
65 #define SS_PRNG_ONESHOT (0 << 15)
66 #define SS_PRNG_CONTINUE (1 << 15)
68 /* IV mode for hash */
69 #define SS_IV_ARBITRARY (1 << 14)
71 /* SS operation mode - bits 12-13 */
72 #define SS_ECB (0 << 12)
73 #define SS_CBC (1 << 12)
74 #define SS_CTS (3 << 12)
76 /* Counter width for CNT mode - bits 10-11 */
77 #define SS_CNT_16BITS (0 << 10)
78 #define SS_CNT_32BITS (1 << 10)
79 #define SS_CNT_64BITS (2 << 10)
81 /* Key size for AES - bits 8-9 */
82 #define SS_AES_128BITS (0 << 8)
83 #define SS_AES_192BITS (1 << 8)
84 #define SS_AES_256BITS (2 << 8)
86 /* Operation direction - bit 7 */
87 #define SS_ENCRYPTION (0 << 7)
88 #define SS_DECRYPTION (1 << 7)
90 /* SS Method - bits 4-6 */
91 #define SS_OP_AES (0 << 4)
92 #define SS_OP_DES (1 << 4)
93 #define SS_OP_3DES (2 << 4)
94 #define SS_OP_SHA1 (3 << 4)
95 #define SS_OP_MD5 (4 << 4)
96 #define SS_OP_PRNG (5 << 4)
98 /* Data end bit - bit 2 */
99 #define SS_DATA_END (1 << 2)
101 /* PRNG start bit - bit 1 */
102 #define SS_PRNG_START (1 << 1)
104 /* SS Enable bit - bit 0 */
105 #define SS_DISABLED (0 << 0)
106 #define SS_ENABLED (1 << 0)
108 /* SS_FCSR configuration values */
109 /* RX FIFO status - bit 30 */
110 #define SS_RXFIFO_FREE (1 << 30)
112 /* RX FIFO empty spaces - bits 24-29 */
113 #define SS_RXFIFO_SPACES(val) (((val) >> 24) & 0x3f)
115 /* TX FIFO status - bit 22 */
116 #define SS_TXFIFO_AVAILABLE (1 << 22)
118 /* TX FIFO available spaces - bits 16-21 */
119 #define SS_TXFIFO_SPACES(val) (((val) >> 16) & 0x3f)
122 #define SS_RX_DEFAULT SS_RX_MAX
125 #define SS_RXFIFO_EMP_INT_PENDING (1 << 10)
126 #define SS_TXFIFO_AVA_INT_PENDING (1 << 8)
127 #define SS_RXFIFO_EMP_INT_ENABLE (1 << 2)
128 #define SS_TXFIFO_AVA_INT_ENABLE (1 << 0)
130 struct sun4i_ss_ctx
{
135 struct reset_control
*reset
;
137 struct resource
*res
;
138 spinlock_t slock
; /* control the use of the device */
141 struct sun4i_ss_alg_template
{
145 struct skcipher_alg crypto
;
146 struct ahash_alg hash
;
148 struct sun4i_ss_ctx
*ss
;
151 struct sun4i_tfm_ctx
{
152 u32 key
[AES_MAX_KEY_SIZE
/ 4];/* divided by sizeof(u32) */
155 struct sun4i_ss_ctx
*ss
;
158 struct sun4i_cipher_req_ctx
{
162 struct sun4i_req_ctx
{
164 u64 byte_count
; /* number of bytes "uploaded" to the device */
165 u32 hash
[5]; /* for storing SS_IVx register */
171 int sun4i_hash_crainit(struct crypto_tfm
*tfm
);
172 int sun4i_hash_init(struct ahash_request
*areq
);
173 int sun4i_hash_update(struct ahash_request
*areq
);
174 int sun4i_hash_final(struct ahash_request
*areq
);
175 int sun4i_hash_finup(struct ahash_request
*areq
);
176 int sun4i_hash_digest(struct ahash_request
*areq
);
177 int sun4i_hash_export_md5(struct ahash_request
*areq
, void *out
);
178 int sun4i_hash_import_md5(struct ahash_request
*areq
, const void *in
);
179 int sun4i_hash_export_sha1(struct ahash_request
*areq
, void *out
);
180 int sun4i_hash_import_sha1(struct ahash_request
*areq
, const void *in
);
182 int sun4i_ss_cbc_aes_encrypt(struct skcipher_request
*areq
);
183 int sun4i_ss_cbc_aes_decrypt(struct skcipher_request
*areq
);
184 int sun4i_ss_ecb_aes_encrypt(struct skcipher_request
*areq
);
185 int sun4i_ss_ecb_aes_decrypt(struct skcipher_request
*areq
);
187 int sun4i_ss_cbc_des_encrypt(struct skcipher_request
*areq
);
188 int sun4i_ss_cbc_des_decrypt(struct skcipher_request
*areq
);
189 int sun4i_ss_ecb_des_encrypt(struct skcipher_request
*areq
);
190 int sun4i_ss_ecb_des_decrypt(struct skcipher_request
*areq
);
192 int sun4i_ss_cbc_des3_encrypt(struct skcipher_request
*areq
);
193 int sun4i_ss_cbc_des3_decrypt(struct skcipher_request
*areq
);
194 int sun4i_ss_ecb_des3_encrypt(struct skcipher_request
*areq
);
195 int sun4i_ss_ecb_des3_decrypt(struct skcipher_request
*areq
);
197 int sun4i_ss_cipher_init(struct crypto_tfm
*tfm
);
198 int sun4i_ss_aes_setkey(struct crypto_skcipher
*tfm
, const u8
*key
,
199 unsigned int keylen
);
200 int sun4i_ss_des_setkey(struct crypto_skcipher
*tfm
, const u8
*key
,
201 unsigned int keylen
);
202 int sun4i_ss_des3_setkey(struct crypto_skcipher
*tfm
, const u8
*key
,
203 unsigned int keylen
);