Paes and Phmac: Refuse clear key material by default
Subsystems (4)
Harald Freudenberger
maintainerv2 cover Paes and Phmac: Refuse clear key material by default
The modivation of these patches is to disable clear key usage of the protected key implementations paes and phmac by default.
With a new kernel module parameter "clrkey" this behavior can be controlled. By default clrkey is 'N' but for testing purpose on module load a true value (1, 'Y') may be given to accept clear key tokens.
Note that during selftest clear keys are always used and thus as long as the algorithm is in larval state indicated by crypto_skcipher_tested() clear keys need to be accepted. However, in this state there is no way to establish an instance of the tfm other than for selftest reasons.
Changelog:
v0: Initial version. Request for internal feedback and review
Please note I assume that patch #1 goes via s390, whereas patch #2, #3 and #4 may go via Herbert Xu's Linux Kernel Crypto.
v1: Sequence changed and feedback from Ingo, Holger and Heiko
integrated.
v2: Integrated the feedback from Holger and Ingo. First version
going out to the linux kernel crypto mailing list
Harald Freudenberger (4):
crypto: skcipher - Add new helper function crypto_skcipher_tested s390/pkey: Support new xflag PKEY_XFLAG_NOCLEARKEY
crypto: s390/phmac - Refuse clear key material by default
crypto: s390/paes - Refuse clear key material by default
arch/s390/crypto/paes_s390.c | 93 ++++++++++++++++++------------ arch/s390/crypto/phmac_s390.c | 29 +++++++--- arch/s390/include/asm/pkey.h | 8 ++- drivers/s390/crypto/pkey_cca.c | 5 ++ drivers/s390/crypto/pkey_ep11.c | 5 ++ drivers/s390/crypto/pkey_pckmo.c | 12 +++- include/crypto/internal/skcipher.h | 7 +++ 7 files changed, 110 insertions(+), 49 deletions(-)
base-commit: 9448598b22c50c8a5bb77a9103e2d49f134c9578 --
2.43.0
Harald Freudenberger
maintainerPATCH v2 3/4 crypto: s390/phmac - Refuse clear key material by default
This patch exploits the new xflag PKEY_XFLAG_NOCLEARKEY from the pkey layer. So now by default the phmac refuses the use of clear key material ("clear key tokens") in the setkey function with -EINVAL.
With a new kernel module parameter "clrkey" this behavior can be controlled. By default clrkey is 'N' but for testing purpose on module load a true value (1, 'Y') may be given to accept clear key tokens.
Note that during selftest clear keys are always used and thus the xflag PKEY_XFLAG_NOCLEARKEY is NOT set as long as the algorithm is in a larval state indicated by crypto_ahash_tested() returning false.
--- arch/s390/crypto/phmac_s390.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
arch/s390/crypto/phmac_s390.c +20 -9
@@ -23,6 +23,10 @@static struct crypto_engine *phmac_crypto_engine;#define MAX_QLEN 10 +static bool pkey_clrkey_allowed; +module_param_named (clrkey, pkey_clrkey_allowed, bool,0444 ); +MODULE_PARM_DESC (clrkey,"Allow clear key material (default N)" ); +/* * A simple hash walk helper */
@@ -311,10 +315,14 @@ static inline int phmac_tfm_ctx_setkey(struct phmac_tfm_ctx *tfm_ctx,* This function may sleep - don't call in non-sleeping context. */ static inline int convert_key (const u8 *key,unsigned int keylen, -struct phmac_protkey *pk) +struct phmac_protkey *pk,bool tested) { +u32 xflags =PKEY_XFLAG_NOMEMALLOC ;int rc, i; +if (tested && !pkey_clrkey_allowed) + xflags |=PKEY_XFLAG_NOCLEARKEY ; + pk->len =sizeof (pk->protkey);/*
@@ -328,7 +336,7 @@ static inline int convert_key(const u8 *key, unsigned int keylen, } rc =pkey_key2protkey (key, keylen, pk->protkey, &pk->len, &pk->type, -PKEY_XFLAG_NOMEMALLOC ); + xflags); } out:
@@ -350,7 +358,7 @@ static inline int convert_key(const u8 *key, unsigned int keylen,* unnecessary additional conversion but never to invalid data on the * hash operation. */ -static int phmac_convert_key (struct phmac_tfm_ctx *tfm_ctx) +static int phmac_convert_key (struct phmac_tfm_ctx *tfm_ctx,bool tested) {struct phmac_protkey pk;int rc;
@@ -359,7 +367,7 @@ static int phmac_convert_key(struct phmac_tfm_ctx *tfm_ctx) tfm_ctx->pk_state =PK_STATE_CONVERT_IN_PROGRESS ;spin_unlock_bh (&tfm_ctx->pk_lock); - rc =convert_key (tfm_ctx->keybuf, tfm_ctx->keylen, &pk); + rc =convert_key (tfm_ctx->keybuf, tfm_ctx->keylen, &pk, tested);/* update context */ spin_lock_bh (&tfm_ctx->pk_lock);
@@ -404,6 +412,7 @@ static int phmac_kmac_update(struct ahash_request *req, bool maysleep)struct kmac_sha2_ctx *ctx = &req_ctx->kmac_ctx;struct hash_walk_helper *hwh = &req_ctx->hwh;unsigned int bs =crypto_ahash_blocksize (tfm); +bool tested =crypto_ahash_tested (tfm);unsigned int offset, k, n;int rc =0 ;
@@ -444,7 +453,7 @@ static int phmac_kmac_update(struct ahash_request *req, bool maysleep) rc = -EKEYEXPIRED ; goto out; } - rc =phmac_convert_key (tfm_ctx); + rc =phmac_convert_key (tfm_ctx, tested);if (rc) goto out;spin_lock_bh (&tfm_ctx->pk_lock);
@@ -480,7 +489,7 @@ static int phmac_kmac_update(struct ahash_request *req, bool maysleep) rc = -EKEYEXPIRED ; goto out; } - rc =phmac_convert_key (tfm_ctx); + rc =phmac_convert_key (tfm_ctx, tested);if (rc) goto out;spin_lock_bh (&tfm_ctx->pk_lock);
@@ -517,6 +526,7 @@ static int phmac_kmac_final(struct ahash_request *req, bool maysleep)struct kmac_sha2_ctx *ctx = &req_ctx->kmac_ctx;unsigned int ds =crypto_ahash_digestsize (tfm);unsigned int bs =crypto_ahash_blocksize (tfm); +bool tested =crypto_ahash_tested (tfm);unsigned int k, n;int rc =0 ;
@@ -537,7 +547,7 @@ static int phmac_kmac_final(struct ahash_request *req, bool maysleep) rc = -EKEYEXPIRED ; goto out; } - rc =phmac_convert_key (tfm_ctx); + rc =phmac_convert_key (tfm_ctx, tested);if (rc) goto out;spin_lock_bh (&tfm_ctx->pk_lock);
@@ -741,11 +751,12 @@ static int phmac_setkey(struct crypto_ahash *tfm,struct phmac_tfm_ctx *tfm_ctx =crypto_ahash_ctx (tfm);unsigned int ds =crypto_ahash_digestsize (tfm);unsigned int bs =crypto_ahash_blocksize (tfm); +bool tested =crypto_ahash_tested (tfm);unsigned int tmpkeylen;u8 *tmpkey =NULL ;int rc =0 ; -if (!crypto_ahash_tested (tfm)) { +if (!tested) {/* * selftest running: key is a raw hmac clear key and needs * to get embedded into a 'clear key token' in order to have
@@ -770,7 +781,7 @@ static int phmac_setkey(struct crypto_ahash *tfm, goto out;/* convert raw key into protected key */ - rc =phmac_convert_key (tfm_ctx); + rc =phmac_convert_key (tfm_ctx, tested);if (rc) goto out;
Signature · 1 line
2.43.0
Harald Freudenberger
maintainerPATCH v2 1/4 crypto: skcipher - Add new helper function crypto_skcipher_tested
In reply to Harald FreudenbergerAdd a new helper function crypto_skcipher_tested() which evaluates the CRYPTO_ALG_TESTED flag from the tfm base cra_flags field.
--- include/crypto/internal/skcipher.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/crypto/internal/skcipher.h +7 -0
@@ -242,6 +242,13 @@ static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm)return crypto_tfm_ctx_dma (&tfm->base); } +static inline bool crypto_skcipher_tested (struct crypto_skcipher *tfm) +{ +struct crypto_tfm *tfm_base =crypto_skcipher_tfm (tfm); + +return tfm_base->__crt_alg->cra_flags &CRYPTO_ALG_TESTED ; +} +static inline void *skcipher_request_ctx (struct skcipher_request *req) {return req->__ctx;
Signature · 1 line
2.43.0
Harald Freudenberger
maintainerPATCH v2 2/4 s390/pkey: Support new xflag PKEY_XFLAG_NOCLEARKEY
In reply to Harald FreudenbergerIntroduce a new xflag PKEY_XFLAG_NOCLEARKEY which when given refuses the conversion of "clear key tokens" to protected key material.
Some algorithms (PAES, PHMAC) have the need to construct "clear key tokens" to be used during selftest. But in general these algorithms should only support clear key material for testing purpose. So now the algorithm implementation can signal via xflag PKEY_XFLAG_NOCLEARKEY that a conversion of clear key material to protected key is not acceptable and thus the pkey layer (usually one of the handler modules) refuses clear key material with -EINVAL.
--- arch/s390/include/asm/pkey.h | 8 +++++++- drivers/s390/crypto/pkey_cca.c | 5 +++++ drivers/s390/crypto/pkey_ep11.c | 5 +++++ drivers/s390/crypto/pkey_pckmo.c | 12 +++++++++--- 4 files changed, 26 insertions(+), 4 deletions(-)
arch/s390/include/asm/pkey.h +7 -1
@@ -21,7 +21,8 @@* @param keylen size of the key blob in bytes * @param protkey pointer to buffer receiving the protected key * @param xflags additional execution flags (see PKEY_XFLAG_* definitions below) -* As of now the only supported flag is PKEY_XFLAG_NOMEMALLOC. +* As of now the only supported flags are PKEY_XFLAG_NOMEMALLOC +* and PKEY_XFLAG_NOCLEARKEY. * @return 0 on success, negative errno value on failure */ int pkey_key2protkey(const u8 *key, u32 keylen,
@@ -38,4 +39,9 @@ int pkey_key2protkey(const u8 *key, u32 keylen,*/ #define PKEY_XFLAG_NOMEMALLOC 0x0001 +/* +* Do not accept a clear key token as source for a protected key. +*/ +#define PKEY_XFLAG_NOCLEARKEY 0x0002 +#endif /* _KAPI_PKEY_H */
drivers/s390/crypto/pkey_cca.c +5 -0
@@ -390,6 +390,11 @@ static int cca_clr2key(const struct pkey_apqn *apqns, size_t nr_apqns,int i, len, rc;u32 xflags; +if (pflags &PKEY_XFLAG_NOCLEARKEY ) { +PKEY_DBF_ERR ("%s clear key but xflag NOCLEARKEY\n" , __func__); +return -EINVAL ; + } + xflags = pflags &PKEY_XFLAG_NOMEMALLOC ?ZCRYPT_XFLAG_NOMEMALLOC :0 ;/* check keytype, subtype, clrkeylen, keybitsize */
drivers/s390/crypto/pkey_ep11.c +5 -0
@@ -358,6 +358,11 @@ static int ep11_clr2key(const struct pkey_apqn *apqns, size_t nr_apqns,int i, len, rc;u32 xflags; +if (pflags &PKEY_XFLAG_NOCLEARKEY ) { +PKEY_DBF_ERR ("%s clear key but xflag NOCLEARKEY\n" , __func__); +return -EINVAL ; + } + xflags = pflags &PKEY_XFLAG_NOMEMALLOC ?ZCRYPT_XFLAG_NOMEMALLOC :0 ;/* check keytype, subtype, clrkeylen, keybitsize */
drivers/s390/crypto/pkey_pckmo.c +9 -3
@@ -215,7 +215,8 @@ static int pckmo_verify_protkey(const u8 *protkey, u32 protkeylen, }static int pckmo_key2protkey (const u8 *key,u32 keylen, -u8 *protkey,u32 *protkeylen,u32 *protkeytype) +u8 *protkey,u32 *protkeylen,u32 *protkeytype, +u32 xflags) {struct keytoken_header *hdr = (struct keytoken_header *)key;int rc = -EINVAL ;
@@ -266,6 +267,11 @@ static int pckmo_key2protkey(const u8 *key, u32 keylen,struct clearkeytoken *t = (struct clearkeytoken *)key;u32 keysize; +if (xflags &PKEY_XFLAG_NOCLEARKEY ) { +PKEY_DBF_ERR ("%s clear key token but xflag NOCLEARKEY\n" , + __func__); + goto out; + }if (keylen <sizeof (*t) || keylen <sizeof (*t) + t->len) goto out;
@@ -406,10 +412,10 @@ static int pkey_pckmo_key2protkey(const struct pkey_apqn *_apqns,size_t _nr_apqns, const u8 *key, u32 keylen, u8 *protkey, u32 *protkeylen, u32 *keyinfo, - u32 _xflags __always_unused) + u32 xflags) { return pckmo_key2protkey(key, keylen, -protkey ,protkeylen ,keyinfo ); +protkey ,protkeylen ,keyinfo ,xflags ); } static int pkey_pckmo_gen_key(const struct pkey_apqn *_apqns, size_t _nr_apqns,
Signature · 1 line
2.43.0
Harald Freudenberger
maintainerPATCH v2 4/4 crypto: s390/paes - Refuse clear key material by default
In reply to Harald FreudenbergerThis patch exploits the new xflag PKEY_XFLAG_NOCLEARKEY from the pkey layer. So now by default all the paes algorithms refuse the use of clear key material ("clear key tokens") in the setkey function with -EINVAL.
With a new kernel module parameter "clrkey" this behavior can be controlled. By default clrkey is 'N' but for testing purpose on module load a true value (1, 'Y') may be given to accept clear key tokens.
Note that during selftest clear keys are always used and thus the xflag PKEY_XFLAG_NOCLEARKEY is NOT set as long as the algorithm is in a larval state indicated by crypto_skcipher_tested() returning false.
--- arch/s390/crypto/paes_s390.c | 93 ++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 36 deletions(-)
arch/s390/crypto/paes_s390.c +57 -36
@@ -40,6 +40,10 @@#define PAES_256_PROTKEY_SIZE (32 + 32)/* key + verification pattern */ #define PXTS_256_PROTKEY_SIZE (32 + 32 + 32)/* k1 + k2 + verification pattern */ +static bool pkey_clrkey_allowed; +module_param_named (clrkey, pkey_clrkey_allowed, bool,0444 ); +MODULE_PARM_DESC (clrkey,"Allow clear key material (default N)" ); +static u8 *ctrblk;static DEFINE_MUTEX (ctrblk_lock);
@@ -192,10 +196,14 @@ static inline int pxts_ctx_setkey(struct s390_pxts_ctx *ctx,* This function may sleep - don't call in non-sleeping context. */ static inline int convert_key (const u8 *key,unsigned int keylen, -struct paes_protkey *pk) +struct paes_protkey *pk,bool tested) { +u32 xflags =PKEY_XFLAG_NOMEMALLOC ;int rc, i; +if (tested && !pkey_clrkey_allowed) + xflags |=PKEY_XFLAG_NOCLEARKEY ; + pk->len =sizeof (pk->protkey);/*
@@ -209,7 +217,7 @@ static inline int convert_key(const u8 *key, unsigned int keylen, } rc =pkey_key2protkey (key, keylen, pk->protkey, &pk->len, &pk->type, -PKEY_XFLAG_NOMEMALLOC ); + xflags); } out:
@@ -231,7 +239,7 @@ static inline int convert_key(const u8 *key, unsigned int keylen,* unnecessary additional conversion but never to invalid data on en- * or decrypt operations. */ -static int paes_convert_key (struct s390_paes_ctx *ctx) +static int paes_convert_key (struct s390_paes_ctx *ctx,bool tested) {struct paes_protkey pk;int rc;
@@ -240,7 +248,7 @@ static int paes_convert_key(struct s390_paes_ctx *ctx) ctx->pk_state =PK_STATE_CONVERT_IN_PROGRESS ;spin_unlock_bh (&ctx->pk_lock); - rc =convert_key (ctx->keybuf, ctx->keylen, &pk); + rc =convert_key (ctx->keybuf, ctx->keylen, &pk, tested);/* update context */ spin_lock_bh (&ctx->pk_lock);
@@ -263,7 +271,7 @@ static int paes_convert_key(struct s390_paes_ctx *ctx)* pk_type, pk_len and the protected key in the tfm context. * See also comments on function paes_convert_key. */ -static int pxts_convert_key (struct s390_pxts_ctx *ctx) +static int pxts_convert_key (struct s390_pxts_ctx *ctx,bool tested) {struct paes_protkey pk0, pk1;size_t split_keylen;
@@ -273,7 +281,7 @@ static int pxts_convert_key(struct s390_pxts_ctx *ctx) ctx->pk_state =PK_STATE_CONVERT_IN_PROGRESS ;spin_unlock_bh (&ctx->pk_lock); - rc =convert_key (ctx->keybuf, ctx->keylen, &pk0); + rc =convert_key (ctx->keybuf, ctx->keylen, &pk0, tested);if (rc) goto out;
@@ -287,7 +295,7 @@ static int pxts_convert_key(struct s390_pxts_ctx *ctx) } split_keylen = ctx->keylen /2 ; rc =convert_key (ctx->keybuf + split_keylen, - split_keylen, &pk1); + split_keylen, &pk1, tested);if (rc) goto out;if (pk0.type != pk1.type) {
@@ -343,6 +351,7 @@ static int ecb_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, unsigned int key_len) {struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm); +bool tested =crypto_skcipher_tested (tfm);long fc;int rc;
@@ -352,7 +361,7 @@ static int ecb_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, goto out;/* convert key into protected key */ - rc =paes_convert_key (ctx); + rc =paes_convert_key (ctx, tested);if (rc) goto out;
@@ -382,7 +391,7 @@ static int ecb_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key,static int ecb_paes_do_crypt (struct s390_paes_ctx *ctx,struct s390_pecb_req_ctx *req_ctx, -bool maysleep) +bool tested,bool maysleep) {struct ecb_param *param = &req_ctx->param;struct skcipher_walk *walk = &req_ctx->walk;
@@ -430,7 +439,7 @@ static int ecb_paes_do_crypt(struct s390_paes_ctx *ctx, rc = -EKEYEXPIRED ; goto out; } - rc =paes_convert_key (ctx); + rc =paes_convert_key (ctx, tested);if (rc) goto out;spin_lock_bh (&ctx->pk_lock);
@@ -450,6 +459,7 @@ static int ecb_paes_crypt(struct skcipher_request *req, unsigned long modifier)struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/*
@@ -468,7 +478,7 @@ static int ecb_paes_crypt(struct skcipher_request *req, unsigned long modifier)/* Try synchronous operation if no active engine usage */ if (!atomic_read (&ctx->via_engine_ctr)) { - rc =ecb_paes_do_crypt (ctx, req_ctx, false); + rc =ecb_paes_do_crypt (ctx, req_ctx, tested, false);if (rc ==0 ) goto out; }
@@ -531,11 +541,12 @@ static int ecb_paes_do_one_request(struct crypto_engine *engine, void *areq)struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/* walk has already been prepared */ - rc =ecb_paes_do_crypt (ctx, req_ctx, true); + rc =ecb_paes_do_crypt (ctx, req_ctx, tested, true);if (rc == -EKEYEXPIRED ) {/* * Protected key expired, conversion is in process.
@@ -602,6 +613,7 @@ static int cbc_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, unsigned int key_len) {struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm); +bool tested =crypto_skcipher_tested (tfm);long fc;int rc;
@@ -611,7 +623,7 @@ static int cbc_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, goto out;/* convert raw key into protected key */ - rc =paes_convert_key (ctx); + rc =paes_convert_key (ctx, tested);if (rc) goto out;
@@ -641,7 +653,7 @@ static int cbc_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key,static int cbc_paes_do_crypt (struct s390_paes_ctx *ctx,struct s390_pcbc_req_ctx *req_ctx, -bool maysleep) +bool tested,bool maysleep) {struct cbc_param *param = &req_ctx->param;struct skcipher_walk *walk = &req_ctx->walk;
@@ -693,7 +705,7 @@ static int cbc_paes_do_crypt(struct s390_paes_ctx *ctx, rc = -EKEYEXPIRED ; goto out; } - rc =paes_convert_key (ctx); + rc =paes_convert_key (ctx, tested);if (rc) goto out;spin_lock_bh (&ctx->pk_lock);
@@ -713,6 +725,7 @@ static int cbc_paes_crypt(struct skcipher_request *req, unsigned long modifier)struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/*
@@ -731,7 +744,7 @@ static int cbc_paes_crypt(struct skcipher_request *req, unsigned long modifier)/* Try synchronous operation if no active engine usage */ if (!atomic_read (&ctx->via_engine_ctr)) { - rc =cbc_paes_do_crypt (ctx, req_ctx, false); + rc =cbc_paes_do_crypt (ctx, req_ctx, tested, false);if (rc ==0 ) goto out; }
@@ -794,11 +807,12 @@ static int cbc_paes_do_one_request(struct crypto_engine *engine, void *areq)struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/* walk has already been prepared */ - rc =cbc_paes_do_crypt (ctx, req_ctx, true); + rc =cbc_paes_do_crypt (ctx, req_ctx, tested, true);if (rc == -EKEYEXPIRED ) {/* * Protected key expired, conversion is in process.
@@ -865,6 +879,7 @@ static int ctr_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, unsigned int key_len) {struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm); +bool tested =crypto_skcipher_tested (tfm);long fc;int rc;
@@ -874,7 +889,7 @@ static int ctr_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, goto out;/* convert raw key into protected key */ - rc =paes_convert_key (ctx); + rc =paes_convert_key (ctx, tested);if (rc) goto out;
@@ -919,7 +934,7 @@ static inline unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytesstatic int ctr_paes_do_crypt (struct s390_paes_ctx *ctx,struct s390_pctr_req_ctx *req_ctx, -bool maysleep) +bool tested,bool maysleep) {struct ctr_param *param = &req_ctx->param;struct skcipher_walk *walk = &req_ctx->walk;
@@ -979,7 +994,7 @@ static int ctr_paes_do_crypt(struct s390_paes_ctx *ctx, rc = -EKEYEXPIRED ; goto out; } - rc =paes_convert_key (ctx); + rc =paes_convert_key (ctx, tested);if (rc) {if (locked)mutex_unlock (&ctrblk_lock);
@@ -1006,7 +1021,7 @@ static int ctr_paes_do_crypt(struct s390_paes_ctx *ctx, rc = -EKEYEXPIRED ; goto out; } - rc =paes_convert_key (ctx); + rc =paes_convert_key (ctx, tested);if (rc) goto out;spin_lock_bh (&ctx->pk_lock);
@@ -1029,6 +1044,7 @@ static int ctr_paes_crypt(struct skcipher_request *req)struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/*
@@ -1046,7 +1062,7 @@ static int ctr_paes_crypt(struct skcipher_request *req)/* Try synchronous operation if no active engine usage */ if (!atomic_read (&ctx->via_engine_ctr)) { - rc =ctr_paes_do_crypt (ctx, req_ctx, false); + rc =ctr_paes_do_crypt (ctx, req_ctx, tested, false);if (rc ==0 ) goto out; }
@@ -1099,11 +1115,12 @@ static int ctr_paes_do_one_request(struct crypto_engine *engine, void *areq)struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_paes_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/* walk has already been prepared */ - rc =ctr_paes_do_crypt (ctx, req_ctx, true); + rc =ctr_paes_do_crypt (ctx, req_ctx, tested, true);if (rc == -EKEYEXPIRED ) {/* * Protected key expired, conversion is in process.
@@ -1190,6 +1207,7 @@ static int xts_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, unsigned int in_keylen) {struct s390_pxts_ctx *ctx =crypto_skcipher_ctx (tfm); +bool tested =crypto_skcipher_tested (tfm);u8 ckey[2 *AES_MAX_KEY_SIZE ];unsigned int ckey_len;long fc;
@@ -1205,7 +1223,7 @@ static int xts_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, goto out;/* convert raw key(s) into protected key(s) */ - rc =pxts_convert_key (ctx); + rc =pxts_convert_key (ctx, tested);if (rc) goto out;
@@ -1255,7 +1273,7 @@ static int xts_paes_setkey(struct crypto_skcipher *tfm, const u8 *in_key,static int xts_paes_do_crypt_fullkey (struct s390_pxts_ctx *ctx,struct s390_pxts_req_ctx *req_ctx, -bool maysleep) +bool tested,bool maysleep) {struct xts_full_km_param *param = &req_ctx->param.full_km_param;struct skcipher_walk *walk = &req_ctx->walk;
@@ -1299,7 +1317,7 @@ static int xts_paes_do_crypt_fullkey(struct s390_pxts_ctx *ctx, rc = -EKEYEXPIRED ; goto out; } - rc =pxts_convert_key (ctx); + rc =pxts_convert_key (ctx, tested);if (rc) goto out;spin_lock_bh (&ctx->pk_lock);
@@ -1318,7 +1336,8 @@ static inline int __xts_2keys_prep_param(struct s390_pxts_ctx *ctx,struct xts_km_param *param, struct skcipher_walk *walk, unsigned int keylen, - unsigned int offset, bool maysleep) + unsigned int offset, + bool tested, bool maysleep) { struct xts_pcc_param pcc_param;unsigned long cc =1 ;
@@ -1337,7 +1356,7 @@ static inline int __xts_2keys_prep_param(struct s390_pxts_ctx *ctx, rc = -EKEYEXPIRED ;break ; } - rc =pxts_convert_key (ctx); + rc =pxts_convert_key (ctx, tested);if (rc)break ;continue ;
@@ -1351,7 +1370,7 @@ static inline int __xts_2keys_prep_param(struct s390_pxts_ctx *ctx,static int xts_paes_do_crypt_2keys (struct s390_pxts_ctx *ctx,struct s390_pxts_req_ctx *req_ctx, -bool maysleep) +bool tested,bool maysleep) {struct xts_km_param *param = &req_ctx->param.km_param;struct skcipher_walk *walk = &req_ctx->walk;
@@ -1369,7 +1388,7 @@ static int xts_paes_do_crypt_2keys(struct s390_pxts_ctx *ctx,if (!req_ctx->param_init_done) { rc =__xts_2keys_prep_param (ctx, param, walk, - keylen, offset, maysleep); + keylen, offset, tested, maysleep);if (rc) goto out; req_ctx->param_init_done = true;
@@ -1392,7 +1411,7 @@ static int xts_paes_do_crypt_2keys(struct s390_pxts_ctx *ctx, rc = -EKEYEXPIRED ; goto out; } - rc =pxts_convert_key (ctx); + rc =pxts_convert_key (ctx, tested);if (rc) goto out;spin_lock_bh (&ctx->pk_lock);
@@ -1408,7 +1427,7 @@ static int xts_paes_do_crypt_2keys(struct s390_pxts_ctx *ctx,static int xts_paes_do_crypt (struct s390_pxts_ctx *ctx,struct s390_pxts_req_ctx *req_ctx, -bool maysleep) +bool tested,bool maysleep) {int pk_state, rc =0 ;
@@ -1436,11 +1455,11 @@ static int xts_paes_do_crypt(struct s390_pxts_ctx *ctx,switch (ctx->fc) {case CPACF_KM_PXTS_128 :case CPACF_KM_PXTS_256 : - rc =xts_paes_do_crypt_2keys (ctx, req_ctx, maysleep); + rc =xts_paes_do_crypt_2keys (ctx, req_ctx, tested, maysleep);break ;case CPACF_KM_PXTS_128_FULL :case CPACF_KM_PXTS_256_FULL : - rc =xts_paes_do_crypt_fullkey (ctx, req_ctx, maysleep); + rc =xts_paes_do_crypt_fullkey (ctx, req_ctx, tested, maysleep);break ;default : rc = -EINVAL ;
@@ -1457,6 +1476,7 @@ static inline int xts_paes_crypt(struct skcipher_request *req, unsigned long modstruct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_pxts_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/*
@@ -1475,7 +1495,7 @@ static inline int xts_paes_crypt(struct skcipher_request *req, unsigned long mod/* Try synchronous operation if no active engine usage */ if (!atomic_read (&ctx->via_engine_ctr)) { - rc =xts_paes_do_crypt (ctx, req_ctx, false); + rc =xts_paes_do_crypt (ctx, req_ctx, tested, false);if (rc ==0 ) goto out; }
@@ -1538,11 +1558,12 @@ static int xts_paes_do_one_request(struct crypto_engine *engine, void *areq)struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req);struct s390_pxts_ctx *ctx =crypto_skcipher_ctx (tfm);struct skcipher_walk *walk = &req_ctx->walk; +bool tested =crypto_skcipher_tested (tfm);int rc;/* walk has already been prepared */ - rc =xts_paes_do_crypt (ctx, req_ctx, true); + rc =xts_paes_do_crypt (ctx, req_ctx, tested, true);if (rc == -EKEYEXPIRED ) {/* * Protected key expired, conversion is in process.
Signature · 1 line
2.43.0
Herbert Xu
maintainerQuoted 39 lines · Harald Freudenberger
The modivation of these patches is to disable clear key usage of the protected key implementations paes and phmac by default.
With a new kernel module parameter "clrkey" this behavior can be controlled. By default clrkey is 'N' but for testing purpose on module load a true value (1, 'Y') may be given to accept clear key tokens.
Note that during selftest clear keys are always used and thus as long as the algorithm is in larval state indicated by
crypto_skcipher_tested()clear keys need to be accepted. However, in this state there is no way to establish an instance of the tfm other than for selftest reasons.Changelog:
v0: Initial version. Request for internal feedback and review
Please note I assume that patch #1 goes via s390, whereas patch #2, #3 and #4 may go via Herbert Xu's Linux Kernel Crypto.
v1: Sequence changed and feedback from Ingo, Holger and Heiko
integrated.
v2: Integrated the feedback from Holger and Ingo. First version
going out to the linux kernel crypto mailing listHarald Freudenberger (4):
crypto: skcipher - Add new helper functioncrypto_skcipher_testeds390/pkey: Support new xflagPKEY_XFLAG_NOCLEARKEY
crypto: s390/phmac - Refuse clear key material by default
crypto: s390/paes - Refuse clear key material by defaultarch/s390/crypto/paes_s390.c | 93 ++++++++++++++++++------------ arch/s390/crypto/phmac_s390.c | 29 +++++++--- arch/s390/include/asm/pkey.h | 8 ++- drivers/s390/crypto/pkey_cca.c | 5 ++ drivers/s390/crypto/pkey_ep11.c | 5 ++ drivers/s390/crypto/pkey_pckmo.c | 12 +++- include/crypto/internal/skcipher.h | 7 +++ 7 files changed, 110 insertions(+), 49 deletions(-)
Quoted 3 lines
base-commit:
9448598b22c50c8a5bb77a9103e2d49f134c9578--
2.43.0
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