Skip to content

Commit 452770a

Browse files
hfreudeherbertx
authored andcommitted
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. Signed-off-by: Harald Freudenberger <freude@linux.ibm.com> Reviewed-by: Holger Dengler <dengler@linux.ibm.com> Reviewed-by: Ingo Franzki <ifranzki@linux.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 2dfca61 commit 452770a

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

arch/s390/crypto/phmac_s390.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
static struct crypto_engine *phmac_crypto_engine;
2424
#define MAX_QLEN 10
2525

26+
static bool pkey_clrkey_allowed;
27+
module_param_named(clrkey, pkey_clrkey_allowed, bool, 0444);
28+
MODULE_PARM_DESC(clrkey, "Allow clear key material (default N)");
29+
2630
/*
2731
* A simple hash walk helper
2832
*/
@@ -311,10 +315,14 @@ static inline int phmac_tfm_ctx_setkey(struct phmac_tfm_ctx *tfm_ctx,
311315
* This function may sleep - don't call in non-sleeping context.
312316
*/
313317
static inline int convert_key(const u8 *key, unsigned int keylen,
314-
struct phmac_protkey *pk)
318+
struct phmac_protkey *pk, bool tested)
315319
{
320+
u32 xflags = PKEY_XFLAG_NOMEMALLOC;
316321
int rc, i;
317322

323+
if (tested && !pkey_clrkey_allowed)
324+
xflags |= PKEY_XFLAG_NOCLEARKEY;
325+
318326
pk->len = sizeof(pk->protkey);
319327

320328
/*
@@ -328,7 +336,7 @@ static inline int convert_key(const u8 *key, unsigned int keylen,
328336
}
329337
rc = pkey_key2protkey(key, keylen,
330338
pk->protkey, &pk->len, &pk->type,
331-
PKEY_XFLAG_NOMEMALLOC);
339+
xflags);
332340
}
333341

334342
out:
@@ -350,7 +358,7 @@ static inline int convert_key(const u8 *key, unsigned int keylen,
350358
* unnecessary additional conversion but never to invalid data on the
351359
* hash operation.
352360
*/
353-
static int phmac_convert_key(struct phmac_tfm_ctx *tfm_ctx)
361+
static int phmac_convert_key(struct phmac_tfm_ctx *tfm_ctx, bool tested)
354362
{
355363
struct phmac_protkey pk;
356364
int rc;
@@ -359,7 +367,7 @@ static int phmac_convert_key(struct phmac_tfm_ctx *tfm_ctx)
359367
tfm_ctx->pk_state = PK_STATE_CONVERT_IN_PROGRESS;
360368
spin_unlock_bh(&tfm_ctx->pk_lock);
361369

362-
rc = convert_key(tfm_ctx->keybuf, tfm_ctx->keylen, &pk);
370+
rc = convert_key(tfm_ctx->keybuf, tfm_ctx->keylen, &pk, tested);
363371

364372
/* update context */
365373
spin_lock_bh(&tfm_ctx->pk_lock);
@@ -404,6 +412,7 @@ static int phmac_kmac_update(struct ahash_request *req, bool maysleep)
404412
struct kmac_sha2_ctx *ctx = &req_ctx->kmac_ctx;
405413
struct hash_walk_helper *hwh = &req_ctx->hwh;
406414
unsigned int bs = crypto_ahash_blocksize(tfm);
415+
bool tested = crypto_ahash_tested(tfm);
407416
unsigned int offset, k, n;
408417
int rc = 0;
409418

@@ -444,7 +453,7 @@ static int phmac_kmac_update(struct ahash_request *req, bool maysleep)
444453
rc = -EKEYEXPIRED;
445454
goto out;
446455
}
447-
rc = phmac_convert_key(tfm_ctx);
456+
rc = phmac_convert_key(tfm_ctx, tested);
448457
if (rc)
449458
goto out;
450459
spin_lock_bh(&tfm_ctx->pk_lock);
@@ -480,7 +489,7 @@ static int phmac_kmac_update(struct ahash_request *req, bool maysleep)
480489
rc = -EKEYEXPIRED;
481490
goto out;
482491
}
483-
rc = phmac_convert_key(tfm_ctx);
492+
rc = phmac_convert_key(tfm_ctx, tested);
484493
if (rc)
485494
goto out;
486495
spin_lock_bh(&tfm_ctx->pk_lock);
@@ -517,6 +526,7 @@ static int phmac_kmac_final(struct ahash_request *req, bool maysleep)
517526
struct kmac_sha2_ctx *ctx = &req_ctx->kmac_ctx;
518527
unsigned int ds = crypto_ahash_digestsize(tfm);
519528
unsigned int bs = crypto_ahash_blocksize(tfm);
529+
bool tested = crypto_ahash_tested(tfm);
520530
unsigned int k, n;
521531
int rc = 0;
522532

@@ -537,7 +547,7 @@ static int phmac_kmac_final(struct ahash_request *req, bool maysleep)
537547
rc = -EKEYEXPIRED;
538548
goto out;
539549
}
540-
rc = phmac_convert_key(tfm_ctx);
550+
rc = phmac_convert_key(tfm_ctx, tested);
541551
if (rc)
542552
goto out;
543553
spin_lock_bh(&tfm_ctx->pk_lock);
@@ -741,11 +751,12 @@ static int phmac_setkey(struct crypto_ahash *tfm,
741751
struct phmac_tfm_ctx *tfm_ctx = crypto_ahash_ctx(tfm);
742752
unsigned int ds = crypto_ahash_digestsize(tfm);
743753
unsigned int bs = crypto_ahash_blocksize(tfm);
754+
bool tested = crypto_ahash_tested(tfm);
744755
unsigned int tmpkeylen;
745756
u8 *tmpkey = NULL;
746757
int rc = 0;
747758

748-
if (!crypto_ahash_tested(tfm)) {
759+
if (!tested) {
749760
/*
750761
* selftest running: key is a raw hmac clear key and needs
751762
* to get embedded into a 'clear key token' in order to have
@@ -770,7 +781,7 @@ static int phmac_setkey(struct crypto_ahash *tfm,
770781
goto out;
771782

772783
/* convert raw key into protected key */
773-
rc = phmac_convert_key(tfm_ctx);
784+
rc = phmac_convert_key(tfm_ctx, tested);
774785
if (rc)
775786
goto out;
776787

0 commit comments

Comments
 (0)