Commit db57656b authored by Horia Geantă's avatar Horia Geantă Committed by Herbert Xu

crypto: caam - group algorithm related params

In preparation of factoring out the shared descriptors,
struct alginfo is introduced to group the algorithm related
parameters.
Signed-off-by: default avatarHoria Geantă <horia.geanta@nxp.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 64c9295b
This diff is collapsed.
...@@ -108,13 +108,11 @@ struct caam_hash_ctx { ...@@ -108,13 +108,11 @@ struct caam_hash_ctx {
dma_addr_t sh_desc_fin_dma; dma_addr_t sh_desc_fin_dma;
dma_addr_t sh_desc_digest_dma; dma_addr_t sh_desc_digest_dma;
struct device *jrdev; struct device *jrdev;
u32 alg_type;
u32 alg_op; u32 alg_op;
u8 key[CAAM_MAX_HASH_KEY_SIZE]; u8 key[CAAM_MAX_HASH_KEY_SIZE];
dma_addr_t key_dma; dma_addr_t key_dma;
int ctx_len; int ctx_len;
unsigned int split_key_len; struct alginfo adata;
unsigned int split_key_pad_len;
}; };
/* ahash state */ /* ahash state */
...@@ -223,9 +221,9 @@ static inline int ctx_map_to_sec4_sg(u32 *desc, struct device *jrdev, ...@@ -223,9 +221,9 @@ static inline int ctx_map_to_sec4_sg(u32 *desc, struct device *jrdev,
/* Common shared descriptor commands */ /* Common shared descriptor commands */
static inline void append_key_ahash(u32 *desc, struct caam_hash_ctx *ctx) static inline void append_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
{ {
append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len, append_key_as_imm(desc, ctx->key, ctx->adata.keylen_pad,
ctx->split_key_len, CLASS_2 | ctx->adata.keylen, CLASS_2 | KEY_DEST_MDHA_SPLIT |
KEY_DEST_MDHA_SPLIT | KEY_ENC); KEY_ENC);
} }
/* Append key if it has been set */ /* Append key if it has been set */
...@@ -235,7 +233,7 @@ static inline void init_sh_desc_key_ahash(u32 *desc, struct caam_hash_ctx *ctx) ...@@ -235,7 +233,7 @@ static inline void init_sh_desc_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
init_sh_desc(desc, HDR_SHARE_SERIAL); init_sh_desc(desc, HDR_SHARE_SERIAL);
if (ctx->split_key_len) { if (ctx->adata.keylen) {
/* Skip if already shared */ /* Skip if already shared */
key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
JUMP_COND_SHRD); JUMP_COND_SHRD);
...@@ -310,7 +308,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) ...@@ -310,7 +308,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash)
u32 have_key = 0; u32 have_key = 0;
u32 *desc; u32 *desc;
if (ctx->split_key_len) if (ctx->adata.keylen)
have_key = OP_ALG_AAI_HMAC_PRECOMP; have_key = OP_ALG_AAI_HMAC_PRECOMP;
/* ahash_update shared descriptor */ /* ahash_update shared descriptor */
...@@ -323,7 +321,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) ...@@ -323,7 +321,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash)
LDST_CLASS_2_CCB | ctx->ctx_len); LDST_CLASS_2_CCB | ctx->ctx_len);
/* Class 2 operation */ /* Class 2 operation */
append_operation(desc, ctx->alg_type | OP_ALG_AS_UPDATE | append_operation(desc, ctx->adata.algtype | OP_ALG_AS_UPDATE |
OP_ALG_ENCRYPT); OP_ALG_ENCRYPT);
/* Load data and write to result or context */ /* Load data and write to result or context */
...@@ -344,7 +342,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) ...@@ -344,7 +342,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash)
/* ahash_update_first shared descriptor */ /* ahash_update_first shared descriptor */
desc = ctx->sh_desc_update_first; desc = ctx->sh_desc_update_first;
ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INIT, ahash_data_to_out(desc, have_key | ctx->adata.algtype, OP_ALG_AS_INIT,
ctx->ctx_len, ctx); ctx->ctx_len, ctx);
ctx->sh_desc_update_first_dma = dma_map_single(jrdev, desc, ctx->sh_desc_update_first_dma = dma_map_single(jrdev, desc,
...@@ -363,7 +361,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) ...@@ -363,7 +361,7 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash)
/* ahash_final shared descriptor */ /* ahash_final shared descriptor */
desc = ctx->sh_desc_fin; desc = ctx->sh_desc_fin;
ahash_ctx_data_to_out(desc, have_key | ctx->alg_type, ahash_ctx_data_to_out(desc, have_key | ctx->adata.algtype,
OP_ALG_AS_FINALIZE, digestsize, ctx); OP_ALG_AS_FINALIZE, digestsize, ctx);
ctx->sh_desc_fin_dma = dma_map_single(jrdev, desc, desc_bytes(desc), ctx->sh_desc_fin_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
...@@ -381,8 +379,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) ...@@ -381,8 +379,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash)
/* ahash_digest shared descriptor */ /* ahash_digest shared descriptor */
desc = ctx->sh_desc_digest; desc = ctx->sh_desc_digest;
ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INITFINAL, ahash_data_to_out(desc, have_key | ctx->adata.algtype,
digestsize, ctx); OP_ALG_AS_INITFINAL, digestsize, ctx);
ctx->sh_desc_digest_dma = dma_map_single(jrdev, desc, ctx->sh_desc_digest_dma = dma_map_single(jrdev, desc,
desc_bytes(desc), desc_bytes(desc),
...@@ -404,9 +402,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) ...@@ -404,9 +402,8 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash)
static int gen_split_hash_key(struct caam_hash_ctx *ctx, const u8 *key_in, static int gen_split_hash_key(struct caam_hash_ctx *ctx, const u8 *key_in,
u32 keylen) u32 keylen)
{ {
return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len, return gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key_in, keylen,
ctx->split_key_pad_len, key_in, keylen, ctx->alg_op);
ctx->alg_op);
} }
/* Digest hash size if it is too large */ /* Digest hash size if it is too large */
...@@ -444,7 +441,7 @@ static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in, ...@@ -444,7 +441,7 @@ static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
} }
/* Job descriptor to perform unkeyed hash on key_in */ /* Job descriptor to perform unkeyed hash on key_in */
append_operation(desc, ctx->alg_type | OP_ALG_ENCRYPT | append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
OP_ALG_AS_INITFINAL); OP_ALG_AS_INITFINAL);
append_seq_in_ptr(desc, src_dma, *keylen, 0); append_seq_in_ptr(desc, src_dma, *keylen, 0);
append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 | append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
...@@ -515,13 +512,13 @@ static int ahash_setkey(struct crypto_ahash *ahash, ...@@ -515,13 +512,13 @@ static int ahash_setkey(struct crypto_ahash *ahash,
} }
/* Pick class 2 key length from algorithm submask */ /* Pick class 2 key length from algorithm submask */
ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >> ctx->adata.keylen = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
OP_ALG_ALGSEL_SHIFT] * 2; OP_ALG_ALGSEL_SHIFT] * 2;
ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16); ctx->adata.keylen_pad = ALIGN(ctx->adata.keylen, 16);
#ifdef DEBUG #ifdef DEBUG
printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n", printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
ctx->split_key_len, ctx->split_key_pad_len); ctx->adata.keylen, ctx->adata.keylen_pad);
print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ", print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1); DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif #endif
...@@ -530,7 +527,7 @@ static int ahash_setkey(struct crypto_ahash *ahash, ...@@ -530,7 +527,7 @@ static int ahash_setkey(struct crypto_ahash *ahash,
if (ret) if (ret)
goto bad_free_key; goto bad_free_key;
ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len, ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->adata.keylen_pad,
DMA_TO_DEVICE); DMA_TO_DEVICE);
if (dma_mapping_error(jrdev, ctx->key_dma)) { if (dma_mapping_error(jrdev, ctx->key_dma)) {
dev_err(jrdev, "unable to map key i/o memory\n"); dev_err(jrdev, "unable to map key i/o memory\n");
...@@ -540,14 +537,15 @@ static int ahash_setkey(struct crypto_ahash *ahash, ...@@ -540,14 +537,15 @@ static int ahash_setkey(struct crypto_ahash *ahash,
#ifdef DEBUG #ifdef DEBUG
print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ", print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
ctx->split_key_pad_len, 1); ctx->adata.keylen_pad, 1);
#endif #endif
ret = ahash_set_sh_desc(ahash); ret = ahash_set_sh_desc(ahash);
if (ret) { if (ret) {
dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len, dma_unmap_single(jrdev, ctx->key_dma, ctx->adata.keylen_pad,
DMA_TO_DEVICE); DMA_TO_DEVICE);
} }
error_free_key: error_free_key:
kfree(hashed_key); kfree(hashed_key);
return ret; return ret;
...@@ -1832,7 +1830,7 @@ static int caam_hash_cra_init(struct crypto_tfm *tfm) ...@@ -1832,7 +1830,7 @@ static int caam_hash_cra_init(struct crypto_tfm *tfm)
return PTR_ERR(ctx->jrdev); return PTR_ERR(ctx->jrdev);
} }
/* copy descriptor header template value */ /* copy descriptor header template value */
ctx->alg_type = OP_TYPE_CLASS2_ALG | caam_hash->alg_type; ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_hash->alg_op; ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_hash->alg_op;
ctx->ctx_len = runninglen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >> ctx->ctx_len = runninglen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
......
...@@ -430,3 +430,22 @@ do { \ ...@@ -430,3 +430,22 @@ do { \
APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data) APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data)
#define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \ #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \
APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data) APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data)
/**
* struct alginfo - Container for algorithm details
* @algtype: algorithm selector; for valid values, see documentation of the
* functions where it is used.
* @keylen: length of the provided algorithm key, in bytes
* @keylen_pad: padded length of the provided algorithm key, in bytes
* @key: address where algorithm key resides; virtual address if key_inline
* is true, dma (bus) address if key_inline is false.
* @key_inline: true - key can be inlined in the descriptor; false - key is
* referenced by the descriptor
*/
struct alginfo {
u32 algtype;
unsigned int keylen;
unsigned int keylen_pad;
u64 key;
bool key_inline;
};
...@@ -41,8 +41,8 @@ Split key generation----------------------------------------------- ...@@ -41,8 +41,8 @@ Split key generation-----------------------------------------------
[06] 0x64260028 fifostr: class2 mdsplit-jdk len=40 [06] 0x64260028 fifostr: class2 mdsplit-jdk len=40
@0xffe04000 @0xffe04000
*/ */
int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len, int gen_split_key(struct device *jrdev, u8 *key_out,
int split_key_pad_len, const u8 *key_in, u32 keylen, struct alginfo * const adata, const u8 *key_in, u32 keylen,
u32 alg_op) u32 alg_op)
{ {
u32 *desc; u32 *desc;
...@@ -63,7 +63,7 @@ int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len, ...@@ -63,7 +63,7 @@ int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len,
goto out_free; goto out_free;
} }
dma_addr_out = dma_map_single(jrdev, key_out, split_key_pad_len, dma_addr_out = dma_map_single(jrdev, key_out, adata->keylen_pad,
DMA_FROM_DEVICE); DMA_FROM_DEVICE);
if (dma_mapping_error(jrdev, dma_addr_out)) { if (dma_mapping_error(jrdev, dma_addr_out)) {
dev_err(jrdev, "unable to map key output memory\n"); dev_err(jrdev, "unable to map key output memory\n");
...@@ -87,7 +87,7 @@ int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len, ...@@ -87,7 +87,7 @@ int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len,
* FIFO_STORE with the explicit split-key content store * FIFO_STORE with the explicit split-key content store
* (0x26 output type) * (0x26 output type)
*/ */
append_fifo_store(desc, dma_addr_out, split_key_len, append_fifo_store(desc, dma_addr_out, adata->keylen,
LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK); LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK);
#ifdef DEBUG #ifdef DEBUG
...@@ -108,11 +108,11 @@ int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len, ...@@ -108,11 +108,11 @@ int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len,
#ifdef DEBUG #ifdef DEBUG
print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ", print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
DUMP_PREFIX_ADDRESS, 16, 4, key_out, DUMP_PREFIX_ADDRESS, 16, 4, key_out,
split_key_pad_len, 1); adata->keylen_pad, 1);
#endif #endif
} }
dma_unmap_single(jrdev, dma_addr_out, split_key_pad_len, dma_unmap_single(jrdev, dma_addr_out, adata->keylen_pad,
DMA_FROM_DEVICE); DMA_FROM_DEVICE);
out_unmap_in: out_unmap_in:
dma_unmap_single(jrdev, dma_addr_in, keylen, DMA_TO_DEVICE); dma_unmap_single(jrdev, dma_addr_in, keylen, DMA_TO_DEVICE);
......
...@@ -12,6 +12,6 @@ struct split_key_result { ...@@ -12,6 +12,6 @@ struct split_key_result {
void split_key_done(struct device *dev, u32 *desc, u32 err, void *context); void split_key_done(struct device *dev, u32 *desc, u32 err, void *context);
int gen_split_key(struct device *jrdev, u8 *key_out, int split_key_len, int gen_split_key(struct device *jrdev, u8 *key_out,
int split_key_pad_len, const u8 *key_in, u32 keylen, struct alginfo * const adata, const u8 *key_in, u32 keylen,
u32 alg_op); u32 alg_op);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment