^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) RSA 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) 2017 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: Gary R Hook <gary.hook@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/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <crypto/internal/rsa.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/internal/akcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <crypto/akcipher.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 inline struct akcipher_request *akcipher_request_cast(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct crypto_async_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return container_of(req, struct akcipher_request, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static inline int ccp_copy_and_save_keypart(u8 **kpbuf, unsigned int *kplen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) const u8 *buf, size_t sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int nskip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) for (nskip = 0; nskip < sz; nskip++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (buf[nskip])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *kplen = sz - nskip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *kpbuf = kmemdup(buf + nskip, *kplen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!*kpbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int ccp_rsa_complete(struct crypto_async_request *async_req, int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct akcipher_request *req = akcipher_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) req->dst_len = rctx->cmd.u.rsa.key_size >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static unsigned int ccp_rsa_maxsize(struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return ctx->u.rsa.n_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int ccp_rsa_crypt(struct akcipher_request *req, bool encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) memset(&rctx->cmd, 0, sizeof(rctx->cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) INIT_LIST_HEAD(&rctx->cmd.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) rctx->cmd.engine = CCP_ENGINE_RSA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) rctx->cmd.u.rsa.key_size = ctx->u.rsa.key_len; /* in bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (encrypt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) rctx->cmd.u.rsa.exp = &ctx->u.rsa.e_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) rctx->cmd.u.rsa.exp_len = ctx->u.rsa.e_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) rctx->cmd.u.rsa.exp = &ctx->u.rsa.d_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) rctx->cmd.u.rsa.exp_len = ctx->u.rsa.d_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) rctx->cmd.u.rsa.mod = &ctx->u.rsa.n_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) rctx->cmd.u.rsa.mod_len = ctx->u.rsa.n_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) rctx->cmd.u.rsa.src = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) rctx->cmd.u.rsa.src_len = req->src_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) rctx->cmd.u.rsa.dst = req->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int ccp_rsa_encrypt(struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return ccp_rsa_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static int ccp_rsa_decrypt(struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ccp_rsa_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int ccp_check_key_length(unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* In bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (len < 8 || len > 4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^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 void ccp_rsa_free_key_bufs(struct ccp_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Clean up old key data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kfree_sensitive(ctx->u.rsa.e_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ctx->u.rsa.e_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ctx->u.rsa.e_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) kfree_sensitive(ctx->u.rsa.n_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ctx->u.rsa.n_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ctx->u.rsa.n_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) kfree_sensitive(ctx->u.rsa.d_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ctx->u.rsa.d_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ctx->u.rsa.d_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned int keylen, bool private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct rsa_key raw_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ccp_rsa_free_key_bufs(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) memset(&raw_key, 0, sizeof(raw_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* Code borrowed from crypto/rsa.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ret = rsa_parse_priv_key(&raw_key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ret = rsa_parse_pub_key(&raw_key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto n_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ret = ccp_copy_and_save_keypart(&ctx->u.rsa.n_buf, &ctx->u.rsa.n_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) raw_key.n, raw_key.n_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto key_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) sg_init_one(&ctx->u.rsa.n_sg, ctx->u.rsa.n_buf, ctx->u.rsa.n_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ctx->u.rsa.key_len = ctx->u.rsa.n_len << 3; /* convert to bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (ccp_check_key_length(ctx->u.rsa.key_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) goto key_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ret = ccp_copy_and_save_keypart(&ctx->u.rsa.e_buf, &ctx->u.rsa.e_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) raw_key.e, raw_key.e_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto key_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) sg_init_one(&ctx->u.rsa.e_sg, ctx->u.rsa.e_buf, ctx->u.rsa.e_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (private) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ret = ccp_copy_and_save_keypart(&ctx->u.rsa.d_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) &ctx->u.rsa.d_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) raw_key.d, raw_key.d_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto key_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) sg_init_one(&ctx->u.rsa.d_sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ctx->u.rsa.d_buf, ctx->u.rsa.d_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) key_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ccp_rsa_free_key_bufs(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) n_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static int ccp_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return ccp_rsa_setkey(tfm, key, keylen, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int ccp_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return ccp_rsa_setkey(tfm, key, keylen, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int ccp_rsa_init_tfm(struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) akcipher_set_reqsize(tfm, sizeof(struct ccp_rsa_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ctx->complete = ccp_rsa_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static void ccp_rsa_exit_tfm(struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct ccp_ctx *ctx = crypto_tfm_ctx(&tfm->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ccp_rsa_free_key_bufs(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static struct akcipher_alg ccp_rsa_defaults = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .encrypt = ccp_rsa_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .decrypt = ccp_rsa_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) .set_pub_key = ccp_rsa_setpubkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .set_priv_key = ccp_rsa_setprivkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) .max_size = ccp_rsa_maxsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .init = ccp_rsa_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) .exit = ccp_rsa_exit_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) .cra_name = "rsa",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .cra_driver_name = "rsa-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) .cra_priority = CCP_CRA_PRIORITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) .cra_ctxsize = 2 * sizeof(struct ccp_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct ccp_rsa_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) unsigned int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) const char *driver_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned int reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct akcipher_alg *alg_defaults;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static struct ccp_rsa_def rsa_algs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) .version = CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) .name = "rsa",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) .driver_name = "rsa-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) .reqsize = sizeof(struct ccp_rsa_req_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) .alg_defaults = &ccp_rsa_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static int ccp_register_rsa_alg(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) const struct ccp_rsa_def *def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct ccp_crypto_akcipher_alg *ccp_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct akcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (!ccp_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) INIT_LIST_HEAD(&ccp_alg->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) alg = &ccp_alg->alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *alg = *def->alg_defaults;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) def->driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ret = crypto_register_akcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) pr_err("%s akcipher algorithm registration error (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) alg->base.cra_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) kfree(ccp_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) list_add(&ccp_alg->entry, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int ccp_register_rsa_algs(struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) unsigned int ccpversion = ccp_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* Register the RSA algorithm in standard mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * This works for CCP v3 and later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) for (i = 0; i < ARRAY_SIZE(rsa_algs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (rsa_algs[i].version > ccpversion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ret = ccp_register_rsa_alg(head, &rsa_algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }