crypto: cryptd - move kcrypto_wq into cryptd
[platform/kernel/linux-rpi.git] / drivers / crypto / cavium / cpt / cptvf_algs.c
1
2 /*
3  * Copyright (C) 2016 Cavium, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 #include <crypto/aes.h>
11 #include <crypto/algapi.h>
12 #include <crypto/authenc.h>
13 #include <crypto/des.h>
14 #include <crypto/xts.h>
15 #include <linux/crypto.h>
16 #include <linux/err.h>
17 #include <linux/list.h>
18 #include <linux/scatterlist.h>
19
20 #include "cptvf.h"
21 #include "cptvf_algs.h"
22
23 struct cpt_device_handle {
24         void *cdev[MAX_DEVICES];
25         u32 dev_count;
26 };
27
28 static struct cpt_device_handle dev_handle;
29
30 static void cvm_callback(u32 status, void *arg)
31 {
32         struct crypto_async_request *req = (struct crypto_async_request *)arg;
33
34         req->complete(req, !status);
35 }
36
37 static inline void update_input_iv(struct cpt_request_info *req_info,
38                                    u8 *iv, u32 enc_iv_len,
39                                    u32 *argcnt)
40 {
41         /* Setting the iv information */
42         req_info->in[*argcnt].vptr = (void *)iv;
43         req_info->in[*argcnt].size = enc_iv_len;
44         req_info->req.dlen += enc_iv_len;
45
46         ++(*argcnt);
47 }
48
49 static inline void update_output_iv(struct cpt_request_info *req_info,
50                                     u8 *iv, u32 enc_iv_len,
51                                     u32 *argcnt)
52 {
53         /* Setting the iv information */
54         req_info->out[*argcnt].vptr = (void *)iv;
55         req_info->out[*argcnt].size = enc_iv_len;
56         req_info->rlen += enc_iv_len;
57
58         ++(*argcnt);
59 }
60
61 static inline void update_input_data(struct cpt_request_info *req_info,
62                                      struct scatterlist *inp_sg,
63                                      u32 nbytes, u32 *argcnt)
64 {
65         req_info->req.dlen += nbytes;
66
67         while (nbytes) {
68                 u32 len = min(nbytes, inp_sg->length);
69                 u8 *ptr = sg_virt(inp_sg);
70
71                 req_info->in[*argcnt].vptr = (void *)ptr;
72                 req_info->in[*argcnt].size = len;
73                 nbytes -= len;
74
75                 ++(*argcnt);
76                 ++inp_sg;
77         }
78 }
79
80 static inline void update_output_data(struct cpt_request_info *req_info,
81                                       struct scatterlist *outp_sg,
82                                       u32 nbytes, u32 *argcnt)
83 {
84         req_info->rlen += nbytes;
85
86         while (nbytes) {
87                 u32 len = min(nbytes, outp_sg->length);
88                 u8 *ptr = sg_virt(outp_sg);
89
90                 req_info->out[*argcnt].vptr = (void *)ptr;
91                 req_info->out[*argcnt].size = len;
92                 nbytes -= len;
93                 ++(*argcnt);
94                 ++outp_sg;
95         }
96 }
97
98 static inline u32 create_ctx_hdr(struct ablkcipher_request *req, u32 enc,
99                                  u32 *argcnt)
100 {
101         struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
102         struct cvm_enc_ctx *ctx = crypto_ablkcipher_ctx(tfm);
103         struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
104         struct fc_context *fctx = &rctx->fctx;
105         u64 *offset_control = &rctx->control_word;
106         u32 enc_iv_len = crypto_ablkcipher_ivsize(tfm);
107         struct cpt_request_info *req_info = &rctx->cpt_req;
108         u64 *ctrl_flags = NULL;
109
110         req_info->ctrl.s.grp = 0;
111         req_info->ctrl.s.dma_mode = DMA_GATHER_SCATTER;
112         req_info->ctrl.s.se_req = SE_CORE_REQ;
113
114         req_info->req.opcode.s.major = MAJOR_OP_FC |
115                                         DMA_MODE_FLAG(DMA_GATHER_SCATTER);
116         if (enc)
117                 req_info->req.opcode.s.minor = 2;
118         else
119                 req_info->req.opcode.s.minor = 3;
120
121         req_info->req.param1 = req->nbytes; /* Encryption Data length */
122         req_info->req.param2 = 0; /*Auth data length */
123
124         fctx->enc.enc_ctrl.e.enc_cipher = ctx->cipher_type;
125         fctx->enc.enc_ctrl.e.aes_key = ctx->key_type;
126         fctx->enc.enc_ctrl.e.iv_source = FROM_DPTR;
127
128         if (ctx->cipher_type == AES_XTS)
129                 memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len * 2);
130         else
131                 memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len);
132         ctrl_flags = (u64 *)&fctx->enc.enc_ctrl.flags;
133         *ctrl_flags = cpu_to_be64(*ctrl_flags);
134
135         *offset_control = cpu_to_be64(((u64)(enc_iv_len) << 16));
136         /* Storing  Packet Data Information in offset
137          * Control Word First 8 bytes
138          */
139         req_info->in[*argcnt].vptr = (u8 *)offset_control;
140         req_info->in[*argcnt].size = CONTROL_WORD_LEN;
141         req_info->req.dlen += CONTROL_WORD_LEN;
142         ++(*argcnt);
143
144         req_info->in[*argcnt].vptr = (u8 *)fctx;
145         req_info->in[*argcnt].size = sizeof(struct fc_context);
146         req_info->req.dlen += sizeof(struct fc_context);
147
148         ++(*argcnt);
149
150         return 0;
151 }
152
153 static inline u32 create_input_list(struct ablkcipher_request  *req, u32 enc,
154                                     u32 enc_iv_len)
155 {
156         struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
157         struct cpt_request_info *req_info = &rctx->cpt_req;
158         u32 argcnt =  0;
159
160         create_ctx_hdr(req, enc, &argcnt);
161         update_input_iv(req_info, req->info, enc_iv_len, &argcnt);
162         update_input_data(req_info, req->src, req->nbytes, &argcnt);
163         req_info->incnt = argcnt;
164
165         return 0;
166 }
167
168 static inline void store_cb_info(struct ablkcipher_request *req,
169                                  struct cpt_request_info *req_info)
170 {
171         req_info->callback = (void *)cvm_callback;
172         req_info->callback_arg = (void *)&req->base;
173 }
174
175 static inline void create_output_list(struct ablkcipher_request *req,
176                                       u32 enc_iv_len)
177 {
178         struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
179         struct cpt_request_info *req_info = &rctx->cpt_req;
180         u32 argcnt = 0;
181
182         /* OUTPUT Buffer Processing
183          * AES encryption/decryption output would be
184          * received in the following format
185          *
186          * ------IV--------|------ENCRYPTED/DECRYPTED DATA-----|
187          * [ 16 Bytes/     [   Request Enc/Dec/ DATA Len AES CBC ]
188          */
189         /* Reading IV information */
190         update_output_iv(req_info, req->info, enc_iv_len, &argcnt);
191         update_output_data(req_info, req->dst, req->nbytes, &argcnt);
192         req_info->outcnt = argcnt;
193 }
194
195 static inline int cvm_enc_dec(struct ablkcipher_request *req, u32 enc)
196 {
197         struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
198         struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
199         u32 enc_iv_len = crypto_ablkcipher_ivsize(tfm);
200         struct fc_context *fctx = &rctx->fctx;
201         struct cpt_request_info *req_info = &rctx->cpt_req;
202         void *cdev = NULL;
203         int status;
204
205         memset(req_info, 0, sizeof(struct cpt_request_info));
206         memset(fctx, 0, sizeof(struct fc_context));
207         create_input_list(req, enc, enc_iv_len);
208         create_output_list(req, enc_iv_len);
209         store_cb_info(req, req_info);
210         cdev = dev_handle.cdev[smp_processor_id()];
211         status = cptvf_do_request(cdev, req_info);
212         /* We perform an asynchronous send and once
213          * the request is completed the driver would
214          * intimate through  registered call back functions
215          */
216
217         if (status)
218                 return status;
219         else
220                 return -EINPROGRESS;
221 }
222
223 static int cvm_encrypt(struct ablkcipher_request *req)
224 {
225         return cvm_enc_dec(req, true);
226 }
227
228 static int cvm_decrypt(struct ablkcipher_request *req)
229 {
230         return cvm_enc_dec(req, false);
231 }
232
233 static int cvm_xts_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
234                    u32 keylen)
235 {
236         struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
237         struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
238         int err;
239         const u8 *key1 = key;
240         const u8 *key2 = key + (keylen / 2);
241
242         err = xts_check_key(tfm, key, keylen);
243         if (err)
244                 return err;
245         ctx->key_len = keylen;
246         memcpy(ctx->enc_key, key1, keylen / 2);
247         memcpy(ctx->enc_key + KEY2_OFFSET, key2, keylen / 2);
248         ctx->cipher_type = AES_XTS;
249         switch (ctx->key_len) {
250         case 32:
251                 ctx->key_type = AES_128_BIT;
252                 break;
253         case 64:
254                 ctx->key_type = AES_256_BIT;
255                 break;
256         default:
257                 return -EINVAL;
258         }
259
260         return 0;
261 }
262
263 static int cvm_validate_keylen(struct cvm_enc_ctx *ctx, u32 keylen)
264 {
265         if ((keylen == 16) || (keylen == 24) || (keylen == 32)) {
266                 ctx->key_len = keylen;
267                 switch (ctx->key_len) {
268                 case 16:
269                         ctx->key_type = AES_128_BIT;
270                         break;
271                 case 24:
272                         ctx->key_type = AES_192_BIT;
273                         break;
274                 case 32:
275                         ctx->key_type = AES_256_BIT;
276                         break;
277                 default:
278                         return -EINVAL;
279                 }
280
281                 if (ctx->cipher_type == DES3_CBC)
282                         ctx->key_type = 0;
283
284                 return 0;
285         }
286
287         return -EINVAL;
288 }
289
290 static int cvm_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
291                       u32 keylen, u8 cipher_type)
292 {
293         struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
294         struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
295
296         ctx->cipher_type = cipher_type;
297         if (!cvm_validate_keylen(ctx, keylen)) {
298                 memcpy(ctx->enc_key, key, keylen);
299                 return 0;
300         } else {
301                 crypto_ablkcipher_set_flags(cipher,
302                                             CRYPTO_TFM_RES_BAD_KEY_LEN);
303                 return -EINVAL;
304         }
305 }
306
307 static int cvm_cbc_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
308                               u32 keylen)
309 {
310         return cvm_setkey(cipher, key, keylen, AES_CBC);
311 }
312
313 static int cvm_ecb_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
314                               u32 keylen)
315 {
316         return cvm_setkey(cipher, key, keylen, AES_ECB);
317 }
318
319 static int cvm_cfb_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
320                               u32 keylen)
321 {
322         return cvm_setkey(cipher, key, keylen, AES_CFB);
323 }
324
325 static int cvm_cbc_des3_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
326                                u32 keylen)
327 {
328         u32 flags = crypto_ablkcipher_get_flags(cipher);
329         int err;
330
331         err = __des3_verify_key(&flags, key);
332         if (unlikely(err)) {
333                 crypto_ablkcipher_set_flags(cipher, flags);
334                 return err;
335         }
336
337         return cvm_setkey(cipher, key, keylen, DES3_CBC);
338 }
339
340 static int cvm_ecb_des3_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
341                                u32 keylen)
342 {
343         u32 flags = crypto_ablkcipher_get_flags(cipher);
344         int err;
345
346         err = __des3_verify_key(&flags, key);
347         if (unlikely(err)) {
348                 crypto_ablkcipher_set_flags(cipher, flags);
349                 return err;
350         }
351
352         return cvm_setkey(cipher, key, keylen, DES3_ECB);
353 }
354
355 static int cvm_enc_dec_init(struct crypto_tfm *tfm)
356 {
357         tfm->crt_ablkcipher.reqsize = sizeof(struct cvm_req_ctx);
358         return 0;
359 }
360
361 static struct crypto_alg algs[] = { {
362         .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
363         .cra_blocksize = AES_BLOCK_SIZE,
364         .cra_ctxsize = sizeof(struct cvm_enc_ctx),
365         .cra_alignmask = 7,
366         .cra_priority = 4001,
367         .cra_name = "xts(aes)",
368         .cra_driver_name = "cavium-xts-aes",
369         .cra_type = &crypto_ablkcipher_type,
370         .cra_u = {
371                 .ablkcipher = {
372                         .ivsize = AES_BLOCK_SIZE,
373                         .min_keysize = 2 * AES_MIN_KEY_SIZE,
374                         .max_keysize = 2 * AES_MAX_KEY_SIZE,
375                         .setkey = cvm_xts_setkey,
376                         .encrypt = cvm_encrypt,
377                         .decrypt = cvm_decrypt,
378                 },
379         },
380         .cra_init = cvm_enc_dec_init,
381         .cra_module = THIS_MODULE,
382 }, {
383         .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
384         .cra_blocksize = AES_BLOCK_SIZE,
385         .cra_ctxsize = sizeof(struct cvm_enc_ctx),
386         .cra_alignmask = 7,
387         .cra_priority = 4001,
388         .cra_name = "cbc(aes)",
389         .cra_driver_name = "cavium-cbc-aes",
390         .cra_type = &crypto_ablkcipher_type,
391         .cra_u = {
392                 .ablkcipher = {
393                         .ivsize = AES_BLOCK_SIZE,
394                         .min_keysize = AES_MIN_KEY_SIZE,
395                         .max_keysize = AES_MAX_KEY_SIZE,
396                         .setkey = cvm_cbc_aes_setkey,
397                         .encrypt = cvm_encrypt,
398                         .decrypt = cvm_decrypt,
399                 },
400         },
401         .cra_init = cvm_enc_dec_init,
402         .cra_module = THIS_MODULE,
403 }, {
404         .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
405         .cra_blocksize = AES_BLOCK_SIZE,
406         .cra_ctxsize = sizeof(struct cvm_enc_ctx),
407         .cra_alignmask = 7,
408         .cra_priority = 4001,
409         .cra_name = "ecb(aes)",
410         .cra_driver_name = "cavium-ecb-aes",
411         .cra_type = &crypto_ablkcipher_type,
412         .cra_u = {
413                 .ablkcipher = {
414                         .ivsize = AES_BLOCK_SIZE,
415                         .min_keysize = AES_MIN_KEY_SIZE,
416                         .max_keysize = AES_MAX_KEY_SIZE,
417                         .setkey = cvm_ecb_aes_setkey,
418                         .encrypt = cvm_encrypt,
419                         .decrypt = cvm_decrypt,
420                 },
421         },
422         .cra_init = cvm_enc_dec_init,
423         .cra_module = THIS_MODULE,
424 }, {
425         .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
426         .cra_blocksize = AES_BLOCK_SIZE,
427         .cra_ctxsize = sizeof(struct cvm_enc_ctx),
428         .cra_alignmask = 7,
429         .cra_priority = 4001,
430         .cra_name = "cfb(aes)",
431         .cra_driver_name = "cavium-cfb-aes",
432         .cra_type = &crypto_ablkcipher_type,
433         .cra_u = {
434                 .ablkcipher = {
435                         .ivsize = AES_BLOCK_SIZE,
436                         .min_keysize = AES_MIN_KEY_SIZE,
437                         .max_keysize = AES_MAX_KEY_SIZE,
438                         .setkey = cvm_cfb_aes_setkey,
439                         .encrypt = cvm_encrypt,
440                         .decrypt = cvm_decrypt,
441                 },
442         },
443         .cra_init = cvm_enc_dec_init,
444         .cra_module = THIS_MODULE,
445 }, {
446         .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
447         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
448         .cra_ctxsize = sizeof(struct cvm_des3_ctx),
449         .cra_alignmask = 7,
450         .cra_priority = 4001,
451         .cra_name = "cbc(des3_ede)",
452         .cra_driver_name = "cavium-cbc-des3_ede",
453         .cra_type = &crypto_ablkcipher_type,
454         .cra_u = {
455                 .ablkcipher = {
456                         .min_keysize = DES3_EDE_KEY_SIZE,
457                         .max_keysize = DES3_EDE_KEY_SIZE,
458                         .ivsize = DES_BLOCK_SIZE,
459                         .setkey = cvm_cbc_des3_setkey,
460                         .encrypt = cvm_encrypt,
461                         .decrypt = cvm_decrypt,
462                 },
463         },
464         .cra_init = cvm_enc_dec_init,
465         .cra_module = THIS_MODULE,
466 }, {
467         .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
468         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
469         .cra_ctxsize = sizeof(struct cvm_des3_ctx),
470         .cra_alignmask = 7,
471         .cra_priority = 4001,
472         .cra_name = "ecb(des3_ede)",
473         .cra_driver_name = "cavium-ecb-des3_ede",
474         .cra_type = &crypto_ablkcipher_type,
475         .cra_u = {
476                 .ablkcipher = {
477                         .min_keysize = DES3_EDE_KEY_SIZE,
478                         .max_keysize = DES3_EDE_KEY_SIZE,
479                         .ivsize = DES_BLOCK_SIZE,
480                         .setkey = cvm_ecb_des3_setkey,
481                         .encrypt = cvm_encrypt,
482                         .decrypt = cvm_decrypt,
483                 },
484         },
485         .cra_init = cvm_enc_dec_init,
486         .cra_module = THIS_MODULE,
487 } };
488
489 static inline int cav_register_algs(void)
490 {
491         int err = 0;
492
493         err = crypto_register_algs(algs, ARRAY_SIZE(algs));
494         if (err)
495                 return err;
496
497         return 0;
498 }
499
500 static inline void cav_unregister_algs(void)
501 {
502         crypto_unregister_algs(algs, ARRAY_SIZE(algs));
503 }
504
505 int cvm_crypto_init(struct cpt_vf *cptvf)
506 {
507         struct pci_dev *pdev = cptvf->pdev;
508         u32 dev_count;
509
510         dev_count = dev_handle.dev_count;
511         dev_handle.cdev[dev_count] = cptvf;
512         dev_handle.dev_count++;
513
514         if (dev_count == 3) {
515                 if (cav_register_algs()) {
516                         dev_err(&pdev->dev, "Error in registering crypto algorithms\n");
517                         return -EINVAL;
518                 }
519         }
520
521         return 0;
522 }
523
524 void cvm_crypto_exit(void)
525 {
526         u32 dev_count;
527
528         dev_count = --dev_handle.dev_count;
529         if (!dev_count)
530                 cav_unregister_algs();
531 }