Skip to contentSkip to conversation

target/s390x: Extend qemu CPACF support

PATCH Superseded Harald Freudenberger 3 participants 49 messages Jun 17 – Jun 23

Subsystems (3)

v7 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v7 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: As this series is more or less complete, a full blown linux can be run and 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 CPAC instructions.

Harald Freudenberger (17):
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: 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

 crypto/aes-helpers.c             | 101 ++++
 crypto/meson.build               |   1 +
 docs/system/s390x/cpacf.rst      | 116 ++++
 docs/system/target-s390x.rst     |   1 +
 include/crypto/aes.h             |  14 +
 target/s390x/gen-features.c      |  31 +
 target/s390x/tcg/cpacf.h         |  63 ++
 target/s390x/tcg/cpacf_aes.c     | 955 +++++++++++++++++++++++++++++++
 target/s390x/tcg/cpacf_sha256.c  | 232 ++++++++
 target/s390x/tcg/cpacf_sha512.c  | 245 ++++++++
 target/s390x/tcg/crypto_helper.c | 423 +++++++-------
 target/s390x/tcg/insn-data.h.inc |   1 +
 target/s390x/tcg/meson.build     |   3 +
 target/s390x/tcg/translate.c     |   2 +
 14 files changed, 1972 insertions(+), 216 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

base-commit: e89049b3ba5f1f0468bc0d294173345597514a1b --
2.43.0

#1 lore raw

PATCH v7 1/17 target/s390x: Rework s390 cpacf implementations

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.

---
 target/s390x/tcg/crypto_helper.c | 84 +++++++++++++++++++++++++++-----
 target/s390x/tcg/insn-data.h.inc |  1 +
 target/s390x/tcg/translate.c     |  2 +
 3 files changed, 74 insertions(+), 13 deletions(-)
target/s390x/tcg/crypto_helper.c +71 -13
index ae392bce0e..35f0cc26a4 100644
@@ -272,6 +272,57 @@ static void fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     }
 }
 
+static int cpacf_kimd(CPUS390XState *env, const int mmu_idx, const uintptr_t ra,
+                      uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case 0x03: /* 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);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    return rc;
+}
+
+static int cpacf_klmd(CPUS390XState *env, const int mmu_idx, const uintptr_t ra,
+                      uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case 0x03: /* 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);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    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)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case 0x72: /* CPACF_PRNO_TRNG */
+        fill_buf_random(env, mmu_idx, ra, &env->regs[r1], &env->regs[r1 + 1]);
+        fill_buf_random(env, mmu_idx, ra, &env->regs[r2], &env->regs[r2 + 1]);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    return rc;
+}
+
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
                      uint32_t type)
 {
@@ -282,13 +333,15 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     uint8_t subfunc[16] = { 0 };
     uint64_t param_addr;
     MemOpIdx oi;
+    int rc = 0;
 
     switch (type) {
-    case S390_FEAT_TYPE_KMAC:
+    case S390_FEAT_TYPE_KDSA:
     case S390_FEAT_TYPE_KIMD:
     case S390_FEAT_TYPE_KLMD:
-    case S390_FEAT_TYPE_PCKMO:
+    case S390_FEAT_TYPE_KMAC:
     case S390_FEAT_TYPE_PCC:
+    case S390_FEAT_TYPE_PCKMO:
         if (mod) {
             tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
         }
@@ -300,25 +353,30 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
     }
 
-    switch (fc) {
-    case 0: /* query subfunction */
+    /* handle query subfunction */
+    if (fc == 0) {
         oi = make_memop_idx(MO_8, mmu_idx);
-        for (int i = 0; i < 16; i++) {
+        for (int i = 0; i < sizeof(subfunc); i++) {
             param_addr = wrap_address(env, env->regs[1] + i);
             cpu_stb_mmu(env, param_addr, subfunc[i], oi, ra);
         }
+        goto out;
+    }
+
+    switch (type) {
+    case S390_FEAT_TYPE_KIMD:
+        rc = cpacf_kimd(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
-    case 3: /* CPACF_*_SHA_512 */
-        return cpacf_sha512(env, mmu_idx, ra, env->regs[1], &env->regs[r2],
-                            &env->regs[r2 + 1], type);
-    case 114: /* CPACF_PRNO_TRNG */
-        fill_buf_random(env, mmu_idx, ra, &env->regs[r1], &env->regs[r1 + 1]);
-        fill_buf_random(env, mmu_idx, ra, &env->regs[r2], &env->regs[r2 + 1]);
+    case S390_FEAT_TYPE_KLMD:
+        rc = cpacf_klmd(env, mmu_idx, ra, r1, r2, r3, fc);
+        break;
+    case S390_FEAT_TYPE_PPNO:
+        rc = cpacf_ppno(env, mmu_idx, ra, r1, r2, r3, fc);
         break;
     default:
-        /* we don't implement any other subfunction yet */
         g_assert_not_reached();
     }
 
-    return 0;
+out:
+    return rc;
 }
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
#2 lore raw

PATCH v7 2/17 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 add a new header file cpacf.h containing the prototypes for the s390 cpacf stuff.

---
 target/s390x/tcg/cpacf.h         |  16 ++
 target/s390x/tcg/cpacf_sha512.c  | 245 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 227 +---------------------------
 target/s390x/tcg/meson.build     |   1 +
 4 files changed, 263 insertions(+), 226 deletions(-)
 create mode 100644 target/s390x/tcg/cpacf.h
 create mode 100644 target/s390x/tcg/cpacf_sha512.c
target/s390x/tcg/cpacf.h new file +16 -0
new file mode 100644
index 0000000000..d27839ddd9
@@ -0,0 +1,16 @@
+/*
+ * 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
+
+/* from crypto_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
target/s390x/tcg/cpacf_sha512.c new file +245 -0

245 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +1 -226

233 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
Reviewed-by on v7 2/17
#3 lore raw

Re: [PATCH v7 02/17] target/s390x: Move cpacf sha512 code into a new file

Quoted 7 lines · Harald Freudenberger

Move the cpacf sha512 implementation into a new file cpacf_sha512.c. Add this new file to the build and add a new header file cpacf.h containing the prototypes for the s390 cpacf stuff.

With the one typo (see below) fixed:

Quoted 16 lines
---
  target/s390x/tcg/cpacf.h         |  16 ++
  target/s390x/tcg/cpacf_sha512.c  | 245 +++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c | 227 +---------------------------
  target/s390x/tcg/meson.build     |   1 +
  4 files changed, 263 insertions(+), 226 deletions(-)

create mode 100644 target/s390x/tcg/cpacf.h create mode 100644 target/s390x/tcg/cpacf_sha512.c

target/s390x/tcg/cpacf.h new file +1 -0
new file mode 100644
index 0000000000..d27839ddd9
@@ -0,0 +1,16 @@
+/*

target/s390x/tcg/cpacf.h line 11

Quoted 10 lines
target/s390x/tcg/cpacf.h new file +10 -0
+ * 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
+
+/* from crypto_sha512.c */

It should be cpacf_sha512.c in the comment instead of crypto_sha512.c

Quoted 4 lines
target/s390x/tcg/cpacf.h new file +4 -0
+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);
+

[ snip ]

#4 lore raw

PATCH v7 3/17 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  | 232 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   8 ++
 target/s390x/tcg/meson.build     |   1 +
 5 files changed, 248 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 d27839ddd9..e2c36306b2 100644
@@ -8,6 +8,11 @@
 #ifndef S390X_CPACF_H
 #define S390X_CPACF_H
 
+/* from crypto_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 crypto_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 +232 -0

232 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +8 -0
index 574a39258c..a701dd8c6f 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 0x02: /* 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 0x03: /* 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 0x02: /* 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 0x03: /* 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
Reviewed-by on v7 3/17
#5 lore raw

Re: [PATCH v7 03/17] target/s390x: Support cpacf sha256

Quoted 5 lines · Harald Freudenberger

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

With the one typo (see below) fixed:

Quoted 28 lines
---
  target/s390x/gen-features.c      |   2 +
  target/s390x/tcg/cpacf.h         |   5 +
  target/s390x/tcg/cpacf_sha256.c  | 232 +++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |   8 ++
  target/s390x/tcg/meson.build     |   1 +
  5 files changed, 248 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 +0 -0
index d27839ddd9..e2c36306b2 100644

target/s390x/tcg/cpacf.h line 11

Quoted 4 lines
target/s390x/tcg/cpacf.h +1 -0
@@ -8,6 +8,11 @@
  #ifndef S390X_CPACF_H
  #define S390X_CPACF_H
 
+/* from crypto_sha256.c */

The comment should be cpacf_sha256.c instead of crypto_sha256.c

Quoted 4 lines
target/s390x/tcg/cpacf.h +4 -0
+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);
+

[ snip ]

#6 lore raw

PATCH v7 4/17 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     | 113 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  24 +++++++
 target/s390x/tcg/meson.build     |   1 +
 5 files changed, 147 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 e2c36306b2..36d0c81893 100644
@@ -18,4 +18,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 crypto_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
target/s390x/tcg/cpacf_aes.c new file +113 -0

113 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +24 -0
index a701dd8c6f..6585dfd4e7 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 0x12: /* CPACF_KM_AES_128 */
+    case 0x13: /* CPACF_KM_AES_192 */
+    case 0x14: /* 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:
+        g_assert_not_reached();
+    }
+
+    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)
 {
@@ -156,6 +177,9 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     case S390_FEAT_TYPE_PPNO:
         rc = cpacf_ppno(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;
     default:
         g_assert_not_reached();
     }
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
Reviewed-by on v7 4/17
#7 lore raw

Re: [PATCH v7 04/17] target/s390x: Support AES ECB for cpacf km instruction

Quoted 5 lines · Harald Freudenberger

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

With the one typo (see below) fixed:

Quoted 28 lines
---
  target/s390x/gen-features.c      |   3 +
  target/s390x/tcg/cpacf.h         |   6 ++
  target/s390x/tcg/cpacf_aes.c     | 113 +++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  24 +++++++
  target/s390x/tcg/meson.build     |   1 +
  5 files changed, 147 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 +0 -0
index e2c36306b2..36d0c81893 100644

target/s390x/tcg/cpacf.h int cpacf_sha512(CPUS390XState *env, const int mmu_idx, uintptr_t ra, · line 21

Quoted 4 lines
target/s390x/tcg/cpacf.h +1 -0
@@ -18,4 +18,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 crypto_aes.c */

Should be cpacf_aes.c instead of crypto_aes.c

Quoted 5 lines
target/s390x/tcg/cpacf.h +5 -0
+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);
+

