Blame SOURCES/openssl-1.1.1-s390x-ecc.patch

cf3dd1
diff -up openssl-1.1.1g/Configurations/00-base-templates.conf.s390x-ecc openssl-1.1.1g/Configurations/00-base-templates.conf
cf3dd1
--- openssl-1.1.1g/Configurations/00-base-templates.conf.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/Configurations/00-base-templates.conf	2020-05-18 12:45:40.855234262 +0200
b63792
@@ -289,6 +289,7 @@ my %targets=(
b63792
 	template	=> 1,
b63792
 	cpuid_asm_src   => "s390xcap.c s390xcpuid.S",
b63792
 	bn_asm_src      => "asm/s390x.S s390x-mont.S s390x-gf2m.s",
b63792
+	ec_asm_src      => "ecp_s390x_nistp.c",
b63792
 	aes_asm_src     => "aes-s390x.S aes-ctr.fake aes-xts.fake",
b63792
 	sha1_asm_src    => "sha1-s390x.S sha256-s390x.S sha512-s390x.S",
b63792
 	rc4_asm_src     => "rc4-s390x.s",
cf3dd1
diff -up openssl-1.1.1g/Configure.s390x-ecc openssl-1.1.1g/Configure
cf3dd1
--- openssl-1.1.1g/Configure.s390x-ecc	2020-05-18 12:45:40.781233618 +0200
cf3dd1
+++ openssl-1.1.1g/Configure	2020-05-18 12:45:40.856234270 +0200
cf3dd1
@@ -1398,6 +1398,9 @@ unless ($disabled{asm}) {
b63792
     if ($target{ec_asm_src} =~ /ecp_nistz256/) {
b63792
         push @{$config{lib_defines}}, "ECP_NISTZ256_ASM";
b63792
     }
b63792
+    if ($target{ec_asm_src} =~ /ecp_s390x_nistp/) {
b63792
+        push @{$config{lib_defines}}, "S390X_EC_ASM";
b63792
+    }
b63792
     if ($target{ec_asm_src} =~ /x25519/) {
b63792
         push @{$config{lib_defines}}, "X25519_ASM";
b63792
     }
cf3dd1
diff -up openssl-1.1.1g/crypto/cmac/cm_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/cmac/cm_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/cmac/cm_pmeth.c.s390x-ecc	2020-05-18 12:45:40.782233627 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/cmac/cm_pmeth.c	2020-05-18 12:45:42.661249957 +0200
b63792
@@ -159,3 +159,8 @@ const EVP_PKEY_METHOD cmac_pkey_meth = {
b63792
     pkey_cmac_ctrl,
b63792
     pkey_cmac_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *cmac_pkey_method(void)
b63792
+{
b63792
+    return &cmac_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/dh/dh_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/dh/dh_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/dh/dh_pmeth.c.s390x-ecc	2020-05-18 12:45:40.782233627 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/dh/dh_pmeth.c	2020-05-18 12:45:42.661249957 +0200
b63792
@@ -512,6 +512,11 @@ const EVP_PKEY_METHOD dh_pkey_meth = {
b63792
     pkey_dh_ctrl_str
b63792
 };
b63792
 
b63792
+const EVP_PKEY_METHOD *dh_pkey_method(void)
b63792
+{
b63792
+    return &dh_pkey_meth;
b63792
+}
b63792
+
b63792
 const EVP_PKEY_METHOD dhx_pkey_meth = {
b63792
     EVP_PKEY_DHX,
b63792
     EVP_PKEY_FLAG_FIPS,
b63792
@@ -545,3 +550,8 @@ const EVP_PKEY_METHOD dhx_pkey_meth = {
b63792
     pkey_dh_ctrl,
b63792
     pkey_dh_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *dhx_pkey_method(void)
b63792
+{
b63792
+    return &dhx_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/dsa/dsa_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/dsa/dsa_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/dsa/dsa_pmeth.c.s390x-ecc	2020-05-18 12:45:40.783233636 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/dsa/dsa_pmeth.c	2020-05-18 12:45:42.662249966 +0200
cf3dd1
@@ -271,3 +271,8 @@ const EVP_PKEY_METHOD dsa_pkey_meth = {
b63792
     pkey_dsa_ctrl,
b63792
     pkey_dsa_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *dsa_pkey_method(void)
b63792
+{
b63792
+    return &dsa_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/build.info.s390x-ecc openssl-1.1.1g/crypto/ec/build.info
cf3dd1
--- openssl-1.1.1g/crypto/ec/build.info.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/build.info	2020-05-18 12:45:42.662249966 +0200
b63792
@@ -26,6 +26,9 @@ GENERATE[ecp_nistz256-armv8.S]=asm/ecp_n
b63792
 INCLUDE[ecp_nistz256-armv8.o]=..
b63792
 GENERATE[ecp_nistz256-ppc64.s]=asm/ecp_nistz256-ppc64.pl $(PERLASM_SCHEME)
b63792
 
b63792
+INCLUDE[ecp_s390x_nistp.o]=..
b63792
+INCLUDE[ecx_meth.o]=..
b63792
+
b63792
 GENERATE[x25519-x86_64.s]=asm/x25519-x86_64.pl $(PERLASM_SCHEME)
b63792
 GENERATE[x25519-ppc64.s]=asm/x25519-ppc64.pl $(PERLASM_SCHEME)
b63792
 
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ec_curve.c.s390x-ecc openssl-1.1.1g/crypto/ec/ec_curve.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ec_curve.c.s390x-ecc	2020-05-18 12:45:40.753233375 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ec_curve.c	2020-05-18 12:45:42.663249975 +0200
b63792
@@ -255,20 +255,29 @@ static const ec_list_element curve_list[
b63792
     {NID_secp256k1, &_EC_SECG_PRIME_256K1.h, 0,
b63792
      "SECG curve over a 256 bit prime field"},
b63792
     /* SECG secp256r1 is the same as X9.62 prime256v1 and hence omitted */
b63792
-    {NID_secp384r1, &_EC_NIST_PRIME_384.h, 0,
b63792
+    {NID_secp384r1, &_EC_NIST_PRIME_384.h,
b63792
+# if defined(S390X_EC_ASM)
b63792
+     EC_GFp_s390x_nistp384_method,
b63792
+# else
b63792
+     0,
b63792
+# endif
b63792
      "NIST/SECG curve over a 384 bit prime field"},
b63792
-#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
b63792
-    {NID_secp521r1, &_EC_NIST_PRIME_521.h, EC_GFp_nistp521_method,
b63792
-     "NIST/SECG curve over a 521 bit prime field"},
b63792
-#else
b63792
-    {NID_secp521r1, &_EC_NIST_PRIME_521.h, 0,
b63792
+    {NID_secp521r1, &_EC_NIST_PRIME_521.h,
b63792
+# if defined(S390X_EC_ASM)
b63792
+     EC_GFp_s390x_nistp521_method,
b63792
+# elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
b63792
+     EC_GFp_nistp521_method,
b63792
+# else
b63792
+     0,
b63792
+# endif
b63792
      "NIST/SECG curve over a 521 bit prime field"},
b63792
-#endif
b63792
     /* X9.62 curves */
b63792
     {NID_X9_62_prime256v1, &_EC_X9_62_PRIME_256V1.h,
b63792
 #if defined(ECP_NISTZ256_ASM)
b63792
      EC_GFp_nistz256_method,
b63792
-#elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
b63792
+# elif defined(S390X_EC_ASM)
b63792
+     EC_GFp_s390x_nistp256_method,
b63792
+# elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
b63792
      EC_GFp_nistp256_method,
b63792
 #else
b63792
      0,
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecdsa_ossl.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecdsa_ossl.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecdsa_ossl.c.s390x-ecc	2020-05-18 12:45:40.784233644 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecdsa_ossl.c	2020-05-18 12:45:42.664249983 +0200
b63792
@@ -18,6 +18,41 @@
b63792
 # include <openssl/fips.h>
b63792
 #endif
b63792
 
b63792
+int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
b63792
+                          BIGNUM **rp)
b63792
+{
b63792
+    if (eckey->group->meth->ecdsa_sign_setup == NULL) {
b63792
+        ECerr(EC_F_OSSL_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
b63792
+        return 0;
b63792
+    }
b63792
+
b63792
+    return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
b63792
+}
b63792
+
b63792
+ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
b63792
+                               const BIGNUM *in_kinv, const BIGNUM *in_r,
b63792
+                               EC_KEY *eckey)
b63792
+{
b63792
+    if (eckey->group->meth->ecdsa_sign_sig == NULL) {
b63792
+        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
b63792
+        return NULL;
b63792
+    }
b63792
+
b63792
+    return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
b63792
+                                              in_kinv, in_r, eckey);
b63792
+}
b63792
+
b63792
+int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
b63792
+                          const ECDSA_SIG *sig, EC_KEY *eckey)
b63792
+{
b63792
+    if (eckey->group->meth->ecdsa_verify_sig == NULL) {
b63792
+        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
b63792
+        return 0;
b63792
+    }
b63792
+
b63792
+    return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
b63792
+}
b63792
+
b63792
 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
b63792
                     unsigned char *sig, unsigned int *siglen,
b63792
                     const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
cf3dd1
@@ -149,15 +184,15 @@ static int ecdsa_sign_setup(EC_KEY *ecke
b63792
     return ret;
b63792
 }
b63792
 
b63792
-int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
b63792
-                          BIGNUM **rp)
b63792
+int ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
b63792
+                            BIGNUM **rp)
b63792
 {
b63792
     return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
b63792
 }
b63792
 
b63792
-ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
b63792
-                               const BIGNUM *in_kinv, const BIGNUM *in_r,
b63792
-                               EC_KEY *eckey)
b63792
+ECDSA_SIG *ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
b63792
+                                 const BIGNUM *in_kinv, const BIGNUM *in_r,
b63792
+                                 EC_KEY *eckey)
b63792
 {
b63792
     int ok = 0, i;
b63792
     BIGNUM *kinv = NULL, *s, *m = NULL;
cf3dd1
@@ -218,25 +253,25 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const uns
b63792
     if (8 * dgst_len > i)
b63792
         dgst_len = (i + 7) / 8;
b63792
     if (!BN_bin2bn(dgst, dgst_len, m)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
     /* If still too long, truncate remaining bits with a shift */
b63792
     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
     do {
b63792
         if (in_kinv == NULL || in_r == NULL) {
b63792
             if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
b63792
-                ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
b63792
+                ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_ECDSA_LIB);
b63792
                 goto err;
b63792
             }
b63792
             ckinv = kinv;
b63792
         } else {
b63792
             ckinv = in_kinv;
b63792
             if (BN_copy(ret->r, in_r) == NULL) {
b63792
-                ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
b63792
+                ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_MALLOC_FAILURE);
b63792
                 goto err;
b63792
             }
b63792
         }
cf3dd1
@@ -250,11 +285,11 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const uns
b63792
          */
b63792
         if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
b63792
             || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
b63792
-            ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
b63792
+            ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
b63792
             goto err;
b63792
         }
b63792
         if (!bn_mod_add_fixed_top(s, s, m, order)) {
b63792
-            ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
b63792
+            ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
b63792
             goto err;
b63792
         }
b63792
         /*
cf3dd1
@@ -263,7 +298,7 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const uns
b63792
          */
b63792
         if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
b63792
             || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
b63792
-            ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
b63792
+            ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
b63792
             goto err;
b63792
         }
b63792
 
cf3dd1
@@ -273,7 +308,7 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const uns
b63792
              * generate new kinv and r values
b63792
              */
b63792
             if (in_kinv != NULL && in_r != NULL) {
b63792
-                ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
b63792
+                ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
b63792
                 goto err;
b63792
             }
b63792
         } else {
cf3dd1
@@ -325,8 +360,8 @@ int ossl_ecdsa_verify(int type, const un
b63792
     return ret;
b63792
 }
b63792
 
b63792
-int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
b63792
-                          const ECDSA_SIG *sig, EC_KEY *eckey)
b63792
+int ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
b63792
+                            const ECDSA_SIG *sig, EC_KEY *eckey)
b63792
 {
b63792
     int ret = -1, i;
b63792
     BN_CTX *ctx;
cf3dd1
@@ -346,18 +381,18 @@ int ossl_ecdsa_verify_sig(const unsigned
b63792
     /* check input values */
b63792
     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
b63792
         (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
b63792
         return -1;
b63792
     }
b63792
 
b63792
     if (!EC_KEY_can_sign(eckey)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
b63792
         return -1;
b63792
     }
b63792
 
b63792
     ctx = BN_CTX_new();
b63792
     if (ctx == NULL) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
b63792
         return -1;
b63792
     }
b63792
     BN_CTX_start(ctx);
cf3dd1
@@ -366,26 +401,26 @@ int ossl_ecdsa_verify_sig(const unsigned
b63792
     m = BN_CTX_get(ctx);
b63792
     X = BN_CTX_get(ctx);
b63792
     if (X == NULL) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
 
b63792
     order = EC_GROUP_get0_order(group);
b63792
     if (order == NULL) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_EC_LIB);
b63792
         goto err;
b63792
     }
b63792
 
b63792
     if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
b63792
         BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
b63792
         BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, EC_R_BAD_SIGNATURE);
b63792
         ret = 0;                /* signature is invalid */
b63792
         goto err;
b63792
     }
b63792
     /* calculate tmp1 = inv(S) mod order */
b63792
     if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
     /* digest -> m */
