Authenc key parsing consolidation
cover Authenc key parsing consolidation
This series removes the code duplication of authenc key parsing by introducing a common helper function crypto_authenc_extractkeys() in patch 1. Patches 2 to 5 change all remaining places to use the new helper. Patches 3 and 4 also fix potential memory corruptions by ensuring the supplied keys won't overflow there respective buffers.
I was unable to test patches 3 to 5 as I don't have the needed hardware for these devices -- not even a cross compiler for those architectures.
In case patches 3 and 4 are enqueued for stable, patch 1 needs to be as well, as it's a prerequisite for those.
Please apply!
Mathias Krause (5):
crypto: authenc - Export key parsing helper function
crypto: authencesn - Simplify key parsing
crypto: ixp4xx - Simplify and harden key parsing
crypto: picoxcell - Simplify and harden key parsing
crypto: talitos - Simplify key parsing
crypto/authenc.c | 48 +++++++++++++++++++++++-------------- crypto/authencesn.c | 26 +++----------------- drivers/crypto/ixp4xx_crypto.c | 26 +++++++------------- drivers/crypto/picoxcell_crypto.c | 32 ++++++------------------ drivers/crypto/talitos.c | 35 ++++++-------------------- include/crypto/authenc.h | 12 ++++++++- 6 files changed, 70 insertions(+), 109 deletions(-)
Signature · 1 line
1.7.2.5
PATCH 2/5 crypto: authencesn - Simplify key parsing
Use the common helper function crypto_authenc_extractkeys() for key parsing.
--- crypto/authencesn.c | 26 ++++---------------------- 1 files changed, 4 insertions(+), 22 deletions(-)
crypto/authencesn.c +4 -22
@@ -59,37 +59,19 @@ static void authenc_esn_request_complete(struct aead_request *req, int err)static int crypto_authenc_esn_setkey (struct crypto_aead *authenc_esn,const u8 *key,unsigned int keylen) { -unsigned int authkeylen; -unsigned int enckeylen;struct crypto_authenc_esn_ctx *ctx =crypto_aead_ctx (authenc_esn);struct crypto_ahash *auth = ctx->auth;struct crypto_ablkcipher *enc = ctx->enc; -struct rtattr *rta = (void *)key; -struct crypto_authenc_key_param *param; +struct crypto_authenc_keys keys;int err = -EINVAL ; -if (!RTA_OK (rta, keylen)) +if (crypto_authenc_extractkeys (&keys, key, keylen) !=0 ) goto badkey; -if (rta->rta_type !=CRYPTO_AUTHENC_KEYA_PARAM ) - goto badkey; -if (RTA_PAYLOAD (rta) <sizeof (*param)) - goto badkey; - - param =RTA_DATA (rta); - enckeylen =be32_to_cpu (param->enckeylen); - - key +=RTA_ALIGN (rta->rta_len); - keylen -=RTA_ALIGN (rta->rta_len); - -if (keylen < enckeylen) - goto badkey; - - authkeylen = keylen - enckeylen;crypto_ahash_clear_flags (auth,CRYPTO_TFM_REQ_MASK );crypto_ahash_set_flags (auth,crypto_aead_get_flags (authenc_esn) &CRYPTO_TFM_REQ_MASK ); - err =crypto_ahash_setkey (auth, key, authkeylen); + err =crypto_ahash_setkey (auth, keys.authkey, keys.authkeylen);crypto_aead_set_flags (authenc_esn,crypto_ahash_get_flags (auth) &CRYPTO_TFM_RES_MASK );
@@ -99,7 +81,7 @@ static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *crypto_ablkcipher_clear_flags (enc,CRYPTO_TFM_REQ_MASK );crypto_ablkcipher_set_flags (enc,crypto_aead_get_flags (authenc_esn) &CRYPTO_TFM_REQ_MASK ); - err =crypto_ablkcipher_setkey (enc, key + authkeylen, enckeylen); + err =crypto_ablkcipher_setkey (enc, keys.enckey, keys.enckeylen);crypto_aead_set_flags (authenc_esn,crypto_ablkcipher_get_flags (enc) &CRYPTO_TFM_RES_MASK );
Signature · 1 line
1.7.2.5
PATCH 1/5 crypto: authenc - Export key parsing helper function
In reply to Mathias KrauseAEAD key parsing is duplicated to multiple places in the kernel. Add a common helper function to consolidate that functionality.
--- crypto/authenc.c | 48 ++++++++++++++++++++++++++++----------------- include/crypto/authenc.h | 12 ++++++++++- 2 files changed, 41 insertions(+), 19 deletions(-)
crypto/authenc.c +30 -18
@@ -52,40 +52,52 @@ static void authenc_request_complete(struct aead_request *req, int err)aead_request_complete (req, err); } -static int crypto_authenc_setkey (struct crypto_aead *authenc,const u8 *key, -unsigned int keylen) +int crypto_authenc_extractkeys (struct crypto_authenc_keys *keys,const u8 *key, +unsigned int keylen) { -unsigned int authkeylen; -unsigned int enckeylen; -struct crypto_authenc_ctx *ctx =crypto_aead_ctx (authenc); -struct crypto_ahash *auth = ctx->auth; -struct crypto_ablkcipher *enc = ctx->enc; -struct rtattr *rta = (void *)key; +struct rtattr *rta = (struct rtattr *)key;struct crypto_authenc_key_param *param; -int err = -EINVAL ;if (!RTA_OK (rta, keylen)) - goto badkey; +return -EINVAL ;if (rta->rta_type !=CRYPTO_AUTHENC_KEYA_PARAM ) - goto badkey; +return -EINVAL ;if (RTA_PAYLOAD (rta) <sizeof (*param)) - goto badkey; +return -EINVAL ; param =RTA_DATA (rta); - enckeylen =be32_to_cpu (param->enckeylen); + keys->enckeylen =be32_to_cpu (param->enckeylen); key +=RTA_ALIGN (rta->rta_len); keylen -=RTA_ALIGN (rta->rta_len); -if (keylen < enckeylen) - goto badkey; +if (keylen < keys->enckeylen) +return -EINVAL ; - authkeylen = keylen - enckeylen; + keys->authkeylen = keylen - keys->enckeylen; + keys->authkey = key; + keys->enckey = key + keys->authkeylen; + +return 0 ; +} +EXPORT_SYMBOL_GPL (crypto_authenc_extractkeys); + +static int crypto_authenc_setkey (struct crypto_aead *authenc,const u8 *key, +unsigned int keylen) +{ +struct crypto_authenc_ctx *ctx =crypto_aead_ctx (authenc); +struct crypto_ahash *auth = ctx->auth; +struct crypto_ablkcipher *enc = ctx->enc; +struct crypto_authenc_keys keys; +int err = -EINVAL ; + +if (crypto_authenc_extractkeys (&keys, key, keylen) !=0 ) + goto badkey;crypto_ahash_clear_flags (auth,CRYPTO_TFM_REQ_MASK );crypto_ahash_set_flags (auth,crypto_aead_get_flags (authenc) &CRYPTO_TFM_REQ_MASK ); - err =crypto_ahash_setkey (auth, key, authkeylen); + err =crypto_ahash_setkey (auth, keys.authkey, keys.authkeylen);crypto_aead_set_flags (authenc,crypto_ahash_get_flags (auth) &CRYPTO_TFM_RES_MASK );
@@ -95,7 +107,7 @@ static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,crypto_ablkcipher_clear_flags (enc,CRYPTO_TFM_REQ_MASK );crypto_ablkcipher_set_flags (enc,crypto_aead_get_flags (authenc) &CRYPTO_TFM_REQ_MASK ); - err =crypto_ablkcipher_setkey (enc, key + authkeylen, enckeylen); + err =crypto_ablkcipher_setkey (enc, keys.enckey, keys.enckeylen);crypto_aead_set_flags (authenc,crypto_ablkcipher_get_flags (enc) &CRYPTO_TFM_RES_MASK );
include/crypto/authenc.h +11 -1
@@ -23,5 +23,15 @@ struct crypto_authenc_key_param { __be32 enckeylen; }; -#endif /* _CRYPTO_AUTHENC_H */ +struct crypto_authenc_keys { +const u8 *authkey; +const u8 *enckey; + +unsigned int authkeylen; +unsigned int enckeylen; +}; +int crypto_authenc_extractkeys (struct crypto_authenc_keys *keys,const u8 *key, +unsigned int keylen); + +#endif /* _CRYPTO_AUTHENC_H */
Signature · 1 line
1.7.2.5
PATCH 3/5 crypto: ixp4xx - Simplify and harden key parsing
In reply to Mathias KrauseUse the common helper function crypto_authenc_extractkeys() for key parsing. Also ensure the keys do fit into the corresponding buffers. Otherwise memory corruption might occur.
---
Not tested as I've no such hardware, nor the needed cross compiler!
drivers/crypto/ixp4xx_crypto.c | 26 +++++++++----------------- 1 files changed, 9 insertions(+), 17 deletions(-)
drivers/crypto/ixp4xx_crypto.c +9 -17
@@ -1159,32 +1159,24 @@ static int aead_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen) {struct ixp_ctx *ctx =crypto_aead_ctx (tfm); -struct rtattr *rta = (struct rtattr *)key; -struct crypto_authenc_key_param *param; +struct crypto_authenc_keys keys; -if (!RTA_OK (rta, keylen)) - goto badkey; -if (rta->rta_type !=CRYPTO_AUTHENC_KEYA_PARAM ) - goto badkey; -if (RTA_PAYLOAD (rta) <sizeof (*param)) +if (crypto_authenc_extractkeys (&keys, key, keylen) !=0 ) goto badkey; - param =RTA_DATA (rta); - ctx->enckey_len =be32_to_cpu (param->enckeylen); - - key +=RTA_ALIGN (rta->rta_len); - keylen -=RTA_ALIGN (rta->rta_len); +if (keys.authkeylen >sizeof (ctx->authkey)) + goto badkey; -if (keylen < ctx->enckey_len) +if (keys.enckeylen >sizeof (ctx->enckey)) goto badkey; - ctx->authkey_len = keylen - ctx->enckey_len; -memcpy (ctx->enckey, key + ctx->authkey_len, ctx->enckey_len); -memcpy (ctx->authkey, key, ctx->authkey_len); +memcpy (ctx->authkey, keys.authkey, keys.authkeylen); +memcpy (ctx->enckey, keys.enckey, keys.enckeylen); + ctx->authkey_len = keys.authkeylen; + ctx->enckey_len = keys.enckeylen;return aead_setup (tfm,crypto_aead_authsize (tfm)); badkey: - ctx->enckey_len =0 ;crypto_aead_set_flags (tfm,CRYPTO_TFM_RES_BAD_KEY_LEN );return -EINVAL ; }
Signature · 1 line
1.7.2.5
PATCH 4/5 crypto: picoxcell - Simplify and harden key parsing
In reply to Mathias KrauseUse the common helper function crypto_authenc_extractkeys() for key parsing. Also ensure the auth key won't overflow the hash_ctx buffer.
---
Not tested as I've no such hardware, nor the needed cross compiler!
drivers/crypto/picoxcell_crypto.c | 32 ++++++++------------------------ 1 files changed, 8 insertions(+), 24 deletions(-)
drivers/crypto/picoxcell_crypto.c +8 -24
@@ -495,45 +495,29 @@ static int spacc_aead_setkey(struct crypto_aead *tfm, const u8 *key, {struct spacc_aead_ctx *ctx =crypto_aead_ctx (tfm);struct spacc_alg *alg =to_spacc_alg (tfm->base.__crt_alg); -struct rtattr *rta = (void *)key; -struct crypto_authenc_key_param *param; -unsigned int authkeylen, enckeylen; +struct crypto_authenc_keys keys;int err = -EINVAL ; -if (!RTA_OK (rta, keylen)) +if (crypto_authenc_extractkeys (&keys, key, keylen) !=0 ) goto badkey; -if (rta->rta_type !=CRYPTO_AUTHENC_KEYA_PARAM ) +if (keys.enckeylen >AES_MAX_KEY_SIZE ) goto badkey; -if (RTA_PAYLOAD (rta) <sizeof (*param)) - goto badkey; - - param =RTA_DATA (rta); - enckeylen =be32_to_cpu (param->enckeylen); - - key +=RTA_ALIGN (rta->rta_len); - keylen -=RTA_ALIGN (rta->rta_len); - -if (keylen < enckeylen) - goto badkey; - - authkeylen = keylen - enckeylen; - -if (enckeylen >AES_MAX_KEY_SIZE ) +if (keys.authkeylen >sizeof (ctx->hash_ctx)) goto badkey;if ((alg->ctrl_default &SPACC_CRYPTO_ALG_MASK ) ==SPA_CTRL_CIPH_ALG_AES ) - err =spacc_aead_aes_setkey (tfm, key + authkeylen, enckeylen); + err =spacc_aead_aes_setkey (tfm, keys.enckey, keys.enckeylen);else - err =spacc_aead_des_setkey (tfm, key + authkeylen, enckeylen); + err =spacc_aead_des_setkey (tfm, keys.enckey, keys.enckeylen);if (err) goto badkey; -memcpy (ctx->hash_ctx, key, authkeylen); - ctx->hash_key_len = authkeylen; +memcpy (ctx->hash_ctx, keys.authkey, keys.authkeylen); + ctx->hash_key_len = keys.authkeylen;return 0 ;
Signature · 1 line
1.7.2.5
PATCH 5/5 crypto: talitos - Simplify key parsing
In reply to Mathias KrauseUse the common helper function crypto_authenc_extractkeys() for key parsing.
---
Not tested as I've no such hardware, nor the needed cross compiler!
drivers/crypto/talitos.c | 35 ++++++++--------------------------- 1 files changed, 8 insertions(+), 27 deletions(-)
drivers/crypto/talitos.c +8 -27
@@ -671,39 +671,20 @@ static int aead_setkey(struct crypto_aead *authenc, const u8 *key, unsigned int keylen) {struct talitos_ctx *ctx =crypto_aead_ctx (authenc); -struct rtattr *rta = (void *)key; -struct crypto_authenc_key_param *param; -unsigned int authkeylen; -unsigned int enckeylen; - -if (!RTA_OK (rta, keylen)) - goto badkey; +struct crypto_authenc_keys keys; -if (rta->rta_type !=CRYPTO_AUTHENC_KEYA_PARAM ) +if (crypto_authenc_extractkeys (&keys, key, keylen) !=0 ) goto badkey; -if (RTA_PAYLOAD (rta) <sizeof (*param)) +if (keys.authkeylen + keys.enckeylen >TALITOS_MAX_KEY_SIZE ) goto badkey; - param =RTA_DATA (rta); - enckeylen =be32_to_cpu (param->enckeylen); - - key +=RTA_ALIGN (rta->rta_len); - keylen -=RTA_ALIGN (rta->rta_len); - -if (keylen < enckeylen) - goto badkey; +memcpy (ctx->key, keys.authkey, keys.authkeylen); +memcpy (&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen); - authkeylen = keylen - enckeylen; - -if (keylen >TALITOS_MAX_KEY_SIZE ) - goto badkey; - -memcpy (&ctx->key, key, keylen); - - ctx->keylen = keylen; - ctx->enckeylen = enckeylen; - ctx->authkeylen = authkeylen; + ctx->keylen = keys.authkeylen + keys.enckeylen; + ctx->enckeylen = keys.enckeylen; + ctx->authkeylen = keys.authkeylen;return 0 ;
Signature · 1 line
1.7.2.5
Herbert Xu
maintainerQuoted 6 lines · Mathias Krause
This series removes the code duplication of authenc key parsing by introducing a common helper function
crypto_authenc_extractkeys()in patch 1. Patches 2 to 5 change all remaining places to use the new helper. Patches 3 and 4 also fix potential memory corruptions by ensuring the supplied keys won't overflow there respective buffers.
Quoted 7 lines
I was unable to test patches 3 to 5 as I don't have the needed hardware for these devices -- not even a cross compiler for those architectures.
In case patches 3 and 4 are enqueued for stable, patch 1 needs to be as well, as it's a prerequisite for those.
Please apply!
All applied. Thanks!
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt