|
51 | 51 |
|
52 | 52 | #include <crypto/aes.h> |
53 | 53 | #include <crypto/algapi.h> |
54 | | -#include <crypto/hash.h> |
55 | 54 | #include <crypto/gcm.h> |
56 | 55 | #include <crypto/sha1.h> |
57 | 56 | #include <crypto/sha2.h> |
@@ -277,88 +276,60 @@ static void get_aes_decrypt_key(unsigned char *dec_key, |
277 | 276 | } |
278 | 277 | } |
279 | 278 |
|
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) |
281 | 281 | { |
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) { |
285 | 293 | 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 | + } |
287 | 299 | break; |
288 | 300 | 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 | + } |
290 | 306 | break; |
291 | 307 | 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 | + } |
293 | 313 | break; |
294 | 314 | 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 | + } |
296 | 320 | break; |
297 | 321 | 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 | + } |
299 | 327 | break; |
| 328 | + default: |
| 329 | + return -EINVAL; |
300 | 330 | } |
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; |
362 | 333 | } |
363 | 334 |
|
364 | 335 | static inline int is_hmac(struct crypto_tfm *tfm) |
@@ -1547,11 +1518,6 @@ static int get_alg_config(struct algo_param *params, |
1547 | 1518 | return 0; |
1548 | 1519 | } |
1549 | 1520 |
|
1550 | | -static inline void chcr_free_shash(struct crypto_shash *base_hash) |
1551 | | -{ |
1552 | | - crypto_free_shash(base_hash); |
1553 | | -} |
1554 | | - |
1555 | 1521 | /** |
1556 | 1522 | * create_hash_wr - Create hash work request |
1557 | 1523 | * @req: Cipher req base |
@@ -2202,53 +2168,13 @@ static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, |
2202 | 2168 | unsigned int keylen) |
2203 | 2169 | { |
2204 | 2170 | 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); |
2210 | 2171 |
|
2211 | 2172 | /* use the key to calculate the ipad and opad. ipad will sent with the |
2212 | 2173 | * first request's data. opad will be sent with the final hash result |
2213 | 2174 | * ipad in hmacctx->ipad and opad in hmacctx->opad location |
2214 | 2175 | */ |
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); |
2252 | 2178 | } |
2253 | 2179 |
|
2254 | 2180 | 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) |
2344 | 2270 |
|
2345 | 2271 | static int chcr_hmac_cra_init(struct crypto_tfm *tfm) |
2346 | 2272 | { |
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 | | - |
2352 | 2273 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
2353 | 2274 | 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); |
2357 | 2275 | return chcr_device_init(crypto_tfm_ctx(tfm)); |
2358 | 2276 | } |
2359 | 2277 |
|
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 | | - |
2371 | 2278 | inline void chcr_aead_common_exit(struct aead_request *req) |
2372 | 2279 | { |
2373 | 2280 | 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, |
3557 | 3464 | struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx); |
3558 | 3465 | /* it contains auth and cipher key both*/ |
3559 | 3466 | struct crypto_authenc_keys keys; |
3560 | | - unsigned int bs, subtype; |
| 3467 | + unsigned int subtype; |
3561 | 3468 | 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; |
3563 | 3470 | 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); |
3566 | 3471 | struct algo_param param; |
3567 | 3472 | int align; |
3568 | | - u8 *o_ptr = NULL; |
3569 | 3473 |
|
3570 | 3474 | crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK); |
3571 | 3475 | 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, |
3613 | 3517 | get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key, |
3614 | 3518 | aeadctx->enckey_len << 3); |
3615 | 3519 | } |
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) |
3619 | 3526 | 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; |
3655 | 3527 |
|
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; |
3658 | 3536 |
|
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 | | - } |
3673 | 3537 | out: |
3674 | 3538 | aeadctx->enckey_len = 0; |
3675 | 3539 | memzero_explicit(&keys, sizeof(keys)); |
3676 | | - if (!IS_ERR(base_hash)) |
3677 | | - chcr_free_shash(base_hash); |
3678 | 3540 | return -EINVAL; |
3679 | 3541 | } |
3680 | 3542 |
|
@@ -4490,7 +4352,6 @@ static int chcr_register_alg(void) |
4490 | 4352 |
|
4491 | 4353 | if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) { |
4492 | 4354 | a_hash->halg.base.cra_init = chcr_hmac_cra_init; |
4493 | | - a_hash->halg.base.cra_exit = chcr_hmac_cra_exit; |
4494 | 4355 | a_hash->init = chcr_hmac_init; |
4495 | 4356 | a_hash->setkey = chcr_ahash_setkey; |
4496 | 4357 | a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX; |
|
0 commit comments