Path: blob/master/drivers/crypto/cavium/nitrox/nitrox_skcipher.c
26285 views
// SPDX-License-Identifier: GPL-2.01#include <linux/crypto.h>2#include <linux/kernel.h>3#include <linux/module.h>4#include <linux/printk.h>56#include <crypto/aes.h>7#include <crypto/skcipher.h>8#include <crypto/scatterwalk.h>9#include <crypto/ctr.h>10#include <crypto/internal/des.h>11#include <crypto/xts.h>1213#include "nitrox_dev.h"14#include "nitrox_common.h"15#include "nitrox_req.h"1617struct nitrox_cipher {18const char *name;19enum flexi_cipher value;20};2122/*23* supported cipher list24*/25static 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 }36};3738static enum flexi_cipher flexi_cipher_type(const char *name)39{40const struct nitrox_cipher *cipher = flexi_cipher_table;4142while (cipher->name) {43if (!strcmp(cipher->name, name))44break;45cipher++;46}47return cipher->value;48}4950static void free_src_sglist(struct skcipher_request *skreq)51{52struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);5354kfree(nkreq->src);55}5657static void free_dst_sglist(struct skcipher_request *skreq)58{59struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);6061kfree(nkreq->dst);62}6364static void nitrox_skcipher_callback(void *arg, int err)65{66struct skcipher_request *skreq = arg;6768free_src_sglist(skreq);69free_dst_sglist(skreq);70if (err) {71pr_err_ratelimited("request failed status 0x%0x\n", err);72err = -EINVAL;73}7475skcipher_request_complete(skreq, err);76}7778static void nitrox_cbc_cipher_callback(void *arg, int err)79{80struct skcipher_request *skreq = arg;81struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);82struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(skreq);83int ivsize = crypto_skcipher_ivsize(cipher);84unsigned int start = skreq->cryptlen - ivsize;8586if (err) {87nitrox_skcipher_callback(arg, err);88return;89}9091if (nkreq->creq.ctrl.s.arg == ENCRYPT) {92scatterwalk_map_and_copy(skreq->iv, skreq->dst, start, ivsize,930);94} else {95if (skreq->src != skreq->dst) {96scatterwalk_map_and_copy(skreq->iv, skreq->src, start,97ivsize, 0);98} else {99memcpy(skreq->iv, nkreq->iv_out, ivsize);100kfree(nkreq->iv_out);101}102}103104nitrox_skcipher_callback(arg, err);105}106107static int nitrox_skcipher_init(struct crypto_skcipher *tfm)108{109struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);110struct crypto_ctx_hdr *chdr;111112/* get the first device */113nctx->ndev = nitrox_get_first_device();114if (!nctx->ndev)115return -ENODEV;116117/* allocate nitrox crypto context */118chdr = crypto_alloc_context(nctx->ndev);119if (!chdr) {120nitrox_put_device(nctx->ndev);121return -ENOMEM;122}123124nctx->callback = nitrox_skcipher_callback;125nctx->chdr = chdr;126nctx->u.ctx_handle = (uintptr_t)((u8 *)chdr->vaddr +127sizeof(struct ctx_hdr));128crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(tfm) +129sizeof(struct nitrox_kcrypt_request));130return 0;131}132133static int nitrox_cbc_init(struct crypto_skcipher *tfm)134{135int err;136struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);137138err = nitrox_skcipher_init(tfm);139if (err)140return err;141142nctx->callback = nitrox_cbc_cipher_callback;143return 0;144}145146static void nitrox_skcipher_exit(struct crypto_skcipher *tfm)147{148struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);149150/* free the nitrox crypto context */151if (nctx->u.ctx_handle) {152struct flexi_crypto_context *fctx = nctx->u.fctx;153154memzero_explicit(&fctx->crypto, sizeof(struct crypto_keys));155memzero_explicit(&fctx->auth, sizeof(struct auth_keys));156crypto_free_context((void *)nctx->chdr);157}158nitrox_put_device(nctx->ndev);159160nctx->u.ctx_handle = 0;161nctx->ndev = NULL;162}163164static inline int nitrox_skcipher_setkey(struct crypto_skcipher *cipher,165int aes_keylen, const u8 *key,166unsigned int keylen)167{168struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);169struct nitrox_crypto_ctx *nctx = crypto_tfm_ctx(tfm);170struct flexi_crypto_context *fctx;171union fc_ctx_flags *flags;172enum flexi_cipher cipher_type;173const char *name;174175name = crypto_tfm_alg_name(tfm);176cipher_type = flexi_cipher_type(name);177if (unlikely(cipher_type == CIPHER_INVALID)) {178pr_err("unsupported cipher: %s\n", name);179return -EINVAL;180}181182/* fill crypto context */183fctx = nctx->u.fctx;184flags = &fctx->flags;185flags->f = 0;186flags->w0.cipher_type = cipher_type;187flags->w0.aes_keylen = aes_keylen;188flags->w0.iv_source = IV_FROM_DPTR;189flags->f = cpu_to_be64(*(u64 *)&flags->w0);190/* copy the key to context */191memcpy(fctx->crypto.u.key, key, keylen);192193return 0;194}195196static int nitrox_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,197unsigned int keylen)198{199int aes_keylen;200201aes_keylen = flexi_aes_keylen(keylen);202if (aes_keylen < 0)203return -EINVAL;204return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);205}206207static int alloc_src_sglist(struct skcipher_request *skreq, int ivsize)208{209struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);210int nents = sg_nents(skreq->src) + 1;211int ret;212213/* Allocate buffer to hold IV and input scatterlist array */214ret = alloc_src_req_buf(nkreq, nents, ivsize);215if (ret)216return ret;217218nitrox_creq_copy_iv(nkreq->src, skreq->iv, ivsize);219nitrox_creq_set_src_sg(nkreq, nents, ivsize, skreq->src,220skreq->cryptlen);221222return 0;223}224225static int alloc_dst_sglist(struct skcipher_request *skreq, int ivsize)226{227struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);228int nents = sg_nents(skreq->dst) + 3;229int ret;230231/* Allocate buffer to hold ORH, COMPLETION and output scatterlist232* array233*/234ret = alloc_dst_req_buf(nkreq, nents);235if (ret)236return ret;237238nitrox_creq_set_orh(nkreq);239nitrox_creq_set_comp(nkreq);240nitrox_creq_set_dst_sg(nkreq, nents, ivsize, skreq->dst,241skreq->cryptlen);242243return 0;244}245246static int nitrox_skcipher_crypt(struct skcipher_request *skreq, bool enc)247{248struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(skreq);249struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(cipher);250struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);251int ivsize = crypto_skcipher_ivsize(cipher);252struct se_crypto_request *creq;253int ret;254255creq = &nkreq->creq;256creq->flags = skreq->base.flags;257creq->gfp = (skreq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?258GFP_KERNEL : GFP_ATOMIC;259260/* fill the request */261creq->ctrl.value = 0;262creq->opcode = FLEXI_CRYPTO_ENCRYPT_HMAC;263creq->ctrl.s.arg = (enc ? ENCRYPT : DECRYPT);264/* param0: length of the data to be encrypted */265creq->gph.param0 = cpu_to_be16(skreq->cryptlen);266creq->gph.param1 = 0;267/* param2: encryption data offset */268creq->gph.param2 = cpu_to_be16(ivsize);269creq->gph.param3 = 0;270271creq->ctx_handle = nctx->u.ctx_handle;272creq->ctrl.s.ctxl = sizeof(struct flexi_crypto_context);273274ret = alloc_src_sglist(skreq, ivsize);275if (ret)276return ret;277278ret = alloc_dst_sglist(skreq, ivsize);279if (ret) {280free_src_sglist(skreq);281return ret;282}283284/* send the crypto request */285return nitrox_process_se_request(nctx->ndev, creq, nctx->callback,286skreq);287}288289static int nitrox_cbc_decrypt(struct skcipher_request *skreq)290{291struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);292struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(skreq);293int ivsize = crypto_skcipher_ivsize(cipher);294gfp_t flags = (skreq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?295GFP_KERNEL : GFP_ATOMIC;296unsigned int start = skreq->cryptlen - ivsize;297298if (skreq->src != skreq->dst)299return nitrox_skcipher_crypt(skreq, false);300301nkreq->iv_out = kmalloc(ivsize, flags);302if (!nkreq->iv_out)303return -ENOMEM;304305scatterwalk_map_and_copy(nkreq->iv_out, skreq->src, start, ivsize, 0);306return nitrox_skcipher_crypt(skreq, false);307}308309static int nitrox_aes_encrypt(struct skcipher_request *skreq)310{311return nitrox_skcipher_crypt(skreq, true);312}313314static int nitrox_aes_decrypt(struct skcipher_request *skreq)315{316return nitrox_skcipher_crypt(skreq, false);317}318319static int nitrox_3des_setkey(struct crypto_skcipher *cipher,320const u8 *key, unsigned int keylen)321{322return verify_skcipher_des3_key(cipher, key) ?:323nitrox_skcipher_setkey(cipher, 0, key, keylen);324}325326static int nitrox_3des_encrypt(struct skcipher_request *skreq)327{328return nitrox_skcipher_crypt(skreq, true);329}330331static int nitrox_3des_decrypt(struct skcipher_request *skreq)332{333return nitrox_skcipher_crypt(skreq, false);334}335336static int nitrox_aes_xts_setkey(struct crypto_skcipher *cipher,337const u8 *key, unsigned int keylen)338{339struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(cipher);340struct flexi_crypto_context *fctx;341int aes_keylen, ret;342343ret = xts_verify_key(cipher, key, keylen);344if (ret)345return ret;346347keylen /= 2;348349aes_keylen = flexi_aes_keylen(keylen);350if (aes_keylen < 0)351return -EINVAL;352353fctx = nctx->u.fctx;354/* copy KEY2 */355memcpy(fctx->auth.u.key2, (key + keylen), keylen);356357return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);358}359360static int nitrox_aes_ctr_rfc3686_setkey(struct crypto_skcipher *cipher,361const u8 *key, unsigned int keylen)362{363struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(cipher);364struct flexi_crypto_context *fctx;365int aes_keylen;366367if (keylen < CTR_RFC3686_NONCE_SIZE)368return -EINVAL;369370fctx = nctx->u.fctx;371372memcpy(fctx->crypto.iv, key + (keylen - CTR_RFC3686_NONCE_SIZE),373CTR_RFC3686_NONCE_SIZE);374375keylen -= CTR_RFC3686_NONCE_SIZE;376377aes_keylen = flexi_aes_keylen(keylen);378if (aes_keylen < 0)379return -EINVAL;380return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);381}382383static struct skcipher_alg nitrox_skciphers[] = { {384.base = {385.cra_name = "cbc(aes)",386.cra_driver_name = "n5_cbc(aes)",387.cra_priority = PRIO,388.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY,389.cra_blocksize = AES_BLOCK_SIZE,390.cra_ctxsize = sizeof(struct nitrox_crypto_ctx),391.cra_alignmask = 0,392.cra_module = THIS_MODULE,393},394.min_keysize = AES_MIN_KEY_SIZE,395.max_keysize = AES_MAX_KEY_SIZE,396.ivsize = AES_BLOCK_SIZE,397.setkey = nitrox_aes_setkey,398.encrypt = nitrox_aes_encrypt,399.decrypt = nitrox_cbc_decrypt,400.init = nitrox_cbc_init,401.exit = nitrox_skcipher_exit,402}, {403.base = {404.cra_name = "ecb(aes)",405.cra_driver_name = "n5_ecb(aes)",406.cra_priority = PRIO,407.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY,408.cra_blocksize = AES_BLOCK_SIZE,409.cra_ctxsize = sizeof(struct nitrox_crypto_ctx),410.cra_alignmask = 0,411.cra_module = THIS_MODULE,412},413.min_keysize = AES_MIN_KEY_SIZE,414.max_keysize = AES_MAX_KEY_SIZE,415.ivsize = AES_BLOCK_SIZE,416.setkey = nitrox_aes_setkey,417.encrypt = nitrox_aes_encrypt,418.decrypt = nitrox_aes_decrypt,419.init = nitrox_skcipher_init,420.exit = nitrox_skcipher_exit,421}, {422.base = {423.cra_name = "xts(aes)",424.cra_driver_name = "n5_xts(aes)",425.cra_priority = PRIO,426.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY,427.cra_blocksize = AES_BLOCK_SIZE,428.cra_ctxsize = sizeof(struct nitrox_crypto_ctx),429.cra_alignmask = 0,430.cra_module = THIS_MODULE,431},432.min_keysize = 2 * AES_MIN_KEY_SIZE,433.max_keysize = 2 * AES_MAX_KEY_SIZE,434.ivsize = AES_BLOCK_SIZE,435.setkey = nitrox_aes_xts_setkey,436.encrypt = nitrox_aes_encrypt,437.decrypt = nitrox_aes_decrypt,438.init = nitrox_skcipher_init,439.exit = nitrox_skcipher_exit,440}, {441.base = {442.cra_name = "rfc3686(ctr(aes))",443.cra_driver_name = "n5_rfc3686(ctr(aes))",444.cra_priority = PRIO,445.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY,446.cra_blocksize = 1,447.cra_ctxsize = sizeof(struct nitrox_crypto_ctx),448.cra_alignmask = 0,449.cra_module = THIS_MODULE,450},451.min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,452.max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,453.ivsize = CTR_RFC3686_IV_SIZE,454.init = nitrox_skcipher_init,455.exit = nitrox_skcipher_exit,456.setkey = nitrox_aes_ctr_rfc3686_setkey,457.encrypt = nitrox_aes_encrypt,458.decrypt = nitrox_aes_decrypt,459}, {460.base = {461.cra_name = "cts(cbc(aes))",462.cra_driver_name = "n5_cts(cbc(aes))",463.cra_priority = PRIO,464.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY,465.cra_blocksize = AES_BLOCK_SIZE,466.cra_ctxsize = sizeof(struct nitrox_crypto_ctx),467.cra_alignmask = 0,468.cra_module = THIS_MODULE,469},470.min_keysize = AES_MIN_KEY_SIZE,471.max_keysize = AES_MAX_KEY_SIZE,472.ivsize = AES_BLOCK_SIZE,473.setkey = nitrox_aes_setkey,474.encrypt = nitrox_aes_encrypt,475.decrypt = nitrox_aes_decrypt,476.init = nitrox_skcipher_init,477.exit = nitrox_skcipher_exit,478}, {479.base = {480.cra_name = "cbc(des3_ede)",481.cra_driver_name = "n5_cbc(des3_ede)",482.cra_priority = PRIO,483.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY,484.cra_blocksize = DES3_EDE_BLOCK_SIZE,485.cra_ctxsize = sizeof(struct nitrox_crypto_ctx),486.cra_alignmask = 0,487.cra_module = THIS_MODULE,488},489.min_keysize = DES3_EDE_KEY_SIZE,490.max_keysize = DES3_EDE_KEY_SIZE,491.ivsize = DES3_EDE_BLOCK_SIZE,492.setkey = nitrox_3des_setkey,493.encrypt = nitrox_3des_encrypt,494.decrypt = nitrox_cbc_decrypt,495.init = nitrox_cbc_init,496.exit = nitrox_skcipher_exit,497}, {498.base = {499.cra_name = "ecb(des3_ede)",500.cra_driver_name = "n5_ecb(des3_ede)",501.cra_priority = PRIO,502.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY,503.cra_blocksize = DES3_EDE_BLOCK_SIZE,504.cra_ctxsize = sizeof(struct nitrox_crypto_ctx),505.cra_alignmask = 0,506.cra_module = THIS_MODULE,507},508.min_keysize = DES3_EDE_KEY_SIZE,509.max_keysize = DES3_EDE_KEY_SIZE,510.ivsize = DES3_EDE_BLOCK_SIZE,511.setkey = nitrox_3des_setkey,512.encrypt = nitrox_3des_encrypt,513.decrypt = nitrox_3des_decrypt,514.init = nitrox_skcipher_init,515.exit = nitrox_skcipher_exit,516}517518};519520int nitrox_register_skciphers(void)521{522return crypto_register_skciphers(nitrox_skciphers,523ARRAY_SIZE(nitrox_skciphers));524}525526void nitrox_unregister_skciphers(void)527{528crypto_unregister_skciphers(nitrox_skciphers,529ARRAY_SIZE(nitrox_skciphers));530}531532533