Skip to contentSkip to conversation

s390: Extend qemu cpacf support

PATCH Superseded Harald Freudenberger 2 participants 18 messages Jan 23 – Feb 2 Arm/Amlogic Meson SoC support

v3 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v3 cover s390: 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. Eric Biggers and others have asked to be able to verify our s390 cpacf exploitation code. 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.

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

 target/s390x/gen-features.c      |  31 +
 target/s390x/tcg/cpacf.h         |  51 ++
 target/s390x/tcg/cpacf_aes.c     | 969 +++++++++++++++++++++++++++++++
 target/s390x/tcg/cpacf_sha256.c  | 229 ++++++++
 target/s390x/tcg/cpacf_sha512.c  | 241 ++++++++
 target/s390x/tcg/crypto_helper.c | 416 +++++++------
 target/s390x/tcg/insn-data.h.inc |   1 +
 target/s390x/tcg/meson.build     |   3 +
 target/s390x/tcg/translate.c     |   7 +
 9 files changed, 1734 insertions(+), 214 deletions(-)
 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

--
2.43.0

#1 lore raw

PATCH v3 1/14 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 | 85 ++++++++++++++++++++++++++------
 target/s390x/tcg/insn-data.h.inc |  1 +
 target/s390x/tcg/translate.c     |  7 +++
 3 files changed, 79 insertions(+), 14 deletions(-)
target/s390x/tcg/crypto_helper.c +71 -14
index 4447bb66ee..0b00d8ba5b 100644
@@ -268,6 +268,57 @@ static void fill_buf_random(CPUS390XState *env, uintptr_t ra,
     }
 }
 
+static int cpacf_kimd(CPUS390XState *env, 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, 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 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, 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, 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, ra, &env->regs[r1], &env->regs[r1 + 1]);
+        fill_buf_random(env, 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)
 {
@@ -276,14 +327,15 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     const uint8_t fc = env->regs[0] & 0x7fULL;
     uint8_t subfunc[16] = { 0 };
     uint64_t param_addr;
-    int i;
+    int i, 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);
         }
@@ -295,24 +347,29 @@ 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 */
-        for (i = 0; i < 16; i++) {
+    /* handle query subfunction */
+    if (fc == 0) {
+        for (i = 0; i < sizeof(subfunc); i++) {
             param_addr = wrap_address(env, env->regs[1] + i);
             cpu_stb_data_ra(env, param_addr, subfunc[i], ra);
         }
+        goto out;
+    }
+
+    switch (type) {
+    case S390_FEAT_TYPE_KIMD:
+        rc = cpacf_kimd(env, ra, r1, r2, r3, fc);
         break;
-    case 3: /* CPACF_*_SHA_512 */
-        return cpacf_sha512(env, ra, env->regs[1], &env->regs[r2],
-                            &env->regs[r2 + 1], type);
-    case 114: /* CPACF_PRNO_TRNG */
-        fill_buf_random(env, ra, &env->regs[r1], &env->regs[r1 + 1]);
-        fill_buf_random(env, ra, &env->regs[r2], &env->regs[r2 + 1]);
+    case S390_FEAT_TYPE_KLMD:
+        rc = cpacf_klmd(env, ra, r1, r2, r3, fc);
+        break;
+    case S390_FEAT_TYPE_PPNO:
+        rc = cpacf_ppno(env, 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 ec730ee091..b6095711f2 100644
@@ -1012,6 +1012,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 +7 -0
index 4d2b8c5e2b..a5fdc05d37 100644
@@ -2565,6 +2565,12 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
     case S390_FEAT_TYPE_PCKMO:
     case S390_FEAT_TYPE_PCC:
         break;
+    case S390_FEAT_TYPE_KDSA:
+        if (r1) {
+            gen_program_exception(s, PGM_SPECIFICATION);
+            return DISAS_NORETURN;
+        }
+        break;
     default:
         g_assert_not_reached();
     };
@@ -6018,6 +6024,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

Re: [PATCH v3 01/14] target/s390x: Rework s390 cpacf implementations

Quoted 5 lines · Harald Freudenberger

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

[...]

target/s390x/tcg/insn-data.h.inc line 1016

Quoted 10 lines
target/s390x/tcg/insn-data.h.inc +1 -0
index ec730ee091..b6095711f2 100644
@@ -1012,6 +1012,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)

@Christian:
Is it time to do the rename to PRNO?
According to the architecture we deprecated PPNO in favor of PRNO with introduction of MSA7.

target/s390x/tcg/translate.c static DisasJumpType op_msa(DisasContext *s, DisasOps *o) · line 2570

Quoted 13 lines
target/s390x/tcg/insn-data.h.inc +0 -0
      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 +3 -0
index 4d2b8c5e2b..a5fdc05d37 100644
@@ -2565,6 +2565,12 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
      case S390_FEAT_TYPE_PCKMO:
      case S390_FEAT_TYPE_PCC:
          break;
+    case S390_FEAT_TYPE_KDSA:
+        if (r1) {
+            gen_program_exception(s, PGM_SPECIFICATION);

Are you sure that's a spec?

r1 and the 8 free bits in the instruction should be set to zero but the architecture doesn't say you'll get a spec if they are not 0.

The architecture reserves the right to use r1 and the free bits for new function codes. If you don't set them to 0 you might invoke new functionality that you don't know about.

Quoted 14 lines
target/s390x/tcg/translate.c +4 -0
+            return DISAS_NORETURN;
+        }
+        break;
      default:
          g_assert_not_reached();
      };
