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

crypto: caam - consolidate split key length computation

Move split key length and padded length computation from caamalg.c
and caamhash.c to key_gen.c.
Signed-off-by: default avatarHoria Geantă <horia.geanta@nxp.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 8cea7b66
...@@ -586,18 +586,9 @@ static int rfc4543_setauthsize(struct crypto_aead *authenc, ...@@ -586,18 +586,9 @@ static int rfc4543_setauthsize(struct crypto_aead *authenc,
return 0; return 0;
} }
static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in,
u32 authkeylen)
{
return gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key_in,
authkeylen);
}
static int aead_setkey(struct crypto_aead *aead, static int aead_setkey(struct crypto_aead *aead,
const u8 *key, unsigned int keylen) const u8 *key, unsigned int keylen)
{ {
/* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
struct caam_ctx *ctx = crypto_aead_ctx(aead); struct caam_ctx *ctx = crypto_aead_ctx(aead);
struct device *jrdev = ctx->jrdev; struct device *jrdev = ctx->jrdev;
struct crypto_authenc_keys keys; struct crypto_authenc_keys keys;
...@@ -606,26 +597,17 @@ static int aead_setkey(struct crypto_aead *aead, ...@@ -606,26 +597,17 @@ static int aead_setkey(struct crypto_aead *aead,
if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
goto badkey; goto badkey;
/* Pick class 2 key length from algorithm submask */
ctx->adata.keylen = mdpadlen[(ctx->adata.algtype &
OP_ALG_ALGSEL_SUBMASK) >>
OP_ALG_ALGSEL_SHIFT] * 2;
ctx->adata.keylen_pad = ALIGN(ctx->adata.keylen, 16);
if (ctx->adata.keylen_pad + keys.enckeylen > CAAM_MAX_KEY_SIZE)
goto badkey;
#ifdef DEBUG #ifdef DEBUG
printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n", printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
keys.authkeylen + keys.enckeylen, keys.enckeylen, keys.authkeylen + keys.enckeylen, keys.enckeylen,
keys.authkeylen); keys.authkeylen);
printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
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
ret = gen_split_aead_key(ctx, keys.authkey, keys.authkeylen); ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, keys.authkey,
keys.authkeylen, CAAM_MAX_KEY_SIZE -
keys.enckeylen);
if (ret) { if (ret) {
goto badkey; goto badkey;
} }
......
...@@ -398,12 +398,6 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash) ...@@ -398,12 +398,6 @@ static int ahash_set_sh_desc(struct crypto_ahash *ahash)
return 0; return 0;
} }
static int gen_split_hash_key(struct caam_hash_ctx *ctx, const u8 *key_in,
u32 keylen)
{
return gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key_in, keylen);
}
/* Digest hash size if it is too large */ /* Digest hash size if it is too large */
static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in, static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
u32 *keylen, u8 *key_out, u32 digestsize) u32 *keylen, u8 *key_out, u32 digestsize)
...@@ -483,8 +477,6 @@ static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in, ...@@ -483,8 +477,6 @@ static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
static int ahash_setkey(struct crypto_ahash *ahash, static int ahash_setkey(struct crypto_ahash *ahash,
const u8 *key, unsigned int keylen) const u8 *key, unsigned int keylen)
{ {
/* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash); struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
struct device *jrdev = ctx->jrdev; struct device *jrdev = ctx->jrdev;
int blocksize = crypto_tfm_alg_blocksize(&ahash->base); int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
...@@ -509,20 +501,8 @@ static int ahash_setkey(struct crypto_ahash *ahash, ...@@ -509,20 +501,8 @@ static int ahash_setkey(struct crypto_ahash *ahash,
key = hashed_key; key = hashed_key;
} }
/* Pick class 2 key length from algorithm submask */ ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key, keylen,
ctx->adata.keylen = mdpadlen[(ctx->adata.algtype & CAAM_MAX_HASH_KEY_SIZE);
OP_ALG_ALGSEL_SUBMASK) >>
OP_ALG_ALGSEL_SHIFT] * 2;
ctx->adata.keylen_pad = ALIGN(ctx->adata.keylen, 16);
#ifdef DEBUG
printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
ctx->adata.keylen, ctx->adata.keylen_pad);
print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif
ret = gen_split_hash_key(ctx, key, keylen);
if (ret) if (ret)
goto bad_free_key; goto bad_free_key;
......
...@@ -10,6 +10,36 @@ ...@@ -10,6 +10,36 @@
#include "desc_constr.h" #include "desc_constr.h"
#include "key_gen.h" #include "key_gen.h"
/**
* split_key_len - Compute MDHA split key length for a given algorithm
* @hash: Hashing algorithm selection, one of OP_ALG_ALGSEL_* - MD5, SHA1,
* SHA224, SHA384, SHA512.
*
* Return: MDHA split key length
*/
static inline u32 split_key_len(u32 hash)
{
/* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
u32 idx;
idx = (hash & OP_ALG_ALGSEL_SUBMASK) >> OP_ALG_ALGSEL_SHIFT;
return (u32)(mdpadlen[idx] * 2);
}
/**
* split_key_pad_len - Compute MDHA split key pad length for a given algorithm
* @hash: Hashing algorithm selection, one of OP_ALG_ALGSEL_* - MD5, SHA1,
* SHA224, SHA384, SHA512.
*
* Return: MDHA split key pad length
*/
static inline u32 split_key_pad_len(u32 hash)
{
return ALIGN(split_key_len(hash), 16);
}
void split_key_done(struct device *dev, u32 *desc, u32 err, void split_key_done(struct device *dev, u32 *desc, u32 err,
void *context) void *context)
{ {
...@@ -42,13 +72,28 @@ Split key generation----------------------------------------------- ...@@ -42,13 +72,28 @@ Split key generation-----------------------------------------------
@0xffe04000 @0xffe04000
*/ */
int gen_split_key(struct device *jrdev, u8 *key_out, int gen_split_key(struct device *jrdev, u8 *key_out,
struct alginfo * const adata, const u8 *key_in, u32 keylen) struct alginfo * const adata, const u8 *key_in, u32 keylen,
int max_keylen)
{ {
u32 *desc; u32 *desc;
struct split_key_result result; struct split_key_result result;
dma_addr_t dma_addr_in, dma_addr_out; dma_addr_t dma_addr_in, dma_addr_out;
int ret = -ENOMEM; int ret = -ENOMEM;
adata->keylen = split_key_len(adata->algtype & OP_ALG_ALGSEL_MASK);
adata->keylen_pad = split_key_pad_len(adata->algtype &
OP_ALG_ALGSEL_MASK);
#ifdef DEBUG
dev_err(jrdev, "split keylen %d split keylen padded %d\n",
adata->keylen, adata->keylen_pad);
print_hex_dump(KERN_ERR, "ctx.key@" __stringify(__LINE__)": ",
DUMP_PREFIX_ADDRESS, 16, 4, key_in, keylen, 1);
#endif
if (adata->keylen_pad > max_keylen)
return -EINVAL;
desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA); desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
if (!desc) { if (!desc) {
dev_err(jrdev, "unable to allocate key input memory\n"); dev_err(jrdev, "unable to allocate key input memory\n");
......
...@@ -13,4 +13,5 @@ struct split_key_result { ...@@ -13,4 +13,5 @@ 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 gen_split_key(struct device *jrdev, u8 *key_out,
struct alginfo * const adata, const u8 *key_in, u32 keylen); struct alginfo * const adata, const u8 *key_in, u32 keylen,
int max_keylen);
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