Skip to contentSkip to conversation

target/s390x: Extend qemu CPACF support

PATCH Superseded Harald Freudenberger 4 participants 35 messages Jul 6 – Jul 8

Subsystems (3)

v10 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#3 lore raw

PATCH v10 2/21 target/s390x: Rework s390 cpacf implementations

In reply to Harald Freudenberger

Fix missing parts for MSA 9 kdsa and rework the cpacf handling code so that further extensions can be made in a clean and structured way.

Introduce a new header file to hold defines, structs and function prototypes around s390 cpacf. Use the cpcaf function defines in the existing code.

---
 target/s390x/tcg/cpacf.h         | 226 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  90 ++++++++++--
 target/s390x/tcg/insn-data.h.inc |   1 +
 target/s390x/tcg/translate.c     |   2 +
 4 files changed, 306 insertions(+), 13 deletions(-)
 create mode 100644 target/s390x/tcg/cpacf.h
target/s390x/tcg/cpacf.h new file +226 -0
new file mode 100644
index 0000000000..05596e0645
@@ -0,0 +1,226 @@
+/*
+ * s390x cpacf
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef S390X_CPACF_H
+#define S390X_CPACF_H
+
+/*
+ * Function codes for the KM instruction
+ */
+#define CPACF_KM_QUERY		0x00
+#define CPACF_KM_DEA		0x01
+#define CPACF_KM_TDEA_128	0x02
+#define CPACF_KM_TDEA_192	0x03
+#define CPACF_KM_AES_128	0x12
+#define CPACF_KM_AES_192	0x13
+#define CPACF_KM_AES_256	0x14
+#define CPACF_KM_PAES_128	0x1a
+#define CPACF_KM_PAES_192	0x1b
+#define CPACF_KM_PAES_256	0x1c
+#define CPACF_KM_XTS_128	0x32
+#define CPACF_KM_XTS_256	0x34
+#define CPACF_KM_PXTS_128	0x3a
+#define CPACF_KM_PXTS_256	0x3c
+#define CPACF_KM_FULL_XTS_128	0x52
+#define CPACF_KM_FULL_XTS_256	0x54
+#define CPACF_KM_FULL_PXTS_128	0x5a
+#define CPACF_KM_FULL_PXTS_256	0x5c
+
+/*
+ * Function codes for the KMC instruction
+ */
+#define CPACF_KMC_QUERY		0x00
+#define CPACF_KMC_DEA		0x01
+#define CPACF_KMC_TDEA_128	0x02
+#define CPACF_KMC_TDEA_192	0x03
+#define CPACF_KMC_AES_128	0x12
+#define CPACF_KMC_AES_192	0x13
+#define CPACF_KMC_AES_256	0x14
+#define CPACF_KMC_PAES_128	0x1a
+#define CPACF_KMC_PAES_192	0x1b
+#define CPACF_KMC_PAES_256	0x1c
+#define CPACF_KMC_PRNG		0x43
+
+/*
+ * Function codes for the KMCTR instruction
+ */
+#define CPACF_KMCTR_QUERY	0x00
+#define CPACF_KMCTR_DEA		0x01
+#define CPACF_KMCTR_TDEA_128	0x02
+#define CPACF_KMCTR_TDEA_192	0x03
+#define CPACF_KMCTR_AES_128	0x12
+#define CPACF_KMCTR_AES_192	0x13
+#define CPACF_KMCTR_AES_256	0x14
+#define CPACF_KMCTR_PAES_128	0x1a
+#define CPACF_KMCTR_PAES_192	0x1b
+#define CPACF_KMCTR_PAES_256	0x1c
+
+/*
+ * Function codes for the KIMD instruction
+ */
+#define CPACF_KIMD_QUERY	0x00
+#define CPACF_KIMD_SHA_1	0x01
+#define CPACF_KIMD_SHA_256	0x02
+#define CPACF_KIMD_SHA_512	0x03
+#define CPACF_KIMD_SHA3_224	0x20
+#define CPACF_KIMD_SHA3_256	0x21
+#define CPACF_KIMD_SHA3_384	0x22
+#define CPACF_KIMD_SHA3_512	0x23
+#define CPACF_KIMD_SHAKE_128    0x24
+#define CPACF_KIMD_SHAKE_256    0x25
+#define CPACF_KIMD_GHASH	0x41
+
+/*
+ * Function codes for the KLMD instruction
+ */
+#define CPACF_KLMD_QUERY	0x00
+#define CPACF_KLMD_SHA_1	0x01
+#define CPACF_KLMD_SHA_256	0x02
+#define CPACF_KLMD_SHA_512	0x03
+#define CPACF_KLMD_SHA3_224	0x20
+#define CPACF_KLMD_SHA3_256	0x21
+#define CPACF_KLMD_SHA3_384	0x22
+#define CPACF_KLMD_SHA3_512	0x23
+#define CPACF_KLMD_SHAKE_128    0x24
+#define CPACF_KLMD_SHAKE_256    0x25
+
+/*
+ * function codes for the KMAC instruction
+ */
+#define CPACF_KMAC_QUERY	 0x00
+#define CPACF_KMAC_DEA		 0x01
+#define CPACF_KMAC_TDEA_128	 0x02
+#define CPACF_KMAC_TDEA_192	 0x03
+#define CPACF_KMAC_AES_128	 0x12
+#define CPACF_KMAC_AES_192	 0x13
+#define CPACF_KMAC_AES_256	 0x14
+#define CPACF_KMAC_PAES_128	 0x1A
+#define CPACF_KMAC_PAES_192	 0x1B
+#define CPACF_KMAC_PAES_256	 0x1C
+#define CPACF_KMAC_HMAC_SHA_224	 0x70
+#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 instruction
+ */
+#define CPACF_PCKMO_QUERY		       0x00
+#define CPACF_PCKMO_ENC_DES_KEY		       0x01
+#define CPACF_PCKMO_ENC_TDES_128_KEY	       0x02
+#define CPACF_PCKMO_ENC_TDES_192_KEY	       0x03
+#define CPACF_PCKMO_ENC_AES_128_KEY	       0x12
+#define CPACF_PCKMO_ENC_AES_192_KEY	       0x13
+#define CPACF_PCKMO_ENC_AES_256_KEY	       0x14
+#define CPACF_PCKMO_ENC_AES_XTS_128_DOUBLE_KEY 0x14
+#define CPACF_PCKMO_ENC_AES_XTS_256_DOUBLE_KEY 0x16
+#define CPACF_PCKMO_ENC_ECC_P256_KEY	       0x20
+#define CPACF_PCKMO_ENC_ECC_P384_KEY	       0x21
+#define CPACF_PCKMO_ENC_ECC_P521_KEY	       0x22
+#define CPACF_PCKMO_ENC_ECC_ED25519_KEY	       0x28
+#define CPACF_PCKMO_ENC_ECC_ED448_KEY	       0x29
+#define CPACF_PCKMO_ENC_HMAC_512_KEY	       0x76
+#define CPACF_PCKMO_ENC_HMAC_1024_KEY	       0x7a
+
+/*
+ * Function codes for the PRNO instruction
+ */
+#define CPACF_PRNO_QUERY		0x00
+#define CPACF_PRNO_SHA512_DRNG_GEN	0x03
+#define CPACF_PRNO_SHA512_DRNG_SEED	0x83
+#define CPACF_PRNO_TRNG_Q_R2C_RATIO	0x70
+#define CPACF_PRNO_TRNG			0x72
+
+/*
+ * Function codes for the KMA instruction
+ */
+#define CPACF_KMA_QUERY		0x00
+#define CPACF_KMA_GCM_AES_128	0x12
+#define CPACF_KMA_GCM_AES_192	0x13
+#define CPACF_KMA_GCM_AES_256	0x14
+#define CPACF_KMA_GCM_PAES_128  0x1A
+#define CPACF_KMA_GCM_PAES_192  0x1B
+#define CPACF_KMA_GCM_PAES_256  0x1C
+
+/*
+ * Function codes for the KMF instruction
+ */
+#define CPACF_KMF_QUERY      0
+#define CPACF_KMF_DEA        1
+#define CPACF_KMF_TDEA_128   2
+#define CPACF_KMF_TDEA_192   3
+#define CPACF_KMF_AES_128   18
+#define CPACF_KMF_AES_192   19
+#define CPACF_KMF_AES_256   20
+#define CPACF_KMF_PAES_128  26
+#define CPACF_KMF_PAES_192  27
+#define CPACF_KMF_PAES_256  28
+
+/*
+ * Function codes for the KMO instruction
+ */
+#define CPACF_KMO_QUERY      0
+#define CPACF_KMO_DEA        1
+#define CPACF_KMO_TDEA_128   2
+#define CPACF_KMO_TDEA_192   3
+#define CPACF_KMO_AES_128   18
+#define CPACF_KMO_AES_192   19
+#define CPACF_KMO_AES_256   20
+#define CPACF_KMO_PAES_128  26
+#define CPACF_KMO_PAES_192  27
+#define CPACF_KMO_PAES_256  28
+
+/*
+ * Function codes for the PCC instruction
+ */
+#define CPACF_PCC_QUERY            0
+#define CPACF_PCC_CMAC_DEA         1
+#define CPACF_PCC_CMAC_TDEA_128    2
+#define CPACF_PCC_CMAC_TDEA_192    3
+#define CPACF_PCC_CMAC_AES_128    18
+#define CPACF_PCC_CMAC_AES_192    19
+#define CPACF_PCC_CMAC_AES_256    20
+#define CPACF_PCC_CMAC_PAES_128   26
+#define CPACF_PCC_CMAC_PAES_192   27
+#define CPACF_PCC_CMAC_PAES_256   28
+#define CPACF_PCC_XTS_AES_128     50
+#define CPACF_PCC_XTS_AES_256     52
+#define CPACF_PCC_XTS_PAES_128    58
+#define CPACF_PCC_XTS_PAES_256    60
+#define CPACF_PCC_SM_P256         64
+#define CPACF_PCC_SM_P384         65
+#define CPACF_PCC_SM_P521         66
+#define CPACF_PCC_SM_ED25519      72
+#define CPACF_PCC_SM_ED448        73
+#define CPACF_PCC_SM_X25519       80
+#define CPACF_PCC_SM_X448         81
+
+/*
+ * Function codes for the KDSA instruction
+ */
+#define CPACF_KDSA_QUERY            0
+#define CPACF_KDSA_VERIFY_P256      1
+#define CPACF_KDSA_VERIFY_P384      2
+#define CPACF_KDSA_VERIFY_P521      3
+#define CPACF_KDSA_SIGN_P256        9
+#define CPACF_KDSA_SIGN_P384       10
+#define CPACF_KDSA_SIGN_P521       11
+#define CPACF_KDSA_PSIGN_P256      17
+#define CPACF_KDSA_PSIGN_P384      18
+#define CPACF_KDSA_PSIGN_P521      19
+#define CPACF_KDSA_VERIFY_ED25519  32
+#define CPACF_KDSA_VERIFY_ED448    36
+#define CPACF_KDSA_SIGN_ED25519    40
+#define CPACF_KDSA_SIGN_ED448      44
+#define CPACF_KDSA_PSIGN_ED25519   48
+#define CPACF_KDSA_PSIGN_ED448     52
+
+#endif /* S390X_CPACF_H */
target/s390x/tcg/crypto_helper.c +77 -13

127 lines not on this page · raw message

target/s390x/tcg/insn-data.h.inc +1 -0
index 0d5392eac5..6a0a7aacda 100644
@@ -1015,6 +1015,7 @@
     D(0xb92e, KM,      RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KM)
     D(0xb92f, KMC,     RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KMC)
     D(0xb929, KMA,     RRF_b, MSA8, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KMA)
+    D(0xb93a, KDSA,    RRE,   MSA9, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KDSA)
     E(0xb93c, PPNO,    RRE,   MSA5, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_PPNO, IF_IO)
     D(0xb93e, KIMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KIMD)
     D(0xb93f, KLMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KLMD)
target/s390x/tcg/translate.c +2 -0
index 82165ac1ec..cef1b55149 100644
@@ -2592,6 +2592,7 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
         /* FALL THROUGH */
     case S390_FEAT_TYPE_PCKMO:
     case S390_FEAT_TYPE_PCC:
+    case S390_FEAT_TYPE_KDSA:
         break;
     default:
         g_assert_not_reached();
@@ -6046,6 +6047,7 @@ enum DisasInsnEnum {
 #define FAC_MSA4        S390_FEAT_MSA_EXT_4 /* msa-extension-4 facility */
 #define FAC_MSA5        S390_FEAT_MSA_EXT_5 /* msa-extension-5 facility */
 #define FAC_MSA8        S390_FEAT_MSA_EXT_8 /* msa-extension-8 facility */
+#define FAC_MSA9        S390_FEAT_MSA_EXT_9 /* msa-extension-9 facility */
 #define FAC_ECT         S390_FEAT_EXTRACT_CPU_TIME
 #define FAC_PCI         S390_FEAT_ZPCI /* z/PCI facility */
 #define FAC_AIS         S390_FEAT_ADAPTER_INT_SUPPRESSION
Signature · 1 line
2.43.0
#8 lore raw

PATCH v10 6/21 target/s390x: Support AES CBC for cpacf kmc instruction

In reply to Harald Freudenberger

Support the subfunctions CPACF_KMC_AES_128, CPACF_KMC_AES_192 and CPACF_KMC_AES_256 for the cpacf kmc instruction.

---
 target/s390x/gen-features.c      |   3 +
 target/s390x/tcg/cpacf.h         |   4 ++
 target/s390x/tcg/cpacf_aes.c     | 103 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  24 +++++++
 4 files changed, 134 insertions(+)
target/s390x/gen-features.c +3 -0
index a35d1fd2f9..9c0c0b229f 100644
@@ -924,6 +924,9 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KM_AES_128,
     S390_FEAT_KM_AES_192,
     S390_FEAT_KM_AES_256,
+    S390_FEAT_KMC_AES_128,
+    S390_FEAT_KMC_AES_192,
+    S390_FEAT_KMC_AES_256,
 };
 
 /****** END FEATURE DEFS ******/
target/s390x/tcg/cpacf.h +4 -0
index ca1850c976..572c588d38 100644
@@ -238,5 +238,9 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t param_addr, uint64_t *dst_ptr_reg,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                  uint64_t param_addr, uint64_t *dst_ptr_reg,
+                  uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                  uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +103 -0

106 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +24 -0
index 3907b9748c..1fe1d7157b 100644
@@ -110,6 +110,27 @@ static int cpacf_km(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     return rc;
 }
 
