Blame SOURCES/openssl-1.0.2k-s390x-update.patch

bbd2dd
diff -up openssl-1.0.2k/crypto/aes/asm/aes-s390x.pl.s390x-update openssl-1.0.2k/crypto/aes/asm/aes-s390x.pl
bbd2dd
--- openssl-1.0.2k/crypto/aes/asm/aes-s390x.pl.s390x-update	2018-06-18 12:20:47.197994636 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/aes/asm/aes-s390x.pl	2018-06-18 13:27:37.109817203 +0200
bbd2dd
@@ -122,6 +122,8 @@ sub _data_word()
bbd2dd
 }
bbd2dd
 
bbd2dd
 $code=<<___;
bbd2dd
+#include "s390x_arch.h"
bbd2dd
+
bbd2dd
 .text
bbd2dd
 
bbd2dd
 .type	AES_Te,\@object
bbd2dd
@@ -814,13 +816,10 @@ $code.=<<___ if (!$softonly);
bbd2dd
 	ar	%r5,%r0
bbd2dd
 
bbd2dd
 	larl	%r1,OPENSSL_s390xcap_P
bbd2dd
-	lg	%r0,0(%r1)
bbd2dd
-	tmhl	%r0,0x4000	# check for message-security assist
bbd2dd
-	jz	.Lekey_internal
bbd2dd
-
bbd2dd
 	llihh	%r0,0x8000
bbd2dd
 	srlg	%r0,%r0,0(%r5)
bbd2dd
-	ng	%r0,48(%r1)	# check kmc capability vector
bbd2dd
+	ng	%r0,S390X_KM(%r1)  # check availability of both km...
bbd2dd
+	ng	%r0,S390X_KMC(%r1) # ...and kmc support for given key length
bbd2dd
 	jz	.Lekey_internal
bbd2dd
 
bbd2dd
 	lmg	%r0,%r1,0($inp)	# just copy 128 bits...
bbd2dd
@@ -1443,7 +1442,7 @@ $code.=<<___ if (0);	######### kmctr cod
bbd2dd
 	larl	%r1,OPENSSL_s390xcap_P
bbd2dd
 	llihh	%r0,0x8000	# check if kmctr supports the function code
bbd2dd
 	srlg	%r0,%r0,0($s0)
bbd2dd
-	ng	%r0,64(%r1)	# check kmctr capability vector
bbd2dd
+	ng	%r0,S390X_KMCTR(%r1)	# check kmctr capability vector
bbd2dd
 	lgr	%r0,$s0
bbd2dd
 	lgr	%r1,$s1
bbd2dd
 	jz	.Lctr32_km_loop
bbd2dd
@@ -1593,7 +1592,7 @@ $code.=<<___ if(1);
bbd2dd
 	larl	%r1,OPENSSL_s390xcap_P
bbd2dd
 	llihh	%r0,0x8000
bbd2dd
 	srlg	%r0,%r0,32($s1)		# check for 32+function code
bbd2dd
-	ng	%r0,32(%r1)		# check km capability vector
bbd2dd
+	ng	%r0,S390X_KM(%r1)	# check km capability vector
bbd2dd
 	lgr	%r0,$s0			# restore the function code
bbd2dd
 	la	%r1,0($key1)		# restore $key1
bbd2dd
 	jz	.Lxts_km_vanilla
bbd2dd
@@ -2220,7 +2219,6 @@ ___
bbd2dd
 }
bbd2dd
 $code.=<<___;
bbd2dd
 .string	"AES for s390x, CRYPTOGAMS by <appro\@openssl.org>"
bbd2dd
-.comm	OPENSSL_s390xcap_P,80,8
bbd2dd
 ___
bbd2dd
 
