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