Skip to content

Commit 9c6ed10

Browse files
Eric Biggersherbertx
authored andcommitted
crypto: chelsio - Use library to prepare HMAC keys
To prepare HMAC keys, just use the library functions instead of crypto_shash. This is much simpler, avoids depending on the fragile export_core and import_core methods, and is faster too. Signed-off-by: Eric Biggers <ebiggers@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 408cf48 commit 9c6ed10

3 files changed

Lines changed: 63 additions & 203 deletions

File tree

drivers/crypto/chelsio/Kconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ config CRYPTO_DEV_CHELSIO
44
depends on CHELSIO_T4
55
select CRYPTO_LIB_AES
66
select CRYPTO_LIB_GF128MUL
7-
select CRYPTO_SHA1
8-
select CRYPTO_SHA256
9-
select CRYPTO_SHA512
7+
select CRYPTO_LIB_SHA1
8+
select CRYPTO_LIB_SHA256
9+
select CRYPTO_LIB_SHA512
1010
select CRYPTO_AUTHENC
1111
help
1212
The Chelsio Crypto Co-processor driver for T6 adapters.

drivers/crypto/chelsio/chcr_algo.c

Lines changed: 60 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151

5252
#include <crypto/aes.h>
5353
#include <crypto/algapi.h>
54-
#include <crypto/hash.h>
5554
#include <crypto/gcm.h>
5655
#include <crypto/sha1.h>
5756
#include <crypto/sha2.h>
@@ -277,88 +276,60 @@ static void get_aes_decrypt_key(unsigned char *dec_key,
277276
}
278277
}
279278

