]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/crypto/ccp/ccp-crypto-sha.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[mirror_ubuntu-hirsute-kernel.git] / drivers / crypto / ccp / ccp-crypto-sha.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
0ab0a1d5
TL
2/*
3 * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
4 *
fa5cd1c7 5 * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
0ab0a1d5
TL
6 *
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
4b394a23 8 * Author: Gary R Hook <gary.hook@amd.com>
0ab0a1d5
TL
9 */
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>
6507c57b 18#include <crypto/hmac.h>
0ab0a1d5
TL
19#include <crypto/internal/hash.h>
20#include <crypto/sha.h>
21#include <crypto/scatterwalk.h>
22
23#include "ccp-crypto.h"
24
0ab0a1d5
TL
25static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
26{
27 struct ahash_request *req = ahash_request_cast(async_req);
28 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
0ab0a1d5
TL
29 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
30 unsigned int digest_size = crypto_ahash_digestsize(tfm);
31
32 if (ret)
33 goto e_free;
34
35 if (rctx->hash_rem) {
36 /* Save remaining data to buffer */
81a59f00 37 unsigned int offset = rctx->nbytes - rctx->hash_rem;
8db88467 38
81a59f00
TL
39 scatterwalk_map_and_copy(rctx->buf, rctx->src,
40 offset, rctx->hash_rem, 0);
0ab0a1d5 41 rctx->buf_count = rctx->hash_rem;
8db88467 42 } else {
0ab0a1d5 43 rctx->buf_count = 0;
8db88467 44 }
0ab0a1d5 45
393897c5 46 /* Update result area if supplied */
0ee991be 47 if (req->result && rctx->final)
393897c5 48 memcpy(req->result, rctx->ctx, digest_size);
0ab0a1d5 49
0ab0a1d5
TL
50e_free:
51 sg_free_table(&rctx->data_sg);
52
53 return ret;
54}
55
56static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
57 unsigned int final)
58{
59 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
c11baa02 60 struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
0ab0a1d5
TL
61 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
62 struct scatterlist *sg;
63 unsigned int block_size =
64 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
81a59f00 65 unsigned int sg_count;
5258de8a 66 gfp_t gfp;
81a59f00 67 u64 len;
0ab0a1d5
TL
68 int ret;
69
81a59f00
TL
70 len = (u64)rctx->buf_count + (u64)nbytes;
71
72 if (!final && (len <= block_size)) {
0ab0a1d5
TL
73 scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
74 0, nbytes, 0);
75 rctx->buf_count += nbytes;
76
77 return 0;
78 }
79
81a59f00
TL
80 rctx->src = req->src;
81 rctx->nbytes = nbytes;
0ab0a1d5
TL
82
83 rctx->final = final;
81a59f00
TL
84 rctx->hash_rem = final ? 0 : len & (block_size - 1);
85 rctx->hash_cnt = len - rctx->hash_rem;
86 if (!final && !rctx->hash_rem) {
0ab0a1d5
TL
87 /* CCP can't do zero length final, so keep some data around */
88 rctx->hash_cnt -= block_size;
89 rctx->hash_rem = block_size;
90 }
91
92 /* Initialize the context scatterlist */
93 sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
94
0ab0a1d5 95 sg = NULL;
77dc4a51
TL
96 if (rctx->buf_count && nbytes) {
97 /* Build the data scatterlist table - allocate enough entries
98 * for both data pieces (buffer and input data)
99 */
100 gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
101 GFP_KERNEL : GFP_ATOMIC;
102 sg_count = sg_nents(req->src) + 1;
103 ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
104 if (ret)
105 return ret;
0ab0a1d5 106
0ab0a1d5
TL
107 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
108 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
355eba5d
TL
109 if (!sg) {
110 ret = -EINVAL;
111 goto e_free;
112 }
0ab0a1d5 113 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
355eba5d
TL
114 if (!sg) {
115 ret = -EINVAL;
116 goto e_free;
117 }
0ab0a1d5
TL
118 sg_mark_end(sg);
119
77dc4a51
TL
120 sg = rctx->data_sg.sgl;
121 } else if (rctx->buf_count) {
122 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
123
124 sg = &rctx->buf_sg;
125 } else if (nbytes) {
126 sg = req->src;
127 }
128
0ab0a1d5
TL
129 rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
130
131 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
132 INIT_LIST_HEAD(&rctx->cmd.entry);
133 rctx->cmd.engine = CCP_ENGINE_SHA;
134 rctx->cmd.u.sha.type = rctx->type;
135 rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
4b394a23
GH
136
137 switch (rctx->type) {
138 case CCP_SHA_TYPE_1:
139 rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE;
140 break;
141 case CCP_SHA_TYPE_224:
142 rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE;
143 break;
144 case CCP_SHA_TYPE_256:
145 rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE;
146 break;
ccebcf3f
GH
147 case CCP_SHA_TYPE_384:
148 rctx->cmd.u.sha.ctx_len = SHA384_DIGEST_SIZE;
149 break;
150 case CCP_SHA_TYPE_512:
151 rctx->cmd.u.sha.ctx_len = SHA512_DIGEST_SIZE;
152 break;
4b394a23
GH
153 default:
154 /* Should never get here */
155 break;
156 }
157
77dc4a51 158 rctx->cmd.u.sha.src = sg;
0ab0a1d5 159 rctx->cmd.u.sha.src_len = rctx->hash_cnt;
c11baa02
TL
160 rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
161 &ctx->u.sha.opad_sg : NULL;
162 rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
163 ctx->u.sha.opad_count : 0;
164 rctx->cmd.u.sha.first = rctx->first;
0ab0a1d5
TL
165 rctx->cmd.u.sha.final = rctx->final;
166 rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
167
168 rctx->first = 0;
169
170 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
171
355eba5d
TL
172 return ret;
173
174e_free:
175 sg_free_table(&rctx->data_sg);
176
0ab0a1d5
TL
177 return ret;
178}
179
180static int ccp_sha_init(struct ahash_request *req)
181{
182 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
77dc4a51 183 struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
0ab0a1d5
TL
184 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
185 struct ccp_crypto_ahash_alg *alg =
186 ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
77dc4a51
TL
187 unsigned int block_size =
188 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
0ab0a1d5
TL
189
190 memset(rctx, 0, sizeof(*rctx));
191
0ab0a1d5
TL
192 rctx->type = alg->type;
193 rctx->first = 1;
194
77dc4a51
TL
195 if (ctx->u.sha.key_len) {
196 /* Buffer the HMAC key for first update */
197 memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
198 rctx->buf_count = block_size;
199 }
200
0ab0a1d5
TL
201 return 0;
202}
203
204static int ccp_sha_update(struct ahash_request *req)
205{
206 return ccp_do_sha_update(req, req->nbytes, 0);
207}
208
209static int ccp_sha_final(struct ahash_request *req)
210{
211 return ccp_do_sha_update(req, 0, 1);
212}
213
214static int ccp_sha_finup(struct ahash_request *req)
215{
216 return ccp_do_sha_update(req, req->nbytes, 1);
217}
218
219static int ccp_sha_digest(struct ahash_request *req)
220{
82d1585b 221 int ret;
0ab0a1d5 222
82d1585b
TL
223 ret = ccp_sha_init(req);
224 if (ret)
225 return ret;
226
227 return ccp_sha_finup(req);
0ab0a1d5
TL
228}
229
952bce97
TL
230static int ccp_sha_export(struct ahash_request *req, void *out)
231{
232 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
b31dde2a 233 struct ccp_sha_exp_ctx state;
952bce97 234
f709b45e
TL
235 /* Don't let anything leak to 'out' */
236 memset(&state, 0, sizeof(state));
237
b31dde2a
TL
238 state.type = rctx->type;
239 state.msg_bits = rctx->msg_bits;
240 state.first = rctx->first;
241 memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
242 state.buf_count = rctx->buf_count;
243 memcpy(state.buf, rctx->buf, sizeof(state.buf));
244
245 /* 'out' may not be aligned so memcpy from local variable */
246 memcpy(out, &state, sizeof(state));
952bce97
TL
247
248 return 0;
249}
250
251static int ccp_sha_import(struct ahash_request *req, const void *in)
252{
253 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
b31dde2a
TL
254 struct ccp_sha_exp_ctx state;
255
256 /* 'in' may not be aligned so memcpy to local variable */
257 memcpy(&state, in, sizeof(state));
258
ce0ae266 259 memset(rctx, 0, sizeof(*rctx));
b31dde2a
TL
260 rctx->type = state.type;
261 rctx->msg_bits = state.msg_bits;
262 rctx->first = state.first;
263 memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
264 rctx->buf_count = state.buf_count;
265 memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
952bce97
TL
266
267 return 0;
268}
269
0ab0a1d5
TL
270static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
271 unsigned int key_len)
272{
273 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
c11baa02 274 struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
61ded524
JSM
275
276 SHASH_DESC_ON_STACK(sdesc, shash);
277
c11baa02
TL
278 unsigned int block_size = crypto_shash_blocksize(shash);
279 unsigned int digest_size = crypto_shash_digestsize(shash);
0ab0a1d5
TL
280 int i, ret;
281
282 /* Set to zero until complete */
283 ctx->u.sha.key_len = 0;
284
285 /* Clear key area to provide zero padding for keys smaller
286 * than the block size
287 */
288 memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
289
290 if (key_len > block_size) {
291 /* Must hash the input key */
61ded524 292 sdesc->tfm = shash;
c11baa02 293
61ded524 294 ret = crypto_shash_digest(sdesc, key, key_len,
c11baa02 295 ctx->u.sha.key);
0ab0a1d5
TL
296 if (ret) {
297 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
298 return -EINVAL;
299 }
300
301 key_len = digest_size;
8db88467 302 } else {
0ab0a1d5 303 memcpy(ctx->u.sha.key, key, key_len);
8db88467 304 }
0ab0a1d5
TL
305
306 for (i = 0; i < block_size; i++) {
6507c57b
CL
307 ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ HMAC_IPAD_VALUE;
308 ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ HMAC_OPAD_VALUE;
0ab0a1d5
TL
309 }
310
c11baa02
TL
311 sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
312 ctx->u.sha.opad_count = block_size;
313
0ab0a1d5
TL
314 ctx->u.sha.key_len = key_len;
315
316 return 0;
317}
318
319static int ccp_sha_cra_init(struct crypto_tfm *tfm)
320{
321 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
322 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
323
324 ctx->complete = ccp_sha_complete;
325 ctx->u.sha.key_len = 0;
326
327 crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
328
329 return 0;
330}
331
332static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
333{
334}
335
336static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
337{
338 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
339 struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
c11baa02 340 struct crypto_shash *hmac_tfm;
0ab0a1d5 341
c11baa02 342 hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
0ab0a1d5
TL
343 if (IS_ERR(hmac_tfm)) {
344 pr_warn("could not load driver %s need for HMAC support\n",
345 alg->child_alg);
346 return PTR_ERR(hmac_tfm);
347 }
348
349 ctx->u.sha.hmac_tfm = hmac_tfm;
350
351 return ccp_sha_cra_init(tfm);
352}
353
354static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
355{
356 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
357
358 if (ctx->u.sha.hmac_tfm)
c11baa02 359 crypto_free_shash(ctx->u.sha.hmac_tfm);
0ab0a1d5
TL
360
361 ccp_sha_cra_exit(tfm);
362}
363
0ab0a1d5 364struct ccp_sha_def {
c7019c4d 365 unsigned int version;
0ab0a1d5
TL
366 const char *name;
367 const char *drv_name;
0ab0a1d5
TL
368 enum ccp_sha_type type;
369 u32 digest_size;
370 u32 block_size;
371};
372
373static struct ccp_sha_def sha_algs[] = {
374 {
c7019c4d 375 .version = CCP_VERSION(3, 0),
0ab0a1d5
TL
376 .name = "sha1",
377 .drv_name = "sha1-ccp",
0ab0a1d5
TL
378 .type = CCP_SHA_TYPE_1,
379 .digest_size = SHA1_DIGEST_SIZE,
380 .block_size = SHA1_BLOCK_SIZE,
381 },
382 {
c7019c4d 383 .version = CCP_VERSION(3, 0),
0ab0a1d5
TL
384 .name = "sha224",
385 .drv_name = "sha224-ccp",
0ab0a1d5
TL
386 .type = CCP_SHA_TYPE_224,
387 .digest_size = SHA224_DIGEST_SIZE,
388 .block_size = SHA224_BLOCK_SIZE,
389 },
390 {
c7019c4d 391 .version = CCP_VERSION(3, 0),
0ab0a1d5
TL
392 .name = "sha256",
393 .drv_name = "sha256-ccp",
0ab0a1d5
TL
394 .type = CCP_SHA_TYPE_256,
395 .digest_size = SHA256_DIGEST_SIZE,
396 .block_size = SHA256_BLOCK_SIZE,
397 },
ccebcf3f
GH
398 {
399 .version = CCP_VERSION(5, 0),
400 .name = "sha384",
401 .drv_name = "sha384-ccp",
402 .type = CCP_SHA_TYPE_384,
403 .digest_size = SHA384_DIGEST_SIZE,
404 .block_size = SHA384_BLOCK_SIZE,
405 },
406 {
407 .version = CCP_VERSION(5, 0),
408 .name = "sha512",
409 .drv_name = "sha512-ccp",
410 .type = CCP_SHA_TYPE_512,
411 .digest_size = SHA512_DIGEST_SIZE,
412 .block_size = SHA512_BLOCK_SIZE,
413 },
0ab0a1d5
TL
414};
415
416static int ccp_register_hmac_alg(struct list_head *head,
417 const struct ccp_sha_def *def,
418 const struct ccp_crypto_ahash_alg *base_alg)
419{
420 struct ccp_crypto_ahash_alg *ccp_alg;
421 struct ahash_alg *alg;
422 struct hash_alg_common *halg;
423 struct crypto_alg *base;
424 int ret;
425
426 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
427 if (!ccp_alg)
428 return -ENOMEM;
429
430 /* Copy the base algorithm and only change what's necessary */
d1dd206c 431 *ccp_alg = *base_alg;
0ab0a1d5
TL
432 INIT_LIST_HEAD(&ccp_alg->entry);
433
434 strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
435
436 alg = &ccp_alg->alg;
437 alg->setkey = ccp_sha_setkey;
438
439 halg = &alg->halg;
440
441 base = &halg->base;
442 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
443 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
444 def->drv_name);
445 base->cra_init = ccp_hmac_sha_cra_init;
446 base->cra_exit = ccp_hmac_sha_cra_exit;
447
448 ret = crypto_register_ahash(alg);
449 if (ret) {
450 pr_err("%s ahash algorithm registration error (%d)\n",
8db88467 451 base->cra_name, ret);
0ab0a1d5
TL
452 kfree(ccp_alg);
453 return ret;
454 }
455
456 list_add(&ccp_alg->entry, head);
457
458 return ret;
459}
460
461static int ccp_register_sha_alg(struct list_head *head,
462 const struct ccp_sha_def *def)
463{
464 struct ccp_crypto_ahash_alg *ccp_alg;
465 struct ahash_alg *alg;
466 struct hash_alg_common *halg;
467 struct crypto_alg *base;
468 int ret;
469
470 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
471 if (!ccp_alg)
472 return -ENOMEM;
473
474 INIT_LIST_HEAD(&ccp_alg->entry);
475
0ab0a1d5
TL
476 ccp_alg->type = def->type;
477
478 alg = &ccp_alg->alg;
479 alg->init = ccp_sha_init;
480 alg->update = ccp_sha_update;
481 alg->final = ccp_sha_final;
482 alg->finup = ccp_sha_finup;
483 alg->digest = ccp_sha_digest;
952bce97
TL
484 alg->export = ccp_sha_export;
485 alg->import = ccp_sha_import;
0ab0a1d5
TL
486
487 halg = &alg->halg;
488 halg->digestsize = def->digest_size;
d1662165 489 halg->statesize = sizeof(struct ccp_sha_exp_ctx);
0ab0a1d5
TL
490
491 base = &halg->base;
492 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
493 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
494 def->drv_name);
6a38f622 495 base->cra_flags = CRYPTO_ALG_ASYNC |
0ab0a1d5
TL
496 CRYPTO_ALG_KERN_DRIVER_ONLY |
497 CRYPTO_ALG_NEED_FALLBACK;
498 base->cra_blocksize = def->block_size;
499 base->cra_ctxsize = sizeof(struct ccp_ctx);
500 base->cra_priority = CCP_CRA_PRIORITY;
0ab0a1d5
TL
501 base->cra_init = ccp_sha_cra_init;
502 base->cra_exit = ccp_sha_cra_exit;
503 base->cra_module = THIS_MODULE;
504
505 ret = crypto_register_ahash(alg);
506 if (ret) {
507 pr_err("%s ahash algorithm registration error (%d)\n",
8db88467 508 base->cra_name, ret);
0ab0a1d5
TL
509 kfree(ccp_alg);
510 return ret;
511 }
512
513 list_add(&ccp_alg->entry, head);
514
515 ret = ccp_register_hmac_alg(head, def, ccp_alg);
516
517 return ret;
518}
519
520int ccp_register_sha_algs(struct list_head *head)
521{
522 int i, ret;
c7019c4d 523 unsigned int ccpversion = ccp_version();
0ab0a1d5
TL
524
525 for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
c7019c4d
GH
526 if (sha_algs[i].version > ccpversion)
527 continue;
0ab0a1d5
TL
528 ret = ccp_register_sha_alg(head, &sha_algs[i]);
529 if (ret)
530 return ret;
531 }
532
533 return 0;
534}