blob: 183d16e46d2057b917beb480c0da16e2bb09d9b9 [file] [log] [blame]
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -06001/*
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
26
27struct ccp_sha_result {
28 struct completion completion;
29 int err;
30};
31
32static void ccp_sync_hash_complete(struct crypto_async_request *req, int err)
33{
34 struct ccp_sha_result *result = req->data;
35
36 if (err == -EINPROGRESS)
37 return;
38
39 result->err = err;
40 complete(&result->completion);
41}
42
43static int ccp_sync_hash(struct crypto_ahash *tfm, u8 *buf,
44 struct scatterlist *sg, unsigned int len)
45{
46 struct ccp_sha_result result;
47 struct ahash_request *req;
48 int ret;
49
50 init_completion(&result.completion);
51
52 req = ahash_request_alloc(tfm, GFP_KERNEL);
53 if (!req)
54 return -ENOMEM;
55
56 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
57 ccp_sync_hash_complete, &result);
58 ahash_request_set_crypt(req, sg, buf, len);
59
60 ret = crypto_ahash_digest(req);
61 if ((ret == -EINPROGRESS) || (ret == -EBUSY)) {
62 ret = wait_for_completion_interruptible(&result.completion);
63 if (!ret)
64 ret = result.err;
65 }
66
67 ahash_request_free(req);
68
69 return ret;
70}
71
72static int ccp_sha_finish_hmac(struct crypto_async_request *async_req)
73{
74 struct ahash_request *req = ahash_request_cast(async_req);
75 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
76 struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
Tom Lendacky393897c2014-01-06 13:34:11 -060077 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060078 struct scatterlist sg[2];
79 unsigned int block_size =
80 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
81 unsigned int digest_size = crypto_ahash_digestsize(tfm);
82
83 sg_init_table(sg, ARRAY_SIZE(sg));
84 sg_set_buf(&sg[0], ctx->u.sha.opad, block_size);
Tom Lendacky393897c2014-01-06 13:34:11 -060085 sg_set_buf(&sg[1], rctx->ctx, digest_size);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -060086
87 return ccp_sync_hash(ctx->u.sha.hmac_tfm, req->result, sg,
88 block_size + digest_size);
89}
90
91static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
92{
93 struct ahash_request *req = ahash_request_cast(async_req);
94 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
95 struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
96 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
97 unsigned int digest_size = crypto_ahash_digestsize(tfm);
98
99 if (ret)
100 goto e_free;
101
102 if (rctx->hash_rem) {
103 /* Save remaining data to buffer */
104 scatterwalk_map_and_copy(rctx->buf, rctx->cmd.u.sha.src,
105 rctx->hash_cnt, rctx->hash_rem, 0);
106 rctx->buf_count = rctx->hash_rem;
107 } else
108 rctx->buf_count = 0;
109
Tom Lendacky393897c2014-01-06 13:34:11 -0600110 /* Update result area if supplied */
111 if (req->result)
112 memcpy(req->result, rctx->ctx, digest_size);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600113
114 /* If we're doing an HMAC, we need to perform that on the final op */
115 if (rctx->final && ctx->u.sha.key_len)
116 ret = ccp_sha_finish_hmac(async_req);
117
118e_free:
119 sg_free_table(&rctx->data_sg);
120
121 return ret;
122}
123
124static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
125 unsigned int final)
126{
127 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600128 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
129 struct scatterlist *sg;
130 unsigned int block_size =
131 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
132 unsigned int len, sg_count;
Tom Lendacky5258de82014-01-06 13:33:59 -0600133 gfp_t gfp;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600134 int ret;
135
136 if (!final && ((nbytes + rctx->buf_count) <= block_size)) {
137 scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
138 0, nbytes, 0);
139 rctx->buf_count += nbytes;
140
141 return 0;
142 }
143
144 len = rctx->buf_count + nbytes;
145
146 rctx->final = final;
147 rctx->hash_cnt = final ? len : len & ~(block_size - 1);
148 rctx->hash_rem = final ? 0 : len & (block_size - 1);
149 if (!final && (rctx->hash_cnt == len)) {
150 /* CCP can't do zero length final, so keep some data around */
151 rctx->hash_cnt -= block_size;
152 rctx->hash_rem = block_size;
153 }
154
155 /* Initialize the context scatterlist */
156 sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
157
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600158 sg = NULL;
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600159 if (rctx->buf_count && nbytes) {
160 /* Build the data scatterlist table - allocate enough entries
161 * for both data pieces (buffer and input data)
162 */
163 gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
164 GFP_KERNEL : GFP_ATOMIC;
165 sg_count = sg_nents(req->src) + 1;
166 ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
167 if (ret)
168 return ret;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600169
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600170 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
171 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600172 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600173 sg_mark_end(sg);
174
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600175 sg = rctx->data_sg.sgl;
176 } else if (rctx->buf_count) {
177 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
178
179 sg = &rctx->buf_sg;
180 } else if (nbytes) {
181 sg = req->src;
182 }
183
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600184 rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
185
186 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
187 INIT_LIST_HEAD(&rctx->cmd.entry);
188 rctx->cmd.engine = CCP_ENGINE_SHA;
189 rctx->cmd.u.sha.type = rctx->type;
190 rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
191 rctx->cmd.u.sha.ctx_len = sizeof(rctx->ctx);
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600192 rctx->cmd.u.sha.src = sg;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600193 rctx->cmd.u.sha.src_len = rctx->hash_cnt;
194 rctx->cmd.u.sha.final = rctx->final;
195 rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
196
197 rctx->first = 0;
198
199 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
200
201 return ret;
202}
203
204static int ccp_sha_init(struct ahash_request *req)
205{
206 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600207 struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600208 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
209 struct ccp_crypto_ahash_alg *alg =
210 ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600211 unsigned int block_size =
212 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600213
214 memset(rctx, 0, sizeof(*rctx));
215
216 memcpy(rctx->ctx, alg->init, sizeof(rctx->ctx));
217 rctx->type = alg->type;
218 rctx->first = 1;
219
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600220 if (ctx->u.sha.key_len) {
221 /* Buffer the HMAC key for first update */
222 memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
223 rctx->buf_count = block_size;
224 }
225
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600226 return 0;
227}
228
229static int ccp_sha_update(struct ahash_request *req)
230{
231 return ccp_do_sha_update(req, req->nbytes, 0);
232}
233
234static int ccp_sha_final(struct ahash_request *req)
235{
236 return ccp_do_sha_update(req, 0, 1);
237}
238
239static int ccp_sha_finup(struct ahash_request *req)
240{
241 return ccp_do_sha_update(req, req->nbytes, 1);
242}
243
244static int ccp_sha_digest(struct ahash_request *req)
245{
246 ccp_sha_init(req);
247
248 return ccp_do_sha_update(req, req->nbytes, 1);
249}
250
251static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
252 unsigned int key_len)
253{
254 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
255 struct scatterlist sg;
256 unsigned int block_size =
257 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
258 unsigned int digest_size = crypto_ahash_digestsize(tfm);
259 int i, ret;
260
261 /* Set to zero until complete */
262 ctx->u.sha.key_len = 0;
263
264 /* Clear key area to provide zero padding for keys smaller
265 * than the block size
266 */
267 memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
268
269 if (key_len > block_size) {
270 /* Must hash the input key */
271 sg_init_one(&sg, key, key_len);
272 ret = ccp_sync_hash(tfm, ctx->u.sha.key, &sg, key_len);
273 if (ret) {
274 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
275 return -EINVAL;
276 }
277
278 key_len = digest_size;
279 } else
280 memcpy(ctx->u.sha.key, key, key_len);
281
282 for (i = 0; i < block_size; i++) {
283 ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ 0x36;
284 ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c;
285 }
286
287 ctx->u.sha.key_len = key_len;
288
289 return 0;
290}
291
292static int ccp_sha_cra_init(struct crypto_tfm *tfm)
293{
294 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
295 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
296
297 ctx->complete = ccp_sha_complete;
298 ctx->u.sha.key_len = 0;
299
300 crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
301
302 return 0;
303}
304
305static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
306{
307}
308
309static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
310{
311 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
312 struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
313 struct crypto_ahash *hmac_tfm;
314
315 hmac_tfm = crypto_alloc_ahash(alg->child_alg,
316 CRYPTO_ALG_TYPE_AHASH, 0);
317 if (IS_ERR(hmac_tfm)) {
318 pr_warn("could not load driver %s need for HMAC support\n",
319 alg->child_alg);
320 return PTR_ERR(hmac_tfm);
321 }
322
323 ctx->u.sha.hmac_tfm = hmac_tfm;
324
325 return ccp_sha_cra_init(tfm);
326}
327
328static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
329{
330 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
331
332 if (ctx->u.sha.hmac_tfm)
333 crypto_free_ahash(ctx->u.sha.hmac_tfm);
334
335 ccp_sha_cra_exit(tfm);
336}
337
Tom Lendacky6f0be9b2013-12-10 10:38:33 -0600338static const __be32 sha1_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600339 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
340 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
341 cpu_to_be32(SHA1_H4), 0, 0, 0,
342};
343
Tom Lendacky6f0be9b2013-12-10 10:38:33 -0600344static const __be32 sha224_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600345 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
346 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
347 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
348 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
349};
350
Tom Lendacky6f0be9b2013-12-10 10:38:33 -0600351static const __be32 sha256_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600352 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
353 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
354 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
355 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
356};
357
358struct ccp_sha_def {
359 const char *name;
360 const char *drv_name;
Tom Lendacky6f0be9b2013-12-10 10:38:33 -0600361 const __be32 *init;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600362 enum ccp_sha_type type;
363 u32 digest_size;
364 u32 block_size;
365};
366
367static struct ccp_sha_def sha_algs[] = {
368 {
369 .name = "sha1",
370 .drv_name = "sha1-ccp",
371 .init = sha1_init,
372 .type = CCP_SHA_TYPE_1,
373 .digest_size = SHA1_DIGEST_SIZE,
374 .block_size = SHA1_BLOCK_SIZE,
375 },
376 {
377 .name = "sha224",
378 .drv_name = "sha224-ccp",
379 .init = sha224_init,
380 .type = CCP_SHA_TYPE_224,
381 .digest_size = SHA224_DIGEST_SIZE,
382 .block_size = SHA224_BLOCK_SIZE,
383 },
384 {
385 .name = "sha256",
386 .drv_name = "sha256-ccp",
387 .init = sha256_init,
388 .type = CCP_SHA_TYPE_256,
389 .digest_size = SHA256_DIGEST_SIZE,
390 .block_size = SHA256_BLOCK_SIZE,
391 },
392};
393
394static int ccp_register_hmac_alg(struct list_head *head,
395 const struct ccp_sha_def *def,
396 const struct ccp_crypto_ahash_alg *base_alg)
397{
398 struct ccp_crypto_ahash_alg *ccp_alg;
399 struct ahash_alg *alg;
400 struct hash_alg_common *halg;
401 struct crypto_alg *base;
402 int ret;
403
404 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
405 if (!ccp_alg)
406 return -ENOMEM;
407
408 /* Copy the base algorithm and only change what's necessary */
Fengguang Wud1dd2062013-12-09 20:08:19 +0800409 *ccp_alg = *base_alg;
Tom Lendacky0ab0a1d2013-11-12 11:46:45 -0600410 INIT_LIST_HEAD(&ccp_alg->entry);
411
412 strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
413
414 alg = &ccp_alg->alg;
415 alg->setkey = ccp_sha_setkey;
416
417 halg = &alg->halg;
418
419 base = &halg->base;
420 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
421 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
422 def->drv_name);
423 base->cra_init = ccp_hmac_sha_cra_init;
424 base->cra_exit = ccp_hmac_sha_cra_exit;
425
426 ret = crypto_register_ahash(alg);
427 if (ret) {
428 pr_err("%s ahash algorithm registration error (%d)\n",
429 base->cra_name, ret);
430 kfree(ccp_alg);
431 return ret;
432 }
433
434 list_add(&ccp_alg->entry, head);
435
436 return ret;
437}
438
439static int ccp_register_sha_alg(struct list_head *head,
440 const struct ccp_sha_def *def)
441{
442 struct ccp_crypto_ahash_alg *ccp_alg;
443 struct ahash_alg *alg;
444 struct hash_alg_common *halg;
445 struct crypto_alg *base;
446 int ret;
447
448 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
449 if (!ccp_alg)
450 return -ENOMEM;
451
452 INIT_LIST_HEAD(&ccp_alg->entry);
453
454 ccp_alg->init = def->init;
455 ccp_alg->type = def->type;
456
457 alg = &ccp_alg->alg;
458 alg->init = ccp_sha_init;
459 alg->update = ccp_sha_update;
460 alg->final = ccp_sha_final;
461 alg->finup = ccp_sha_finup;
462 alg->digest = ccp_sha_digest;
463
464 halg = &alg->halg;
465 halg->digestsize = def->digest_size;
466
467 base = &halg->base;
468 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
469 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
470 def->drv_name);
471 base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
472 CRYPTO_ALG_KERN_DRIVER_ONLY |
473 CRYPTO_ALG_NEED_FALLBACK;
474 base->cra_blocksize = def->block_size;
475 base->cra_ctxsize = sizeof(struct ccp_ctx);
476 base->cra_priority = CCP_CRA_PRIORITY;
477 base->cra_type = &crypto_ahash_type;
478 base->cra_init = ccp_sha_cra_init;
479 base->cra_exit = ccp_sha_cra_exit;
480 base->cra_module = THIS_MODULE;
481
482 ret = crypto_register_ahash(alg);
483 if (ret) {
484 pr_err("%s ahash algorithm registration error (%d)\n",
485 base->cra_name, ret);
486 kfree(ccp_alg);
487 return ret;
488 }
489
490 list_add(&ccp_alg->entry, head);
491
492 ret = ccp_register_hmac_alg(head, def, ccp_alg);
493
494 return ret;
495}
496
497int ccp_register_sha_algs(struct list_head *head)
498{
499 int i, ret;
500
501 for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
502 ret = ccp_register_sha_alg(head, &sha_algs[i]);
503 if (ret)
504 return ret;
505 }
506
507 return 0;
508}