GNU Linux-libre 4.14.262-gnu1
[releases.git] / drivers / crypto / sunxi-ss / sun4i-ss-cipher.c
1 /*
2  * sun4i-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC
3  *
4  * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
5  *
6  * This file add support for AES cipher with 128,192,256 bits
7  * keysize in CBC and ECB mode.
8  * Add support also for DES and 3DES in CBC and ECB mode.
9  *
10  * You could find the datasheet in Documentation/arm/sunxi/README
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  */
17 #include "sun4i-ss.h"
18
19 static int sun4i_ss_opti_poll(struct skcipher_request *areq)
20 {
21         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
22         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
23         struct sun4i_ss_ctx *ss = op->ss;
24         unsigned int ivsize = crypto_skcipher_ivsize(tfm);
25         struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
26         u32 mode = ctx->mode;
27         /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
28         u32 rx_cnt = SS_RX_DEFAULT;
29         u32 tx_cnt = 0;
30         u32 spaces;
31         u32 v;
32         int err = 0;
33         unsigned int i;
34         unsigned int ileft = areq->cryptlen;
35         unsigned int oleft = areq->cryptlen;
36         unsigned int todo;
37         unsigned long pi = 0, po = 0; /* progress for in and out */
38         bool miter_err;
39         struct sg_mapping_iter mi, mo;
40         unsigned int oi, oo; /* offset for in and out */
41         unsigned long flags;
42
43         if (!areq->cryptlen)
44                 return 0;
45
46         if (!areq->iv) {
47                 dev_err_ratelimited(ss->dev, "ERROR: Empty IV\n");
48                 return -EINVAL;
49         }
50
51         if (!areq->src || !areq->dst) {
52                 dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
53                 return -EINVAL;
54         }
55
56         spin_lock_irqsave(&ss->slock, flags);
57
58         for (i = 0; i < op->keylen / 4; i++)
59                 writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
60
61         if (areq->iv) {
62                 for (i = 0; i < 4 && i < ivsize / 4; i++) {
63                         v = *(u32 *)(areq->iv + i * 4);
64                         writesl(ss->base + SS_IV0 + i * 4, &v, 1);
65                 }
66         }
67         writel(mode, ss->base + SS_CTL);
68
69
70         ileft = areq->cryptlen / 4;
71         oleft = areq->cryptlen / 4;
72         oi = 0;
73         oo = 0;
74         do {
75                 if (ileft) {
76                         sg_miter_start(&mi, areq->src, sg_nents(areq->src),
77                                         SG_MITER_FROM_SG | SG_MITER_ATOMIC);
78                         if (pi)
79                                 sg_miter_skip(&mi, pi);
80                         miter_err = sg_miter_next(&mi);
81                         if (!miter_err || !mi.addr) {
82                                 dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
83                                 err = -EINVAL;
84                                 goto release_ss;
85                         }
86                         todo = min(rx_cnt, ileft);
87                         todo = min_t(size_t, todo, (mi.length - oi) / 4);
88                         if (todo) {
89                                 ileft -= todo;
90                                 writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
91                                 oi += todo * 4;
92                         }
93                         if (oi == mi.length) {
94                                 pi += mi.length;
95                                 oi = 0;
96                         }
97                         sg_miter_stop(&mi);
98                 }
99
100                 spaces = readl(ss->base + SS_FCSR);
101                 rx_cnt = SS_RXFIFO_SPACES(spaces);
102                 tx_cnt = SS_TXFIFO_SPACES(spaces);
103
104                 sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
105                                SG_MITER_TO_SG | SG_MITER_ATOMIC);
106                 if (po)
107                         sg_miter_skip(&mo, po);
108                 miter_err = sg_miter_next(&mo);
109                 if (!miter_err || !mo.addr) {
110                         dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
111                         err = -EINVAL;
112                         goto release_ss;
113                 }
114                 todo = min(tx_cnt, oleft);
115                 todo = min_t(size_t, todo, (mo.length - oo) / 4);
116                 if (todo) {
117                         oleft -= todo;
118                         readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
119                         oo += todo * 4;
120                 }
121                 if (oo == mo.length) {
122                         oo = 0;
123                         po += mo.length;
124                 }
125                 sg_miter_stop(&mo);
126         } while (oleft);
127
128         if (areq->iv) {
129                 for (i = 0; i < 4 && i < ivsize / 4; i++) {
130                         v = readl(ss->base + SS_IV0 + i * 4);
131                         *(u32 *)(areq->iv + i * 4) = v;
132                 }
133         }
134
135 release_ss:
136         writel(0, ss->base + SS_CTL);
137         spin_unlock_irqrestore(&ss->slock, flags);
138         return err;
139 }
140
141 /* Generic function that support SG with size not multiple of 4 */
142 static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
143 {
144         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
145         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
146         struct sun4i_ss_ctx *ss = op->ss;
147         int no_chunk = 1;
148         struct scatterlist *in_sg = areq->src;
149         struct scatterlist *out_sg = areq->dst;
150         unsigned int ivsize = crypto_skcipher_ivsize(tfm);
151         struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
152         u32 mode = ctx->mode;
153         /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
154         u32 rx_cnt = SS_RX_DEFAULT;
155         u32 tx_cnt = 0;
156         u32 v;
157         u32 spaces;
158         int err = 0;
159         unsigned int i;
160         unsigned int ileft = areq->cryptlen;
161         unsigned int oleft = areq->cryptlen;
162         unsigned int todo;
163         struct sg_mapping_iter mi, mo;
164         unsigned long pi = 0, po = 0; /* progress for in and out */
165         bool miter_err;
166         unsigned int oi, oo;    /* offset for in and out */
167         char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
168         char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
169         unsigned int ob = 0;    /* offset in buf */
170         unsigned int obo = 0;   /* offset in bufo*/
171         unsigned int obl = 0;   /* length of data in bufo */
172         unsigned long flags;
173
174         if (!areq->cryptlen)
175                 return 0;
176
177         if (!areq->iv) {
178                 dev_err_ratelimited(ss->dev, "ERROR: Empty IV\n");
179                 return -EINVAL;
180         }
181
182         if (!areq->src || !areq->dst) {
183                 dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
184                 return -EINVAL;
185         }
186
187         /*
188          * if we have only SGs with size multiple of 4,
189          * we can use the SS optimized function
190          */
191         while (in_sg && no_chunk == 1) {
192                 if ((in_sg->length | in_sg->offset) & 3u)
193                         no_chunk = 0;
194                 in_sg = sg_next(in_sg);
195         }
196         while (out_sg && no_chunk == 1) {
197                 if ((out_sg->length | out_sg->offset) & 3u)
198                         no_chunk = 0;
199                 out_sg = sg_next(out_sg);
200         }
201
202         if (no_chunk == 1)
203                 return sun4i_ss_opti_poll(areq);
204
205         spin_lock_irqsave(&ss->slock, flags);
206
207         for (i = 0; i < op->keylen / 4; i++)
208                 writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
209
210         if (areq->iv) {
211                 for (i = 0; i < 4 && i < ivsize / 4; i++) {
212                         v = *(u32 *)(areq->iv + i * 4);
213                         writesl(ss->base + SS_IV0 + i * 4, &v, 1);
214                 }
215         }
216         writel(mode, ss->base + SS_CTL);
217
218         ileft = areq->cryptlen;
219         oleft = areq->cryptlen;
220         oi = 0;
221         oo = 0;
222
223         while (oleft) {
224                 if (ileft) {
225                         sg_miter_start(&mi, areq->src, sg_nents(areq->src),
226                                        SG_MITER_FROM_SG | SG_MITER_ATOMIC);
227                         if (pi)
228                                 sg_miter_skip(&mi, pi);
229                         miter_err = sg_miter_next(&mi);
230                         if (!miter_err || !mi.addr) {
231                                 dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
232                                 err = -EINVAL;
233                                 goto release_ss;
234                         }
235                         /*
236                          * todo is the number of consecutive 4byte word that we
237                          * can read from current SG
238                          */
239                         todo = min(rx_cnt, ileft / 4);
240                         todo = min_t(size_t, todo, (mi.length - oi) / 4);
241                         if (todo && !ob) {
242                                 writesl(ss->base + SS_RXFIFO, mi.addr + oi,
243                                         todo);
244                                 ileft -= todo * 4;
245                                 oi += todo * 4;
246                         } else {
247                                 /*
248                                  * not enough consecutive bytes, so we need to
249                                  * linearize in buf. todo is in bytes
250                                  * After that copy, if we have a multiple of 4
251                                  * we need to be able to write all buf in one
252                                  * pass, so it is why we min() with rx_cnt
253                                  */
254                                 todo = min(rx_cnt * 4 - ob, ileft);
255                                 todo = min_t(size_t, todo, mi.length - oi);
256                                 memcpy(buf + ob, mi.addr + oi, todo);
257                                 ileft -= todo;
258                                 oi += todo;
259                                 ob += todo;
260                                 if (!(ob % 4)) {
261                                         writesl(ss->base + SS_RXFIFO, buf,
262                                                 ob / 4);
263                                         ob = 0;
264                                 }
265                         }
266                         if (oi == mi.length) {
267                                 pi += mi.length;
268                                 oi = 0;
269                         }
270                         sg_miter_stop(&mi);
271                 }
272
273                 spaces = readl(ss->base + SS_FCSR);
274                 rx_cnt = SS_RXFIFO_SPACES(spaces);
275                 tx_cnt = SS_TXFIFO_SPACES(spaces);
276
277                 if (!tx_cnt)
278                         continue;
279                 sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
280                                SG_MITER_TO_SG | SG_MITER_ATOMIC);
281                 if (po)
282                         sg_miter_skip(&mo, po);
283                 miter_err = sg_miter_next(&mo);
284                 if (!miter_err || !mo.addr) {
285                         dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
286                         err = -EINVAL;
287                         goto release_ss;
288                 }
289                 /* todo in 4bytes word */
290                 todo = min(tx_cnt, oleft / 4);
291                 todo = min_t(size_t, todo, (mo.length - oo) / 4);
292
293                 if (todo) {
294                         readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
295                         oleft -= todo * 4;
296                         oo += todo * 4;
297                         if (oo == mo.length) {
298                                 po += mo.length;
299                                 oo = 0;
300                         }
301                 } else {
302                         /*
303                          * read obl bytes in bufo, we read at maximum for
304                          * emptying the device
305                          */
306                         readsl(ss->base + SS_TXFIFO, bufo, tx_cnt);
307                         obl = tx_cnt * 4;
308                         obo = 0;
309                         do {
310                                 /*
311                                  * how many bytes we can copy ?
312                                  * no more than remaining SG size
313                                  * no more than remaining buffer
314                                  * no need to test against oleft
315                                  */
316                                 todo = min_t(size_t,
317                                              mo.length - oo, obl - obo);
318                                 memcpy(mo.addr + oo, bufo + obo, todo);
319                                 oleft -= todo;
320                                 obo += todo;
321                                 oo += todo;
322                                 if (oo == mo.length) {
323                                         po += mo.length;
324                                         sg_miter_next(&mo);
325                                         oo = 0;
326                                 }
327                         } while (obo < obl);
328                         /* bufo must be fully used here */
329                 }
330                 sg_miter_stop(&mo);
331         }
332         if (areq->iv) {
333                 for (i = 0; i < 4 && i < ivsize / 4; i++) {
334                         v = readl(ss->base + SS_IV0 + i * 4);
335                         *(u32 *)(areq->iv + i * 4) = v;
336                 }
337         }
338
339 release_ss:
340         writel(0, ss->base + SS_CTL);
341         spin_unlock_irqrestore(&ss->slock, flags);
342
343         return err;
344 }
345
346 /* CBC AES */
347 int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq)
348 {
349         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
350         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
351         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
352
353         rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
354                 op->keymode;
355         return sun4i_ss_cipher_poll(areq);
356 }
357
358 int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq)
359 {
360         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
361         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
362         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
363
364         rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
365                 op->keymode;
366         return sun4i_ss_cipher_poll(areq);
367 }
368
369 /* ECB AES */
370 int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq)
371 {
372         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
373         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
374         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
375
376         rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
377                 op->keymode;
378         return sun4i_ss_cipher_poll(areq);
379 }
380
381 int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq)
382 {
383         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
384         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
385         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
386
387         rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
388                 op->keymode;
389         return sun4i_ss_cipher_poll(areq);
390 }
391
392 /* CBC DES */
393 int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq)
394 {
395         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
396         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
397         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
398
399         rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
400                 op->keymode;
401         return sun4i_ss_cipher_poll(areq);
402 }
403
404 int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq)
405 {
406         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
407         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
408         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
409
410         rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
411                 op->keymode;
412         return sun4i_ss_cipher_poll(areq);
413 }
414
415 /* ECB DES */
416 int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq)
417 {
418         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
419         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
420         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
421
422         rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
423                 op->keymode;
424         return sun4i_ss_cipher_poll(areq);
425 }
426
427 int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq)
428 {
429         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
430         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
431         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
432
433         rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
434                 op->keymode;
435         return sun4i_ss_cipher_poll(areq);
436 }
437
438 /* CBC 3DES */
439 int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq)
440 {
441         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
442         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
443         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
444
445         rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
446                 op->keymode;
447         return sun4i_ss_cipher_poll(areq);
448 }
449
450 int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq)
451 {
452         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
453         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
454         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
455
456         rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
457                 op->keymode;
458         return sun4i_ss_cipher_poll(areq);
459 }
460
461 /* ECB 3DES */
462 int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq)
463 {
464         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
465         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
466         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
467
468         rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
469                 op->keymode;
470         return sun4i_ss_cipher_poll(areq);
471 }
472
473 int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq)
474 {
475         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
476         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
477         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
478
479         rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
480                 op->keymode;
481         return sun4i_ss_cipher_poll(areq);
482 }
483
484 int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
485 {
486         struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
487         struct sun4i_ss_alg_template *algt;
488
489         memset(op, 0, sizeof(struct sun4i_tfm_ctx));
490
491         algt = container_of(tfm->__crt_alg, struct sun4i_ss_alg_template,
492                             alg.crypto.base);
493         op->ss = algt->ss;
494
495         crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
496                                     sizeof(struct sun4i_cipher_req_ctx));
497
498         return 0;
499 }
500
501 /* check and set the AES key, prepare the mode to be used */
502 int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
503                         unsigned int keylen)
504 {
505         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
506         struct sun4i_ss_ctx *ss = op->ss;
507
508         switch (keylen) {
509         case 128 / 8:
510                 op->keymode = SS_AES_128BITS;
511                 break;
512         case 192 / 8:
513                 op->keymode = SS_AES_192BITS;
514                 break;
515         case 256 / 8:
516                 op->keymode = SS_AES_256BITS;
517                 break;
518         default:
519                 dev_err(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
520                 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
521                 return -EINVAL;
522         }
523         op->keylen = keylen;
524         memcpy(op->key, key, keylen);
525         return 0;
526 }
527
528 /* check and set the DES key, prepare the mode to be used */
529 int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
530                         unsigned int keylen)
531 {
532         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
533         struct sun4i_ss_ctx *ss = op->ss;
534         u32 flags;
535         u32 tmp[DES_EXPKEY_WORDS];
536         int ret;
537
538         if (unlikely(keylen != DES_KEY_SIZE)) {
539                 dev_err(ss->dev, "Invalid keylen %u\n", keylen);
540                 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
541                 return -EINVAL;
542         }
543
544         flags = crypto_skcipher_get_flags(tfm);
545
546         ret = des_ekey(tmp, key);
547         if (unlikely(!ret) && (flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
548                 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
549                 dev_dbg(ss->dev, "Weak key %u\n", keylen);
550                 return -EINVAL;
551         }
552
553         op->keylen = keylen;
554         memcpy(op->key, key, keylen);
555         return 0;
556 }
557
558 /* check and set the 3DES key, prepare the mode to be used */
559 int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
560                          unsigned int keylen)
561 {
562         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
563         struct sun4i_ss_ctx *ss = op->ss;
564
565         if (unlikely(keylen != 3 * DES_KEY_SIZE)) {
566                 dev_err(ss->dev, "Invalid keylen %u\n", keylen);
567                 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
568                 return -EINVAL;
569         }
570         op->keylen = keylen;
571         memcpy(op->key, key, keylen);
572         return 0;
573 }