2 * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
4 * Copyright (C) 2013 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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/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>
24 #include "ccp-crypto.h"
27 struct ccp_sha_result
{
28 struct completion completion
;
32 static void ccp_sync_hash_complete(struct crypto_async_request
*req
, int err
)
34 struct ccp_sha_result
*result
= req
->data
;
36 if (err
== -EINPROGRESS
)
40 complete(&result
->completion
);
43 static int ccp_sync_hash(struct crypto_ahash
*tfm
, u8
*buf
,
44 struct scatterlist
*sg
, unsigned int len
)
46 struct ccp_sha_result result
;
47 struct ahash_request
*req
;
50 init_completion(&result
.completion
);
52 req
= ahash_request_alloc(tfm
, GFP_KERNEL
);
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
);
60 ret
= crypto_ahash_digest(req
);
61 if ((ret
== -EINPROGRESS
) || (ret
== -EBUSY
)) {
62 ret
= wait_for_completion_interruptible(&result
.completion
);
67 ahash_request_free(req
);
72 static int ccp_sha_finish_hmac(struct crypto_async_request
*async_req
)
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
);
77 struct scatterlist sg
[2];
78 unsigned int block_size
=
79 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm
));
80 unsigned int digest_size
= crypto_ahash_digestsize(tfm
);
82 sg_init_table(sg
, ARRAY_SIZE(sg
));
83 sg_set_buf(&sg
[0], ctx
->u
.sha
.opad
, block_size
);
84 sg_set_buf(&sg
[1], req
->result
, digest_size
);
86 return ccp_sync_hash(ctx
->u
.sha
.hmac_tfm
, req
->result
, sg
,
87 block_size
+ digest_size
);
90 static int ccp_sha_complete(struct crypto_async_request
*async_req
, int ret
)
92 struct ahash_request
*req
= ahash_request_cast(async_req
);
93 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
94 struct ccp_ctx
*ctx
= crypto_ahash_ctx(tfm
);
95 struct ccp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
96 unsigned int digest_size
= crypto_ahash_digestsize(tfm
);
101 if (rctx
->hash_rem
) {
102 /* Save remaining data to buffer */
103 scatterwalk_map_and_copy(rctx
->buf
, rctx
->cmd
.u
.sha
.src
,
104 rctx
->hash_cnt
, rctx
->hash_rem
, 0);
105 rctx
->buf_count
= rctx
->hash_rem
;
109 memcpy(req
->result
, rctx
->ctx
, digest_size
);
111 /* If we're doing an HMAC, we need to perform that on the final op */
112 if (rctx
->final
&& ctx
->u
.sha
.key_len
)
113 ret
= ccp_sha_finish_hmac(async_req
);
116 sg_free_table(&rctx
->data_sg
);
121 static int ccp_do_sha_update(struct ahash_request
*req
, unsigned int nbytes
,
124 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
125 struct ccp_ctx
*ctx
= crypto_ahash_ctx(tfm
);
126 struct ccp_sha_req_ctx
*rctx
= ahash_request_ctx(req
);
127 struct scatterlist
*sg
;
128 unsigned int block_size
=
129 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm
));
130 unsigned int len
, sg_count
;
134 if (!final
&& ((nbytes
+ rctx
->buf_count
) <= block_size
)) {
135 scatterwalk_map_and_copy(rctx
->buf
+ rctx
->buf_count
, req
->src
,
137 rctx
->buf_count
+= nbytes
;
142 len
= rctx
->buf_count
+ nbytes
;
145 rctx
->hash_cnt
= final
? len
: len
& ~(block_size
- 1);
146 rctx
->hash_rem
= final
? 0 : len
& (block_size
- 1);
147 if (!final
&& (rctx
->hash_cnt
== len
)) {
148 /* CCP can't do zero length final, so keep some data around */
149 rctx
->hash_cnt
-= block_size
;
150 rctx
->hash_rem
= block_size
;
153 /* Initialize the context scatterlist */
154 sg_init_one(&rctx
->ctx_sg
, rctx
->ctx
, sizeof(rctx
->ctx
));
156 /* Build the data scatterlist table - allocate enough entries for all
157 * possible data pieces (hmac ipad, buffer, input data)
159 sg_count
= (nbytes
) ? sg_nents(req
->src
) + 2 : 2;
160 gfp
= req
->base
.flags
& CRYPTO_TFM_REQ_MAY_SLEEP
?
161 GFP_KERNEL
: GFP_ATOMIC
;
162 ret
= sg_alloc_table(&rctx
->data_sg
, sg_count
, gfp
);
167 if (rctx
->first
&& ctx
->u
.sha
.key_len
) {
168 rctx
->hash_cnt
+= block_size
;
170 sg_init_one(&rctx
->pad_sg
, ctx
->u
.sha
.ipad
, block_size
);
171 sg
= ccp_crypto_sg_table_add(&rctx
->data_sg
, &rctx
->pad_sg
);
174 if (rctx
->buf_count
) {
175 sg_init_one(&rctx
->buf_sg
, rctx
->buf
, rctx
->buf_count
);
176 sg
= ccp_crypto_sg_table_add(&rctx
->data_sg
, &rctx
->buf_sg
);
180 sg
= ccp_crypto_sg_table_add(&rctx
->data_sg
, req
->src
);
185 rctx
->msg_bits
+= (rctx
->hash_cnt
<< 3); /* Total in bits */
187 memset(&rctx
->cmd
, 0, sizeof(rctx
->cmd
));
188 INIT_LIST_HEAD(&rctx
->cmd
.entry
);
189 rctx
->cmd
.engine
= CCP_ENGINE_SHA
;
190 rctx
->cmd
.u
.sha
.type
= rctx
->type
;
191 rctx
->cmd
.u
.sha
.ctx
= &rctx
->ctx_sg
;
192 rctx
->cmd
.u
.sha
.ctx_len
= sizeof(rctx
->ctx
);
193 rctx
->cmd
.u
.sha
.src
= (sg
) ? rctx
->data_sg
.sgl
: NULL
;
194 rctx
->cmd
.u
.sha
.src_len
= rctx
->hash_cnt
;
195 rctx
->cmd
.u
.sha
.final
= rctx
->final
;
196 rctx
->cmd
.u
.sha
.msg_bits
= rctx
->msg_bits
;
200 ret
= ccp_crypto_enqueue_request(&req
->base
, &rctx
->cmd
);
205 static int ccp_sha_init(struct ahash_request
*req
)
207 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
208 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
));
212 memset(rctx
, 0, sizeof(*rctx
));
214 memcpy(rctx
->ctx
, alg
->init
, sizeof(rctx
->ctx
));
215 rctx
->type
= alg
->type
;
221 static int ccp_sha_update(struct ahash_request
*req
)
223 return ccp_do_sha_update(req
, req
->nbytes
, 0);
226 static int ccp_sha_final(struct ahash_request
*req
)
228 return ccp_do_sha_update(req
, 0, 1);
231 static int ccp_sha_finup(struct ahash_request
*req
)
233 return ccp_do_sha_update(req
, req
->nbytes
, 1);
236 static int ccp_sha_digest(struct ahash_request
*req
)
240 return ccp_do_sha_update(req
, req
->nbytes
, 1);
243 static int ccp_sha_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
244 unsigned int key_len
)
246 struct ccp_ctx
*ctx
= crypto_tfm_ctx(crypto_ahash_tfm(tfm
));
247 struct scatterlist sg
;
248 unsigned int block_size
=
249 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm
));
250 unsigned int digest_size
= crypto_ahash_digestsize(tfm
);
253 /* Set to zero until complete */
254 ctx
->u
.sha
.key_len
= 0;
256 /* Clear key area to provide zero padding for keys smaller
257 * than the block size
259 memset(ctx
->u
.sha
.key
, 0, sizeof(ctx
->u
.sha
.key
));
261 if (key_len
> block_size
) {
262 /* Must hash the input key */
263 sg_init_one(&sg
, key
, key_len
);
264 ret
= ccp_sync_hash(tfm
, ctx
->u
.sha
.key
, &sg
, key_len
);
266 crypto_ahash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
270 key_len
= digest_size
;
272 memcpy(ctx
->u
.sha
.key
, key
, key_len
);
274 for (i
= 0; i
< block_size
; i
++) {
275 ctx
->u
.sha
.ipad
[i
] = ctx
->u
.sha
.key
[i
] ^ 0x36;
276 ctx
->u
.sha
.opad
[i
] = ctx
->u
.sha
.key
[i
] ^ 0x5c;
279 ctx
->u
.sha
.key_len
= key_len
;
284 static int ccp_sha_cra_init(struct crypto_tfm
*tfm
)
286 struct ccp_ctx
*ctx
= crypto_tfm_ctx(tfm
);
287 struct crypto_ahash
*ahash
= __crypto_ahash_cast(tfm
);
289 ctx
->complete
= ccp_sha_complete
;
290 ctx
->u
.sha
.key_len
= 0;
292 crypto_ahash_set_reqsize(ahash
, sizeof(struct ccp_sha_req_ctx
));
297 static void ccp_sha_cra_exit(struct crypto_tfm
*tfm
)
301 static int ccp_hmac_sha_cra_init(struct crypto_tfm
*tfm
)
303 struct ccp_ctx
*ctx
= crypto_tfm_ctx(tfm
);
304 struct ccp_crypto_ahash_alg
*alg
= ccp_crypto_ahash_alg(tfm
);
305 struct crypto_ahash
*hmac_tfm
;
307 hmac_tfm
= crypto_alloc_ahash(alg
->child_alg
,
308 CRYPTO_ALG_TYPE_AHASH
, 0);
309 if (IS_ERR(hmac_tfm
)) {
310 pr_warn("could not load driver %s need for HMAC support\n",
312 return PTR_ERR(hmac_tfm
);
315 ctx
->u
.sha
.hmac_tfm
= hmac_tfm
;
317 return ccp_sha_cra_init(tfm
);
320 static void ccp_hmac_sha_cra_exit(struct crypto_tfm
*tfm
)
322 struct ccp_ctx
*ctx
= crypto_tfm_ctx(tfm
);
324 if (ctx
->u
.sha
.hmac_tfm
)
325 crypto_free_ahash(ctx
->u
.sha
.hmac_tfm
);
327 ccp_sha_cra_exit(tfm
);
330 static const __be32 sha1_init
[CCP_SHA_CTXSIZE
/ sizeof(__be32
)] = {
331 cpu_to_be32(SHA1_H0
), cpu_to_be32(SHA1_H1
),
332 cpu_to_be32(SHA1_H2
), cpu_to_be32(SHA1_H3
),
333 cpu_to_be32(SHA1_H4
), 0, 0, 0,
336 static const __be32 sha224_init
[CCP_SHA_CTXSIZE
/ sizeof(__be32
)] = {
337 cpu_to_be32(SHA224_H0
), cpu_to_be32(SHA224_H1
),
338 cpu_to_be32(SHA224_H2
), cpu_to_be32(SHA224_H3
),
339 cpu_to_be32(SHA224_H4
), cpu_to_be32(SHA224_H5
),
340 cpu_to_be32(SHA224_H6
), cpu_to_be32(SHA224_H7
),
343 static const __be32 sha256_init
[CCP_SHA_CTXSIZE
/ sizeof(__be32
)] = {
344 cpu_to_be32(SHA256_H0
), cpu_to_be32(SHA256_H1
),
345 cpu_to_be32(SHA256_H2
), cpu_to_be32(SHA256_H3
),
346 cpu_to_be32(SHA256_H4
), cpu_to_be32(SHA256_H5
),
347 cpu_to_be32(SHA256_H6
), cpu_to_be32(SHA256_H7
),
352 const char *drv_name
;
354 enum ccp_sha_type type
;
359 static struct ccp_sha_def sha_algs
[] = {
362 .drv_name
= "sha1-ccp",
364 .type
= CCP_SHA_TYPE_1
,
365 .digest_size
= SHA1_DIGEST_SIZE
,
366 .block_size
= SHA1_BLOCK_SIZE
,
370 .drv_name
= "sha224-ccp",
372 .type
= CCP_SHA_TYPE_224
,
373 .digest_size
= SHA224_DIGEST_SIZE
,
374 .block_size
= SHA224_BLOCK_SIZE
,
378 .drv_name
= "sha256-ccp",
380 .type
= CCP_SHA_TYPE_256
,
381 .digest_size
= SHA256_DIGEST_SIZE
,
382 .block_size
= SHA256_BLOCK_SIZE
,
386 static int ccp_register_hmac_alg(struct list_head
*head
,
387 const struct ccp_sha_def
*def
,
388 const struct ccp_crypto_ahash_alg
*base_alg
)
390 struct ccp_crypto_ahash_alg
*ccp_alg
;
391 struct ahash_alg
*alg
;
392 struct hash_alg_common
*halg
;
393 struct crypto_alg
*base
;
396 ccp_alg
= kzalloc(sizeof(*ccp_alg
), GFP_KERNEL
);
400 /* Copy the base algorithm and only change what's necessary */
401 *ccp_alg
= *base_alg
;
402 INIT_LIST_HEAD(&ccp_alg
->entry
);
404 strncpy(ccp_alg
->child_alg
, def
->name
, CRYPTO_MAX_ALG_NAME
);
407 alg
->setkey
= ccp_sha_setkey
;
412 snprintf(base
->cra_name
, CRYPTO_MAX_ALG_NAME
, "hmac(%s)", def
->name
);
413 snprintf(base
->cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "hmac-%s",
415 base
->cra_init
= ccp_hmac_sha_cra_init
;
416 base
->cra_exit
= ccp_hmac_sha_cra_exit
;
418 ret
= crypto_register_ahash(alg
);
420 pr_err("%s ahash algorithm registration error (%d)\n",
421 base
->cra_name
, ret
);
426 list_add(&ccp_alg
->entry
, head
);
431 static int ccp_register_sha_alg(struct list_head
*head
,
432 const struct ccp_sha_def
*def
)
434 struct ccp_crypto_ahash_alg
*ccp_alg
;
435 struct ahash_alg
*alg
;
436 struct hash_alg_common
*halg
;
437 struct crypto_alg
*base
;
440 ccp_alg
= kzalloc(sizeof(*ccp_alg
), GFP_KERNEL
);
444 INIT_LIST_HEAD(&ccp_alg
->entry
);
446 ccp_alg
->init
= def
->init
;
447 ccp_alg
->type
= def
->type
;
450 alg
->init
= ccp_sha_init
;
451 alg
->update
= ccp_sha_update
;
452 alg
->final
= ccp_sha_final
;
453 alg
->finup
= ccp_sha_finup
;
454 alg
->digest
= ccp_sha_digest
;
457 halg
->digestsize
= def
->digest_size
;
460 snprintf(base
->cra_name
, CRYPTO_MAX_ALG_NAME
, "%s", def
->name
);
461 snprintf(base
->cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s",
463 base
->cra_flags
= CRYPTO_ALG_TYPE_AHASH
| CRYPTO_ALG_ASYNC
|
464 CRYPTO_ALG_KERN_DRIVER_ONLY
|
465 CRYPTO_ALG_NEED_FALLBACK
;
466 base
->cra_blocksize
= def
->block_size
;
467 base
->cra_ctxsize
= sizeof(struct ccp_ctx
);
468 base
->cra_priority
= CCP_CRA_PRIORITY
;
469 base
->cra_type
= &crypto_ahash_type
;
470 base
->cra_init
= ccp_sha_cra_init
;
471 base
->cra_exit
= ccp_sha_cra_exit
;
472 base
->cra_module
= THIS_MODULE
;
474 ret
= crypto_register_ahash(alg
);
476 pr_err("%s ahash algorithm registration error (%d)\n",
477 base
->cra_name
, ret
);
482 list_add(&ccp_alg
->entry
, head
);
484 ret
= ccp_register_hmac_alg(head
, def
, ccp_alg
);
489 int ccp_register_sha_algs(struct list_head
*head
)
493 for (i
= 0; i
< ARRAY_SIZE(sha_algs
); i
++) {
494 ret
= ccp_register_sha_alg(head
, &sha_algs
[i
]);