cf3dd1
@@ -396,41 +431,41 @@ int ossl_ecdsa_verify_sig(const unsigned
b63792
     if (8 * dgst_len > i)
b63792
         dgst_len = (i + 7) / 8;
b63792
     if (!BN_bin2bn(dgst, dgst_len, m)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
     /* If still too long truncate remaining bits with a shift */
b63792
     if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
     /* u1 = m * tmp mod order */
b63792
     if (!BN_mod_mul(u1, m, u2, order, ctx)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
     /* u2 = r * w mod q */
b63792
     if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
 
b63792
     if ((point = EC_POINT_new(group)) == NULL) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
b63792
         goto err;
b63792
     }
b63792
     if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_EC_LIB);
b63792
         goto err;
b63792
     }
b63792
 
b63792
     if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_EC_LIB);
b63792
         goto err;
b63792
     }
b63792
 
b63792
     if (!BN_nnmod(u1, X, order, ctx)) {
b63792
-        ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
b63792
         goto err;
b63792
     }
b63792
     /*  if the signature is correct u1 is equal to sig->r */
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ec_err.c.s390x-ecc openssl-1.1.1g/crypto/ec/ec_err.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ec_err.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ec_err.c	2020-05-18 12:45:42.664249983 +0200
b63792
@@ -31,6 +31,11 @@ static const ERR_STRING_DATA EC_str_func
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIGN_SETUP, 0), "ECDSA_sign_setup"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIG_NEW, 0), "ECDSA_SIG_new"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_VERIFY, 0), "ECDSA_verify"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIMPLE_SIGN_SETUP, 0), "ecdsa_simple_sign_setup"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIMPLE_SIGN_SIG, 0), "ecdsa_simple_sign_sig"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIMPLE_VERIFY_SIG, 0), "ecdsa_simple_verify_sig"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_S390X_NISTP_SIGN_SIG, 0), "ecdsa_s390x_nistp_sign_sig"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, 0), "ecdsa_s390x_nistp_verify_sig"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_ECD_ITEM_VERIFY, 0), "ecd_item_verify"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PARAM2TYPE, 0), "eckey_param2type"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PARAM_DECODE, 0), "eckey_param_decode"},
b63792
@@ -266,6 +271,7 @@ static const ERR_STRING_DATA EC_str_func
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_OLD_EC_PRIV_DECODE, 0), "old_ec_priv_decode"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDH_COMPUTE_KEY, 0),
b63792
      "ossl_ecdh_compute_key"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDSA_SIGN_SETUP, 0), "ossl_ecdsa_sign_setup"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDSA_SIGN_SIG, 0), "ossl_ecdsa_sign_sig"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDSA_VERIFY_SIG, 0),
b63792
      "ossl_ecdsa_verify_sig"},
b63792
@@ -284,6 +290,12 @@ static const ERR_STRING_DATA EC_str_func
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_KEYGEN, 0), "pkey_ec_keygen"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_PARAMGEN, 0), "pkey_ec_paramgen"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_SIGN, 0), "pkey_ec_sign"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_S390X_PKEY_ECD_DIGESTSIGN25519, 0), "s390x_pkey_ecd_digestsign25519"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_S390X_PKEY_ECD_DIGESTSIGN448, 0), "s390x_pkey_ecd_digestsign448"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_S390X_PKEY_ECD_KEYGEN25519, 0), "s390x_pkey_ecd_keygen25519"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_S390X_PKEY_ECD_KEYGEN448, 0), "s390x_pkey_ecd_keygen448"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_S390X_PKEY_ECX_KEYGEN25519, 0), "s390x_pkey_ecx_keygen25519"},
b63792
+    {ERR_PACK(ERR_LIB_EC, EC_F_S390X_PKEY_ECX_KEYGEN448, 0), "s390x_pkey_ecx_keygen448"},
b63792
     {ERR_PACK(ERR_LIB_EC, EC_F_VALIDATE_ECX_DERIVE, 0), "validate_ecx_derive"},
b63792
     {0, NULL}
b63792
 };
b63792
@@ -298,6 +310,8 @@ static const ERR_STRING_DATA EC_str_reas
b63792
     "coordinates out of range"},
b63792
     {ERR_PACK(ERR_LIB_EC, 0, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH),
b63792
     "curve does not support ecdh"},
b63792
+    {ERR_PACK(ERR_LIB_EC, 0, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA),
b63792
+    "curve does not support ecdsa"},
b63792
     {ERR_PACK(ERR_LIB_EC, 0, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING),
b63792
     "curve does not support signing"},
b63792
     {ERR_PACK(ERR_LIB_EC, 0, EC_R_D2I_ECPKPARAMETERS_FAILURE),
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ec_local.h.s390x-ecc openssl-1.1.1g/crypto/ec/ec_local.h
cf3dd1
--- openssl-1.1.1g/crypto/ec/ec_local.h.s390x-ecc	2020-05-18 12:45:40.554231646 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ec_local.h	2020-05-18 12:45:44.564266496 +0200
b63792
@@ -179,6 +179,14 @@ struct ec_method_st {
b63792
     /* custom ECDH operation */
b63792
     int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
b63792
                             const EC_POINT *pub_key, const EC_KEY *ecdh);
b63792
+    /* custom ECDSA */
b63792
+    int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinvp,
b63792
+                            BIGNUM **rp);
b63792
+    ECDSA_SIG *(*ecdsa_sign_sig)(const unsigned char *dgst, int dgstlen,
b63792
+                                 const BIGNUM *kinv, const BIGNUM *r,
b63792
+                                 EC_KEY *eckey);
b63792
+    int (*ecdsa_verify_sig)(const unsigned char *dgst, int dgstlen,
b63792
+                            const ECDSA_SIG *sig, EC_KEY *eckey);
b63792
     /* Inverse modulo order */
b63792
     int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r,
b63792
                                  const BIGNUM *x, BN_CTX *);
b63792
@@ -587,6 +595,11 @@ int ec_group_simple_order_bits(const EC_
b63792
  */
b63792
 const EC_METHOD *EC_GFp_nistz256_method(void);
b63792
 #endif
b63792
+#ifdef S390X_EC_ASM
b63792
+const EC_METHOD *EC_GFp_s390x_nistp256_method(void);
b63792
+const EC_METHOD *EC_GFp_s390x_nistp384_method(void);
b63792
+const EC_METHOD *EC_GFp_s390x_nistp521_method(void);
b63792
+#endif
b63792
 
b63792
 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
b63792
                               unsigned char *buf, size_t len);
cf3dd1
@@ -651,6 +664,13 @@ int ossl_ecdsa_verify(int type, const un
b63792
                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
b63792
 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
b63792
                           const ECDSA_SIG *sig, EC_KEY *eckey);
b63792
+int ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
b63792
+                            BIGNUM **rp);
b63792
+ECDSA_SIG *ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
b63792
+                                 const BIGNUM *in_kinv, const BIGNUM *in_r,
b63792
+                                 EC_KEY *eckey);
b63792
+int ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
b63792
+                            const ECDSA_SIG *sig, EC_KEY *eckey);
b63792
 
b63792
 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