+static int cpacf_kmc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                     uint32_t r1, uint32_t r2, uint32_t r3,
+                     uint8_t fc, uint8_t mod)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case CPACF_KMC_AES_128:
+    case CPACF_KMC_AES_192:
+    case CPACF_KMC_AES_256:
+        rc = cpacf_aes_cbc(env, mmu_idx, ra, env->regs[1],
+                           &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                           S390_FEAT_TYPE_KMC, fc, mod);
+        break;
+    default:
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+    }
+
+    return rc;
+}
+
 static int cpacf_ppno(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                       uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
 {
@@ -177,6 +198,9 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     case S390_FEAT_TYPE_KM:
         rc = cpacf_km(env, mmu_idx, ra, r1, r2, r3, fc, mod);
         break;
+    case S390_FEAT_TYPE_KMC:
+        rc = cpacf_kmc(env, mmu_idx, ra, r1, r2, r3, fc, mod);
+        break;
     case S390_FEAT_TYPE_PPNO:
         rc = cpacf_ppno(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
Signature · 1 line
2.43.0
#0 lore raw

v10 cover target/s390x: Extend qemu CPACF support

This patch series extends the s390 qemu CPACF support to be able to run a subset of the CPACF instruction cross platform. There have been requests on the kernel crypto mailing list about a way to test s390 specific crypto implementations. For example a way to test s390 CPACF exploitation code like the s390_aes.ko kernel module.

So here now is a set of patches verified on x86 and s390 which over (slow but working) support for a subset of the subfunctions of some of the CPACF instructions.

Test: There are some very basic tests included with this patch series suitable for some CI run. Better test coverage can be done by running a full blown Linux and use for example the in-kernel crypto modules. The 'usual' in-kernel crpyto modules will be automatically loaded which run a bunch of test cases. So there is now support for these kernel modules:
* sha256_s390x (autoloaded, sha256)
* sha512_s390x (autoloaded, sha512)
* aes_s390x (autoloaded, clear key aes ecb, cbc, ctr, xts)
* pkey_pckmo (autoloaded, derive AES protected key from clear key)
* paes_s390x (not autoloaded, protected key aes ecb, cbc, ctr, xts) All these modules run selftests if configured by the kernel (which is enabled by default). Failures are reported via syslog. Additionally the aes testcases from libica can be run either inside such an qemu environment or with a static build executed with the qemu tcg application qemu-s390x --cpu max <static-build-libica-test>.

Changelog:
v1: Initial version with
- Related code restructured
- Support KIMD SHA512 and thus SHA256
- Support KMC AES-128, AES-192 and AES-256 and thus have basic AES support (ECB mode) enabled.
- Support PCC Compute-XTS-Parameter-AES-128 and Compute-XTS-Parameter-AES-256 but only for block sequence number
0. This is a requirement for the next step:
- Support KM XTS-AES-128 and KM XTS-AES-256. Together with the minimal PCC support this enables AES-XTS CPACF acceleration.
v2: - Basic PCKMO support to be able to 'derive' an AES protected key
from clear key. See header details.
- Support protected key AES-ECB.
- Support protected key AES-CBC.
- Minimal protected key AES-XTS support for CPACF PCC.
- Support protected key AES-XTS.
- Support AES-CTR.
- Support protected key AES-CTR.
v3: - Reordered patches as suggested by Finn.
- One small bug fix in CPACF_aes.c related to address translation.
v4: - Rename of the parameters based on feedback from Janosch to
make clear these are registers or ptrs to registers. Added Tested by from Holger. Fixed typo "face" -> "fake".
v5: - Add documentation file docs/system/s390x/cpacf.rst which
describes the state of the CPACF instructions and which functions are covered when this series is applied. First version sent to public mailing list qemu-s390x.
v6: - Rebase/rework to build on current qemu head.
- Add docs/system/s390x/cpacf.rst to target-s390x.rst
- New file crypto/aes-helpers.c with some simple functions to support AES modes CBC, CTR and XTS.
- Slight rewrite of the s390x CPACF implementations to use these generic AES mode implementations.
v7: - Update on docs/system/s390x/cpacf.rst to mention
the zArchicteture Principles of Operation document which describes all these CPACF instructions.
v8: - Add a fix which deals with incorrect address handling
in the sha512 implementation related to fetch and push data from/to memory.
- Slight rework around the capcf function implementation and exception generation.
- Added some more details to the new cpacf.rst file.
- Fixed some typos and added some suggestions from Finn.
- Fixed cc handling on return of PCKMO (must not update cc).
Missing: simple test cases to verify that the implemented and not implemented cpacf functions and subfunctions work as expected. But see the statement about tests at the header.
v9: - Add simple tests for all the implemented CPACF instructions but
pckmo (which is a privileged instruction).
- Reworked the Fix for wrong address to call the wrap function inline; rephrased commit header.
- Improve the header file cpacf.h to hold defines for all the cpacf instruction functions and use them in the code.
- one new commit comprising the base protected key support with exposing the xor pattern and wkvp and en/decrypt key functions via cpacf.h. So the testcases can use this header file.
- one new commit which reworks the fetch memory and store memory from and to guest (suggested by Ilya Leoshkevich).
v10: - Fixed v9 patch 3 (cpacf_sha512.c was missing)
Please note that patch #10 "target/s390x: Base support for cpacf protected keys" produces a build warning ("unused function"). As by default the qemu build has warnings=errors enabled, this one patch does not build on it's own. If this is not acceptable, the hunk introducing the two functions may be moved to the next commit; another solution would be to merge with patch #11.

Harald Freudenberger (21):
target/s390x: Fix wrong address handling in address loops target/s390x: Rework s390 cpacf implementations target/s390x: Move cpacf sha512 code into a new file target/s390x: Support cpacf sha256
target/s390x: Support AES ECB for cpacf km instruction target/s390x: Support AES CBC for cpacf kmc instruction target/s390x: Support AES CTR for cpacf kmctr instruction target/s390x: Minimal AES XTS support for cpacf pcc instruction target/s390x: Support AES XTS for cpacf km instruction target/s390x: Base support for cpacf protected keys target/s390x: Support pckmo encrypt AES subfunctions target/s390x: Support protected key AES ECB for cpacf km instruction target/s390x: Support protected key AES CBC for cpacf kmc instruction target/s390x: Support protected key AES CTR for cpacf kmctr
instruction
target/s390x: Minimal protected key AES XTS support for cpacf pcc
instruction
target/s390x: Support protected key AES XTS for cpacf km instruction docs/s390: Document CPACF instructions support
crypto: Add aes-helpers file to support some AES modes target/s390x: Use generic AES helper functions target/s390x: Improve fetch and store mem from and to guest tests/tcg/s390x: Add tests for CPACF instructions

 crypto/aes-helpers.c             | 106 ++++
 crypto/meson.build               |   1 +
 docs/system/s390x/cpacf.rst      | 144 +++++
 docs/system/target-s390x.rst     |   1 +
 include/crypto/aes.h             |  14 +
 target/s390x/gen-features.c      |  31 ++
 target/s390x/tcg/cpacf.h         | 312 +++++++++++
 target/s390x/tcg/cpacf_aes.c     | 903 +++++++++++++++++++++++++++++++
 target/s390x/tcg/cpacf_sha256.c  | 228 ++++++++
 target/s390x/tcg/cpacf_sha512.c  | 241 +++++++++
 target/s390x/tcg/crypto_helper.c | 426 ++++++++-------
 target/s390x/tcg/insn-data.h.inc |   1 +
 target/s390x/tcg/meson.build     |   3 +
 target/s390x/tcg/translate.c     |  11 +-
 tests/tcg/s390x/Makefile.target  |   9 +
 tests/tcg/s390x/cpacf-kdsa.c     |  59 ++
 tests/tcg/s390x/cpacf-kimd.c     | 164 ++++++
 tests/tcg/s390x/cpacf-klmd.c     | 202 +++++++
 tests/tcg/s390x/cpacf-km.c       | 576 ++++++++++++++++++++
 tests/tcg/s390x/cpacf-kmac.c     |  59 ++
 tests/tcg/s390x/cpacf-kmc.c      | 342 ++++++++++++
 tests/tcg/s390x/cpacf-kmctr.c    | 354 ++++++++++++
 tests/tcg/s390x/cpacf-pcc.c      | 241 +++++++++
 tests/tcg/s390x/cpacf-prno.c     | 130 +++++
 tests/tcg/s390x/cpacf.h          | 570 +++++++++++++++++++
 25 files changed, 4911 insertions(+), 217 deletions(-)
 create mode 100644 crypto/aes-helpers.c
 create mode 100644 docs/system/s390x/cpacf.rst
 create mode 100644 target/s390x/tcg/cpacf.h
 create mode 100644 target/s390x/tcg/cpacf_aes.c
 create mode 100644 target/s390x/tcg/cpacf_sha256.c
 create mode 100644 target/s390x/tcg/cpacf_sha512.c
 create mode 100644 tests/tcg/s390x/cpacf-kdsa.c
 create mode 100644 tests/tcg/s390x/cpacf-kimd.c
 create mode 100644 tests/tcg/s390x/cpacf-klmd.c
 create mode 100644 tests/tcg/s390x/cpacf-km.c
 create mode 100644 tests/tcg/s390x/cpacf-kmac.c
 create mode 100644 tests/tcg/s390x/cpacf-kmc.c
 create mode 100644 tests/tcg/s390x/cpacf-kmctr.c
 create mode 100644 tests/tcg/s390x/cpacf-pcc.c
 create mode 100644 tests/tcg/s390x/cpacf-prno.c
 create mode 100644 tests/tcg/s390x/cpacf.h

base-commit: 20553466cc47af6a8c95f665b601fce3c852e503 --
2.43.0

#1 lore raw

PATCH v10 1/21 target/s390x: Fix wrong address handling in address loops

The loop increments addr by the element stride (+= 4) before calling wrap_address, but then overwrites the loop addr with the wrapped value. On the next iteration the stride is applied to the wrapped address of the previous element, not to the original unwrapped address. This results in every element after the first is read from a wrong (wrapped) address.

Fixes 9f17bfdab4 ("target/s390x: support SHA-512 extensions")
---
 target/s390x/tcg/crypto_helper.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
target/s390x/tcg/crypto_helper.c +4 -8
index ae392bce0e..8fe0a22219 100644
@@ -126,8 +126,7 @@ static void sha512_read_icv(CPUS390XState *env, const int mmu_idx,
     const MemOpIdx oi = make_memop_idx(MO_BE | MO_64 | MO_UNALN, mmu_idx);
 
     for (int i = 0; i < 8; i++, addr += 8) {
-        addr = wrap_address(env, addr);
-        a[i] = cpu_ldq_mmu(env, addr, oi, ra);
+        a[i] = cpu_ldq_mmu(env, wrap_address(env, addr), oi, ra);
     }
 }
 
@@ -137,8 +136,7 @@ static void sha512_write_ocv(CPUS390XState *env, const int mmu_idx,
     const MemOpIdx oi = make_memop_idx(MO_BE | MO_64 | MO_UNALN, mmu_idx);
 
     for (int i = 0; i < 8; i++, addr += 8) {
-        addr = wrap_address(env, addr);
-        cpu_stq_mmu(env, addr, a[i], oi, ra);
+        cpu_stq_mmu(env, wrap_address(env, addr), a[i], oi, ra);
     }
 }
 
@@ -148,8 +146,7 @@ static void sha512_read_block(CPUS390XState *env, const int mmu_idx,
     const MemOpIdx oi = make_memop_idx(MO_BE | MO_64 | MO_UNALN, mmu_idx);
 
     for (int i = 0; i < 16; i++, addr += 8) {
-        addr = wrap_address(env, addr);
-        a[i] = cpu_ldq_mmu(env, addr, oi, ra);
+        a[i] = cpu_ldq_mmu(env, wrap_address(env, addr), oi, ra);
     }
 }
 
@@ -159,8 +156,7 @@ static void sha512_read_mbl_be64(CPUS390XState *env, const int mmu_idx,
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
 
     for (int i = 0; i < 16; i++, addr += 1) {
-        addr = wrap_address(env, addr);
-        a[i] = cpu_ldb_mmu(env, addr, oi, ra);
+        a[i] = cpu_ldb_mmu(env, wrap_address(env, addr), oi, ra);
     }
 }
 
Signature · 1 line
2.43.0
#7 lore raw

PATCH v10 5/21 target/s390x: Support AES ECB for cpacf km instruction

In reply to Harald Freudenberger

Support the subfunctions CPACF_KM_AES_128, CPACF_KM_AES_192 and CPACF_KM_AES_256 for the cpacf km instruction.

---
 target/s390x/gen-features.c      |   3 +
 target/s390x/tcg/cpacf.h         |   6 ++
 target/s390x/tcg/cpacf_aes.c     | 112 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  24 +++++++
 target/s390x/tcg/meson.build     |   1 +
 5 files changed, 146 insertions(+)
 create mode 100644 target/s390x/tcg/cpacf_aes.c
target/s390x/gen-features.c +3 -0
index 5cf5b92c37..a35d1fd2f9 100644
@@ -921,6 +921,9 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KLMD_SHA_256,
     S390_FEAT_KLMD_SHA_512,
     S390_FEAT_PRNO_TRNG,
+    S390_FEAT_KM_AES_128,
+    S390_FEAT_KM_AES_192,
+    S390_FEAT_KM_AES_256,
 };
 
 /****** END FEATURE DEFS ******/
target/s390x/tcg/cpacf.h +6 -0
index d295b49699..ca1850c976 100644
@@ -233,4 +233,10 @@ int cpacf_sha512(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                  uint64_t param_addr, uint64_t *message_reg, uint64_t *len_reg,
                  uint32_t type);
 
+/* from cpacf_aes.c */
+int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                  uint64_t param_addr, uint64_t *dst_ptr_reg,
+                  uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                  uint32_t type, uint8_t fc, uint8_t mod);
+
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c new file +112 -0

112 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +24 -0
index 6c296f6731..3907b9748c 100644
@@ -89,6 +89,27 @@ static int cpacf_klmd(CPUS390XState *env, const int mmu_idx, const uintptr_t ra,
     return rc;
 }
 
+static int cpacf_km(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                    uint32_t r1, uint32_t r2, uint32_t r3,
+                    uint8_t fc, uint8_t mod)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case CPACF_KM_AES_128:
+    case CPACF_KM_AES_192:
+    case CPACF_KM_AES_256:
+        rc = cpacf_aes_ecb(env, mmu_idx, ra, env->regs[1],
+                           &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                           S390_FEAT_TYPE_KM, fc, mod);
+        break;
+    default:
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+    }
+
+    return rc;
+}
+
 static int cpacf_ppno(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                       uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
 {
@@ -153,6 +174,9 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     case S390_FEAT_TYPE_KLMD:
         rc = cpacf_klmd(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
+    case S390_FEAT_TYPE_KM:
+        rc = cpacf_km(env, mmu_idx, ra, r1, r2, r3, fc, mod);
+        break;
     case S390_FEAT_TYPE_PPNO:
         rc = cpacf_ppno(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
target/s390x/tcg/meson.build +1 -0
index 8ae8da9708..6f2e75764b 100644
@@ -5,6 +5,7 @@ s390x_ss.add(when: 'CONFIG_TCG', if_true: files(
 ))
 s390x_common_ss.add(when: 'CONFIG_TCG', if_true: files(
   'cc_helper.c',
+  'cpacf_aes.c',
   'cpacf_sha256.c',
   'cpacf_sha512.c',
   'crypto_helper.c',
Signature · 1 line
2.43.0
#9 lore raw

PATCH v10 7/21 target/s390x: Support AES CTR for cpacf kmctr instruction

In reply to Harald Freudenberger

Support the subfunctions CPACF_KMCTR_AES_128, CPACF_KMCTR_AES_192 and CPACF_KMCTR_AES_256 for the cpacf kmctr instruction.

---
 target/s390x/gen-features.c      |  3 ++
 target/s390x/tcg/cpacf.h         |  5 +++
 target/s390x/tcg/cpacf_aes.c     | 77 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 24 ++++++++++
 4 files changed, 109 insertions(+)
target/s390x/gen-features.c +3 -0
index 9c0c0b229f..59c2a47539 100644
@@ -927,6 +927,9 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KMC_AES_128,
     S390_FEAT_KMC_AES_192,
     S390_FEAT_KMC_AES_256,
+    S390_FEAT_KMCTR_AES_128,
+    S390_FEAT_KMCTR_AES_192,
+    S390_FEAT_KMCTR_AES_256,
 };
 
 /****** END FEATURE DEFS ******/
target/s390x/tcg/cpacf.h +5 -0
index 572c588d38..3707308661 100644
@@ -242,5 +242,10 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t param_addr, uint64_t *dst_ptr_reg,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                  uint64_t param_addr, uint64_t *dst_ptr_reg,
+                  uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                  uint64_t *ctr_ptr_reg, uint32_t type,
+                  uint8_t fc, uint8_t mod);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +77 -0

80 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +24 -0
index 1fe1d7157b..9be8a14a80 100644
@@ -131,6 +131,27 @@ static int cpacf_kmc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     return rc;
 }
 
+static int cpacf_kmctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                       uint32_t r1, uint32_t r2, uint32_t r3,
+                       uint8_t fc, uint8_t mod)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case CPACF_KMCTR_AES_128:
+    case CPACF_KMCTR_AES_192:
+    case CPACF_KMCTR_AES_256:
+        rc = cpacf_aes_ctr(env, mmu_idx, ra, env->regs[1],
+                           &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                           &env->regs[r3], S390_FEAT_TYPE_KMCTR, fc, mod);
+        break;
+    default:
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+    }
+
+    return rc;
+}
+
 static int cpacf_ppno(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                       uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
 {
@@ -201,6 +222,9 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     case S390_FEAT_TYPE_KMC:
         rc = cpacf_kmc(env, mmu_idx, ra, r1, r2, r3, fc, mod);
         break;
+    case S390_FEAT_TYPE_KMCTR:
+        rc = cpacf_kmctr(env, mmu_idx, ra, r1, r2, r3, fc, mod);
+        break;
     case S390_FEAT_TYPE_PPNO:
         rc = cpacf_ppno(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
Signature · 1 line
2.43.0
#22 lore raw

PATCH v10 13/21 target/s390x: Support protected key AES CBC for cpacf kmc instruction

In reply to Harald Freudenberger

Support the subfunctions CPACF_KMC_PAES_128, CPACF_KMC_PAES_192 and CPACF_KMC_PAES_256 for the cpacf kmc instruction.

---
 target/s390x/gen-features.c      |   3 +
 target/s390x/tcg/cpacf.h         |   4 ++
 target/s390x/tcg/cpacf_aes.c     | 108 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   7 ++
 4 files changed, 122 insertions(+)
target/s390x/gen-features.c +3 -0
index 71e0e41d6e..074c53aecd 100644
@@ -932,6 +932,9 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KMC_AES_128,
     S390_FEAT_KMC_AES_192,
     S390_FEAT_KMC_AES_256,
+    S390_FEAT_KMC_EAES_128,
+    S390_FEAT_KMC_EAES_192,
+    S390_FEAT_KMC_EAES_256,
     S390_FEAT_KMCTR_AES_128,
     S390_FEAT_KMCTR_AES_192,
     S390_FEAT_KMCTR_AES_256,
target/s390x/tcg/cpacf.h +4 -0
index cc545fb703..b0d484c3cb 100644
@@ -286,5 +286,9 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint64_t param_addr, uint64_t *dst_ptr_reg,
                    uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                    uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                   uint64_t param_addr, uint64_t *dst_ptr_reg,
+                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                   uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +108 -0

111 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index b00351d8c1..237ce744b7 100644
@@ -137,6 +137,13 @@ static int cpacf_kmc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
                            S390_FEAT_TYPE_KMC, fc, mod);
         break;
+    case CPACF_KMC_PAES_128:
+    case CPACF_KMC_PAES_192:
+    case CPACF_KMC_PAES_256:
+        rc = cpacf_paes_cbc(env, mmu_idx, ra, env->regs[1],
+                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                            S390_FEAT_TYPE_KMC, fc, mod);
+        break;
     default:
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
     }
Signature · 1 line
2.43.0
#6 lore raw

PATCH v10 4/21 target/s390x: Support cpacf sha256

In reply to Harald Freudenberger

Add a new file cpacf_sha256.c which implements sha256. Add support for the sha256 subfuction for CPACF kimd and klmd.

---
 target/s390x/gen-features.c      |   2 +
 target/s390x/tcg/cpacf.h         |   5 +
 target/s390x/tcg/cpacf_sha256.c  | 228 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   8 ++
 target/s390x/tcg/meson.build     |   1 +
 5 files changed, 244 insertions(+)
 create mode 100644 target/s390x/tcg/cpacf_sha256.c
target/s390x/gen-features.c +2 -0
index 8218e6470e..5cf5b92c37 100644
@@ -916,7 +916,9 @@ static uint16_t qemu_V7_1[] = {
  */
 static uint16_t qemu_MAX[] = {
     S390_FEAT_MSA_EXT_5,
+    S390_FEAT_KIMD_SHA_256,
     S390_FEAT_KIMD_SHA_512,
+    S390_FEAT_KLMD_SHA_256,
     S390_FEAT_KLMD_SHA_512,
     S390_FEAT_PRNO_TRNG,
 };
target/s390x/tcg/cpacf.h +5 -0
index 6de79a2f8f..d295b49699 100644
@@ -223,6 +223,11 @@
 #define CPACF_KDSA_PSIGN_ED25519   48
 #define CPACF_KDSA_PSIGN_ED448     52
 
+/* from cpacf_sha256.c */
+int cpacf_sha256(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                 uint64_t param_addr, uint64_t *message_reg, uint64_t *len_reg,
+                 uint32_t type);
+
 /* from cpacf_sha512.c */
 int cpacf_sha512(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                  uint64_t param_addr, uint64_t *message_reg, uint64_t *len_reg,
target/s390x/tcg/cpacf_sha256.c new file +228 -0

228 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +8 -0
index dba46baa0d..6c296f6731 100644
@@ -53,6 +53,10 @@ static int cpacf_kimd(CPUS390XState *env, const int mmu_idx, const uintptr_t ra,
     int rc = 0;
 
     switch (fc) {
+    case CPACF_KIMD_SHA_256:
+        rc = cpacf_sha256(env, mmu_idx, ra, env->regs[1], &env->regs[r2],
+                          &env->regs[r2 + 1], S390_FEAT_TYPE_KIMD);
+        break;
     case CPACF_KIMD_SHA_512:
         rc = cpacf_sha512(env, mmu_idx, ra, env->regs[1], &env->regs[r2],
                           &env->regs[r2 + 1], S390_FEAT_TYPE_KIMD);
@@ -70,6 +74,10 @@ static int cpacf_klmd(CPUS390XState *env, const int mmu_idx, const uintptr_t ra,
     int rc = 0;
 
     switch (fc) {
+    case CPACF_KLMD_SHA_256:
+        rc = cpacf_sha256(env, mmu_idx, ra, env->regs[1], &env->regs[r2],
+                          &env->regs[r2 + 1], S390_FEAT_TYPE_KLMD);
+        break;
     case CPACF_KLMD_SHA_512:
         rc = cpacf_sha512(env, mmu_idx, ra, env->regs[1], &env->regs[r2],
                           &env->regs[r2 + 1], S390_FEAT_TYPE_KLMD);
target/s390x/tcg/meson.build +1 -0
index 54a87393a3..8ae8da9708 100644
@@ -5,6 +5,7 @@ s390x_ss.add(when: 'CONFIG_TCG', if_true: files(
 ))
 s390x_common_ss.add(when: 'CONFIG_TCG', if_true: files(
   'cc_helper.c',
+  'cpacf_sha256.c',
   'cpacf_sha512.c',
   'crypto_helper.c',
   'excp_helper.c',
Signature · 1 line
2.43.0
#10 lore raw

PATCH v10 8/21 target/s390x: Minimal AES XTS support for cpacf pcc instruction

In reply to Harald Freudenberger

Support CPACF pcc subfunctions PCC-Compute-XTS-Parameter-AES-128 and PCC-Compute-XTS-Parameter-AES-128 but only for the special case block sequential number is 0. However, this covers the s390 AES XTS implementation in the Linux kernel and Libica and thus also Opencryptoki clear key via Libica.

---
 target/s390x/gen-features.c      |  2 +
 target/s390x/tcg/cpacf.h         |  2 +
 target/s390x/tcg/cpacf_aes.c     | 63 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 20 ++++++++++
 4 files changed, 87 insertions(+)
target/s390x/gen-features.c +2 -0
index 59c2a47539..1b6a874b90 100644
@@ -930,6 +930,8 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KMCTR_AES_128,
     S390_FEAT_KMCTR_AES_192,
     S390_FEAT_KMCTR_AES_256,
+    S390_FEAT_PCC_XTS_AES_128,
+    S390_FEAT_PCC_XTS_AES_256,
 };
 
 /****** END FEATURE DEFS ******/
target/s390x/tcg/cpacf.h +2 -0
index 3707308661..2e8ed72758 100644
@@ -247,5 +247,7 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint64_t *ctr_ptr_reg, uint32_t type,
                   uint8_t fc, uint8_t mod);
+int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                  uint64_t param_addr, uint8_t fc);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +63 -0

66 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +20 -0
index 9be8a14a80..1d447cef30 100644
@@ -169,6 +169,23 @@ static int cpacf_ppno(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     return rc;
 }
 
+static int cpacf_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                     uint8_t fc)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case CPACF_PCC_XTS_AES_128:
+    case CPACF_PCC_XTS_AES_256:
+            rc = cpacf_aes_pcc(env, mmu_idx, ra, env->regs[1], fc);
+            break;
+    default:
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+    }
+
+    return rc;
+}
+
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
                      uint32_t type)
 {
@@ -225,6 +242,9 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     case S390_FEAT_TYPE_KMCTR:
         rc = cpacf_kmctr(env, mmu_idx, ra, r1, r2, r3, fc, mod);
         break;
+    case S390_FEAT_TYPE_PCC:
+        rc = cpacf_pcc(env, mmu_idx, ra, fc);
+        break;
     case S390_FEAT_TYPE_PPNO:
         rc = cpacf_ppno(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
Signature · 1 line
2.43.0
#23 lore raw

PATCH v10 14/21 target/s390x: Support protected key AES CTR for cpacf kmctr instruction

In reply to Harald Freudenberger

Support the subfunctions CPACF_KMCTR_PAES_128, CPACF_KMCTR_PAES_192 and CPACF_KMCTR_PAES_256 for the cpacf kmctr instruction.

---
 target/s390x/gen-features.c      |  3 ++
 target/s390x/tcg/cpacf.h         |  5 ++
 target/s390x/tcg/cpacf_aes.c     | 89 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  7 +++
 4 files changed, 104 insertions(+)
target/s390x/gen-features.c +3 -0
index 074c53aecd..4a131dc191 100644
@@ -938,6 +938,9 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KMCTR_AES_128,
     S390_FEAT_KMCTR_AES_192,
     S390_FEAT_KMCTR_AES_256,
+    S390_FEAT_KMCTR_EAES_128,
+    S390_FEAT_KMCTR_EAES_192,
+    S390_FEAT_KMCTR_EAES_256,
     S390_FEAT_PCC_XTS_AES_128,
     S390_FEAT_PCC_XTS_AES_256,
     S390_FEAT_PCKMO_AES_128,
target/s390x/tcg/cpacf.h +5 -0
index b0d484c3cb..6071f21fb6 100644
@@ -290,5 +290,10 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint64_t param_addr, uint64_t *dst_ptr_reg,
                    uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                    uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                   uint64_t param_addr, uint64_t *dst_ptr_reg,
+                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                   uint64_t *ctr_ptr_reg, uint32_t type,
+                   uint8_t fc, uint8_t mod);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +89 -0

92 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index 237ce744b7..73b2a6557c 100644
@@ -165,6 +165,13 @@ static int cpacf_kmctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
                            &env->regs[r3], S390_FEAT_TYPE_KMCTR, fc, mod);
         break;
+    case CPACF_KMCTR_PAES_128:
+    case CPACF_KMCTR_PAES_192:
+    case CPACF_KMCTR_PAES_256:
+        rc = cpacf_paes_ctr(env, mmu_idx, ra, env->regs[1],
+                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                            &env->regs[r3], S390_FEAT_TYPE_KMCTR, fc, mod);
+        break;
     default:
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
     }
Signature · 1 line
2.43.0
#26 lore raw

PATCH v10 17/21 docs/s390: Document CPACF instructions support

In reply to Harald Freudenberger

Add a first document covering the Qemu s390 CPACF instructions and functions supported.

---
 docs/system/s390x/cpacf.rst  | 144 +++++++++++++++++++++++++++++++++++
 docs/system/target-s390x.rst |   1 +
 2 files changed, 145 insertions(+)
 create mode 100644 docs/system/s390x/cpacf.rst
docs/system/s390x/cpacf.rst new file +144 -0
new file mode 100644
index 0000000000..1c3a07c59d
@@ -0,0 +1,144 @@
+CPACF Support
+=============
+
+CPACF
+-----
+
+CP Assist for Cryptographic Function (CPACF) is a hardware-integrated
+coprocessor feature built into every  processor core of IBM Z and
+LinuxONE mainframes (s390x architecture). It provides high-speed,
+hardware-accelerated encryption and hashing directly on the CPU.
+
+CPACF provides a set of z/Architecture instructions (known as Message
+Security Assist or MSA) that execute cryptographic operations
+synchronously with the main processor.
+
+- Symmetric Encryption: Support for AES (128, 192, 256-bit), DES, and
+  Triple-DES (TDES).
+- Hashing: Acceleration for SHA-1, SHA-2 (up to SHA-512), SHA-3 and
+  SHAKE.
+- Random Number Generation: Pseudo Random Number Generator (PRNG) and
+  a hardware-based True Random Number Generator (TRNG).
+- Asymmetric Support: Elliptic Curve Cryptography (ECC) primitives
+  P-256, P-384, P-521, Montgomery/Edwards curves (e.g., Ed25519).
+
+Documentation about CPACF instructions is publicly available and
+can be found in the "z/Architecture Principles of Operation"
+accessible at the IBM documentation hub https://www.ibm.com/docs/en.
+For example the latest version as a pdf is available here:
+https://www.ibm.com/support/pages/zvm/library/other/22783214.pdf
+
+
+CPACF instructions
+------------------
+
+Here is a list of implemented CPACF instructions and the supported
+functions for each instruction:
+
+KDSA (COMPUTE DIGITAL SIGNATURE AUTHENTICATION)
+- Function code 0x00 - Function Query
+
+KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
+- Function code 0x00 - Function Query
+- Function code 0x02 - CPACF_KIMD_SHA_256
+- Function code 0x03 - CPACF_KIMD_SHA_512
+
+KLMD (COMPUTE LAST MESSAGE DIGEST)
+- Function code 0x00 - Function Query
+- Function code 0x02 - CPACF_KLMD_SHA_256
+- Function code 0x03 - CPACF_KLMD_SHA_512
+
+KM (CIPHER MESSAGE)
+- Function code 0x00 - Function Query
+- Function code 0x12 - CPACF_KM_AES_128
+- Function code 0x13 - CPACF_KM_AES_192
+- Function code 0x14 - CPACF_KM_AES_256
+- Function code 0x1a - CPACF_KM_PAES_128
+- Function code 0x1b - CPACF_KM_PAES_192
+- Function code 0x1c - CPACF_KM_PAES_256
+- Function code 0x32 - CPACF_KM_XTS_128
+- Function code 0x34 - CPACF_KM_XTS_256
+- Function code 0x3a - CPACF_KM_PXTS_128
+- Function code 0x3c - CPACF_KM_PXTS_256
+
+KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
+- Function code 0x00 - Function Query
+
+KMC (CIPHER MESSAGE WITH CHAINING)
+- Function code 0x00 - Function Query
+- Function code 0x12 - CPACF_KMC_AES_128
+- Function code 0x13 - CPACF_KMC_AES_192
+- Function code 0x14 - CPACF_KMC_AES_256
+- Function code 0x1a - CPACF_KMC_PAES_128
+- Function code 0x1b - CPACF_KMC_PAES_192
+- Function code 0x1c - CPACF_KMC_PAES_256
+
+KMCTR (CIPHER MESSAGE WITH COUNTER)
+- Function code 0x00 - Function Query
+- Function code 0x12 - CPACF_KMCTR_AES_128
+- Function code 0x13 - CPACF_KMCTR_AES_192
+- Function code 0x14 - CPACF_KMCTR_AES_256
+- Function code 0x1a - CPACF_KMCTR_PAES_128
+- Function code 0x1b - CPACF_KMCTR_PAES_192
+- Function code 0x1c - CPACF_KMCTR_PAES_256
+
+KMF (CIPHER MESSAGE WITH CIPHER FEEDBACK)
+- not supported
+
+KMO (CIPHER MESSAGE WITH OUTPUT FEEDBACK)
+- not supported
+
+PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
+- Function code 0x00 - Function Query
+- Function code 0x32 - compute XTS param AES-128
+- Function code 0x34 - compute XTS param AES-256
+- Function code 0x3a - compute XTS param Encrypted AES-128
+- Function code 0x3c - compute XTS param Encrypted AES-256
+
+PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT OPERATION)
+- Function code 0x00 - Function Query
+- Function code 0x12 - CPACF_PCKMO_ENC_AES_128_KEY
+- Function code 0x13 - CPACF_PCKMO_ENC_AES_192_KEY
+- Function code 0x14 - CPACF_PCKMO_ENC_AES_256_KEY
+
+PRNO (PERFORM RANDOM NUMBER OPERATION)
+- Function code 0x00 - Function Query
+- Function code 0x72 - CPACF_PRNO_TRNG
+
+Note that the use of a not supported CPACF instruction (KMF and KMO)
+or invocation of a not listed function will result in a Specification
+Exception.
+
+Not listed CPACF instructions (KMA, KMF, KMO) cause an Operation
+Exception when used. Not listed functions cause a Specification
+Exception when called. If only the query function is listed (KDSA),
+then the query function will return a function status word with all
+but the query function bit set to 0.
+
+
+Protected key support
+---------------------
+
+The qemu version for protected key support is only a fake provided
+here for developing and testing purpose:
+
+- The protected key is _derived_ from the clear key by xoring the
+  fixed pattern 0xAAAA... onto the key value.
+- The AES Wrapping Key Verification Pattern is a fixed value of 32
+  bytes 0xFACEFACE...
+
+The PCKMO instruction implementation does exactly this - _derive_ a
+_protected_ key from the clear key given by xor 0xAAAA... and writing
+the fixed value for the WKVP of 0xFACEFACE into the blob.
+The other subfunctions of the CPACF instructions dealing with
+protected key treat the protected key blob by first checking for the
+WKVP (against the fixed value of 0xFACEFACE...) and second
+_decrypting_ the key value by xoring 0xAAAA... and then execute the
+clear key operation with the decrypted key value.
+This is suitable for testing purpose but such keys are not for real
+production load and would open up a huge security breach!
+
+For more details about protected keys see the "z/Architecture
+Principles of Operation" document chapter "General Instructions"
+sub-chapter "Protection of Cryptographic Keys" and again the
+implementation here does NOT implement what is explained there.
docs/system/target-s390x.rst +1 -0
index 94c981e732..49159826eb 100644
@@ -35,3 +35,4 @@ Architectural features
    s390x/bootdevices
    s390x/protvirt
    s390x/cpu-topology
