Lines Matching +full:sg +full:- +full:micro

1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
21 #include "ccp-crypto.h"
34 if (rctx->hash_rem) { in ccp_aes_cmac_complete()
36 unsigned int offset = rctx->nbytes - rctx->hash_rem; in ccp_aes_cmac_complete()
38 scatterwalk_map_and_copy(rctx->buf, rctx->src, in ccp_aes_cmac_complete()
39 offset, rctx->hash_rem, 0); in ccp_aes_cmac_complete()
40 rctx->buf_count = rctx->hash_rem; in ccp_aes_cmac_complete()
42 rctx->buf_count = 0; in ccp_aes_cmac_complete()
46 if (req->result && rctx->final) in ccp_aes_cmac_complete()
47 memcpy(req->result, rctx->iv, digest_size); in ccp_aes_cmac_complete()
50 sg_free_table(&rctx->data_sg); in ccp_aes_cmac_complete()
61 struct scatterlist *sg, *cmac_key_sg = NULL; in ccp_do_cmac_update() local
69 if (!ctx->u.aes.key_len) in ccp_do_cmac_update()
70 return -EINVAL; in ccp_do_cmac_update()
73 rctx->null_msg = 0; in ccp_do_cmac_update()
75 len = (u64)rctx->buf_count + (u64)nbytes; in ccp_do_cmac_update()
78 scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src, in ccp_do_cmac_update()
80 rctx->buf_count += nbytes; in ccp_do_cmac_update()
85 rctx->src = req->src; in ccp_do_cmac_update()
86 rctx->nbytes = nbytes; in ccp_do_cmac_update()
88 rctx->final = final; in ccp_do_cmac_update()
89 rctx->hash_rem = final ? 0 : len & (block_size - 1); in ccp_do_cmac_update()
90 rctx->hash_cnt = len - rctx->hash_rem; in ccp_do_cmac_update()
91 if (!final && !rctx->hash_rem) { in ccp_do_cmac_update()
93 rctx->hash_cnt -= block_size; in ccp_do_cmac_update()
94 rctx->hash_rem = block_size; in ccp_do_cmac_update()
97 if (final && (rctx->null_msg || (len & (block_size - 1)))) in ccp_do_cmac_update()
102 sg_init_one(&rctx->iv_sg, rctx->iv, sizeof(rctx->iv)); in ccp_do_cmac_update()
104 /* Build the data scatterlist table - allocate enough entries for all in ccp_do_cmac_update()
107 sg_count = (nbytes) ? sg_nents(req->src) + 2 : 2; in ccp_do_cmac_update()
108 gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? in ccp_do_cmac_update()
110 ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp); in ccp_do_cmac_update()
114 sg = NULL; in ccp_do_cmac_update()
115 if (rctx->buf_count) { in ccp_do_cmac_update()
116 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count); in ccp_do_cmac_update()
117 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg); in ccp_do_cmac_update()
118 if (!sg) { in ccp_do_cmac_update()
119 ret = -EINVAL; in ccp_do_cmac_update()
125 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src); in ccp_do_cmac_update()
126 if (!sg) { in ccp_do_cmac_update()
127 ret = -EINVAL; in ccp_do_cmac_update()
133 int pad_length = block_size - (len & (block_size - 1)); in ccp_do_cmac_update()
135 rctx->hash_cnt += pad_length; in ccp_do_cmac_update()
137 memset(rctx->pad, 0, sizeof(rctx->pad)); in ccp_do_cmac_update()
138 rctx->pad[0] = 0x80; in ccp_do_cmac_update()
139 sg_init_one(&rctx->pad_sg, rctx->pad, pad_length); in ccp_do_cmac_update()
140 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->pad_sg); in ccp_do_cmac_update()
141 if (!sg) { in ccp_do_cmac_update()
142 ret = -EINVAL; in ccp_do_cmac_update()
146 if (sg) { in ccp_do_cmac_update()
147 sg_mark_end(sg); in ccp_do_cmac_update()
148 sg = rctx->data_sg.sgl; in ccp_do_cmac_update()
153 cmac_key_sg = (need_pad) ? &ctx->u.aes.k2_sg in ccp_do_cmac_update()
154 : &ctx->u.aes.k1_sg; in ccp_do_cmac_update()
156 memset(&rctx->cmd, 0, sizeof(rctx->cmd)); in ccp_do_cmac_update()
157 INIT_LIST_HEAD(&rctx->cmd.entry); in ccp_do_cmac_update()
158 rctx->cmd.engine = CCP_ENGINE_AES; in ccp_do_cmac_update()
159 rctx->cmd.u.aes.type = ctx->u.aes.type; in ccp_do_cmac_update()
160 rctx->cmd.u.aes.mode = ctx->u.aes.mode; in ccp_do_cmac_update()
161 rctx->cmd.u.aes.action = CCP_AES_ACTION_ENCRYPT; in ccp_do_cmac_update()
162 rctx->cmd.u.aes.key = &ctx->u.aes.key_sg; in ccp_do_cmac_update()
163 rctx->cmd.u.aes.key_len = ctx->u.aes.key_len; in ccp_do_cmac_update()
164 rctx->cmd.u.aes.iv = &rctx->iv_sg; in ccp_do_cmac_update()
165 rctx->cmd.u.aes.iv_len = AES_BLOCK_SIZE; in ccp_do_cmac_update()
166 rctx->cmd.u.aes.src = sg; in ccp_do_cmac_update()
167 rctx->cmd.u.aes.src_len = rctx->hash_cnt; in ccp_do_cmac_update()
168 rctx->cmd.u.aes.dst = NULL; in ccp_do_cmac_update()
169 rctx->cmd.u.aes.cmac_key = cmac_key_sg; in ccp_do_cmac_update()
170 rctx->cmd.u.aes.cmac_key_len = ctx->u.aes.kn_len; in ccp_do_cmac_update()
171 rctx->cmd.u.aes.cmac_final = final; in ccp_do_cmac_update()
173 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); in ccp_do_cmac_update()
178 sg_free_table(&rctx->data_sg); in ccp_do_cmac_update()
189 rctx->null_msg = 1; in ccp_aes_cmac_init()
196 return ccp_do_cmac_update(req, req->nbytes, 0); in ccp_aes_cmac_update()
206 return ccp_do_cmac_update(req, req->nbytes, 1); in ccp_aes_cmac_finup()
228 state.null_msg = rctx->null_msg; in ccp_aes_cmac_export()
229 memcpy(state.iv, rctx->iv, sizeof(state.iv)); in ccp_aes_cmac_export()
230 state.buf_count = rctx->buf_count; in ccp_aes_cmac_export()
231 memcpy(state.buf, rctx->buf, sizeof(state.buf)); in ccp_aes_cmac_export()
248 rctx->null_msg = state.null_msg; in ccp_aes_cmac_import()
249 memcpy(rctx->iv, state.iv, sizeof(rctx->iv)); in ccp_aes_cmac_import()
250 rctx->buf_count = state.buf_count; in ccp_aes_cmac_import()
251 memcpy(rctx->buf, state.buf, sizeof(rctx->buf)); in ccp_aes_cmac_import()
270 ctx->u.aes.type = CCP_AES_TYPE_128; in ccp_aes_cmac_setkey()
273 ctx->u.aes.type = CCP_AES_TYPE_192; in ccp_aes_cmac_setkey()
276 ctx->u.aes.type = CCP_AES_TYPE_256; in ccp_aes_cmac_setkey()
279 return -EINVAL; in ccp_aes_cmac_setkey()
281 ctx->u.aes.mode = alg->mode; in ccp_aes_cmac_setkey()
284 ctx->u.aes.key_len = 0; in ccp_aes_cmac_setkey()
291 /* Encrypt a block of zeroes - use key area in context */ in ccp_aes_cmac_setkey()
292 memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key)); in ccp_aes_cmac_setkey()
293 aes_encrypt(&aes, ctx->u.aes.key, ctx->u.aes.key); in ccp_aes_cmac_setkey()
297 k0_hi = be64_to_cpu(*((__be64 *)ctx->u.aes.key)); in ccp_aes_cmac_setkey()
298 k0_lo = be64_to_cpu(*((__be64 *)ctx->u.aes.key + 1)); in ccp_aes_cmac_setkey()
302 if (ctx->u.aes.key[0] & 0x80) { in ccp_aes_cmac_setkey()
306 gk = (__be64 *)ctx->u.aes.k1; in ccp_aes_cmac_setkey()
313 if (ctx->u.aes.k1[0] & 0x80) { in ccp_aes_cmac_setkey()
317 gk = (__be64 *)ctx->u.aes.k2; in ccp_aes_cmac_setkey()
322 ctx->u.aes.kn_len = sizeof(ctx->u.aes.k1); in ccp_aes_cmac_setkey()
323 sg_init_one(&ctx->u.aes.k1_sg, ctx->u.aes.k1, sizeof(ctx->u.aes.k1)); in ccp_aes_cmac_setkey()
324 sg_init_one(&ctx->u.aes.k2_sg, ctx->u.aes.k2, sizeof(ctx->u.aes.k2)); in ccp_aes_cmac_setkey()
327 memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key)); in ccp_aes_cmac_setkey()
328 memcpy(ctx->u.aes.key, key, key_len); in ccp_aes_cmac_setkey()
329 ctx->u.aes.key_len = key_len; in ccp_aes_cmac_setkey()
330 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len); in ccp_aes_cmac_setkey()
340 ctx->complete = ccp_aes_cmac_complete; in ccp_aes_cmac_cra_init()
341 ctx->u.aes.key_len = 0; in ccp_aes_cmac_cra_init()
358 return -ENOMEM; in ccp_register_aes_cmac_algs()
360 INIT_LIST_HEAD(&ccp_alg->entry); in ccp_register_aes_cmac_algs()
361 ccp_alg->mode = CCP_AES_MODE_CMAC; in ccp_register_aes_cmac_algs()
363 alg = &ccp_alg->alg; in ccp_register_aes_cmac_algs()
364 alg->init = ccp_aes_cmac_init; in ccp_register_aes_cmac_algs()
365 alg->update = ccp_aes_cmac_update; in ccp_register_aes_cmac_algs()
366 alg->final = ccp_aes_cmac_final; in ccp_register_aes_cmac_algs()
367 alg->finup = ccp_aes_cmac_finup; in ccp_register_aes_cmac_algs()
368 alg->digest = ccp_aes_cmac_digest; in ccp_register_aes_cmac_algs()
369 alg->export = ccp_aes_cmac_export; in ccp_register_aes_cmac_algs()
370 alg->import = ccp_aes_cmac_import; in ccp_register_aes_cmac_algs()
371 alg->setkey = ccp_aes_cmac_setkey; in ccp_register_aes_cmac_algs()
373 halg = &alg->halg; in ccp_register_aes_cmac_algs()
374 halg->digestsize = AES_BLOCK_SIZE; in ccp_register_aes_cmac_algs()
375 halg->statesize = sizeof(struct ccp_aes_cmac_exp_ctx); in ccp_register_aes_cmac_algs()
377 base = &halg->base; in ccp_register_aes_cmac_algs()
378 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "cmac(aes)"); in ccp_register_aes_cmac_algs()
379 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "cmac-aes-ccp"); in ccp_register_aes_cmac_algs()
380 base->cra_flags = CRYPTO_ALG_ASYNC | in ccp_register_aes_cmac_algs()
384 base->cra_blocksize = AES_BLOCK_SIZE; in ccp_register_aes_cmac_algs()
385 base->cra_ctxsize = sizeof(struct ccp_ctx); in ccp_register_aes_cmac_algs()
386 base->cra_priority = CCP_CRA_PRIORITY; in ccp_register_aes_cmac_algs()
387 base->cra_init = ccp_aes_cmac_cra_init; in ccp_register_aes_cmac_algs()
388 base->cra_module = THIS_MODULE; in ccp_register_aes_cmac_algs()
393 base->cra_name, ret); in ccp_register_aes_cmac_algs()
398 list_add(&ccp_alg->entry, head); in ccp_register_aes_cmac_algs()