xref: /linux/drivers/crypto/caam/caamhash_desc.c (revision 0efa7579f3de1907acd16a358b0ade214b020d77)
1*0efa7579SHoria Geantă // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2*0efa7579SHoria Geantă /*
3*0efa7579SHoria Geantă  * Shared descriptors for ahash algorithms
4*0efa7579SHoria Geantă  *
5*0efa7579SHoria Geantă  * Copyright 2017 NXP
6*0efa7579SHoria Geantă  */
7*0efa7579SHoria Geantă 
8*0efa7579SHoria Geantă #include "compat.h"
9*0efa7579SHoria Geantă #include "desc_constr.h"
10*0efa7579SHoria Geantă #include "caamhash_desc.h"
11*0efa7579SHoria Geantă 
12*0efa7579SHoria Geantă /**
13*0efa7579SHoria Geantă  * cnstr_shdsc_ahash - ahash shared descriptor
14*0efa7579SHoria Geantă  * @desc: pointer to buffer used for descriptor construction
15*0efa7579SHoria Geantă  * @adata: pointer to authentication transform definitions.
16*0efa7579SHoria Geantă  *         A split key is required for SEC Era < 6; the size of the split key
17*0efa7579SHoria Geantă  *         is specified in this case.
18*0efa7579SHoria Geantă  *         Valid algorithm values - one of OP_ALG_ALGSEL_{MD5, SHA1, SHA224,
19*0efa7579SHoria Geantă  *         SHA256, SHA384, SHA512}.
20*0efa7579SHoria Geantă  * @state: algorithm state OP_ALG_AS_{INIT, FINALIZE, INITFINALIZE, UPDATE}
21*0efa7579SHoria Geantă  * @digestsize: algorithm's digest size
22*0efa7579SHoria Geantă  * @ctx_len: size of Context Register
23*0efa7579SHoria Geantă  * @import_ctx: true if previous Context Register needs to be restored
24*0efa7579SHoria Geantă  *              must be true for ahash update and final
25*0efa7579SHoria Geantă  *              must be false for for ahash first and digest
26*0efa7579SHoria Geantă  * @era: SEC Era
27*0efa7579SHoria Geantă  */
28*0efa7579SHoria Geantă void cnstr_shdsc_ahash(u32 * const desc, struct alginfo *adata, u32 state,
29*0efa7579SHoria Geantă 		       int digestsize, int ctx_len, bool import_ctx, int era)
30*0efa7579SHoria Geantă {
31*0efa7579SHoria Geantă 	u32 op = adata->algtype;
32*0efa7579SHoria Geantă 
33*0efa7579SHoria Geantă 	init_sh_desc(desc, HDR_SHARE_SERIAL);
34*0efa7579SHoria Geantă 
35*0efa7579SHoria Geantă 	/* Append key if it has been set; ahash update excluded */
36*0efa7579SHoria Geantă 	if (state != OP_ALG_AS_UPDATE && adata->keylen) {
37*0efa7579SHoria Geantă 		u32 *skip_key_load;
38*0efa7579SHoria Geantă 
39*0efa7579SHoria Geantă 		/* Skip key loading if already shared */
40*0efa7579SHoria Geantă 		skip_key_load = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
41*0efa7579SHoria Geantă 					    JUMP_COND_SHRD);
42*0efa7579SHoria Geantă 
43*0efa7579SHoria Geantă 		if (era < 6)
44*0efa7579SHoria Geantă 			append_key_as_imm(desc, adata->key_virt,
45*0efa7579SHoria Geantă 					  adata->keylen_pad,
46*0efa7579SHoria Geantă 					  adata->keylen, CLASS_2 |
47*0efa7579SHoria Geantă 					  KEY_DEST_MDHA_SPLIT | KEY_ENC);
48*0efa7579SHoria Geantă 		else
49*0efa7579SHoria Geantă 			append_proto_dkp(desc, adata);
50*0efa7579SHoria Geantă 
51*0efa7579SHoria Geantă 		set_jump_tgt_here(desc, skip_key_load);
52*0efa7579SHoria Geantă 
53*0efa7579SHoria Geantă 		op |= OP_ALG_AAI_HMAC_PRECOMP;
54*0efa7579SHoria Geantă 	}
55*0efa7579SHoria Geantă 
56*0efa7579SHoria Geantă 	/* If needed, import context from software */
57*0efa7579SHoria Geantă 	if (import_ctx)
58*0efa7579SHoria Geantă 		append_seq_load(desc, ctx_len, LDST_CLASS_2_CCB |
59*0efa7579SHoria Geantă 				LDST_SRCDST_BYTE_CONTEXT);
60*0efa7579SHoria Geantă 
61*0efa7579SHoria Geantă 	/* Class 2 operation */
62*0efa7579SHoria Geantă 	append_operation(desc, op | state | OP_ALG_ENCRYPT);
63*0efa7579SHoria Geantă 
64*0efa7579SHoria Geantă 	/*
65*0efa7579SHoria Geantă 	 * Load from buf and/or src and write to req->result or state->context
66*0efa7579SHoria Geantă 	 * Calculate remaining bytes to read
67*0efa7579SHoria Geantă 	 */
68*0efa7579SHoria Geantă 	append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
69*0efa7579SHoria Geantă 	/* Read remaining bytes */
70*0efa7579SHoria Geantă 	append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 |
71*0efa7579SHoria Geantă 			     FIFOLD_TYPE_MSG | KEY_VLF);
72*0efa7579SHoria Geantă 	/* Store class2 context bytes */
73*0efa7579SHoria Geantă 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
74*0efa7579SHoria Geantă 			 LDST_SRCDST_BYTE_CONTEXT);
75*0efa7579SHoria Geantă }
76*0efa7579SHoria Geantă EXPORT_SYMBOL(cnstr_shdsc_ahash);
77*0efa7579SHoria Geantă 
78*0efa7579SHoria Geantă MODULE_LICENSE("Dual BSD/GPL");
79*0efa7579SHoria Geantă MODULE_DESCRIPTION("FSL CAAM ahash descriptors support");
80*0efa7579SHoria Geantă MODULE_AUTHOR("NXP Semiconductors");
81