Skip to content

Commit bc6e2bd

Browse files
Mathias Krauseherbertx
authored andcommitted
crypto: authenc - Export key parsing helper function
AEAD key parsing is duplicated to multiple places in the kernel. Add a common helper function to consolidate that functionality. Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: "David S. Miller" <davem@davemloft.net> Signed-off-by: Mathias Krause <mathias.krause@secunet.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 6d3aab4 commit bc6e2bd

2 files changed

Lines changed: 41 additions & 19 deletions

File tree

‎crypto/authenc.c‎

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,52 @@ static void authenc_request_complete(struct aead_request *req, int err)
5252
aead_request_complete(req, err);
5353
}
5454

55-
static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
56-
unsigned int keylen)
55+
int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
56+
unsigned int keylen)
5757
{
58-
unsigned int authkeylen;
59-
unsigned int enckeylen;
60-
struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
61-
struct crypto_ahash *auth = ctx->auth;
62-
struct crypto_ablkcipher *enc = ctx->enc;
63-
struct rtattr *rta = (void *)key;
58+
struct rtattr *rta = (struct rtattr *)key;
6459
struct crypto_authenc_key_param *param;
65-
int err = -EINVAL;
6660

6761
if (!RTA_OK(rta, keylen))
68-
goto badkey;
62+
return -EINVAL;
6963
if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
70-
goto badkey;
64+
return -EINVAL;
7165
if (RTA_PAYLOAD(rta) < sizeof(*param))
72-
goto badkey;
66+
return -EINVAL;
7367

7468
param = RTA_DATA(rta);
75-
enckeylen = be32_to_cpu(param->enckeylen);
69+
keys->enckeylen = be32_to_cpu(param->enckeylen);
7670

7771
key += RTA_ALIGN(rta->rta_len);
7872
keylen -= RTA_ALIGN(rta->rta_len);
7973

80-
if (keylen < enckeylen)
81-
goto badkey;
74+
if (keylen < keys->enckeylen)
75+
return -EINVAL;
8276

83-
authkeylen = keylen - enckeylen;
77+
keys->authkeylen = keylen - keys->enckeylen;
78+
keys->authkey = key;
79+
keys->enckey = key + keys->authkeylen;
80+
81+
return 0;
82+
}
83+
EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);
84+
85+
static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
86+
unsigned int keylen)
87+
{
88+
struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
89+
struct crypto_ahash *auth = ctx->auth;
90+
struct crypto_ablkcipher *enc = ctx->enc;
91+
struct crypto_authenc_keys keys;
92+
int err = -EINVAL;
93+
94+
if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
95+
goto badkey;
8496

8597
crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
8698
crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
8799
CRYPTO_TFM_REQ_MASK);
88-
err = crypto_ahash_setkey(auth, key, authkeylen);
100+
err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
89101
crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
90102
CRYPTO_TFM_RES_MASK);
91103

@@ -95,7 +107,7 @@ static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
95107
crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
96108
crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
97109
CRYPTO_TFM_REQ_MASK);
98-
err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
110+
err = crypto_ablkcipher_setkey(enc, keys.enckey, keys.enckeylen);
99111
crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
100112
CRYPTO_TFM_RES_MASK);
101113

‎include/crypto/authenc.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,15 @@ struct crypto_authenc_key_param {
2323
__be32 enckeylen;
2424
};
2525

26-
#endif /* _CRYPTO_AUTHENC_H */
26+
struct crypto_authenc_keys {
27+
const u8 *authkey;
28+
const u8 *enckey;
29+
30+
unsigned int authkeylen;
31+
unsigned int enckeylen;
32+
};
2733

34+
int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
35+
unsigned int keylen);
36+
37+
#endif /* _CRYPTO_AUTHENC_H */

0 commit comments

Comments
 (0)