bbd2dd
 $code =~ s/\`([^\`]*)\`/eval $1/gem;
bbd2dd
diff -up openssl-1.0.2k/crypto/aes/Makefile.s390x-update openssl-1.0.2k/crypto/aes/Makefile
bbd2dd
--- openssl-1.0.2k/crypto/aes/Makefile.s390x-update	2017-01-26 14:22:03.000000000 +0100
bbd2dd
+++ openssl-1.0.2k/crypto/aes/Makefile	2018-06-18 13:27:37.108817179 +0200
bbd2dd
@@ -92,6 +92,8 @@ aesv8-armx.S:	asm/aesv8-armx.pl
bbd2dd
 	$(PERL) asm/aesv8-armx.pl $(PERLASM_SCHEME) $@
bbd2dd
 aesv8-armx.o:	aesv8-armx.S
bbd2dd
 
bbd2dd
+aes-s390x.o:	aes-s390x.S
bbd2dd
+
bbd2dd
 # GNU make "catch all"
bbd2dd
 aes-%.S:	asm/aes-%.pl;	$(PERL) $< $(PERLASM_SCHEME) > $@
bbd2dd
 aes-armv4.o:	aes-armv4.S
bbd2dd
diff -up openssl-1.0.2k/crypto/evp/e_aes.c.s390x-update openssl-1.0.2k/crypto/evp/e_aes.c
bbd2dd
--- openssl-1.0.2k/crypto/evp/e_aes.c.s390x-update	2018-06-18 12:20:47.104992361 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/evp/e_aes.c	2018-06-18 13:28:07.033543735 +0200
bbd2dd
@@ -854,6 +854,723 @@ static const EVP_CIPHER aes_##keylen##_#
bbd2dd
 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
bbd2dd
 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
bbd2dd
 
bbd2dd
+#elif defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
bbd2dd
+/*
bbd2dd
+ * IBM S390X support
bbd2dd
+ */
bbd2dd
+# include "s390x_arch.h"
bbd2dd
+
bbd2dd
+typedef struct {
bbd2dd
+    union {
bbd2dd
+        double align;
bbd2dd
+        /*-
bbd2dd
+         * KMA-GCM-AES parameter block
bbd2dd
+         * (see z/Architecture Principles of Operation SA22-7832-11)
bbd2dd
+         */
bbd2dd
+        struct {
bbd2dd
+            unsigned char reserved[12];
bbd2dd
+            union {
bbd2dd
+                unsigned int w;
bbd2dd
+                unsigned char b[4];
bbd2dd
+            } cv;
bbd2dd
+            union {
bbd2dd
+                unsigned long long g[2];
bbd2dd
+                unsigned char b[16];
bbd2dd
+            } t;
bbd2dd
+            unsigned char h[16];
bbd2dd
+            unsigned long long taadl;
bbd2dd
+            unsigned long long tpcl;
bbd2dd
+            union {
bbd2dd
+                unsigned long long g[2];
bbd2dd
+                unsigned int w[4];
bbd2dd
+            } j0;
bbd2dd
+            unsigned char k[32];
bbd2dd
+        } param;
bbd2dd
+    } kma;
bbd2dd
+    unsigned int fc;
bbd2dd
+    int key_set;
bbd2dd
+
bbd2dd
+    unsigned char *iv;
bbd2dd
+    int ivlen;
bbd2dd
+    int iv_set;
bbd2dd
+    int iv_gen;
bbd2dd
+
bbd2dd
+    int taglen;
bbd2dd
+
bbd2dd
+    unsigned char ares[16];
bbd2dd
+    unsigned char mres[16];
bbd2dd
+    unsigned char kres[16];
bbd2dd
+    int areslen;
bbd2dd
+    int mreslen;
bbd2dd
+    int kreslen;
bbd2dd
+
bbd2dd
+    int tls_aad_len;
bbd2dd
+} S390X_AES_GCM_CTX;
bbd2dd
+
bbd2dd
+# define S390X_aes_128_CAPABLE ((OPENSSL_s390xcap_P.km[0] &	\
bbd2dd
+                                 S390X_CAPBIT(S390X_AES_128)) &&\
bbd2dd
+                                (OPENSSL_s390xcap_P.kmc[0] &	\
bbd2dd
+                                 S390X_CAPBIT(S390X_AES_128)))
bbd2dd
+# define S390X_aes_192_CAPABLE ((OPENSSL_s390xcap_P.km[0] &	\
bbd2dd
+                                 S390X_CAPBIT(S390X_AES_192)) &&\
bbd2dd
+                                (OPENSSL_s390xcap_P.kmc[0] &	\
bbd2dd
+                                 S390X_CAPBIT(S390X_AES_192)))
bbd2dd
+# define S390X_aes_256_CAPABLE ((OPENSSL_s390xcap_P.km[0] &	\
bbd2dd
+                                 S390X_CAPBIT(S390X_AES_256)) &&\
bbd2dd
+                                (OPENSSL_s390xcap_P.kmc[0] &	\
bbd2dd
+                                 S390X_CAPBIT(S390X_AES_256)))
bbd2dd
+
bbd2dd
+# define s390x_aes_init_key aes_init_key
bbd2dd
+static int s390x_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
bbd2dd
+                              const unsigned char *iv, int enc);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_cbc_CAPABLE	1	/* checked by callee */
bbd2dd
+# define S390X_aes_192_cbc_CAPABLE	1
bbd2dd
+# define S390X_aes_256_cbc_CAPABLE	1
bbd2dd
+
bbd2dd
+# define s390x_aes_cbc_cipher aes_cbc_cipher
bbd2dd
+static int s390x_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_ecb_CAPABLE	0
bbd2dd
+# define S390X_aes_192_ecb_CAPABLE	0
bbd2dd
+# define S390X_aes_256_ecb_CAPABLE	0
bbd2dd
+
bbd2dd
+# define s390x_aes_ecb_cipher aes_ecb_cipher
bbd2dd
+static int s390x_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_ofb_CAPABLE	0
bbd2dd
+# define S390X_aes_192_ofb_CAPABLE	0
bbd2dd
+# define S390X_aes_256_ofb_CAPABLE	0
bbd2dd
+
bbd2dd
+# define s390x_aes_ofb_cipher aes_ofb_cipher
bbd2dd
+static int s390x_aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_cfb_CAPABLE	0
bbd2dd
+# define S390X_aes_192_cfb_CAPABLE	0
bbd2dd
+# define S390X_aes_256_cfb_CAPABLE	0
bbd2dd
+
bbd2dd
+# define s390x_aes_cfb_cipher aes_cfb_cipher
bbd2dd
+static int s390x_aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_cfb8_CAPABLE	0
bbd2dd
+# define S390X_aes_192_cfb8_CAPABLE	0
bbd2dd
+# define S390X_aes_256_cfb8_CAPABLE	0
bbd2dd
+
bbd2dd
+# define s390x_aes_cfb8_cipher aes_cfb8_cipher
bbd2dd
+static int s390x_aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                 const unsigned char *in, size_t len);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_cfb1_CAPABLE	0
bbd2dd
+# define S390X_aes_192_cfb1_CAPABLE	0
bbd2dd
+# define S390X_aes_256_cfb1_CAPABLE	0
bbd2dd
+
bbd2dd
+# define s390x_aes_cfb1_cipher aes_cfb1_cipher
bbd2dd
+static int s390x_aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                 const unsigned char *in, size_t len);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_ctr_CAPABLE	1	/* checked by callee */
bbd2dd
+# define S390X_aes_192_ctr_CAPABLE	1
bbd2dd
+# define S390X_aes_256_ctr_CAPABLE	1
bbd2dd
+
bbd2dd
+# define s390x_aes_ctr_cipher aes_ctr_cipher
bbd2dd
+static int s390x_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len);
bbd2dd
+
bbd2dd
+# define S390X_aes_128_gcm_CAPABLE (S390X_aes_128_CAPABLE &&		\
bbd2dd
+                                    (OPENSSL_s390xcap_P.kma[0] &	\
bbd2dd
+                                     S390X_CAPBIT(S390X_AES_128)))
bbd2dd
+# define S390X_aes_192_gcm_CAPABLE (S390X_aes_192_CAPABLE &&		\
bbd2dd
+                                    (OPENSSL_s390xcap_P.kma[0] &	\
bbd2dd
+                                     S390X_CAPBIT(S390X_AES_192)))
bbd2dd
+# define S390X_aes_256_gcm_CAPABLE (S390X_aes_256_CAPABLE &&		\
bbd2dd
+                                    (OPENSSL_s390xcap_P.kma[0] &	\
bbd2dd
+                                     S390X_CAPBIT(S390X_AES_256)))
bbd2dd
+
bbd2dd
+/* iv + padding length for iv lenghts != 12 */
bbd2dd
+# define S390X_gcm_ivpadlen(i)	((((i) + 15) >> 4 << 4) + 16)
bbd2dd
+
bbd2dd
+static int s390x_aes_gcm_aad(S390X_AES_GCM_CTX *ctx, const unsigned char *aad,
bbd2dd
+                             size_t len)
bbd2dd
+{
bbd2dd
+    unsigned long long alen;
bbd2dd
+    int n, rem;
bbd2dd
+
bbd2dd
+    if (ctx->kma.param.tpcl)
bbd2dd
+        return -2;
bbd2dd
+
bbd2dd
+    alen = ctx->kma.param.taadl + len;
bbd2dd
+    if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
bbd2dd
+        return -1;
bbd2dd
+    ctx->kma.param.taadl = alen;
bbd2dd
+
bbd2dd
+    n = ctx->areslen;
bbd2dd
+    if (n) {
bbd2dd
+        while (n && len) {
bbd2dd
+            ctx->ares[n] = *aad;
bbd2dd
+            n = (n + 1) & 0xf;
bbd2dd
+            ++aad;
bbd2dd
+            --len;
bbd2dd
+        }
bbd2dd
+        /* ctx->ares contains a complete block if offset has wrapped around */
bbd2dd
+        if (!n) {
bbd2dd
+            s390x_kma(ctx->ares, 16, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
bbd2dd
+            ctx->fc |= S390X_KMA_HS;
bbd2dd
+        }
bbd2dd
+        ctx->areslen = n;
bbd2dd
+    }
bbd2dd
+
bbd2dd
+    rem = len & 0xf;
bbd2dd
+
bbd2dd
+    len &= ~0xf;
bbd2dd
+    if (len) {
bbd2dd
+        s390x_kma(aad, len, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
bbd2dd
+        aad += len;
bbd2dd
+        ctx->fc |= S390X_KMA_HS;
bbd2dd
+    }
bbd2dd
+
bbd2dd
+    if (rem) {
bbd2dd
+        ctx->areslen = rem;
bbd2dd
+
bbd2dd
+        do {
bbd2dd
+            --rem;
bbd2dd
+            ctx->ares[rem] = aad[rem];
bbd2dd
+        } while (rem);
bbd2dd
+    }
bbd2dd
+    return 0;
bbd2dd
+}
bbd2dd
+
bbd2dd
+static int s390x_aes_gcm(S390X_AES_GCM_CTX *ctx, const unsigned char *in,
bbd2dd
+                         unsigned char *out, size_t len)
bbd2dd
+{
bbd2dd
+    const unsigned char *inptr;
bbd2dd
+    unsigned long long mlen;
bbd2dd
+    union {
bbd2dd
+        unsigned int w[4];
bbd2dd
+        unsigned char b[16];
bbd2dd
+    } buf;
bbd2dd
+    size_t inlen;
bbd2dd
+    int n, rem, i;
bbd2dd
+
bbd2dd
+    mlen = ctx->kma.param.tpcl + len;
bbd2dd
+    if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
bbd2dd
+        return -1;
bbd2dd
+    ctx->kma.param.tpcl = mlen;
bbd2dd
+
bbd2dd
+    n = ctx->mreslen;
bbd2dd
+    if (n) {
bbd2dd
+        inptr = in;
bbd2dd
+        inlen = len;
bbd2dd
+        while (n && inlen) {
bbd2dd
+            ctx->mres[n] = *inptr;
bbd2dd
+            n = (n + 1) & 0xf;
bbd2dd
+            ++inptr;
bbd2dd
+            --inlen;
bbd2dd
+        }
bbd2dd
+        /* ctx->mres contains a complete block if offset has wrapped around */
bbd2dd
+        if (!n) {
bbd2dd
+            s390x_kma(ctx->ares, ctx->areslen, ctx->mres, 16, buf.b,
bbd2dd
+                      ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
bbd2dd
+            ctx->fc |= S390X_KMA_HS;
bbd2dd
+            ctx->areslen = 0;
bbd2dd
+
bbd2dd
+            /* previous call already encrypted/decrypted its remainder,
bbd2dd
+             * see comment below */
bbd2dd
+            n = ctx->mreslen;
bbd2dd
+            while (n) {
bbd2dd
+                *out = buf.b[n];
bbd2dd
+                n = (n + 1) & 0xf;
bbd2dd
+                ++out;
bbd2dd
+                ++in;
bbd2dd
+                --len;
bbd2dd
+            }
bbd2dd
+            ctx->mreslen = 0;
bbd2dd
+        }
bbd2dd
+    }
bbd2dd
+
bbd2dd
+    rem = len & 0xf;
bbd2dd
+
bbd2dd
+    len &= ~0xf;
bbd2dd
+    if (len) {
bbd2dd
+        s390x_kma(ctx->ares, ctx->areslen, in, len, out,
bbd2dd
+                  ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
bbd2dd
+        in += len;
bbd2dd
+        out += len;
bbd2dd
+        ctx->fc |= S390X_KMA_HS;
bbd2dd
+        ctx->areslen = 0;
bbd2dd
+    }
bbd2dd
+
bbd2dd
+    /*-
bbd2dd
+     * If there is a remainder, it has to be saved such that it can be
bbd2dd
+     * processed by kma later. However, we also have to do the for-now
bbd2dd
+     * unauthenticated encryption/decryption part here and now...
bbd2dd
+     */
bbd2dd
+    if (rem) {
bbd2dd
+        if (!ctx->mreslen) {
bbd2dd
+            buf.w[0] = ctx->kma.param.j0.w[0];
bbd2dd
+            buf.w[1] = ctx->kma.param.j0.w[1];
bbd2dd
+            buf.w[2] = ctx->kma.param.j0.w[2];
bbd2dd
+            buf.w[3] = ctx->kma.param.cv.w + 1;
bbd2dd
+            s390x_km(buf.b, 16, ctx->kres, ctx->fc & 0x1f, &ctx->kma.param.k);
bbd2dd
+        }
bbd2dd
+
bbd2dd
+        n = ctx->mreslen;
bbd2dd
+        for (i = 0; i < rem; i++) {
bbd2dd
+            ctx->mres[n + i] = in[i];
bbd2dd
+            out[i] = in[i] ^ ctx->kres[n + i];
bbd2dd
+        }
bbd2dd
+
bbd2dd
+        ctx->mreslen += rem;
bbd2dd
+    }
bbd2dd
+    return 0;
bbd2dd
+}
bbd2dd
+
bbd2dd
+static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx,
bbd2dd
+                                const unsigned char *iv)
bbd2dd
+{
bbd2dd
+    ctx->kma.param.t.g[0] = 0;
bbd2dd
+    ctx->kma.param.t.g[1] = 0;
bbd2dd
+    ctx->kma.param.tpcl = 0;
bbd2dd
+    ctx->kma.param.taadl = 0;
bbd2dd
+    ctx->mreslen = 0;
bbd2dd
+    ctx->areslen = 0;
bbd2dd
+    ctx->kreslen = 0;
bbd2dd
+
bbd2dd
+    if (ctx->ivlen == 12) {
bbd2dd
+        memcpy(&ctx->kma.param.j0, iv, ctx->ivlen);
bbd2dd
+        ctx->kma.param.j0.w[3] = 1;
bbd2dd
+        ctx->kma.param.cv.w = 1;
bbd2dd
+    } else {
bbd2dd
+        /* ctx->iv has the right size and is already padded. */
bbd2dd
+        memcpy(ctx->iv, iv, ctx->ivlen);
bbd2dd
+        s390x_kma(ctx->iv, S390X_gcm_ivpadlen(ctx->ivlen), NULL, 0, NULL,
bbd2dd
+                  ctx->fc, &ctx->kma.param);
bbd2dd
+        ctx->fc |= S390X_KMA_HS;
bbd2dd
+
bbd2dd
+        ctx->kma.param.j0.g[0] = ctx->kma.param.t.g[0];
bbd2dd
+        ctx->kma.param.j0.g[1] = ctx->kma.param.t.g[1];
bbd2dd
+        ctx->kma.param.cv.w = ctx->kma.param.j0.w[3];
bbd2dd
+        ctx->kma.param.t.g[0] = 0;
bbd2dd
+        ctx->kma.param.t.g[1] = 0;
bbd2dd
+    }
bbd2dd
+}
bbd2dd
+
bbd2dd
+static int s390x_aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
bbd2dd
+{
bbd2dd
+    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
bbd2dd
+    S390X_AES_GCM_CTX *gctx_out;
bbd2dd
+    EVP_CIPHER_CTX *out;
bbd2dd
+    unsigned char *buf, *iv;
bbd2dd
+    int ivlen, enc, len;
bbd2dd
+
bbd2dd
+    switch (type) {
bbd2dd
+    case EVP_CTRL_INIT:
bbd2dd
+        ivlen = c->cipher->iv_len;;
bbd2dd
+        iv = c->iv;
bbd2dd
+        gctx->key_set = 0;
bbd2dd
+        gctx->iv_set = 0;
bbd2dd
+        gctx->ivlen = ivlen;
bbd2dd
+        gctx->iv = iv;
bbd2dd
+        gctx->taglen = -1;
bbd2dd
+        gctx->iv_gen = 0;
bbd2dd
+        gctx->tls_aad_len = -1;
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_GCM_SET_IVLEN:
bbd2dd
+        if (arg <= 0)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        if (arg != 12) {
bbd2dd
+            iv = c->iv;
bbd2dd
+            len = S390X_gcm_ivpadlen(arg);
bbd2dd
+
bbd2dd
+            /* Allocate memory for iv if needed. */
bbd2dd
+            if (gctx->ivlen == 12 || len > S390X_gcm_ivpadlen(gctx->ivlen)) {
bbd2dd
+                if (gctx->iv != iv)
bbd2dd
+                    OPENSSL_free(gctx->iv);
bbd2dd
+
bbd2dd
+                gctx->iv = OPENSSL_malloc(len);
bbd2dd
+                if (gctx->iv == NULL)
bbd2dd
+                    return 0;
bbd2dd
+            }
bbd2dd
+            /* Add padding. */
bbd2dd
+            memset(gctx->iv + arg, 0, len - arg - 8);
bbd2dd
+            *((unsigned long long *)(gctx->iv + len - 8)) = arg << 3;
bbd2dd
+        }
bbd2dd
+        gctx->ivlen = arg;
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_GCM_SET_TAG:
bbd2dd
+        buf = c->buf;
bbd2dd
+        enc = c->encrypt;
bbd2dd
+        if (arg <= 0 || arg > 16 || enc)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        memcpy(buf, ptr, arg);
bbd2dd
+        gctx->taglen = arg;
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_GCM_GET_TAG:
bbd2dd
+        enc = c->encrypt;
bbd2dd
+        if (arg <= 0 || arg > 16 || !enc || gctx->taglen < 0)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        memcpy(ptr, gctx->kma.param.t.b, arg);
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_GCM_SET_IV_FIXED:
bbd2dd
+        /* Special case: -1 length restores whole iv */
bbd2dd
+        if (arg == -1) {
bbd2dd
+            memcpy(gctx->iv, ptr, gctx->ivlen);
bbd2dd
+            gctx->iv_gen = 1;
bbd2dd
+            return 1;
bbd2dd
+        }
bbd2dd
+        /*
bbd2dd
+         * Fixed field must be at least 4 bytes and invocation field at least
bbd2dd
+         * 8.
bbd2dd
+         */
bbd2dd
+        if ((arg < 4) || (gctx->ivlen - arg) < 8)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        if (arg)
bbd2dd
+            memcpy(gctx->iv, ptr, arg);
bbd2dd
+
bbd2dd
+        enc = c->encrypt;
bbd2dd
+        if (enc && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        gctx->iv_gen = 1;
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_GCM_IV_GEN:
bbd2dd
+        if (gctx->iv_gen == 0 || gctx->key_set == 0)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        s390x_aes_gcm_setiv(gctx, gctx->iv);
bbd2dd
+
bbd2dd
+        if (arg <= 0 || arg > gctx->ivlen)
bbd2dd
+            arg = gctx->ivlen;
bbd2dd
+
bbd2dd
+        memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
bbd2dd
+        /*
bbd2dd
+         * Invocation field will be at least 8 bytes in size and so no need
bbd2dd
+         * to check wrap around or increment more than last 8 bytes.
bbd2dd
+         */
bbd2dd
+        (*(unsigned long long *)(gctx->iv + gctx->ivlen - 8))++;
bbd2dd
+        gctx->iv_set = 1;
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_GCM_SET_IV_INV:
bbd2dd
+        enc = c->encrypt;
bbd2dd
+        if (gctx->iv_gen == 0 || gctx->key_set == 0 || enc)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
bbd2dd
+        s390x_aes_gcm_setiv(gctx, gctx->iv);
bbd2dd
+        gctx->iv_set = 1;
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_AEAD_TLS1_AAD:
bbd2dd
+        /* Save the aad for later use. */
bbd2dd
+        if (arg != EVP_AEAD_TLS1_AAD_LEN)
bbd2dd
+            return 0;
bbd2dd
+
bbd2dd
+        buf = c->buf;
bbd2dd
+        memcpy(buf, ptr, arg);
bbd2dd
+        gctx->tls_aad_len = arg;
bbd2dd
+
bbd2dd
+        len = buf[arg - 2] << 8 | buf[arg - 1];
bbd2dd
+        /* Correct length for explicit iv. */
bbd2dd
+        if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
bbd2dd
+            return 0;
bbd2dd
+        len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
bbd2dd
+
bbd2dd
+        /* If decrypting correct for tag too. */
bbd2dd
+        enc = c->encrypt;
bbd2dd
+        if (!enc) {
bbd2dd
+            if (len < EVP_GCM_TLS_TAG_LEN)
bbd2dd
+                return 0;
bbd2dd
+            len -= EVP_GCM_TLS_TAG_LEN;
bbd2dd
+        }
bbd2dd
+        buf[arg - 2] = len >> 8;
bbd2dd
+        buf[arg - 1] = len & 0xff;
bbd2dd
+        /* Extra padding: tag appended to record. */
bbd2dd
+        return EVP_GCM_TLS_TAG_LEN;
bbd2dd
+
bbd2dd
+    case EVP_CTRL_COPY:
bbd2dd
+        out = ptr;
bbd2dd
+        gctx_out = EVP_C_DATA(S390X_AES_GCM_CTX, out);
bbd2dd
+        iv = c->iv;
bbd2dd
+
bbd2dd
+        if (gctx->iv == iv) {
bbd2dd
+            gctx_out->iv = out->iv;
bbd2dd
+        } else {
bbd2dd
+            len = S390X_gcm_ivpadlen(gctx->ivlen);
bbd2dd
+
bbd2dd
+            gctx_out->iv = OPENSSL_malloc(len);
bbd2dd
+            if (gctx_out->iv == NULL)
bbd2dd
+                return 0;
bbd2dd
+
bbd2dd
+            memcpy(gctx_out->iv, gctx->iv, len);
bbd2dd
+        }
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    default:
bbd2dd
+        return -1;
bbd2dd
+    }
bbd2dd
+}
bbd2dd
+
bbd2dd
+static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX *ctx,
bbd2dd
+                                  const unsigned char *key,
bbd2dd
+                                  const unsigned char *iv, int enc)
bbd2dd
+{
bbd2dd
+    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
bbd2dd
+    int keylen;
bbd2dd
+
bbd2dd
+    if (iv == NULL && key == NULL)
bbd2dd
+        return 1;
bbd2dd
+
bbd2dd
+    if (key != NULL) {
bbd2dd
+        keylen = EVP_CIPHER_CTX_key_length(ctx);
bbd2dd
+        memcpy(&gctx->kma.param.k, key, keylen);
bbd2dd
+
bbd2dd
+        /* Convert key size to function code. */
bbd2dd
+        gctx->fc = S390X_AES_128 + (((keylen << 3) - 128) >> 6);
bbd2dd
+        if (!enc)
bbd2dd
+            gctx->fc |= S390X_DECRYPT;
bbd2dd
+
bbd2dd
+        if (iv == NULL && gctx->iv_set)
bbd2dd
+            iv = gctx->iv;
bbd2dd
+
bbd2dd
+        if (iv != NULL) {
bbd2dd
+            s390x_aes_gcm_setiv(gctx, iv);
bbd2dd
+            gctx->iv_set = 1;
bbd2dd
+        }
bbd2dd
+        gctx->key_set = 1;
bbd2dd
+    } else {
bbd2dd
+        if (gctx->key_set)
bbd2dd
+            s390x_aes_gcm_setiv(gctx, iv);
bbd2dd
+        else
bbd2dd
+            memcpy(gctx->iv, iv, gctx->ivlen);
bbd2dd
+
bbd2dd
+        gctx->iv_set = 1;
bbd2dd
+        gctx->iv_gen = 0;
bbd2dd
+    }
bbd2dd
+    return 1;
bbd2dd
+}
bbd2dd
+
bbd2dd
+static int s390x_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                    const unsigned char *in, size_t len)
bbd2dd
+{
bbd2dd
+    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
bbd2dd
+    const unsigned char *buf = ctx->buf;
bbd2dd
+    const int enc = ctx->encrypt;
bbd2dd
+    int rv = -1;
bbd2dd
+
bbd2dd
+    if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
bbd2dd
+        return -1;
bbd2dd
+
bbd2dd
+    if (EVP_CIPHER_CTX_ctrl(ctx, enc ? EVP_CTRL_GCM_IV_GEN
bbd2dd
+                                     : EVP_CTRL_GCM_SET_IV_INV,
bbd2dd
+                            EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
bbd2dd
+        goto err;
bbd2dd
+
bbd2dd
+    in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
bbd2dd
+    out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
bbd2dd
+    len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
bbd2dd
+
bbd2dd
+    gctx->kma.param.taadl = gctx->tls_aad_len << 3;
bbd2dd
+    gctx->kma.param.tpcl = len << 3;
bbd2dd
+    s390x_kma(buf, gctx->tls_aad_len, in, len, out,
bbd2dd
+              gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
bbd2dd
+
bbd2dd
+    if (enc) {
bbd2dd
+        memcpy(out + len, gctx->kma.param.t.b, EVP_GCM_TLS_TAG_LEN);
bbd2dd
+        rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
bbd2dd
+    } else {
bbd2dd
+        if (CRYPTO_memcmp(gctx->kma.param.t.b, in + len,
bbd2dd
+                          EVP_GCM_TLS_TAG_LEN)) {
bbd2dd
+            OPENSSL_cleanse(out, len);
bbd2dd
+            goto err;
bbd2dd
+        }
bbd2dd
+        rv = len;
bbd2dd
+    }
bbd2dd
+err:
bbd2dd
+    gctx->iv_set = 0;
bbd2dd
+    gctx->tls_aad_len = -1;
bbd2dd
+    return rv;
bbd2dd
+}
bbd2dd
+
bbd2dd
+static int s390x_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len)
bbd2dd
+{
bbd2dd
+    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
bbd2dd
+    unsigned char *buf, tmp[16];
bbd2dd
+    int enc;
bbd2dd
+
bbd2dd
+    if (!gctx->key_set)
bbd2dd
+        return -1;
bbd2dd
+
bbd2dd
+    if (gctx->tls_aad_len >= 0)
bbd2dd
+        return s390x_aes_gcm_tls_cipher(ctx, out, in, len);
bbd2dd
+
bbd2dd
+    if (!gctx->iv_set)
bbd2dd
+        return -1;
bbd2dd
+
bbd2dd
+    if (in != NULL) {
bbd2dd
+        if (out == NULL) {
bbd2dd
+            if (s390x_aes_gcm_aad(gctx, in, len))
bbd2dd
+                return -1;
bbd2dd
+        } else {
bbd2dd
+            if (s390x_aes_gcm(gctx, in, out, len))
bbd2dd
+                return -1;
bbd2dd
+        }
bbd2dd
+        return len;
bbd2dd
+    } else {
bbd2dd
+        gctx->kma.param.taadl <<= 3;
bbd2dd
+        gctx->kma.param.tpcl <<= 3;
bbd2dd
+        s390x_kma(gctx->ares, gctx->areslen, gctx->mres, gctx->mreslen, tmp,
bbd2dd
+                  gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
bbd2dd
+        /* recall that we already did en-/decrypt gctx->mres
bbd2dd
+         * and returned it to caller... */
bbd2dd
+        OPENSSL_cleanse(tmp, gctx->mreslen);
bbd2dd
+        gctx->iv_set = 0;
bbd2dd
+
bbd2dd
+        enc = ctx->encrypt;
bbd2dd
+        if (enc) {
bbd2dd
+            gctx->taglen = 16;
bbd2dd
+        } else {
bbd2dd
+            if (gctx->taglen < 0)
bbd2dd
+                return -1;
bbd2dd
+
bbd2dd
+            buf = ctx->buf;
bbd2dd
+            if (CRYPTO_memcmp(buf, gctx->kma.param.t.b, gctx->taglen))
bbd2dd
+                return -1;
bbd2dd
+        }
bbd2dd
+        return 0;
bbd2dd
+    }
bbd2dd
+}
bbd2dd
+
bbd2dd
+static int s390x_aes_gcm_cleanup(EVP_CIPHER_CTX *c)
bbd2dd
+{
bbd2dd
+    S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
bbd2dd
+    const unsigned char *iv;
bbd2dd
+
bbd2dd
+    if (gctx == NULL)
bbd2dd
+        return 0;
bbd2dd
+
bbd2dd
+    iv = c->iv;
bbd2dd
+    if (iv != gctx->iv)
bbd2dd
+        OPENSSL_free(gctx->iv);
bbd2dd
+
bbd2dd
+    OPENSSL_cleanse(gctx, sizeof(*gctx));
bbd2dd
+    return 1;
bbd2dd
+}
bbd2dd
+
bbd2dd
+# define S390X_AES_XTS_CTX		EVP_AES_XTS_CTX
bbd2dd
+# define S390X_aes_128_xts_CAPABLE	1	/* checked by callee */
bbd2dd
+# define S390X_aes_256_xts_CAPABLE	1
bbd2dd
+
bbd2dd
+# define s390x_aes_xts_init_key aes_xts_init_key
bbd2dd
+static int s390x_aes_xts_init_key(EVP_CIPHER_CTX *ctx,
bbd2dd
+                                  const unsigned char *key,
bbd2dd
+                                  const unsigned char *iv, int enc);
bbd2dd
+# define s390x_aes_xts_cipher aes_xts_cipher
bbd2dd
+static int s390x_aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len);
bbd2dd
+# define s390x_aes_xts_ctrl aes_xts_ctrl
bbd2dd
+static int s390x_aes_xts_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
bbd2dd
+# define s390x_aes_xts_cleanup aes_xts_cleanup
bbd2dd
+
bbd2dd
+# define S390X_AES_CCM_CTX		EVP_AES_CCM_CTX
bbd2dd
+# define S390X_aes_128_ccm_CAPABLE	0
bbd2dd
+# define S390X_aes_192_ccm_CAPABLE	0
bbd2dd
+# define S390X_aes_256_ccm_CAPABLE	0
bbd2dd
+
bbd2dd
+# define s390x_aes_ccm_init_key aes_ccm_init_key
bbd2dd
+static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX *ctx,
bbd2dd
+                                  const unsigned char *key,
bbd2dd
+                                  const unsigned char *iv, int enc);
bbd2dd
+# define s390x_aes_ccm_cipher aes_ccm_cipher
bbd2dd
+static int s390x_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
bbd2dd
+                                const unsigned char *in, size_t len);
bbd2dd
+# define s390x_aes_ccm_ctrl aes_ccm_ctrl
bbd2dd
+static int s390x_aes_ccm_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
bbd2dd
+# define s390x_aes_ccm_cleanup aes_ccm_cleanup
bbd2dd
+
bbd2dd
+# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,	\
bbd2dd
+                              MODE,flags)				\
bbd2dd
+static const EVP_CIPHER s390x_aes_##keylen##_##mode = {			\
bbd2dd
+    nid##_##keylen##_##nmode,blocksize,					\
bbd2dd
+    keylen / 8,								\
bbd2dd
+    ivlen,								\
bbd2dd
+    flags | EVP_CIPH_##MODE##_MODE,					\
bbd2dd
+    s390x_aes_init_key,							\
bbd2dd
+    s390x_aes_##mode##_cipher,						\
bbd2dd
+    NULL,								\
bbd2dd
+    sizeof(EVP_AES_KEY),						\
bbd2dd
+    NULL,								\
bbd2dd
+    NULL,								\
bbd2dd
+    NULL,								\
bbd2dd
+    NULL								\
bbd2dd
+};									\
bbd2dd
+static const EVP_CIPHER aes_##keylen##_##mode = {			\
bbd2dd
+    nid##_##keylen##_##nmode,						\
bbd2dd
+    blocksize,								\
bbd2dd
+    keylen / 8,								\
bbd2dd
+    ivlen,								\
bbd2dd
+    flags | EVP_CIPH_##MODE##_MODE,					\
bbd2dd
+    aes_init_key,							\
bbd2dd
+    aes_##mode##_cipher,						\
bbd2dd
+    NULL,								\
bbd2dd
+    sizeof(EVP_AES_KEY),						\
bbd2dd
+    NULL,NULL,NULL,NULL							\
bbd2dd
+};									\
bbd2dd
+const EVP_CIPHER *EVP_aes_##keylen##_##mode(void)			\
bbd2dd
+{									\
bbd2dd
+    return S390X_aes_##keylen##_##mode##_CAPABLE ?			\
bbd2dd
+           &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode;	\
bbd2dd
+}
bbd2dd
+
bbd2dd
+# define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags)\
bbd2dd
+static const EVP_CIPHER s390x_aes_##keylen##_##mode = {			\
bbd2dd
+    nid##_##keylen##_##mode,						\
bbd2dd
+    blocksize,								\
bbd2dd
+    (EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8,	\
bbd2dd
+    ivlen,								\
bbd2dd
+    flags | EVP_CIPH_##MODE##_MODE,					\
bbd2dd
+    s390x_aes_##mode##_init_key,					\
bbd2dd
+    s390x_aes_##mode##_cipher,						\
bbd2dd
+    s390x_aes_##mode##_cleanup,						\
bbd2dd
+    sizeof(S390X_AES_##MODE##_CTX),					\
bbd2dd
+    NULL,								\
bbd2dd
+    NULL,								\
bbd2dd
+    s390x_aes_##mode##_ctrl,						\
bbd2dd
+    NULL								\
bbd2dd
+};									\
bbd2dd
+static const EVP_CIPHER aes_##keylen##_##mode = {			\
bbd2dd
+    nid##_##keylen##_##mode,blocksize,					\
bbd2dd
+    (EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8,	\
bbd2dd
+    ivlen,								\
bbd2dd
+    flags | EVP_CIPH_##MODE##_MODE,					\
bbd2dd
+    aes_##mode##_init_key,						\
bbd2dd
+    aes_##mode##_cipher,						\
bbd2dd
+    aes_##mode##_cleanup,						\
bbd2dd
+    sizeof(EVP_AES_##MODE##_CTX),					\
bbd2dd
+    NULL,								\
bbd2dd
+    NULL,								\
bbd2dd
+    aes_##mode##_ctrl,							\
bbd2dd
+    NULL								\
bbd2dd
+};									\
bbd2dd
+const EVP_CIPHER *EVP_aes_##keylen##_##mode(void)			\
bbd2dd
+{									\
bbd2dd
+    return S390X_aes_##keylen##_##mode##_CAPABLE ?			\
bbd2dd
+           &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode;	\
bbd2dd
+}
bbd2dd
+
bbd2dd
 # else
bbd2dd
 
bbd2dd
 #  define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
bbd2dd
diff -up openssl-1.0.2k/crypto/Makefile.s390x-update openssl-1.0.2k/crypto/Makefile
bbd2dd
--- openssl-1.0.2k/crypto/Makefile.s390x-update	2018-06-18 12:20:49.079040659 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/Makefile	2018-06-18 13:27:53.065204592 +0200
bbd2dd
@@ -77,6 +77,7 @@ alphacpuid.s:	alphacpuid.pl
bbd2dd
 	(preproc=$$$$.$@.S; trap "rm $$preproc" INT; \
bbd2dd
 	$(PERL) alphacpuid.pl > $$preproc && \
bbd2dd
 	$(CC) -E -P $$preproc > $@ && rm $$preproc)
bbd2dd
+s390xcpuid.S:	s390xcpuid.pl;	$(PERL) s390xcpuid.pl $(PERLASM_SCHEME) $@
bbd2dd
 
bbd2dd
 testapps:
bbd2dd
 	[ -z "$(THIS)" ] || (	if echo $(SDIRS) | fgrep ' des '; \
bbd2dd
diff -up openssl-1.0.2k/crypto/modes/asm/ghash-s390x.pl.s390x-update openssl-1.0.2k/crypto/modes/asm/ghash-s390x.pl
bbd2dd
--- openssl-1.0.2k/crypto/modes/asm/ghash-s390x.pl.s390x-update	2018-06-18 12:20:47.170993976 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/modes/asm/ghash-s390x.pl	2018-06-18 13:27:37.110817228 +0200
bbd2dd
@@ -73,6 +73,8 @@ $rem_4bit="%r14";
bbd2dd
 $sp="%r15";
bbd2dd
 
bbd2dd
 $code.=<<___;
bbd2dd
+#include "s390x_arch.h"
bbd2dd
+
bbd2dd
 .text
bbd2dd
 
bbd2dd
 .globl	gcm_gmult_4bit
bbd2dd
@@ -85,12 +87,13 @@ $code.=<<___ if(!$softonly && 0);	# hard
bbd2dd
 	tmhl	%r0,0x4000	# check for message-security-assist
bbd2dd
 	jz	.Lsoft_gmult
bbd2dd
 	lghi	%r0,0
bbd2dd
-	lg	%r1,24(%r1)	# load second word of kimd capabilities vector
bbd2dd
+	lg	%r1,S390X_KIMD+8(%r1)	# load second word of kimd capabilities
bbd2dd
+					#  vector
bbd2dd
 	tmhh	%r1,0x4000	# check for function 65
bbd2dd
 	jz	.Lsoft_gmult
bbd2dd
 	stg	%r0,16($sp)	# arrange 16 bytes of zero input
bbd2dd
 	stg	%r0,24($sp)
bbd2dd
-	lghi	%r0,65		# function 65
bbd2dd
+	lghi	%r0,S390X_GHASH	# function 65
bbd2dd
 	la	%r1,0($Xi)	# H lies right after Xi in gcm128_context
bbd2dd
 	la	$inp,16($sp)
bbd2dd
 	lghi	$len,16
bbd2dd
@@ -119,16 +122,11 @@ gcm_ghash_4bit:
bbd2dd
 ___
bbd2dd
 $code.=<<___ if(!$softonly);
bbd2dd
 	larl	%r1,OPENSSL_s390xcap_P
bbd2dd
-	lg	%r0,0(%r1)
bbd2dd
-	tmhl	%r0,0x4000	# check for message-security-assist
bbd2dd
-	jz	.Lsoft_ghash
bbd2dd
-	lghi	%r0,0
bbd2dd
-	la	%r1,16($sp)
bbd2dd
-	.long	0xb93e0004	# kimd %r0,%r4
bbd2dd
-	lg	%r1,24($sp)
bbd2dd
-	tmhh	%r1,0x4000	# check for function 65
bbd2dd
+	lg	%r0,S390X_KIMD+8(%r1)	# load second word of kimd capabilities
bbd2dd
+					#  vector
bbd2dd
+	tmhh	%r0,0x4000	# check for function 65
bbd2dd
 	jz	.Lsoft_ghash
bbd2dd
-	lghi	%r0,65		# function 65
bbd2dd
+	lghi	%r0,S390X_GHASH	# function 65
bbd2dd
 	la	%r1,0($Xi)	# H lies right after Xi in gcm128_context
bbd2dd
 	.long	0xb93e0004	# kimd %r0,$inp
bbd2dd
 	brc	1,.-4		# pay attention to "partial completion"
bbd2dd
diff -up openssl-1.0.2k/crypto/modes/Makefile.s390x-update openssl-1.0.2k/crypto/modes/Makefile
bbd2dd
--- openssl-1.0.2k/crypto/modes/Makefile.s390x-update	2018-06-18 12:20:47.020990305 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/modes/Makefile	2018-06-18 13:27:37.110817228 +0200
bbd2dd
@@ -71,6 +71,8 @@ ghash-%.S:	asm/ghash-%.pl;	$(PERL) $< $(
bbd2dd
 ghash-armv4.o:	ghash-armv4.S
bbd2dd
 ghashv8-armx.o:	ghashv8-armx.S
bbd2dd
 
bbd2dd
+ghash-s390x.o:	ghash-s390x.S
bbd2dd
+
bbd2dd
 files:
bbd2dd
 	$(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
bbd2dd
 
bbd2dd
diff -up openssl-1.0.2k/crypto/sha/asm/sha1-s390x.pl.s390x-update openssl-1.0.2k/crypto/sha/asm/sha1-s390x.pl
bbd2dd
--- openssl-1.0.2k/crypto/sha/asm/sha1-s390x.pl.s390x-update	2018-06-18 12:20:47.174994073 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/sha/asm/sha1-s390x.pl	2018-06-18 13:27:37.112817276 +0200
bbd2dd
@@ -152,6 +152,8 @@ ___
bbd2dd
 }
bbd2dd
 
bbd2dd
 $code.=<<___;
bbd2dd
+#include "s390x_arch.h"
bbd2dd
+
bbd2dd
 .text
bbd2dd
 .align	64
bbd2dd
 .type	Ktable,\@object
bbd2dd
@@ -164,10 +166,7 @@ sha1_block_data_order:
bbd2dd
 ___
bbd2dd
 $code.=<<___ if ($kimdfunc);
bbd2dd
 	larl	%r1,OPENSSL_s390xcap_P
bbd2dd
-	lg	%r0,0(%r1)
bbd2dd
-	tmhl	%r0,0x4000	# check for message-security assist
bbd2dd
-	jz	.Lsoftware
bbd2dd
-	lg	%r0,16(%r1)	# check kimd capabilities
bbd2dd
+	lg	%r0,S390X_KIMD(%r1)	# check kimd capabilities
bbd2dd
 	tmhh	%r0,`0x8000>>$kimdfunc`
bbd2dd
 	jz	.Lsoftware
bbd2dd
 	lghi	%r0,$kimdfunc
bbd2dd
@@ -234,7 +233,6 @@ $code.=<<___;
bbd2dd
 	br	%r14
bbd2dd
 .size	sha1_block_data_order,.-sha1_block_data_order
bbd2dd
 .string	"SHA1 block transform for s390x, CRYPTOGAMS by <appro\@openssl.org>"
bbd2dd
-.comm	OPENSSL_s390xcap_P,80,8
bbd2dd
 ___
bbd2dd
 
bbd2dd
 $code =~ s/\`([^\`]*)\`/eval $1/gem;
bbd2dd
diff -up openssl-1.0.2k/crypto/sha/asm/sha512-s390x.pl.s390x-update openssl-1.0.2k/crypto/sha/asm/sha512-s390x.pl
bbd2dd
--- openssl-1.0.2k/crypto/sha/asm/sha512-s390x.pl.s390x-update	2018-06-18 12:20:47.179994196 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/sha/asm/sha512-s390x.pl	2018-06-18 13:27:37.112817276 +0200
bbd2dd
@@ -163,6 +163,8 @@ ___
bbd2dd
 }
bbd2dd
 
bbd2dd
 $code.=<<___;
bbd2dd
+#include "s390x_arch.h"
bbd2dd
+
bbd2dd
 .text
bbd2dd
 .align	64
bbd2dd
 .type	$Table,\@object
bbd2dd
@@ -237,10 +239,7 @@ $Func:
bbd2dd
 ___
bbd2dd
 $code.=<<___ if ($kimdfunc);
bbd2dd
 	larl	%r1,OPENSSL_s390xcap_P
bbd2dd
-	lg	%r0,0(%r1)
bbd2dd
-	tmhl	%r0,0x4000	# check for message-security assist
bbd2dd
-	jz	.Lsoftware
bbd2dd
-	lg	%r0,16(%r1)	# check kimd capabilities
bbd2dd
+	lg	%r0,S390X_KIMD(%r1)	# check kimd capabilities
bbd2dd
 	tmhh	%r0,`0x8000>>$kimdfunc`
bbd2dd
 	jz	.Lsoftware
bbd2dd
 	lghi	%r0,$kimdfunc
bbd2dd
@@ -308,7 +307,6 @@ $code.=<<___;
bbd2dd
 	br	%r14
bbd2dd
 .size	$Func,.-$Func
bbd2dd
 .string	"SHA${label} block transform for s390x, CRYPTOGAMS by <appro\@openssl.org>"
bbd2dd
-.comm	OPENSSL_s390xcap_P,80,8
bbd2dd
 ___
bbd2dd
 
bbd2dd
 $code =~ s/\`([^\`]*)\`/eval $1/gem;
bbd2dd
diff -up openssl-1.0.2k/crypto/sha/Makefile.s390x-update openssl-1.0.2k/crypto/sha/Makefile
bbd2dd
--- openssl-1.0.2k/crypto/sha/Makefile.s390x-update	2018-06-18 12:20:49.482050519 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/sha/Makefile	2018-06-18 13:27:37.112817276 +0200
bbd2dd
@@ -100,6 +100,10 @@ sha1-armv8.o:		sha1-armv8.S
bbd2dd
 sha256-armv8.o:		sha256-armv8.S
bbd2dd
 sha512-armv8.o:		sha512-armv8.S
bbd2dd
 
bbd2dd
+sha1-s390x.o:		sha1-s390x.S
bbd2dd
+sha256-s390x.o:		sha256-s390x.S
bbd2dd
+sha512-s390x.o:		sha512-s390x.S
bbd2dd
+
bbd2dd
 files:
bbd2dd
 	$(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
bbd2dd
 
bbd2dd
diff -up openssl-1.0.2k/crypto/s390x_arch.h.s390x-update openssl-1.0.2k/crypto/s390x_arch.h
bbd2dd
--- openssl-1.0.2k/crypto/s390x_arch.h.s390x-update	2018-06-18 13:27:37.110817228 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/s390x_arch.h	2018-06-18 13:27:53.066204616 +0200
bbd2dd
@@ -0,0 +1,93 @@
bbd2dd
+/*
bbd2dd
+ * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
bbd2dd
+ *
bbd2dd
+ * Licensed under the OpenSSL license (the "License").  You may not use
bbd2dd
+ * this file except in compliance with the License.  You can obtain a copy
bbd2dd
+ * in the file LICENSE in the source distribution or at
bbd2dd
+ * https://www.openssl.org/source/license.html
bbd2dd
+ */
bbd2dd
+
bbd2dd
+#ifndef S390X_ARCH_H
bbd2dd
+# define S390X_ARCH_H
bbd2dd
+
bbd2dd
+# ifndef __ASSEMBLER__
bbd2dd
+
bbd2dd
+void s390x_km(const unsigned char *in, size_t len, unsigned char *out,
bbd2dd
+              unsigned int fc, void *param);
bbd2dd
+void s390x_kma(const unsigned char *aad, size_t alen, const unsigned char *in,
bbd2dd
+               size_t len, unsigned char *out, unsigned int fc, void *param);
bbd2dd
+
bbd2dd
+/*
bbd2dd
+ * The field elements of OPENSSL_s390xcap_P are the 64-bit words returned by
bbd2dd
+ * the STFLE instruction followed by the 64-bit word pairs returned by
bbd2dd
+ * instructions' QUERY functions. If STFLE returns fewer data or an instruction
bbd2dd
+ * is not supported, the corresponding field elements are zero.
bbd2dd
+ */
bbd2dd
+struct OPENSSL_s390xcap_st {
bbd2dd
+    unsigned long long stfle[4];
bbd2dd
+    unsigned long long kimd[2];
bbd2dd
+    unsigned long long klmd[2];
bbd2dd
+    unsigned long long km[2];
bbd2dd
+    unsigned long long kmc[2];
bbd2dd
+    unsigned long long kmac[2];
bbd2dd
+    unsigned long long kmctr[2];
bbd2dd
+    unsigned long long kmo[2];
bbd2dd
+    unsigned long long kmf[2];
bbd2dd
+    unsigned long long prno[2];
bbd2dd
+    unsigned long long kma[2];
bbd2dd
+};
bbd2dd
+
bbd2dd
+extern struct OPENSSL_s390xcap_st OPENSSL_s390xcap_P;
bbd2dd
+
bbd2dd
+/* convert facility bit number or function code to bit mask */
bbd2dd
+#  define S390X_CAPBIT(i)	(1ULL << (63 - (i) % 64))
bbd2dd
+
bbd2dd
+# endif
bbd2dd
+
bbd2dd
+/* OPENSSL_s390xcap_P offsets [bytes] */
bbd2dd
+# define S390X_STFLE		0x00
bbd2dd
+# define S390X_KIMD		0x20
bbd2dd
+# define S390X_KLMD		0x30
bbd2dd
+# define S390X_KM		0x40
bbd2dd
+# define S390X_KMC		0x50
bbd2dd
+# define S390X_KMAC		0x60
bbd2dd
+# define S390X_KMCTR		0x70
bbd2dd
+# define S390X_KMO		0x80
bbd2dd
+# define S390X_KMF		0x90
bbd2dd
+# define S390X_PRNO		0xa0
bbd2dd
+# define S390X_KMA		0xb0
bbd2dd
+
bbd2dd
+/* Facility Bit Numbers */
bbd2dd
+# define S390X_VX		129
bbd2dd
+# define S390X_VXD		134
bbd2dd
+# define S390X_VXE		135
bbd2dd
+
bbd2dd
+/* Function Codes */
bbd2dd
+
bbd2dd
+/* all instructions */
bbd2dd
+# define S390X_QUERY		0
bbd2dd
+
bbd2dd
+/* kimd/klmd */
bbd2dd
+# define S390X_SHA3_224		32
bbd2dd
+# define S390X_SHA3_256		33
bbd2dd
+# define S390X_SHA3_384		34
bbd2dd
+# define S390X_SHA3_512		35
bbd2dd
+# define S390X_SHAKE_128	36
bbd2dd
+# define S390X_SHAKE_256	37
bbd2dd
+# define S390X_GHASH		65
bbd2dd
+
bbd2dd
+/* km/kmc/kmac/kmctr/kmo/kmf/kma */
bbd2dd
+# define S390X_AES_128		18
bbd2dd
+# define S390X_AES_192		19
bbd2dd
+# define S390X_AES_256		20
bbd2dd
+
bbd2dd
+/* prno */
bbd2dd
+# define S390X_TRNG		114
bbd2dd
+
bbd2dd
+/* Register 0 Flags */
bbd2dd
+# define S390X_DECRYPT		0x80
bbd2dd
+# define S390X_KMA_LPC		0x100
bbd2dd
+# define S390X_KMA_LAAD		0x200
bbd2dd
+# define S390X_KMA_HS		0x400
bbd2dd
+
bbd2dd
+#endif
bbd2dd
diff -up openssl-1.0.2k/crypto/s390xcap.c.s390x-update openssl-1.0.2k/crypto/s390xcap.c
bbd2dd
--- openssl-1.0.2k/crypto/s390xcap.c.s390x-update	2017-01-26 14:22:03.000000000 +0100
bbd2dd
+++ openssl-1.0.2k/crypto/s390xcap.c	2018-06-18 13:27:37.111817252 +0200
bbd2dd
@@ -4,8 +4,7 @@
bbd2dd
 #include <setjmp.h>
bbd2dd
 #include <signal.h>
bbd2dd
 #include "cryptlib.h"
bbd2dd
-
bbd2dd
-extern unsigned long OPENSSL_s390xcap_P[];
bbd2dd
+#include "s390x_arch.h"
bbd2dd
 
bbd2dd
 static sigjmp_buf ill_jmp;
bbd2dd
 static void ill_handler(int sig)
bbd2dd
@@ -13,30 +12,48 @@ static void ill_handler(int sig)
bbd2dd
     siglongjmp(ill_jmp, sig);
bbd2dd
 }
bbd2dd
 
bbd2dd
-unsigned long OPENSSL_s390x_facilities(void);
bbd2dd
+void OPENSSL_s390x_facilities(void);
bbd2dd
+void OPENSSL_vx_probe(void);
bbd2dd
+
bbd2dd
+struct OPENSSL_s390xcap_st OPENSSL_s390xcap_P;
bbd2dd
 
bbd2dd
 void OPENSSL_cpuid_setup(void)
bbd2dd
 {
bbd2dd
     sigset_t oset;
bbd2dd
     struct sigaction ill_act, oact;
bbd2dd
 
bbd2dd
-    if (OPENSSL_s390xcap_P[0])
bbd2dd
+    if (OPENSSL_s390xcap_P.stfle[0])
bbd2dd
         return;
bbd2dd
 
bbd2dd
-    OPENSSL_s390xcap_P[0] = 1UL << (8 * sizeof(unsigned long) - 1);
bbd2dd
+    /* set a bit that will not be tested later */
bbd2dd
+    OPENSSL_s390xcap_P.stfle[0] |= S390X_CAPBIT(0);
bbd2dd
 
bbd2dd
     memset(&ill_act, 0, sizeof(ill_act));
bbd2dd
     ill_act.sa_handler = ill_handler;
bbd2dd
     sigfillset(&ill_act.sa_mask);
bbd2dd
     sigdelset(&ill_act.sa_mask, SIGILL);
bbd2dd
+    sigdelset(&ill_act.sa_mask, SIGFPE);
bbd2dd
     sigdelset(&ill_act.sa_mask, SIGTRAP);
bbd2dd
     sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
bbd2dd
     sigaction(SIGILL, &ill_act, &oact;;
bbd2dd
+    sigaction(SIGFPE, &ill_act, &oact;;
bbd2dd
 
bbd2dd
     /* protection against missing store-facility-list-extended */
bbd2dd
     if (sigsetjmp(ill_jmp, 1) == 0)
bbd2dd
         OPENSSL_s390x_facilities();
bbd2dd
 
bbd2dd
+    /* protection against disabled vector facility */
bbd2dd
+    if ((OPENSSL_s390xcap_P.stfle[2] & S390X_CAPBIT(S390X_VX))
bbd2dd
+        && (sigsetjmp(ill_jmp, 1) == 0)) {
bbd2dd
+        OPENSSL_vx_probe();
bbd2dd
+    } else {
bbd2dd
+        OPENSSL_s390xcap_P.stfle[2] &= ~(S390X_CAPBIT(S390X_VX)
bbd2dd
+                                         | S390X_CAPBIT(S390X_VXD)
bbd2dd
+                                         | S390X_CAPBIT(S390X_VXE));
bbd2dd
+    }
bbd2dd
+
bbd2dd
+    sigaction(SIGFPE, &oact, NULL);
bbd2dd
+
bbd2dd
     sigaction(SIGILL, &oact, NULL);
bbd2dd
     sigprocmask(SIG_SETMASK, &oset, NULL);
bbd2dd
 }
bbd2dd
diff -up openssl-1.0.2k/crypto/s390xcpuid.pl.s390x-update openssl-1.0.2k/crypto/s390xcpuid.pl
bbd2dd
--- openssl-1.0.2k/crypto/s390xcpuid.pl.s390x-update	2018-06-18 13:27:53.067204641 +0200
bbd2dd
+++ openssl-1.0.2k/crypto/s390xcpuid.pl	2018-06-18 13:27:53.067204641 +0200
bbd2dd
@@ -0,0 +1,259 @@
bbd2dd
+#! /usr/bin/env perl
bbd2dd
+
bbd2dd
+$flavour = shift;
bbd2dd
+
bbd2dd
+if ($flavour =~ /3[12]/) {
bbd2dd
+	$SIZE_T=4;
bbd2dd
+	$g="";
bbd2dd
+} else {
bbd2dd
+	$SIZE_T=8;
bbd2dd
+	$g="g";
bbd2dd
+}
bbd2dd
+
bbd2dd
+while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
bbd2dd
+open STDOUT,">$output";
bbd2dd
+
bbd2dd
+$ra="%r14";
bbd2dd
+$sp="%r15";
bbd2dd
+$stdframe=16*$SIZE_T+4*8;
bbd2dd
+
bbd2dd
+$code=<<___;
bbd2dd
+#include "s390x_arch.h"
bbd2dd
+
bbd2dd
+.text
bbd2dd
+
bbd2dd
+.globl	OPENSSL_s390x_facilities
bbd2dd
+.type	OPENSSL_s390x_facilities,\@function
bbd2dd
+.align	16
bbd2dd
+OPENSSL_s390x_facilities:
bbd2dd
+	lghi	%r0,0
bbd2dd
+	larl	%r4,OPENSSL_s390xcap_P
bbd2dd
+
bbd2dd
+	stg	%r0,S390X_STFLE+8(%r4)	# wipe capability vectors
bbd2dd
+	stg	%r0,S390X_STFLE+16(%r4)
bbd2dd
+	stg	%r0,S390X_STFLE+24(%r4)
bbd2dd
+	stg	%r0,S390X_KIMD(%r4)
bbd2dd
+	stg	%r0,S390X_KIMD+8(%r4)
bbd2dd
+	stg	%r0,S390X_KLMD(%r4)
bbd2dd
+	stg	%r0,S390X_KLMD+8(%r4)
bbd2dd
+	stg	%r0,S390X_KM(%r4)
bbd2dd
+	stg	%r0,S390X_KM+8(%r4)
bbd2dd
+	stg	%r0,S390X_KMC(%r4)
bbd2dd
+	stg	%r0,S390X_KMC+8(%r4)
bbd2dd
+	stg	%r0,S390X_KMAC(%r4)
bbd2dd
+	stg	%r0,S390X_KMAC+8(%r4)
bbd2dd
+	stg	%r0,S390X_KMCTR(%r4)
bbd2dd
+	stg	%r0,S390X_KMCTR+8(%r4)
bbd2dd
+	stg	%r0,S390X_KMO(%r4)
bbd2dd
+	stg	%r0,S390X_KMO+8(%r4)
bbd2dd
+	stg	%r0,S390X_KMF(%r4)
bbd2dd
+	stg	%r0,S390X_KMF+8(%r4)
bbd2dd
+	stg	%r0,S390X_PRNO(%r4)
bbd2dd
+	stg	%r0,S390X_PRNO+8(%r4)
bbd2dd
+	stg	%r0,S390X_KMA(%r4)
bbd2dd
+	stg	%r0,S390X_KMA+8(%r4)
bbd2dd
+
bbd2dd
+	.long	0xb2b04000		# stfle	0(%r4)
bbd2dd
+	brc	8,.Ldone
bbd2dd
+	lghi	%r0,1
bbd2dd
+	.long	0xb2b04000		# stfle 0(%r4)
bbd2dd
+	brc	8,.Ldone
bbd2dd
+	lghi	%r0,2
bbd2dd
+	.long	0xb2b04000		# stfle 0(%r4)
bbd2dd
+.Ldone:
bbd2dd
+	lmg	%r2,%r3,S390X_STFLE(%r4)
bbd2dd
+	tmhl	%r2,0x4000		# check for message-security-assist
bbd2dd
+ 	jz	.Lret
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query kimd capabilities
bbd2dd
+	la	%r1,S390X_KIMD(%r4)
bbd2dd
+	.long	0xb93e0002		# kimd %r0,%r2
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query klmd capabilities
bbd2dd
+	la	%r1,S390X_KLMD(%r4)
bbd2dd
+	.long	0xb93f0002		# klmd %r0,%r2
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query km capability vector
bbd2dd
+	la	%r1,S390X_KM(%r4)
bbd2dd
+	.long	0xb92e0042		# km %r4,%r2
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query kmc capability vector
bbd2dd
+	la	%r1,S390X_KMC(%r4)
bbd2dd
+	.long	0xb92f0042		# kmc %r4,%r2
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query kmac capability vector
bbd2dd
+	la	%r1,S390X_KMAC(%r4)
bbd2dd
+	.long	0xb91e0042		# kmac %r4,%r2
bbd2dd
+
bbd2dd
+	tmhh	%r3,0x0004		# check for message-security-assist-4
bbd2dd
+ 	jz	.Lret
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query kmctr capability vector
bbd2dd
+	la	%r1,S390X_KMCTR(%r4)
bbd2dd
+	.long	0xb92d2042		# kmctr %r4,%r2,%r2
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query kmo capability vector
bbd2dd
+	la	%r1,S390X_KMO(%r4)
bbd2dd
+	.long	0xb92b0042		# kmo %r4,%r2
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query kmf capability vector
bbd2dd
+	la	%r1,S390X_KMF(%r4)
bbd2dd
+	.long	0xb92a0042		# kmf %r4,%r2
bbd2dd
+
bbd2dd
+	tml	%r2,0x40		# check for message-security-assist-5
bbd2dd
+	jz	.Lret
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query prno capability vector
bbd2dd
+	la	%r1,S390X_PRNO(%r4)
bbd2dd
+	.long	0xb93c0042		# prno %r4,%r2
bbd2dd
+
bbd2dd
+	lg	%r2,S390X_STFLE+16(%r4)
bbd2dd
+	tmhl	%r2,0x2000		# check for message-security-assist-8
bbd2dd
+	jz	.Lret
bbd2dd
+
bbd2dd
+	lghi	%r0,S390X_QUERY		# query kma capability vector
bbd2dd
+	la	%r1,S390X_KMA(%r4)
bbd2dd
+	.long	0xb9294022		# kma %r2,%r4,%r2
bbd2dd
+
bbd2dd
+.Lret:
bbd2dd
+	br	$ra
bbd2dd
+.size	OPENSSL_s390x_facilities,.-OPENSSL_s390x_facilities
bbd2dd
+
bbd2dd
+.globl	OPENSSL_rdtsc
bbd2dd
+.type	OPENSSL_rdtsc,\@function
bbd2dd
+.align	16
bbd2dd
+OPENSSL_rdtsc:
bbd2dd
+	stck	16($sp)
bbd2dd
+	lg	%r2,16($sp)
bbd2dd
+	br	$ra
bbd2dd
+.size	OPENSSL_rdtsc,.-OPENSSL_rdtsc
bbd2dd
+
bbd2dd
+.globl	OPENSSL_atomic_add
bbd2dd
+.type	OPENSSL_atomic_add,\@function
bbd2dd
+.align	16
bbd2dd
+OPENSSL_atomic_add:
bbd2dd
+	l	%r1,0(%r2)
bbd2dd
+.Lspin:	lr	%r0,%r1
bbd2dd
+	ar	%r0,%r3
bbd2dd
+	cs	%r1,%r0,0(%r2)
bbd2dd
+	brc	4,.Lspin
bbd2dd
+	lgfr	%r2,%r0		# OpenSSL expects the new value
bbd2dd
+	br	$ra
bbd2dd
+.size	OPENSSL_atomic_add,.-OPENSSL_atomic_add
bbd2dd
+
bbd2dd
+.globl	OPENSSL_wipe_cpu
bbd2dd
+.type	OPENSSL_wipe_cpu,\@function
bbd2dd
+.align	16
bbd2dd
+OPENSSL_wipe_cpu:
bbd2dd
+	xgr	%r0,%r0
bbd2dd
+	xgr	%r1,%r1
bbd2dd
+	lgr	%r2,$sp
bbd2dd
+	xgr	%r3,%r3
bbd2dd
+	xgr	%r4,%r4
bbd2dd
+	lzdr	%f0
bbd2dd
+	lzdr	%f1
bbd2dd
+	lzdr	%f2
bbd2dd
+	lzdr	%f3
bbd2dd
+	lzdr	%f4
bbd2dd
+	lzdr	%f5
bbd2dd
+	lzdr	%f6
bbd2dd
+	lzdr	%f7
bbd2dd
+	br	$ra
bbd2dd
+.size	OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
bbd2dd
+
bbd2dd
+.globl	OPENSSL_cleanse
bbd2dd
+.type	OPENSSL_cleanse,\@function
bbd2dd
+.align	16
bbd2dd
+OPENSSL_cleanse:
bbd2dd
+#if !defined(__s390x__) && !defined(__s390x)
bbd2dd
+	llgfr	%r3,%r3
bbd2dd
+#endif
bbd2dd
+	lghi	%r4,15
bbd2dd
+	lghi	%r0,0
bbd2dd
+	clgr	%r3,%r4
bbd2dd
+	jh	.Lot
bbd2dd
+	clgr	%r3,%r0
bbd2dd
+	bcr	8,%r14
bbd2dd
+.Little:
bbd2dd
+	stc	%r0,0(%r2)
bbd2dd
+	la	%r2,1(%r2)
bbd2dd
+	brctg	%r3,.Little
bbd2dd
+	br	%r14
bbd2dd
+.align	4
bbd2dd
+.Lot:	tmll	%r2,7
bbd2dd
+	jz	.Laligned
bbd2dd
+	stc	%r0,0(%r2)
bbd2dd
+	la	%r2,1(%r2)
bbd2dd
+	brctg	%r3,.Lot
bbd2dd
+.Laligned:
bbd2dd
+	srlg	%r4,%r3,3
bbd2dd
+.Loop:	stg	%r0,0(%r2)
bbd2dd
+	la	%r2,8(%r2)
bbd2dd
+	brctg	%r4,.Loop
bbd2dd
+	lghi	%r4,7
bbd2dd
+	ngr	%r3,%r4
bbd2dd
+	jnz	.Little
bbd2dd
+	br	$ra
bbd2dd
+.size	OPENSSL_cleanse,.-OPENSSL_cleanse
bbd2dd
+
bbd2dd
+.globl	OPENSSL_vx_probe
bbd2dd
+.type	OPENSSL_vx_probe,\@function
bbd2dd
+.align	16
bbd2dd
+OPENSSL_vx_probe:
bbd2dd
+	.word	0xe700,0x0000,0x0044	# vzero %v0
bbd2dd
+	br	$ra
bbd2dd
+.size	OPENSSL_vx_probe,.-OPENSSL_vx_probe
bbd2dd
+___
bbd2dd
+
bbd2dd
+################
bbd2dd
+# void s390x_km(const unsigned char *in, size_t len, unsigned char *out,
bbd2dd
+#               unsigned int fc, void *param)
bbd2dd
+{
bbd2dd
+my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
bbd2dd
+$code.=<<___;
bbd2dd
+.globl	s390x_km
bbd2dd
+.type	s390x_km,\@function
bbd2dd
+.align	16
bbd2dd
+s390x_km:
bbd2dd
+	lr	%r0,$fc
bbd2dd
+	l${g}r	%r1,$param
bbd2dd
+
bbd2dd
+	.long	0xb92e0042	# km $out,$in
bbd2dd
+	brc	1,.-4		# pay attention to "partial completion"
bbd2dd
+
bbd2dd
+	br	$ra
bbd2dd
+.size	s390x_km,.-s390x_km
bbd2dd
+___
bbd2dd
+}
bbd2dd
+
bbd2dd
+################
bbd2dd
+# void s390x_kma(const unsigned char *aad, size_t alen,
bbd2dd
+#                const unsigned char *in, size_t len,
bbd2dd
+#                unsigned char *out, unsigned int fc, void *param)
bbd2dd
+{
bbd2dd
+my ($aad,$alen,$in,$len,$out) = map("%r$_",(2..6));
bbd2dd
+$code.=<<___;
bbd2dd
+.globl	s390x_kma
bbd2dd
+.type	s390x_kma,\@function
bbd2dd
+.align	16
bbd2dd
+s390x_kma:
bbd2dd
+	st${g}	$out,6*$SIZE_T($sp)
bbd2dd
+	lm${g}	%r0,%r1,$stdframe($sp)
bbd2dd
+
bbd2dd
+	.long	0xb9292064	# kma $out,$aad,$in
bbd2dd
+	brc	1,.-4		# pay attention to "partial completion"
bbd2dd
+
bbd2dd
+	l${g}	$out,6*$SIZE_T($sp)
bbd2dd
+	br	$ra
bbd2dd
+.size	s390x_kma,.-s390x_kma
bbd2dd
+___
bbd2dd
+}
bbd2dd
+
bbd2dd
+$code.=<<___;
bbd2dd
+.section	.init
bbd2dd
+	brasl	$ra,OPENSSL_cpuid_setup
bbd2dd
+___
bbd2dd
+
bbd2dd
+$code =~ s/\`([^\`]*)\`/eval $1/gem;
bbd2dd
+print $code;
bbd2dd
+close STDOUT;	# force flush