280-
static struct crypto_shash *chcr_alloc_shash(unsigned int ds)
279+
static int chcr_prepare_hmac_key(const u8 *raw_key, unsigned int raw_key_len,
280+
int digestsize, void *istate, void *ostate)
281281
{
282-
struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
283-
284-
switch (ds) {
282+
__be32 *istate32 = istate, *ostate32 = ostate;
283+
__be64 *istate64 = istate, *ostate64 = ostate;
284+
union {
285+
struct hmac_sha1_key sha1;
286+
struct hmac_sha224_key sha224;
287+
struct hmac_sha256_key sha256;
288+
struct hmac_sha384_key sha384;
289+
struct hmac_sha512_key sha512;
290+
} k;
291+
292+
switch (digestsize) {
285293
case SHA1_DIGEST_SIZE:
286-
base_hash = crypto_alloc_shash("sha1", 0, 0);
294+
hmac_sha1_preparekey(&k.sha1, raw_key, raw_key_len);
295+
for (int i = 0; i < ARRAY_SIZE(k.sha1.istate.h); i++) {
296+
istate32[i] = cpu_to_be32(k.sha1.istate.h[i]);
297+
ostate32[i] = cpu_to_be32(k.sha1.ostate.h[i]);
298+
}
287299
break;
288300
case SHA224_DIGEST_SIZE:
289-
base_hash = crypto_alloc_shash("sha224", 0, 0);
301+
hmac_sha224_preparekey(&k.sha224, raw_key, raw_key_len);
302+
for (int i = 0; i < ARRAY_SIZE(k.sha224.key.istate.h); i++) {
303+
istate32[i] = cpu_to_be32(k.sha224.key.istate.h[i]);
304+
ostate32[i] = cpu_to_be32(k.sha224.key.ostate.h[i]);
305+
}
290306
break;
291307
case SHA256_DIGEST_SIZE:
292-
base_hash = crypto_alloc_shash("sha256", 0, 0);
308+
hmac_sha256_preparekey(&k.sha256, raw_key, raw_key_len);
309+
for (int i = 0; i < ARRAY_SIZE(k.sha256.key.istate.h); i++) {
310+
istate32[i] = cpu_to_be32(k.sha256.key.istate.h[i]);
311+
ostate32[i] = cpu_to_be32(k.sha256.key.ostate.h[i]);
312+
}
293313
break;
294314
case SHA384_DIGEST_SIZE:
295-
base_hash = crypto_alloc_shash("sha384", 0, 0);
315+
hmac_sha384_preparekey(&k.sha384, raw_key, raw_key_len);
316+
for (int i = 0; i < ARRAY_SIZE(k.sha384.key.istate.h); i++) {
317+
istate64[i] = cpu_to_be64(k.sha384.key.istate.h[i]);
318+
ostate64[i] = cpu_to_be64(k.sha384.key.ostate.h[i]);
319+
}
296320
break;
297321
case SHA512_DIGEST_SIZE:
298-
base_hash = crypto_alloc_shash("sha512", 0, 0);
322+
hmac_sha512_preparekey(&k.sha512, raw_key, raw_key_len);
323+
for (int i = 0; i < ARRAY_SIZE(k.sha512.key.istate.h); i++) {
324+
istate64[i] = cpu_to_be64(k.sha512.key.istate.h[i]);
325+
ostate64[i] = cpu_to_be64(k.sha512.key.ostate.h[i]);
326+
}
299327
break;
328+
default:
329+
return -EINVAL;
300330
}
301-
302-
return base_hash;
303-
}
304-
305-
static int chcr_compute_partial_hash(struct shash_desc *desc,
306-
char *iopad, char *result_hash,
307-
int digest_size)
308-
{
309-
struct sha1_state sha1_st;
310-
struct sha256_state sha256_st;
311-
struct sha512_state sha512_st;
312-
int error;
313-
314-
if (digest_size == SHA1_DIGEST_SIZE) {
315-
error = crypto_shash_init(desc) ?:
316-
crypto_shash_update(desc, iopad, SHA1_BLOCK_SIZE) ?:
317-
crypto_shash_export_core(desc, &sha1_st);
318-
memcpy(result_hash, sha1_st.state, SHA1_DIGEST_SIZE);
319-
} else if (digest_size == SHA224_DIGEST_SIZE) {
320-
error = crypto_shash_init(desc) ?:
321-
crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
322-
crypto_shash_export_core(desc, &sha256_st);
323-
memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
324-
325-
} else if (digest_size == SHA256_DIGEST_SIZE) {
326-
error = crypto_shash_init(desc) ?:
327-
crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
328-
crypto_shash_export_core(desc, &sha256_st);
329-
memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
330-
331-
} else if (digest_size == SHA384_DIGEST_SIZE) {
332-
error = crypto_shash_init(desc) ?:
333-
crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
334-
crypto_shash_export_core(desc, &sha512_st);
335-
memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
336-
337-
} else if (digest_size == SHA512_DIGEST_SIZE) {
338-
error = crypto_shash_init(desc) ?:
339-
crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
340-
crypto_shash_export_core(desc, &sha512_st);
341-
memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
342-
} else {
343-
error = -EINVAL;
344-
pr_err("Unknown digest size %d\n", digest_size);
345-
}
346-
return error;
347-
}
348-
349-
static void chcr_change_order(char *buf, int ds)
350-
{
351-
int i;
352-
353-
if (ds == SHA512_DIGEST_SIZE) {
354-
for (i = 0; i < (ds / sizeof(u64)); i++)
355-
*((__be64 *)buf + i) =
356-
cpu_to_be64(*((u64 *)buf + i));
357-
} else {
358-
for (i = 0; i < (ds / sizeof(u32)); i++)
359-
*((__be32 *)buf + i) =
360-
cpu_to_be32(*((u32 *)buf + i));
361-
}
331+
memzero_explicit(&k, sizeof(k));
332+
return 0;
362333
}
363334

364335
static inline int is_hmac(struct crypto_tfm *tfm)
@@ -1547,11 +1518,6 @@ static int get_alg_config(struct algo_param *params,
15471518
return 0;
15481519
}
15491520

1550-
static inline void chcr_free_shash(struct crypto_shash *base_hash)
1551-
{
1552-
crypto_free_shash(base_hash);
1553-
}
1554-
15551521
/**
15561522
* create_hash_wr - Create hash work request
15571523
* @req: Cipher req base
@@ -2202,53 +2168,13 @@ static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
22022168
unsigned int keylen)
22032169
{
22042170
struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
2205-
unsigned int digestsize = crypto_ahash_digestsize(tfm);
2206-
unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2207-
unsigned int i, err = 0, updated_digestsize;
2208-
2209-
SHASH_DESC_ON_STACK(shash, hmacctx->base_hash);
22102171

22112172
/* use the key to calculate the ipad and opad. ipad will sent with the
22122173
* first request's data. opad will be sent with the final hash result
22132174
* ipad in hmacctx->ipad and opad in hmacctx->opad location
22142175
*/
2215-
shash->tfm = hmacctx->base_hash;
2216-
if (keylen > bs) {
2217-
err = crypto_shash_digest(shash, key, keylen,
2218-
hmacctx->ipad);
2219-
if (err)
2220-
goto out;
2221-
keylen = digestsize;
2222-
} else {
2223-
memcpy(hmacctx->ipad, key, keylen);
2224-
}
2225-
memset(hmacctx->ipad + keylen, 0, bs - keylen);
2226-
unsafe_memcpy(hmacctx->opad, hmacctx->ipad, bs,
2227-
"fortified memcpy causes -Wrestrict warning");
2228-
2229-
for (i = 0; i < bs / sizeof(int); i++) {
2230-
*((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
2231-
*((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
2232-
}
2233-
2234-
updated_digestsize = digestsize;
2235-
if (digestsize == SHA224_DIGEST_SIZE)
2236-
updated_digestsize = SHA256_DIGEST_SIZE;
2237-
else if (digestsize == SHA384_DIGEST_SIZE)
2238-
updated_digestsize = SHA512_DIGEST_SIZE;
2239-
err = chcr_compute_partial_hash(shash, hmacctx->ipad,
2240-
hmacctx->ipad, digestsize);
2241-
if (err)
2242-
goto out;
2243-
chcr_change_order(hmacctx->ipad, updated_digestsize);
2244-
2245-
err = chcr_compute_partial_hash(shash, hmacctx->opad,
2246-
hmacctx->opad, digestsize);
2247-
if (err)
2248-
goto out;
2249-
chcr_change_order(hmacctx->opad, updated_digestsize);
2250-
out:
2251-
return err;
2176+
return chcr_prepare_hmac_key(key, keylen, crypto_ahash_digestsize(tfm),
2177+
hmacctx->ipad, hmacctx->opad);
22522178
}
22532179

22542180
static int chcr_aes_xts_setkey(struct crypto_skcipher *cipher, const u8 *key,
@@ -2344,30 +2270,11 @@ static int chcr_hmac_init(struct ahash_request *areq)
23442270

23452271
static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
23462272
{
2347-
struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2348-
struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2349-
unsigned int digestsize =
2350-
crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
2351-
23522273
crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
23532274
sizeof(struct chcr_ahash_req_ctx));
2354-
hmacctx->base_hash = chcr_alloc_shash(digestsize);
2355-
if (IS_ERR(hmacctx->base_hash))
2356-
return PTR_ERR(hmacctx->base_hash);
23572275
return chcr_device_init(crypto_tfm_ctx(tfm));
23582276
}
23592277

2360-
static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
2361-
{
2362-
struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2363-
struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2364-
2365-
if (hmacctx->base_hash) {
2366-
chcr_free_shash(hmacctx->base_hash);
2367-
hmacctx->base_hash = NULL;
2368-
}
2369-
}
2370-
23712278
inline void chcr_aead_common_exit(struct aead_request *req)
23722279
{
23732280
struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
@@ -3557,15 +3464,12 @@ static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
35573464
struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
35583465
/* it contains auth and cipher key both*/
35593466
struct crypto_authenc_keys keys;
3560-
unsigned int bs, subtype;
3467+
unsigned int subtype;
35613468
unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
3562-
int err = 0, i, key_ctx_len = 0;
3469+
int err = 0, key_ctx_len = 0;
35633470
unsigned char ck_size = 0;
3564-
unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 };
3565-
struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
35663471
struct algo_param param;
35673472
int align;
3568-
u8 *o_ptr = NULL;
35693473

35703474
crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
35713475
crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
@@ -3613,68 +3517,26 @@ static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
36133517
get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
36143518
aeadctx->enckey_len << 3);
36153519
}
3616-
base_hash = chcr_alloc_shash(max_authsize);
3617-
if (IS_ERR(base_hash)) {
3618-
pr_err("Base driver cannot be loaded\n");
3520+
3521+
align = KEYCTX_ALIGN_PAD(max_authsize);
3522+
err = chcr_prepare_hmac_key(keys.authkey, keys.authkeylen, max_authsize,
3523+
actx->h_iopad,
3524+
actx->h_iopad + param.result_size + align);
3525+
if (err)
36193526
goto out;
3620-
}
3621-
{
3622-
SHASH_DESC_ON_STACK(shash, base_hash);
3623-
3624-
shash->tfm = base_hash;
3625-
bs = crypto_shash_blocksize(base_hash);
3626-
align = KEYCTX_ALIGN_PAD(max_authsize);
3627-
o_ptr = actx->h_iopad + param.result_size + align;
3628-
3629-
if (keys.authkeylen > bs) {
3630-
err = crypto_shash_digest(shash, keys.authkey,
3631-
keys.authkeylen,
3632-
o_ptr);
3633-
if (err) {
3634-
pr_err("Base driver cannot be loaded\n");
3635-
goto out;
3636-
}
3637-
keys.authkeylen = max_authsize;
3638-
} else
3639-
memcpy(o_ptr, keys.authkey, keys.authkeylen);
3640-
3641-
/* Compute the ipad-digest*/
3642-
memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3643-
memcpy(pad, o_ptr, keys.authkeylen);
3644-
for (i = 0; i < bs >> 2; i++)
3645-
*((unsigned int *)pad + i) ^= IPAD_DATA;
3646-
3647-
if (chcr_compute_partial_hash(shash, pad, actx->h_iopad,
3648-
max_authsize))
3649-
goto out;
3650-
/* Compute the opad-digest */
3651-
memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3652-
memcpy(pad, o_ptr, keys.authkeylen);
3653-
for (i = 0; i < bs >> 2; i++)
3654-
*((unsigned int *)pad + i) ^= OPAD_DATA;
36553527

3656-
if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize))
3657-
goto out;
3528+
key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16) +
3529+
(param.result_size + align) * 2;
3530+
aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size, 0, 1,
3531+
key_ctx_len >> 4);
3532+
actx->auth_mode = param.auth_mode;
3533+
3534+
memzero_explicit(&keys, sizeof(keys));
3535+
return 0;
36583536

