Skip to contentSkip to conversation

New s390 specific protected key hmac

PATCH Superseded Harald Freudenberger 2 participants 5 messages Nov 15, 2024

Subsystems (3)

v4 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
author maintainer
#2 lore raw

PATCH v4 2/3 s390/crypto: Add protected key hmac subfunctions for KMAC

In reply to Harald Freudenberger

From: 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
index 59ab1192e2d5..0468dd5c4690 100644
@@ -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
author maintainer
#3 lore raw

PATCH v4 3/3 s390/crypto: New s390 specific shash phmac

In reply to 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_ALG interface.

Please note also that this implementation actively checks for non-sleeping context before attempting to derive a protected key from the given raw key material. This is due to the fact that it may be (in the future) that this process has to interact with crypto cards and thus requires IO traffic - which requires sleeping allowed context. So there is a check for in_task() and if this fails, -EOPNOTSUPP is returned to the caller.

---
 arch/s390/configs/debug_defconfig |   1 +
 arch/s390/configs/defconfig       |   1 +
 arch/s390/crypto/Makefile         |   1 +
 arch/s390/crypto/phmac_s390.c     | 481 ++++++++++++++++++++++++++++++
 drivers/crypto/Kconfig            |  12 +
 5 files changed, 496 insertions(+)
 create mode 100644 arch/s390/crypto/phmac_s390.c
arch/s390/configs/debug_defconfig +1 -0
index 6b602d972e91..a8ba72e7162d 100644
@@ -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
index 7844b9f5851b..cd2e9c94b736 100644
@@ -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
index a0cb96937c3d..47637140b95c 100644
@@ -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 +481 -0

481 lines not on this page · raw message

drivers/crypto/Kconfig +12 -0
index 0a9cdd31cbd9..519305e04f18 100644
@@ -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

Harald Freudenberger

author maintainer
#0 lore raw

v4 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.
v4: Rewind back more or less to v2. Add code to check for
non-sleeping context. Non-sleeping context during attempt to derive the protected key from raw key material is not accepted and -EOPNOTSUPP is returned (also currently all derivation pathes would in fact never sleep). In general the phmac implementation is not to be used within non-sleeping context and the code header mentions this. Tested with (patched) dm-integrity - works fine.

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     | 481 ++++++++++++++++++++++++++++++
 arch/s390/include/asm/cpacf.h     |   4 +
 drivers/crypto/Kconfig            |  12 +
 include/crypto/hash.h             |   5 +-
 7 files changed, 502 insertions(+), 3 deletions(-)
 create mode 100644 arch/s390/crypto/phmac_s390.c

base-commit: e200565d434b66e5b2bfc3b143b66b8ca29666ad

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v4 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
index 2d5ea9f9ff43..5b724dd7c5a7 100644
@@ -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
maintainer
#4 lore raw

Re: [PATCH v4 3/3] s390/crypto: New s390 specific shash phmac

In reply to Harald Freudenberger
Quoted 8 lines · Harald Freudenberger

Please note also that this implementation actively checks for non-sleeping context before attempting to derive a protected key from the given raw key material. This is due to the fact that it may be (in the future) that this process has to interact with crypto cards and thus requires IO traffic - which requires sleeping allowed context. So there is a check for in_task() and if this fails, -EOPNOTSUPP is returned to the caller.

I don't think in_task is right. You cannot sleep when spinlocks are held and in general there is no way to test for that:

/*
* Are we running in atomic context? WARNING: this macro cannot
* always detect atomic context; in particular, it cannot know about
* held spinlocks in non-preemptible kernels. Thus it should not be
* used in the general case to determine whether sleeping is possible.
* Do not use in_atomic() in driver code.
*/
#define in_atomic() (preempt_count() != 0)

While the Crypto API provides a mechanism for you to determine whether you can sleep (CRYPTO_TFM_REQ_MAY_SLEEP), I don't think it is acceptable to just randomly fail because you got called in a unsleepable context.

The general solution for this problem is to make your algorithms asynchronous in the unsleepable context. See how we handle this in crypto/simd.c.

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