[ snip ]

#8 lore raw

PATCH v7 5/17 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     | 102 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  24 ++++++++
 4 files changed, 133 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 36d0c81893..8b21b16147 100644
@@ -23,5 +23,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
target/s390x/tcg/cpacf_aes.c +102 -0

105 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +24 -0
index 6585dfd4e7..b6f7696809 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 0x12: /* CPACF_KMC_AES_128 */
+    case 0x13: /* CPACF_KMC_AES_192 */
+    case 0x14: /* 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:
+        g_assert_not_reached();
+    }
+
+    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)
 {
@@ -180,6 +201,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;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
Reviewed-by on v7 5/17
#9 lore raw

Re: [PATCH v7 05/17] target/s390x: Support AES CBC for cpacf kmc instruction

Quoted 5 lines · Harald Freudenberger

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

Quoted 6 lines
---
  target/s390x/gen-features.c      |   3 +
  target/s390x/tcg/cpacf.h         |   4 ++
  target/s390x/tcg/cpacf_aes.c     | 102 +++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  24 ++++++++
  4 files changed, 133 insertions(+)

[ snip ]

#10 lore raw

PATCH v7 6/17 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     | 76 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 24 ++++++++++
 4 files changed, 108 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 8b21b16147..d73cb98c38 100644
@@ -27,5 +27,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
target/s390x/tcg/cpacf_aes.c +76 -0

79 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +24 -0
index b6f7696809..98dfa37185 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 0x12: /* CPACF_KMCTR_AES_128 */
+    case 0x13: /* CPACF_KMCTR_AES_192 */
+    case 0x14: /* 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:
+        g_assert_not_reached();
+    }
+
+    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)
 {
@@ -204,6 +225,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;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
Reviewed-by on v7 6/17
#11 lore raw

Re: [PATCH v7 06/17] target/s390x: Support AES CTR for cpacf kmctr instruction

Quoted 4 lines · Harald Freudenberger

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

With the one typo fixed:

Quoted 6 lines
---
  target/s390x/gen-features.c      |  3 ++
  target/s390x/tcg/cpacf.h         |  5 +++
  target/s390x/tcg/cpacf_aes.c     | 76 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c | 24 ++++++++++
  4 files changed, 108 insertions(+)

[ snip ]

Quoted 5 lines
target/s390x/tcg/cpacf_aes.c +0 -0
index 6412cc187d..e200a9a87a 100644
@@ -213,3 +213,79 @@ int cpacf_aes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,

[ snip ]

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

Quoted 9 lines
target/s390x/tcg/cpacf_aes.c +9 -0
+    /* 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);
+        /* 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 */

typo: exor -> xor

Quoted 5 lines
target/s390x/tcg/cpacf_aes.c +5 -0
+        aes_xor(in, buf, out);
+        /* write out the processed block */
+        aes_write_block(env, mmu_idx, *dst_ptr_reg + done, out, ra);
+        len -= AES_BLOCK_SIZE, done += AES_BLOCK_SIZE;
+    }

[ snip ]

#12 lore raw

PATCH v7 7/17 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     | 71 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 20 +++++++++
 4 files changed, 95 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 d73cb98c38..381a6c3ff1 100644
@@ -32,5 +32,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
target/s390x/tcg/cpacf_aes.c +71 -0

