^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * AMD Cryptographic Coprocessor (CCP) AES crypto API support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2013-2019 Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Tom Lendacky <thomas.lendacky@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <crypto/ctr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "ccp-crypto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int ccp_aes_complete(struct crypto_async_request *async_req, int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct skcipher_request *req = skcipher_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (ctx->u.aes.mode != CCP_AES_MODE_ECB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) memcpy(req->iv, rctx->iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int ccp_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) switch (key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) case AES_KEYSIZE_128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ctx->u.aes.type = CCP_AES_TYPE_128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) case AES_KEYSIZE_192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ctx->u.aes.type = CCP_AES_TYPE_192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) case AES_KEYSIZE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ctx->u.aes.type = CCP_AES_TYPE_256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ctx->u.aes.mode = alg->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ctx->u.aes.key_len = key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) memcpy(ctx->u.aes.key, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int ccp_aes_crypt(struct skcipher_request *req, bool encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct scatterlist *iv_sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned int iv_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!ctx->u.aes.key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (((ctx->u.aes.mode == CCP_AES_MODE_ECB) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) (ctx->u.aes.mode == CCP_AES_MODE_CBC)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) (req->cryptlen & (AES_BLOCK_SIZE - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (ctx->u.aes.mode != CCP_AES_MODE_ECB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!req->iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) memcpy(rctx->iv, req->iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) iv_sg = &rctx->iv_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) iv_len = AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) sg_init_one(iv_sg, rctx->iv, iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) memset(&rctx->cmd, 0, sizeof(rctx->cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) INIT_LIST_HEAD(&rctx->cmd.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) rctx->cmd.engine = CCP_ENGINE_AES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) rctx->cmd.u.aes.type = ctx->u.aes.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) rctx->cmd.u.aes.mode = ctx->u.aes.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rctx->cmd.u.aes.action =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) (encrypt) ? CCP_AES_ACTION_ENCRYPT : CCP_AES_ACTION_DECRYPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) rctx->cmd.u.aes.iv = iv_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) rctx->cmd.u.aes.iv_len = iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rctx->cmd.u.aes.src = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) rctx->cmd.u.aes.src_len = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) rctx->cmd.u.aes.dst = req->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int ccp_aes_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return ccp_aes_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int ccp_aes_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return ccp_aes_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int ccp_aes_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ctx->complete = ccp_aes_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ctx->u.aes.key_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int ccp_aes_rfc3686_complete(struct crypto_async_request *async_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct skcipher_request *req = skcipher_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* Restore the original pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) req->iv = rctx->rfc3686_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return ccp_aes_complete(async_req, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static int ccp_aes_rfc3686_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (key_len < CTR_RFC3686_NONCE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) key_len -= CTR_RFC3686_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) memcpy(ctx->u.aes.nonce, key + key_len, CTR_RFC3686_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return ccp_aes_setkey(tfm, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int ccp_aes_rfc3686_crypt(struct skcipher_request *req, bool encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) u8 *iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Initialize the CTR block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) iv = rctx->rfc3686_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) memcpy(iv, ctx->u.aes.nonce, CTR_RFC3686_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) iv += CTR_RFC3686_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) memcpy(iv, req->iv, CTR_RFC3686_IV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) iv += CTR_RFC3686_IV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *(__be32 *)iv = cpu_to_be32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Point to the new IV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) rctx->rfc3686_info = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) req->iv = rctx->rfc3686_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return ccp_aes_crypt(req, encrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int ccp_aes_rfc3686_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return ccp_aes_rfc3686_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int ccp_aes_rfc3686_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return ccp_aes_rfc3686_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int ccp_aes_rfc3686_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ctx->complete = ccp_aes_rfc3686_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ctx->u.aes.key_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static const struct skcipher_alg ccp_aes_defaults = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .setkey = ccp_aes_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .encrypt = ccp_aes_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) .decrypt = ccp_aes_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) .min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .init = ccp_aes_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .base.cra_flags = CRYPTO_ALG_ASYNC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) CRYPTO_ALG_ALLOCATES_MEMORY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) CRYPTO_ALG_KERN_DRIVER_ONLY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .base.cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) .base.cra_ctxsize = sizeof(struct ccp_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .base.cra_priority = CCP_CRA_PRIORITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static const struct skcipher_alg ccp_aes_rfc3686_defaults = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) .setkey = ccp_aes_rfc3686_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) .encrypt = ccp_aes_rfc3686_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) .decrypt = ccp_aes_rfc3686_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) .init = ccp_aes_rfc3686_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .base.cra_flags = CRYPTO_ALG_ASYNC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) CRYPTO_ALG_ALLOCATES_MEMORY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) CRYPTO_ALG_KERN_DRIVER_ONLY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) .base.cra_blocksize = CTR_RFC3686_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) .base.cra_ctxsize = sizeof(struct ccp_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) .base.cra_priority = CCP_CRA_PRIORITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct ccp_aes_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) enum ccp_aes_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) const char *driver_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) unsigned int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) unsigned int ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) const struct skcipher_alg *alg_defaults;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static struct ccp_aes_def aes_algs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .mode = CCP_AES_MODE_ECB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) .version = CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) .name = "ecb(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) .driver_name = "ecb-aes-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) .blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) .ivsize = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) .alg_defaults = &ccp_aes_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) .mode = CCP_AES_MODE_CBC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) .version = CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) .name = "cbc(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) .driver_name = "cbc-aes-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) .blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) .alg_defaults = &ccp_aes_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) .mode = CCP_AES_MODE_CFB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) .version = CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) .name = "cfb(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) .driver_name = "cfb-aes-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) .blocksize = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) .alg_defaults = &ccp_aes_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) .mode = CCP_AES_MODE_OFB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) .version = CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .name = "ofb(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .driver_name = "ofb-aes-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .blocksize = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .alg_defaults = &ccp_aes_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) .mode = CCP_AES_MODE_CTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .version = CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .name = "ctr(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .driver_name = "ctr-aes-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .blocksize = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .alg_defaults = &ccp_aes_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .mode = CCP_AES_MODE_CTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) .version = CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .name = "rfc3686(ctr(aes))",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .driver_name = "rfc3686-ctr-aes-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) .blocksize = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) .ivsize = CTR_RFC3686_IV_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .alg_defaults = &ccp_aes_rfc3686_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int ccp_register_aes_alg(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) const struct ccp_aes_def *def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct ccp_crypto_skcipher_alg *ccp_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (!ccp_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) INIT_LIST_HEAD(&ccp_alg->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ccp_alg->mode = def->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* Copy the defaults and override as necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) alg = &ccp_alg->alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) *alg = *def->alg_defaults;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) def->driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) alg->base.cra_blocksize = def->blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) alg->ivsize = def->ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = crypto_register_skcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) pr_err("%s skcipher algorithm registration error (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) alg->base.cra_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) kfree(ccp_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) list_add(&ccp_alg->entry, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int ccp_register_aes_algs(struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) unsigned int ccpversion = ccp_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (aes_algs[i].version > ccpversion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = ccp_register_aes_alg(head, &aes_algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }