Commit | Line | Data |
---|---|---|
6298e948 LC |
1 | /* |
2 | * sun4i-ss.h - hardware cryptographic accelerator for Allwinner A20 SoC | |
3 | * | |
4 | * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com> | |
5 | * | |
6 | * Support AES cipher with 128,192,256 bits keysize. | |
7 | * Support MD5 and SHA1 hash algorithms. | |
8 | * Support DES and 3DES | |
9 | * | |
10 | * You could find the datasheet in Documentation/arm/sunxi/README | |
11 | * | |
12 | * Licensed under the GPL-2. | |
13 | */ | |
14 | ||
15 | #include <linux/clk.h> | |
16 | #include <linux/crypto.h> | |
17 | #include <linux/io.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/of.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <crypto/scatterwalk.h> | |
22 | #include <linux/scatterlist.h> | |
23 | #include <linux/interrupt.h> | |
24 | #include <linux/delay.h> | |
25 | #include <crypto/md5.h> | |
26 | #include <crypto/sha.h> | |
27 | #include <crypto/hash.h> | |
28 | #include <crypto/internal/hash.h> | |
29 | #include <crypto/aes.h> | |
30 | #include <crypto/des.h> | |
31 | #include <crypto/internal/rng.h> | |
32 | ||
33 | #define SS_CTL 0x00 | |
34 | #define SS_KEY0 0x04 | |
35 | #define SS_KEY1 0x08 | |
36 | #define SS_KEY2 0x0C | |
37 | #define SS_KEY3 0x10 | |
38 | #define SS_KEY4 0x14 | |
39 | #define SS_KEY5 0x18 | |
40 | #define SS_KEY6 0x1C | |
41 | #define SS_KEY7 0x20 | |
42 | ||
43 | #define SS_IV0 0x24 | |
44 | #define SS_IV1 0x28 | |
45 | #define SS_IV2 0x2C | |
46 | #define SS_IV3 0x30 | |
47 | ||
48 | #define SS_FCSR 0x44 | |
49 | ||
50 | #define SS_MD0 0x4C | |
51 | #define SS_MD1 0x50 | |
52 | #define SS_MD2 0x54 | |
53 | #define SS_MD3 0x58 | |
54 | #define SS_MD4 0x5C | |
55 | ||
56 | #define SS_RXFIFO 0x200 | |
57 | #define SS_TXFIFO 0x204 | |
58 | ||
59 | /* SS_CTL configuration values */ | |
60 | ||
61 | /* PRNG generator mode - bit 15 */ | |
62 | #define SS_PRNG_ONESHOT (0 << 15) | |
63 | #define SS_PRNG_CONTINUE (1 << 15) | |
64 | ||
65 | /* IV mode for hash */ | |
66 | #define SS_IV_ARBITRARY (1 << 14) | |
67 | ||
68 | /* SS operation mode - bits 12-13 */ | |
69 | #define SS_ECB (0 << 12) | |
70 | #define SS_CBC (1 << 12) | |
71 | #define SS_CTS (3 << 12) | |
72 | ||
73 | /* Counter width for CNT mode - bits 10-11 */ | |
74 | #define SS_CNT_16BITS (0 << 10) | |
75 | #define SS_CNT_32BITS (1 << 10) | |
76 | #define SS_CNT_64BITS (2 << 10) | |
77 | ||
78 | /* Key size for AES - bits 8-9 */ | |
79 | #define SS_AES_128BITS (0 << 8) | |
80 | #define SS_AES_192BITS (1 << 8) | |
81 | #define SS_AES_256BITS (2 << 8) | |
82 | ||
83 | /* Operation direction - bit 7 */ | |
84 | #define SS_ENCRYPTION (0 << 7) | |
85 | #define SS_DECRYPTION (1 << 7) | |
86 | ||
87 | /* SS Method - bits 4-6 */ | |
88 | #define SS_OP_AES (0 << 4) | |
89 | #define SS_OP_DES (1 << 4) | |
90 | #define SS_OP_3DES (2 << 4) | |
91 | #define SS_OP_SHA1 (3 << 4) | |
92 | #define SS_OP_MD5 (4 << 4) | |
93 | #define SS_OP_PRNG (5 << 4) | |
94 | ||
95 | /* Data end bit - bit 2 */ | |
96 | #define SS_DATA_END (1 << 2) | |
97 | ||
98 | /* PRNG start bit - bit 1 */ | |
99 | #define SS_PRNG_START (1 << 1) | |
100 | ||
101 | /* SS Enable bit - bit 0 */ | |
102 | #define SS_DISABLED (0 << 0) | |
103 | #define SS_ENABLED (1 << 0) | |
104 | ||
105 | /* SS_FCSR configuration values */ | |
106 | /* RX FIFO status - bit 30 */ | |
107 | #define SS_RXFIFO_FREE (1 << 30) | |
108 | ||
109 | /* RX FIFO empty spaces - bits 24-29 */ | |
110 | #define SS_RXFIFO_SPACES(val) (((val) >> 24) & 0x3f) | |
111 | ||
112 | /* TX FIFO status - bit 22 */ | |
113 | #define SS_TXFIFO_AVAILABLE (1 << 22) | |
114 | ||
115 | /* TX FIFO available spaces - bits 16-21 */ | |
116 | #define SS_TXFIFO_SPACES(val) (((val) >> 16) & 0x3f) | |
117 | ||
118 | #define SS_RX_MAX 32 | |
119 | #define SS_RX_DEFAULT SS_RX_MAX | |
120 | #define SS_TX_MAX 33 | |
121 | ||
122 | #define SS_RXFIFO_EMP_INT_PENDING (1 << 10) | |
123 | #define SS_TXFIFO_AVA_INT_PENDING (1 << 8) | |
124 | #define SS_RXFIFO_EMP_INT_ENABLE (1 << 2) | |
125 | #define SS_TXFIFO_AVA_INT_ENABLE (1 << 0) | |
126 | ||
127 | struct sun4i_ss_ctx { | |
128 | void __iomem *base; | |
129 | int irq; | |
130 | struct clk *busclk; | |
131 | struct clk *ssclk; | |
132 | struct device *dev; | |
133 | struct resource *res; | |
134 | spinlock_t slock; /* control the use of the device */ | |
135 | }; | |
136 | ||
137 | struct sun4i_ss_alg_template { | |
138 | u32 type; | |
139 | u32 mode; | |
140 | union { | |
141 | struct crypto_alg crypto; | |
142 | struct ahash_alg hash; | |
143 | } alg; | |
144 | struct sun4i_ss_ctx *ss; | |
145 | }; | |
146 | ||
147 | struct sun4i_tfm_ctx { | |
148 | u32 key[AES_MAX_KEY_SIZE / 4];/* divided by sizeof(u32) */ | |
149 | u32 keylen; | |
150 | u32 keymode; | |
151 | struct sun4i_ss_ctx *ss; | |
152 | }; | |
153 | ||
154 | struct sun4i_cipher_req_ctx { | |
155 | u32 mode; | |
156 | }; | |
157 | ||
158 | struct sun4i_req_ctx { | |
159 | u32 mode; | |
160 | u64 byte_count; /* number of bytes "uploaded" to the device */ | |
161 | u32 hash[5]; /* for storing SS_IVx register */ | |
162 | char buf[64]; | |
163 | unsigned int len; | |
164 | struct sun4i_ss_ctx *ss; | |
165 | }; | |
166 | ||
167 | int sun4i_hash_crainit(struct crypto_tfm *tfm); | |
168 | int sun4i_hash_init(struct ahash_request *areq); | |
169 | int sun4i_hash_update(struct ahash_request *areq); | |
170 | int sun4i_hash_final(struct ahash_request *areq); | |
171 | int sun4i_hash_finup(struct ahash_request *areq); | |
172 | int sun4i_hash_digest(struct ahash_request *areq); | |
173 | int sun4i_hash_export_md5(struct ahash_request *areq, void *out); | |
174 | int sun4i_hash_import_md5(struct ahash_request *areq, const void *in); | |
175 | int sun4i_hash_export_sha1(struct ahash_request *areq, void *out); | |
176 | int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in); | |
177 | ||
178 | int sun4i_ss_cbc_aes_encrypt(struct ablkcipher_request *areq); | |
179 | int sun4i_ss_cbc_aes_decrypt(struct ablkcipher_request *areq); | |
180 | int sun4i_ss_ecb_aes_encrypt(struct ablkcipher_request *areq); | |
181 | int sun4i_ss_ecb_aes_decrypt(struct ablkcipher_request *areq); | |
182 | ||
183 | int sun4i_ss_cbc_des_encrypt(struct ablkcipher_request *areq); | |
184 | int sun4i_ss_cbc_des_decrypt(struct ablkcipher_request *areq); | |
185 | int sun4i_ss_ecb_des_encrypt(struct ablkcipher_request *areq); | |
186 | int sun4i_ss_ecb_des_decrypt(struct ablkcipher_request *areq); | |
187 | ||
188 | int sun4i_ss_cbc_des3_encrypt(struct ablkcipher_request *areq); | |
189 | int sun4i_ss_cbc_des3_decrypt(struct ablkcipher_request *areq); | |
190 | int sun4i_ss_ecb_des3_encrypt(struct ablkcipher_request *areq); | |
191 | int sun4i_ss_ecb_des3_decrypt(struct ablkcipher_request *areq); | |
192 | ||
193 | int sun4i_ss_cipher_init(struct crypto_tfm *tfm); | |
194 | int sun4i_ss_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, | |
195 | unsigned int keylen); | |
196 | int sun4i_ss_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key, | |
197 | unsigned int keylen); | |
198 | int sun4i_ss_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key, | |
199 | unsigned int keylen); |