80 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +20 -0
index 98dfa37185..1a0a503844 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 0x32: /* CPACF_PCC compute XTS param AES-128 */
+    case 0x34: /* CPACF PCC compute XTS param AES-256 */
+            rc = cpacf_aes_pcc(env, mmu_idx, ra, env->regs[1], fc);
+            break;
+    default:
+        g_assert_not_reached();
+    }
+
+    return rc;
+}
+
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
                      uint32_t type)
 {
@@ -228,6 +245,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;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#13 lore raw

Re: [PATCH v7 07/17] target/s390x: Minimal AES XTS support for cpacf pcc instruction

Quoted 14 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 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     | 71 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c | 20 +++++++++
  4 files changed, 95 insertions(+)

[ snip ]

Quoted 5 lines
target/s390x/tcg/cpacf_aes.c +0 -0
index e200a9a87a..43c556f31b 100644

target/s390x/tcg/cpacf_aes.c line 25

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c +7 -0
@@ -16,6 +16,13 @@
  #include "crypto/aes.h"
  #include "cpacf.h"
 
+/* #define DEBUG_HELPER */
+#ifdef DEBUG_HELPER
+#define HELPER_LOG(x...) qemu_log(x)
+#else
+#define HELPER_LOG(x...)
+#endif
+

You never use this... Seems to be unrelated to this commit so put this in its own commit if you need it later.

Quoted 3 lines
target/s390x/tcg/cpacf_aes.c +0 -0
  static void aes_read_block(CPUS390XState *env, const int mmu_idx,
                             uint64_t addr, uint8_t *a, uintptr_t ra)
  {

[ snip ]

#14 lore raw

PATCH v7 8/17 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     | 107 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   6 ++
 4 files changed, 119 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 381a6c3ff1..7e53ce2a14 100644
@@ -34,5 +34,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
target/s390x/tcg/cpacf_aes.c +107 -0

110 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +6 -0
index 1a0a503844..19d625f37f 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 0x32: /* CPACF_KM_XTS_128 */
+    case 0x34: /* 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:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH v7 08/17] target/s390x: Support AES XTS for cpacf km instruction

Quoted 11 lines · 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     | 107 +++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |   6 ++
  4 files changed, 119 insertions(+)

[ snip ]

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

Quoted 3 lines
target/s390x/tcg/cpacf_aes.c +3 -0
+
+    /* process up to MAX_BLOCKS_PER_RUN aes blocks */
+    for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {

I guess this has the same "feature" as the real xts where the pcc part is not included if not explicitly done beforehand by the user right? But since CPACF does it that way I guess you can't do it the "correct" way. Instead of XTS + PCC you could implement Full XTS... just something to think about.

Quoted 3 lines
target/s390x/tcg/cpacf_aes.c +3 -0
+        /* fetch one AES block into buf1  */
+        aes_read_block(env, mmu_idx, *src_ptr_reg + done, buf1, ra);
+        /* buf1 xor tweak => buf2 */

[ snip ]

#16 lore raw

Re: [PATCH v7 08/17] target/s390x: Support AES XTS for cpacf km instruction

Quoted 27 lines · Finn Callies
Quoted 12 lines · 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     | 107 
+++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |   6 ++
  4 files changed, 119 insertions(+)

[ snip ]

Quoted 4 lines
target/s390x/tcg/cpacf_aes.c +3 -0
+
+    /* process up to MAX_BLOCKS_PER_RUN aes blocks */
+    for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) 

{

I guess this has the same "feature" as the real xts where the pcc part is not included if not explicitly done beforehand by the user right? But since CPACF does it that way I guess you can't do it the "correct" way. Instead of XTS + PCC you could implement Full XTS... just something to think about.

Quoted 5 lines
Quoted 3 lines
target/s390x/tcg/cpacf_aes.c +3 -0
+        /* fetch one AES block into buf1  */
+        aes_read_block(env, mmu_idx, *src_ptr_reg + done, buf1, ra);
+        /* buf1 xor tweak => buf2 */

[ snip ]

Well Finn, it is a CPACF emulation - not an emulation about what CPACF should return if it would have been designed the right way :-)

#17 lore raw

PATCH v7 9/17 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.

The qemu version provided here is only a fake indented 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...

---
 target/s390x/gen-features.c      |  3 ++
 target/s390x/tcg/cpacf.h         |  2 +
 target/s390x/tcg/cpacf_aes.c     | 66 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 21 ++++++++++
 4 files changed, 92 insertions(+)
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 +2 -0
index 7e53ce2a14..5588e2bdec 100644
@@ -38,5 +38,7 @@ 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);
+int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                    uint64_t param_addr, uint8_t fc);
 
 #endif
target/s390x/tcg/cpacf_aes.c +66 -0

69 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +21 -0
index 19d625f37f..e1952ae4bc 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 0x12: /* CPACF_PCKMO_ENC_AES_128_KEY */
+    case 0x13: /* CPACF_PCKMO_ENC_AES_192_KEY */
+    case 0x14: /* CPACF_PCKMO_ENC_AES_256_KEY */
+        rc = cpacf_aes_pckmo(env, mmu_idx, ra, env->regs[1], fc);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    return rc;
+}
+
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
                      uint32_t type)
 {
@@ -254,6 +272,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;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
Reviewed-by on v7 9/17
#18 lore raw

Re: [PATCH v7 09/17] target/s390x: Support pckmo encrypt AES subfunctions

Quoted 9 lines · 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.

The qemu version provided here is only a fake indented to make

typo: indeted -> intended

Quoted 8 lines

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...

With the typo fixed and the other comments at least considered:

Quoted 6 lines
---
  target/s390x/gen-features.c      |  3 ++
  target/s390x/tcg/cpacf.h         |  2 +
  target/s390x/tcg/cpacf_aes.c     | 66 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c | 21 ++++++++++
  4 files changed, 92 insertions(+)

[ snip ]

Quoted 5 lines
target/s390x/tcg/cpacf_aes.c +0 -0
index 0312436c43..5a0a3473d5 100644

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

Quoted 9 lines
target/s390x/tcg/cpacf_aes.c +6 -0
@@ -467,3 +467,69 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
      return !len ? 0 : 3;
  }