+   s390x/cpacf
Signature · 1 line
2.43.0
#15 lore raw

PATCH v10 10/21 target/s390x: Base support for cpacf protected keys

In reply to Harald Freudenberger

Add base support for cpacf protected key handling.

The qemu version provided here is only a fake intended to make protected key available for developing and testing purpose:
* The protected key is 'derived' from the clear key by xoring the fixed pattern 0xAAAA... onto the key value.
* The AES Wrapping Key Verification Pattern is a fixed value of 32 bytes 0xFACEFACE...

Add preprocessor defines for the xor pattern and wkvp used to construct ('encrypt') a protected key from a clear key value with this implementation. Also add some static functions to 'encrypt' from clear key to protected key and 'decrypt' back to cpacf_aes.c.

The preprocessor defines shall be used later in testcases to construct and decode protected keys.

---
 target/s390x/tcg/cpacf.h     | 25 +++++++++++++++++++++++
 target/s390x/tcg/cpacf_aes.c | 39 ++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
target/s390x/tcg/cpacf.h +25 -0
index e585b77766..df7f258443 100644
@@ -254,4 +254,29 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint32_t type, uint8_t fc, uint8_t mod);
 
+/*
+ * Support for protected key cpacf functions. Note that this is
+ * a fake implementation intended for debugging and development.
+ * Do not use for production load !
+ */
+
+/*
+ * Hard coded pattern xored with the AES clear key
+ * to 'produce' the protected key.
+ */
+#define PROTKEY_XOR_PATTERN { \
+    0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, \
+    0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, \
+    0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, \
+    0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }
+
+/*
+ * Hard coded wkvp ("Wrapping Key Verification Pattern")
+ */
+#define PROTKEY_WKVP { \
+    0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E, \
+    0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E, \
+    0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E, \
+    0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E }
+
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +39 -0
index 4a28cc3d7c..6702006b66 100644
@@ -461,3 +461,42 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     return !len ? 0 : 3;
 }
