3 * Copyright (C) 2016 Cavium, Inc.
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.
10 #include <crypto/aes.h>
11 #include <crypto/algapi.h>
12 #include <crypto/authenc.h>
13 #include <crypto/cryptd.h>
14 #include <crypto/crypto_wq.h>
15 #include <crypto/des.h>
16 #include <crypto/xts.h>
17 #include <linux/crypto.h>
18 #include <linux/err.h>
19 #include <linux/list.h>
20 #include <linux/scatterlist.h>
23 #include "cptvf_algs.h"
25 struct cpt_device_handle {
26 void *cdev[MAX_DEVICES];
30 static struct cpt_device_handle dev_handle;
32 static void cvm_callback(u32 status, void *arg)
34 struct crypto_async_request *req = (struct crypto_async_request *)arg;
36 req->complete(req, !status);
39 static inline void update_input_iv(struct cpt_request_info *req_info,
40 u8 *iv, u32 enc_iv_len,
43 /* Setting the iv information */
44 req_info->in[*argcnt].vptr = (void *)iv;
45 req_info->in[*argcnt].size = enc_iv_len;
46 req_info->req.dlen += enc_iv_len;
51 static inline void update_output_iv(struct cpt_request_info *req_info,
52 u8 *iv, u32 enc_iv_len,
55 /* Setting the iv information */
56 req_info->out[*argcnt].vptr = (void *)iv;
57 req_info->out[*argcnt].size = enc_iv_len;
58 req_info->rlen += enc_iv_len;
63 static inline void update_input_data(struct cpt_request_info *req_info,
64 struct scatterlist *inp_sg,
65 u32 nbytes, u32 *argcnt)
67 req_info->req.dlen += nbytes;
70 u32 len = min(nbytes, inp_sg->length);
71 u8 *ptr = sg_virt(inp_sg);
73 req_info->in[*argcnt].vptr = (void *)ptr;
74 req_info->in[*argcnt].size = len;
82 static inline void update_output_data(struct cpt_request_info *req_info,
83 struct scatterlist *outp_sg,
84 u32 nbytes, u32 *argcnt)
86 req_info->rlen += nbytes;
89 u32 len = min(nbytes, outp_sg->length);
90 u8 *ptr = sg_virt(outp_sg);
92 req_info->out[*argcnt].vptr = (void *)ptr;
93 req_info->out[*argcnt].size = len;
100 static inline u32 create_ctx_hdr(struct ablkcipher_request *req, u32 enc,
103 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
104 struct cvm_enc_ctx *ctx = crypto_ablkcipher_ctx(tfm);
105 struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
106 struct fc_context *fctx = &rctx->fctx;
107 u64 *offset_control = &rctx->control_word;
108 u32 enc_iv_len = crypto_ablkcipher_ivsize(tfm);
109 struct cpt_request_info *req_info = &rctx->cpt_req;
110 u64 *ctrl_flags = NULL;
112 req_info->ctrl.s.grp = 0;
113 req_info->ctrl.s.dma_mode = DMA_GATHER_SCATTER;
114 req_info->ctrl.s.se_req = SE_CORE_REQ;
116 req_info->req.opcode.s.major = MAJOR_OP_FC |
117 DMA_MODE_FLAG(DMA_GATHER_SCATTER);
119 req_info->req.opcode.s.minor = 2;
121 req_info->req.opcode.s.minor = 3;
123 req_info->req.param1 = req->nbytes; /* Encryption Data length */
124 req_info->req.param2 = 0; /*Auth data length */
126 fctx->enc.enc_ctrl.e.enc_cipher = ctx->cipher_type;
127 fctx->enc.enc_ctrl.e.aes_key = ctx->key_type;
128 fctx->enc.enc_ctrl.e.iv_source = FROM_DPTR;
130 if (ctx->cipher_type == AES_XTS)
131 memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len * 2);
133 memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len);
134 ctrl_flags = (u64 *)&fctx->enc.enc_ctrl.flags;
135 *ctrl_flags = cpu_to_be64(*ctrl_flags);
137 *offset_control = cpu_to_be64(((u64)(enc_iv_len) << 16));
138 /* Storing Packet Data Information in offset
139 * Control Word First 8 bytes
141 req_info->in[*argcnt].vptr = (u8 *)offset_control;
142 req_info->in[*argcnt].size = CONTROL_WORD_LEN;
143 req_info->req.dlen += CONTROL_WORD_LEN;
146 req_info->in[*argcnt].vptr = (u8 *)fctx;
147 req_info->in[*argcnt].size = sizeof(struct fc_context);
148 req_info->req.dlen += sizeof(struct fc_context);
155 static inline u32 create_input_list(struct ablkcipher_request *req, u32 enc,
158 struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
159 struct cpt_request_info *req_info = &rctx->cpt_req;
162 create_ctx_hdr(req, enc, &argcnt);
163 update_input_iv(req_info, req->info, enc_iv_len, &argcnt);
164 update_input_data(req_info, req->src, req->nbytes, &argcnt);
165 req_info->incnt = argcnt;
170 static inline void store_cb_info(struct ablkcipher_request *req,
171 struct cpt_request_info *req_info)
173 req_info->callback = (void *)cvm_callback;
174 req_info->callback_arg = (void *)&req->base;
177 static inline void create_output_list(struct ablkcipher_request *req,
180 struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
181 struct cpt_request_info *req_info = &rctx->cpt_req;
184 /* OUTPUT Buffer Processing
185 * AES encryption/decryption output would be
186 * received in the following format
188 * ------IV--------|------ENCRYPTED/DECRYPTED DATA-----|
189 * [ 16 Bytes/ [ Request Enc/Dec/ DATA Len AES CBC ]
191 /* Reading IV information */
192 update_output_iv(req_info, req->info, enc_iv_len, &argcnt);
193 update_output_data(req_info, req->dst, req->nbytes, &argcnt);
194 req_info->outcnt = argcnt;
197 static inline int cvm_enc_dec(struct ablkcipher_request *req, u32 enc)
199 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
200 struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
201 u32 enc_iv_len = crypto_ablkcipher_ivsize(tfm);
202 struct fc_context *fctx = &rctx->fctx;
203 struct cpt_request_info *req_info = &rctx->cpt_req;
207 memset(req_info, 0, sizeof(struct cpt_request_info));
208 req_info->may_sleep = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) != 0;
209 memset(fctx, 0, sizeof(struct fc_context));
210 create_input_list(req, enc, enc_iv_len);
211 create_output_list(req, enc_iv_len);
212 store_cb_info(req, req_info);
213 cdev = dev_handle.cdev[smp_processor_id()];
214 status = cptvf_do_request(cdev, req_info);
215 /* We perform an asynchronous send and once
216 * the request is completed the driver would
217 * intimate through registered call back functions
226 static int cvm_encrypt(struct ablkcipher_request *req)
228 return cvm_enc_dec(req, true);
231 static int cvm_decrypt(struct ablkcipher_request *req)
233 return cvm_enc_dec(req, false);
236 static int cvm_xts_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
239 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
240 struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
242 const u8 *key1 = key;
243 const u8 *key2 = key + (keylen / 2);
245 err = xts_check_key(tfm, key, keylen);
248 ctx->key_len = keylen;
249 memcpy(ctx->enc_key, key1, keylen / 2);
250 memcpy(ctx->enc_key + KEY2_OFFSET, key2, keylen / 2);
251 ctx->cipher_type = AES_XTS;
252 switch (ctx->key_len) {
254 ctx->key_type = AES_128_BIT;
257 ctx->key_type = AES_256_BIT;
266 static int cvm_validate_keylen(struct cvm_enc_ctx *ctx, u32 keylen)
268 if ((keylen == 16) || (keylen == 24) || (keylen == 32)) {
269 ctx->key_len = keylen;
270 switch (ctx->key_len) {
272 ctx->key_type = AES_128_BIT;
275 ctx->key_type = AES_192_BIT;
278 ctx->key_type = AES_256_BIT;
284 if (ctx->cipher_type == DES3_CBC)
293 static int cvm_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
294 u32 keylen, u8 cipher_type)
296 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
297 struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
299 ctx->cipher_type = cipher_type;
300 if (!cvm_validate_keylen(ctx, keylen)) {
301 memcpy(ctx->enc_key, key, keylen);
304 crypto_ablkcipher_set_flags(cipher,
305 CRYPTO_TFM_RES_BAD_KEY_LEN);
310 static int cvm_cbc_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
313 return cvm_setkey(cipher, key, keylen, AES_CBC);
316 static int cvm_ecb_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
319 return cvm_setkey(cipher, key, keylen, AES_ECB);
322 static int cvm_cfb_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
325 return cvm_setkey(cipher, key, keylen, AES_CFB);
328 static int cvm_cbc_des3_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
331 return cvm_setkey(cipher, key, keylen, DES3_CBC);
334 static int cvm_ecb_des3_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
337 return cvm_setkey(cipher, key, keylen, DES3_ECB);
340 static int cvm_enc_dec_init(struct crypto_tfm *tfm)
342 struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
344 memset(ctx, 0, sizeof(*ctx));
345 tfm->crt_ablkcipher.reqsize = sizeof(struct cvm_req_ctx) +
346 sizeof(struct ablkcipher_request);
347 /* Additional memory for ablkcipher_request is
348 * allocated since the cryptd daemon uses
349 * this memory for request_ctx information
355 static struct crypto_alg algs[] = { {
356 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
357 .cra_blocksize = AES_BLOCK_SIZE,
358 .cra_ctxsize = sizeof(struct cvm_enc_ctx),
360 .cra_priority = 4001,
361 .cra_name = "xts(aes)",
362 .cra_driver_name = "cavium-xts-aes",
363 .cra_type = &crypto_ablkcipher_type,
366 .ivsize = AES_BLOCK_SIZE,
367 .min_keysize = 2 * AES_MIN_KEY_SIZE,
368 .max_keysize = 2 * AES_MAX_KEY_SIZE,
369 .setkey = cvm_xts_setkey,
370 .encrypt = cvm_encrypt,
371 .decrypt = cvm_decrypt,
374 .cra_init = cvm_enc_dec_init,
375 .cra_module = THIS_MODULE,
377 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
378 .cra_blocksize = AES_BLOCK_SIZE,
379 .cra_ctxsize = sizeof(struct cvm_enc_ctx),
381 .cra_priority = 4001,
382 .cra_name = "cbc(aes)",
383 .cra_driver_name = "cavium-cbc-aes",
384 .cra_type = &crypto_ablkcipher_type,
387 .ivsize = AES_BLOCK_SIZE,
388 .min_keysize = AES_MIN_KEY_SIZE,
389 .max_keysize = AES_MAX_KEY_SIZE,
390 .setkey = cvm_cbc_aes_setkey,
391 .encrypt = cvm_encrypt,
392 .decrypt = cvm_decrypt,
395 .cra_init = cvm_enc_dec_init,
396 .cra_module = THIS_MODULE,
398 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
399 .cra_blocksize = AES_BLOCK_SIZE,
400 .cra_ctxsize = sizeof(struct cvm_enc_ctx),
402 .cra_priority = 4001,
403 .cra_name = "ecb(aes)",
404 .cra_driver_name = "cavium-ecb-aes",
405 .cra_type = &crypto_ablkcipher_type,
408 .ivsize = AES_BLOCK_SIZE,
409 .min_keysize = AES_MIN_KEY_SIZE,
410 .max_keysize = AES_MAX_KEY_SIZE,
411 .setkey = cvm_ecb_aes_setkey,
412 .encrypt = cvm_encrypt,
413 .decrypt = cvm_decrypt,
416 .cra_init = cvm_enc_dec_init,
417 .cra_module = THIS_MODULE,
419 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
420 .cra_blocksize = AES_BLOCK_SIZE,
421 .cra_ctxsize = sizeof(struct cvm_enc_ctx),
423 .cra_priority = 4001,
424 .cra_name = "cfb(aes)",
425 .cra_driver_name = "cavium-cfb-aes",
426 .cra_type = &crypto_ablkcipher_type,
429 .ivsize = AES_BLOCK_SIZE,
430 .min_keysize = AES_MIN_KEY_SIZE,
431 .max_keysize = AES_MAX_KEY_SIZE,
432 .setkey = cvm_cfb_aes_setkey,
433 .encrypt = cvm_encrypt,
434 .decrypt = cvm_decrypt,
437 .cra_init = cvm_enc_dec_init,
438 .cra_module = THIS_MODULE,
440 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
441 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
442 .cra_ctxsize = sizeof(struct cvm_des3_ctx),
444 .cra_priority = 4001,
445 .cra_name = "cbc(des3_ede)",
446 .cra_driver_name = "cavium-cbc-des3_ede",
447 .cra_type = &crypto_ablkcipher_type,
450 .min_keysize = DES3_EDE_KEY_SIZE,
451 .max_keysize = DES3_EDE_KEY_SIZE,
452 .ivsize = DES_BLOCK_SIZE,
453 .setkey = cvm_cbc_des3_setkey,
454 .encrypt = cvm_encrypt,
455 .decrypt = cvm_decrypt,
458 .cra_init = cvm_enc_dec_init,
459 .cra_module = THIS_MODULE,
461 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
462 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
463 .cra_ctxsize = sizeof(struct cvm_des3_ctx),
465 .cra_priority = 4001,
466 .cra_name = "ecb(des3_ede)",
467 .cra_driver_name = "cavium-ecb-des3_ede",
468 .cra_type = &crypto_ablkcipher_type,
471 .min_keysize = DES3_EDE_KEY_SIZE,
472 .max_keysize = DES3_EDE_KEY_SIZE,
473 .ivsize = DES_BLOCK_SIZE,
474 .setkey = cvm_ecb_des3_setkey,
475 .encrypt = cvm_encrypt,
476 .decrypt = cvm_decrypt,
479 .cra_init = cvm_enc_dec_init,
480 .cra_module = THIS_MODULE,
483 static inline int cav_register_algs(void)
487 err = crypto_register_algs(algs, ARRAY_SIZE(algs));
494 static inline void cav_unregister_algs(void)
496 crypto_unregister_algs(algs, ARRAY_SIZE(algs));
499 int cvm_crypto_init(struct cpt_vf *cptvf)
501 struct pci_dev *pdev = cptvf->pdev;
504 dev_count = dev_handle.dev_count;
505 dev_handle.cdev[dev_count] = cptvf;
506 dev_handle.dev_count++;
508 if (dev_count == 3) {
509 if (cav_register_algs()) {
510 dev_err(&pdev->dev, "Error in registering crypto algorithms\n");
518 void cvm_crypto_exit(void)
522 dev_count = --dev_handle.dev_count;
524 cav_unregister_algs();