blob: fa3ae8e78f6f3e412fe89b230dc56d393238688f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -06002/*
3 * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
4 *
Hook, Garyfa5cd1c2018-12-18 15:48:29 +00005 * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -06006 *
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hook4b394a22016-07-26 19:10:21 -05008 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -06009 */
10
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/delay.h>
14#include <linux/scatterlist.h>
15#include <linux/crypto.h>
16#include <crypto/algapi.h>
17#include <crypto/hash.h>
Corentin LABBE6507c572017-05-19 08:53:31 +020018#include <crypto/hmac.h>
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060019#include <crypto/internal/hash.h>
Eric Biggersa24d22b2020-11-12 21:20:21 -080020#include <crypto/sha1.h>
21#include <crypto/sha2.h>
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060022#include <crypto/scatterwalk.h>
Herbert Xu2c2e1832020-07-09 22:44:04 +100023#include <linux/string.h>
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060024
25#include "ccp-crypto.h"
26
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060027static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
28{
29 struct ahash_request *req = ahash_request_cast(async_req);
30 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Herbert Xu99c6b20e2022-12-02 17:20:49 +080031 struct ccp_sha_req_ctx *rctx = ahash_request_ctx_dma(req);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060032 unsigned int digest_size = crypto_ahash_digestsize(tfm);
33
34 if (ret)
35 goto e_free;
36
37 if (rctx->hash_rem) {
38 /* Save remaining data to buffer */
Tom Lendacky81a59f02014-01-06 13:34:17 -060039 unsigned int offset = rctx->nbytes - rctx->hash_rem;
Tom Lendacky8db88462015-02-03 13:07:05 -060040
Tom Lendacky81a59f02014-01-06 13:34:17 -060041 scatterwalk_map_and_copy(rctx->buf, rctx->src,
42 offset, rctx->hash_rem, 0);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060043 rctx->buf_count = rctx->hash_rem;
Tom Lendacky8db88462015-02-03 13:07:05 -060044 } else {
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060045 rctx->buf_count = 0;
Tom Lendacky8db88462015-02-03 13:07:05 -060046 }
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060047
Tom Lendacky393897c2014-01-06 13:34:11 -060048 /* Update result area if supplied */
Gary R Hook0ee991b2018-03-07 11:37:42 -060049 if (req->result && rctx->final)
Tom Lendacky393897c2014-01-06 13:34:11 -060050 memcpy(req->result, rctx->ctx, digest_size);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060051
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060052e_free:
53 sg_free_table(&rctx->data_sg);
54
55 return ret;
56}
57
58static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
59 unsigned int final)
60{
61 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Herbert Xu99c6b20e2022-12-02 17:20:49 +080062 struct ccp_ctx *ctx = crypto_ahash_ctx_dma(tfm);
63 struct ccp_sha_req_ctx *rctx = ahash_request_ctx_dma(req);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060064 struct scatterlist *sg;
65 unsigned int block_size =
66 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
Tom Lendacky81a59f02014-01-06 13:34:17 -060067 unsigned int sg_count;
Tom Lendacky5258de82014-01-06 13:33:59 -060068 gfp_t gfp;
Tom Lendacky81a59f02014-01-06 13:34:17 -060069 u64 len;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060070 int ret;
71
Tom Lendacky81a59f02014-01-06 13:34:17 -060072 len = (u64)rctx->buf_count + (u64)nbytes;
73
74 if (!final && (len <= block_size)) {
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060075 scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
76 0, nbytes, 0);
77 rctx->buf_count += nbytes;
78
79 return 0;
80 }
81
Tom Lendacky81a59f02014-01-06 13:34:17 -060082 rctx->src = req->src;
83 rctx->nbytes = nbytes;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060084
85 rctx->final = final;
Tom Lendacky81a59f02014-01-06 13:34:17 -060086 rctx->hash_rem = final ? 0 : len & (block_size - 1);
87 rctx->hash_cnt = len - rctx->hash_rem;
88 if (!final && !rctx->hash_rem) {
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060089 /* CCP can't do zero length final, so keep some data around */
90 rctx->hash_cnt -= block_size;
91 rctx->hash_rem = block_size;
92 }
93
94 /* Initialize the context scatterlist */
95 sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
96
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060097 sg = NULL;
Tom Lendacky77dc4a52014-01-06 13:34:05 -060098 if (rctx->buf_count && nbytes) {
99 /* Build the data scatterlist table - allocate enough entries
100 * for both data pieces (buffer and input data)
101 */
102 gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
103 GFP_KERNEL : GFP_ATOMIC;
104 sg_count = sg_nents(req->src) + 1;
105 ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
106 if (ret)
107 return ret;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600108
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600109 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
110 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
Tom Lendacky355eba52015-10-01 16:32:31 -0500111 if (!sg) {
112 ret = -EINVAL;
113 goto e_free;
114 }
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600115 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
Tom Lendacky355eba52015-10-01 16:32:31 -0500116 if (!sg) {
117 ret = -EINVAL;
118 goto e_free;
119 }
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600120 sg_mark_end(sg);
121
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600122 sg = rctx->data_sg.sgl;
123 } else if (rctx->buf_count) {
124 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
125
126 sg = &rctx->buf_sg;
127 } else if (nbytes) {
128 sg = req->src;
129 }
130
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600131 rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
132
133 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
134 INIT_LIST_HEAD(&rctx->cmd.entry);
135 rctx->cmd.engine = CCP_ENGINE_SHA;
136 rctx->cmd.u.sha.type = rctx->type;
137 rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
Gary R Hook4b394a22016-07-26 19:10:21 -0500138
139 switch (rctx->type) {
140 case CCP_SHA_TYPE_1:
141 rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE;
142 break;
143 case CCP_SHA_TYPE_224:
144 rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE;
145 break;
146 case CCP_SHA_TYPE_256:
147 rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE;
148 break;
Gary R Hookccebcf32017-03-15 13:20:43 -0500149 case CCP_SHA_TYPE_384:
150 rctx->cmd.u.sha.ctx_len = SHA384_DIGEST_SIZE;
151 break;
152 case CCP_SHA_TYPE_512:
153 rctx->cmd.u.sha.ctx_len = SHA512_DIGEST_SIZE;
154 break;
Gary R Hook4b394a22016-07-26 19:10:21 -0500155 default:
156 /* Should never get here */
157 break;
158 }
159
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600160 rctx->cmd.u.sha.src = sg;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600161 rctx->cmd.u.sha.src_len = rctx->hash_cnt;
Tom Lendackyc11baa02014-01-24 16:18:02 -0600162 rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
163 &ctx->u.sha.opad_sg : NULL;
164 rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
165 ctx->u.sha.opad_count : 0;
166 rctx->cmd.u.sha.first = rctx->first;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600167 rctx->cmd.u.sha.final = rctx->final;
168 rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
169
170 rctx->first = 0;
171
172 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
173
174 return ret;
Tom Lendacky355eba52015-10-01 16:32:31 -0500175
176e_free:
177 sg_free_table(&rctx->data_sg);
178
179 return ret;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600180}
181
182static int ccp_sha_init(struct ahash_request *req)
183{
184 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800185 struct ccp_ctx *ctx = crypto_ahash_ctx_dma(tfm);
186 struct ccp_sha_req_ctx *rctx = ahash_request_ctx_dma(req);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600187 struct ccp_crypto_ahash_alg *alg =
188 ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600189 unsigned int block_size =
190 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600191
192 memset(rctx, 0, sizeof(*rctx));
193
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600194 rctx->type = alg->type;
195 rctx->first = 1;
196
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600197 if (ctx->u.sha.key_len) {
198 /* Buffer the HMAC key for first update */
199 memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
200 rctx->buf_count = block_size;
201 }
202
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600203 return 0;
204}
205
206static int ccp_sha_update(struct ahash_request *req)
207{
208 return ccp_do_sha_update(req, req->nbytes, 0);
209}
210
211static int ccp_sha_final(struct ahash_request *req)
212{
213 return ccp_do_sha_update(req, 0, 1);
214}
215
216static int ccp_sha_finup(struct ahash_request *req)
217{
218 return ccp_do_sha_update(req, req->nbytes, 1);
219}
220
221static int ccp_sha_digest(struct ahash_request *req)
222{
Tom Lendacky82d15852014-01-06 13:34:23 -0600223 int ret;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600224
Tom Lendacky82d15852014-01-06 13:34:23 -0600225 ret = ccp_sha_init(req);
226 if (ret)
227 return ret;
228
229 return ccp_sha_finup(req);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600230}
231
Tom Lendacky952bce92016-01-12 11:17:38 -0600232static int ccp_sha_export(struct ahash_request *req, void *out)
233{
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800234 struct ccp_sha_req_ctx *rctx = ahash_request_ctx_dma(req);
Tom Lendackyb31dde22016-02-02 11:38:21 -0600235 struct ccp_sha_exp_ctx state;
Tom Lendacky952bce92016-01-12 11:17:38 -0600236
Tom Lendackyf709b452016-04-13 10:52:25 -0500237 /* Don't let anything leak to 'out' */
238 memset(&state, 0, sizeof(state));
239
Tom Lendackyb31dde22016-02-02 11:38:21 -0600240 state.type = rctx->type;
241 state.msg_bits = rctx->msg_bits;
242 state.first = rctx->first;
243 memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
244 state.buf_count = rctx->buf_count;
245 memcpy(state.buf, rctx->buf, sizeof(state.buf));
246
247 /* 'out' may not be aligned so memcpy from local variable */
248 memcpy(out, &state, sizeof(state));
Tom Lendacky952bce92016-01-12 11:17:38 -0600249
250 return 0;
251}
252
253static int ccp_sha_import(struct ahash_request *req, const void *in)
254{
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800255 struct ccp_sha_req_ctx *rctx = ahash_request_ctx_dma(req);
Tom Lendackyb31dde22016-02-02 11:38:21 -0600256 struct ccp_sha_exp_ctx state;
Tom Lendacky952bce92016-01-12 11:17:38 -0600257
Tom Lendackyb31dde22016-02-02 11:38:21 -0600258 /* 'in' may not be aligned so memcpy to local variable */
259 memcpy(&state, in, sizeof(state));
260
Tom Lendackyce0ae2662016-02-25 16:48:13 -0600261 memset(rctx, 0, sizeof(*rctx));
Tom Lendackyb31dde22016-02-02 11:38:21 -0600262 rctx->type = state.type;
263 rctx->msg_bits = state.msg_bits;
264 rctx->first = state.first;
265 memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
266 rctx->buf_count = state.buf_count;
267 memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
Tom Lendacky952bce92016-01-12 11:17:38 -0600268
269 return 0;
270}
271
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600272static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
273 unsigned int key_len)
274{
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800275 struct ccp_ctx *ctx = crypto_ahash_ctx_dma(tfm);
Tom Lendackyc11baa02014-01-24 16:18:02 -0600276 struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
Tom Lendackyc11baa02014-01-24 16:18:02 -0600277 unsigned int block_size = crypto_shash_blocksize(shash);
278 unsigned int digest_size = crypto_shash_digestsize(shash);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600279 int i, ret;
280
281 /* Set to zero until complete */
282 ctx->u.sha.key_len = 0;
283
284 /* Clear key area to provide zero padding for keys smaller
285 * than the block size
286 */
287 memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
288
289 if (key_len > block_size) {
290 /* Must hash the input key */
Eric Biggersf32b6772020-05-01 22:31:07 -0700291 ret = crypto_shash_tfm_digest(shash, key, key_len,
292 ctx->u.sha.key);
Eric Biggers674f3682019-12-30 21:19:36 -0600293 if (ret)
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600294 return -EINVAL;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600295
296 key_len = digest_size;
Tom Lendacky8db88462015-02-03 13:07:05 -0600297 } else {
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600298 memcpy(ctx->u.sha.key, key, key_len);
Tom Lendacky8db88462015-02-03 13:07:05 -0600299 }
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600300
301 for (i = 0; i < block_size; i++) {
Corentin LABBE6507c572017-05-19 08:53:31 +0200302 ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ HMAC_IPAD_VALUE;
303 ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ HMAC_OPAD_VALUE;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600304 }
305
Tom Lendackyc11baa02014-01-24 16:18:02 -0600306 sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
307 ctx->u.sha.opad_count = block_size;
308
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600309 ctx->u.sha.key_len = key_len;
310
311 return 0;
312}
313
314static int ccp_sha_cra_init(struct crypto_tfm *tfm)
315{
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600316 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800317 struct ccp_ctx *ctx = crypto_ahash_ctx_dma(ahash);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600318
319 ctx->complete = ccp_sha_complete;
320 ctx->u.sha.key_len = 0;
321
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800322 crypto_ahash_set_reqsize_dma(ahash, sizeof(struct ccp_sha_req_ctx));
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600323
324 return 0;
325}
326
327static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
328{
329}
330
331static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
332{
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800333 struct ccp_ctx *ctx = crypto_tfm_ctx_dma(tfm);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600334 struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
Tom Lendackyc11baa02014-01-24 16:18:02 -0600335 struct crypto_shash *hmac_tfm;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600336
Tom Lendackyc11baa02014-01-24 16:18:02 -0600337 hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600338 if (IS_ERR(hmac_tfm)) {
339 pr_warn("could not load driver %s need for HMAC support\n",
340 alg->child_alg);
341 return PTR_ERR(hmac_tfm);
342 }
343
344 ctx->u.sha.hmac_tfm = hmac_tfm;
345
346 return ccp_sha_cra_init(tfm);
347}
348
349static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
350{
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800351 struct ccp_ctx *ctx = crypto_tfm_ctx_dma(tfm);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600352
353 if (ctx->u.sha.hmac_tfm)
Tom Lendackyc11baa02014-01-24 16:18:02 -0600354 crypto_free_shash(ctx->u.sha.hmac_tfm);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600355
356 ccp_sha_cra_exit(tfm);
357}
358
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600359struct ccp_sha_def {
Gary R Hookc7019c42016-03-01 13:49:15 -0600360 unsigned int version;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600361 const char *name;
362 const char *drv_name;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600363 enum ccp_sha_type type;
364 u32 digest_size;
365 u32 block_size;
366};
367
368static struct ccp_sha_def sha_algs[] = {
369 {
Gary R Hookc7019c42016-03-01 13:49:15 -0600370 .version = CCP_VERSION(3, 0),
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600371 .name = "sha1",
372 .drv_name = "sha1-ccp",
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600373 .type = CCP_SHA_TYPE_1,
374 .digest_size = SHA1_DIGEST_SIZE,
375 .block_size = SHA1_BLOCK_SIZE,
376 },
377 {
Gary R Hookc7019c42016-03-01 13:49:15 -0600378 .version = CCP_VERSION(3, 0),
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600379 .name = "sha224",
380 .drv_name = "sha224-ccp",
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600381 .type = CCP_SHA_TYPE_224,
382 .digest_size = SHA224_DIGEST_SIZE,
383 .block_size = SHA224_BLOCK_SIZE,
384 },
385 {
Gary R Hookc7019c42016-03-01 13:49:15 -0600386 .version = CCP_VERSION(3, 0),
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600387 .name = "sha256",
388 .drv_name = "sha256-ccp",
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600389 .type = CCP_SHA_TYPE_256,
390 .digest_size = SHA256_DIGEST_SIZE,
391 .block_size = SHA256_BLOCK_SIZE,
392 },
Gary R Hookccebcf32017-03-15 13:20:43 -0500393 {
394 .version = CCP_VERSION(5, 0),
395 .name = "sha384",
396 .drv_name = "sha384-ccp",
397 .type = CCP_SHA_TYPE_384,
398 .digest_size = SHA384_DIGEST_SIZE,
399 .block_size = SHA384_BLOCK_SIZE,
400 },
401 {
402 .version = CCP_VERSION(5, 0),
403 .name = "sha512",
404 .drv_name = "sha512-ccp",
405 .type = CCP_SHA_TYPE_512,
406 .digest_size = SHA512_DIGEST_SIZE,
407 .block_size = SHA512_BLOCK_SIZE,
408 },
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600409};
410
411static int ccp_register_hmac_alg(struct list_head *head,
412 const struct ccp_sha_def *def,
413 const struct ccp_crypto_ahash_alg *base_alg)
414{
415 struct ccp_crypto_ahash_alg *ccp_alg;
416 struct ahash_alg *alg;
417 struct hash_alg_common *halg;
418 struct crypto_alg *base;
419 int ret;
420
421 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
422 if (!ccp_alg)
423 return -ENOMEM;
424
425 /* Copy the base algorithm and only change what's necessary */
Fengguang Wud1dd2062013-12-09 20:08:19 +0800426 *ccp_alg = *base_alg;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600427 INIT_LIST_HEAD(&ccp_alg->entry);
428
Herbert Xu2c2e1832020-07-09 22:44:04 +1000429 strscpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600430
431 alg = &ccp_alg->alg;
432 alg->setkey = ccp_sha_setkey;
433
434 halg = &alg->halg;
435
436 base = &halg->base;
437 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
438 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
439 def->drv_name);
440 base->cra_init = ccp_hmac_sha_cra_init;
441 base->cra_exit = ccp_hmac_sha_cra_exit;
442
443 ret = crypto_register_ahash(alg);
444 if (ret) {
445 pr_err("%s ahash algorithm registration error (%d)\n",
Tom Lendacky8db88462015-02-03 13:07:05 -0600446 base->cra_name, ret);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600447 kfree(ccp_alg);
448 return ret;
449 }
450
451 list_add(&ccp_alg->entry, head);
452
453 return ret;
454}
455
456static int ccp_register_sha_alg(struct list_head *head,
457 const struct ccp_sha_def *def)
458{
459 struct ccp_crypto_ahash_alg *ccp_alg;
460 struct ahash_alg *alg;
461 struct hash_alg_common *halg;
462 struct crypto_alg *base;
463 int ret;
464
465 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
466 if (!ccp_alg)
467 return -ENOMEM;
468
469 INIT_LIST_HEAD(&ccp_alg->entry);
470
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600471 ccp_alg->type = def->type;
472
473 alg = &ccp_alg->alg;
474 alg->init = ccp_sha_init;
475 alg->update = ccp_sha_update;
476 alg->final = ccp_sha_final;
477 alg->finup = ccp_sha_finup;
478 alg->digest = ccp_sha_digest;
Tom Lendacky952bce92016-01-12 11:17:38 -0600479 alg->export = ccp_sha_export;
480 alg->import = ccp_sha_import;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600481
482 halg = &alg->halg;
483 halg->digestsize = def->digest_size;
Tom Lendackyd1662162016-01-29 12:45:14 -0600484 halg->statesize = sizeof(struct ccp_sha_exp_ctx);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600485
486 base = &halg->base;
487 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
488 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
489 def->drv_name);
Eric Biggers6a38f6222018-06-30 15:16:12 -0700490 base->cra_flags = CRYPTO_ALG_ASYNC |
Mikulas Patockab8aa7dc2020-07-09 23:20:41 -0700491 CRYPTO_ALG_ALLOCATES_MEMORY |
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600492 CRYPTO_ALG_KERN_DRIVER_ONLY |
493 CRYPTO_ALG_NEED_FALLBACK;
494 base->cra_blocksize = def->block_size;
Herbert Xu99c6b20e2022-12-02 17:20:49 +0800495 base->cra_ctxsize = sizeof(struct ccp_ctx) + crypto_dma_padding();
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600496 base->cra_priority = CCP_CRA_PRIORITY;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600497 base->cra_init = ccp_sha_cra_init;
498 base->cra_exit = ccp_sha_cra_exit;
499 base->cra_module = THIS_MODULE;
500
501 ret = crypto_register_ahash(alg);
502 if (ret) {
503 pr_err("%s ahash algorithm registration error (%d)\n",
Tom Lendacky8db88462015-02-03 13:07:05 -0600504 base->cra_name, ret);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600505 kfree(ccp_alg);
506 return ret;
507 }
508
509 list_add(&ccp_alg->entry, head);
510
511 ret = ccp_register_hmac_alg(head, def, ccp_alg);
512
513 return ret;
514}
515
516int ccp_register_sha_algs(struct list_head *head)
517{
518 int i, ret;
Gary R Hookc7019c42016-03-01 13:49:15 -0600519 unsigned int ccpversion = ccp_version();
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600520
521 for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
Gary R Hookc7019c42016-03-01 13:49:15 -0600522 if (sha_algs[i].version > ccpversion)
523 continue;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600524 ret = ccp_register_sha_alg(head, &sha_algs[i]);
525 if (ret)
526 return ret;
527 }
528
529 return 0;
530}