b63792
                  const uint8_t public_key[32], const uint8_t private_key[32]);
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ec_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/ec/ec_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ec_pmeth.c.s390x-ecc	2020-05-18 12:45:40.784233644 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ec_pmeth.c	2020-05-18 12:45:44.565266505 +0200
b63792
@@ -474,3 +474,8 @@ const EVP_PKEY_METHOD ec_pkey_meth = {
b63792
     pkey_ec_ctrl,
b63792
     pkey_ec_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *ec_pkey_method(void)
b63792
+{
b63792
+    return &ec_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_mont.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_mont.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_mont.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_mont.c	2020-05-18 12:45:44.567266523 +0200
b63792
@@ -63,6 +63,9 @@ const EC_METHOD *EC_GFp_mont_method(void
b63792
         0, /* keycopy */
b63792
         0, /* keyfinish */
b63792
         ecdh_simple_compute_key,
b63792
+        ecdsa_simple_sign_setup,
b63792
+        ecdsa_simple_sign_sig,
b63792
+        ecdsa_simple_verify_sig,
b63792
         0, /* field_inverse_mod_ord */
b63792
         ec_GFp_simple_blind_coordinates,
b63792
         ec_GFp_simple_ladder_pre,
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_nist.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_nist.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_nist.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_nist.c	2020-05-18 12:45:44.567266523 +0200
b63792
@@ -65,6 +65,9 @@ const EC_METHOD *EC_GFp_nist_method(void
b63792
         0, /* keycopy */
b63792
         0, /* keyfinish */
b63792
         ecdh_simple_compute_key,
b63792
+        ecdsa_simple_sign_setup,
b63792
+        ecdsa_simple_sign_sig,
b63792
+        ecdsa_simple_verify_sig,
b63792
         0, /* field_inverse_mod_ord */
b63792
         ec_GFp_simple_blind_coordinates,
b63792
         ec_GFp_simple_ladder_pre,
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_nistp224.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_nistp224.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_nistp224.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_nistp224.c	2020-05-18 12:45:44.568266531 +0200
b63792
@@ -292,6 +292,9 @@ const EC_METHOD *EC_GFp_nistp224_method(
b63792
         0, /* keycopy */
b63792
         0, /* keyfinish */
b63792
         ecdh_simple_compute_key,
b63792
+        ecdsa_simple_sign_setup,
b63792
+        ecdsa_simple_sign_sig,
b63792
+        ecdsa_simple_verify_sig,
b63792
         0, /* field_inverse_mod_ord */
b63792
         0, /* blind_coordinates */
b63792
         0, /* ladder_pre */
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_nistp256.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_nistp256.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_nistp256.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_nistp256.c	2020-05-18 12:45:44.568266531 +0200
cf3dd1
@@ -1829,6 +1829,9 @@ const EC_METHOD *EC_GFp_nistp256_method(
b63792
         0, /* keycopy */
b63792
         0, /* keyfinish */
b63792
         ecdh_simple_compute_key,
b63792
+        ecdsa_simple_sign_setup,
b63792
+        ecdsa_simple_sign_sig,
b63792
+        ecdsa_simple_verify_sig,
b63792
         0, /* field_inverse_mod_ord */
b63792
         0, /* blind_coordinates */
b63792
         0, /* ladder_pre */
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_nistp521.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_nistp521.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_nistp521.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_nistp521.c	2020-05-18 12:45:44.569266540 +0200
cf3dd1
@@ -1669,6 +1669,9 @@ const EC_METHOD *EC_GFp_nistp521_method(
b63792
         0, /* keycopy */
b63792
         0, /* keyfinish */
b63792
         ecdh_simple_compute_key,
b63792
+        ecdsa_simple_sign_setup,
b63792
+        ecdsa_simple_sign_sig,
b63792
+        ecdsa_simple_verify_sig,
b63792
         0, /* field_inverse_mod_ord */
b63792
         0, /* blind_coordinates */
b63792
         0, /* ladder_pre */
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_nistz256.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_nistz256.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_nistz256.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_nistz256.c	2020-05-18 12:45:44.570266549 +0200
cf3dd1
@@ -1720,6 +1720,9 @@ const EC_METHOD *EC_GFp_nistz256_method(
b63792
         0, /* keycopy */
b63792
         0, /* keyfinish */
b63792
         ecdh_simple_compute_key,
b63792
+        ecdsa_simple_sign_setup,
b63792
+        ecdsa_simple_sign_sig,
b63792
+        ecdsa_simple_verify_sig,
b63792
         ecp_nistz256_inv_mod_ord,                   /* can be #define-d NULL */
b63792
         0,                                          /* blind_coordinates */
b63792
         0,                                          /* ladder_pre */
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_s390x_nistp.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_s390x_nistp.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_s390x_nistp.c.s390x-ecc	2020-05-18 12:45:44.571266557 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_s390x_nistp.c	2020-05-18 12:45:44.571266557 +0200
b63792
@@ -0,0 +1,394 @@
b63792
+/*
b63792
+ * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
b63792
+ *
b63792
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
b63792
+ * this file except in compliance with the License.  You can obtain a copy
b63792
+ * in the file LICENSE in the source distribution or at
b63792
+ * https://www.openssl.org/source/license.html
b63792
+ */
b63792
+
b63792
+#include <stdlib.h>
b63792
+#include <string.h>
b63792
+#include <openssl/err.h>
b63792
+#include <openssl/rand.h>
cf3dd1
+#include "ec_local.h"
b63792
+#include "s390x_arch.h"
b63792
+
b63792
+/* Size of parameter blocks */
b63792
+#define S390X_SIZE_PARAM                4096
b63792
+
b63792
+/* Size of fields in parameter blocks */
b63792
+#define S390X_SIZE_P256                 32
b63792
+#define S390X_SIZE_P384                 48
b63792
+#define S390X_SIZE_P521                 80
b63792
+
b63792
+/* Offsets of fields in PCC parameter blocks */
b63792
+#define S390X_OFF_RES_X(n)              (0 * n)
b63792
+#define S390X_OFF_RES_Y(n)              (1 * n)
b63792
+#define S390X_OFF_SRC_X(n)              (2 * n)
b63792
+#define S390X_OFF_SRC_Y(n)              (3 * n)
b63792
+#define S390X_OFF_SCALAR(n)             (4 * n)
b63792
+
b63792
+/* Offsets of fields in KDSA parameter blocks */
b63792
+#define S390X_OFF_R(n)                  (0 * n)
b63792
+#define S390X_OFF_S(n)                  (1 * n)
b63792
+#define S390X_OFF_H(n)                  (2 * n)
b63792
+#define S390X_OFF_K(n)                  (3 * n)
b63792
+#define S390X_OFF_X(n)                  (3 * n)
b63792
+#define S390X_OFF_RN(n)                 (4 * n)
b63792
+#define S390X_OFF_Y(n)                  (4 * n)
b63792
+
b63792
+static int ec_GFp_s390x_nistp_mul(const EC_GROUP *group, EC_POINT *r,
b63792
+                                  const BIGNUM *scalar,
b63792
+                                  size_t num, const EC_POINT *points[],
b63792
+                                  const BIGNUM *scalars[],
b63792
+                                  BN_CTX *ctx, unsigned int fc, int len)
b63792
+{
b63792
+    unsigned char param[S390X_SIZE_PARAM];
b63792
+    BIGNUM *x, *y;
b63792
+    const EC_POINT *point_ptr = NULL;
b63792
+    const BIGNUM *scalar_ptr = NULL;
b63792
+    BN_CTX *new_ctx = NULL;
b63792
+    int rc = -1;
b63792
+
b63792
+    if (ctx == NULL) {
b63792
+        ctx = new_ctx = BN_CTX_new();
b63792
+        if (ctx == NULL)
b63792
+            return 0;
b63792
+    }
b63792
+
b63792
+    BN_CTX_start(ctx);
b63792
+
b63792
+    x = BN_CTX_get(ctx);
b63792
+    y = BN_CTX_get(ctx);
b63792
+    if (x == NULL || y == NULL) {
b63792
+        rc = 0;
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    /*
b63792
+     * Use PCC for EC keygen and ECDH key derivation:
b63792
+     * scalar * generator and scalar * peer public key,
b63792
+     * scalar in [0,order).
b63792
+     */
b63792
+    if ((scalar != NULL && num == 0 && BN_is_negative(scalar) == 0)
b63792
+        || (scalar == NULL && num == 1 && BN_is_negative(scalars[0]) == 0)) {
b63792
+
b63792
+        if (num == 0) {
b63792
+            point_ptr = EC_GROUP_get0_generator(group);
b63792
+            scalar_ptr = scalar;
b63792
+        } else {
b63792
+            point_ptr = points[0];
b63792
+            scalar_ptr = scalars[0];
b63792
+        }
b63792
+
b63792
+        if (EC_POINT_is_at_infinity(group, point_ptr) == 1
b63792
+            || BN_is_zero(scalar_ptr)) {
b63792
+            rc = EC_POINT_set_to_infinity(group, r);
b63792
+            goto ret;
b63792
+        }
b63792
+
b63792
+        memset(&param, 0, sizeof(param));
b63792
+
b63792
+        if (group->meth->point_get_affine_coordinates(group, point_ptr,
b63792
+                                                      x, y, ctx) != 1
b63792
+            || BN_bn2binpad(x, param + S390X_OFF_SRC_X(len), len) == -1
b63792
+            || BN_bn2binpad(y, param + S390X_OFF_SRC_Y(len), len) == -1
b63792
+            || BN_bn2binpad(scalar_ptr,
b63792
+                            param + S390X_OFF_SCALAR(len), len) == -1
b63792
+            || s390x_pcc(fc, param) != 0
b63792
+            || BN_bin2bn(param + S390X_OFF_RES_X(len), len, x) == NULL
b63792
+            || BN_bin2bn(param + S390X_OFF_RES_Y(len), len, y) == NULL
b63792
+            || group->meth->point_set_affine_coordinates(group, r,
b63792
+                                                         x, y, ctx) != 1)
b63792
+            goto ret;
b63792
+
b63792
+        rc = 1;
b63792
+    }
b63792
+
b63792
+ret:
b63792
+    /* Otherwise use default. */
b63792
+    if (rc == -1)
b63792
+        rc = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
b63792
+    OPENSSL_cleanse(param + S390X_OFF_SCALAR(len), len);
b63792
+    BN_CTX_end(ctx);
b63792
+    BN_CTX_free(new_ctx);
b63792
+    return rc;
b63792
+}
b63792
+
b63792
+static ECDSA_SIG *ecdsa_s390x_nistp_sign_sig(const unsigned char *dgst,
b63792
+                                             int dgstlen,
b63792
+                                             const BIGNUM *kinv,
b63792
+                                             const BIGNUM *r,
b63792
+                                             EC_KEY *eckey,
b63792
+                                             unsigned int fc, int len)
b63792
+{
b63792
+    unsigned char param[S390X_SIZE_PARAM];
b63792
+    int ok = 0;
b63792
+    BIGNUM *k;
b63792
+    ECDSA_SIG *sig;
b63792
+    const EC_GROUP *group;
b63792
+    const BIGNUM *privkey;
b63792
+    int off;
b63792
+
b63792
+    group = EC_KEY_get0_group(eckey);
b63792
+    privkey = EC_KEY_get0_private_key(eckey);
b63792
+    if (group == NULL || privkey == NULL) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, EC_R_MISSING_PARAMETERS);
b63792
+        return NULL;
b63792
+    }
b63792
+
b63792
+    if (!EC_KEY_can_sign(eckey)) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
b63792
+              EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
b63792
+        return NULL;
b63792
+    }
b63792
+
b63792
+    k = BN_secure_new();
b63792
+    sig = ECDSA_SIG_new();
b63792
+    if (k == NULL || sig == NULL) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    sig->r = BN_new();
b63792
+    sig->s = BN_new();
b63792
+    if (sig->r == NULL || sig->s == NULL) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    memset(param, 0, sizeof(param));
b63792
+    off = len - (dgstlen > len ? len : dgstlen);
b63792
+    memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
b63792
+
b63792
+    if (BN_bn2binpad(privkey, param + S390X_OFF_K(len), len) == -1) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    if (r == NULL || kinv == NULL) {
b63792
+        /*
b63792
+         * Generate random k and copy to param param block. RAND_priv_bytes
b63792
+         * is used instead of BN_priv_rand_range or BN_generate_dsa_nonce
b63792
+         * because kdsa instruction constructs an in-range, invertible nonce
b63792
+         * internally implementing counter-measures for RNG weakness.
b63792
+         */
b63792
+         if (RAND_priv_bytes(param + S390X_OFF_RN(len), len) != 1) {
b63792
+             ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
b63792
+                   EC_R_RANDOM_NUMBER_GENERATION_FAILED);
b63792
+             goto ret;
b63792
+         }
b63792
+    } else {
b63792
+        /* Reconstruct k = (k^-1)^-1. */
b63792
+        if (ec_group_do_inverse_ord(group, k, kinv, NULL) == 0
b63792
+            || BN_bn2binpad(k, param + S390X_OFF_RN(len), len) == -1) {
b63792
+            ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
b63792
+            goto ret;
b63792
+        }
b63792
+        /* Turns KDSA internal nonce-generation off. */
b63792
+        fc |= S390X_KDSA_D;
b63792
+    }
b63792
+
b63792
+    if (s390x_kdsa(fc, param, NULL, 0) != 0) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_ECDSA_LIB);
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    if (BN_bin2bn(param + S390X_OFF_R(len), len, sig->r) == NULL
b63792
+        || BN_bin2bn(param + S390X_OFF_S(len), len, sig->s) == NULL) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    ok = 1;
b63792
+ret:
b63792
+    OPENSSL_cleanse(param + S390X_OFF_K(len), 2 * len);
b63792
+    if (ok != 1) {
b63792
+        ECDSA_SIG_free(sig);
b63792
+        sig = NULL;
b63792
+    }
b63792
+    BN_clear_free(k);
b63792
+    return sig;
b63792
+}
b63792
+
b63792
+static int ecdsa_s390x_nistp_verify_sig(const unsigned char *dgst, int dgstlen,
b63792
+                                        const ECDSA_SIG *sig, EC_KEY *eckey,
b63792
+                                        unsigned int fc, int len)
b63792
+{
b63792
+    unsigned char param[S390X_SIZE_PARAM];
b63792
+    int rc = -1;
b63792
+    BN_CTX *ctx;
b63792
+    BIGNUM *x, *y;
b63792
+    const EC_GROUP *group;
b63792
+    const EC_POINT *pubkey;
b63792
+    int off;
b63792
+
b63792
+    group = EC_KEY_get0_group(eckey);
b63792
+    pubkey = EC_KEY_get0_public_key(eckey);
b63792
+    if (eckey == NULL || group == NULL || pubkey == NULL || sig == NULL) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
b63792
+        return -1;
b63792
+    }
b63792
+
b63792
+    if (!EC_KEY_can_sign(eckey)) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG,
b63792
+              EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
b63792
+        return -1;
b63792
+    }
b63792
+
b63792
+    ctx = BN_CTX_new();
b63792
+    if (ctx == NULL) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
b63792
+        return -1;
b63792
+    }
b63792
+
b63792
+    BN_CTX_start(ctx);
b63792
+
b63792
+    x = BN_CTX_get(ctx);
b63792
+    y = BN_CTX_get(ctx);
b63792
+    if (x == NULL || y == NULL) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    memset(param, 0, sizeof(param));
b63792
+    off = len - (dgstlen > len ? len : dgstlen);
b63792
+    memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
b63792
+
b63792
+    if (group->meth->point_get_affine_coordinates(group, pubkey,
b63792
+                                                  x, y, ctx) != 1
b63792
+        || BN_bn2binpad(sig->r, param + S390X_OFF_R(len), len) == -1
b63792
+        || BN_bn2binpad(sig->s, param + S390X_OFF_S(len), len) == -1
b63792
+        || BN_bn2binpad(x, param + S390X_OFF_X(len), len) == -1
b63792
+        || BN_bn2binpad(y, param + S390X_OFF_Y(len), len) == -1) {
b63792
+        ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_BN_LIB);
b63792
+        goto ret;
b63792
+    }
b63792
+
b63792
+    rc = s390x_kdsa(fc, param, NULL, 0) == 0 ? 1 : 0;
b63792
+ret:
b63792
+    BN_CTX_end(ctx);
b63792
+    BN_CTX_free(ctx);
b63792
+    return rc;
b63792
+}
b63792
+
b63792
+#define EC_GFP_S390X_NISTP_METHOD(bits)                                 \
b63792
+                                                                        \
b63792
+static int ec_GFp_s390x_nistp##bits##_mul(const EC_GROUP *group,        \
b63792
+                                          EC_POINT *r,                  \
b63792
+                                          const BIGNUM *scalar,         \
b63792
+                                          size_t num,                   \
b63792
+                                          const EC_POINT *points[],     \
b63792
+                                          const BIGNUM *scalars[],      \
b63792
+                                          BN_CTX *ctx)                  \
b63792
+{                                                                       \
b63792
+    return ec_GFp_s390x_nistp_mul(group, r, scalar, num, points,        \
b63792
+                                  scalars, ctx,                         \
b63792
+                                  S390X_SCALAR_MULTIPLY_P##bits,        \
b63792
+                                  S390X_SIZE_P##bits);                  \
b63792
+}                                                                       \
b63792
+                                                                        \
b63792
+static ECDSA_SIG *ecdsa_s390x_nistp##bits##_sign_sig(const unsigned     \
b63792
+                                                     char *dgst,        \
b63792
+                                                     int dgstlen,       \
b63792
+                                                     const BIGNUM *kinv,\
b63792
+                                                     const BIGNUM *r,   \
b63792
+                                                     EC_KEY *eckey)     \
b63792
+{                                                                       \
b63792
+    return ecdsa_s390x_nistp_sign_sig(dgst, dgstlen, kinv, r, eckey,    \
b63792
+                                      S390X_ECDSA_SIGN_P##bits,         \
b63792
+                                      S390X_SIZE_P##bits);              \
b63792
+}                                                                       \
b63792
+                                                                        \
b63792
+static int ecdsa_s390x_nistp##bits##_verify_sig(const                   \
b63792
+                                                unsigned char *dgst,    \
b63792
+                                                int dgstlen,            \
b63792
+                                                const ECDSA_SIG *sig,   \
b63792
+                                                EC_KEY *eckey)          \
b63792
+{                                                                       \
b63792
+    return ecdsa_s390x_nistp_verify_sig(dgst, dgstlen, sig, eckey,      \
b63792
+                                        S390X_ECDSA_VERIFY_P##bits,     \
b63792
+                                        S390X_SIZE_P##bits);            \
b63792
+}                                                                       \
b63792
+                                                                        \
b63792
+const EC_METHOD *EC_GFp_s390x_nistp##bits##_method(void)                \
b63792
+{                                                                       \
b63792
+    static const EC_METHOD EC_GFp_s390x_nistp##bits##_meth = {          \
b63792
+        EC_FLAGS_DEFAULT_OCT,                                           \
b63792
+        NID_X9_62_prime_field,                                          \
b63792
+        ec_GFp_simple_group_init,                                       \
b63792
+        ec_GFp_simple_group_finish,                                     \
b63792
+        ec_GFp_simple_group_clear_finish,                               \
b63792
+        ec_GFp_simple_group_copy,                                       \
b63792
+        ec_GFp_simple_group_set_curve,                                  \
b63792
+        ec_GFp_simple_group_get_curve,                                  \
b63792
+        ec_GFp_simple_group_get_degree,                                 \
b63792
+        ec_group_simple_order_bits,                                     \
b63792
+        ec_GFp_simple_group_check_discriminant,                         \
b63792
+        ec_GFp_simple_point_init,                                       \
b63792
+        ec_GFp_simple_point_finish,                                     \
b63792
+        ec_GFp_simple_point_clear_finish,                               \
b63792
+        ec_GFp_simple_point_copy,                                       \
b63792
+        ec_GFp_simple_point_set_to_infinity,                            \
b63792
+        ec_GFp_simple_set_Jprojective_coordinates_GFp,                  \
b63792
+        ec_GFp_simple_get_Jprojective_coordinates_GFp,                  \
b63792
+        ec_GFp_simple_point_set_affine_coordinates,                     \
b63792
+        ec_GFp_simple_point_get_affine_coordinates,                     \
b63792
+        NULL, /* point_set_compressed_coordinates */                    \
b63792
+        NULL, /* point2oct */                                           \
b63792
+        NULL, /* oct2point */                                           \
b63792
+        ec_GFp_simple_add,                                              \
b63792
+        ec_GFp_simple_dbl,                                              \
b63792
+        ec_GFp_simple_invert,                                           \
b63792
+        ec_GFp_simple_is_at_infinity,                                   \
b63792
+        ec_GFp_simple_is_on_curve,                                      \
b63792
+        ec_GFp_simple_cmp,                                              \
b63792
+        ec_GFp_simple_make_affine,                                      \
b63792
+        ec_GFp_simple_points_make_affine,                               \
b63792
+        ec_GFp_s390x_nistp##bits##_mul,                                 \
b63792
+        NULL, /* precompute_mult */                                     \
b63792
+        NULL, /* have_precompute_mult */                                \
b63792
+        ec_GFp_simple_field_mul,                                        \
b63792
+        ec_GFp_simple_field_sqr,                                        \
b63792
+        NULL, /* field_div */                                           \
b63792
+        ec_GFp_simple_field_inv,                                        \
b63792
+        NULL, /* field_encode */                                        \
b63792
+        NULL, /* field_decode */                                        \
b63792
+        NULL, /* field_set_to_one */                                    \
b63792
+        ec_key_simple_priv2oct,                                         \
b63792
+        ec_key_simple_oct2priv,                                         \
b63792
+        NULL, /* set_private */                                         \
b63792
+        ec_key_simple_generate_key,                                     \
b63792
+        ec_key_simple_check_key,                                        \
b63792
+        ec_key_simple_generate_public_key,                              \
b63792
+        NULL, /* keycopy */                                             \
b63792
+        NULL, /* keyfinish */                                           \
b63792
+        ecdh_simple_compute_key,                                        \
b63792
+        ecdsa_simple_sign_setup,                                        \
b63792
+        ecdsa_s390x_nistp##bits##_sign_sig,                             \
b63792
+        ecdsa_s390x_nistp##bits##_verify_sig,                           \
b63792
+        NULL, /* field_inverse_mod_ord */                               \
b63792
+        ec_GFp_simple_blind_coordinates,                                \
b63792
+        ec_GFp_simple_ladder_pre,                                       \
b63792
+        ec_GFp_simple_ladder_step,                                      \
b63792
+        ec_GFp_simple_ladder_post                                       \
b63792
+    };                                                                  \
b63792
+    static const EC_METHOD *ret;                                        \
b63792
+                                                                        \
b63792
+    if ((OPENSSL_s390xcap_P.pcc[1]                                      \
b63792
+         & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_P##bits))                 \
b63792
+        && (OPENSSL_s390xcap_P.kdsa[0]                                  \
b63792
+            & S390X_CAPBIT(S390X_ECDSA_VERIFY_P##bits))                 \
b63792
+        && (OPENSSL_s390xcap_P.kdsa[0]                                  \
b63792
+            & S390X_CAPBIT(S390X_ECDSA_SIGN_P##bits)))                  \
b63792
+        ret = &EC_GFp_s390x_nistp##bits##_meth;                         \
b63792
+    else                                                                \
b63792
+        ret = EC_GFp_mont_method();                                     \
b63792
+                                                                        \
b63792
+    return ret;                                                         \
b63792
+}
b63792
+
b63792
+EC_GFP_S390X_NISTP_METHOD(256)
b63792
+EC_GFP_S390X_NISTP_METHOD(384)
b63792
+EC_GFP_S390X_NISTP_METHOD(521)
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecp_smpl.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecp_smpl.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecp_smpl.c.s390x-ecc	2020-05-18 12:45:40.769233514 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecp_smpl.c	2020-05-18 12:45:44.572266566 +0200
b63792
@@ -64,6 +64,9 @@ const EC_METHOD *EC_GFp_simple_method(vo
b63792
         0, /* keycopy */
b63792
         0, /* keyfinish */
b63792
         ecdh_simple_compute_key,
b63792
+        ecdsa_simple_sign_setup,
b63792
+        ecdsa_simple_sign_sig,
b63792
+        ecdsa_simple_verify_sig,
b63792
         0, /* field_inverse_mod_ord */
b63792
         ec_GFp_simple_blind_coordinates,
b63792
         ec_GFp_simple_ladder_pre,
cf3dd1
diff -up openssl-1.1.1g/crypto/ec/ecx_meth.c.s390x-ecc openssl-1.1.1g/crypto/ec/ecx_meth.c
cf3dd1
--- openssl-1.1.1g/crypto/ec/ecx_meth.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/ec/ecx_meth.c	2020-05-18 12:45:44.573266575 +0200
b63792
@@ -20,6 +20,7 @@
b63792
 #define X25519_BITS          253
b63792
 #define X25519_SECURITY_BITS 128
b63792
 
b63792
+#define ED25519_KEYLEN       32
b63792
 #define ED25519_SIGSIZE      64
b63792
 
b63792
 #define X448_BITS            448
b63792
@@ -839,3 +840,666 @@ const EVP_PKEY_METHOD ed448_pkey_meth =
b63792
     pkey_ecd_digestsign448,
b63792
     pkey_ecd_digestverify448
b63792
 };
b63792
+
b63792
+#ifdef S390X_EC_ASM
b63792
+# include "s390x_arch.h"
cf3dd1
+# include "internal/constant_time.h"
b63792
+
b63792
+static void s390x_x25519_mod_p(unsigned char u[32])
b63792
+{
b63792
+    unsigned char u_red[32];
b63792
+    unsigned int c = 0;
b63792
+    int i;
b63792
+
b63792
+    memcpy(u_red, u, sizeof(u_red));
b63792
+
b63792
+    c += (unsigned int)u_red[31] + 19;
b63792
+    u_red[31] = (unsigned char)c;
b63792
+    c >>= 8;
b63792
+
b63792
+    for (i = 30; i >= 0; i--) {
b63792
+        c += (unsigned int)u_red[i];
b63792
+        u_red[i] = (unsigned char)c;
b63792
+        c >>= 8;
b63792
+    }
b63792
+
b63792
+    c = (u_red[0] & 0x80) >> 7;
b63792
+    u_red[0] &= 0x7f;
b63792
+    constant_time_cond_swap_buff(0 - (unsigned char)c,
b63792
+                                 u, u_red, sizeof(u_red));
b63792
+}
b63792
+
b63792
+static void s390x_x448_mod_p(unsigned char u[56])
b63792
+{
b63792
+    unsigned char u_red[56];
b63792
+    unsigned int c = 0;
b63792
+    int i;
b63792
+
b63792
+    memcpy(u_red, u, sizeof(u_red));
b63792
+
b63792
+    c += (unsigned int)u_red[55] + 1;
b63792
+    u_red[55] = (unsigned char)c;
b63792
+    c >>= 8;
b63792
+
b63792
+    for (i = 54; i >= 28; i--) {
b63792
+        c += (unsigned int)u_red[i];
b63792
+        u_red[i] = (unsigned char)c;
b63792
+        c >>= 8;
b63792
+    }
b63792
+
b63792
+    c += (unsigned int)u_red[27] + 1;
b63792
+    u_red[27] = (unsigned char)c;
b63792
+    c >>= 8;
b63792
+
b63792
+    for (i = 26; i >= 0; i--) {
b63792
+        c += (unsigned int)u_red[i];
b63792
+        u_red[i] = (unsigned char)c;
b63792
+        c >>= 8;
b63792
+    }
b63792
+
b63792
+    constant_time_cond_swap_buff(0 - (unsigned char)c,
b63792
+                                 u, u_red, sizeof(u_red));
b63792
+}
b63792
+
b63792
+static int s390x_x25519_mul(unsigned char u_dst[32],
b63792
+                            const unsigned char u_src[32],
b63792
+                            const unsigned char d_src[32])
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char u_dst[32];
b63792
+            unsigned char u_src[32];
b63792
+            unsigned char d_src[32];
b63792
+        } x25519;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    int rc;
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+
b63792
+    s390x_flip_endian32(param.x25519.u_src, u_src);
b63792
+    param.x25519.u_src[0] &= 0x7f;
b63792
+    s390x_x25519_mod_p(param.x25519.u_src);
b63792
+
b63792
+    s390x_flip_endian32(param.x25519.d_src, d_src);
b63792
+    param.x25519.d_src[31] &= 248;
b63792
+    param.x25519.d_src[0] &= 127;
b63792
+    param.x25519.d_src[0] |= 64;
b63792
+
b63792
+    rc = s390x_pcc(S390X_SCALAR_MULTIPLY_X25519, &param.x25519) ? 0 : 1;
b63792
+    if (rc == 1)
b63792
+        s390x_flip_endian32(u_dst, param.x25519.u_dst);
b63792
+
b63792
+    OPENSSL_cleanse(param.x25519.d_src, sizeof(param.x25519.d_src));
b63792
+    return rc;
b63792
+}
b63792
+
b63792
+static int s390x_x448_mul(unsigned char u_dst[56],
b63792
+                          const unsigned char u_src[56],
b63792
+                          const unsigned char d_src[56])
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char u_dst[64];
b63792
+            unsigned char u_src[64];
b63792
+            unsigned char d_src[64];
b63792
+        } x448;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    int rc;
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+
b63792
+    memcpy(param.x448.u_src, u_src, 56);
b63792
+    memcpy(param.x448.d_src, d_src, 56);
b63792
+
b63792
+    s390x_flip_endian64(param.x448.u_src, param.x448.u_src);
b63792
+    s390x_x448_mod_p(param.x448.u_src + 8);
b63792
+
b63792
+    s390x_flip_endian64(param.x448.d_src, param.x448.d_src);
b63792
+    param.x448.d_src[63] &= 252;
b63792
+    param.x448.d_src[8] |= 128;
b63792
+
b63792
+    rc = s390x_pcc(S390X_SCALAR_MULTIPLY_X448, &param.x448) ? 0 : 1;
b63792
+    if (rc == 1) {
b63792
+        s390x_flip_endian64(param.x448.u_dst, param.x448.u_dst);
b63792
+        memcpy(u_dst, param.x448.u_dst, 56);
b63792
+    }
b63792
+
b63792
+    OPENSSL_cleanse(param.x448.d_src, sizeof(param.x448.d_src));
b63792
+    return rc;
b63792
+}
b63792
+
b63792
+static int s390x_ed25519_mul(unsigned char x_dst[32],
b63792
+                             unsigned char y_dst[32],
b63792
+                             const unsigned char x_src[32],
b63792
+                             const unsigned char y_src[32],
b63792
+                             const unsigned char d_src[32])
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char x_dst[32];
b63792
+            unsigned char y_dst[32];
b63792
+            unsigned char x_src[32];
b63792
+            unsigned char y_src[32];
b63792
+            unsigned char d_src[32];
b63792
+        } ed25519;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    int rc;
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+
b63792
+    s390x_flip_endian32(param.ed25519.x_src, x_src);
b63792
+    s390x_flip_endian32(param.ed25519.y_src, y_src);
b63792
+    s390x_flip_endian32(param.ed25519.d_src, d_src);
b63792
+
b63792
+    rc = s390x_pcc(S390X_SCALAR_MULTIPLY_ED25519, &param.ed25519) ? 0 : 1;
b63792
+    if (rc == 1) {
b63792
+        s390x_flip_endian32(x_dst, param.ed25519.x_dst);
b63792
+        s390x_flip_endian32(y_dst, param.ed25519.y_dst);
b63792
+    }
b63792
+
b63792
+    OPENSSL_cleanse(param.ed25519.d_src, sizeof(param.ed25519.d_src));
b63792
+    return rc;
b63792
+}
b63792
+
b63792
+static int s390x_ed448_mul(unsigned char x_dst[57],
b63792
+                           unsigned char y_dst[57],
b63792
+                           const unsigned char x_src[57],
b63792
+                           const unsigned char y_src[57],
b63792
+                           const unsigned char d_src[57])
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char x_dst[64];
b63792
+            unsigned char y_dst[64];
b63792
+            unsigned char x_src[64];
b63792
+            unsigned char y_src[64];
b63792
+            unsigned char d_src[64];
b63792
+        } ed448;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    int rc;
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+
b63792
+    memcpy(param.ed448.x_src, x_src, 57);
b63792
+    memcpy(param.ed448.y_src, y_src, 57);
b63792
+    memcpy(param.ed448.d_src, d_src, 57);
b63792
+    s390x_flip_endian64(param.ed448.x_src, param.ed448.x_src);
b63792
+    s390x_flip_endian64(param.ed448.y_src, param.ed448.y_src);
b63792
+    s390x_flip_endian64(param.ed448.d_src, param.ed448.d_src);
b63792
+
b63792
+    rc = s390x_pcc(S390X_SCALAR_MULTIPLY_ED448, &param.ed448) ? 0 : 1;
b63792
+    if (rc == 1) {
b63792
+        s390x_flip_endian64(param.ed448.x_dst, param.ed448.x_dst);
b63792
+        s390x_flip_endian64(param.ed448.y_dst, param.ed448.y_dst);
b63792
+        memcpy(x_dst, param.ed448.x_dst, 57);
b63792
+        memcpy(y_dst, param.ed448.y_dst, 57);
b63792
+    }
b63792
+
b63792
+    OPENSSL_cleanse(param.ed448.d_src, sizeof(param.ed448.d_src));
b63792
+    return rc;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecx_keygen25519(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
b63792
+{
b63792
+    static const unsigned char generator[] = {
b63792
+        0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b63792
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b63792
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
b63792
+    };
b63792
+    ECX_KEY *key;
b63792
+    unsigned char *privkey = NULL, *pubkey;
b63792
+
b63792
+    key = OPENSSL_zalloc(sizeof(*key));
b63792
+    if (key == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECX_KEYGEN25519, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    pubkey = key->pubkey;
b63792
+
b63792
+    privkey = key->privkey = OPENSSL_secure_malloc(X25519_KEYLEN);
b63792
+    if (privkey == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECX_KEYGEN25519, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    if (RAND_priv_bytes(privkey, X25519_KEYLEN) <= 0)
b63792
+        goto err;
b63792
+
b63792
+    privkey[0] &= 248;
b63792
+    privkey[31] &= 127;
b63792
+    privkey[31] |= 64;
b63792
+
b63792
+    if (s390x_x25519_mul(pubkey, generator, privkey) != 1)
b63792
+        goto err;
b63792
+
b63792
+    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
b63792
+    return 1;
b63792
+ err:
b63792
+    OPENSSL_secure_clear_free(privkey, X25519_KEYLEN);
b63792
+    key->privkey = NULL;
b63792
+    OPENSSL_free(key);
b63792
+    return 0;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecx_keygen448(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
b63792
+{
b63792
+    static const unsigned char generator[] = {
b63792
+        0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b63792
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b63792
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b63792
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
b63792
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
b63792
+    };
b63792
+    ECX_KEY *key;
b63792
+    unsigned char *privkey = NULL, *pubkey;
b63792
+
b63792
+    key = OPENSSL_zalloc(sizeof(*key));
b63792
+    if (key == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECX_KEYGEN448, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    pubkey = key->pubkey;
b63792
+
b63792
+    privkey = key->privkey = OPENSSL_secure_malloc(X448_KEYLEN);
b63792
+    if (privkey == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECX_KEYGEN448, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    if (RAND_priv_bytes(privkey, X448_KEYLEN) <= 0)
b63792
+        goto err;
b63792
+
b63792
+    privkey[0] &= 252;
b63792
+    privkey[55] |= 128;
b63792
+
b63792
+    if (s390x_x448_mul(pubkey, generator, privkey) != 1)
b63792
+        goto err;
b63792
+
b63792
+    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
b63792
+    return 1;
b63792
+ err:
b63792
+    OPENSSL_secure_clear_free(privkey, X448_KEYLEN);
b63792
+    key->privkey = NULL;
b63792
+    OPENSSL_free(key);
b63792
+    return 0;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecd_keygen25519(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
b63792
+{
b63792
+    static const unsigned char generator_x[] = {
b63792
+        0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9, 0xb2, 0xa7, 0x25, 0x95,
b63792
+        0x60, 0xc7, 0x2c, 0x69, 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
b63792
+        0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
b63792
+    };
b63792
+    static const unsigned char generator_y[] = {
b63792
+        0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
b63792
+        0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
b63792
+        0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
b63792
+    };
b63792
+    unsigned char x_dst[32], buff[SHA512_DIGEST_LENGTH];
b63792
+    ECX_KEY *key;
b63792
+    unsigned char *privkey = NULL, *pubkey;
b63792
+
b63792
+    key = OPENSSL_zalloc(sizeof(*key));
b63792
+    if (key == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECD_KEYGEN25519, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    pubkey = key->pubkey;
b63792
+
b63792
+    privkey = key->privkey = OPENSSL_secure_malloc(ED25519_KEYLEN);
b63792
+    if (privkey == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECD_KEYGEN25519, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    if (RAND_priv_bytes(privkey, ED25519_KEYLEN) <= 0)
b63792
+        goto err;
b63792
+
b63792
+    SHA512(privkey, 32, buff);
b63792
+    buff[0] &= 248;
b63792
+    buff[31] &= 63;
b63792
+    buff[31] |= 64;
b63792
+
b63792
+    if (s390x_ed25519_mul(x_dst, pubkey,
b63792
+                          generator_x, generator_y, buff) != 1)
b63792
+        goto err;
b63792
+
b63792
+    pubkey[31] |= ((x_dst[0] & 0x01) << 7);
b63792
+
b63792
+    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
b63792
+    return 1;
b63792
+ err:
b63792
+    OPENSSL_secure_clear_free(privkey, ED25519_KEYLEN);
b63792
+    key->privkey = NULL;
b63792
+    OPENSSL_free(key);
b63792
+    return 0;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecd_keygen448(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
b63792
+{
b63792
+    static const unsigned char generator_x[] = {
b63792
+        0x5e, 0xc0, 0x0c, 0xc7, 0x2b, 0xa8, 0x26, 0x26, 0x8e, 0x93, 0x00, 0x8b,
b63792
+        0xe1, 0x80, 0x3b, 0x43, 0x11, 0x65, 0xb6, 0x2a, 0xf7, 0x1a, 0xae, 0x12,
b63792
+        0x64, 0xa4, 0xd3, 0xa3, 0x24, 0xe3, 0x6d, 0xea, 0x67, 0x17, 0x0f, 0x47,
b63792
+        0x70, 0x65, 0x14, 0x9e, 0xda, 0x36, 0xbf, 0x22, 0xa6, 0x15, 0x1d, 0x22,
b63792
+        0xed, 0x0d, 0xed, 0x6b, 0xc6, 0x70, 0x19, 0x4f, 0x00
b63792
+    };
b63792
+    static const unsigned char generator_y[] = {
b63792
+        0x14, 0xfa, 0x30, 0xf2, 0x5b, 0x79, 0x08, 0x98, 0xad, 0xc8, 0xd7, 0x4e,
b63792
+        0x2c, 0x13, 0xbd, 0xfd, 0xc4, 0x39, 0x7c, 0xe6, 0x1c, 0xff, 0xd3, 0x3a,
b63792
+        0xd7, 0xc2, 0xa0, 0x05, 0x1e, 0x9c, 0x78, 0x87, 0x40, 0x98, 0xa3, 0x6c,
b63792
+        0x73, 0x73, 0xea, 0x4b, 0x62, 0xc7, 0xc9, 0x56, 0x37, 0x20, 0x76, 0x88,
b63792
+        0x24, 0xbc, 0xb6, 0x6e, 0x71, 0x46, 0x3f, 0x69, 0x00
b63792
+    };
b63792
+    unsigned char x_dst[57], buff[114];
b63792
+    ECX_KEY *key;
b63792
+    unsigned char *privkey = NULL, *pubkey;
b63792
+    EVP_MD_CTX *hashctx = NULL;
b63792
+
b63792
+    key = OPENSSL_zalloc(sizeof(*key));
b63792
+    if (key == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECD_KEYGEN448, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    pubkey = key->pubkey;
b63792
+
b63792
+    privkey = key->privkey = OPENSSL_secure_malloc(ED448_KEYLEN);
b63792
+    if (privkey == NULL) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECD_KEYGEN448, ERR_R_MALLOC_FAILURE);
b63792
+        goto err;
b63792
+    }
b63792
+
b63792
+    if (RAND_priv_bytes(privkey, ED448_KEYLEN) <= 0)
b63792
+        goto err;
b63792
+
b63792
+    hashctx = EVP_MD_CTX_new();
b63792
+    if (hashctx == NULL)
b63792
+        goto err;
b63792
+    if (EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL) != 1)
b63792
+        goto err;
b63792
+    if (EVP_DigestUpdate(hashctx, privkey, 57) != 1)
b63792
+        goto err;
b63792
+    if (EVP_DigestFinalXOF(hashctx, buff, sizeof(buff)) != 1)
b63792
+        goto err;
b63792
+
b63792
+    buff[0] &= -4;
b63792
+    buff[55] |= 0x80;
b63792
+    buff[56] = 0;
b63792
+
b63792
+    if (s390x_ed448_mul(x_dst, pubkey,
b63792
+                        generator_x, generator_y, buff) != 1)
b63792
+        goto err;
b63792
+
b63792
+    pubkey[56] |= ((x_dst[0] & 0x01) << 7);
b63792
+
b63792
+    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
b63792
+    EVP_MD_CTX_free(hashctx);
b63792
+    return 1;
b63792
+ err:
b63792
+    OPENSSL_secure_clear_free(privkey, ED448_KEYLEN);
b63792
+    key->privkey = NULL;
b63792
+    OPENSSL_free(key);
b63792
+    EVP_MD_CTX_free(hashctx);
b63792
+    return 0;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecx_derive25519(EVP_PKEY_CTX *ctx, unsigned char *key,
b63792
+                                      size_t *keylen)
b63792
+{
b63792
+    const unsigned char *privkey, *pubkey;
b63792
+
b63792
+    if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey))
b63792
+        return 0;
b63792
+
b63792
+    if (key != NULL)
b63792
+        return s390x_x25519_mul(key, pubkey, privkey);
b63792
+
b63792
+    *keylen = X25519_KEYLEN;
b63792
+    return 1;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecx_derive448(EVP_PKEY_CTX *ctx, unsigned char *key,
b63792
+                                      size_t *keylen)
b63792
+{
b63792
+    const unsigned char *privkey, *pubkey;
b63792
+
b63792
+    if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey))
b63792
+        return 0;
b63792
+
b63792
+    if (key != NULL)
b63792
+        return s390x_x448_mul(key, pubkey, privkey);
b63792
+
b63792
+    *keylen = X448_KEYLEN;
b63792
+    return 1;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecd_digestsign25519(EVP_MD_CTX *ctx,
b63792
+                                          unsigned char *sig, size_t *siglen,
b63792
+                                          const unsigned char *tbs,
b63792
+                                          size_t tbslen)
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char sig[64];
b63792
+            unsigned char priv[32];
b63792
+        } ed25519;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
b63792
+    int rc;
b63792
+
b63792
+    if (sig == NULL) {
b63792
+        *siglen = ED25519_SIGSIZE;
b63792
+        return 1;
b63792
+    }
b63792
+
b63792
+    if (*siglen < ED25519_SIGSIZE) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECD_DIGESTSIGN25519, EC_R_BUFFER_TOO_SMALL);
b63792
+        return 0;
b63792
+    }
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+    memcpy(param.ed25519.priv, edkey->privkey, sizeof(param.ed25519.priv));
b63792
+
b63792
+    rc = s390x_kdsa(S390X_EDDSA_SIGN_ED25519, &param.ed25519, tbs, tbslen);
b63792
+    OPENSSL_cleanse(param.ed25519.priv, sizeof(param.ed25519.priv));
b63792
+    if (rc != 0)
b63792
+        return 0;
b63792
+
b63792
+    s390x_flip_endian32(sig, param.ed25519.sig);
b63792
+    s390x_flip_endian32(sig + 32, param.ed25519.sig + 32);
b63792
+
b63792
+    *siglen = ED25519_SIGSIZE;
b63792
+    return 1;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecd_digestsign448(EVP_MD_CTX *ctx,
b63792
+                                        unsigned char *sig, size_t *siglen,
b63792
+                                        const unsigned char *tbs,
b63792
+                                        size_t tbslen)
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char sig[128];
b63792
+            unsigned char priv[64];
b63792
+        } ed448;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
b63792
+    int rc;
b63792
+
b63792
+    if (sig == NULL) {
b63792
+        *siglen = ED448_SIGSIZE;
b63792
+        return 1;
b63792
+    }
b63792
+
b63792
+    if (*siglen < ED448_SIGSIZE) {
b63792
+        ECerr(EC_F_S390X_PKEY_ECD_DIGESTSIGN448, EC_R_BUFFER_TOO_SMALL);
b63792
+        return 0;
b63792
+    }
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+    memcpy(param.ed448.priv + 64 - 57, edkey->privkey, 57);
b63792
+
b63792
+    rc = s390x_kdsa(S390X_EDDSA_SIGN_ED448, &param.ed448, tbs, tbslen);
b63792
+    OPENSSL_cleanse(param.ed448.priv, sizeof(param.ed448.priv));
b63792
+    if (rc != 0)
b63792
+        return 0;
b63792
+
b63792
+    s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
b63792
+    s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
b63792
+    memcpy(sig, param.ed448.sig, 57);
b63792
+    memcpy(sig + 57, param.ed448.sig + 64, 57);
b63792
+
b63792
+    *siglen = ED448_SIGSIZE;
b63792
+    return 1;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecd_digestverify25519(EVP_MD_CTX *ctx,
b63792
+                                            const unsigned char *sig,
b63792
+                                            size_t siglen,
b63792
+                                            const unsigned char *tbs,
b63792
+                                            size_t tbslen)
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char sig[64];
b63792
+            unsigned char pub[32];
b63792
+        } ed25519;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
b63792
+
b63792
+    if (siglen != ED25519_SIGSIZE)
b63792
+        return 0;
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+    s390x_flip_endian32(param.ed25519.sig, sig);
b63792
+    s390x_flip_endian32(param.ed25519.sig + 32, sig + 32);
b63792
+    s390x_flip_endian32(param.ed25519.pub, edkey->pubkey);
b63792
+
b63792
+    return s390x_kdsa(S390X_EDDSA_VERIFY_ED25519,
b63792
+                      &param.ed25519, tbs, tbslen) == 0 ? 1 : 0;
b63792
+}
b63792
+
b63792
+static int s390x_pkey_ecd_digestverify448(EVP_MD_CTX *ctx,
b63792
+                                          const unsigned char *sig,
b63792
+                                          size_t siglen,
b63792
+                                          const unsigned char *tbs,
b63792
+                                          size_t tbslen)
b63792
+{
b63792
+    union {
b63792
+        struct {
b63792
+            unsigned char sig[128];
b63792
+            unsigned char pub[64];
b63792
+        } ed448;
b63792
+        unsigned long long buff[512];
b63792
+    } param;
b63792
+    const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
b63792
+
b63792
+    if (siglen != ED448_SIGSIZE)
b63792
+        return 0;
b63792
+
b63792
+    memset(&param, 0, sizeof(param));
b63792
+    memcpy(param.ed448.sig, sig, 57);
b63792
+    s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
b63792
+    memcpy(param.ed448.sig + 64, sig + 57, 57);
b63792
+    s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
b63792
+    memcpy(param.ed448.pub, edkey->pubkey, 57);
b63792
+    s390x_flip_endian64(param.ed448.pub, param.ed448.pub);
b63792
+
b63792
+    return s390x_kdsa(S390X_EDDSA_VERIFY_ED448,
b63792
+                      &param.ed448, tbs, tbslen) == 0 ? 1 : 0;
b63792
+}
b63792
+
b63792
+static const EVP_PKEY_METHOD ecx25519_s390x_pkey_meth = {
b63792
+    EVP_PKEY_X25519,
b63792
+    0, 0, 0, 0, 0, 0, 0,
b63792
+    s390x_pkey_ecx_keygen25519,
b63792
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
b63792
+    s390x_pkey_ecx_derive25519,
b63792
+    pkey_ecx_ctrl,
b63792
+    0
b63792
+};
b63792
+
b63792
+static const EVP_PKEY_METHOD ecx448_s390x_pkey_meth = {
b63792
+    EVP_PKEY_X448,
b63792
+    0, 0, 0, 0, 0, 0, 0,
b63792
+    s390x_pkey_ecx_keygen448,
b63792
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
b63792
+    s390x_pkey_ecx_derive448,
b63792
+    pkey_ecx_ctrl,
b63792
+    0
b63792
+};
b63792
+static const EVP_PKEY_METHOD ed25519_s390x_pkey_meth = {
b63792
+    EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
b63792
+    0, 0, 0, 0, 0, 0,
b63792
+    s390x_pkey_ecd_keygen25519,
b63792
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
b63792
+    pkey_ecd_ctrl,
b63792
+    0,
b63792
+    s390x_pkey_ecd_digestsign25519,
b63792
+    s390x_pkey_ecd_digestverify25519
b63792
+};
b63792
+
b63792
+static const EVP_PKEY_METHOD ed448_s390x_pkey_meth = {
b63792
+    EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
b63792
+    0, 0, 0, 0, 0, 0,
b63792
+    s390x_pkey_ecd_keygen448,
b63792
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
b63792
+    pkey_ecd_ctrl,
b63792
+    0,
b63792
+    s390x_pkey_ecd_digestsign448,
b63792
+    s390x_pkey_ecd_digestverify448
b63792
+};
b63792
+#endif
b63792
+
b63792
+const EVP_PKEY_METHOD *ecx25519_pkey_method(void)
b63792
+{
b63792
+#ifdef S390X_EC_ASM
b63792
+    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519))
b63792
+        return &ecx25519_s390x_pkey_meth;
b63792
+#endif
b63792
+    return &ecx25519_pkey_meth;
b63792
+}
b63792
+
b63792
+const EVP_PKEY_METHOD *ecx448_pkey_method(void)
b63792
+{
b63792
+#ifdef S390X_EC_ASM
b63792
+    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448))
b63792
+        return &ecx448_s390x_pkey_meth;
b63792
+#endif
b63792
+    return &ecx448_pkey_meth;
b63792
+}
b63792
+
b63792
+const EVP_PKEY_METHOD *ed25519_pkey_method(void)
b63792
+{
b63792
+#ifdef S390X_EC_ASM
b63792
+    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED25519)
b63792
+        && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED25519)
b63792
+        && OPENSSL_s390xcap_P.kdsa[0]
b63792
+            & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED25519))
b63792
+        return &ed25519_s390x_pkey_meth;
b63792
+#endif
b63792
+    return &ed25519_pkey_meth;
b63792
+}
b63792
+
b63792
+const EVP_PKEY_METHOD *ed448_pkey_method(void)
b63792
+{
b63792
+#ifdef S390X_EC_ASM
b63792
+    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED448)
b63792
+        && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED448)
b63792
+        && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED448))
b63792
+        return &ed448_s390x_pkey_meth;
b63792
+#endif
b63792
+    return &ed448_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/err/openssl.txt.s390x-ecc openssl-1.1.1g/crypto/err/openssl.txt
cf3dd1
--- openssl-1.1.1g/crypto/err/openssl.txt.s390x-ecc	2020-05-18 12:45:40.834234079 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/err/openssl.txt	2020-05-18 12:45:44.575266592 +0200
cf3dd1
@@ -496,6 +496,11 @@ EC_F_ECDSA_SIGN_EX:254:ECDSA_sign_ex
b63792
 EC_F_ECDSA_SIGN_SETUP:248:ECDSA_sign_setup
