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