+
+/*
+ * Support for protected key cpacf functions. Note that this is
+ * a fake implementation intended for debugging and development.
+ * Do not use for production load !
+ */
+
+/*
+ * Hard coded pattern xored with the AES clear key
+ * to 'produce' the protected key.
+ */
+static const uint8_t protkey_xor_pattern[32] = PROTKEY_XOR_PATTERN;
+
+/*
+ * Hard coded wkvp ("Wrapping Key Verification Pattern")
+ */
+static const uint8_t protkey_wkvp[32] = PROTKEY_WKVP;
+
+/*
+ * 'encrypt' the clear key value into a protected key
+ * by xor-ing the protkey_xor_pattern onto it.
+ */
+static void encrypt_clrkey(uint8_t *key, int keysize)
+{
+    for (int i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];
+    }
+}
+
+/*
+ * 'decrypt' the protected key by reverting the xor
+ * of the protkey_xor_pattern onto the clear key value.
+ */
+static void decrypt_protkey(uint8_t *key, int keysize)
+{
+    for (int i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];
+    }
+}
Signature · 1 line
2.43.0
#21 lore raw

PATCH v10 12/21 target/s390x: Support protected key AES ECB for cpacf km instruction

In reply to Harald Freudenberger

Support the subfunctions CPACF_KM_PAES_128, CPACF_KM_PAES_192 and CPACF_KM_PAES_256 for the cpacf km instruction.

---
 target/s390x/gen-features.c      |  3 ++
 target/s390x/tcg/cpacf.h         |  4 ++
 target/s390x/tcg/cpacf_aes.c     | 86 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  7 +++
 4 files changed, 100 insertions(+)
target/s390x/gen-features.c +3 -0
index d3e69aaca6..71e0e41d6e 100644
@@ -924,6 +924,9 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KM_AES_128,
     S390_FEAT_KM_AES_192,
     S390_FEAT_KM_AES_256,
+    S390_FEAT_KM_EAES_128,
+    S390_FEAT_KM_EAES_192,
+    S390_FEAT_KM_EAES_256,
     S390_FEAT_KM_XTS_AES_128,
     S390_FEAT_KM_XTS_AES_256,
     S390_FEAT_KMC_AES_128,
target/s390x/tcg/cpacf.h +4 -0
index 7512c0ea4c..cc545fb703 100644
@@ -282,5 +282,9 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 /* from cpacf_aes.c */
 int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                     uint64_t param_addr, uint8_t fc);
+int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                   uint64_t param_addr, uint64_t *dst_ptr_reg,
+                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                   uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +86 -0

89 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index 08151e916f..b00351d8c1 100644
@@ -103,6 +103,13 @@ static int cpacf_km(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
                            S390_FEAT_TYPE_KM, fc, mod);
         break;
+    case CPACF_KM_PAES_128:
+    case CPACF_KM_PAES_192:
+    case CPACF_KM_PAES_256:
+        rc = cpacf_paes_ecb(env, mmu_idx, ra, env->regs[1],
+                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                            S390_FEAT_TYPE_KM, fc, mod);
+        break;
     case CPACF_KM_XTS_128:
     case CPACF_KM_XTS_256:
         rc = cpacf_aes_xts(env, mmu_idx, ra, env->regs[1],
Signature · 1 line
2.43.0
#4 lore raw

PATCH v10 3/21 target/s390x: Move cpacf sha512 code into a new file

In reply to Harald Freudenberger

Move the cpacf sha512 implementation into a new file cpacf_sha512.c. Add this new file to the build and use the cpacf.h header file storing function the prototypes.

---
 target/s390x/tcg/cpacf.h         |   5 +
 target/s390x/tcg/cpacf_sha512.c  | 241 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 222 ----------------------------
 target/s390x/tcg/meson.build     |   1 +
 4 files changed, 247 insertions(+), 222 deletions(-)
 create mode 100644 target/s390x/tcg/cpacf_sha512.c
target/s390x/tcg/cpacf.h +5 -0
index 05596e0645..6de79a2f8f 100644
@@ -223,4 +223,9 @@
 #define CPACF_KDSA_PSIGN_ED25519   48
 #define CPACF_KDSA_PSIGN_ED448     52
 
+/* from cpacf_sha512.c */
+int cpacf_sha512(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                 uint64_t param_addr, uint64_t *message_reg, uint64_t *len_reg,
+                 uint32_t type);
+
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_sha512.c new file +241 -0

241 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +0 -222

228 lines not on this page · raw message

target/s390x/tcg/meson.build +1 -0
index 36cb0e079e..54a87393a3 100644
@@ -5,6 +5,7 @@ s390x_ss.add(when: 'CONFIG_TCG', if_true: files(
 ))
 s390x_common_ss.add(when: 'CONFIG_TCG', if_true: files(
   'cc_helper.c',
+  'cpacf_sha512.c',
   'crypto_helper.c',
   'excp_helper.c',
   'fpu_helper.c',
Signature · 1 line
2.43.0
#24 lore raw

PATCH v10 15/21 target/s390x: Minimal protected key AES XTS support for cpacf pcc instruction

In reply to Harald Freudenberger

Support CPACF pcc subfunctions PCC-Compute-XTS-Parameter-Encrypted-AES-128 and PCC-Compute-XTS-Parameter-Encrypted-AES-128 but only for the special case block sequential number is 0. However, this covers the s390 PAES XTS implementation in the Linux kernel.

---
 target/s390x/gen-features.c      |  2 +
 target/s390x/tcg/cpacf.h         |  2 +
 target/s390x/tcg/cpacf_aes.c     | 77 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  4 ++
 4 files changed, 85 insertions(+)
target/s390x/gen-features.c +2 -0
index 4a131dc191..126bacb281 100644
@@ -943,6 +943,8 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KMCTR_EAES_256,
     S390_FEAT_PCC_XTS_AES_128,
     S390_FEAT_PCC_XTS_AES_256,
+    S390_FEAT_PCC_XTS_EAES_128,
+    S390_FEAT_PCC_XTS_EAES_256,
     S390_FEAT_PCKMO_AES_128,
     S390_FEAT_PCKMO_AES_192,
     S390_FEAT_PCKMO_AES_256,
target/s390x/tcg/cpacf.h +2 -0
index 6071f21fb6..9dc197388e 100644
@@ -295,5 +295,7 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                    uint64_t *ctr_ptr_reg, uint32_t type,
                    uint8_t fc, uint8_t mod);
+int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                   uint64_t param_addr, uint8_t fc);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +77 -0

80 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +4 -0
index 73b2a6557c..5e924b78f5 100644
@@ -206,6 +206,10 @@ static int cpacf_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     case CPACF_PCC_XTS_AES_256:
             rc = cpacf_aes_pcc(env, mmu_idx, ra, env->regs[1], fc);
             break;
+    case CPACF_PCC_XTS_PAES_128:
+    case CPACF_PCC_XTS_PAES_256:
+            rc = cpacf_paes_pcc(env, mmu_idx, ra, env->regs[1], fc);
+            break;
     default:
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
     }
Signature · 1 line
2.43.0
#27 lore raw

PATCH v10 18/21 crypto: Add aes-helpers file to support some AES modes

In reply to Harald Freudenberger

Add a new file crypto/aes-helpers.c with simple functions to support some AES modes:
- AES cbc: AES_cbc_encrypt() AES_cbc_decrypt()
- AES ctr: AES_ctr_encrypt()
- AES xts: AES_xts_encrypt() AES_xts_decrypt() and some AES related helpers:
- AES_xor()
- AES_xts_prep_next_tweak()

---
 crypto/aes-helpers.c | 106 +++++++++++++++++++++++++++++++++++++++++++
 crypto/meson.build   |   1 +
 include/crypto/aes.h |  14 ++++++
 3 files changed, 121 insertions(+)
 create mode 100644 crypto/aes-helpers.c