b63792
 EC_F_ECDSA_SIG_NEW:265:ECDSA_SIG_new
b63792
 EC_F_ECDSA_VERIFY:253:ECDSA_verify
b63792
+EC_F_ECDSA_SIMPLE_SIGN_SETUP:310:ecdsa_simple_sign_setup
b63792
+EC_F_ECDSA_SIMPLE_SIGN_SIG:311:ecdsa_simple_sign_sig
b63792
+EC_F_ECDSA_SIMPLE_VERIFY_SIG:312:ecdsa_simple_verify_sig
b63792
+EC_F_ECDSA_S390X_NISTP_SIGN_SIG:313:ecdsa_s390x_nistp_sign_sig
b63792
+EC_F_ECDSA_S390X_NISTP_VERIFY_SIG:314:ecdsa_s390x_nistp_verify_sig
b63792
 EC_F_ECD_ITEM_VERIFY:270:ecd_item_verify
b63792
 EC_F_ECKEY_PARAM2TYPE:223:eckey_param2type
b63792
 EC_F_ECKEY_PARAM_DECODE:212:eckey_param_decode
cf3dd1
@@ -657,6 +662,7 @@ EC_F_NISTP521_PRE_COMP_NEW:237:nistp521_
b63792
 EC_F_O2I_ECPUBLICKEY:152:o2i_ECPublicKey