@@ -6018,6 +6024,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
#3 lore raw

Re: [PATCH v3 01/14] target/s390x: Rework s390 cpacf implementations

Quoted 47 lines · Janosch Frank
Quoted 5 lines · Harald Freudenberger

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

[...]

Quoted 13 lines
target/s390x/tcg/insn-data.h.inc +1 -0
index ec730ee091..b6095711f2 100644
@@ -1012,6 +1012,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)

Quoted 2 lines
      E(0xb93c, PPNO,    RRE,   MSA5, 0, 0, 0, 0, msa, 0, 

S390_FEAT_TYPE_PPNO, IF_IO)

@Christian:
Is it time to do the rename to PRNO?
According to the architecture we deprecated PPNO in favor of PRNO with introduction of MSA7.

Quoted 9 lines
      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 +0 -0
index 4d2b8c5e2b..a5fdc05d37 100644

target/s390x/tcg/translate.c static DisasJumpType op_msa(DisasContext *s, · line 2571

Quoted 7 lines
target/s390x/tcg/translate.c +3 -0
@@ -2565,6 +2565,12 @@ static DisasJumpType op_msa(DisasContext *s, 
 DisasOps *o)
      case S390_FEAT_TYPE_PCKMO:
      case S390_FEAT_TYPE_PCC:
          break;
+    case S390_FEAT_TYPE_KDSA:
+        if (r1) {
+            gen_program_exception(s, PGM_SPECIFICATION);

Are you sure that's a spec?

Quoted 7 lines

r1 and the 8 free bits in the instruction should be set to zero but the architecture doesn't say you'll get a spec if they are not 0.

The architecture reserves the right to use r1 and the free bits for new function codes. If you don't set them to 0 you might invoke new functionality that you don't know about.

Yep, you are right. This check is not needed at all. The code should look like:

       ...
       case S390_FEAT_TYPE_PCKMO:
       case S390_FEAT_TYPE_PCC:
       case S390_FEAT_TYPE_KDSA:
           break;
       ...
Quoted 18 lines
Quoted 18 lines
target/s390x/tcg/translate.c +3 -0
+            return DISAS_NORETURN;
+        }
+        break;
      default:
          g_assert_not_reached();
      };
target/s390x/tcg/translate.c +0 -0
@@ -6018,6 +6024,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
#4 lore raw

PATCH v3 2/14 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         |  15 ++
 target/s390x/tcg/cpacf_sha512.c  | 241 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 225 +----------------------------
 target/s390x/tcg/meson.build     |   1 +
 4 files changed, 258 insertions(+), 224 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 +15 -0