crypto/aes-helpers.c new file +106 -0
new file mode 100644
index 0000000000..065a7afabf
@@ -0,0 +1,106 @@
+/*
+ * AES helper functions and mode implementations
+ *
+ *  Authors:
+ *   Harald Freudenberger <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include <stdint.h>
+#include <string.h>
+#include "crypto/aes.h"
+
+void AES_xor(const unsigned char *src1, const unsigned char *src2,
+             unsigned char *dst)
+{
+    int i;
+
+    for (i = 0; i < AES_BLOCK_SIZE; i++) {
+        dst[i] = src1[i] ^ src2[i];
+    }
+}
+
+void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
+                     unsigned char *iv, const AES_KEY *key)
+{
+    unsigned char buf[AES_BLOCK_SIZE];
+
+    /* in xor iv => buf */
+    AES_xor(in, iv, buf);
+    /* encrypt buf => out */
+    AES_encrypt(buf, out, key);
+    /* prep iv for next round */
+    memcpy(iv, out, AES_BLOCK_SIZE);
+}
+
+void AES_cbc_decrypt(const unsigned char *in, unsigned char *out,
+                     unsigned char *iv, const AES_KEY *key)
+{
+    unsigned char buf[AES_BLOCK_SIZE];
+
+    /* decrypt in => buf */
+    AES_decrypt(in, buf, key);
+    /* buf xor iv => out */
+    AES_xor(buf, iv, out);
+    /* prep iv for next round */
+    memcpy(iv, in, AES_BLOCK_SIZE);
+}
+
+void AES_ctr_encrypt(const unsigned char *in, unsigned char *out,
+                     const unsigned char *ctr, const AES_KEY *key)
+{
+    unsigned char buf[AES_BLOCK_SIZE];
+
+    /* encrypt ctr => buf */
+    AES_encrypt(ctr, buf, key);
+    /* exor input data with encrypted ctr => out */
+    AES_xor(in, buf, out);
+}
+
+/* Tweak calculation for AES XTS.
+ * Multiply tweak by α (x) in GF(2^128) per IEEE 1619-2007. The tweak
+ * is a 128-bit little-endian integer (tweak[0]=LSB, tweak[15]=MSB).
+ * This implementation has been verified on litte and big endian.
+ */
+void AES_xts_prep_next_tweak(unsigned char *tweak)
+{
+    unsigned char carry;
+    int i;
+
+    carry = tweak[AES_BLOCK_SIZE - 1] >> 7;
+
+    for (i = AES_BLOCK_SIZE - 1; i > 0; i--) {
+        tweak[i] = (unsigned char)((tweak[i] << 1) | (tweak[i - 1] >> 7));
+    }
+
+    tweak[i] = (unsigned char)(tweak[i] << 1);
+    tweak[i] ^= (unsigned char)(0x87 & (unsigned char)(-(unsigned char)carry));
+}
+
+void AES_xts_encrypt(const unsigned char *in, unsigned char *out,
+                     const unsigned char *tweak, const AES_KEY *key)
+{
+    unsigned char buf1[AES_BLOCK_SIZE], buf2[AES_BLOCK_SIZE];
+
+    /* in xor tweak => buf1 */
+    AES_xor(in, tweak, buf1);
+    /* encrypt buf1 => buf2 */
+    AES_encrypt(buf1, buf2, key);
+    /* buf2 xor tweak => out */
+    AES_xor(buf2, tweak, out);
+}
+
+void AES_xts_decrypt(const unsigned char *in, unsigned char *out,
+                     const unsigned char *tweak, const AES_KEY *key)
+{
+    unsigned char buf1[AES_BLOCK_SIZE], buf2[AES_BLOCK_SIZE];
+
+    /* in xor tweak => buf1 */
+    AES_xor(in, tweak, buf1);
+    /* encrypt buf1 => buf2 */
+    AES_decrypt(buf1, buf2, key);
+    /* buf2 xor tweak => out */
+    AES_xor(buf2, tweak, out);
+}
crypto/meson.build +1 -0
index b51597a879..675f27311c 100644
@@ -55,6 +55,7 @@ system_ss.add(when: gnutls, if_true: files('tls-cipher-suites.c'))
 
 util_ss.add(files(
   'aes.c',
+  'aes-helpers.c',
   'clmul.c',
   'init.c',
   'sm4.c',
include/crypto/aes.h +14 -0
index 381f24c902..df6239cb9c 100644
@@ -37,4 +37,18 @@ AES_Td0[x] = Si[x].[0e, 09, 0d, 0b];
 
 extern const uint32_t AES_Te0[256], AES_Td0[256];
 
+void AES_xor(const unsigned char *src1, const unsigned char *src2,
+             unsigned char *dst);
+void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
+                     unsigned char *iv, const AES_KEY *key);
+void AES_cbc_decrypt(const unsigned char *in, unsigned char *out,
+                     unsigned char *iv, const AES_KEY *key);
+void  AES_ctr_encrypt(const unsigned char *in, unsigned char *out,
+                      const unsigned char *ctr, const AES_KEY *key);
+void AES_xts_prep_next_tweak(unsigned char *tweak);
+void AES_xts_encrypt(const unsigned char *in, unsigned char *out,
+                     const unsigned char *tweak, const AES_KEY *key);
+void AES_xts_decrypt(const unsigned char *in, unsigned char *out,
+                     const unsigned char *tweak, const AES_KEY *key);
+
 #endif
Signature · 1 line
2.43.0
#13 lore raw

PATCH v10 9/21 target/s390x: Support AES XTS for cpacf km instruction

In reply to Harald Freudenberger

Support the subfunctions XTS-AES-128 and XTS-AES-256 for the cpacf km instruction.

---
 target/s390x/gen-features.c      |   2 +
 target/s390x/tcg/cpacf.h         |   4 ++
 target/s390x/tcg/cpacf_aes.c     | 108 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   6 ++
 4 files changed, 120 insertions(+)
target/s390x/gen-features.c +2 -0
index 1b6a874b90..f9b1a40c7c 100644
@@ -924,6 +924,8 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KM_AES_128,
     S390_FEAT_KM_AES_192,
     S390_FEAT_KM_AES_256,
+    S390_FEAT_KM_XTS_AES_128,
+    S390_FEAT_KM_XTS_AES_256,
     S390_FEAT_KMC_AES_128,
     S390_FEAT_KMC_AES_192,
     S390_FEAT_KMC_AES_256,
target/s390x/tcg/cpacf.h +4 -0
index 2e8ed72758..e585b77766 100644
@@ -249,5 +249,9 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint8_t fc, uint8_t mod);
 int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t param_addr, uint8_t fc);
+int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                  uint64_t param_addr, uint64_t *dst_ptr_reg,
+                  uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                  uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +108 -0

111 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +6 -0
index 1d447cef30..564f7fa243 100644
@@ -103,6 +103,12 @@ static int cpacf_km(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
                            S390_FEAT_TYPE_KM, fc, mod);
         break;
+    case CPACF_KM_XTS_128:
+    case CPACF_KM_XTS_256:
+        rc = cpacf_aes_xts(env, mmu_idx, ra, env->regs[1],
+                           &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                           S390_FEAT_TYPE_KM, fc, mod);
+        break;
     default:
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
     }
Signature · 1 line
2.43.0
#30 lore raw

PATCH v10 21/21 tests/tcg/s390x: Add tests for CPACF instructions

In reply to Harald Freudenberger

Add simple tests for the CPACF instructions implemented:
- kdsa - minimal as only query is implemented
- kimd - query, sha256, sha512
- klmd - query, sha256, sha512
- km - query, aes 128, 192, 256 with clear and prot key
- kmac - minimal as only query is implemented
- kmc - query, aes 128, 192, 256 with clear and prot key
- kmctr - query, aes 128, 192, 256 with clear and prot key
- pcc - query, xts aes 128, 256 and prot key xts aes 128, 256
- prno - query, trng
No test for pckmo as this is a privileged instruction. No test for kma, kmf, kmo as these instructions are currently not implemented at all.

---
 target/s390x/tcg/cpacf.h        |   7 +
 tests/tcg/s390x/Makefile.target |   9 +
 tests/tcg/s390x/cpacf-kdsa.c    |  59 ++++
 tests/tcg/s390x/cpacf-kimd.c    | 164 +++++++++
 tests/tcg/s390x/cpacf-klmd.c    | 202 +++++++++++
 tests/tcg/s390x/cpacf-km.c      | 576 ++++++++++++++++++++++++++++++++
 tests/tcg/s390x/cpacf-kmac.c    |  59 ++++
 tests/tcg/s390x/cpacf-kmc.c     | 342 +++++++++++++++++++
 tests/tcg/s390x/cpacf-kmctr.c   | 354 ++++++++++++++++++++
 tests/tcg/s390x/cpacf-pcc.c     | 241 +++++++++++++
 tests/tcg/s390x/cpacf-prno.c    | 130 +++++++
 tests/tcg/s390x/cpacf.h         | 570 +++++++++++++++++++++++++++++++
 12 files changed, 2713 insertions(+)
 create mode 100644 tests/tcg/s390x/cpacf-kdsa.c
 create mode 100644 tests/tcg/s390x/cpacf-kimd.c
 create mode 100644 tests/tcg/s390x/cpacf-klmd.c
 create mode 100644 tests/tcg/s390x/cpacf-km.c
 create mode 100644 tests/tcg/s390x/cpacf-kmac.c
 create mode 100644 tests/tcg/s390x/cpacf-kmc.c
 create mode 100644 tests/tcg/s390x/cpacf-kmctr.c
 create mode 100644 tests/tcg/s390x/cpacf-pcc.c
 create mode 100644 tests/tcg/s390x/cpacf-prno.c
 create mode 100644 tests/tcg/s390x/cpacf.h
target/s390x/tcg/cpacf.h +7 -0
index b2223b4d64..685fd773c2 100644
@@ -223,6 +223,8 @@
 #define CPACF_KDSA_PSIGN_ED25519   48
 #define CPACF_KDSA_PSIGN_ED448     52
 
+#ifndef CPACF_H_INCLUDE_FOR_TESTS
+
 /* from cpacf_sha256.c */
 int cpacf_sha256(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                  uint64_t param_addr, uint64_t *message_reg, uint64_t *len_reg,
@@ -254,6 +256,8 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint32_t type, uint8_t fc, uint8_t mod);
 
+#endif /* CPACF_H_INCLUDE_FOR_TESTS */
+
 /*
  * Support for protected key cpacf functions. Note that this is
  * a fake implementation intended for debugging and development.
@@ -279,6 +283,8 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E, \
     0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E }
 
+#ifndef CPACF_H_INCLUDE_FOR_TESTS
+
 /* from cpacf_aes.c */
 int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                     uint64_t param_addr, uint8_t fc);
@@ -302,4 +308,5 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                    uint32_t type, uint8_t fc, uint8_t mod);
 
+#endif /* CPACF_H_INCLUDE_FOR_TESTS */
 #endif /* S390X_CPACF_H */
tests/tcg/s390x/Makefile.target +9 -0
index 0ca030ded0..68e6a1816d 100644
@@ -50,6 +50,15 @@ TESTS+=cvb
 TESTS+=ts
 TESTS+=ex-smc
 TESTS+=divide-to-integer
+TESTS+=cpacf-kdsa
+TESTS+=cpacf-kimd
+TESTS+=cpacf-klmd
+TESTS+=cpacf-km
+TESTS+=cpacf-kmac
+TESTS+=cpacf-kmc
+TESTS+=cpacf-kmctr
+TESTS+=cpacf-pcc
+TESTS+=cpacf-prno
 
 cdsg: CFLAGS+=-pthread
 cdsg: LDFLAGS+=-pthread
tests/tcg/s390x/cpacf-kdsa.c new file +59 -0

59 lines not on this page · raw message

tests/tcg/s390x/cpacf-kimd.c new file +164 -0

164 lines not on this page · raw message

tests/tcg/s390x/cpacf-klmd.c new file +202 -0

202 lines not on this page · raw message

tests/tcg/s390x/cpacf-km.c new file +576 -0

576 lines not on this page · raw message

tests/tcg/s390x/cpacf-kmac.c new file +59 -0

59 lines not on this page · raw message

tests/tcg/s390x/cpacf-kmc.c new file +342 -0

342 lines not on this page · raw message

tests/tcg/s390x/cpacf-kmctr.c new file +354 -0

354 lines not on this page · raw message

tests/tcg/s390x/cpacf-pcc.c new file +241 -0

241 lines not on this page · raw message

tests/tcg/s390x/cpacf-prno.c new file +130 -0

130 lines not on this page · raw message

tests/tcg/s390x/cpacf.h new file +570 -0

570 lines not on this page · raw message

Signature · 1 line
2.43.0
#20 lore raw

PATCH v10 11/21 target/s390x: Support pckmo encrypt AES subfunctions

In reply to Harald Freudenberger

Support the subfuctions PCKMO-Encrypt-AES-128-Key, PCKMO-Encrypt-AES-192-Key and PCKMO-Encrypt-AES-256-Key.

These subfunctions derive a protected key from an AES clear key by encrypting it with an internal AES wrapping key. More details can be found in the "z/Architecture Prinziples of Operation" document.

---
 target/s390x/gen-features.c      |  3 +++
 target/s390x/tcg/cpacf.h         |  4 +++
 target/s390x/tcg/cpacf_aes.c     | 45 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 21 +++++++++++++++
 target/s390x/tcg/translate.c     |  9 +++++--
 5 files changed, 80 insertions(+), 2 deletions(-)
target/s390x/gen-features.c +3 -0
index f9b1a40c7c..d3e69aaca6 100644
@@ -934,6 +934,9 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KMCTR_AES_256,
     S390_FEAT_PCC_XTS_AES_128,
     S390_FEAT_PCC_XTS_AES_256,
+    S390_FEAT_PCKMO_AES_128,
+    S390_FEAT_PCKMO_AES_192,
+    S390_FEAT_PCKMO_AES_256,
 };
 
 /****** END FEATURE DEFS ******/
target/s390x/tcg/cpacf.h +4 -0
index df7f258443..7512c0ea4c 100644
@@ -279,4 +279,8 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E, \
     0x0F, 0x0A, 0x0C, 0x0E, 0x0F, 0x0A, 0x0C, 0x0E }
 
+/* from cpacf_aes.c */
+int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                    uint64_t param_addr, uint8_t fc);
+
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +45 -0

48 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +21 -0
index 564f7fa243..08151e916f 100644
@@ -192,6 +192,24 @@ static int cpacf_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     return rc;
 }
 
+static int cpacf_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                       uint8_t fc)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case CPACF_PCKMO_ENC_AES_128_KEY:
+    case CPACF_PCKMO_ENC_AES_192_KEY:
+    case CPACF_PCKMO_ENC_AES_256_KEY:
+        rc = cpacf_aes_pckmo(env, mmu_idx, ra, env->regs[1], fc);
+        break;
+    default:
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+    }
+
+    return rc;
+}
+
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
                      uint32_t type)
 {
@@ -251,6 +269,9 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     case S390_FEAT_TYPE_PCC:
         rc = cpacf_pcc(env, mmu_idx, ra, fc);
         break;
+    case S390_FEAT_TYPE_PCKMO:
+        rc = cpacf_pckmo(env, mmu_idx, ra, fc);
+        break;
     case S390_FEAT_TYPE_PPNO:
         rc = cpacf_ppno(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
target/s390x/tcg/translate.c +7 -2
index cef1b55149..d7a99e6c1e 100644
@@ -2558,6 +2558,7 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
     int r2 = have_field(s, r2) ? get_field(s, r2) : 0;
     int r3 = have_field(s, r3) ? get_field(s, r3) : 0;
     TCGv_i32 t_r1, t_r2, t_r3, type;
+    bool update_cc = true;
 
     switch (s->insn->data) {
     case S390_FEAT_TYPE_KMA:
@@ -2589,8 +2590,10 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
             gen_program_exception(s, PGM_SPECIFICATION);
             return DISAS_NORETURN;
         }
-        /* FALL THROUGH */
+        break;
     case S390_FEAT_TYPE_PCKMO:
+        update_cc = false;
+        /* FALL THROUGH */
     case S390_FEAT_TYPE_PCC:
     case S390_FEAT_TYPE_KDSA:
         break;
@@ -2603,7 +2606,9 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
     t_r3 = tcg_constant_i32(r3);
     type = tcg_constant_i32(s->insn->data);
     gen_helper_msa(cc_op, tcg_env, t_r1, t_r2, t_r3, type);
-    set_cc_static(s);
+    if (update_cc) {
+        set_cc_static(s);
+    }
     return DISAS_NEXT;
 }
 
Signature · 1 line
2.43.0
#25 lore raw

PATCH v10 16/21 target/s390x: Support protected key AES XTS for cpacf km instruction

In reply to Harald Freudenberger

Support the subfunctions CPACF_KM_PXTS_128 and CPACF_KM_PXTS_256 for the cpacf km instruction.

---
 target/s390x/gen-features.c      |   2 +
 target/s390x/tcg/cpacf.h         |   4 ++
 target/s390x/tcg/cpacf_aes.c     | 105 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   6 ++
 4 files changed, 117 insertions(+)
target/s390x/gen-features.c +2 -0
index 126bacb281..c4c59c3504 100644
@@ -929,6 +929,8 @@ static uint16_t qemu_MAX[] = {
     S390_FEAT_KM_EAES_256,
     S390_FEAT_KM_XTS_AES_128,
     S390_FEAT_KM_XTS_AES_256,
+    S390_FEAT_KM_XTS_EAES_128,
+    S390_FEAT_KM_XTS_EAES_256,
     S390_FEAT_KMC_AES_128,
     S390_FEAT_KMC_AES_192,
     S390_FEAT_KMC_AES_256,
target/s390x/tcg/cpacf.h +4 -0
index 9dc197388e..b2223b4d64 100644
@@ -297,5 +297,9 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint8_t fc, uint8_t mod);
 int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint64_t param_addr, uint8_t fc);
+int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                   uint64_t param_addr, uint64_t *dst_ptr_reg,
+                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
+                   uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif /* S390X_CPACF_H */
target/s390x/tcg/cpacf_aes.c +105 -0

