Linux 5.1.15
[linux/fpc-iii.git] / drivers / crypto / cavium / nitrox / nitrox_skcipher.c
blobd4935d6cefdd351522e277d8c82ca9071f098471
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/crypto.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/printk.h>
7 #include <crypto/aes.h>
8 #include <crypto/skcipher.h>
9 #include <crypto/ctr.h>
10 #include <crypto/des.h>
11 #include <crypto/xts.h>
13 #include "nitrox_dev.h"
14 #include "nitrox_common.h"
15 #include "nitrox_req.h"
17 struct nitrox_cipher {
18 const char *name;
19 enum flexi_cipher value;
22 /**
23 * supported cipher list
25 static const struct nitrox_cipher flexi_cipher_table[] = {
26 { "null", CIPHER_NULL },
27 { "cbc(des3_ede)", CIPHER_3DES_CBC },
28 { "ecb(des3_ede)", CIPHER_3DES_ECB },
29 { "cbc(aes)", CIPHER_AES_CBC },
30 { "ecb(aes)", CIPHER_AES_ECB },
31 { "cfb(aes)", CIPHER_AES_CFB },
32 { "rfc3686(ctr(aes))", CIPHER_AES_CTR },
33 { "xts(aes)", CIPHER_AES_XTS },
34 { "cts(cbc(aes))", CIPHER_AES_CBC_CTS },
35 { NULL, CIPHER_INVALID }
38 static enum flexi_cipher flexi_cipher_type(const char *name)
40 const struct nitrox_cipher *cipher = flexi_cipher_table;
42 while (cipher->name) {
43 if (!strcmp(cipher->name, name))
44 break;
45 cipher++;
47 return cipher->value;
50 static int nitrox_skcipher_init(struct crypto_skcipher *tfm)
52 struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);
53 struct crypto_ctx_hdr *chdr;
55 /* get the first device */
56 nctx->ndev = nitrox_get_first_device();
57 if (!nctx->ndev)
58 return -ENODEV;
60 /* allocate nitrox crypto context */
61 chdr = crypto_alloc_context(nctx->ndev);
62 if (!chdr) {
63 nitrox_put_device(nctx->ndev);
64 return -ENOMEM;
66 nctx->chdr = chdr;
67 nctx->u.ctx_handle = (uintptr_t)((u8 *)chdr->vaddr +
68 sizeof(struct ctx_hdr));
69 crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(tfm) +
70 sizeof(struct nitrox_kcrypt_request));
71 return 0;
74 static void nitrox_skcipher_exit(struct crypto_skcipher *tfm)
76 struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);
78 /* free the nitrox crypto context */
79 if (nctx->u.ctx_handle) {
80 struct flexi_crypto_context *fctx = nctx->u.fctx;
82 memzero_explicit(&fctx->crypto, sizeof(struct crypto_keys));
83 memzero_explicit(&fctx->auth, sizeof(struct auth_keys));
84 crypto_free_context((void *)nctx->chdr);
86 nitrox_put_device(nctx->ndev);
88 nctx->u.ctx_handle = 0;
89 nctx->ndev = NULL;
92 static inline int nitrox_skcipher_setkey(struct crypto_skcipher *cipher,
93 int aes_keylen, const u8 *key,
94 unsigned int keylen)
96 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
97 struct nitrox_crypto_ctx *nctx = crypto_tfm_ctx(tfm);
98 struct flexi_crypto_context *fctx;
99 union fc_ctx_flags *flags;
100 enum flexi_cipher cipher_type;
101 const char *name;
103 name = crypto_tfm_alg_name(tfm);
104 cipher_type = flexi_cipher_type(name);
105 if (unlikely(cipher_type == CIPHER_INVALID)) {
106 pr_err("unsupported cipher: %s\n", name);
107 return -EINVAL;
110 /* fill crypto context */
111 fctx = nctx->u.fctx;
112 flags = &fctx->flags;
113 flags->f = 0;
114 flags->w0.cipher_type = cipher_type;
115 flags->w0.aes_keylen = aes_keylen;
116 flags->w0.iv_source = IV_FROM_DPTR;
117 flags->f = cpu_to_be64(*(u64 *)&flags->w0);
118 /* copy the key to context */
119 memcpy(fctx->crypto.u.key, key, keylen);
121 return 0;
124 static int nitrox_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
125 unsigned int keylen)
127 int aes_keylen;
129 aes_keylen = flexi_aes_keylen(keylen);
130 if (aes_keylen < 0) {
131 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
132 return -EINVAL;
134 return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);
137 static int alloc_src_sglist(struct skcipher_request *skreq, int ivsize)
139 struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
140 int nents = sg_nents(skreq->src) + 1;
141 int ret;
143 /* Allocate buffer to hold IV and input scatterlist array */
144 ret = alloc_src_req_buf(nkreq, nents, ivsize);
145 if (ret)
146 return ret;
148 nitrox_creq_copy_iv(nkreq->src, skreq->iv, ivsize);
149 nitrox_creq_set_src_sg(nkreq, nents, ivsize, skreq->src,
150 skreq->cryptlen);
152 return 0;
155 static int alloc_dst_sglist(struct skcipher_request *skreq, int ivsize)
157 struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
158 int nents = sg_nents(skreq->dst) + 3;
159 int ret;
161 /* Allocate buffer to hold ORH, COMPLETION and output scatterlist
162 * array
164 ret = alloc_dst_req_buf(nkreq, nents);
165 if (ret)
166 return ret;
168 nitrox_creq_set_orh(nkreq);
169 nitrox_creq_set_comp(nkreq);
170 nitrox_creq_set_dst_sg(nkreq, nents, ivsize, skreq->dst,
171 skreq->cryptlen);
173 return 0;
176 static void free_src_sglist(struct skcipher_request *skreq)
178 struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
180 kfree(nkreq->src);
183 static void free_dst_sglist(struct skcipher_request *skreq)
185 struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
187 kfree(nkreq->dst);
190 static void nitrox_skcipher_callback(void *arg, int err)
192 struct skcipher_request *skreq = arg;
194 free_src_sglist(skreq);
195 free_dst_sglist(skreq);
196 if (err) {
197 pr_err_ratelimited("request failed status 0x%0x\n", err);
198 err = -EINVAL;
201 skcipher_request_complete(skreq, err);
204 static int nitrox_skcipher_crypt(struct skcipher_request *skreq, bool enc)
206 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(skreq);
207 struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(cipher);
208 struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
209 int ivsize = crypto_skcipher_ivsize(cipher);
210 struct se_crypto_request *creq;
211 int ret;
213 creq = &nkreq->creq;
214 creq->flags = skreq->base.flags;
215 creq->gfp = (skreq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
216 GFP_KERNEL : GFP_ATOMIC;
218 /* fill the request */
219 creq->ctrl.value = 0;
220 creq->opcode = FLEXI_CRYPTO_ENCRYPT_HMAC;
221 creq->ctrl.s.arg = (enc ? ENCRYPT : DECRYPT);
222 /* param0: length of the data to be encrypted */
223 creq->gph.param0 = cpu_to_be16(skreq->cryptlen);
224 creq->gph.param1 = 0;
225 /* param2: encryption data offset */
226 creq->gph.param2 = cpu_to_be16(ivsize);
227 creq->gph.param3 = 0;
229 creq->ctx_handle = nctx->u.ctx_handle;
230 creq->ctrl.s.ctxl = sizeof(struct flexi_crypto_context);
232 ret = alloc_src_sglist(skreq, ivsize);
233 if (ret)
234 return ret;
236 ret = alloc_dst_sglist(skreq, ivsize);
237 if (ret) {
238 free_src_sglist(skreq);
239 return ret;
242 /* send the crypto request */
243 return nitrox_process_se_request(nctx->ndev, creq,
244 nitrox_skcipher_callback, skreq);
247 static int nitrox_aes_encrypt(struct skcipher_request *skreq)
249 return nitrox_skcipher_crypt(skreq, true);
252 static int nitrox_aes_decrypt(struct skcipher_request *skreq)
254 return nitrox_skcipher_crypt(skreq, false);
257 static int nitrox_3des_setkey(struct crypto_skcipher *cipher,
258 const u8 *key, unsigned int keylen)
260 if (keylen != DES3_EDE_KEY_SIZE) {
261 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
262 return -EINVAL;
265 return nitrox_skcipher_setkey(cipher, 0, key, keylen);
268 static int nitrox_3des_encrypt(struct skcipher_request *skreq)
270 return nitrox_skcipher_crypt(skreq, true);
273 static int nitrox_3des_decrypt(struct skcipher_request *skreq)
275 return nitrox_skcipher_crypt(skreq, false);
278 static int nitrox_aes_xts_setkey(struct crypto_skcipher *cipher,
279 const u8 *key, unsigned int keylen)
281 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
282 struct nitrox_crypto_ctx *nctx = crypto_tfm_ctx(tfm);
283 struct flexi_crypto_context *fctx;
284 int aes_keylen, ret;
286 ret = xts_check_key(tfm, key, keylen);
287 if (ret)
288 return ret;
290 keylen /= 2;
292 aes_keylen = flexi_aes_keylen(keylen);
293 if (aes_keylen < 0) {
294 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
295 return -EINVAL;
298 fctx = nctx->u.fctx;
299 /* copy KEY2 */
300 memcpy(fctx->auth.u.key2, (key + keylen), keylen);
302 return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);
305 static int nitrox_aes_ctr_rfc3686_setkey(struct crypto_skcipher *cipher,
306 const u8 *key, unsigned int keylen)
308 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
309 struct nitrox_crypto_ctx *nctx = crypto_tfm_ctx(tfm);
310 struct flexi_crypto_context *fctx;
311 int aes_keylen;
313 if (keylen < CTR_RFC3686_NONCE_SIZE)
314 return -EINVAL;
316 fctx = nctx->u.fctx;
318 memcpy(fctx->crypto.iv, key + (keylen - CTR_RFC3686_NONCE_SIZE),
319 CTR_RFC3686_NONCE_SIZE);
321 keylen -= CTR_RFC3686_NONCE_SIZE;
323 aes_keylen = flexi_aes_keylen(keylen);
324 if (aes_keylen < 0) {
325 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
326 return -EINVAL;
328 return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);
331 static struct skcipher_alg nitrox_skciphers[] = { {
332 .base = {
333 .cra_name = "cbc(aes)",
334 .cra_driver_name = "n5_cbc(aes)",
335 .cra_priority = PRIO,
336 .cra_flags = CRYPTO_ALG_ASYNC,
337 .cra_blocksize = AES_BLOCK_SIZE,
338 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
339 .cra_alignmask = 0,
340 .cra_module = THIS_MODULE,
342 .min_keysize = AES_MIN_KEY_SIZE,
343 .max_keysize = AES_MAX_KEY_SIZE,
344 .ivsize = AES_BLOCK_SIZE,
345 .setkey = nitrox_aes_setkey,
346 .encrypt = nitrox_aes_encrypt,
347 .decrypt = nitrox_aes_decrypt,
348 .init = nitrox_skcipher_init,
349 .exit = nitrox_skcipher_exit,
350 }, {
351 .base = {
352 .cra_name = "ecb(aes)",
353 .cra_driver_name = "n5_ecb(aes)",
354 .cra_priority = PRIO,
355 .cra_flags = CRYPTO_ALG_ASYNC,
356 .cra_blocksize = AES_BLOCK_SIZE,
357 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
358 .cra_alignmask = 0,
359 .cra_module = THIS_MODULE,
361 .min_keysize = AES_MIN_KEY_SIZE,
362 .max_keysize = AES_MAX_KEY_SIZE,
363 .ivsize = AES_BLOCK_SIZE,
364 .setkey = nitrox_aes_setkey,
365 .encrypt = nitrox_aes_encrypt,
366 .decrypt = nitrox_aes_decrypt,
367 .init = nitrox_skcipher_init,
368 .exit = nitrox_skcipher_exit,
369 }, {
370 .base = {
371 .cra_name = "cfb(aes)",
372 .cra_driver_name = "n5_cfb(aes)",
373 .cra_priority = PRIO,
374 .cra_flags = CRYPTO_ALG_ASYNC,
375 .cra_blocksize = AES_BLOCK_SIZE,
376 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
377 .cra_alignmask = 0,
378 .cra_module = THIS_MODULE,
380 .min_keysize = AES_MIN_KEY_SIZE,
381 .max_keysize = AES_MAX_KEY_SIZE,
382 .ivsize = AES_BLOCK_SIZE,
383 .setkey = nitrox_aes_setkey,
384 .encrypt = nitrox_aes_encrypt,
385 .decrypt = nitrox_aes_decrypt,
386 .init = nitrox_skcipher_init,
387 .exit = nitrox_skcipher_exit,
388 }, {
389 .base = {
390 .cra_name = "xts(aes)",
391 .cra_driver_name = "n5_xts(aes)",
392 .cra_priority = PRIO,
393 .cra_flags = CRYPTO_ALG_ASYNC,
394 .cra_blocksize = AES_BLOCK_SIZE,
395 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
396 .cra_alignmask = 0,
397 .cra_module = THIS_MODULE,
399 .min_keysize = 2 * AES_MIN_KEY_SIZE,
400 .max_keysize = 2 * AES_MAX_KEY_SIZE,
401 .ivsize = AES_BLOCK_SIZE,
402 .setkey = nitrox_aes_xts_setkey,
403 .encrypt = nitrox_aes_encrypt,
404 .decrypt = nitrox_aes_decrypt,
405 .init = nitrox_skcipher_init,
406 .exit = nitrox_skcipher_exit,
407 }, {
408 .base = {
409 .cra_name = "rfc3686(ctr(aes))",
410 .cra_driver_name = "n5_rfc3686(ctr(aes))",
411 .cra_priority = PRIO,
412 .cra_flags = CRYPTO_ALG_ASYNC,
413 .cra_blocksize = 1,
414 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
415 .cra_alignmask = 0,
416 .cra_module = THIS_MODULE,
418 .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
419 .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
420 .ivsize = CTR_RFC3686_IV_SIZE,
421 .init = nitrox_skcipher_init,
422 .exit = nitrox_skcipher_exit,
423 .setkey = nitrox_aes_ctr_rfc3686_setkey,
424 .encrypt = nitrox_aes_encrypt,
425 .decrypt = nitrox_aes_decrypt,
426 }, {
427 .base = {
428 .cra_name = "cts(cbc(aes))",
429 .cra_driver_name = "n5_cts(cbc(aes))",
430 .cra_priority = PRIO,
431 .cra_flags = CRYPTO_ALG_ASYNC,
432 .cra_blocksize = AES_BLOCK_SIZE,
433 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
434 .cra_alignmask = 0,
435 .cra_type = &crypto_ablkcipher_type,
436 .cra_module = THIS_MODULE,
438 .min_keysize = AES_MIN_KEY_SIZE,
439 .max_keysize = AES_MAX_KEY_SIZE,
440 .ivsize = AES_BLOCK_SIZE,
441 .setkey = nitrox_aes_setkey,
442 .encrypt = nitrox_aes_encrypt,
443 .decrypt = nitrox_aes_decrypt,
444 .init = nitrox_skcipher_init,
445 .exit = nitrox_skcipher_exit,
446 }, {
447 .base = {
448 .cra_name = "cbc(des3_ede)",
449 .cra_driver_name = "n5_cbc(des3_ede)",
450 .cra_priority = PRIO,
451 .cra_flags = CRYPTO_ALG_ASYNC,
452 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
453 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
454 .cra_alignmask = 0,
455 .cra_module = THIS_MODULE,
457 .min_keysize = DES3_EDE_KEY_SIZE,
458 .max_keysize = DES3_EDE_KEY_SIZE,
459 .ivsize = DES3_EDE_BLOCK_SIZE,
460 .setkey = nitrox_3des_setkey,
461 .encrypt = nitrox_3des_encrypt,
462 .decrypt = nitrox_3des_decrypt,
463 .init = nitrox_skcipher_init,
464 .exit = nitrox_skcipher_exit,
465 }, {
466 .base = {
467 .cra_name = "ecb(des3_ede)",
468 .cra_driver_name = "n5_ecb(des3_ede)",
469 .cra_priority = PRIO,
470 .cra_flags = CRYPTO_ALG_ASYNC,
471 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
472 .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
473 .cra_alignmask = 0,
474 .cra_module = THIS_MODULE,
476 .min_keysize = DES3_EDE_KEY_SIZE,
477 .max_keysize = DES3_EDE_KEY_SIZE,
478 .ivsize = DES3_EDE_BLOCK_SIZE,
479 .setkey = nitrox_3des_setkey,
480 .encrypt = nitrox_3des_encrypt,
481 .decrypt = nitrox_3des_decrypt,
482 .init = nitrox_skcipher_init,
483 .exit = nitrox_skcipher_exit,
488 int nitrox_register_skciphers(void)
490 return crypto_register_skciphers(nitrox_skciphers,
491 ARRAY_SIZE(nitrox_skciphers));
494 void nitrox_unregister_skciphers(void)
496 crypto_unregister_skciphers(nitrox_skciphers,
497 ARRAY_SIZE(nitrox_skciphers));