new file mode 100644
index 0000000000..99b8a11232
@@ -0,0 +1,15 @@
+/*
+ * 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, 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 +241 -0

241 lines not on this page · raw message

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

231 lines not on this page · raw message

target/s390x/tcg/meson.build +1 -0
index ee4e8fec77..6dbc7ead65 100644
@@ -1,5 +1,6 @@
 s390x_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
#5 lore raw

PATCH v3 3/14 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         |   4 +
 target/s390x/tcg/cpacf_sha256.c  | 229 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   8 ++
 target/s390x/tcg/meson.build     |   1 +
 5 files changed, 244 insertions(+)
 create mode 100644 target/s390x/tcg/cpacf_sha256.c
target/s390x/gen-features.c +2 -0
index 8218e6470e..5cf5b92c37 100644
@@ -916,7 +916,9 @@ static uint16_t qemu_V7_1[] = {
  */
 static uint16_t qemu_MAX[] = {
     S390_FEAT_MSA_EXT_5,
+    S390_FEAT_KIMD_SHA_256,
     S390_FEAT_KIMD_SHA_512,
+    S390_FEAT_KLMD_SHA_256,
     S390_FEAT_KLMD_SHA_512,
     S390_FEAT_PRNO_TRNG,
 };
target/s390x/tcg/cpacf.h +4 -0
index 99b8a11232..79f05e1e14 100644
@@ -8,6 +8,10 @@
 #ifndef S390X_CPACF_H
 #define S390X_CPACF_H
 
+/* from crypto_sha256.c */
+int cpacf_sha256(CPUS390XState *env, 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, uintptr_t ra, uint64_t param_addr,
                  uint64_t *message_reg, uint64_t *len_reg, uint32_t type);
target/s390x/tcg/cpacf_sha256.c new file +229 -0