3659-
/* convert the ipad and opad digest to network order */
3660-
chcr_change_order(actx->h_iopad, param.result_size);
3661-
chcr_change_order(o_ptr, param.result_size);
3662-
key_ctx_len = sizeof(struct _key_ctx) +
3663-
roundup(keys.enckeylen, 16) +
3664-
(param.result_size + align) * 2;
3665-
aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size,
3666-
0, 1, key_ctx_len >> 4);
3667-
actx->auth_mode = param.auth_mode;
3668-
chcr_free_shash(base_hash);
3669-
3670-
memzero_explicit(&keys, sizeof(keys));
3671-
return 0;
3672-
}
36733537
out:
36743538
aeadctx->enckey_len = 0;
36753539
memzero_explicit(&keys, sizeof(keys));
3676-
if (!IS_ERR(base_hash))
3677-
chcr_free_shash(base_hash);
36783540
return -EINVAL;
36793541
}
36803542

@@ -4490,7 +4352,6 @@ static int chcr_register_alg(void)
44904352

44914353
if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
44924354
a_hash->halg.base.cra_init = chcr_hmac_cra_init;
4493-
a_hash->halg.base.cra_exit = chcr_hmac_cra_exit;
44944355
a_hash->init = chcr_hmac_init;
44954356
a_hash->setkey = chcr_ahash_setkey;
44964357
a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;

drivers/crypto/chelsio/chcr_crypto.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ struct chcr_aead_ctx {
241241
};
242242

243243
struct hmac_ctx {
244-
struct crypto_shash *base_hash;
245244
u8 ipad[CHCR_HASH_MAX_BLOCK_SIZE_128];
246245
u8 opad[CHCR_HASH_MAX_BLOCK_SIZE_128];
247246
};

0 commit comments

Comments
 (0)