108 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +6 -0
index 5e924b78f5..6172012979 100644
@@ -116,6 +116,12 @@ static int cpacf_km(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
                            S390_FEAT_TYPE_KM, fc, mod);
         break;
+    case CPACF_KM_PXTS_128:
+    case CPACF_KM_PXTS_256:
+        rc = cpacf_paes_xts(env, mmu_idx, ra, env->regs[1],
+                            &env->regs[r1], &env->regs[r2], &env->regs[r2 + 1],
+                            S390_FEAT_TYPE_KM, fc, mod);
+        break;
     default:
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
     }
Signature · 1 line
2.43.0
#28 lore raw

PATCH v10 19/21 target/s390x: Use generic AES helper functions

In reply to Harald Freudenberger

Rewrite the cpacf implementations to use the generic AES helper functions from crypto/aes-helpers.c

---
 target/s390x/tcg/cpacf_aes.c | 124 ++++++++++-------------------------
 1 file changed, 36 insertions(+), 88 deletions(-)
target/s390x/tcg/cpacf_aes.c +36 -88
index 0c3cbb29e3..4298a0eac6 100644
@@ -111,20 +111,13 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     return !len ? 0 : 3;
 }
 
-static void aes_xor(const uint8_t *src1, const uint8_t *src2, uint8_t *dst)
-{
-    for (int i = 0; i < AES_BLOCK_SIZE; i++) {
-        dst[i] = src1[i] ^ src2[i];
-    }
-}
-
 int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t param_addr, uint64_t *dst_ptr_reg,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint32_t type, uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
+    uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint64_t addr, len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
@@ -180,19 +173,11 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
         if (mod) {
-            /* decrypt in => buf */
-            AES_decrypt(in, buf, &exkey);
-            /* buf xor iv => out */
-            aes_xor(buf, iv, out);
-            /* prep iv for next round */
-            memcpy(iv, in, AES_BLOCK_SIZE);
+            /* decrypt in => out */
+            AES_cbc_decrypt(in, out, iv, &exkey);
         } else {
-            /* in xor iv => buf */
-            aes_xor(in, iv, buf);
-            /* encrypt buf => out */
-            AES_encrypt(buf, out, &exkey);
-            /* prep iv for next round */
-            memcpy(iv, out, AES_BLOCK_SIZE);
+            /* encrypt in => out */
+            AES_cbc_encrypt(in, out, iv, &exkey);
         }
         aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
         len -= AES_BLOCK_SIZE;
@@ -222,11 +207,10 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
-    uint8_t ctr[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
     uint64_t addr, len = *src_len_reg, done = 0;
+    uint8_t ctr[AES_BLOCK_SIZE], key[32];
     int i, keysize, addr_reg_size = 64;
-    uint8_t key[32];
     AES_KEY exkey;
 
     g_assert(type == S390_FEAT_TYPE_KMCTR);
@@ -268,12 +252,10 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         /* read in nonce/ctr => ctr */
         aes_read_block(env, mmu_idx, *ctr_ptr_reg + done, ctr, ra);
-        /* encrypt ctr => buf */
-        AES_encrypt(ctr, buf, &exkey);
         /* read in one block of input data => in */
         aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
-        /* xor input data with encrypted ctr => out */
-        aes_xor(in, buf, out);
+        /* encrypt ctr and xor with in => out */
+        AES_ctr_encrypt(in, out, ctr, &exkey);
         /* write out the processed block */
         aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
         len -= AES_BLOCK_SIZE;
@@ -354,28 +336,13 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     return 0;
 }
 
-static void aes_xts_prep_next_tweak(uint8_t tweak[AES_BLOCK_SIZE])
-{
-    uint8_t carry;
-    int i;
-
-    carry = tweak[AES_BLOCK_SIZE - 1] >> 7;
-
-    for (i = AES_BLOCK_SIZE - 1; i > 0; i--) {
-        tweak[i] = (uint8_t)((tweak[i] << 1) | (tweak[i - 1] >> 7));
-    }
-
-    tweak[i] = (uint8_t)(tweak[i] << 1);
-    tweak[i] ^= (uint8_t)(0x87 & (uint8_t)(-(int8_t)carry));
-}
-
 int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t param_addr, uint64_t *dst_ptr_reg,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint32_t type, uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    uint8_t buf1[AES_BLOCK_SIZE], buf2[AES_BLOCK_SIZE];
+    uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint64_t addr, len = *src_len_reg, done = 0;
     uint8_t key[32], tweak[AES_BLOCK_SIZE];
@@ -426,23 +393,19 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
-        /* fetch one AES block into buf1  */
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, buf1, ra);
-        /* buf1 xor tweak => buf2 */
-        aes_xor(buf1, tweak, buf2);
+        /* fetch one AES block into in  */
+        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
         if (mod) {
-            /* decrypt buf2 => buf1 */
-            AES_decrypt(buf2, buf1, &exkey);
+            /* decrypt in => out */
+            AES_xts_decrypt(in, out, tweak, &exkey);
         } else {
-            /* encrypt buf2 => buf1 */
-            AES_encrypt(buf2, buf1, &exkey);
+            /* encrypt in => out */
+            AES_xts_encrypt(in, out, tweak, &exkey);
         }
-        /* buf1 xor tweak => buf2 */
-        aes_xor(buf1, tweak, buf2);
         /* prep tweak for next round */
-        aes_xts_prep_next_tweak(tweak);
-        /* write out this processed block from buf2 */
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, buf2, ra);
+        AES_xts_prep_next_tweak(tweak);
+        /* write out this processed block from out */
+        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
@@ -638,7 +601,7 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint32_t type, uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
+    uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t key[32], wkvp[32], iv[AES_BLOCK_SIZE];
     uint64_t addr, len = *src_len_reg, done = 0;
@@ -706,19 +669,11 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
         if (mod) {
-            /* decrypt in => buf */
-            AES_decrypt(in, buf, &exkey);
-            /* buf xor iv => out */
-            aes_xor(buf, iv, out);
-            /* prep iv for next round */
-            memcpy(iv, in, AES_BLOCK_SIZE);
+            /* decrypt in => out */
+            AES_cbc_decrypt(in, out, iv, &exkey);
         } else {
-            /* in xor iv => buf */
-            aes_xor(in, iv, buf);
-            /* encrypt buf => out */
-            AES_encrypt(buf, out, &exkey);
-            /* prep iv for next round */
-            memcpy(iv, out, AES_BLOCK_SIZE);
+            /* encrypt in => out */
+            AES_cbc_encrypt(in, out, iv, &exkey);
         }
         aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
         len -= AES_BLOCK_SIZE;
@@ -748,11 +703,10 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
-    uint8_t ctr[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
+    uint8_t ctr[AES_BLOCK_SIZE], key[32], wkvp[32];
     uint64_t addr, len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
-    uint8_t key[32], wkvp[32];
     AES_KEY exkey;
 
     g_assert(type == S390_FEAT_TYPE_KMCTR);
@@ -806,12 +760,10 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         /* read in nonce/ctr => ctr */
         aes_read_block(env, mmu_idx, *ctr_ptr_reg + done, ctr, ra);
-        /* encrypt ctr => buf */
-        AES_encrypt(ctr, buf, &exkey);
         /* read in one block of input data => in */
         aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
-        /* exor input data with encrypted ctr => out */
-        aes_xor(in, buf, out);
+        /* encrypt ctr and xor with in => out */
+        AES_ctr_encrypt(in, out, ctr, &exkey);
         /* write out the processed block */
         aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
         len -= AES_BLOCK_SIZE;
@@ -912,7 +864,7 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint32_t type, uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    uint8_t buf1[AES_BLOCK_SIZE], buf2[AES_BLOCK_SIZE];
+    uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t key[32], wkvp[32], tweak[AES_BLOCK_SIZE];
     uint64_t addr, len = *src_len_reg, done = 0;
@@ -975,23 +927,19 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
-        /* fetch one AES block into buf1  */
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, buf1, ra);
-        /* buf1 xor tweak => buf2 */
-        aes_xor(buf1, tweak, buf2);
+        /* fetch one AES block into in */
+        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
         if (mod) {
-            /* decrypt buf2 => buf1 */
-            AES_decrypt(buf2, buf1, &exkey);
+            /* decrypt in => out */
+            AES_xts_decrypt(in, out, tweak, &exkey);
         } else {
-            /* encrypt buf2 => buf1 */
-            AES_encrypt(buf2, buf1, &exkey);
+            /* encrypt in => out */
+            AES_xts_encrypt(in, out, tweak, &exkey);
         }
-        /* buf1 xor tweak => buf2 */
-        aes_xor(buf1, tweak, buf2);
         /* prep tweak for next round */
-        aes_xts_prep_next_tweak(tweak);
-        /* write out this processed block from buf2 */
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, buf2, ra);
+        AES_xts_prep_next_tweak(tweak);
+        /* write out this processed block from out */
+        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
Signature · 1 line
2.43.0
#29 lore raw

PATCH v10 20/21 target/s390x: Improve fetch and store mem from and to guest

In reply to Harald Freudenberger

Introduce helper functions:
* copy_from_guest_wrap()
* copy_to_guest_wrap()
for copy some memory from guest into a local buffer and the reverse direction. Rework the other functions to use these helpers. By doing so some local variables could be removed also and the code is better readable now.

---
 target/s390x/tcg/cpacf_aes.c | 305 ++++++++++++++---------------------
 1 file changed, 124 insertions(+), 181 deletions(-)
target/s390x/tcg/cpacf_aes.c +124 -181
index 4298a0eac6..15124f3274 100644
@@ -16,37 +16,64 @@
 #include "crypto/aes.h"
 #include "target/s390x/tcg/cpacf.h"
 
