2 * AMD Cryptographic Coprocessor (CCP) RSA crypto API support
4 * Copyright (C) 2017 Advanced Micro Devices, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/scatterlist.h>
16 #include <linux/crypto.h>
17 #include <crypto/algapi.h>
18 #include <crypto/internal/rsa.h>
19 #include <crypto/internal/akcipher.h>
20 #include <crypto/akcipher.h>
21 #include <crypto/scatterwalk.h>
23 #include "ccp-crypto.h"
25 static inline struct akcipher_request *akcipher_request_cast(
26 struct crypto_async_request *req)
28 return container_of(req, struct akcipher_request, base);
31 static inline int ccp_copy_and_save_keypart(u8 **kpbuf, unsigned int *kplen,
32 const u8 *buf, size_t sz)
36 for (nskip = 0; nskip < sz; nskip++)
40 *kpbuf = kmemdup(buf + nskip, *kplen, GFP_KERNEL);
47 static int ccp_rsa_complete(struct crypto_async_request *async_req, int ret)
49 struct akcipher_request *req = akcipher_request_cast(async_req);
50 struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
55 req->dst_len = rctx->cmd.u.rsa.key_size >> 3;
60 static unsigned int ccp_rsa_maxsize(struct crypto_akcipher *tfm)
62 struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
64 return ctx->u.rsa.n_len;
67 static int ccp_rsa_crypt(struct akcipher_request *req, bool encrypt)
69 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
70 struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
71 struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
74 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
75 INIT_LIST_HEAD(&rctx->cmd.entry);
76 rctx->cmd.engine = CCP_ENGINE_RSA;
78 rctx->cmd.u.rsa.key_size = ctx->u.rsa.key_len; /* in bits */
80 rctx->cmd.u.rsa.exp = &ctx->u.rsa.e_sg;
81 rctx->cmd.u.rsa.exp_len = ctx->u.rsa.e_len;
83 rctx->cmd.u.rsa.exp = &ctx->u.rsa.d_sg;
84 rctx->cmd.u.rsa.exp_len = ctx->u.rsa.d_len;
86 rctx->cmd.u.rsa.mod = &ctx->u.rsa.n_sg;
87 rctx->cmd.u.rsa.mod_len = ctx->u.rsa.n_len;
88 rctx->cmd.u.rsa.src = req->src;
89 rctx->cmd.u.rsa.src_len = req->src_len;
90 rctx->cmd.u.rsa.dst = req->dst;
92 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
97 static int ccp_rsa_encrypt(struct akcipher_request *req)
99 return ccp_rsa_crypt(req, true);
102 static int ccp_rsa_decrypt(struct akcipher_request *req)
104 return ccp_rsa_crypt(req, false);
107 static int ccp_check_key_length(unsigned int len)
110 if (len < 8 || len > 4096)
115 static void ccp_rsa_free_key_bufs(struct ccp_ctx *ctx)
117 /* Clean up old key data */
118 kzfree(ctx->u.rsa.e_buf);
119 ctx->u.rsa.e_buf = NULL;
120 ctx->u.rsa.e_len = 0;
121 kzfree(ctx->u.rsa.n_buf);
122 ctx->u.rsa.n_buf = NULL;
123 ctx->u.rsa.n_len = 0;
124 kzfree(ctx->u.rsa.d_buf);
125 ctx->u.rsa.d_buf = NULL;
126 ctx->u.rsa.d_len = 0;
129 static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
130 unsigned int keylen, bool private)
132 struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
133 struct rsa_key raw_key;
136 ccp_rsa_free_key_bufs(ctx);
137 memset(&raw_key, 0, sizeof(raw_key));
139 /* Code borrowed from crypto/rsa.c */
141 ret = rsa_parse_priv_key(&raw_key, key, keylen);
143 ret = rsa_parse_pub_key(&raw_key, key, keylen);
147 ret = ccp_copy_and_save_keypart(&ctx->u.rsa.n_buf, &ctx->u.rsa.n_len,
148 raw_key.n, raw_key.n_sz);
151 sg_init_one(&ctx->u.rsa.n_sg, ctx->u.rsa.n_buf, ctx->u.rsa.n_len);
153 ctx->u.rsa.key_len = ctx->u.rsa.n_len << 3; /* convert to bits */
154 if (ccp_check_key_length(ctx->u.rsa.key_len)) {
159 ret = ccp_copy_and_save_keypart(&ctx->u.rsa.e_buf, &ctx->u.rsa.e_len,
160 raw_key.e, raw_key.e_sz);
163 sg_init_one(&ctx->u.rsa.e_sg, ctx->u.rsa.e_buf, ctx->u.rsa.e_len);
166 ret = ccp_copy_and_save_keypart(&ctx->u.rsa.d_buf,
168 raw_key.d, raw_key.d_sz);
171 sg_init_one(&ctx->u.rsa.d_sg,
172 ctx->u.rsa.d_buf, ctx->u.rsa.d_len);
178 ccp_rsa_free_key_bufs(ctx);
184 static int ccp_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key,
187 return ccp_rsa_setkey(tfm, key, keylen, true);
190 static int ccp_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key,
193 return ccp_rsa_setkey(tfm, key, keylen, false);
196 static int ccp_rsa_init_tfm(struct crypto_akcipher *tfm)
198 struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
200 akcipher_set_reqsize(tfm, sizeof(struct ccp_rsa_req_ctx));
201 ctx->complete = ccp_rsa_complete;
206 static void ccp_rsa_exit_tfm(struct crypto_akcipher *tfm)
208 struct ccp_ctx *ctx = crypto_tfm_ctx(&tfm->base);
210 ccp_rsa_free_key_bufs(ctx);
213 static struct akcipher_alg ccp_rsa_defaults = {
214 .encrypt = ccp_rsa_encrypt,
215 .decrypt = ccp_rsa_decrypt,
216 .set_pub_key = ccp_rsa_setpubkey,
217 .set_priv_key = ccp_rsa_setprivkey,
218 .max_size = ccp_rsa_maxsize,
219 .init = ccp_rsa_init_tfm,
220 .exit = ccp_rsa_exit_tfm,
223 .cra_driver_name = "rsa-ccp",
224 .cra_priority = CCP_CRA_PRIORITY,
225 .cra_module = THIS_MODULE,
226 .cra_ctxsize = 2 * sizeof(struct ccp_ctx),
231 unsigned int version;
233 const char *driver_name;
234 unsigned int reqsize;
235 struct akcipher_alg *alg_defaults;
238 static struct ccp_rsa_def rsa_algs[] = {
240 .version = CCP_VERSION(3, 0),
242 .driver_name = "rsa-ccp",
243 .reqsize = sizeof(struct ccp_rsa_req_ctx),
244 .alg_defaults = &ccp_rsa_defaults,
248 static int ccp_register_rsa_alg(struct list_head *head,
249 const struct ccp_rsa_def *def)
251 struct ccp_crypto_akcipher_alg *ccp_alg;
252 struct akcipher_alg *alg;
255 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
259 INIT_LIST_HEAD(&ccp_alg->entry);
262 *alg = *def->alg_defaults;
263 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
264 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
266 ret = crypto_register_akcipher(alg);
268 pr_err("%s akcipher algorithm registration error (%d)\n",
269 alg->base.cra_name, ret);
274 list_add(&ccp_alg->entry, head);
279 int ccp_register_rsa_algs(struct list_head *head)
282 unsigned int ccpversion = ccp_version();
284 /* Register the RSA algorithm in standard mode
285 * This works for CCP v3 and later
287 for (i = 0; i < ARRAY_SIZE(rsa_algs); i++) {
288 if (rsa_algs[i].version > ccpversion)
290 ret = ccp_register_rsa_alg(head, &rsa_algs[i]);