Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
0002 /*
0003  * Shared descriptors for ahash algorithms
0004  *
0005  * Copyright 2017-2019 NXP
0006  */
0007 
0008 #include "compat.h"
0009 #include "desc_constr.h"
0010 #include "caamhash_desc.h"
0011 
0012 /**
0013  * cnstr_shdsc_ahash - ahash shared descriptor
0014  * @desc: pointer to buffer used for descriptor construction
0015  * @adata: pointer to authentication transform definitions.
0016  *         A split key is required for SEC Era < 6; the size of the split key
0017  *         is specified in this case.
0018  *         Valid algorithm values - one of OP_ALG_ALGSEL_{MD5, SHA1, SHA224,
0019  *         SHA256, SHA384, SHA512}.
0020  * @state: algorithm state OP_ALG_AS_{INIT, FINALIZE, INITFINALIZE, UPDATE}
0021  * @digestsize: algorithm's digest size
0022  * @ctx_len: size of Context Register
0023  * @import_ctx: true if previous Context Register needs to be restored
0024  *              must be true for ahash update and final
0025  *              must be false for ahash first and digest
0026  * @era: SEC Era
0027  */
0028 void cnstr_shdsc_ahash(u32 * const desc, struct alginfo *adata, u32 state,
0029                int digestsize, int ctx_len, bool import_ctx, int era)
0030 {
0031     u32 op = adata->algtype;
0032 
0033     init_sh_desc(desc, HDR_SHARE_SERIAL);
0034 
0035     /* Append key if it has been set; ahash update excluded */
0036     if (state != OP_ALG_AS_UPDATE && adata->keylen) {
0037         u32 *skip_key_load;
0038 
0039         /* Skip key loading if already shared */
0040         skip_key_load = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
0041                         JUMP_COND_SHRD);
0042 
0043         if (era < 6)
0044             append_key_as_imm(desc, adata->key_virt,
0045                       adata->keylen_pad,
0046                       adata->keylen, CLASS_2 |
0047                       KEY_DEST_MDHA_SPLIT | KEY_ENC);
0048         else
0049             append_proto_dkp(desc, adata);
0050 
0051         set_jump_tgt_here(desc, skip_key_load);
0052 
0053         op |= OP_ALG_AAI_HMAC_PRECOMP;
0054     }
0055 
0056     /* If needed, import context from software */
0057     if (import_ctx)
0058         append_seq_load(desc, ctx_len, LDST_CLASS_2_CCB |
0059                 LDST_SRCDST_BYTE_CONTEXT);
0060 
0061     /* Class 2 operation */
0062     append_operation(desc, op | state | OP_ALG_ENCRYPT);
0063 
0064     /*
0065      * Load from buf and/or src and write to req->result or state->context
0066      * Calculate remaining bytes to read
0067      */
0068     append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
0069     /* Read remaining bytes */
0070     append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 |
0071                  FIFOLD_TYPE_MSG | KEY_VLF);
0072     /* Store class2 context bytes */
0073     append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
0074              LDST_SRCDST_BYTE_CONTEXT);
0075 }
0076 EXPORT_SYMBOL(cnstr_shdsc_ahash);
0077 
0078 /**
0079  * cnstr_shdsc_sk_hash - shared descriptor for symmetric key cipher-based
0080  *                       hash algorithms
0081  * @desc: pointer to buffer used for descriptor construction
0082  * @adata: pointer to authentication transform definitions.
0083  * @state: algorithm state OP_ALG_AS_{INIT, FINALIZE, INITFINALIZE, UPDATE}
0084  * @digestsize: algorithm's digest size
0085  * @ctx_len: size of Context Register
0086  */
0087 void cnstr_shdsc_sk_hash(u32 * const desc, struct alginfo *adata, u32 state,
0088              int digestsize, int ctx_len)
0089 {
0090     u32 *skip_key_load;
0091 
0092     init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
0093 
0094     /* Skip loading of key, context if already shared */
0095     skip_key_load = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
0096 
0097     if (state == OP_ALG_AS_INIT || state == OP_ALG_AS_INITFINAL) {
0098         append_key_as_imm(desc, adata->key_virt, adata->keylen,
0099                   adata->keylen, CLASS_1 | KEY_DEST_CLASS_REG);
0100     } else { /* UPDATE, FINALIZE */
0101         if (is_xcbc_aes(adata->algtype))
0102             /* Load K1 */
0103             append_key(desc, adata->key_dma, adata->keylen,
0104                    CLASS_1 | KEY_DEST_CLASS_REG | KEY_ENC);
0105         else /* CMAC */
0106             append_key_as_imm(desc, adata->key_virt, adata->keylen,
0107                       adata->keylen, CLASS_1 |
0108                       KEY_DEST_CLASS_REG);
0109         /* Restore context */
0110         append_seq_load(desc, ctx_len, LDST_CLASS_1_CCB |
0111                 LDST_SRCDST_BYTE_CONTEXT);
0112     }
0113 
0114     set_jump_tgt_here(desc, skip_key_load);
0115 
0116     /* Class 1 operation */
0117     append_operation(desc, adata->algtype | state | OP_ALG_ENCRYPT);
0118 
0119     /*
0120      * Load from buf and/or src and write to req->result or state->context
0121      * Calculate remaining bytes to read
0122      */
0123     append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
0124 
0125     /* Read remaining bytes */
0126     append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLD_TYPE_LAST1 |
0127                  FIFOLD_TYPE_MSG | FIFOLDST_VLF);
0128 
0129     /*
0130      * Save context:
0131      * - xcbc: partial hash, keys K2 and K3
0132      * - cmac: partial hash, constant L = E(K,0)
0133      */
0134     append_seq_store(desc, digestsize, LDST_CLASS_1_CCB |
0135              LDST_SRCDST_BYTE_CONTEXT);
0136     if (is_xcbc_aes(adata->algtype) && state == OP_ALG_AS_INIT)
0137         /* Save K1 */
0138         append_fifo_store(desc, adata->key_dma, adata->keylen,
0139                   LDST_CLASS_1_CCB | FIFOST_TYPE_KEY_KEK);
0140 }
0141 EXPORT_SYMBOL(cnstr_shdsc_sk_hash);
0142 
0143 MODULE_LICENSE("Dual BSD/GPL");
0144 MODULE_DESCRIPTION("FSL CAAM ahash descriptors support");
0145 MODULE_AUTHOR("NXP Semiconductors");