diff options
author | Horia Geantă <horia.geanta@nxp.com> | 2018-09-12 04:59:35 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2018-09-21 01:24:51 -0400 |
commit | 0efa7579f3de1907acd16a358b0ade214b020d77 (patch) | |
tree | 61d6afcd2eb9665247e7ae935c97657094bd150d | |
parent | 226853ac3ebee425300b9a692c9cd5f9e2e72571 (diff) |
crypto: caam - export ahash shared descriptor generation
caam/qi2 driver will support ahash algorithms,
thus move ahash descriptors generation in a shared location.
Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r-- | drivers/crypto/caam/Kconfig | 3 | ||||
-rw-r--r-- | drivers/crypto/caam/Makefile | 1 | ||||
-rw-r--r-- | drivers/crypto/caam/caamhash.c | 79 | ||||
-rw-r--r-- | drivers/crypto/caam/caamhash_desc.c | 80 | ||||
-rw-r--r-- | drivers/crypto/caam/caamhash_desc.h | 21 |
5 files changed, 114 insertions, 70 deletions
diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig index 8d162cb34591..03c1db0239ae 100644 --- a/drivers/crypto/caam/Kconfig +++ b/drivers/crypto/caam/Kconfig | |||
@@ -172,3 +172,6 @@ config CRYPTO_DEV_FSL_CAAM_CRYPTO_API_DESC | |||
172 | def_tristate (CRYPTO_DEV_FSL_CAAM_CRYPTO_API || \ | 172 | def_tristate (CRYPTO_DEV_FSL_CAAM_CRYPTO_API || \ |
173 | CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI || \ | 173 | CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI || \ |
174 | CRYPTO_DEV_FSL_DPAA2_CAAM) | 174 | CRYPTO_DEV_FSL_DPAA2_CAAM) |
175 | |||
176 | config CRYPTO_DEV_FSL_CAAM_AHASH_API_DESC | ||
177 | def_tristate (CRYPTO_DEV_FSL_CAAM_AHASH_API) | ||
diff --git a/drivers/crypto/caam/Makefile b/drivers/crypto/caam/Makefile index fd507bab1828..7bbfd06a11ff 100644 --- a/drivers/crypto/caam/Makefile +++ b/drivers/crypto/caam/Makefile | |||
@@ -15,6 +15,7 @@ obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API) += caamalg.o | |||
15 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += caamalg_qi.o | 15 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += caamalg_qi.o |
16 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_DESC) += caamalg_desc.o | 16 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_DESC) += caamalg_desc.o |
17 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o | 17 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o |
18 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API_DESC) += caamhash_desc.o | ||
18 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o | 19 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o |
19 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caam_pkc.o | 20 | obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caam_pkc.o |
20 | 21 | ||
diff --git a/drivers/crypto/caam/caamhash.c b/drivers/crypto/caam/caamhash.c index 43975ab5f09c..4c30bb5516d6 100644 --- a/drivers/crypto/caam/caamhash.c +++ b/drivers/crypto/caam/caamhash.c | |||
@@ -62,6 +62,7 @@ | |||
62 | #include "error.h" | 62 | #include "error.h" |
63 | #include "sg_sw_sec4.h" | 63 | #include "sg_sw_sec4.h" |
64 | #include "key_gen.h" | 64 | #include "key_gen.h" |
65 | #include "caamhash_desc.h" | ||
65 | 66 | ||
66 | #define CAAM_CRA_PRIORITY 3000 | 67 | #define CAAM_CRA_PRIORITY 3000 |
67 | 68 | ||
@@ -71,14 +72,6 @@ | |||
71 | #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE | 72 | #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE |
72 | #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE | 73 | #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE |
73 | 74 | ||
74 | /* length of descriptors text */ | ||
75 | #define DESC_AHASH_BASE (3 * CAAM_CMD_SZ) | ||
76 | #define DESC_AHASH_UPDATE_LEN (6 * CAAM_CMD_SZ) | ||
77 | #define DESC_AHASH_UPDATE_FIRST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ) | ||
78 | #define DESC_AHASH_FINAL_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ) | ||
79 | #define DESC_AHASH_FINUP_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ) | ||
80 | #define DESC_AHASH_DIGEST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ) | ||
81 | |||
82 | #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \ | 75 | #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \ |
83 | CAAM_MAX_HASH_KEY_SIZE) | 76 | CAAM_MAX_HASH_KEY_SIZE) |
84 | #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ) | 77 | #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ) |
@@ -235,60 +228,6 @@ static inline int ctx_map_to_sec4_sg(struct device *jrdev, | |||
235 | return 0; | 228 | return 0; |
236 | } | 229 | } |
237 | 230 | ||
238 | /* | ||
239 | * For ahash update, final and finup (import_ctx = true) | ||
240 | * import context, read and write to seqout | ||
241 | * For ahash firsts and digest (import_ctx = false) | ||
242 | * read and write to seqout | ||
243 | */ | ||
244 | static inline void ahash_gen_sh_desc(u32 *desc, u32 state, int digestsize, | ||
245 | struct caam_hash_ctx *ctx, bool import_ctx, | ||
246 | int era) | ||
247 | { | ||
248 | u32 op = ctx->adata.algtype; | ||
249 | u32 *skip_key_load; | ||
250 | |||
251 | init_sh_desc(desc, HDR_SHARE_SERIAL); | ||
252 | |||
253 | /* Append key if it has been set; ahash update excluded */ | ||
254 | if ((state != OP_ALG_AS_UPDATE) && (ctx->adata.keylen)) { | ||
255 | /* Skip key loading if already shared */ | ||
256 | skip_key_load = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | | ||
257 | JUMP_COND_SHRD); | ||
258 | |||
259 | if (era < 6) | ||
260 | append_key_as_imm(desc, ctx->key, ctx->adata.keylen_pad, | ||
261 | ctx->adata.keylen, CLASS_2 | | ||
262 | KEY_DEST_MDHA_SPLIT | KEY_ENC); | ||
263 | else | ||
264 | append_proto_dkp(desc, &ctx->adata); | ||
265 | |||
266 | set_jump_tgt_here(desc, skip_key_load); | ||
267 | |||
268 | op |= OP_ALG_AAI_HMAC_PRECOMP; | ||
269 | } | ||
270 | |||
271 | /* If needed, import context from software */ | ||
272 | if (import_ctx) | ||
273 | append_seq_load(desc, ctx->ctx_len, LDST_CLASS_2_CCB | | ||
274 | LDST_SRCDST_BYTE_CONTEXT); | ||
275 | |||
276 | /* Class 2 operation */ | ||
277 | append_operation(desc, op | state | OP_ALG_ENCRYPT); | ||
278 | |||
279 | /* | ||
280 | * Load from buf and/or src and write to req->result or state->context | ||
281 | * Calculate remaining bytes to read | ||
282 | */ | ||
283 | append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ); | ||
284 | /* Read remaining bytes */ | ||
285 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 | | ||
286 | FIFOLD_TYPE_MSG | KEY_VLF); | ||
287 | /* Store class2 context bytes */ | ||
288 | append_seq_store(desc, digestsize, LDST_CLASS_2_CCB | | ||
289 | LDST_SRCDST_BYTE_CONTEXT); | ||
290 | } | ||
291 | |||
292 | static int ahash_set_sh_desc(struct crypto_ahash *ahash) | 231 | static int ahash_set_sh_desc(struct crypto_ahash *ahash) |
293 | { | 232 | { |
294 | struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); | 233 | struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); |
@@ -301,8 +240,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) | |||
301 | 240 | ||
302 | /* ahash_update shared descriptor */ | 241 | /* ahash_update shared descriptor */ |
303 | desc = ctx->sh_desc_update; | 242 | desc = ctx->sh_desc_update; |
304 | ahash_gen_sh_desc(desc, OP_ALG_AS_UPDATE, ctx->ctx_len, ctx, true, | 243 | cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len, |
305 | ctrlpriv->era); | 244 | ctx->ctx_len, true, ctrlpriv->era); |
306 | dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma, | 245 | dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma, |
307 | desc_bytes(desc), ctx->dir); | 246 | desc_bytes(desc), ctx->dir); |
308 | #ifdef DEBUG | 247 | #ifdef DEBUG |
@@ -313,8 +252,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) | |||
313 | 252 | ||
314 | /* ahash_update_first shared descriptor */ | 253 | /* ahash_update_first shared descriptor */ |
315 | desc = ctx->sh_desc_update_first; | 254 | desc = ctx->sh_desc_update_first; |
316 | ahash_gen_sh_desc(desc, OP_ALG_AS_INIT, ctx->ctx_len, ctx, false, | 255 | cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len, |
317 | ctrlpriv->era); | 256 | ctx->ctx_len, false, ctrlpriv->era); |
318 | dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma, | 257 | dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma, |
319 | desc_bytes(desc), ctx->dir); | 258 | desc_bytes(desc), ctx->dir); |
320 | #ifdef DEBUG | 259 | #ifdef DEBUG |
@@ -325,8 +264,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) | |||
325 | 264 | ||
326 | /* ahash_final shared descriptor */ | 265 | /* ahash_final shared descriptor */ |
327 | desc = ctx->sh_desc_fin; | 266 | desc = ctx->sh_desc_fin; |
328 | ahash_gen_sh_desc(desc, OP_ALG_AS_FINALIZE, digestsize, ctx, true, | 267 | cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize, |
329 | ctrlpriv->era); | 268 | ctx->ctx_len, true, ctrlpriv->era); |
330 | dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma, | 269 | dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma, |
331 | desc_bytes(desc), ctx->dir); | 270 | desc_bytes(desc), ctx->dir); |
332 | #ifdef DEBUG | 271 | #ifdef DEBUG |
@@ -337,8 +276,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) | |||
337 | 276 | ||
338 | /* ahash_digest shared descriptor */ | 277 | /* ahash_digest shared descriptor */ |
339 | desc = ctx->sh_desc_digest; | 278 | desc = ctx->sh_desc_digest; |
340 | ahash_gen_sh_desc(desc, OP_ALG_AS_INITFINAL, digestsize, ctx, false, | 279 | cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize, |
341 | ctrlpriv->era); | 280 | ctx->ctx_len, false, ctrlpriv->era); |
342 | dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma, | 281 | dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma, |
343 | desc_bytes(desc), ctx->dir); | 282 | desc_bytes(desc), ctx->dir); |
344 | #ifdef DEBUG | 283 | #ifdef DEBUG |
diff --git a/drivers/crypto/caam/caamhash_desc.c b/drivers/crypto/caam/caamhash_desc.c new file mode 100644 index 000000000000..a12f7959a2c3 --- /dev/null +++ b/drivers/crypto/caam/caamhash_desc.c | |||
@@ -0,0 +1,80 @@ | |||
1 | // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) | ||
2 | /* | ||
3 | * Shared descriptors for ahash algorithms | ||
4 | * | ||
5 | * Copyright 2017 NXP | ||
6 | */ | ||
7 | |||
8 | #include "compat.h" | ||
9 | #include "desc_constr.h" | ||
10 | #include "caamhash_desc.h" | ||
11 | |||
12 | /** | ||
13 | * cnstr_shdsc_ahash - ahash shared descriptor | ||
14 | * @desc: pointer to buffer used for descriptor construction | ||
15 | * @adata: pointer to authentication transform definitions. | ||
16 | * A split key is required for SEC Era < 6; the size of the split key | ||
17 | * is specified in this case. | ||
18 | * Valid algorithm values - one of OP_ALG_ALGSEL_{MD5, SHA1, SHA224, | ||
19 | * SHA256, SHA384, SHA512}. | ||
20 | * @state: algorithm state OP_ALG_AS_{INIT, FINALIZE, INITFINALIZE, UPDATE} | ||
21 | * @digestsize: algorithm's digest size | ||
22 | * @ctx_len: size of Context Register | ||
23 | * @import_ctx: true if previous Context Register needs to be restored | ||
24 | * must be true for ahash update and final | ||
25 | * must be false for for ahash first and digest | ||
26 | * @era: SEC Era | ||
27 | */ | ||
28 | void cnstr_shdsc_ahash(u32 * const desc, struct alginfo *adata, u32 state, | ||
29 | int digestsize, int ctx_len, bool import_ctx, int era) | ||
30 | { | ||
31 | u32 op = adata->algtype; | ||
32 | |||
33 | init_sh_desc(desc, HDR_SHARE_SERIAL); | ||
34 | |||
35 | /* Append key if it has been set; ahash update excluded */ | ||
36 | if (state != OP_ALG_AS_UPDATE && adata->keylen) { | ||
37 | u32 *skip_key_load; | ||
38 | |||
39 | /* Skip key loading if already shared */ | ||
40 | skip_key_load = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | | ||
41 | JUMP_COND_SHRD); | ||
42 | |||
43 | if (era < 6) | ||
44 | append_key_as_imm(desc, adata->key_virt, | ||
45 | adata->keylen_pad, | ||
46 | adata->keylen, CLASS_2 | | ||
47 | KEY_DEST_MDHA_SPLIT | KEY_ENC); | ||
48 | else | ||
49 | append_proto_dkp(desc, adata); | ||
50 | |||
51 | set_jump_tgt_here(desc, skip_key_load); | ||
52 | |||
53 | op |= OP_ALG_AAI_HMAC_PRECOMP; | ||
54 | } | ||
55 | |||
56 | /* If needed, import context from software */ | ||
57 | if (import_ctx) | ||
58 | append_seq_load(desc, ctx_len, LDST_CLASS_2_CCB | | ||
59 | LDST_SRCDST_BYTE_CONTEXT); | ||
60 | |||
61 | /* Class 2 operation */ | ||
62 | append_operation(desc, op | state | OP_ALG_ENCRYPT); | ||
63 | |||
64 | /* | ||
65 | * Load from buf and/or src and write to req->result or state->context | ||
66 | * Calculate remaining bytes to read | ||
67 | */ | ||
68 | append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ); | ||
69 | /* Read remaining bytes */ | ||
70 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 | | ||
71 | FIFOLD_TYPE_MSG | KEY_VLF); | ||
72 | /* Store class2 context bytes */ | ||
73 | append_seq_store(desc, digestsize, LDST_CLASS_2_CCB | | ||
74 | LDST_SRCDST_BYTE_CONTEXT); | ||
75 | } | ||
76 | EXPORT_SYMBOL(cnstr_shdsc_ahash); | ||
77 | |||
78 | MODULE_LICENSE("Dual BSD/GPL"); | ||
79 | MODULE_DESCRIPTION("FSL CAAM ahash descriptors support"); | ||
80 | MODULE_AUTHOR("NXP Semiconductors"); | ||
diff --git a/drivers/crypto/caam/caamhash_desc.h b/drivers/crypto/caam/caamhash_desc.h new file mode 100644 index 000000000000..631fc1ac312c --- /dev/null +++ b/drivers/crypto/caam/caamhash_desc.h | |||
@@ -0,0 +1,21 @@ | |||
1 | /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */ | ||
2 | /* | ||
3 | * Shared descriptors for ahash algorithms | ||
4 | * | ||
5 | * Copyright 2017 NXP | ||
6 | */ | ||
7 | |||
8 | #ifndef _CAAMHASH_DESC_H_ | ||
9 | #define _CAAMHASH_DESC_H_ | ||
10 | |||
11 | /* length of descriptors text */ | ||
12 | #define DESC_AHASH_BASE (3 * CAAM_CMD_SZ) | ||
13 | #define DESC_AHASH_UPDATE_LEN (6 * CAAM_CMD_SZ) | ||
14 | #define DESC_AHASH_UPDATE_FIRST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ) | ||
15 | #define DESC_AHASH_FINAL_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ) | ||
16 | #define DESC_AHASH_DIGEST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ) | ||
17 | |||
18 | void cnstr_shdsc_ahash(u32 * const desc, struct alginfo *adata, u32 state, | ||
19 | int digestsize, int ctx_len, bool import_ctx, int era); | ||
20 | |||
21 | #endif /* _CAAMHASH_DESC_H_ */ | ||