b63792
 EC_F_OLD_EC_PRIV_DECODE:222:old_ec_priv_decode
b63792
 EC_F_OSSL_ECDH_COMPUTE_KEY:247:ossl_ecdh_compute_key
b63792
+EC_F_OSSL_ECDSA_SIGN_SETUP:300:ossl_ecdsa_sign_setup
b63792
 EC_F_OSSL_ECDSA_SIGN_SIG:249:ossl_ecdsa_sign_sig
b63792
 EC_F_OSSL_ECDSA_VERIFY_SIG:250:ossl_ecdsa_verify_sig
b63792
 EC_F_PKEY_ECD_CTRL:271:pkey_ecd_ctrl
cf3dd1
@@ -672,6 +678,12 @@ EC_F_PKEY_EC_KDF_DERIVE:283:pkey_ec_kdf_
b63792
 EC_F_PKEY_EC_KEYGEN:199:pkey_ec_keygen
b63792
 EC_F_PKEY_EC_PARAMGEN:219:pkey_ec_paramgen
b63792
 EC_F_PKEY_EC_SIGN:218:pkey_ec_sign
b63792
+EC_F_S390X_PKEY_ECD_DIGESTSIGN25519:303:s390x_pkey_ecd_digestsign25519
b63792
+EC_F_S390X_PKEY_ECD_DIGESTSIGN448:304:s390x_pkey_ecd_digestsign448
b63792
+EC_F_S390X_PKEY_ECD_KEYGEN25519:305:s390x_pkey_ecd_keygen25519
b63792
+EC_F_S390X_PKEY_ECD_KEYGEN448:306:s390x_pkey_ecd_keygen448
b63792
+EC_F_S390X_PKEY_ECX_KEYGEN25519:307:s390x_pkey_ecx_keygen25519
b63792
+EC_F_S390X_PKEY_ECX_KEYGEN448:308:s390x_pkey_ecx_keygen448
b63792
 EC_F_VALIDATE_ECX_DERIVE:278:validate_ecx_derive
