^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Shared descriptors for ahash algorithms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2017-2019 NXP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "compat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "desc_constr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "caamhash_desc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * cnstr_shdsc_ahash - ahash shared descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * @desc: pointer to buffer used for descriptor construction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @adata: pointer to authentication transform definitions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * A split key is required for SEC Era < 6; the size of the split key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * is specified in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Valid algorithm values - one of OP_ALG_ALGSEL_{MD5, SHA1, SHA224,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * SHA256, SHA384, SHA512}.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @state: algorithm state OP_ALG_AS_{INIT, FINALIZE, INITFINALIZE, UPDATE}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @digestsize: algorithm's digest size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @ctx_len: size of Context Register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @import_ctx: true if previous Context Register needs to be restored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * must be true for ahash update and final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * must be false for for ahash first and digest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @era: SEC Era
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void cnstr_shdsc_ahash(u32 * const desc, struct alginfo *adata, u32 state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int digestsize, int ctx_len, bool import_ctx, int era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u32 op = adata->algtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) init_sh_desc(desc, HDR_SHARE_SERIAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* Append key if it has been set; ahash update excluded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (state != OP_ALG_AS_UPDATE && adata->keylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u32 *skip_key_load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Skip key loading if already shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) skip_key_load = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) JUMP_COND_SHRD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (era < 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) append_key_as_imm(desc, adata->key_virt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) adata->keylen_pad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) adata->keylen, CLASS_2 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) KEY_DEST_MDHA_SPLIT | KEY_ENC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) append_proto_dkp(desc, adata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) set_jump_tgt_here(desc, skip_key_load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) op |= OP_ALG_AAI_HMAC_PRECOMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* If needed, import context from software */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (import_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) append_seq_load(desc, ctx_len, LDST_CLASS_2_CCB |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) LDST_SRCDST_BYTE_CONTEXT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* Class 2 operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) append_operation(desc, op | state | OP_ALG_ENCRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Load from buf and/or src and write to req->result or state->context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * Calculate remaining bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Read remaining bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) FIFOLD_TYPE_MSG | KEY_VLF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Store class2 context bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) LDST_SRCDST_BYTE_CONTEXT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) EXPORT_SYMBOL(cnstr_shdsc_ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * cnstr_shdsc_sk_hash - shared descriptor for symmetric key cipher-based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * hash algorithms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @desc: pointer to buffer used for descriptor construction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * @adata: pointer to authentication transform definitions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @state: algorithm state OP_ALG_AS_{INIT, FINALIZE, INITFINALIZE, UPDATE}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @digestsize: algorithm's digest size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @ctx_len: size of Context Register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) void cnstr_shdsc_sk_hash(u32 * const desc, struct alginfo *adata, u32 state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int digestsize, int ctx_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u32 *skip_key_load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* Skip loading of key, context if already shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) skip_key_load = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (state == OP_ALG_AS_INIT || state == OP_ALG_AS_INITFINAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) append_key_as_imm(desc, adata->key_virt, adata->keylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) adata->keylen, CLASS_1 | KEY_DEST_CLASS_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } else { /* UPDATE, FINALIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (is_xcbc_aes(adata->algtype))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Load K1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) append_key(desc, adata->key_dma, adata->keylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) CLASS_1 | KEY_DEST_CLASS_REG | KEY_ENC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) else /* CMAC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) append_key_as_imm(desc, adata->key_virt, adata->keylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) adata->keylen, CLASS_1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) KEY_DEST_CLASS_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Restore context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) append_seq_load(desc, ctx_len, LDST_CLASS_1_CCB |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) LDST_SRCDST_BYTE_CONTEXT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) set_jump_tgt_here(desc, skip_key_load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* Class 1 operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) append_operation(desc, adata->algtype | state | OP_ALG_ENCRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Load from buf and/or src and write to req->result or state->context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * Calculate remaining bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* Read remaining bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLD_TYPE_LAST1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) FIFOLD_TYPE_MSG | FIFOLDST_VLF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * Save context:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * - xcbc: partial hash, keys K2 and K3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * - cmac: partial hash, constant L = E(K,0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) append_seq_store(desc, digestsize, LDST_CLASS_1_CCB |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) LDST_SRCDST_BYTE_CONTEXT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (is_xcbc_aes(adata->algtype) && state == OP_ALG_AS_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* Save K1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) append_fifo_store(desc, adata->key_dma, adata->keylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) LDST_CLASS_1_CCB | FIFOST_TYPE_KEY_KEK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) EXPORT_SYMBOL(cnstr_shdsc_sk_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) MODULE_LICENSE("Dual BSD/GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) MODULE_DESCRIPTION("FSL CAAM ahash descriptors support");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) MODULE_AUTHOR("NXP Semiconductors");