229 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +8 -0
index 2d1c0290b5..b69dbb61a6 100644
@@ -51,6 +51,10 @@ static int cpacf_kimd(CPUS390XState *env, const uintptr_t ra,
     int rc = 0;
 
     switch (fc) {
+    case 0x02: /* CPACF_KIMD_SHA_256 */
+        rc = cpacf_sha256(env, 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, ra, env->regs[1], &env->regs[r2],
                           &env->regs[r2 + 1], S390_FEAT_TYPE_KIMD);
@@ -68,6 +72,10 @@ static int cpacf_klmd(CPUS390XState *env, const uintptr_t ra,
     int rc = 0;
 
     switch (fc) {
+    case 0x02: /* CPACF_KLMD_SHA_256 */
+        rc = cpacf_sha256(env, 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, ra, env->regs[1], &env->regs[r2],
                           &env->regs[r2 + 1], S390_FEAT_TYPE_KLMD);
target/s390x/tcg/meson.build +1 -0
index 6dbc7ead65..4115f4c704 100644
@@ -1,5 +1,6 @@
 s390x_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
#6 lore raw

PATCH v3 4/14 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         |   5 ++
 target/s390x/tcg/cpacf_aes.c     | 108 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  24 +++++++
 target/s390x/tcg/meson.build     |   1 +
 5 files changed, 141 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 +5 -0
index 79f05e1e14..06423abc00 100644
@@ -16,4 +16,9 @@ int cpacf_sha256(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_sha512(CPUS390XState *env, 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, uintptr_t ra, uint64_t param_addr,
+                  uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                  uint32_t type, uint8_t fc, uint8_t mod);
+
 #endif
target/s390x/tcg/cpacf_aes.c new file +108 -0

108 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +24 -0
index b69dbb61a6..dff119176e 100644
@@ -87,6 +87,27 @@ static int cpacf_klmd(CPUS390XState *env, const uintptr_t ra,
     return rc;
 }
 
+
+static int cpacf_km(CPUS390XState *env, 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, 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, uintptr_t ra,
                       uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
 {
@@ -151,6 +172,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, ra, r1, r2, r3, fc);
         break;
+    case S390_FEAT_TYPE_KM:
+        rc = cpacf_km(env, ra, r1, r2, r3, fc, mod);
+        break;
     default:
         g_assert_not_reached();
     }
target/s390x/tcg/meson.build +1 -0
index 4115f4c704..da948471ea 100644
@@ -1,5 +1,6 @@
 s390x_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
#7 lore raw

Re: [PATCH v3 04/14] 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.

---

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+int cpacf_aes_ecb(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                  uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,

You're passing registers as pointers with names that sound like they aren't registers. I had to look at this a couple of times until I understood why you're seemingly not touching registers in this function.

Additionally you don't even need to pass them because you have "env" as the first parameter anyway.

Quoted 17 lines
target/s390x/tcg/cpacf_aes.c new file +17 -0
+                  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];
+    uint64_t addr, len = *src_len, processed = 0;
+    int i, keysize, data_reg_len = 64;
+    uint8_t key[32];
+    AES_KEY exkey;
+
+    g_assert(type == S390_FEAT_TYPE_KM);
+    switch (fc) {
+    case 0x12: /* CPACF_KM_AES_128 */
+        keysize = 16;
+        break;
+    case 0x13: /* CPACF_KM_AES_192 */
+        keysize = 24;
+        break;

target/s390x/tcg/cpacf_aes.c line 70

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c new file +10 -0
+    case 0x14: /* CPACF_KM_AES_256 */
+        keysize = 32;
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    if (!(env->psw.mask & PSW_MASK_64)) {
+        len = (uint32_t)len;
+        data_reg_len = (env->psw.mask & PSW_MASK_32) ? 32 : 24;

You're NOT working on data registers, you're handling address registers and hence you have to determine addressing size. The naming is very misleading.

Quoted 13 lines
target/s390x/tcg/cpacf_aes.c new file +13 -0
+    }
+
+    /* length has to be properly aligned. */
+    if (!QEMU_IS_ALIGNED(len, AES_BLOCK_SIZE)) {
+        tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
+    }
+
+    /* fetch key from param block */
+    for (i = 0; i < keysize; i++) {
+        addr = wrap_address(env, param_addr + i);
+        key[i] = cpu_ldub_data_ra(env, addr, ra);
+    }
+

target/s390x/tcg/cpacf_aes.c line 93

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c new file +10 -0
+    /* expand key */
+    if (mod) {
+        AES_set_decrypt_key(key, keysize * 8, &exkey);
+    } else {
+        AES_set_encrypt_key(key, keysize * 8, &exkey);
+    }
+
+    /* process up to MAX_BLOCKS_PER_RUN aes blocks */
+    for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
+        aes_read_block(env, *src_ptr + processed, in, ra);

Yeah, you effectively have a double pointer since you're pointing to a register in memory that contains an address.

Quoted 4 lines
target/s390x/tcg/cpacf_aes.c new file +4 -0
+        if (mod) {
+            AES_decrypt(in, out, &exkey);
+        } else {
+            AES_encrypt(in, out, &exkey);

target/s390x/tcg/cpacf_aes.c line 107

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c new file +10 -0
+        }
+        aes_write_block(env, *dst_ptr + processed, out, ra);
+        len -= AES_BLOCK_SIZE, processed += AES_BLOCK_SIZE;
+    }
+
+    *src_ptr = deposit64(*src_ptr, 0, data_reg_len, *src_ptr + processed);
+    *dst_ptr = deposit64(*dst_ptr, 0, data_reg_len, *dst_ptr + processed);
+    *src_len -= processed;
+
+    return !len ? 0 : 3;

I'm fine with adding variables to avoid having to index env->regs[]. But please improve the naming and make it clear where you have a register backing it.

#8 lore raw

PATCH v3 5/14 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         |   3 +
 target/s390x/tcg/cpacf_aes.c     | 100 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  23 +++++++
 4 files changed, 129 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 +3 -0
index 06423abc00..18686991f7 100644
@@ -20,5 +20,8 @@ int cpacf_sha512(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_aes_ecb(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
                   uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_aes_cbc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                  uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                  uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif
target/s390x/tcg/cpacf_aes.c +100 -0

103 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +23 -0
index dff119176e..df01c1c54e 100644
@@ -108,6 +108,26 @@ static int cpacf_km(CPUS390XState *env, uintptr_t ra, uint32_t r1,
     return rc;
 }
 
+static int cpacf_kmc(CPUS390XState *env, 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, 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, uintptr_t ra,
                       uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
 {
@@ -175,6 +195,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, ra, r1, r2, r3, fc, mod);
         break;
+    case S390_FEAT_TYPE_KMC:
+        rc = cpacf_kmc(env, ra, r1, r2, r3, fc, mod);
+        break;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#9 lore raw

PATCH v3 6/14 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         |  3 ++
 target/s390x/tcg/cpacf_aes.c     | 70 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 23 +++++++++++
 4 files changed, 99 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 +3 -0
index 18686991f7..21fe2e4690 100644
@@ -23,5 +23,8 @@ int cpacf_aes_ecb(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_aes_cbc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
                   uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_aes_ctr(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                  uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                  uint64_t *ctr_ptr, uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif
target/s390x/tcg/cpacf_aes.c +70 -0

73 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +23 -0
index df01c1c54e..c1c79daf19 100644
@@ -128,6 +128,26 @@ static int cpacf_kmc(CPUS390XState *env, uintptr_t ra, uint32_t r1,
     return rc;
 }
 
+static int cpacf_kmctr(CPUS390XState *env, 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, 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, uintptr_t ra,
                       uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
 {
@@ -198,6 +218,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, ra, r1, r2, r3, fc, mod);
         break;
+    case S390_FEAT_TYPE_KMCTR:
+        rc = cpacf_kmctr(env, ra, r1, r2, r3, fc, mod);
+        break;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#10 lore raw

PATCH v3 7/14 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     | 70 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 19 +++++++++
 4 files changed, 93 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 21fe2e4690..aa39a3c85a 100644
@@ -26,5 +26,7 @@ int cpacf_aes_cbc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_aes_ctr(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
                   uint64_t *ctr_ptr, uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_aes_pcc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                  uint8_t fc);
 
 #endif
target/s390x/tcg/cpacf_aes.c +70 -0

79 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +19 -0
index c1c79daf19..a7f4e135e1 100644
@@ -165,6 +165,22 @@ static int cpacf_ppno(CPUS390XState *env, uintptr_t ra,
     return rc;
 }
 
+static int cpacf_pcc(CPUS390XState *env, 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, 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)
 {
@@ -221,6 +237,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, ra, r1, r2, r3, fc, mod);
         break;
+    case S390_FEAT_TYPE_PCC:
+        rc = cpacf_pcc(env, ra, fc);
+        break;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#11 lore raw

PATCH v3 8/14 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         |   3 +
 target/s390x/tcg/cpacf_aes.c     | 103 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   6 ++
 4 files changed, 114 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 +3 -0
index aa39a3c85a..8c1dbee67f 100644
@@ -28,5 +28,8 @@ int cpacf_aes_ctr(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                   uint64_t *ctr_ptr, uint32_t type, uint8_t fc, uint8_t mod);
 int cpacf_aes_pcc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                   uint8_t fc);
+int cpacf_aes_xts(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                  uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                  uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif
target/s390x/tcg/cpacf_aes.c +103 -0

106 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +6 -0
index a7f4e135e1..7129b38703 100644
@@ -101,6 +101,12 @@ static int cpacf_km(CPUS390XState *env, uintptr_t ra, uint32_t r1,
                            &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, 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
#12 lore raw

PATCH v3 9/14 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     | 65 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c | 20 ++++++++++
 4 files changed, 90 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 8c1dbee67f..2617cbed80 100644
@@ -31,5 +31,7 @@ int cpacf_aes_pcc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_aes_xts(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
                   uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_aes_pckmo(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                    uint8_t fc);
 
 #endif
target/s390x/tcg/cpacf_aes.c +65 -0

68 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +20 -0
index 7129b38703..8d1070ec89 100644
@@ -187,6 +187,23 @@ static int cpacf_pcc(CPUS390XState *env, uintptr_t ra, uint8_t fc)
     return rc;
 }
 
+static int cpacf_pckmo(CPUS390XState *env, 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, 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)
 {
@@ -246,6 +263,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, ra, fc);
         break;
+    case S390_FEAT_TYPE_PCKMO:
+        rc = cpacf_pckmo(env, ra, fc);
+        break;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#13 lore raw

PATCH v3 10/14 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         |  3 ++
 target/s390x/tcg/cpacf_aes.c     | 83 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  7 +++
 4 files changed, 96 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 +3 -0
index 2617cbed80..4c1e0ee58d 100644
@@ -33,5 +33,8 @@ int cpacf_aes_xts(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                   uint32_t type, uint8_t fc, uint8_t mod);
 int cpacf_aes_pckmo(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                     uint8_t fc);
+int cpacf_paes_ecb(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                   uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif
target/s390x/tcg/cpacf_aes.c +83 -0

86 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index 8d1070ec89..146f970713 100644
@@ -101,6 +101,13 @@ static int cpacf_km(CPUS390XState *env, uintptr_t ra, uint32_t r1,
                            &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, 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, ra, env->regs[1],
Signature · 1 line
2.43.0
#14 lore raw

PATCH v3 11/14 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         |   3 +
 target/s390x/tcg/cpacf_aes.c     | 105 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   7 +++
 4 files changed, 118 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 +3 -0
index 4c1e0ee58d..920c1f50fb 100644
@@ -36,5 +36,8 @@ int cpacf_aes_pckmo(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_paes_ecb(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                    uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
                    uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_paes_cbc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                   uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif
target/s390x/tcg/cpacf_aes.c +105 -0

108 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index 146f970713..a69f4edf74 100644
@@ -134,6 +134,13 @@ static int cpacf_kmc(CPUS390XState *env, uintptr_t ra, uint32_t r1,
                            &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, 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
#15 lore raw

PATCH v3 12/14 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         |  3 ++
 target/s390x/tcg/cpacf_aes.c     | 84 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  7 +++
 4 files changed, 97 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 +3 -0
index 920c1f50fb..35518aef4b 100644
@@ -39,5 +39,8 @@ int cpacf_paes_ecb(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_paes_cbc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                    uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
                    uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_paes_ctr(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                   uint64_t *ctr_ptr, uint32_t type, uint8_t fc, uint8_t mod);
 
 #endif
target/s390x/tcg/cpacf_aes.c +84 -0

87 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +7 -0
index a69f4edf74..2791e69f84 100644
@@ -161,6 +161,13 @@ static int cpacf_kmctr(CPUS390XState *env, uintptr_t ra, uint32_t r1,
                            &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, 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
#16 lore raw

PATCH v3 13/14 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     | 79 ++++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |  4 ++
 4 files changed, 87 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 35518aef4b..9d0801b217 100644
@@ -42,5 +42,7 @@ int cpacf_paes_cbc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
 int cpacf_paes_ctr(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                    uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
                    uint64_t *ctr_ptr, uint32_t type, uint8_t fc, uint8_t mod);
+int cpacf_paes_pcc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                   uint8_t fc);
 
 #endif
target/s390x/tcg/cpacf_aes.c +79 -0

82 lines not on this page · raw message

target/s390x/tcg/crypto_helper.c +4 -0
index 2791e69f84..3ff8331993 100644
@@ -201,6 +201,10 @@ static int cpacf_pcc(CPUS390XState *env, uintptr_t ra, uint8_t fc)
     case 0x34: /* CPACF PCC compute XTS param AES-256 */
             rc = cpacf_aes_pcc(env, 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, ra, env->regs[1], fc);
+            break;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.43.0
#17 lore raw

PATCH v3 14/14 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         |   3 +
 target/s390x/tcg/cpacf_aes.c     | 102 +++++++++++++++++++++++++++++++
 target/s390x/tcg/crypto_helper.c |   6 ++
 4 files changed, 113 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 +3 -0
index 9d0801b217..47dfa6bef7 100644
@@ -44,5 +44,8 @@ int cpacf_paes_ctr(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                    uint64_t *ctr_ptr, uint32_t type, uint8_t fc, uint8_t mod);
 int cpacf_paes_pcc(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
                    uint8_t fc);
+int cpacf_paes_xts(CPUS390XState *env, uintptr_t ra, uint64_t param_addr,
+                   uint64_t *dst_ptr, uint64_t *src_ptr, uint64_t *src_len,
+                   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 +6 -0
index 3ff8331993..1dd61cac5e 100644
@@ -114,6 +114,12 @@ static int cpacf_km(CPUS390XState *env, uintptr_t ra, uint32_t r1,
                            &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, 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