-static void aes_read_block(CPUS390XState *env, const int mmu_idx,
-                           uint64_t addr, uint8_t *a, uintptr_t ra)
+/*
+ * helper function to copy some memory from guest to a local buffer
+ */
+static inline void copy_from_guest_wrap(CPUS390XState *env, const int mmu_idx,
+                                        const uintptr_t ra, uint64_t guest_addr,
+                                        uint8_t *dest, size_t len)
 {
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
 
-    for (int i = 0; i < AES_BLOCK_SIZE; i++, addr += 1) {
-        uint64_t _addr = wrap_address(env, addr);
-        a[i] = cpu_ldb_mmu(env, _addr, oi, ra);
+    for (size_t i = 0; i < len; i++, guest_addr++) {
+        uint64_t waddr = wrap_address(env, guest_addr);
+        dest[i] = cpu_ldb_mmu(env, waddr, oi, ra);
     }
 }
 
-static void aes_write_block(CPUS390XState *env, const int mmu_idx,
-                            uint64_t addr, uint8_t *a, uintptr_t ra)
+/*
+ * helper function to copy from a local buffer to guest memory
+ */
+static inline void copy_to_guest_wrap(CPUS390XState *env, const int mmu_idx,
+                                      const uintptr_t ra, uint64_t guest_addr,
+                                      const uint8_t *src, size_t len)
 {
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
 
-    for (int i = 0; i < AES_BLOCK_SIZE; i++, addr += 1) {
-        uint64_t _addr = wrap_address(env, addr);
-        cpu_stb_mmu(env, _addr, a[i], oi, ra);
+    for (size_t i = 0; i < len; i++, guest_addr++) {
+        uint64_t waddr = wrap_address(env, guest_addr);
+        cpu_stb_mmu(env, waddr, src[i], oi, ra);
     }
 }
 
+/*
+ * read exactly one AES block from guest memory into a local buffer
+ */
+static inline void aes_read_block(CPUS390XState *env, const int mmu_idx,
+                                  const uintptr_t ra, uint64_t guest_addr,
+                                  uint8_t *buf)
+{
+    copy_from_guest_wrap(env, mmu_idx, ra, guest_addr, buf, AES_BLOCK_SIZE);
+}
+
+/*
+ * write exactly one AES block from local buffer to guest memory
+ */
+static void aes_write_block(CPUS390XState *env, const int mmu_idx,
+                            const uintptr_t ra, uint64_t guest_addr,
+                            uint8_t *buf)
+{
+    copy_to_guest_wrap(env, mmu_idx, ra, guest_addr, buf, AES_BLOCK_SIZE);
+}
+
 int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t param_addr, uint64_t *dst_ptr_reg,
                   uint64_t *src_ptr_reg, uint64_t *src_len_reg,
                   uint32_t type, uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
     uint8_t key[32];
     AES_KEY exkey;
@@ -77,10 +104,7 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
 
     /* expand key */
     if (mod) {
@@ -91,13 +115,13 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         if (mod) {
             AES_decrypt(in, out, &exkey);
         } else {
             AES_encrypt(in, out, &exkey);
         }
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
@@ -118,8 +142,7 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
     uint8_t key[32], iv[AES_BLOCK_SIZE];
     AES_KEY exkey;
@@ -151,16 +174,11 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch iv from param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + i);
-        iv[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE);
 
     /* fetch key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + AES_BLOCK_SIZE + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + AES_BLOCK_SIZE, key, keysize);
 
     /* expand key */
     if (mod) {
@@ -171,7 +189,7 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         if (mod) {
             /* decrypt in => out */
             AES_cbc_decrypt(in, out, iv, &exkey);
@@ -179,16 +197,13 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
             /* encrypt in => out */
             AES_cbc_encrypt(in, out, iv, &exkey);
         }
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
 
     /* update iv in param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + i);
-        cpu_stb_mmu(env, addr, iv[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE);
 
     *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
                              *src_ptr_reg + done);
@@ -206,9 +221,8 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     uint8_t ctr[AES_BLOCK_SIZE], key[32];
     int i, keysize, addr_reg_size = 64;
     AES_KEY exkey;
@@ -240,10 +254,7 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
 
     /* expand key */
     AES_set_encrypt_key(key, keysize * 8, &exkey);
@@ -251,13 +262,13 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         /* read in nonce/ctr => ctr */
-        aes_read_block(env, mmu_idx, *ctr_ptr_reg + done, ctr, ra);
+        aes_read_block(env, mmu_idx, ra, *ctr_ptr_reg + done, ctr);
         /* read in one block of input data => in */
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         /* encrypt ctr and xor with in => out */
         AES_ctr_encrypt(in, out, ctr, &exkey);
         /* write out the processed block */
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
@@ -277,9 +288,7 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint64_t param_addr, uint8_t fc)
 {
     uint8_t key[32], tweak[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     int keysize, i;
-    uint64_t addr;
     AES_KEY exkey;
 
     switch (fc) {
@@ -294,10 +303,9 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch block sequence nr from param block into buf */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + AES_BLOCK_SIZE + i);
-        buf[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize + AES_BLOCK_SIZE,
+                         buf, AES_BLOCK_SIZE);
 
     /* is the block sequence nr 0 ? */
     for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++) {
@@ -310,16 +318,11 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
 
     /* fetch tweak from param block into tweak */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        tweak[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize, tweak, AES_BLOCK_SIZE);
 
     /* expand key */
     AES_set_encrypt_key(key, keysize * 8, &exkey);
@@ -328,10 +331,9 @@ int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     AES_encrypt(tweak, buf, &exkey);
 
     /* store encrypted tweak into xts parameter field of the param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + 3 * AES_BLOCK_SIZE + i);
-        cpu_stb_mmu(env, addr, buf[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra,
+                       param_addr + keysize + 3 * AES_BLOCK_SIZE,
+                       buf, AES_BLOCK_SIZE);
 
     return 0;
 }
@@ -343,8 +345,7 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     uint8_t key[32], tweak[AES_BLOCK_SIZE];
     int i, keysize, addr_reg_size = 64;
     AES_KEY exkey;
@@ -373,10 +374,7 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
 
     /* expand key */
     if (mod) {
@@ -386,15 +384,13 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch tweak from param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        tweak[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize, tweak, AES_BLOCK_SIZE);
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         /* fetch one AES block into in  */
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         if (mod) {
             /* decrypt in => out */
             AES_xts_decrypt(in, out, tweak, &exkey);
@@ -405,16 +401,14 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
         /* prep tweak for next round */
         AES_xts_prep_next_tweak(tweak);
         /* write out this processed block from out */
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
 
     /* update tweak in param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        cpu_stb_mmu(env, addr, tweak[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra,
+                       param_addr + keysize, tweak, AES_BLOCK_SIZE);
 
     *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
                              *src_ptr_reg + done);
@@ -467,10 +461,8 @@ static void decrypt_protkey(uint8_t *key, int keysize)
 int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                     uint64_t param_addr, uint8_t fc)
 {
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t key[32];
-    int keysize, i;
-    uint64_t addr;
+    int keysize;
 
     switch (fc) {
     case CPACF_PCKMO_ENC_AES_128_KEY:
@@ -487,24 +479,17 @@ int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
 
     /* 'derive' the protected key from the clear key */
     encrypt_clrkey(key, keysize);
 
     /* store the protected key into param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        cpu_stb_mmu(env, addr, key[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
     /* followed by the fake wkvp */
-    for (i = 0; i < sizeof(protkey_wkvp); i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        cpu_stb_mmu(env, addr, protkey_wkvp[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra,
+                       param_addr + keysize,
+                       protkey_wkvp, sizeof(protkey_wkvp));
 
     return 0;
 }
@@ -515,9 +500,8 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint32_t type, uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
     uint8_t key[32], wkvp[32];
     AES_KEY exkey;
@@ -549,20 +533,15 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch and check wkvp from param block */
-    for (i = 0; i < sizeof(wkvp); i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize, wkvp, sizeof(wkvp));
     if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) {
         /* wkvp mismatch -> return with cc 1 */
         return 1;
     }
 
     /* fetch protected key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
     /* decrypt the protected key */
     decrypt_protkey(key, keysize);
 
@@ -575,13 +554,13 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         if (mod) {
             AES_decrypt(in, out, &exkey);
         } else {
             AES_encrypt(in, out, &exkey);
         }
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
@@ -602,9 +581,8 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t key[32], wkvp[32], iv[AES_BLOCK_SIZE];
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
     AES_KEY exkey;
 
@@ -635,26 +613,20 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch and check wkvp from param block */
-    for (i = 0; i < sizeof(wkvp); i++) {
-        addr = wrap_address(env, param_addr + AES_BLOCK_SIZE + keysize + i);
-        wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + AES_BLOCK_SIZE + keysize,
+                         wkvp, sizeof(wkvp));
     if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) {
         /* wkvp mismatch -> return with cc 1 */
         return 1;
     }
 
     /* fetch iv from param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + i);
-        iv[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE);
 
     /* fetch protected key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + AES_BLOCK_SIZE + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + AES_BLOCK_SIZE, key, keysize);
     /* decrypt the protected key */
     decrypt_protkey(key, keysize);
 
@@ -667,7 +639,7 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         if (mod) {
             /* decrypt in => out */
             AES_cbc_decrypt(in, out, iv, &exkey);
@@ -675,16 +647,13 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
             /* encrypt in => out */
             AES_cbc_encrypt(in, out, iv, &exkey);
         }
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
 
     /* update iv in param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + i);
-        cpu_stb_mmu(env, addr, iv[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra, param_addr, iv, AES_BLOCK_SIZE);
 
     *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
                              *src_ptr_reg + done);
@@ -702,10 +671,9 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint8_t fc, uint8_t mod)
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
     uint8_t ctr[AES_BLOCK_SIZE], key[32], wkvp[32];
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
     AES_KEY exkey;
 
@@ -736,20 +704,15 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch and check wkvp from param block */
-    for (i = 0; i < sizeof(wkvp); i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize, wkvp, sizeof(wkvp));
     if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) {
         /* wkvp mismatch -> return with cc 1 */
         return 1;
     }
 
     /* fetch protected key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
     /* decrypt the protected key */
     decrypt_protkey(key, keysize);
 
@@ -759,13 +722,13 @@ int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         /* read in nonce/ctr => ctr */
-        aes_read_block(env, mmu_idx, *ctr_ptr_reg + done, ctr, ra);
+        aes_read_block(env, mmu_idx, ra, *ctr_ptr_reg + done, ctr);
         /* read in one block of input data => in */
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         /* encrypt ctr and xor with in => out */
         AES_ctr_encrypt(in, out, ctr, &exkey);
         /* write out the processed block */
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
@@ -785,9 +748,7 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                    uint64_t param_addr, uint8_t fc)
 {
     uint8_t key[32], wkvp[32], tweak[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     int keysize, i;
-    uint64_t addr;
     AES_KEY exkey;
 
     switch (fc) {
@@ -802,21 +763,17 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch and check wkvp from param block */
-    for (i = 0; i < sizeof(wkvp); i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize, wkvp, sizeof(wkvp));
     if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) {
         /* wkvp mismatch -> return with cc 1 */
         return 1;
     }
 
     /* fetch block sequence nr from param block into buf */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize +
-                            sizeof(wkvp) + AES_BLOCK_SIZE + i);
-        buf[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize + sizeof(wkvp) + AES_BLOCK_SIZE,
+                         buf, AES_BLOCK_SIZE);
 
     /* is the block sequence nr 0 ? */
     for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++) {
@@ -829,18 +786,14 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch protected key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
     /* decrypt the protected key */
     decrypt_protkey(key, keysize);
 
     /* fetch tweak from param block into tweak */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + sizeof(wkvp) + i);
-        tweak[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize + sizeof(wkvp),
+                         tweak, AES_BLOCK_SIZE);
 
     /* expand key */
     AES_set_encrypt_key(key, keysize * 8, &exkey);
@@ -849,11 +802,9 @@ int cpacf_paes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     AES_encrypt(tweak, buf, &exkey);
 
     /* store encrypted tweak into xts parameter field of the param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize +
-                            sizeof(wkvp) + 3 * AES_BLOCK_SIZE + i);
-        cpu_stb_mmu(env, addr, buf[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra,
+                       param_addr + keysize + sizeof(wkvp) + 3 * AES_BLOCK_SIZE,
+                       buf, AES_BLOCK_SIZE);
 
     return 0;
 }
@@ -865,9 +816,8 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t key[32], wkvp[32], tweak[AES_BLOCK_SIZE];
-    uint64_t addr, len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg, done = 0;
     int i, keysize, addr_reg_size = 64;
     AES_KEY exkey;
 
@@ -895,20 +845,15 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch and check wkvp from param block */
-    for (i = 0; i < sizeof(wkvp); i++) {
-        addr = wrap_address(env, param_addr + keysize + i);
-        wkvp[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize, wkvp, sizeof(wkvp));
     if (memcmp(wkvp, protkey_wkvp, sizeof(wkvp))) {
         /* wkvp mismatch -> return with cc 1 */
         return 1;
     }
 
     /* fetch protected key from param block */
-    for (i = 0; i < keysize; i++) {
-        addr = wrap_address(env, param_addr + i);
-        key[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra, param_addr, key, keysize);
     /* decrypt the protected key */
     decrypt_protkey(key, keysize);
 
@@ -920,15 +865,14 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 
     /* fetch tweak from param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + sizeof(wkvp) + i);
-        tweak[i] = cpu_ldb_mmu(env, addr, oi, ra);
-    }
+    copy_from_guest_wrap(env, mmu_idx, ra,
+                         param_addr + keysize + sizeof(wkvp),
+                         tweak, AES_BLOCK_SIZE);
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
         /* fetch one AES block into in */
-        aes_read_block(env, mmu_idx, *src_ptr_reg + done, in, ra);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
         if (mod) {
             /* decrypt in => out */
             AES_xts_decrypt(in, out, tweak, &exkey);
@@ -939,16 +883,15 @@ int cpacf_paes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
         /* prep tweak for next round */
         AES_xts_prep_next_tweak(tweak);
         /* write out this processed block from out */
-        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
         len -= AES_BLOCK_SIZE;
         done += AES_BLOCK_SIZE;
     }
 
     /* update tweak in param block */
-    for (i = 0; i < AES_BLOCK_SIZE; i++) {
-        addr = wrap_address(env, param_addr + keysize + sizeof(wkvp) + i);
-        cpu_stb_mmu(env, addr, tweak[i], oi, ra);
-    }
+    copy_to_guest_wrap(env, mmu_idx, ra,
+                       param_addr + keysize + sizeof(wkvp),
+                       tweak, AES_BLOCK_SIZE);
 
     *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
                              *src_ptr_reg + done);
Signature · 1 line
2.43.0
#16 lore raw

Re: [PATCH v10 10/21] target/s390x: Base support for cpacf protected keys

In reply to Harald Freudenberger
Quoted 9 lines · Harald Freudenberger

Add base support for cpacf protected key handling.

The qemu version provided here is only a fake intended to make protected key available for developing and testing purpose:
* The protected key is 'derived' from the clear key by xoring the fixed pattern 0xAAAA... onto the key value.
* The AES Wrapping Key Verification Pattern is a fixed value of 32 bytes 0xFACEFACE...

Quoted 7 lines

Add preprocessor defines for the xor pattern and wkvp used to construct ('encrypt') a protected key from a clear key value with this implementation. Also add some static functions to 'encrypt' from clear key to protected key and 'decrypt' back to cpacf_aes.c.

The preprocessor defines shall be used later in testcases to construct and decode protected keys.

Hmm... so does that mean that we only provide the protected key handling in the !KVM case for people who want to run via tcg for some development purposes? Does the user actually get some kind of notice in that case, if for example they run with !KVM due to some configuration hiccup? IOW, do users get some clue that they are running with a fake placeholder implementation, other than the setup being slow?

Quoted 6 lines
---
 target/s390x/tcg/cpacf.h     | 25 +++++++++++++++++++++++
 target/s390x/tcg/cpacf_aes.c | 39 ++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
#17 lore raw

Re: [PATCH v10 10/21] target/s390x: Base support for cpacf protected keys

Quoted 19 lines · Cornelia Huck
Quoted 9 lines · Harald Freudenberger

Add base support for cpacf protected key handling.

The qemu version provided here is only a fake intended to make protected key available for developing and testing purpose:
* The protected key is 'derived' from the clear key by xoring the fixed pattern 0xAAAA... onto the key value.
* The AES Wrapping Key Verification Pattern is a fixed value of 32 bytes 0xFACEFACE...

Quoted 7 lines

Add preprocessor defines for the xor pattern and wkvp used to construct ('encrypt') a protected key from a clear key value with this implementation. Also add some static functions to 'encrypt' from clear key to protected key and 'decrypt' back to cpacf_aes.c.

The preprocessor defines shall be used later in testcases to construct and decode protected keys.

Quoted 7 lines

Hmm... so does that mean that we only provide the protected key handling in the !KVM case for people who want to run via tcg for some development purposes? Does the user actually get some kind of notice in that case, if for example they run with !KVM due to some configuration hiccup? IOW, do users get some clue that they are running with a fake placeholder implementation, other than the setup being slow?

Well, there are 2 paths leading to protected use:
1) you have a secure key and want to 'derive' a protected key from that to operate faster. This is for example the case now when you have an PAES encrypted filesystem.
In such a case you need a crypto card with the Master Key setting fitting to when your secure key was generated.
-> this is not a practical way for qemu on !KVM as you need a crypto card AND the s390 firmware stack.
2) you 'derive' a protected key from a clear key value. On a s390 system this is done via the privileged instruction PCKMO. On this qemu with !KVM this is the fake I implemented.
In general it is absolutely not recommended to go this path as the
source
of the key is/was/needs to stay in OS memory. So you don't win any security by using protected keys this way - in fact you would have done better to just use the clear key as it is. Please note also,
that
by default this path on a s390 system is disabled - however, for
testing
purpose there are ways to use it this way.
However, this is exactly what this implementation here is for. For
testing
purpose you may 'derive' a protected key from clear key or any other
way
as the algorithm to derive is clear documented here. And then you can
play
around and run tests with protected key implementations.

I see your point: How can a innocent user see that his/her vm is running with this fake installation instead of the real one. The answer is:
If the source of the protected key is a secure key - then there is no path on the faked implementation to get a hand on a protected key.
If the source of the protected key is a clear key - honestly does it even matter which implementation is then running?

Hopefully I could explain the difference.

Quoted 7 lines
Quoted 7 lines
---
 target/s390x/tcg/cpacf.h     | 25 +++++++++++++++++++++++
 target/s390x/tcg/cpacf_aes.c | 39 
++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
Reviewed-by on v10 1/21
#2 lore raw

Re: [PATCH v10 01/21] target/s390x: Fix wrong address handling in address loops

In reply to Harald Freudenberger
Quoted 1 line · Harald Freudenberger

The loop increments addr by the element stride (+= 4) before calling

+= 4? The code says += 8. Which one is correct? Please correct, if necessary.

Quoted 8 lines

wrap_address, but then overwrites the loop addr with the wrapped value. On the next iteration the stride is applied to the wrapped address of the previous element, not to the original unwrapped address. This results in every element after the first is read from a wrong (wrapped) address.

Fixes 9f17bfdab4 ("target/s390x: support SHA-512 extensions")
Signature · 2 lines
Mit freundlichen Grüßen / Kind regards
Holger Dengler
#18 lore raw

Re: [PATCH v10 10/21] target/s390x: Base support for cpacf protected keys

In reply to Harald Freudenberger
Quoted 31 lines · Harald Freudenberger
Quoted 19 lines · Cornelia Huck
Quoted 9 lines · Harald Freudenberger

Add base support for cpacf protected key handling.

The qemu version provided here is only a fake intended to make protected key available for developing and testing purpose:
* The protected key is 'derived' from the clear key by xoring the fixed pattern 0xAAAA... onto the key value.
* The AES Wrapping Key Verification Pattern is a fixed value of 32 bytes 0xFACEFACE...

Quoted 7 lines

Add preprocessor defines for the xor pattern and wkvp used to construct ('encrypt') a protected key from a clear key value with this implementation. Also add some static functions to 'encrypt' from clear key to protected key and 'decrypt' back to cpacf_aes.c.

The preprocessor defines shall be used later in testcases to construct and decode protected keys.

Quoted 10 lines

Hmm... so does that mean that we only provide the protected key handling
in the !KVM case for people who want to run via tcg for some development
purposes? Does the user actually get some kind of notice in that case, if for example they run with !KVM due to some configuration hiccup? IOW,
do users get some clue that they are running with a fake placeholder implementation, other than the setup being slow?

Quoted 8 lines

Well, there are 2 paths leading to protected use:
1) you have a secure key and want to 'derive' a protected key from that to operate faster. This is for example the case now when you have an PAES encrypted filesystem.
In such a case you need a crypto card with the Master Key setting fitting to when your secure key was generated.
-> this is not a practical way for qemu on !KVM as you need a crypto card AND the s390 firmware stack.

Nod.

Quoted 11 lines

2) you 'derive' a protected key from a clear key value. On a s390 system this is done via the privileged instruction PCKMO. On this qemu with !KVM this is the fake I implemented.
In general it is absolutely not recommended to go this path as the
source
of the key is/was/needs to stay in OS memory. So you don't win any security by using protected keys this way - in fact you would have done better to just use the clear key as it is. Please note also,
that
by default this path on a s390 system is disabled - however, for
testing

Quoted 8 lines