b63792
 ENGINE_F_DIGEST_UPDATE:198:digest_update
b63792
 ENGINE_F_DYNAMIC_CTRL:180:dynamic_ctrl
cf3dd1
@@ -2160,6 +2172,7 @@ EC_R_BUFFER_TOO_SMALL:100:buffer too sma
b63792
 EC_R_CANNOT_INVERT:165:cannot invert
b63792
 EC_R_COORDINATES_OUT_OF_RANGE:146:coordinates out of range
b63792
 EC_R_CURVE_DOES_NOT_SUPPORT_ECDH:160:curve does not support ecdh
b63792
+EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA:170:curve does not support ecdsa
b63792
 EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING:159:curve does not support signing
b63792
 EC_R_D2I_ECPKPARAMETERS_FAILURE:117:d2i ecpkparameters failure
b63792
 EC_R_DECODE_ERROR:142:decode error
cf3dd1
diff -up openssl-1.1.1g/crypto/evp/pmeth_lib.c.s390x-ecc openssl-1.1.1g/crypto/evp/pmeth_lib.c
cf3dd1
--- openssl-1.1.1g/crypto/evp/pmeth_lib.c.s390x-ecc	2020-05-18 12:45:40.787233671 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/evp/pmeth_lib.c	2020-05-18 12:45:44.576266601 +0200
b63792
@@ -17,60 +17,67 @@
cf3dd1
 #include "crypto/evp.h"
b63792
 #include "internal/numbers.h"
b63792
 
b63792
+typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
b63792
 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
b63792
 
b63792
 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
b63792
 
b63792
 /* This array needs to be in order of NIDs */
b63792
-static const EVP_PKEY_METHOD *standard_methods[] = {
b63792
+static pmeth_fn standard_methods[] = {
b63792
 #ifndef OPENSSL_NO_RSA
b63792
-    &rsa_pkey_meth,
b63792
+    rsa_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_DH
b63792
-    &dh_pkey_meth,
b63792
+    dh_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_DSA
b63792
-    &dsa_pkey_meth,
b63792
+    dsa_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_EC
b63792
-    &ec_pkey_meth,
b63792
+    ec_pkey_method,
b63792
 #endif
b63792
-    &hmac_pkey_meth,
b63792
+    hmac_pkey_method,
b63792
 #ifndef OPENSSL_NO_CMAC
b63792
-    &cmac_pkey_meth,
b63792
+    cmac_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_RSA
b63792
-    &rsa_pss_pkey_meth,
b63792
+    rsa_pss_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_DH
b63792
-    &dhx_pkey_meth,
b63792
+    dhx_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_SCRYPT
b63792
-    &scrypt_pkey_meth,
b63792
+    scrypt_pkey_method,
b63792
 #endif
b63792
-    &tls1_prf_pkey_meth,
b63792
+    tls1_prf_pkey_method,
b63792
 #ifndef OPENSSL_NO_EC
b63792
-    &ecx25519_pkey_meth,
b63792
-    &ecx448_pkey_meth,
b63792
+    ecx25519_pkey_method,
b63792
+    ecx448_pkey_method,
b63792
 #endif
b63792
-    &hkdf_pkey_meth,
b63792
+    hkdf_pkey_method,
b63792
 #ifndef OPENSSL_NO_POLY1305
b63792
-    &poly1305_pkey_meth,
b63792
+    poly1305_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_SIPHASH
b63792
-    &siphash_pkey_meth,
b63792
+    siphash_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_EC
b63792
-    &ed25519_pkey_meth,
b63792
-    &ed448_pkey_meth,
b63792
+    ed25519_pkey_method,
b63792
+    ed448_pkey_method,
b63792
 #endif
b63792
 #ifndef OPENSSL_NO_SM2
b63792
-    &sm2_pkey_meth,
b63792
+    sm2_pkey_method,
b63792
 #endif
b63792
 };
b63792
 
b63792
-DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
b63792
-                           pmeth);
b63792
+DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
b63792
+
b63792
+static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
b63792
+{
b63792
+    return ((*a)->pkey_id - ((**b)())->pkey_id);
b63792
+}
b63792
+
b63792
+IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
b63792
 
b63792
 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
b63792
                      const EVP_PKEY_METHOD *const *b)
b63792
@@ -78,13 +85,11 @@ static int pmeth_cmp(const EVP_PKEY_METH
b63792
     return ((*a)->pkey_id - (*b)->pkey_id);
b63792
 }
b63792
 
b63792
-IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
b63792
-                             pmeth);
b63792
-
b63792
 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
b63792
 {
b63792
+    pmeth_fn *ret;
b63792
     EVP_PKEY_METHOD tmp;
b63792
-    const EVP_PKEY_METHOD *t = &tmp, **ret;
b63792
+    const EVP_PKEY_METHOD *t = &tm;;
b63792
     tmp.pkey_id = type;
b63792
     if (app_pkey_methods) {
b63792
         int idx;
b63792
@@ -92,12 +97,12 @@ const EVP_PKEY_METHOD *EVP_PKEY_meth_fin
b63792
         if (idx >= 0)
b63792
             return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
b63792
     }
b63792
-    ret = OBJ_bsearch_pmeth(&t, standard_methods,
b63792
-                            sizeof(standard_methods) /
b63792
-                            sizeof(EVP_PKEY_METHOD *));
b63792
+    ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
b63792
+                                 sizeof(standard_methods) /
b63792
+                                 sizeof(pmeth_fn));
b63792
     if (!ret || !*ret)
b63792
         return NULL;
b63792
-    return *ret;
b63792
+    return (**ret)();
b63792
 }
b63792
 
b63792
 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
b63792
@@ -348,7 +353,7 @@ size_t EVP_PKEY_meth_get_count(void)
b63792
 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
