Lines Matching +full:sg +full:- +full:micro
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
24 #include "ccp-crypto.h"
36 if (rctx->hash_rem) { in ccp_sha_complete()
38 unsigned int offset = rctx->nbytes - rctx->hash_rem; in ccp_sha_complete()
40 scatterwalk_map_and_copy(rctx->buf, rctx->src, in ccp_sha_complete()
41 offset, rctx->hash_rem, 0); in ccp_sha_complete()
42 rctx->buf_count = rctx->hash_rem; in ccp_sha_complete()
44 rctx->buf_count = 0; in ccp_sha_complete()
48 if (req->result && rctx->final) in ccp_sha_complete()
49 memcpy(req->result, rctx->ctx, digest_size); in ccp_sha_complete()
52 sg_free_table(&rctx->data_sg); in ccp_sha_complete()
63 struct scatterlist *sg; in ccp_do_sha_update() local
71 len = (u64)rctx->buf_count + (u64)nbytes; in ccp_do_sha_update()
74 scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src, in ccp_do_sha_update()
76 rctx->buf_count += nbytes; in ccp_do_sha_update()
81 rctx->src = req->src; in ccp_do_sha_update()
82 rctx->nbytes = nbytes; in ccp_do_sha_update()
84 rctx->final = final; in ccp_do_sha_update()
85 rctx->hash_rem = final ? 0 : len & (block_size - 1); in ccp_do_sha_update()
86 rctx->hash_cnt = len - rctx->hash_rem; in ccp_do_sha_update()
87 if (!final && !rctx->hash_rem) { in ccp_do_sha_update()
89 rctx->hash_cnt -= block_size; in ccp_do_sha_update()
90 rctx->hash_rem = block_size; in ccp_do_sha_update()
94 sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx)); in ccp_do_sha_update()
96 sg = NULL; in ccp_do_sha_update()
97 if (rctx->buf_count && nbytes) { in ccp_do_sha_update()
98 /* Build the data scatterlist table - allocate enough entries in ccp_do_sha_update()
101 gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? in ccp_do_sha_update()
103 sg_count = sg_nents(req->src) + 1; in ccp_do_sha_update()
104 ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp); in ccp_do_sha_update()
108 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count); in ccp_do_sha_update()
109 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg); in ccp_do_sha_update()
110 if (!sg) { in ccp_do_sha_update()
111 ret = -EINVAL; in ccp_do_sha_update()
114 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src); in ccp_do_sha_update()
115 if (!sg) { in ccp_do_sha_update()
116 ret = -EINVAL; in ccp_do_sha_update()
119 sg_mark_end(sg); in ccp_do_sha_update()
121 sg = rctx->data_sg.sgl; in ccp_do_sha_update()
122 } else if (rctx->buf_count) { in ccp_do_sha_update()
123 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count); in ccp_do_sha_update()
125 sg = &rctx->buf_sg; in ccp_do_sha_update()
127 sg = req->src; in ccp_do_sha_update()
130 rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */ in ccp_do_sha_update()
132 memset(&rctx->cmd, 0, sizeof(rctx->cmd)); in ccp_do_sha_update()
133 INIT_LIST_HEAD(&rctx->cmd.entry); in ccp_do_sha_update()
134 rctx->cmd.engine = CCP_ENGINE_SHA; in ccp_do_sha_update()
135 rctx->cmd.u.sha.type = rctx->type; in ccp_do_sha_update()
136 rctx->cmd.u.sha.ctx = &rctx->ctx_sg; in ccp_do_sha_update()
138 switch (rctx->type) { in ccp_do_sha_update()
140 rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE; in ccp_do_sha_update()
143 rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE; in ccp_do_sha_update()
146 rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE; in ccp_do_sha_update()
149 rctx->cmd.u.sha.ctx_len = SHA384_DIGEST_SIZE; in ccp_do_sha_update()
152 rctx->cmd.u.sha.ctx_len = SHA512_DIGEST_SIZE; in ccp_do_sha_update()
159 rctx->cmd.u.sha.src = sg; in ccp_do_sha_update()
160 rctx->cmd.u.sha.src_len = rctx->hash_cnt; in ccp_do_sha_update()
161 rctx->cmd.u.sha.opad = ctx->u.sha.key_len ? in ccp_do_sha_update()
162 &ctx->u.sha.opad_sg : NULL; in ccp_do_sha_update()
163 rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ? in ccp_do_sha_update()
164 ctx->u.sha.opad_count : 0; in ccp_do_sha_update()
165 rctx->cmd.u.sha.first = rctx->first; in ccp_do_sha_update()
166 rctx->cmd.u.sha.final = rctx->final; in ccp_do_sha_update()
167 rctx->cmd.u.sha.msg_bits = rctx->msg_bits; in ccp_do_sha_update()
169 rctx->first = 0; in ccp_do_sha_update()
171 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); in ccp_do_sha_update()
176 sg_free_table(&rctx->data_sg); in ccp_do_sha_update()
193 rctx->type = alg->type; in ccp_sha_init()
194 rctx->first = 1; in ccp_sha_init()
196 if (ctx->u.sha.key_len) { in ccp_sha_init()
198 memcpy(rctx->buf, ctx->u.sha.ipad, block_size); in ccp_sha_init()
199 rctx->buf_count = block_size; in ccp_sha_init()
207 return ccp_do_sha_update(req, req->nbytes, 0); in ccp_sha_update()
217 return ccp_do_sha_update(req, req->nbytes, 1); in ccp_sha_finup()
239 state.type = rctx->type; in ccp_sha_export()
240 state.msg_bits = rctx->msg_bits; in ccp_sha_export()
241 state.first = rctx->first; in ccp_sha_export()
242 memcpy(state.ctx, rctx->ctx, sizeof(state.ctx)); in ccp_sha_export()
243 state.buf_count = rctx->buf_count; in ccp_sha_export()
244 memcpy(state.buf, rctx->buf, sizeof(state.buf)); in ccp_sha_export()
261 rctx->type = state.type; in ccp_sha_import()
262 rctx->msg_bits = state.msg_bits; in ccp_sha_import()
263 rctx->first = state.first; in ccp_sha_import()
264 memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx)); in ccp_sha_import()
265 rctx->buf_count = state.buf_count; in ccp_sha_import()
266 memcpy(rctx->buf, state.buf, sizeof(rctx->buf)); in ccp_sha_import()
275 struct crypto_shash *shash = ctx->u.sha.hmac_tfm; in ccp_sha_setkey()
281 ctx->u.sha.key_len = 0; in ccp_sha_setkey()
286 memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key)); in ccp_sha_setkey()
291 ctx->u.sha.key); in ccp_sha_setkey()
293 return -EINVAL; in ccp_sha_setkey()
297 memcpy(ctx->u.sha.key, key, key_len); in ccp_sha_setkey()
301 ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ HMAC_IPAD_VALUE; in ccp_sha_setkey()
302 ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ HMAC_OPAD_VALUE; in ccp_sha_setkey()
305 sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size); in ccp_sha_setkey()
306 ctx->u.sha.opad_count = block_size; in ccp_sha_setkey()
308 ctx->u.sha.key_len = key_len; in ccp_sha_setkey()
318 ctx->complete = ccp_sha_complete; in ccp_sha_cra_init()
319 ctx->u.sha.key_len = 0; in ccp_sha_cra_init()
336 hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0); in ccp_hmac_sha_cra_init()
339 alg->child_alg); in ccp_hmac_sha_cra_init()
343 ctx->u.sha.hmac_tfm = hmac_tfm; in ccp_hmac_sha_cra_init()
352 if (ctx->u.sha.hmac_tfm) in ccp_hmac_sha_cra_exit()
353 crypto_free_shash(ctx->u.sha.hmac_tfm); in ccp_hmac_sha_cra_exit()
371 .drv_name = "sha1-ccp",
379 .drv_name = "sha224-ccp",
387 .drv_name = "sha256-ccp",
395 .drv_name = "sha384-ccp",
403 .drv_name = "sha512-ccp",
422 return -ENOMEM; in ccp_register_hmac_alg()
426 INIT_LIST_HEAD(&ccp_alg->entry); in ccp_register_hmac_alg()
428 strscpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME); in ccp_register_hmac_alg()
430 alg = &ccp_alg->alg; in ccp_register_hmac_alg()
431 alg->setkey = ccp_sha_setkey; in ccp_register_hmac_alg()
433 halg = &alg->halg; in ccp_register_hmac_alg()
435 base = &halg->base; in ccp_register_hmac_alg()
436 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name); in ccp_register_hmac_alg()
437 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s", in ccp_register_hmac_alg()
438 def->drv_name); in ccp_register_hmac_alg()
439 base->cra_init = ccp_hmac_sha_cra_init; in ccp_register_hmac_alg()
440 base->cra_exit = ccp_hmac_sha_cra_exit; in ccp_register_hmac_alg()
445 base->cra_name, ret); in ccp_register_hmac_alg()
450 list_add(&ccp_alg->entry, head); in ccp_register_hmac_alg()
466 return -ENOMEM; in ccp_register_sha_alg()
468 INIT_LIST_HEAD(&ccp_alg->entry); in ccp_register_sha_alg()
470 ccp_alg->type = def->type; in ccp_register_sha_alg()
472 alg = &ccp_alg->alg; in ccp_register_sha_alg()
473 alg->init = ccp_sha_init; in ccp_register_sha_alg()
474 alg->update = ccp_sha_update; in ccp_register_sha_alg()
475 alg->final = ccp_sha_final; in ccp_register_sha_alg()
476 alg->finup = ccp_sha_finup; in ccp_register_sha_alg()
477 alg->digest = ccp_sha_digest; in ccp_register_sha_alg()
478 alg->export = ccp_sha_export; in ccp_register_sha_alg()
479 alg->import = ccp_sha_import; in ccp_register_sha_alg()
481 halg = &alg->halg; in ccp_register_sha_alg()
482 halg->digestsize = def->digest_size; in ccp_register_sha_alg()
483 halg->statesize = sizeof(struct ccp_sha_exp_ctx); in ccp_register_sha_alg()
485 base = &halg->base; in ccp_register_sha_alg()
486 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); in ccp_register_sha_alg()
487 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", in ccp_register_sha_alg()
488 def->drv_name); in ccp_register_sha_alg()
489 base->cra_flags = CRYPTO_ALG_ASYNC | in ccp_register_sha_alg()
493 base->cra_blocksize = def->block_size; in ccp_register_sha_alg()
494 base->cra_ctxsize = sizeof(struct ccp_ctx); in ccp_register_sha_alg()
495 base->cra_priority = CCP_CRA_PRIORITY; in ccp_register_sha_alg()
496 base->cra_init = ccp_sha_cra_init; in ccp_register_sha_alg()
497 base->cra_exit = ccp_sha_cra_exit; in ccp_register_sha_alg()
498 base->cra_module = THIS_MODULE; in ccp_register_sha_alg()
503 base->cra_name, ret); in ccp_register_sha_alg()
508 list_add(&ccp_alg->entry, head); in ccp_register_sha_alg()