purpose there are ways to use it this way.
However, this is exactly what this implementation here is for. For
testing
purpose you may 'derive' a protected key from clear key or any other
way
as the algorithm to derive is clear documented here. And then you can
play
around and run tests with protected key implementations.

Yes, and it is useful for that.

Quoted 11 lines

I see your point: How can a innocent user see that his/her vm is running with
this fake installation instead of the real one. The answer is:
If the source of the protected key is a secure key - then there is no path
on the faked implementation to get a hand on a protected key. If the source of the protected key is a clear key - honestly does it even
matter which implementation is then running?

Quoted 1 line

Hopefully I could explain the difference.

Yes, thanks.

It seems we have similar cases for other archs, so let's just keep it as it is now.

Reviewed-by on v10 3/21
#5 lore raw

Re: [PATCH v10 03/21] target/s390x: Move cpacf sha512 code into a new file

In reply to Harald Freudenberger
Quoted 8 lines · Harald Freudenberger

Move the cpacf sha512 implementation into a new file cpacf_sha512.c. Add this new file to the build and use the cpacf.h header file storing function the prototypes.

Signature · 2 lines
Mit freundlichen Grüßen / Kind regards
Holger Dengler
Reviewed-by on v10 8/21
#11 lore raw

Re: [PATCH v10 08/21] target/s390x: Minimal AES XTS support for cpacf pcc instruction

In reply to Harald Freudenberger
Quoted 2 lines · Harald Freudenberger

Support CPACF pcc subfunctions PCC-Compute-XTS-Parameter-AES-128 and PCC-Compute-XTS-Parameter-AES-128 but only for the special

typo(?): --> PCC-Compute-XTS-Parameter-AES-256

Quoted 6 lines

case block sequential number is 0. However, this covers the s390 AES XTS implementation in the Linux kernel and Libica and thus also Opencryptoki clear key via Libica.

Ordering? (T-b first, then S-o-b). See my comment below.

Quoted 7 lines
---
  target/s390x/gen-features.c      |  2 +
  target/s390x/tcg/cpacf.h         |  2 +
  target/s390x/tcg/cpacf_aes.c     | 63 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c | 20 ++++++++++
  4 files changed, 87 insertions(+)

[...]

Quoted 34 lines
target/s390x/tcg/cpacf_aes.c +26 -0
index 3d6aa19df2..f41b7dc541 100644
@@ -290,3 +290,66 @@ int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
      return !len ? 0 : 3;
  }
+
+int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                  uint64_t param_addr, uint8_t fc)
+{
+    uint8_t key[32], tweak[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
+    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
+    int keysize, i;
+    uint64_t addr;
+    AES_KEY exkey;
+
+    switch (fc) {
+    case CPACF_PCC_XTS_AES_128:
+        keysize = 16;
+        break;
+    case CPACF_PCC_XTS_AES_256:
+        keysize = 32;
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    /* fetch block sequence nr from param block into buf */
+    for (i = 0; i < AES_BLOCK_SIZE; i++) {
+        addr = wrap_address(env, param_addr + keysize + AES_BLOCK_SIZE + i);
+        buf[i] = cpu_ldb_mmu(env, addr, oi, ra);
+    }

target/s390x/tcg/cpacf_aes.c int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, · line 328

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c +10 -0
+
+    /* is the block sequence nr 0 ? */
+    for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++) {
+            ;
+    }
+    if (i < AES_BLOCK_SIZE) {
+        /* no, sorry handling of non zero block sequence is not implemented */
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+        return 1;
+    }

Why not

for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++);
if (i < AES_BLOCK_SIZE) {
...

Or would that be against the coding guidelines?

I personally would prefer the following, because it is more obvious. But your solution also works, so feel free to change it or leave it as is.

for (i = 0; i < AES_BLOCK_SIZE; i++) {
    if (buf[i]) {
        /*
         * no, sorry handling of non zero block sequence is not
         * implemented
         */
        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
        return 1;
    }
}

IMO, it is easier to read.

[...]

Signature · 2 lines
Mit freundlichen Grüßen / Kind regards
Holger Dengler
#31 lore raw
In reply to Harald Freudenberger
Quoted 9 lines · Harald Freudenberger

This patch series extends the s390 qemu CPACF support to be able to run a subset of the CPACF instruction cross platform. There have been requests on the kernel crypto mailing list about a way to test s390 specific crypto implementations. For example a way to test s390 CPACF exploitation code like the s390_aes.ko kernel module.

So here now is a set of patches verified on x86 and s390 which over (slow but working) support for a subset of the subfunctions of some of the CPACF instructions.

Hi,

I wanted to pick this, but unfortunately, there are some problems with it, as spotted by checkpatch (sadly, I did not see this earlier, and softfreeze is upon us...)

- Licensing information: new files (e.g. cpacf.h) must use an SPDX identifier instead of licence boilerplate text - this needs to be fixed
- some files are using tabs instead of spaces in some places
- there are also some other moans (less important), but you may want to look at them anyway (the long lines are not really a problem)

Cornelia

#34 lore raw
In reply to Harald Freudenberger
Quoted 2 lines · Harald Freudenberger

Harald Freudenberger (21):
target/s390x: Fix wrong address handling in address loops

I've picked the first patch as a bugfix.

Quoted 22 lines

target/s390x: Rework s390 cpacf implementations target/s390x: Move cpacf sha512 code into a new file target/s390x: Support cpacf sha256
target/s390x: Support AES ECB for cpacf km instruction target/s390x: Support AES CBC for cpacf kmc instruction target/s390x: Support AES CTR for cpacf kmctr instruction target/s390x: Minimal AES XTS support for cpacf pcc instruction target/s390x: Support AES XTS for cpacf km instruction target/s390x: Base support for cpacf protected keys target/s390x: Support pckmo encrypt AES subfunctions target/s390x: Support protected key AES ECB for cpacf km instruction target/s390x: Support protected key AES CBC for cpacf kmc instruction target/s390x: Support protected key AES CTR for cpacf kmctr
instruction
target/s390x: Minimal protected key AES XTS support for cpacf pcc
instruction
target/s390x: Support protected key AES XTS for cpacf km instruction docs/s390: Document CPACF instructions support
crypto: Add aes-helpers file to support some AES modes target/s390x: Use generic AES helper functions target/s390x: Improve fetch and store mem from and to guest tests/tcg/s390x: Add tests for CPACF instructions

#12 lore raw

Re: [PATCH v10 08/21] target/s390x: Minimal AES XTS support for cpacf pcc instruction

In reply to Holger Dengler
Quoted 10 lines · Holger Dengler
Quoted 2 lines · Harald Freudenberger

Support CPACF pcc subfunctions PCC-Compute-XTS-Parameter-AES-128 and PCC-Compute-XTS-Parameter-AES-128 but only for the special

typo(?): --> PCC-Compute-XTS-Parameter-AES-256

Quoted 4 lines

case block sequential number is 0. However, this covers the s390 AES XTS implementation in the Linux kernel and Libica and thus also Opencryptoki clear key via Libica.

Quoted 6 lines
Quoted 2 lines

Ordering? (T-b first, then S-o-b). See my comment below.

Yea ... ordering

Quoted 59 lines
Quoted 8 lines
---
  target/s390x/gen-features.c      |  2 +
  target/s390x/tcg/cpacf.h         |  2 +
  target/s390x/tcg/cpacf_aes.c     | 63 
++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c | 20 ++++++++++
  4 files changed, 87 insertions(+)

[...]

Quoted 44 lines
target/s390x/tcg/cpacf_aes.c +2 -0
index 3d6aa19df2..f41b7dc541 100644
@@ -290,3 +290,66 @@ int cpacf_aes_ctr(CPUS390XState *env, const int 
 mmu_idx, uintptr_t ra,
        return !len ? 0 : 3;
  }
+
+int cpacf_aes_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t 

ra,

target/s390x/tcg/cpacf_aes.c +22 -0
+                  uint64_t param_addr, uint8_t fc)
+{
+    uint8_t key[32], tweak[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
+    const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
+    int keysize, i;
+    uint64_t addr;
+    AES_KEY exkey;
+
+    switch (fc) {
+    case CPACF_PCC_XTS_AES_128:
+        keysize = 16;
+        break;
+    case CPACF_PCC_XTS_AES_256:
+        keysize = 32;
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    /* fetch block sequence nr from param block into buf */
+    for (i = 0; i < AES_BLOCK_SIZE; i++) {
+        addr = wrap_address(env, param_addr + keysize + 

AES_BLOCK_SIZE + i);

target/s390x/tcg/cpacf_aes.c +9 -0
+        buf[i] = cpu_ldb_mmu(env, addr, oi, ra);
+    }
+
+    /* is the block sequence nr 0 ? */
+    for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++) {
+            ;
+    }
+    if (i < AES_BLOCK_SIZE) {
+        /* no, sorry handling of non zero block sequence is not 

implemented */

target/s390x/tcg/cpacf_aes.c int cpacf_aes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t ra, · line 328

Quoted 3 lines
target/s390x/tcg/cpacf_aes.c +3 -0
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+        return 1;
+    }

Why not

Quoted 6 lines

for (i = 0; i < AES_BLOCK_SIZE && !buf[i]; i++);
if (i < AES_BLOCK_SIZE) {
...

Or would that be against the coding guidelines?

Yes. Qemu requires to always use {}.
But I could do some (inline) function is_mem_zero(mem, len) instead. Maybe in v11 then.

Quoted 18 lines

I personally would prefer the following, because it is more obvious. But your solution also works, so feel free to change it or leave it as is.

for (i = 0; i < AES_BLOCK_SIZE; i++) {
    if (buf[i]) {
        /*
         * no, sorry handling of non zero block sequence is not
         * implemented
         */
        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
        return 1;
    }
}

IMO, it is easier to read.

[...]

#32 lore raw
In reply to Cornelia Huck
Quoted 18 lines · Cornelia Huck
Quoted 9 lines · Harald Freudenberger

This patch series extends the s390 qemu CPACF support to be able to run a subset of the CPACF instruction cross platform. There have been requests on the kernel crypto mailing list about a way to test s390 specific crypto implementations. For example a way to test s390 CPACF exploitation code like the s390_aes.ko kernel module.

So here now is a set of patches verified on x86 and s390 which over (slow but working) support for a subset of the subfunctions of some of the CPACF instructions.

Hi,

I wanted to pick this, but unfortunately, there are some problems with it, as spotted by checkpatch (sadly, I did not see this earlier, and softfreeze is upon us...)

Quoted 8 lines

- Licensing information: new files (e.g. cpacf.h) must use an SPDX identifier instead of licence boilerplate text - this needs to be
fixed
- some files are using tabs instead of spaces in some places
- there are also some other moans (less important), but you may want to look at them anyway (the long lines are not really a problem)

Cornelia

Yes, I saw these checkpatch complains but as nobody else seems to obey to the checkpatch findings I ignored most of them. However, will fix the license and tabs things and then let's see what remains.

Reviewed-by on v10 9/21
#14 lore raw

Re: [PATCH v10 09/21] target/s390x: Support AES XTS for cpacf km instruction

In reply to Harald Freudenberger
Quoted 5 lines · Harald Freudenberger

Support the subfunctions XTS-AES-128 and XTS-AES-256 for the cpacf km instruction.

Signature · 2 lines
Mit freundlichen Grüßen / Kind regards
Holger Dengler
#33 lore raw
In reply to Harald Freudenberger
Quoted 26 lines · Harald Freudenberger
Quoted 25 lines · Cornelia Huck
Quoted 9 lines · Harald Freudenberger

This patch series extends the s390 qemu CPACF support to be able to run a subset of the CPACF instruction cross platform. There have been requests on the kernel crypto mailing list about a way to test s390 specific crypto implementations. For example a way to test s390 CPACF exploitation code like the s390_aes.ko kernel module.

So here now is a set of patches verified on x86 and s390 which over (slow but working) support for a subset of the subfunctions of some of the CPACF instructions.

Hi,

I wanted to pick this, but unfortunately, there are some problems with it, as spotted by checkpatch (sadly, I did not see this earlier, and softfreeze is upon us...)

- Licensing information: new files (e.g. cpacf.h) must use an SPDX identifier instead of licence boilerplate text - this needs to be
fixed
- some files are using tabs instead of spaces in some places
- there are also some other moans (less important), but you may want to look at them anyway (the long lines are not really a problem)

Quoted 4 lines
Quoted 1 line

Cornelia

Yes, I saw these checkpatch complains but as nobody else seems to obey to the checkpatch findings I ignored most of them.

That is very much the wrong conclusion to draw. Our existing in-tree code may not be clean on checkpatch, because we change checkpatch rules over time but we don't retrospectively change existing code.

New code is expected to be clean with checkpatch unless there are some genuine false positives which can't be reasonably avoided.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#19 lore raw

Re: [PATCH v10 10/21] target/s390x: Base support for cpacf protected keys

In reply to Harald Freudenberger
Quoted 52 lines · Harald Freudenberger
Quoted 19 lines · Cornelia Huck
Quoted 9 lines · Harald Freudenberger

Add base support for cpacf protected key handling.

The qemu version provided here is only a fake intended to make protected key available for developing and testing purpose:
* The protected key is 'derived' from the clear key by xoring the fixed pattern 0xAAAA... onto the key value.
* The AES Wrapping Key Verification Pattern is a fixed value of 32 bytes 0xFACEFACE...

Quoted 7 lines

Add preprocessor defines for the xor pattern and wkvp used to construct ('encrypt') a protected key from a clear key value with this implementation. Also add some static functions to 'encrypt' from clear key to protected key and 'decrypt' back to cpacf_aes.c.

The preprocessor defines shall be used later in testcases to construct and decode protected keys.

Quoted 7 lines

Hmm... so does that mean that we only provide the protected key handling in the !KVM case for people who want to run via tcg for some development purposes? Does the user actually get some kind of notice in that case, if for example they run with !KVM due to some configuration hiccup? IOW, do users get some clue that they are running with a fake placeholder implementation, other than the setup being slow?

Well, there are 2 paths leading to protected use:
1) you have a secure key and want to 'derive' a protected key from that to operate faster. This is for example the case now when you have an PAES encrypted filesystem.
In such a case you need a crypto card with the Master Key setting fitting to when your secure key was generated.
-> this is not a practical way for qemu on !KVM as you need a crypto card AND the s390 firmware stack.
2) you 'derive' a protected key from a clear key value. On a s390 system this is done via the privileged instruction PCKMO. On this qemu with !KVM this is the fake I implemented.
In general it is absolutely not recommended to go this path as the source of the key is/was/needs to stay in OS memory. So you don't win any security by using protected keys this way - in fact you would have done better to just use the clear key as it is. Please note also, that by default this path on a s390 system is disabled - however, for testing purpose there are ways to use it this way.
However, this is exactly what this implementation here is for. For
testing
purpose you may 'derive' a protected key from clear key or any other way as the algorithm to derive is clear documented here. And then you can
play
around and run tests with protected key implementations.

Quoted 7 lines

I see your point: How can a innocent user see that his/her vm is running with
this fake installation instead of the real one. The answer is:
If the source of the protected key is a secure key - then there is no path on the faked implementation to get a hand on a protected key. If the source of the protected key is a clear key - honestly does it even matter which implementation is then running?

IMHO any use of TCG inherantly throws away any security guarantees that rely on a guaranteed hardware impl, and in addition we don't claim that TCG provides a secure host/guest boundary & thus excluded it from CVE triage.

IOW, if someone is concerned about integrity of their VM, the answer is to detect the presence of TCG in general, rather than detect anything related to this narrow protected key feature.

TL;DR: this is at most a docs problem to highlight the limitations of using TCG in general, and not a problem for this patch series.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|