b63792
 {
b63792
     if (idx < OSSL_NELEM(standard_methods))
b63792
-        return standard_methods[idx];
b63792
+        return (standard_methods[idx])();
b63792
     if (app_pkey_methods == NULL)
b63792
         return NULL;
b63792
     idx -= OSSL_NELEM(standard_methods);
cf3dd1
diff -up openssl-1.1.1g/crypto/hmac/hm_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/hmac/hm_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/hmac/hm_pmeth.c.s390x-ecc	2020-05-18 12:45:40.796233749 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/hmac/hm_pmeth.c	2020-05-18 12:45:44.576266601 +0200
b63792
@@ -210,3 +210,8 @@ const EVP_PKEY_METHOD hmac_pkey_meth = {
b63792
     pkey_hmac_ctrl,
b63792
     pkey_hmac_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *hmac_pkey_method(void)
b63792
+{
b63792
+    return &hmac_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/kdf/hkdf.c.s390x-ecc openssl-1.1.1g/crypto/kdf/hkdf.c
cf3dd1
--- openssl-1.1.1g/crypto/kdf/hkdf.c.s390x-ecc	2020-05-18 12:45:40.826234009 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/kdf/hkdf.c	2020-05-18 12:45:44.577266609 +0200
b63792
@@ -233,6 +233,11 @@ const EVP_KDF_METHOD hkdf_kdf_meth = {
b63792
     kdf_hkdf_derive
b63792
 };
b63792
 
b63792
+const EVP_PKEY_METHOD *hkdf_pkey_method(void)
b63792
+{
b63792
+    return &hkdf_pkey_meth;
b63792
+}
b63792
+
b63792
 static int HKDF(const EVP_MD *evp_md,
b63792
                 const unsigned char *salt, size_t salt_len,
b63792
                 const unsigned char *key, size_t key_len,
cf3dd1
diff -up openssl-1.1.1g/crypto/kdf/scrypt.c.s390x-ecc openssl-1.1.1g/crypto/kdf/scrypt.c
cf3dd1
--- openssl-1.1.1g/crypto/kdf/scrypt.c.s390x-ecc	2020-05-18 12:45:40.827234018 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/kdf/scrypt.c	2020-05-18 12:45:44.578266618 +0200
cf3dd1
@@ -504,4 +504,9 @@ static int scrypt_alg(const char *pass,
b63792
     return rv;
b63792
 }
b63792
 
b63792
+const EVP_PKEY_METHOD *scrypt_pkey_method(void)
b63792
+{
b63792
+    return &scrypt_pkey_meth;
b63792
+}
b63792
+
b63792
 #endif
cf3dd1
diff -up openssl-1.1.1g/crypto/kdf/tls1_prf.c.s390x-ecc openssl-1.1.1g/crypto/kdf/tls1_prf.c
cf3dd1
--- openssl-1.1.1g/crypto/kdf/tls1_prf.c.s390x-ecc	2020-05-18 12:45:40.828234027 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/kdf/tls1_prf.c	2020-05-18 12:45:44.578266618 +0200
b63792
@@ -168,6 +168,11 @@ const EVP_KDF_METHOD tls1_prf_kdf_meth =
b63792
     kdf_tls1_prf_derive
b63792
 };
b63792
 
b63792
+const EVP_PKEY_METHOD *tls1_prf_pkey_method(void)
b63792
+{
b63792
+    return &tls1_prf_pkey_meth;
b63792
+}
b63792
+
b63792
 static int tls1_prf_P_hash(const EVP_MD *md,
b63792
                            const unsigned char *sec, size_t sec_len,
b63792
                            const unsigned char *seed, size_t seed_len,
cf3dd1
diff -up openssl-1.1.1g/crypto/poly1305/poly1305_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/poly1305/poly1305_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/poly1305/poly1305_pmeth.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/poly1305/poly1305_pmeth.c	2020-05-18 12:45:44.579266627 +0200
b63792
@@ -192,3 +192,8 @@ const EVP_PKEY_METHOD poly1305_pkey_meth
b63792
     pkey_poly1305_ctrl,
b63792
     pkey_poly1305_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *poly1305_pkey_method(void)
b63792
+{
b63792
+    return &poly1305_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/rsa/rsa_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/rsa/rsa_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/rsa/rsa_pmeth.c.s390x-ecc	2020-05-18 12:45:40.798233766 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/rsa/rsa_pmeth.c	2020-05-18 12:45:44.580266635 +0200
b63792
@@ -789,6 +789,11 @@ const EVP_PKEY_METHOD rsa_pkey_meth = {
b63792
     pkey_rsa_ctrl_str
b63792
 };
b63792
 
b63792
+const EVP_PKEY_METHOD *rsa_pkey_method(void)
b63792
+{
b63792
+    return &rsa_pkey_meth;
b63792
+}
b63792
+
b63792
 /*
b63792
  * Called for PSS sign or verify initialisation: checks PSS parameter
b63792
  * sanity and sets any restrictions on key usage.
b63792
@@ -859,3 +864,8 @@ const EVP_PKEY_METHOD rsa_pss_pkey_meth
b63792
     pkey_rsa_ctrl,
b63792
     pkey_rsa_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *rsa_pss_pkey_method(void)
b63792
+{
b63792
+    return &rsa_pss_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/s390x_arch.h.s390x-ecc openssl-1.1.1g/crypto/s390x_arch.h
cf3dd1
--- openssl-1.1.1g/crypto/s390x_arch.h.s390x-ecc	2020-05-18 12:45:40.603232072 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/s390x_arch.h	2020-05-18 12:45:44.580266635 +0200
b63792
@@ -26,6 +26,12 @@ void s390x_kmf(const unsigned char *in,
b63792
                unsigned int fc, void *param);
b63792
 void s390x_kma(const unsigned char *aad, size_t alen, const unsigned char *in,
b63792
                size_t len, unsigned char *out, unsigned int fc, void *param);
b63792
+int s390x_pcc(unsigned int fc, void *param);
b63792
+int s390x_kdsa(unsigned int fc, void *param, const unsigned char *in,
b63792
+               size_t len);
b63792
+
b63792
+void s390x_flip_endian32(unsigned char dst[32], const unsigned char src[32]);
b63792
+void s390x_flip_endian64(unsigned char dst[64], const unsigned char src[64]);
b63792
 
b63792
 /*
b63792
  * The field elements of OPENSSL_s390xcap_P are the 64-bit words returned by
b63792
@@ -45,6 +51,8 @@ struct OPENSSL_s390xcap_st {
b63792
     unsigned long long kmf[2];
b63792
     unsigned long long prno[2];
b63792
     unsigned long long kma[2];
b63792
+    unsigned long long pcc[2];
b63792
+    unsigned long long kdsa[2];
b63792
 };
b63792
 
b63792
 extern struct OPENSSL_s390xcap_st OPENSSL_s390xcap_P;
b63792
@@ -66,11 +74,14 @@ extern struct OPENSSL_s390xcap_st OPENSS
b63792
 # define S390X_KMF		0x90
b63792
 # define S390X_PRNO		0xa0
b63792
 # define S390X_KMA		0xb0
b63792
+# define S390X_PCC		0xc0
b63792
+# define S390X_KDSA		0xd0
b63792
 
b63792
 /* Facility Bit Numbers */
b63792
 # define S390X_VX		129
b63792
 # define S390X_VXD		134
b63792
 # define S390X_VXE		135
b63792
+# define S390X_MSA9		155	/* message-security-assist-ext. 9 */
b63792
 
b63792
 /* Function Codes */
b63792
 
b63792
@@ -94,10 +105,32 @@ extern struct OPENSSL_s390xcap_st OPENSS
b63792
 /* prno */
b63792
 # define S390X_TRNG		114
b63792
 
b63792
+/* pcc */
b63792
+# define S390X_SCALAR_MULTIPLY_P256	64
b63792
+# define S390X_SCALAR_MULTIPLY_P384	65
b63792
+# define S390X_SCALAR_MULTIPLY_P521	66
b63792
+# define S390X_SCALAR_MULTIPLY_ED25519  72
b63792
+# define S390X_SCALAR_MULTIPLY_ED448    73
b63792
+# define S390X_SCALAR_MULTIPLY_X25519   80
b63792
+# define S390X_SCALAR_MULTIPLY_X448     81
b63792
+
b63792
+/* kdsa */
b63792
+# define S390X_ECDSA_VERIFY_P256	1
b63792
+# define S390X_ECDSA_VERIFY_P384	2
b63792
+# define S390X_ECDSA_VERIFY_P521	3
b63792
+# define S390X_ECDSA_SIGN_P256		9
b63792
+# define S390X_ECDSA_SIGN_P384		10
b63792
+# define S390X_ECDSA_SIGN_P521		11
b63792
+# define S390X_EDDSA_VERIFY_ED25519     32
b63792
+# define S390X_EDDSA_VERIFY_ED448       36
b63792
+# define S390X_EDDSA_SIGN_ED25519       40
b63792
+# define S390X_EDDSA_SIGN_ED448         44
b63792
+
b63792
 /* Register 0 Flags */
b63792
 # define S390X_DECRYPT		0x80
b63792
 # define S390X_KMA_LPC		0x100
b63792
 # define S390X_KMA_LAAD		0x200
b63792
 # define S390X_KMA_HS		0x400
b63792
+# define S390X_KDSA_D		0x80
b63792
 
b63792
 #endif
cf3dd1
diff -up openssl-1.1.1g/crypto/s390xcpuid.pl.s390x-ecc openssl-1.1.1g/crypto/s390xcpuid.pl
cf3dd1
--- openssl-1.1.1g/crypto/s390xcpuid.pl.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/s390xcpuid.pl	2020-05-18 12:45:44.581266644 +0200
b63792
@@ -58,6 +58,10 @@ OPENSSL_s390x_facilities:
b63792
 	stg	%r0,S390X_PRNO+8(%r4)
b63792
 	stg	%r0,S390X_KMA(%r4)
b63792
 	stg	%r0,S390X_KMA+8(%r4)
b63792
+	stg	%r0,S390X_PCC(%r4)
b63792
+	stg	%r0,S390X_PCC+8(%r4)
b63792
+	stg	%r0,S390X_KDSA(%r4)
b63792
+	stg	%r0,S390X_KDSA+8(%r4)
b63792
 
b63792
 	.long	0xb2b04000		# stfle	0(%r4)
b63792
 	brc	8,.Ldone
b63792
@@ -68,6 +72,7 @@ OPENSSL_s390x_facilities:
b63792
 	.long	0xb2b04000		# stfle 0(%r4)
b63792
 .Ldone:
b63792
 	lmg	%r2,%r3,S390X_STFLE(%r4)
b63792
+
b63792
 	tmhl	%r2,0x4000		# check for message-security-assist
b63792
 	jz	.Lret
b63792
 
b63792
@@ -91,6 +96,13 @@ OPENSSL_s390x_facilities:
b63792
 	la	%r1,S390X_KMAC(%r4)
b63792
 	.long	0xb91e0042		# kmac %r4,%r2
b63792
 
b63792
+	tmhh	%r3,0x0008		# check for message-security-assist-3
b63792
+	jz	.Lret
b63792
+
b63792
+	lghi	%r0,S390X_QUERY		# query pcc capability vector
b63792
+	la	%r1,S390X_PCC(%r4)
b63792
+	.long	0xb92c0000		# pcc
b63792
+
b63792
 	tmhh	%r3,0x0004		# check for message-security-assist-4
b63792
 	jz	.Lret
b63792
 
b63792
@@ -114,6 +126,7 @@ OPENSSL_s390x_facilities:
b63792
 	.long	0xb93c0042		# prno %r4,%r2
b63792
 
b63792
 	lg	%r2,S390X_STFLE+16(%r4)
b63792
+
b63792
 	tmhl	%r2,0x2000		# check for message-security-assist-8
b63792
 	jz	.Lret
b63792
 
b63792
@@ -121,6 +134,13 @@ OPENSSL_s390x_facilities:
b63792
 	la	%r1,S390X_KMA(%r4)
b63792
 	.long	0xb9294022		# kma %r2,%r4,%r2
b63792
 
b63792
+	tmhl	%r2,0x0010		# check for message-security-assist-9
b63792
+	jz	.Lret
b63792
+
b63792
+	lghi	%r0,S390X_QUERY		# query kdsa capability vector
b63792
+	la	%r1,S390X_KDSA(%r4)
b63792
+	.long	0xb93a0002		# kdsa %r0,%r2
b63792
+
b63792
 .Lret:
b63792
 	br	$ra
b63792
 .size	OPENSSL_s390x_facilities,.-OPENSSL_s390x_facilities
b63792
@@ -411,6 +431,113 @@ s390x_kma:
b63792
 ___
b63792
 }
b63792
 
b63792
+################
b63792
+# int s390x_pcc(unsigned int fc, void *param)
b63792
+{
b63792
+my ($fc,$param) = map("%r$_",(2..3));
b63792
+$code.=<<___;
b63792
+.globl	s390x_pcc
b63792
+.type	s390x_pcc,\@function
b63792
+.align	16
b63792
+s390x_pcc:
b63792
+	lr	%r0,$fc
b63792
+	l${g}r	%r1,$param
b63792
+	lhi	%r2,0
b63792
+
b63792
+	.long	0xb92c0000	# pcc
b63792
+	brc	1,.-4		# pay attention to "partial completion"
b63792
+	brc	7,.Lpcc_err	# if CC==0 return 0, else return 1
b63792
+.Lpcc_out:
b63792
+	br	$ra
b63792
+.Lpcc_err:
b63792
+	lhi	%r2,1
b63792
+	j	.Lpcc_out
b63792
+.size	s390x_pcc,.-s390x_pcc
b63792
+___
b63792
+}
b63792
+
b63792
+################
b63792
+# int s390x_kdsa(unsigned int fc, void *param,
b63792
+#                const unsigned char *in, size_t len)
b63792
+{
b63792
+my ($fc,$param,$in,$len) = map("%r$_",(2..5));
b63792
+$code.=<<___;
b63792
+.globl	s390x_kdsa
b63792
+.type	s390x_kdsa,\@function
b63792
+.align	16
b63792
+s390x_kdsa:
b63792
+	lr	%r0,$fc
b63792
+	l${g}r	%r1,$param
b63792
+	lhi	%r2,0
b63792
+
b63792
+	.long	0xb93a0004	# kdsa %r0,$in
b63792
+	brc	1,.-4		# pay attention to "partial completion"
b63792
+	brc	7,.Lkdsa_err	# if CC==0 return 0, else return 1
b63792
+.Lkdsa_out:
b63792
+	br	$ra
b63792
+.Lkdsa_err:
b63792
+	lhi	%r2,1
b63792
+	j	.Lkdsa_out
b63792
+.size	s390x_kdsa,.-s390x_kdsa
b63792
+___
b63792
+}
b63792
+
b63792
+################
b63792
+# void s390x_flip_endian32(unsigned char dst[32], const unsigned char src[32])
b63792
+{
b63792
+my ($dst,$src) = map("%r$_",(2..3));
b63792
+$code.=<<___;
b63792
+.globl	s390x_flip_endian32
b63792
+.type	s390x_flip_endian32,\@function
b63792
+.align	16
b63792
+s390x_flip_endian32:
b63792
+	lrvg	%r0,0(%r0,$src)
b63792
+	lrvg	%r1,8(%r0,$src)
b63792
+	lrvg	%r4,16(%r0,$src)
b63792
+	lrvg	%r5,24(%r0,$src)
b63792
+	stg	%r0,24(%r0,$dst)
b63792
+	stg	%r1,16(%r0,$dst)
b63792
+	stg	%r4,8(%r0,$dst)
b63792
+	stg	%r5,0(%r0,$dst)
b63792
+	br	$ra
b63792
+.size	s390x_flip_endian32,.-s390x_flip_endian32
b63792
+___
b63792
+}
b63792
+
b63792
+################
b63792
+# void s390x_flip_endian64(unsigned char dst[64], const unsigned char src[64])
b63792
+{
b63792
+my ($dst,$src) = map("%r$_",(2..3));
b63792
+$code.=<<___;
b63792
+.globl	s390x_flip_endian64
b63792
+.type	s390x_flip_endian64,\@function
b63792
+.align	16
b63792
+s390x_flip_endian64:
b63792
+	stmg	%r6,%r9,6*$SIZE_T($sp)
b63792
+
b63792
+	lrvg	%r0,0(%r0,$src)
b63792
+	lrvg	%r1,8(%r0,$src)
b63792
+	lrvg	%r4,16(%r0,$src)
b63792
+	lrvg	%r5,24(%r0,$src)
b63792
+	lrvg	%r6,32(%r0,$src)
b63792
+	lrvg	%r7,40(%r0,$src)
b63792
+	lrvg	%r8,48(%r0,$src)
b63792
+	lrvg	%r9,56(%r0,$src)
b63792
+	stg	%r0,56(%r0,$dst)
b63792
+	stg	%r1,48(%r0,$dst)
b63792
+	stg	%r4,40(%r0,$dst)
b63792
+	stg	%r5,32(%r0,$dst)
b63792
+	stg	%r6,24(%r0,$dst)
b63792
+	stg	%r7,16(%r0,$dst)
b63792
+	stg	%r8,8(%r0,$dst)
b63792
+	stg	%r9,0(%r0,$dst)
b63792
+
b63792
+	lmg	%r6,%r9,6*$SIZE_T($sp)
b63792
+	br	$ra
b63792
+.size	s390x_flip_endian64,.-s390x_flip_endian64
b63792
+___
b63792
+}
b63792
+
b63792
 $code.=<<___;
b63792
 .section	.init
b63792
 	brasl	$ra,OPENSSL_cpuid_setup
cf3dd1
diff -up openssl-1.1.1g/crypto/siphash/siphash_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/siphash/siphash_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/siphash/siphash_pmeth.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/siphash/siphash_pmeth.c	2020-05-18 12:45:44.581266644 +0200
b63792
@@ -203,3 +203,8 @@ const EVP_PKEY_METHOD siphash_pkey_meth
b63792
     pkey_siphash_ctrl,
b63792
     pkey_siphash_ctrl_str
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *siphash_pkey_method(void)
b63792
+{
b63792
+    return &siphash_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/crypto/sm2/sm2_pmeth.c.s390x-ecc openssl-1.1.1g/crypto/sm2/sm2_pmeth.c
cf3dd1
--- openssl-1.1.1g/crypto/sm2/sm2_pmeth.c.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/crypto/sm2/sm2_pmeth.c	2020-05-18 12:45:44.582266653 +0200
cf3dd1
@@ -327,3 +327,8 @@ const EVP_PKEY_METHOD sm2_pkey_meth = {
b63792
 
b63792
     pkey_sm2_digest_custom
b63792
 };
b63792
+
b63792
+const EVP_PKEY_METHOD *sm2_pkey_method(void)
b63792
+{
b63792
+    return &sm2_pkey_meth;
b63792
+}
cf3dd1
diff -up openssl-1.1.1g/include/crypto/evp.h.s390x-ecc openssl-1.1.1g/include/crypto/evp.h
cf3dd1
--- openssl-1.1.1g/include/crypto/evp.h.s390x-ecc	2020-05-18 12:45:40.834234079 +0200
cf3dd1
+++ openssl-1.1.1g/include/crypto/evp.h	2020-05-18 12:45:44.577266609 +0200
cf3dd1
@@ -459,3 +459,22 @@ void evp_encode_ctx_set_flags(EVP_ENCODE
cf3dd1
 #define EVP_ENCODE_CTX_NO_NEWLINES          1
cf3dd1
 /* Use the SRP base64 alphabet instead of the standard one */
cf3dd1
 #define EVP_ENCODE_CTX_USE_SRP_ALPHABET     2
cf3dd1
+
cf3dd1
+const EVP_PKEY_METHOD *cmac_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *dh_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *dhx_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *dsa_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *ec_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *sm2_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *ecx25519_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *ecx448_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *ed25519_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *ed448_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *hmac_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *rsa_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *rsa_pss_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *scrypt_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *tls1_prf_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *hkdf_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *poly1305_pkey_method(void);
cf3dd1
+const EVP_PKEY_METHOD *siphash_pkey_method(void);
cf3dd1
diff -up openssl-1.1.1g/include/internal/constant_time.h.s390x-ecc openssl-1.1.1g/include/internal/constant_time.h
cf3dd1
--- openssl-1.1.1g/include/internal/constant_time.h.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/include/internal/constant_time.h	2020-05-18 12:45:44.582266653 +0200
cf3dd1
@@ -353,6 +353,34 @@ static ossl_inline void constant_time_co
b63792
 }
b63792
 
b63792
 /*
b63792
+ * mask must be 0xFF or 0x00.
b63792
+ * "constant time" is per len.
b63792
+ *
b63792
+ * if (mask) {
b63792
+ *     unsigned char tmp[len];
b63792
+ *
b63792
+ *     memcpy(tmp, a, len);
b63792
+ *     memcpy(a, b);
b63792
+ *     memcpy(b, tmp);
b63792
+ * }
b63792
+ */
b63792
+static ossl_inline void constant_time_cond_swap_buff(unsigned char mask,
b63792
+                                                     unsigned char *a,
b63792
+                                                     unsigned char *b,
b63792
+                                                     size_t len)
b63792
+{
b63792
+    size_t i;
b63792
+    unsigned char tmp;
b63792
+
b63792
+    for (i = 0; i < len; i++) {
b63792
+        tmp = a[i] ^ b[i];
b63792
+        tmp &= mask;
b63792
+        a[i] ^= tmp;
b63792
+        b[i] ^= tmp;
b63792
+    }
b63792
+}
b63792
+
b63792
+/*
b63792
  * table is a two dimensional array of bytes. Each row has rowsize elements.
b63792
  * Copies row number idx into out. rowsize and numrows are not considered
b63792
  * private.
cf3dd1
diff -up openssl-1.1.1g/include/openssl/ecerr.h.s390x-ecc openssl-1.1.1g/include/openssl/ecerr.h
cf3dd1
--- openssl-1.1.1g/include/openssl/ecerr.h.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/include/openssl/ecerr.h	2020-05-18 12:45:44.583266662 +0200
cf3dd1
@@ -42,6 +42,11 @@ int ERR_load_EC_strings(void);
b63792
 #  define EC_F_ECDSA_SIGN_SETUP                            248
b63792
 #  define EC_F_ECDSA_SIG_NEW                               265
b63792
 #  define EC_F_ECDSA_VERIFY                                253
b63792
+#  define EC_F_ECDSA_SIMPLE_SIGN_SETUP                     310
b63792
+#  define EC_F_ECDSA_SIMPLE_SIGN_SIG                       311
b63792
+#  define EC_F_ECDSA_SIMPLE_VERIFY_SIG                     312
b63792
+#  define EC_F_ECDSA_S390X_NISTP_SIGN_SIG                  313
b63792
+#  define EC_F_ECDSA_S390X_NISTP_VERIFY_SIG                314
b63792
 #  define EC_F_ECD_ITEM_VERIFY                             270
b63792
 #  define EC_F_ECKEY_PARAM2TYPE                            223
b63792
 #  define EC_F_ECKEY_PARAM_DECODE                          212
cf3dd1
@@ -185,6 +190,7 @@ int ERR_load_EC_strings(void);
b63792
 #  define EC_F_O2I_ECPUBLICKEY                             152
b63792
 #  define EC_F_OLD_EC_PRIV_DECODE                          222
b63792
 #  define EC_F_OSSL_ECDH_COMPUTE_KEY                       247
b63792
+#  define EC_F_OSSL_ECDSA_SIGN_SETUP                       300
b63792
 #  define EC_F_OSSL_ECDSA_SIGN_SIG                         249
b63792
 #  define EC_F_OSSL_ECDSA_VERIFY_SIG                       250
b63792
 #  define EC_F_PKEY_ECD_CTRL                               271
cf3dd1
@@ -200,6 +206,12 @@ int ERR_load_EC_strings(void);
b63792
 #  define EC_F_PKEY_EC_KEYGEN                              199
b63792
 #  define EC_F_PKEY_EC_PARAMGEN                            219
b63792
 #  define EC_F_PKEY_EC_SIGN                                218
b63792
+#   define EC_F_S390X_PKEY_ECD_DIGESTSIGN25519             320
b63792
+#   define EC_F_S390X_PKEY_ECD_DIGESTSIGN448               321
b63792
+#   define EC_F_S390X_PKEY_ECD_KEYGEN25519                 322
b63792
+#   define EC_F_S390X_PKEY_ECD_KEYGEN448                   323
b63792
+#   define EC_F_S390X_PKEY_ECX_KEYGEN25519                 324
b63792
+#   define EC_F_S390X_PKEY_ECX_KEYGEN448                   325
b63792
 #  define EC_F_VALIDATE_ECX_DERIVE                         278
b63792
 
b63792
 /*
cf3dd1
@@ -212,6 +224,7 @@ int ERR_load_EC_strings(void);
b63792
 #  define EC_R_CANNOT_INVERT                               165
b63792
 #  define EC_R_COORDINATES_OUT_OF_RANGE                    146
b63792
 #  define EC_R_CURVE_DOES_NOT_SUPPORT_ECDH                 160
b63792
+#  define EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA                170
b63792
 #  define EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING              159
b63792
 #  define EC_R_D2I_ECPKPARAMETERS_FAILURE                  117
b63792
 #  define EC_R_DECODE_ERROR                                142
cf3dd1
diff -up openssl-1.1.1g/test/recipes/30-test_evp_data/evppkey.txt.s390x-ecc openssl-1.1.1g/test/recipes/30-test_evp_data/evppkey.txt
cf3dd1
--- openssl-1.1.1g/test/recipes/30-test_evp_data/evppkey.txt.s390x-ecc	2020-04-21 14:22:39.000000000 +0200
cf3dd1
+++ openssl-1.1.1g/test/recipes/30-test_evp_data/evppkey.txt	2020-05-18 12:45:44.590266722 +0200
b63792
@@ -814,6 +814,8 @@ PublicKeyRaw=Bob-448-PUBLIC-Raw:X448:3eb
b63792
 
b63792
 PrivPubKeyPair = Bob-448-Raw:Bob-448-PUBLIC-Raw
b63792
 
b63792
+PublicKeyRaw=Bob-448-PUBLIC-Raw-NonCanonical:X448:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
b63792
+
b63792
 Derive=Alice-448
b63792
 PeerKey=Bob-448-PUBLIC
b63792
 SharedSecret=07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b56fd2464c335543936521c24403085d59a449a5037514a879d
b63792
@@ -830,6 +832,11 @@ Derive=Bob-448-Raw
b63792
 PeerKey=Alice-448-PUBLIC-Raw
b63792
 SharedSecret=07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b56fd2464c335543936521c24403085d59a449a5037514a879d
b63792
 
b63792
+# Self-generated non-canonical
b63792
+Derive=Alice-448-Raw
b63792
+PeerKey=Bob-448-PUBLIC-Raw-NonCanonical
b63792
+SharedSecret=66e2e682b1f8e68c809f1bb3e406bd826921d9c1a5bfbfcbab7ae72feecee63660eabd54934f3382061d17607f581a90bdac917a064959fb
b63792
+
b63792
 # Illegal sign/verify operations with X448 key
b63792
 
b63792
 Sign=Alice-448