+
+/*
+ * Hard coded pattern xored with the AES clear key
+ * to 'produce' the protected key.
+ */
+static const uint8_t protkey_xor_pattern[32] = {

the wrapping key for protected keys is called virtual server master key if I recall correctly. Maybe name this accordingly or at least something with master or wrapping key in it as this describes the purpose better I think.

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

Quoted 9 lines
target/s390x/tcg/cpacf_aes.c +9 -0
+    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")
+ */
+static const uint8_t protkey_wkvp[32] = {

Since you call this protkey_wkvp you could call the wrapping key protkey_wk.

Quoted 4 lines
target/s390x/tcg/cpacf_aes.c +4 -0
+    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 };

[ snip ]

#19 lore raw

Re: [PATCH v7 09/17] target/s390x: Support pckmo encrypt AES subfunctions

Quoted 47 lines · Finn Callies
Quoted 9 lines · 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.

The qemu version provided here is only a fake indented to make

typo: indeted -> intended

Quoted 8 lines

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...

With the typo fixed and the other comments at least considered:

Quoted 7 lines
---
  target/s390x/gen-features.c      |  3 ++
  target/s390x/tcg/cpacf.h         |  2 +
  target/s390x/tcg/cpacf_aes.c     | 66 
++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c | 21 ++++++++++
  4 files changed, 92 insertions(+)

[ snip ]

Quoted 11 lines
target/s390x/tcg/cpacf_aes.c +3 -0
index 0312436c43..5a0a3473d5 100644
@@ -467,3 +467,69 @@ int cpacf_aes_xts(CPUS390XState *env, const int 
 mmu_idx, uintptr_t ra,
        return !len ? 0 : 3;
  }
+
+/*
+ * Hard coded pattern xored with the AES clear key
Quoted 9 lines

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

Quoted 3 lines
target/s390x/tcg/cpacf_aes.c +3 -0
+ * to 'produce' the protected key.
+ */
+static const uint8_t protkey_xor_pattern[32] = {

the wrapping key for protected keys is called virtual server master key if I recall correctly. Maybe name this accordingly or at least something with master or wrapping key in it as this describes the purpose better I think.

But honestly I don't want to call this a key - neither a wrapping nor a virtual server master key. This is a simple xor pattern to be applied to the clear key value. I don't want to call this a key in any way. If I would label this a key someone (or AI) would immediately raise a security finding - hard coded key / weak key / key in code ...

Quoted 18 lines

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

Quoted 9 lines
target/s390x/tcg/cpacf_aes.c +9 -0
+    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")
+ */
+static const uint8_t protkey_wkvp[32] = {

Since you call this protkey_wkvp you could call the wrapping key protkey_wk.

Quoted 4 lines
target/s390x/tcg/cpacf_aes.c +4 -0
+    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 };

[ snip ]

#20 lore raw

PATCH v7 10/17 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     | 87 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  7 +++
 4 files changed, 101 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 5588e2bdec..7cf739e7a4 100644
@@ -40,5 +40,9 @@ int cpacf_aes_xts(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
                   uint32_t type, uint8_t fc, uint8_t mod);
 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
target/s390x/tcg/cpacf_aes.c +87 -0

90 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index e1952ae4bc..988226338d 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 0x1a: /* CPACF_KM_PAES_128 */
+    case 0x1b: /* CPACF_KM_PAES_192 */
+    case 0x1c: /* 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 0x32: /* CPACF_KM_XTS_128 */
     case 0x34: /* CPACF_KM_XTS_256 */
         rc = cpacf_aes_xts(env, mmu_idx, ra, env->regs[1],
Signature · 1 line
2.43.0
Reviewed-by on v7 10/17
#21 lore raw

Re: [PATCH v7 10/17] target/s390x: Support protected key AES ECB for cpacf km instruction

Quoted 5 lines · Harald Freudenberger

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

With the comments at least considered:

Quoted 12 lines
---
  target/s390x/gen-features.c      |  3 ++
  target/s390x/tcg/cpacf.h         |  4 ++
  target/s390x/tcg/cpacf_aes.c     | 87 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  7 +++
  4 files changed, 101 insertions(+)
target/s390x/gen-features.c +0 -0
index d3e69aaca6..71e0e41d6e 100644

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 929

Quoted 6 lines
target/s390x/gen-features.c +3 -0
@@ -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,

I would strongly prefer PAES instead of EAES. I know the CPACF function is called KM...ENCRYPTED...AES... but since we use protected or PAES in any other context and never Encrypted AES I would highly suggest PAES here as well. Or as an alternative expand the E to S390_FEAT_KM_ENCRYPTED_AES_128 to match the CPACF naming.

Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_KM_XTS_AES_128,
      S390_FEAT_KM_XTS_AES_256,
      S390_FEAT_KMC_AES_128,

[ snip ]

Quoted 14 lines
target/s390x/tcg/cpacf_aes.c +6 -0
index 5a0a3473d5..bcfcf3b660 100644
@@ -533,3 +533,90 @@ int cpacf_aes_pckmo(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
      return 0;
  }
+
+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)
+{

[ snip ]

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

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c +10 -0
+
+    /* 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);
+    }
+    /* 'decrypt' the protected key */
+    for (i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];
+    }

Think about outsourcing this into a function as it would improve readability and reduce code dublication in future commits.

Quoted 7 lines
target/s390x/tcg/cpacf_aes.c +7 -0
+
+    /* expand key */
+    if (mod) {
+        AES_set_decrypt_key(key, keysize * 8, &exkey);
+    } else {
+        AES_set_encrypt_key(key, keysize * 8, &exkey);
+    }

[ snip ]

#22 lore raw

Re: [PATCH v7 10/17] target/s390x: Support protected key AES ECB for cpacf km instruction

Quoted 28 lines · Finn Callies
Quoted 5 lines · Harald Freudenberger

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

With the comments at least considered:

Quoted 17 lines
---
  target/s390x/gen-features.c      |  3 ++
  target/s390x/tcg/cpacf.h         |  4 ++
  target/s390x/tcg/cpacf_aes.c     | 87 
++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  7 +++
  4 files changed, 101 insertions(+)
target/s390x/gen-features.c +1 -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,
Quoted 9 lines

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 929

Quoted 2 lines
target/s390x/gen-features.c +2 -0
+    S390_FEAT_KM_EAES_192,
+    S390_FEAT_KM_EAES_256,

I would strongly prefer PAES instead of EAES. I know the CPACF function is called KM...ENCRYPTED...AES... but since we use protected or PAES in any other context and never Encrypted AES I would highly suggest PAES here as well. Or as an alternative expand the E to S390_FEAT_KM_ENCRYPTED_AES_128 to match the CPACF naming.

Yes, I am with you. But this is not part of this patch series. These defines have been introduced into qemu long before this patch. Maybe someone could rework them but not here and now with this patch series.

Quoted 35 lines
Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_KM_XTS_AES_128,
      S390_FEAT_KM_XTS_AES_256,
      S390_FEAT_KMC_AES_128,

[ snip ]

Quoted 15 lines
target/s390x/tcg/cpacf_aes.c +2 -0
index 5a0a3473d5..bcfcf3b660 100644
@@ -533,3 +533,90 @@ int cpacf_aes_pckmo(CPUS390XState *env, const int 
 mmu_idx, uintptr_t ra,
        return 0;
  }
+
+int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t 

ra,

target/s390x/tcg/cpacf_aes.c +4 -0
+                   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)
+{

[ snip ]

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

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c +10 -0
+
+    /* 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);
+    }
+    /* 'decrypt' the protected key */
+    for (i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];
+    }
Quoted 3 lines

Think about outsourcing this into a function as it would improve readability and reduce code dublication in future commits.

Yes, let's do this - see v8 of the patch series.

Quoted 9 lines
Quoted 7 lines
target/s390x/tcg/cpacf_aes.c +7 -0
+
+    /* expand key */
+    if (mod) {
+        AES_set_decrypt_key(key, keysize * 8, &exkey);
+    } else {
+        AES_set_encrypt_key(key, keysize * 8, &exkey);
+    }

[ snip ]

#23 lore raw

Re: [PATCH v7 10/17] target/s390x: Support protected key AES ECB for cpacf km instruction

Quoted 20 lines · Harald Freudenberger
Quoted 18 lines · Finn Callies

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 929

Quoted 10 lines · Harald Freudenberger

index d3e69aaca6..71e0e41d6e 100644

target/s390x/gen-features.c +3 -0
@@ -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,

I would strongly prefer PAES instead of EAES. I know the CPACF function is called KM...ENCRYPTED...AES... but since we use protected or PAES in any other context and never Encrypted AES I would highly suggest PAES here as well. Or as an alternative expand the E to S390_FEAT_KM_ENCRYPTED_AES_128 to match the CPACF naming.

Quoted 4 lines

Yes, I am with you. But this is not part of this patch series. These defines have been introduced into qemu long before this patch. Maybe someone could rework them but not here and now with this patch series.

They have been introduced with the CPU model for the hardware model that introduced those. Naming should not change as we would run into the difference for the cpu feature itself:

DEF_FEAT(KM_EAES_128, "km-eaes-128", KM, 26, "KM Encrypted-AES-128")

The "km-eaes-128" indication is actually the visible/external part of the CPU model.

#24 lore raw

Re: [PATCH v7 10/17] target/s390x: Support protected key AES ECB for cpacf km instruction

In reply to Harald Freudenberger
Quoted 11 lines · 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     | 87 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  7 +++
  4 files changed, 101 insertions(+)

[ snip ]

target/s390x/tcg/crypto_helper.c static int cpacf_km(CPUS390XState *env, const int mmu_idx, uintptr_t ra, · line 108

Quoted 11 lines
target/s390x/tcg/crypto_helper.c +3 -0
index e1952ae4bc..988226338d 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 0x1a: /* CPACF_KM_PAES_128 */
+    case 0x1b: /* CPACF_KM_PAES_192 */
+    case 0x1c: /* CPACF_KM_PAES_256 */

please order these correctly in the switch. For the switch statement it doesn't matter but it improves readability. 0x1a is greater than 0x34.

Quoted 7 lines
target/s390x/tcg/crypto_helper.c +4 -0
+        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 0x32: /* CPACF_KM_XTS_128 */
      case 0x34: /* CPACF_KM_XTS_256 */
          rc = cpacf_aes_xts(env, mmu_idx, ra, env->regs[1],
#25 lore raw

Re: [PATCH v7 10/17] target/s390x: Support protected key AES ECB for cpacf km instruction

Quoted 25 lines · Finn Callies
Quoted 12 lines · 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     | 87 
++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  7 +++
  4 files changed, 101 insertions(+)

[ snip ]

Quoted 9 lines
target/s390x/tcg/crypto_helper.c +0 -0
index e1952ae4bc..988226338d 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);
Quoted 9 lines

target/s390x/tcg/crypto_helper.c static int cpacf_km(CPUS390XState *env, const int mmu_idx, uintptr_t ra, · line 108

Quoted 4 lines
target/s390x/tcg/crypto_helper.c +3 -0
          break;
+    case 0x1a: /* CPACF_KM_PAES_128 */
+    case 0x1b: /* CPACF_KM_PAES_192 */
+    case 0x1c: /* CPACF_KM_PAES_256 */

please order these correctly in the switch. For the switch statement it doesn't matter but it improves readability. 0x1a is greater than 0x34.

I don't get this. The switch is ordered by increasing value. What do you mean here ?

Quoted 8 lines
Quoted 8 lines
target/s390x/tcg/crypto_helper.c +4 -0
+        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 0x32: /* CPACF_KM_XTS_128 */
      case 0x34: /* CPACF_KM_XTS_256 */
          rc = cpacf_aes_xts(env, mmu_idx, ra, env->regs[1],
#26 lore raw

PATCH v7 11/17 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     | 109 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   7 ++
 4 files changed, 123 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 7cf739e7a4..e302c17a2f 100644
@@ -44,5 +44,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
target/s390x/tcg/cpacf_aes.c +109 -0

112 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index 988226338d..3fc48bc9a1 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 0x1a: /* CPACF_KMC_PAES_128 */
+    case 0x1b: /* CPACF_KMC_PAES_192 */
+    case 0x1c: /* 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:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
Reviewed-by on v7 11/17
#27 lore raw

Re: [PATCH v7 11/17] target/s390x: Support protected key AES CBC for cpacf kmc instruction

Quoted 5 lines · Harald Freudenberger

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

With the suggested changes:

Quoted 12 lines
---
  target/s390x/gen-features.c      |   3 +
  target/s390x/tcg/cpacf.h         |   4 ++
  target/s390x/tcg/cpacf_aes.c     | 109 +++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |   7 ++
  4 files changed, 123 insertions(+)
target/s390x/gen-features.c +0 -0
index 71e0e41d6e..074c53aecd 100644

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 937

Quoted 6 lines
target/s390x/gen-features.c +3 -0
@@ -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,

Again, either EAES -> PAES or EAES -> ENCRYPTED_AES

Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_KMCTR_AES_128,
      S390_FEAT_KMCTR_AES_192,
      S390_FEAT_KMCTR_AES_256,

[ snip ]

Quoted 14 lines
target/s390x/tcg/cpacf_aes.c +6 -0
index bcfcf3b660..a6487261e1 100644
@@ -620,3 +620,112 @@ int cpacf_paes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
      return !len ? 0 : 3;
  }
+
+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)
+{

[ snip ]

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

Quoted 9 lines
target/s390x/tcg/cpacf_aes.c +9 -0
+    /* 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);
+    }
+    /* 'decrypt' the protected key */
+    for (i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];
+    }

You would already benefit from the suggested outsourcing.

Quoted 7 lines
target/s390x/tcg/cpacf_aes.c +7 -0
+
+    /* expand key */
+    if (mod) {
+        AES_set_decrypt_key(key, keysize * 8, &exkey);
+    } else {
+        AES_set_encrypt_key(key, keysize * 8, &exkey);
+    }

[ snip ]

#28 lore raw

Re: [PATCH v7 11/17] target/s390x: Support protected key AES CBC for cpacf kmc instruction

Quoted 19 lines · Finn Callies
Quoted 5 lines · Harald Freudenberger

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

With the suggested changes:

Quoted 8 lines
---
  target/s390x/gen-features.c      |   3 +
  target/s390x/tcg/cpacf.h         |   4 ++
  target/s390x/tcg/cpacf_aes.c     | 109 
+++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |   7 ++
  4 files changed, 123 insertions(+)
Quoted 14 lines

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 937

Quoted 11 lines
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,

Again, either EAES -> PAES or EAES -> ENCRYPTED_AES

Again - out of the scope of this patch series.

Quoted 34 lines
Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_KMCTR_AES_128,
      S390_FEAT_KMCTR_AES_192,
      S390_FEAT_KMCTR_AES_256,

[ snip ]

Quoted 15 lines
target/s390x/tcg/cpacf_aes.c +2 -0
index bcfcf3b660..a6487261e1 100644
@@ -620,3 +620,112 @@ int cpacf_paes_ecb(CPUS390XState *env, const int 
 mmu_idx, uintptr_t ra,
        return !len ? 0 : 3;
  }
+
+int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t 

ra,

target/s390x/tcg/cpacf_aes.c +4 -0
+                   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)
+{

[ snip ]

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

Quoted 9 lines
target/s390x/tcg/cpacf_aes.c +9 -0
+    /* 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);
+    }
+    /* 'decrypt' the protected key */
+    for (i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];
+    }
Quoted 2 lines

You would already benefit from the suggested outsourcing.

Yep, done.

Quoted 9 lines
Quoted 7 lines
target/s390x/tcg/cpacf_aes.c +7 -0
+
+    /* expand key */
+    if (mod) {
+        AES_set_decrypt_key(key, keysize * 8, &exkey);
+    } else {
+        AES_set_encrypt_key(key, keysize * 8, &exkey);
+    }

[ snip ]

#29 lore raw

PATCH v7 12/17 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     | 90 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  7 +++
 4 files changed, 105 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 e302c17a2f..223bada622 100644
@@ -48,5 +48,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
target/s390x/tcg/cpacf_aes.c +90 -0

93 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index 3fc48bc9a1..60c3ebd79e 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 0x1a: /* CPACF_KMCTR_PAES_128 */
+    case 0x1b: /* CPACF_KMCTR_PAES_192 */
+    case 0x1c: /* 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:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#30 lore raw

Re: [PATCH v7 12/17] target/s390x: Support protected key AES CTR for cpacf kmctr instruction

Quoted 16 lines · 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     | 90 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  7 +++
  4 files changed, 105 insertions(+)
target/s390x/gen-features.c +0 -0
index 074c53aecd..4a131dc191 100644

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 943

Quoted 6 lines
target/s390x/gen-features.c +3 -0
@@ -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,

EAES -> PAES or EAES -> ENCRYPTED_AES

Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_PCC_XTS_AES_128,
      S390_FEAT_PCC_XTS_AES_256,
      S390_FEAT_PCKMO_AES_128,

[ snip ]

Quoted 15 lines
target/s390x/tcg/cpacf_aes.c +7 -0
index a6487261e1..ffa286d422 100644
@@ -729,3 +729,93 @@ int cpacf_paes_cbc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
      return !len ? 0 : 3;
  }
+
+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)
+{

[ snip ]

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

Quoted 8 lines
target/s390x/tcg/cpacf_aes.c +8 -0
+    /* 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);
+    }
+    /* 'decrypt' the protected key */
+    for (i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];

Outsourcing if you see a benefit.

Quoted 4 lines
target/s390x/tcg/cpacf_aes.c +4 -0
+    }
+
+    /* expand key */
+    AES_set_encrypt_key(key, keysize * 8, &exkey);

[ snip ]

#31 lore raw

Re: [PATCH v7 12/17] target/s390x: Support protected key AES CTR for cpacf kmctr instruction

Quoted 13 lines · Finn Callies
Quoted 12 lines · 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     | 90 
++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  7 +++
  4 files changed, 105 insertions(+)
Quoted 14 lines

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 943

Quoted 11 lines
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,

EAES -> PAES or EAES -> ENCRYPTED_AES

Again - not in the scope of this patch series.

Quoted 34 lines
Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_PCC_XTS_AES_128,
      S390_FEAT_PCC_XTS_AES_256,
      S390_FEAT_PCKMO_AES_128,

[ snip ]

Quoted 16 lines
target/s390x/tcg/cpacf_aes.c +2 -0
index a6487261e1..ffa286d422 100644
@@ -729,3 +729,93 @@ int cpacf_paes_cbc(CPUS390XState *env, const int 
 mmu_idx, uintptr_t ra,
        return !len ? 0 : 3;
  }
+
+int cpacf_paes_ctr(CPUS390XState *env, const int mmu_idx, uintptr_t 

ra,

target/s390x/tcg/cpacf_aes.c +5 -0
+                   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)
+{

[ snip ]

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

Quoted 8 lines
target/s390x/tcg/cpacf_aes.c +8 -0
+    /* 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);
+    }
+    /* 'decrypt' the protected key */
+    for (i = 0; i < keysize; i++) {
+        key[i] ^= protkey_xor_pattern[i];
Quoted 2 lines

Outsourcing if you see a benefit.

Yep, done.

Quoted 6 lines
Quoted 4 lines
target/s390x/tcg/cpacf_aes.c +4 -0
+    }
+
+    /* expand key */
+    AES_set_encrypt_key(key, keysize * 8, &exkey);

[ snip ]

#32 lore raw

PATCH v7 13/17 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     | 80 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  4 ++
 4 files changed, 88 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 223bada622..6df474fbc8 100644
@@ -53,5 +53,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
target/s390x/tcg/cpacf_aes.c +80 -0

83 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +4 -0
index 60c3ebd79e..38f7d900fa 100644
@@ -206,6 +206,10 @@ static int cpacf_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     case 0x34: /* CPACF PCC compute XTS param AES-256 */
             rc = cpacf_aes_pcc(env, mmu_idx, ra, env->regs[1], fc);
             break;
+    case 0x3a: /* CPACF_PCC compute XTS param Encrypted AES-128 */
+    case 0x3c: /* CPACF PCC compute XTS param Encrypted AES-256 */
+            rc = cpacf_paes_pcc(env, mmu_idx, ra, env->regs[1], fc);
+            break;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
Reviewed-by on v7 13/17
#33 lore raw

Re: [PATCH v7 13/17] target/s390x: Minimal protected key AES XTS support for cpacf pcc instruction

Quoted 7 lines · 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.

With the suggestions:

Quoted 12 lines
---
  target/s390x/gen-features.c      |  2 +
  target/s390x/tcg/cpacf.h         |  2 +
  target/s390x/tcg/cpacf_aes.c     | 80 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  4 ++
  4 files changed, 88 insertions(+)
target/s390x/gen-features.c +0 -0
index 4a131dc191..126bacb281 100644

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 947

Quoted 5 lines
target/s390x/gen-features.c +2 -0
@@ -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,

EAES -> PAES or EAES -> ENCRYPTED_AES

Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_PCKMO_AES_128,
      S390_FEAT_PCKMO_AES_192,
      S390_FEAT_PCKMO_AES_256,

[ snip ]

#34 lore raw

Re: [PATCH v7 13/17] target/s390x: Minimal protected key AES XTS support for cpacf pcc instruction

Quoted 21 lines · Finn Callies
Quoted 7 lines · 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.

With the suggestions:

Quoted 8 lines
---
  target/s390x/gen-features.c      |  2 +
  target/s390x/tcg/cpacf.h         |  2 +
  target/s390x/tcg/cpacf_aes.c     | 80 
++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  4 ++
  4 files changed, 88 insertions(+)
Quoted 13 lines

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 947

Quoted 10 lines
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,

EAES -> PAES or EAES -> ENCRYPTED_AES

Again - this change is outside of this patch series.

Quoted 5 lines
Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_PCKMO_AES_128,
      S390_FEAT_PCKMO_AES_192,
      S390_FEAT_PCKMO_AES_256,

[ snip ]

Reviewed-by on v7 13/17
#35 lore raw

Re: [PATCH v7 13/17] target/s390x: Minimal protected key AES XTS support for cpacf pcc instruction

In reply to Harald Freudenberger
Quoted 7 lines · 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.

With the suggestions:

Quoted 12 lines
---
  target/s390x/gen-features.c      |  2 +
  target/s390x/tcg/cpacf.h         |  2 +
  target/s390x/tcg/cpacf_aes.c     | 80 ++++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |  4 ++
  4 files changed, 88 insertions(+)
target/s390x/gen-features.c +0 -0
index 4a131dc191..126bacb281 100644

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 947

Quoted 5 lines
target/s390x/gen-features.c +2 -0
@@ -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,

EAES -> PAES or EAES -> ENCRYPTED_AES

Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_PCKMO_AES_128,
      S390_FEAT_PCKMO_AES_192,
      S390_FEAT_PCKMO_AES_256,

[ snip ]

target/s390x/tcg/crypto_helper.c static int cpacf_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra, · line 212

Quoted 12 lines
target/s390x/tcg/crypto_helper.c +4 -0
index 60c3ebd79e..38f7d900fa 100644
@@ -206,6 +206,10 @@ static int cpacf_pcc(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
      case 0x34: /* CPACF PCC compute XTS param AES-256 */
              rc = cpacf_aes_pcc(env, mmu_idx, ra, env->regs[1], fc);
              break;
+    case 0x3a: /* CPACF_PCC compute XTS param Encrypted AES-128 */
+    case 0x3c: /* CPACF PCC compute XTS param Encrypted AES-256 */
+            rc = cpacf_paes_pcc(env, mmu_idx, ra, env->regs[1], fc);
+            break;

Funny enough that you got the ordering here correct xD

Quoted 3 lines
target/s390x/tcg/crypto_helper.c +0 -0
      default:
          g_assert_not_reached();
      }
#36 lore raw

PATCH v7 14/17 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_PAES_256 for the cpacf km instruction.

---
 target/s390x/gen-features.c      |   2 +
 target/s390x/tcg/cpacf.h         |   4 ++
 target/s390x/tcg/cpacf_aes.c     | 106 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   6 ++
 4 files changed, 118 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 6df474fbc8..25990c2df6 100644
@@ -55,5 +55,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
target/s390x/tcg/cpacf_aes.c +106 -0

109 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +6 -0
index 38f7d900fa..8054e4facd 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 0x3a: /* CPACF_KM_PXTS_128 */
+    case 0x3c: /* 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:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
Reviewed-by on v7 14/17
#37 lore raw

Re: [PATCH v7 14/17] target/s390x: Support protected key AES XTS for cpacf km instruction

Quoted 5 lines · Harald Freudenberger

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

With the suggestion:

Quoted 12 lines
---
  target/s390x/gen-features.c      |   2 +
  target/s390x/tcg/cpacf.h         |   4 ++
  target/s390x/tcg/cpacf_aes.c     | 106 +++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |   6 ++
  4 files changed, 118 insertions(+)
target/s390x/gen-features.c +0 -0
index 126bacb281..c4c59c3504 100644

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 933

Quoted 5 lines
target/s390x/gen-features.c +2 -0
@@ -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,

PAES or ENCRYPTED_AES

Quoted 3 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_KMC_AES_128,
      S390_FEAT_KMC_AES_192,
      S390_FEAT_KMC_AES_256,

[ snip ]

#38 lore raw

Re: [PATCH v7 14/17] target/s390x: Support protected key AES XTS for cpacf km instruction

Quoted 19 lines · Finn Callies
Quoted 5 lines · Harald Freudenberger

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

With the suggestion:

Quoted 8 lines
---
  target/s390x/gen-features.c      |   2 +
  target/s390x/tcg/cpacf.h         |   4 ++
  target/s390x/tcg/cpacf_aes.c     | 106 
+++++++++++++++++++++++++++++++
  target/s390x/tcg/crypto_helper.c |   6 ++
  4 files changed, 118 insertions(+)
Quoted 12 lines

target/s390x/gen-features.c static uint16_t qemu_MAX[] = { · line 933

Quoted 10 lines
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,

PAES or ENCRYPTED_AES

Again - not within this patch series.

Quoted 6 lines
Quoted 4 lines
target/s390x/gen-features.c +0 -0
      S390_FEAT_KMC_AES_128,
      S390_FEAT_KMC_AES_192,
      S390_FEAT_KMC_AES_256,

[ snip ]

#39 lore raw

PATCH v7 15/17 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  | 116 +++++++++++++++++++++++++++++++++++
 docs/system/target-s390x.rst |   1 +
 2 files changed, 117 insertions(+)
 create mode 100644 docs/system/s390x/cpacf.rst
docs/system/s390x/cpacf.rst new file +116 -0
new file mode 100644
index 0000000000..85f35b4e9c
@@ -0,0 +1,116 @@
+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 public 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 (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.
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
Reviewed-by on v7 15/17
#40 lore raw

Re: [PATCH v7 15/17] docs/s390: Document CPACF instructions support

Quoted 4 lines · Harald Freudenberger

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

With the one comment applied:

Quoted 91 lines
---
  docs/system/s390x/cpacf.rst  | 116 +++++++++++++++++++++++++++++++++++
  docs/system/target-s390x.rst |   1 +
  2 files changed, 117 insertions(+)

create mode 100644 docs/system/s390x/cpacf.rst

docs/system/s390x/cpacf.rst new file +79 -0
new file mode 100644
index 0000000000..85f35b4e9c
@@ -0,0 +1,116 @@
+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 public 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

docs/system/s390x/cpacf.rst line 89

Quoted 10 lines
docs/system/s390x/cpacf.rst new file +10 -0
+- 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

You list these as unsupported but KMA is missing completely. At least mention it as unsupported as well as you do so for KMO and KMF or leave the unsupported ones out completely.

Quoted 36 lines
docs/system/s390x/cpacf.rst new file +27 -0
+
+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 (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.
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
#41 lore raw

Re: [PATCH v7 15/17] docs/s390: Document CPACF instructions support

Quoted 108 lines · Finn Callies
Quoted 4 lines · Harald Freudenberger

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

With the one comment applied:

Quoted 98 lines
---
  docs/system/s390x/cpacf.rst  | 116 
+++++++++++++++++++++++++++++++++++
  docs/system/target-s390x.rst |   1 +
  2 files changed, 117 insertions(+)

create mode 100644 docs/system/s390x/cpacf.rst

docs/system/s390x/cpacf.rst new file +85 -0
new file mode 100644
index 0000000000..85f35b4e9c
@@ -0,0 +1,116 @@
+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 public 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)
Quoted 9 lines

docs/system/s390x/cpacf.rst line 89

Quoted 4 lines
docs/system/s390x/cpacf.rst new file +4 -0
+- not supported
+
+KMO (CIPHER MESSAGE WITH OUTPUT FEEDBACK)
+- not supported

You list these as unsupported but KMA is missing completely. At least mention it as unsupported as well as you do so for KMO and KMF or leave the unsupported ones out completely.

KMA is MSA 8. I am not sure if qemu even has configured some MSA 8. I'll check this - but as of now all unknown instructions (and KMA is one of this) would result in a Operation Exception. However, at least KMA shold be listed then below. I'll check this maybe add KMA query support or at least document it here.

Update: MSA 8 is listed in qemu. So i added KMA to the docu.

Quoted 15 lines
Quoted 15 lines
docs/system/s390x/cpacf.rst new file +15 -0
+
+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)
Quoted 8 lines

docs/system/s390x/cpacf.rst line 112

Quoted 8 lines
docs/system/s390x/cpacf.rst new file +8 -0
+- 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 (KMF, KMO) cause an Operation Exception

^^^ here I added KMA

Quoted 13 lines
Quoted 13 lines
docs/system/s390x/cpacf.rst new file +4 -0
+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.
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
#42 lore raw

PATCH v7 16/17 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 | 101 +++++++++++++++++++++++++++++++++++++++++++
 crypto/meson.build   |   1 +
 include/crypto/aes.h |  14 ++++++
 3 files changed, 116 insertions(+)
 create mode 100644 crypto/aes-helpers.c
crypto/aes-helpers.c new file +101 -0
new file mode 100644
index 0000000000..8ad3c5132a
@@ -0,0 +1,101 @@
+/*
+ * 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);
+}
+
+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
Reviewed-by on v7 16/17
#43 lore raw

Re: [PATCH v7 16/17] crypto: Add aes-helpers file to support some AES modes

Quoted 10 lines · 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()

I like this, but why do you do this so late in the series instead of implementing it directly like that? Otherwise:

Quoted 6 lines
---
  crypto/aes-helpers.c | 101 +++++++++++++++++++++++++++++++++++++++++++
  crypto/meson.build   |   1 +
  include/crypto/aes.h |  14 ++++++
  3 files changed, 116 insertions(+)

create mode 100644 crypto/aes-helpers.c

[ snip ]

#44 lore raw

Re: [PATCH v7 16/17] crypto: Add aes-helpers file to support some AES modes

Quoted 10 lines · Finn Callies
Quoted 9 lines · 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()

Quoted 6 lines
Quoted 1 line

I like this, but why do you do this so late in the series instead of implementing it directly like that? Otherwise:

This was a suggestion by Holger. And I want to have this separate to be able to remove this and the next patch without the need to rework the implementation.

Quoted 10 lines
Quoted 8 lines
---
  crypto/aes-helpers.c | 101 
+++++++++++++++++++++++++++++++++++++++++++
  crypto/meson.build   |   1 +
  include/crypto/aes.h |  14 ++++++
  3 files changed, 116 insertions(+)

create mode 100644 crypto/aes-helpers.c

[ snip ]

#45 lore raw

PATCH v7 17/17 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 98d5134d5f..9935b6b39c 100644
@@ -119,20 +119,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;
@@ -188,19 +181,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, done += AES_BLOCK_SIZE;
@@ -229,11 +214,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);
@@ -275,12 +259,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);
-        /* 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, done += AES_BLOCK_SIZE;
@@ -361,28 +343,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];
@@ -433,23 +400,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;
     }
 
@@ -627,7 +590,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;
@@ -697,19 +660,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, done += AES_BLOCK_SIZE;
@@ -738,11 +693,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);
@@ -798,12 +752,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, done += AES_BLOCK_SIZE;
@@ -906,7 +858,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;
@@ -971,23 +923,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
Reviewed-by on v7 17/17
#46 lore raw

Re: [PATCH v7 17/17] target/s390x: Use generic AES helper functions

Quoted 4 lines · Harald Freudenberger

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

LGTM

Quoted 3 lines
---
  target/s390x/tcg/cpacf_aes.c | 124 ++++++++++-------------------------
  1 file changed, 36 insertions(+), 88 deletions(-)

[ snip ]

#47 lore raw
In reply to Harald Freudenberger
Quoted 84 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.

Test: As this series is more or less complete, a full blown linux can be run and 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 CPAC instructions.

Harald Freudenberger (17):
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: 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

Quoted 10 lines
  crypto/aes-helpers.c             | 101 ++++
  crypto/meson.build               |   1 +
  docs/system/s390x/cpacf.rst      | 116 ++++
  docs/system/target-s390x.rst     |   1 +
  include/crypto/aes.h             |  14 +
  target/s390x/gen-features.c      |  31 +
  target/s390x/tcg/cpacf.h         |  63 ++
  target/s390x/tcg/cpacf_aes.c     | 955 +++++++++++++++++++++++++++++++
  target/s390x/tcg/cpacf_sha256.c  | 232 ++++++++
  target/s390x/tcg/cpacf_sha512.c  | 245 ++++++++

Since you implemented all aes related stuff into cpacf_aes.c maybe think about putting all sha related stuff into cpacf_sha.c instead of singular files. Or go the singular way with the aes stuff as well.

Quoted 17 lines
  target/s390x/tcg/crypto_helper.c | 423 +++++++-------
  target/s390x/tcg/insn-data.h.inc |   1 +
  target/s390x/tcg/meson.build     |   3 +
  target/s390x/tcg/translate.c     |   2 +
  14 files changed, 1972 insertions(+), 216 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

base-commit: e89049b3ba5f1f0468bc0d294173345597514a1b --
2.43.0

#48 lore raw
Quoted 103 lines · Finn Callies
Quoted 90 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.

Test: As this series is more or less complete, a full blown linux can be run and 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 CPAC instructions.

Harald Freudenberger (17):
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: 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

Quoted 11 lines
  crypto/aes-helpers.c             | 101 ++++
  crypto/meson.build               |   1 +
  docs/system/s390x/cpacf.rst      | 116 ++++
  docs/system/target-s390x.rst     |   1 +
  include/crypto/aes.h             |  14 +
  target/s390x/gen-features.c      |  31 +
  target/s390x/tcg/cpacf.h         |  63 ++
  target/s390x/tcg/cpacf_aes.c     | 955 
+++++++++++++++++++++++++++++++
  target/s390x/tcg/cpacf_sha256.c  | 232 ++++++++
  target/s390x/tcg/cpacf_sha512.c  | 245 ++++++++
Quoted 4 lines

Since you implemented all aes related stuff into cpacf_aes.c maybe think about putting all sha related stuff into cpacf_sha.c instead of singular files. Or go the singular way with the aes stuff as well.

Well, you are right. But ... the sha512 implementation is from Jason Donenfeld and has a extra Copyright. I tried to keep this implementation as close as possible to the original version. So no mixing of any weired copyrights.
And I think the AES implementations belong together, similar for example when ECC implementations come up (:-O) or some hmacs or so.

Quoted 17 lines
Quoted 17 lines
  target/s390x/tcg/crypto_helper.c | 423 +++++++-------
  target/s390x/tcg/insn-data.h.inc |   1 +
  target/s390x/tcg/meson.build     |   3 +
  target/s390x/tcg/translate.c     |   2 +
  14 files changed, 1972 insertions(+), 216 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

base-commit: e89049b3ba5f1f0468bc0d294173345597514a1b --
2.43.0