New s390 specific protected key hmac
Subsystems (3)
Harald Freudenberger
maintainerv3 cover New s390 specific protected key hmac
Add support for protected key hmac ("phmac") for s390 arch.
With the latest machine generation there is now support for protected key (that is a key wrapped by a master key stored in firmware) hmac for sha2 (sha224, sha256, sha384 and sha512) for the s390 specific CPACF instruction kmac.
This patch adds support via 4 new shashes registered as phmac(sha224), phmac(sha256), phmac(sha384) and phmac(sha512).
Please note that as of now, there is no selftest enabled for these shashes, but the implementation has been tested with testcases via AF_ALG interface. However, there may come an improvement soon to use the available clear key hmac selftests.
Changelog:
v1: Initial version
v2: Increase HASH_MAX_DESCSIZE generic (not just for arch s390).
Fix one finding to use kmemdup instead of kmalloc/memcpy from test robot. Remove unneeded cpacf subfunctions checks. Simplify clone_tfm() function.
Rebased to s390/features.
v3: Feedback from Herbert: Use GFP_ATOMIC in setkey function.
Feedback from Holger: rework tfm clone function, move convert key invocation from setkey to init function. Rebased to updated s390/features from 11/7/2024. Ready for integration if there are no complains on v3.
Holger Dengler (3):
crypto: api - Adjust HASH_MAX_DESCSIZE for s390-phmac context s390/crypto: Add protected key hmac subfunctions for KMAC s390/crypto: New s390 specific shash phmac
arch/s390/configs/debug_defconfig | 1 + arch/s390/configs/defconfig | 1 + arch/s390/crypto/Makefile | 1 + arch/s390/crypto/phmac_s390.c | 473 ++++++++++++++++++++++++++++++ arch/s390/include/asm/cpacf.h | 4 + drivers/crypto/Kconfig | 12 + include/crypto/hash.h | 5 +- 7 files changed, 494 insertions(+), 3 deletions(-) create mode 100644 arch/s390/crypto/phmac_s390.c
base-commit: de786f0f83824eaa12aa92603be8655ec5d1a5bf --
2.43.0
Harald Freudenberger
maintainerPATCH v3 1/3 crypto: api - Adjust HASH_MAX_DESCSIZE for s390-phmac context
From: Holger Dengler <[email protected]>
The phmac context exceeds the generic "worst case". Change this define to cover the s390 specific 'phmac(sha512)' which is a "protected" key hmac-sha2 implementation.
--- include/crypto/hash.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
include/crypto/hash.h +2 -3
@@ -159,10 +159,9 @@ struct shash_desc {#define HASH_MAX_DIGESTSIZE 64/* -* Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc' -* containing a 'struct sha3_state'. +* The max case is currently the s390 specific shash 'phmac(sha512)'. */ -#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360) +#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 376)#define SHASH_DESC_ON_STACK (shash, ctx) \ char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
Signature · 1 line
2.43.0
Harald Freudenberger
maintainerPATCH v3 2/3 s390/crypto: Add protected key hmac subfunctions for KMAC
In reply to Harald FreudenbergerFrom: Holger Dengler <[email protected]>
The CPACF KMAC instruction supports new subfunctions for protected key hmac. Add defines for these 4 new subfuctions.
--- arch/s390/include/asm/cpacf.h | 4 ++++ 1 file changed, 4 insertions(+)
arch/s390/include/asm/cpacf.h +4 -0
@@ -129,6 +129,10 @@#define CPACF_KMAC_HMAC_SHA_256 0x71#define CPACF_KMAC_HMAC_SHA_384 0x72#define CPACF_KMAC_HMAC_SHA_512 0x73 +#define CPACF_KMAC_PHMAC_SHA_224 0x78 +#define CPACF_KMAC_PHMAC_SHA_256 0x79 +#define CPACF_KMAC_PHMAC_SHA_384 0x7a +#define CPACF_KMAC_PHMAC_SHA_512 0x7b/* * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
Signature · 1 line
2.43.0
Harald Freudenberger
maintainerPATCH v3 3/3 s390/crypto: New s390 specific shash phmac
In reply to Harald FreudenbergerFrom: Holger Dengler <[email protected]>
Add support for protected key hmac ("phmac") for s390 arch.
With the latest machine generation there is now support for protected key (that is a key wrapped by a master key stored in firmware) hmac for sha2 (sha224, sha256, sha384 and sha512) for the s390 specific CPACF instruction kmac.
This patch adds support via 4 new shashes registered as phmac(sha224), phmac(sha256), phmac(sha384) and phmac(sha512).
Please note that as of now, there is no selftest enabled for these shashes, but the implementation has been tested with testcases via AF_ALG interface.
--- arch/s390/configs/debug_defconfig | 1 + arch/s390/configs/defconfig | 1 + arch/s390/crypto/Makefile | 1 + arch/s390/crypto/phmac_s390.c | 473 ++++++++++++++++++++++++++++++ drivers/crypto/Kconfig | 12 + 5 files changed, 488 insertions(+) create mode 100644 arch/s390/crypto/phmac_s390.c
arch/s390/configs/debug_defconfig +1 -0
@@ -804,6 +804,7 @@ CONFIG_PKEY_EP11=m CONFIG_PKEY_PCKMO=m CONFIG_PKEY_UV=m CONFIG_CRYPTO_PAES_S390=m +CONFIG_CRYPTO_PHMAC_S390=m CONFIG_CRYPTO_DEV_VIRTIO=m CONFIG_SYSTEM_BLACKLIST_KEYRING=y CONFIG_CORDIC=m
arch/s390/configs/defconfig +1 -0
@@ -790,6 +790,7 @@ CONFIG_PKEY_EP11=m CONFIG_PKEY_PCKMO=m CONFIG_PKEY_UV=m CONFIG_CRYPTO_PAES_S390=m +CONFIG_CRYPTO_PHMAC_S390=m CONFIG_CRYPTO_DEV_VIRTIO=m CONFIG_SYSTEM_BLACKLIST_KEYRING=y CONFIG_CORDIC=m
arch/s390/crypto/Makefile +1 -0
@@ -16,6 +16,7 @@ obj-$(CONFIG_S390_PRNG) += prng.o obj-$(CONFIG_CRYPTO_GHASH_S390) += ghash_s390.o obj-$(CONFIG_CRYPTO_CRC32_S390) += crc32-vx_s390.o obj-$(CONFIG_CRYPTO_HMAC_S390) += hmac_s390.o +obj-$(CONFIG_CRYPTO_PHMAC_S390) += phmac_s390.o obj-y += arch_random.o crc32-vx_s390-y := crc32-vx.o crc32le-vx.o crc32be-vx.o
arch/s390/crypto/phmac_s390.c new file +473 -0
473 lines not on this page · raw message
drivers/crypto/Kconfig +12 -0
@@ -187,6 +187,18 @@ config CRYPTO_PAES_S390 Select this option if you want to use the paes cipher for example to use protected key encrypted devices. +config CRYPTO_PHMAC_S390 + tristate "PHMAC cipher algorithms" + depends on S390 + depends on PKEY + select CRYPTO_HASH + help + This is the s390 hardware accelerated implementation of the + protected key HMAC support for SHA224, SHA256, SHA384 and SHA512. + + Select this option if you want to use the phmac digests + for example to use dm-integrity with secure/protected keys. + config S390_PRNG tristate "Pseudo random number generator device driver" depends on S390
Signature · 1 line
2.43.0
Re: [PATCH v3 3/3] s390/crypto: New s390 specific shash phmac
Quoted 145 lines · Harald Freudenberger
From: Holger Dengler <[email protected]>
Add support for protected key hmac ("phmac") for s390 arch.
With the latest machine generation there is now support for protected key (that is a key wrapped by a master key stored in firmware) hmac for sha2 (sha224, sha256, sha384 and sha512) for the s390 specific CPACF instruction kmac.
This patch adds support via 4 new shashes registered as phmac(sha224), phmac(sha256), phmac(sha384) and phmac(sha512).
Please note that as of now, there is no selftest enabled for these shashes, but the implementation has been tested with testcases via
AF_ALGinterface.Signed-off-by Holger Dengler <[email protected]>Signed-off-by Harald Freudenberger <[email protected]>--- arch/s390/configs/debug_defconfig | 1 + arch/s390/configs/defconfig | 1 + arch/s390/crypto/Makefile | 1 + arch/s390/crypto/phmac_s390.c | 473 ++++++++++++++++++++++++++++++ drivers/crypto/Kconfig | 12 + 5 files changed, 488 insertions(+) create mode 100644 arch/s390/crypto/phmac_s390.carch/s390/configs/debug_defconfig +1
-0@@ -804,6 +804,7 @@ CONFIG_PKEY_EP11=m CONFIG_PKEY_PCKMO=m CONFIG_PKEY_UV=m CONFIG_CRYPTO_PAES_S390=m +CONFIG_CRYPTO_PHMAC_S390=m CONFIG_CRYPTO_DEV_VIRTIO=m CONFIG_SYSTEM_BLACKLIST_KEYRING=y CONFIG_CORDIC=march/s390/configs/defconfig +1
-0@@ -790,6 +790,7 @@ CONFIG_PKEY_EP11=m CONFIG_PKEY_PCKMO=m CONFIG_PKEY_UV=m CONFIG_CRYPTO_PAES_S390=m +CONFIG_CRYPTO_PHMAC_S390=m CONFIG_CRYPTO_DEV_VIRTIO=m CONFIG_SYSTEM_BLACKLIST_KEYRING=y CONFIG_CORDIC=march/s390/crypto/Makefile +1
-0@@ -16,6 +16,7 @@ obj-$(CONFIG_S390_PRNG) += prng.o obj-$(CONFIG_CRYPTO_GHASH_S390) += ghash_s390.o obj-$(CONFIG_CRYPTO_CRC32_S390) += crc32-vx_s390.o obj-$(CONFIG_CRYPTO_HMAC_S390) += hmac_s390.o +obj-$(CONFIG_CRYPTO_PHMAC_S390) += phmac_s390.o obj-y += arch_random.o crc32-vx_s390-y := crc32-vx.o crc32le-vx.o crc32be-vx.oarch/s390/crypto/phmac_s390.c new file +76
-0@@ -0,0 +1,473 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright IBM Corp. 2024 +* +* s390 specific HMAC support for protected keys. +*/ + +#define KMSG_COMPONENT "phmac_s390" +#define pr_fmt (fmt) KMSG_COMPONENT ": " fmt + +#include <asm/cpacf.h> +#include <asm/pkey.h> +#include <crypto/sha2.h> +#include <crypto/internal/hash.h> +#include <linux/cpufeature.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/spinlock.h> + +/* +* KMAC param block layout for sha2 function codes: +* The layout of the param block for the KMAC instruction depends on the +* blocksize of the used hashing sha2-algorithm function codes. The param block +* contains the hash chaining value (cv), the input message bit-length (imbl) +* and the hmac-secret (key). To prevent code duplication, the sizes of all +* these are calculated based on the blocksize. +* +* param-block: +* +-------+ +* | cv | +* +-------+ +* | imbl | +* +-------+ +* | key | +* +-------+ +* +* sizes: +* part | sh2-alg | calculation | size | type +* -----+---------+-------------+------+-------- +* cv | 224/256 | blocksize/2 | 32 | u64[8] +* | 384/512 | | 64 | u128[8] +* imbl | 224/256 | blocksize/8 | 8 | u64 +* | 384/512 | | 16 | u128 +* key | 224/256 | blocksize | 96 | u8[96] +* | 384/512 | | 160 | u8[160] +*/ + +#define MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define MAX_IMBL_SIZE sizeof(u128) +#define MAX_BLOCK_SIZE SHA512_BLOCK_SIZE + +#define SHA2_CV_SIZE (bs) ((bs) >> 1) +#define SHA2_IMBL_SIZE (bs) ((bs) >> 3) + +#define SHA2_IMBL_OFFSET (bs) (SHA2_CV_SIZE(bs)) +#define SHA2_KEY_OFFSET (bs) (SHA2_CV_SIZE(bs) + SHA2_IMBL_SIZE(bs)) + +#define PHMAC_SHA256_KEY_SIZE (SHA256_BLOCK_SIZE + 32) +#define PHMAC_SHA512_KEY_SIZE (SHA512_BLOCK_SIZE + 32) +#define PHMAC_MAX_KEY_SIZE PHMAC_SHA512_KEY_SIZE + +struct phmac_protkey { +u32 type; +u32 len; +u8 protkey[PHMAC_MAX_KEY_SIZE ]; +}; + +struct s390_phmac_ctx { +u8 *key; +unsigned int keylen; + +struct phmac_protkey pk; +/* spinlock to atomic update pk */ +spinlock_t pk_lock; +}; +
....
Quoted 18 lines
arch/s390/crypto/phmac_s390.c new file +18
-0+ +static inline int s390_phmac_sha2_setkey (struct crypto_shash *tfm, +const u8 *key,unsigned int keylen) +{ +struct s390_phmac_ctx *tfm_ctx =crypto_shash_ctx (tfm); + +if (tfm_ctx->keylen) { +kfree_sensitive (tfm_ctx->key); + tfm_ctx->key =NULL ; + tfm_ctx->keylen =0 ; + } + + tfm_ctx->key =kmemdup (key, keylen,GFP_ATOMIC ); +if (!tfm_ctx->key) +return -ENOMEM ; + tfm_ctx->keylen = keylen; + +return 0 ;
arch/s390/crypto/phmac_s390.c line 198
Quoted 10 lines
arch/s390/crypto/phmac_s390.c new file +10
-0+} + +static int s390_phmac_sha2_init (struct shash_desc *desc) +{ +struct s390_phmac_ctx *tfm_ctx =crypto_shash_ctx (desc->tfm); +struct s390_kmac_sha2_ctx *ctx =shash_desc_ctx (desc); +unsigned int bs =crypto_shash_blocksize (desc->tfm); +int rc; + + rc =phmac_convert_key (desc->tfm);
There is no need to perform the (possibly time consuming) key conversation at every init. You only need to convert the key if a new key was set (via setkey function) before. Once converted, it can be reused by multiple init calls, if no other setkey is done in between.
One possibility to achieve this is to add a flag field into struct s390_phmac_ctx (i.e. new_key_set).
In the setkey function set new_key_set to 1 once te key has been updated in the context.
In the init function, if new_key_set is != 0, then convert the key and set new_key_set to 0.
Any subsequent init calls (with no setkey in-between) will see new_key_set == 0 and thus will not convert the key again, but reuse the already converted key.
If the key hasn't changed it is simply unnecessary to perform the (possibly time consuming) key conversation again and again.
This improves performance for use cases where a tfm is allocated, a key is set once, and then multiple sequences of init/update/final are performed to calculate the HMACs of different pieces of data but with the same key. This is for example what dm-integrity does, it does init/update/final for every sector, but the key is set only once at the very beginning.
Quoted 34 lines
arch/s390/crypto/phmac_s390.c new file +33
-0+if (rc) + goto out; + +spin_lock_bh (&tfm_ctx->pk_lock); +memcpy (ctx->param +SHA2_KEY_OFFSET (bs), + tfm_ctx->pk.protkey, tfm_ctx->pk.len); +spin_unlock_bh (&tfm_ctx->pk_lock); + + ctx->buflen =0 ; + ctx->gr0.reg =0 ; + +switch (crypto_shash_digestsize (desc->tfm)) { +case SHA224_DIGEST_SIZE : + ctx->gr0.fc =CPACF_KMAC_PHMAC_SHA_224 ; +break ; +case SHA256_DIGEST_SIZE : + ctx->gr0.fc =CPACF_KMAC_PHMAC_SHA_256 ; +break ; +case SHA384_DIGEST_SIZE : + ctx->gr0.fc =CPACF_KMAC_PHMAC_SHA_384 ; +break ; +case SHA512_DIGEST_SIZE : + ctx->gr0.fc =CPACF_KMAC_PHMAC_SHA_512 ; +break ; +default : + rc = -EINVAL ; + } + +out: +pr_debug ("rc=%d\n" , rc); +return rc; +} +
Signature · 10 lines
Ingo Franzki eMail: [email protected] Tel: ++49 (0)7031-16-4648 Linux on IBM Z Development, Schoenaicher Str. 220, 71032 Boeblingen, Germany IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Gregor Pillen Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294 IBM DATA Privacy Statement: https://www.ibm.com/privacy/us/en/
Herbert Xu
maintainerRe: [PATCH v3 3/3] s390/crypto: New s390 specific shash phmac
Quoted 2 lines · Ingo Franzki
There is no need to perform the (possibly time consuming) key conversation at every init.
The key is converted not just at init, but multiple times during each update operation. Remember the API is multi-threaded, so at any time there could be multiple tfm's, each with a different key being operated on in parallel.
If the hardware is such that you can only have one converted key, then I'm afraid you're stuck with having to do the conversion before each use.
However, since we are converting the key during each update, why is it even stored in the tfm_ctx?
Cheers,
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
Herbert Xu
maintainerRe: [PATCH v3 3/3] s390/crypto: New s390 specific shash phmac
In reply to Harald FreudenbergerQuoted 6 lines · Harald Freudenberger
arch/s390/crypto/phmac_s390.c new file +5
-0+static int s390_phmac_sha2_init (struct shash_desc *desc) +{ +struct s390_phmac_ctx *tfm_ctx =crypto_shash_ctx (desc->tfm); +struct s390_kmac_sha2_ctx *ctx =shash_desc_ctx (desc); +unsigned int bs =crypto_shash_blocksize (desc->tfm);
arch/s390/crypto/phmac_s390.c line 205
Quoted 10 lines
arch/s390/crypto/phmac_s390.c new file +10
-0+int rc; + + rc =phmac_convert_key (desc->tfm); +if (rc) + goto out; + +spin_lock_bh (&tfm_ctx->pk_lock); +memcpy (ctx->param +SHA2_KEY_OFFSET (bs), + tfm_ctx->pk.protkey, tfm_ctx->pk.len); +spin_unlock_bh (&tfm_ctx->pk_lock);
This appers to be completely broken. Each tfm can be used by an unlimited number of descriptors in parallel. So you cannot modify the tfm context. I see that you have taken spinlocks around it, but it is still broken:
CPU1 CPU2
lock(tfm)tfm->pk = pk1
unlock(tfm)
lock(tfm) tfm->pk = pk2 unlock(tfm)
lock(tfm)
copy tfm->pk to desc
pk2 is copied
unlock(tfm)
Now this could all be harmless because pk1 and pk2 may be guaranteed to be the same, but if that's the case why go through all this in the first place? You could've just done it in setkey.
Cheers,
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
Harald Freudenberger
maintainerRe: [PATCH v3 3/3] s390/crypto: New s390 specific shash phmac
Quoted 35 lines · Herbert Xu
Quoted 6 lines · Harald Freudenberger
arch/s390/crypto/phmac_s390.c new file +5
-0+static int s390_phmac_sha2_init (struct shash_desc *desc) +{ +struct s390_phmac_ctx *tfm_ctx =crypto_shash_ctx (desc->tfm); +struct s390_kmac_sha2_ctx *ctx =shash_desc_ctx (desc); +unsigned int bs =crypto_shash_blocksize (desc->tfm);arch/s390/crypto/phmac_s390.c line 205
Quoted 10 lines
arch/s390/crypto/phmac_s390.c new file +10
-0+int rc; + + rc =phmac_convert_key (desc->tfm); +if (rc) + goto out; + +spin_lock_bh (&tfm_ctx->pk_lock); +memcpy (ctx->param +SHA2_KEY_OFFSET (bs), + tfm_ctx->pk.protkey, tfm_ctx->pk.len); +spin_unlock_bh (&tfm_ctx->pk_lock);This appers to be completely broken. Each tfm can be used by an unlimited number of descriptors in parallel. So you cannot modify the tfm context. I see that you have taken spinlocks around it, but it is still broken:
CPU1 CPU2
lock(tfm)tfm->pk= pk1
unlock(tfm)lock(tfm) tfm->pk = pk2 unlock(tfm)lock(tfm)
copytfm->pkto desc
pk2 is copied
unlock(tfm)
Quoted 5 lines
Now this could all be harmless because pk1 and pk2 may be guaranteed to be the same, but if that's the case why go through all this in the first place? You could've just done it in setkey.
Cheers,
Well, we had a similar discussion once with paes (See
https://lore.kernel.org/linux-crypto/[email protected]/)
The tfm holds the pkey which is a hardware wrapped version of the key value. It is generated by a special invocation done via the PKEY kernel module(s) which knows how to unpack the raw key material and re-wrap it so it can be used with the CPACF instructions. The hardware wrapping key may change - in fact it chances for example with a KVM guest relocated to another system and then this unpack/rewrap cycle needs to be triggered again and thus the pkey may change but the underlying "effective" or "real" key stays the same. In that sense the tfm holding the pkey value is updated. To make the update of the pkey atomic the spinlock is used as the tfm may be used by multiple hash contexts.
Why not convert in the setkey() function? As of now this could be an option as the invocation of convert_key() in the end within the PKEY pkey_pckmo kernel module only calls PCKMO to generate the wrapped pkey. However, long term we will have another path using a crypto card for this action and then we are clearly in a sleeping context which must not be used from setkey(). So make it correct now means to delay the conversion from setkey() to later: the init of the hash context is the next chance to do this.
I see that calling the conversion each time a shash_init() is called is total overkill and an avoidable action as the dm-integrity layer calls this per sector. This may even get worse if we intent to go a hardware path down to convert the key. So I am thinking of a better way which avoids this overhead ... patch is under construction ...
Regards
Harald Freudenberger
Herbert Xu
maintainerRe: [PATCH v3 3/3] s390/crypto: New s390 specific shash phmac
On Tue, Nov 12, 2024 at 01:45:53PM +0100, Harald Freudenberger wrote:
.
Quoted 2 lines
Well, we had a similar discussion once with paes (See
https://lore.kernel.org/linux-crypto/[email protected]/)
Thanks for refreshing my memroy.
Quoted 7 lines
Why not convert in the
setkey()function? As of now this could be an option as the invocation ofconvert_key()in the end within the PKEYpkey_pckmokernel module only calls PCKMO to generate the wrapped pkey. However, long term we will have another path using a crypto card for this action and then we are clearly in a sleeping context which must not be used fromsetkey(). So make it correct now means to delay the conversion fromsetkey()to later: the init of the hash context is the next chance to do this.
I'm not sure I get this justification for putting it in init instead of setkey. If you can't do it in setkey because it has to sleep, then surely you can't do it from init/udpate either as they too can be called from atomic contexts?
Cheers,
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