1
2
3
4
5
6
7
8#include <crypto/aes.h>
9#include <crypto/des.h>
10#include <crypto/engine.h>
11#include <crypto/rng.h>
12#include <crypto/skcipher.h>
13#include <linux/atomic.h>
14#include <linux/debugfs.h>
15#include <linux/crypto.h>
16#include <crypto/internal/hash.h>
17#include <crypto/md5.h>
18#include <crypto/sha1.h>
19#include <crypto/sha2.h>
20
21#define SS_START 1
22
23#define SS_ENCRYPTION 0
24#define SS_DECRYPTION BIT(6)
25
26#define SS_ALG_AES 0
27#define SS_ALG_DES (1 << 2)
28#define SS_ALG_3DES (2 << 2)
29#define SS_ALG_MD5 (3 << 2)
30#define SS_ALG_PRNG (4 << 2)
31#define SS_ALG_SHA1 (6 << 2)
32#define SS_ALG_SHA224 (7 << 2)
33#define SS_ALG_SHA256 (8 << 2)
34
35#define SS_CTL_REG 0x00
36#define SS_INT_CTL_REG 0x04
37#define SS_INT_STA_REG 0x08
38#define SS_KEY_ADR_REG 0x10
39#define SS_IV_ADR_REG 0x18
40#define SS_SRC_ADR_REG 0x20
41#define SS_DST_ADR_REG 0x28
42#define SS_LEN_ADR_REG 0x30
43
44#define SS_ID_NOTSUPP 0xFF
45
46#define SS_ID_CIPHER_AES 0
47#define SS_ID_CIPHER_DES 1
48#define SS_ID_CIPHER_DES3 2
49#define SS_ID_CIPHER_MAX 3
50
51#define SS_ID_OP_ECB 0
52#define SS_ID_OP_CBC 1
53#define SS_ID_OP_MAX 2
54
55#define SS_AES_128BITS 0
56#define SS_AES_192BITS 1
57#define SS_AES_256BITS 2
58
59#define SS_OP_ECB 0
60#define SS_OP_CBC (1 << 13)
61
62#define SS_ID_HASH_MD5 0
63#define SS_ID_HASH_SHA1 1
64#define SS_ID_HASH_SHA224 2
65#define SS_ID_HASH_SHA256 3
66#define SS_ID_HASH_MAX 4
67
68#define SS_FLOW0 BIT(30)
69#define SS_FLOW1 BIT(31)
70
71#define SS_PRNG_CONTINUE BIT(18)
72
73#define MAX_SG 8
74
75#define MAXFLOW 2
76
77#define SS_MAX_CLOCKS 2
78
79#define SS_DIE_ID_SHIFT 20
80#define SS_DIE_ID_MASK 0x07
81
82#define PRNG_DATA_SIZE (160 / 8)
83#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
84
85
86
87
88
89
90
91struct ss_clock {
92 const char *name;
93 unsigned long freq;
94 unsigned long max_freq;
95};
96
97
98
99
100
101
102
103
104
105
106struct ss_variant {
107 char alg_cipher[SS_ID_CIPHER_MAX];
108 char alg_hash[SS_ID_HASH_MAX];
109 u32 op_mode[SS_ID_OP_MAX];
110 struct ss_clock ss_clks[SS_MAX_CLOCKS];
111};
112
113struct sginfo {
114 u32 addr;
115 u32 len;
116};
117
118
119
120
121
122
123
124
125struct sun8i_ss_flow {
126 struct crypto_engine *engine;
127 struct completion complete;
128 int status;
129#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
130 unsigned long stat_req;
131#endif
132};
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147struct sun8i_ss_dev {
148 void __iomem *base;
149 struct clk *ssclks[SS_MAX_CLOCKS];
150 struct reset_control *reset;
151 struct device *dev;
152 struct mutex mlock;
153 struct sun8i_ss_flow *flows;
154 atomic_t flow;
155 const struct ss_variant *variant;
156#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
157 struct dentry *dbgfs_dir;
158 struct dentry *dbgfs_stats;
159#endif
160};
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177struct sun8i_cipher_req_ctx {
178 struct sginfo t_src[MAX_SG];
179 struct sginfo t_dst[MAX_SG];
180 u32 p_key;
181 u32 p_iv;
182 u32 method;
183 u32 op_mode;
184 u32 op_dir;
185 int flow;
186 unsigned int ivlen;
187 unsigned int keylen;
188 void *biv;
189 struct skcipher_request fallback_req;
190};
191
192
193
194
195
196
197
198
199
200
201
202struct sun8i_cipher_tfm_ctx {
203 struct crypto_engine_ctx enginectx;
204 u32 *key;
205 u32 keylen;
206 struct sun8i_ss_dev *ss;
207 struct crypto_skcipher *fallback_tfm;
208};
209
210
211
212
213
214
215struct sun8i_ss_rng_tfm_ctx {
216 void *seed;
217 unsigned int slen;
218};
219
220
221
222
223
224
225
226
227
228struct sun8i_ss_hash_tfm_ctx {
229 struct crypto_engine_ctx enginectx;
230 struct crypto_ahash *fallback_tfm;
231 struct sun8i_ss_dev *ss;
232};
233
234
235
236
237
238
239
240
241
242struct sun8i_ss_hash_reqctx {
243 struct sginfo t_src[MAX_SG];
244 struct sginfo t_dst[MAX_SG];
245 struct ahash_request fallback_req;
246 u32 method;
247 int flow;
248};
249
250
251
252
253
254
255
256
257
258
259
260
261
262struct sun8i_ss_alg_template {
263 u32 type;
264 u32 ss_algo_id;
265 u32 ss_blockmode;
266 struct sun8i_ss_dev *ss;
267 union {
268 struct skcipher_alg skcipher;
269 struct rng_alg rng;
270 struct ahash_alg hash;
271 } alg;
272#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
273 unsigned long stat_req;
274 unsigned long stat_fb;
275 unsigned long stat_bytes;
276#endif
277};
278
279int sun8i_ss_enqueue(struct crypto_async_request *areq, u32 type);
280
281int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
282 unsigned int keylen);
283int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
284 unsigned int keylen);
285int sun8i_ss_cipher_init(struct crypto_tfm *tfm);
286void sun8i_ss_cipher_exit(struct crypto_tfm *tfm);
287int sun8i_ss_skdecrypt(struct skcipher_request *areq);
288int sun8i_ss_skencrypt(struct skcipher_request *areq);
289
290int sun8i_ss_get_engine_number(struct sun8i_ss_dev *ss);
291
292int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx, const char *name);
293int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
294 unsigned int slen, u8 *dst, unsigned int dlen);
295int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
296int sun8i_ss_prng_init(struct crypto_tfm *tfm);
297void sun8i_ss_prng_exit(struct crypto_tfm *tfm);
298
299int sun8i_ss_hash_crainit(struct crypto_tfm *tfm);
300void sun8i_ss_hash_craexit(struct crypto_tfm *tfm);
301int sun8i_ss_hash_init(struct ahash_request *areq);
302int sun8i_ss_hash_export(struct ahash_request *areq, void *out);
303int sun8i_ss_hash_import(struct ahash_request *areq, const void *in);
304int sun8i_ss_hash_final(struct ahash_request *areq);
305int sun8i_ss_hash_update(struct ahash_request *areq);
306int sun8i_ss_hash_finup(struct ahash_request *areq);
307int sun8i_ss_hash_digest(struct ahash_request *areq);
308int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq);
309