81356c
diff -up openssl-1.1.1k/crypto/evp/e_aes.c.s390x-aes openssl-1.1.1k/crypto/evp/e_aes.c
81356c
--- openssl-1.1.1k/crypto/evp/e_aes.c.s390x-aes	2021-07-16 11:03:14.362127435 +0200
81356c
+++ openssl-1.1.1k/crypto/evp/e_aes.c	2021-07-16 15:00:42.531477251 +0200
81356c
@@ -1168,9 +1168,9 @@ typedef struct {
81356c
 static int s390x_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
81356c
                               const unsigned char *iv, int enc);
81356c
 
81356c
-# define S390X_aes_128_cbc_CAPABLE	1	/* checked by callee */
81356c
-# define S390X_aes_192_cbc_CAPABLE	1
81356c
-# define S390X_aes_256_cbc_CAPABLE	1
81356c
+# define S390X_aes_128_cbc_CAPABLE	0	/* checked by callee */
81356c
+# define S390X_aes_192_cbc_CAPABLE	0
81356c
+# define S390X_aes_256_cbc_CAPABLE	0
81356c
 # define S390X_AES_CBC_CTX		EVP_AES_KEY
81356c
 
81356c
 # define s390x_aes_cbc_init_key aes_init_key
81356c
@@ -1190,11 +1190,10 @@ static int s390x_aes_ecb_init_key(EVP_CI
81356c
     S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx);
81356c
     const int keylen = EVP_CIPHER_CTX_key_length(ctx);
81356c
 
81356c
-    cctx->fc = S390X_AES_FC(keylen);
81356c
-    if (!enc)
81356c
-        cctx->fc |= S390X_DECRYPT;
81356c
+    cctx->fc = S390X_AES_FC(keylen) | (enc ? 0 : S390X_DECRYPT);
81356c
 
81356c
-    memcpy(cctx->km.param.k, key, keylen);
81356c
+    if (key != NULL)
81356c
+        memcpy(cctx->km.param.k, key, keylen);
81356c
     return 1;
81356c
 }
81356c
 
81356c
@@ -1222,14 +1221,17 @@ static int s390x_aes_ofb_init_key(EVP_CI
81356c
                                   const unsigned char *ivec, int enc)
81356c
 {
81356c
     S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
81356c
-    const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
81356c
+    const unsigned char *oiv = EVP_CIPHER_CTX_original_iv(ctx);
81356c
     const int keylen = EVP_CIPHER_CTX_key_length(ctx);
81356c
     const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
81356c
 
81356c
-    memcpy(cctx->kmo.param.cv, iv, ivlen);
81356c
-    memcpy(cctx->kmo.param.k, key, keylen);
81356c
     cctx->fc = S390X_AES_FC(keylen);
81356c
+
81356c
+    if (key != NULL)
81356c
+        memcpy(cctx->kmo.param.k, key, keylen);
81356c
+
81356c
     cctx->res = 0;
81356c
+	memcpy(cctx->kmo.param.cv, oiv, ivlen);
81356c
     return 1;
81356c
 }
81356c
 
81356c
@@ -1287,18 +1289,18 @@ static int s390x_aes_cfb_init_key(EVP_CI
81356c
                                   const unsigned char *ivec, int enc)
81356c
 {
81356c
     S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
81356c
-    const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
81356c
+    const unsigned char *oiv = EVP_CIPHER_CTX_original_iv(ctx);
81356c
     const int keylen = EVP_CIPHER_CTX_key_length(ctx);
81356c
     const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
81356c
 
81356c
-    cctx->fc = S390X_AES_FC(keylen);
81356c
-    cctx->fc |= 16 << 24;   /* 16 bytes cipher feedback */
81356c
-    if (!enc)
81356c
-        cctx->fc |= S390X_DECRYPT;
81356c
+    cctx->fc = S390X_AES_FC(keylen)| (enc ? 0 : S390X_DECRYPT)
81356c
+               | (16 << 24); /* 16 bytes cipher feedback */
81356c
+
81356c
+    if (key != NULL)
81356c
+         memcpy(cctx->kmf.param.k, key, keylen);
81356c
 
81356c
     cctx->res = 0;
81356c
-    memcpy(cctx->kmf.param.cv, iv, ivlen);
81356c
-    memcpy(cctx->kmf.param.k, key, keylen);
81356c
+	memcpy(cctx->kmf.param.cv, oiv, ivlen);
81356c
     return 1;
81356c
 }
81356c
 
81356c
@@ -1360,17 +1362,18 @@ static int s390x_aes_cfb8_init_key(EVP_C
81356c
                                    const unsigned char *ivec, int enc)
81356c
 {
81356c
     S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
81356c
-    const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
81356c
+    const unsigned char *oiv = EVP_CIPHER_CTX_original_iv(ctx);
81356c
     const int keylen = EVP_CIPHER_CTX_key_length(ctx);
81356c
     const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
81356c
 
81356c
-    cctx->fc = S390X_AES_FC(keylen);
81356c
-    cctx->fc |= 1 << 24;   /* 1 byte cipher feedback */
81356c
-    if (!enc)
81356c
-        cctx->fc |= S390X_DECRYPT;
81356c
+    cctx->fc = S390X_AES_FC(keylen) | (enc ? 0 : S390X_DECRYPT)
81356c
+		       | (1 << 24); /* 1 byte cipher feedback flag */
81356c
+
81356c
+    if (key != NULL)
81356c
+        memcpy(cctx->kmf.param.k, key, keylen);
81356c
 
81356c
-    memcpy(cctx->kmf.param.cv, iv, ivlen);
81356c
-    memcpy(cctx->kmf.param.k, key, keylen);
81356c
+    cctx->res = 0;
81356c
+	 memcpy(cctx->kmf.param.cv, oiv, ivlen);
81356c
     return 1;
81356c
 }
81356c
 
81356c
@@ -1393,9 +1396,9 @@ static int s390x_aes_cfb8_cipher(EVP_CIP
81356c
 static int s390x_aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
81356c
                                  const unsigned char *in, size_t len);
81356c
 
81356c
-# define S390X_aes_128_ctr_CAPABLE	1	/* checked by callee */
81356c
-# define S390X_aes_192_ctr_CAPABLE	1
81356c
-# define S390X_aes_256_ctr_CAPABLE	1
81356c
+# define S390X_aes_128_ctr_CAPABLE	0	/* checked by callee */
81356c
+# define S390X_aes_192_ctr_CAPABLE	0
81356c
+# define S390X_aes_256_ctr_CAPABLE	0
81356c
 # define S390X_AES_CTR_CTX		EVP_AES_KEY
81356c
 
81356c
 # define s390x_aes_ctr_init_key aes_init_key
81356c
@@ -1563,8 +1566,7 @@ static int s390x_aes_gcm(S390X_AES_GCM_C
81356c
 /*-
81356c
  * Initialize context structure. Code is big-endian.
81356c
  */
81356c
-static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx,
81356c
-                                const unsigned char *iv)
81356c
+static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx)
81356c
 {
81356c
     ctx->kma.param.t.g[0] = 0;
81356c
     ctx->kma.param.t.g[1] = 0;
81356c
@@ -1575,12 +1577,11 @@ static void s390x_aes_gcm_setiv(S390X_AE
81356c
     ctx->kreslen = 0;
81356c
 
81356c
     if (ctx->ivlen == 12) {
81356c
-        memcpy(&ctx->kma.param.j0, iv, ctx->ivlen);
81356c
+        memcpy(&ctx->kma.param.j0, ctx->iv, ctx->ivlen);
81356c
         ctx->kma.param.j0.w[3] = 1;
81356c
         ctx->kma.param.cv.w = 1;
81356c
     } else {
81356c
         /* ctx->iv has the right size and is already padded. */
81356c
-        memcpy(ctx->iv, iv, ctx->ivlen);
81356c
         s390x_kma(ctx->iv, S390X_gcm_ivpadlen(ctx->ivlen), NULL, 0, NULL,
81356c
                   ctx->fc, &ctx->kma.param);
81356c
         ctx->fc |= S390X_KMA_HS;
81356c
@@ -1694,7 +1695,7 @@ static int s390x_aes_gcm_ctrl(EVP_CIPHER
81356c
         if (gctx->iv_gen == 0 || gctx->key_set == 0)
81356c
             return 0;
81356c
 
81356c
-        s390x_aes_gcm_setiv(gctx, gctx->iv);
81356c
+        s390x_aes_gcm_setiv(gctx);
81356c
 
81356c
         if (arg <= 0 || arg > gctx->ivlen)
81356c
             arg = gctx->ivlen;
81356c
@@ -1714,7 +1715,7 @@ static int s390x_aes_gcm_ctrl(EVP_CIPHER
81356c
             return 0;
81356c
 
81356c
         memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
81356c
-        s390x_aes_gcm_setiv(gctx, gctx->iv);
81356c
+        s390x_aes_gcm_setiv(gctx);
81356c
         gctx->iv_set = 1;
81356c
         return 1;
81356c
 
81356c
@@ -1770,43 +1771,35 @@ static int s390x_aes_gcm_ctrl(EVP_CIPHER
81356c
 }
81356c
 
81356c
 /*-
81356c
- * Set key and/or iv. Returns 1 on success. Otherwise 0 is returned.
81356c
+ * Set key or iv or enc/dec. Returns 1 on success. Otherwise 0 is returned.
81356c
  */
81356c
 static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX *ctx,
81356c
                                   const unsigned char *key,
81356c
                                   const unsigned char *iv, int enc)
81356c
 {
81356c
     S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
81356c
-    int keylen;
81356c
+    const int keylen = EVP_CIPHER_CTX_key_length(ctx);
81356c
 
81356c
-    if (iv == NULL && key == NULL)
81356c
-        return 1;
81356c
+	 gctx->fc = S390X_AES_FC(keylen) | (enc ? 0 : S390X_DECRYPT);
81356c
 
81356c
     if (key != NULL) {
81356c
-        keylen = EVP_CIPHER_CTX_key_length(ctx);
81356c
+		 gctx->fc &= ~S390X_KMA_HS;
81356c
         memcpy(&gctx->kma.param.k, key, keylen);
81356c
-
81356c
-        gctx->fc = S390X_AES_FC(keylen);
81356c
-        if (!enc)
81356c
-            gctx->fc |= S390X_DECRYPT;
81356c
-
81356c
-        if (iv == NULL && gctx->iv_set)
81356c
-            iv = gctx->iv;
81356c
-
81356c
-        if (iv != NULL) {
81356c
-            s390x_aes_gcm_setiv(gctx, iv);
81356c
-            gctx->iv_set = 1;
81356c
-        }
81356c
         gctx->key_set = 1;
81356c
-    } else {
81356c
-        if (gctx->key_set)
81356c
-            s390x_aes_gcm_setiv(gctx, iv);
81356c
-        else
81356c
-            memcpy(gctx->iv, iv, gctx->ivlen);
81356c
-
81356c
-        gctx->iv_set = 1;
81356c
+    }
81356c
+    if (iv != NULL) {
81356c
+		memcpy(gctx->iv, iv, gctx->ivlen);
81356c
         gctx->iv_gen = 0;
81356c
+        gctx->iv_set = 1;
81356c
     }
81356c
+
81356c
+    if (gctx->key_set && gctx->iv_set)
81356c
+                 s390x_aes_gcm_setiv(gctx);
81356c
+
81356c
+    gctx->fc &= ~(S390X_KMA_LPC | S390X_KMA_LAAD);
81356c
+    gctx->areslen = 0;
81356c
+    gctx->mreslen = 0;
81356c
+    gctx->kreslen = 0;
81356c
     return 1;
81356c
 }
81356c
 
81356c
@@ -1895,7 +1888,6 @@ static int s390x_aes_gcm_cipher(EVP_CIPH
81356c
         /* recall that we already did en-/decrypt gctx->mres
81356c
          * and returned it to caller... */
81356c
         OPENSSL_cleanse(tmp, gctx->mreslen);
81356c
-        gctx->iv_set = 0;
81356c
 
81356c
         enc = EVP_CIPHER_CTX_encrypting(ctx);
81356c
         if (enc) {
81356c
@@ -1929,8 +1921,8 @@ static int s390x_aes_gcm_cleanup(EVP_CIP
81356c
 }
81356c
 
81356c
 # define S390X_AES_XTS_CTX		EVP_AES_XTS_CTX
81356c
-# define S390X_aes_128_xts_CAPABLE	1	/* checked by callee */
81356c
-# define S390X_aes_256_xts_CAPABLE	1
81356c
+# define S390X_aes_128_xts_CAPABLE	0	/* checked by callee */
81356c
+# define S390X_aes_256_xts_CAPABLE	0
81356c
 
81356c
 # define s390x_aes_xts_init_key aes_xts_init_key
81356c
 static int s390x_aes_xts_init_key(EVP_CIPHER_CTX *ctx,
81356c
@@ -2134,9 +2126,10 @@ static int s390x_aes_ccm_tls_cipher(EVP_
81356c
                                     const unsigned char *in, size_t len)
81356c
 {
81356c
     S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
81356c
-    unsigned char *ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
81356c
+    const unsigned char *ivec = EVP_CIPHER_CTX_iv(ctx);
81356c
     unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
81356c
     const int enc = EVP_CIPHER_CTX_encrypting(ctx);
81356c
+	 unsigned char iv[EVP_MAX_IV_LENGTH];
81356c
 
81356c
     if (out != in
81356c
             || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->aes.ccm.m))
81356c
@@ -2152,8 +2145,9 @@ static int s390x_aes_ccm_tls_cipher(EVP_
81356c
      * Get explicit iv (sequence number). We already have fixed iv
81356c
      * (server/client_write_iv) here.
81356c
      */
81356c
-    memcpy(ivec + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
81356c
-    s390x_aes_ccm_setiv(cctx, ivec, len);
81356c
+    memcpy(iv, ivec, sizeof(iv));
81356c
+    memcpy(iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
81356c
+    s390x_aes_ccm_setiv(cctx, iv, len);
81356c
 
81356c
     /* Process aad (sequence number|type|version|length) */
81356c
     s390x_aes_ccm_aad(cctx, buf, cctx->aes.ccm.tls_aad_len);
81356c
@@ -2180,42 +2174,34 @@ static int s390x_aes_ccm_tls_cipher(EVP_
81356c
 }
81356c
 
81356c
 /*-
81356c
- * Set key and flag field and/or iv. Returns 1 if successful. Otherwise 0 is
81356c
- * returned.
81356c
+ * Set key or iv or enc/dec. Returns 1 if successful.
81356c
+ * Otherwise 0 is returned.
81356c
  */
81356c
 static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX *ctx,
81356c
                                   const unsigned char *key,
81356c
                                   const unsigned char *iv, int enc)
81356c
 {
81356c
     S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
81356c
-    unsigned char *ivec;
81356c
-    int keylen;
81356c
+    const int keylen  = EVP_CIPHER_CTX_key_length(ctx);
81356c
+    unsigned char *ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
81356c
 
81356c
-    if (iv == NULL && key == NULL)
81356c
-        return 1;
81356c
+    cctx->aes.ccm.fc = S390X_AES_FC(keylen);
81356c
 
81356c
     if (key != NULL) {
81356c
-        keylen = EVP_CIPHER_CTX_key_length(ctx);
81356c
-        cctx->aes.ccm.fc = S390X_AES_FC(keylen);
81356c
         memcpy(cctx->aes.ccm.kmac_param.k, key, keylen);
81356c
-
81356c
-        /* Store encoded m and l. */
81356c
-        cctx->aes.ccm.nonce.b[0] = ((cctx->aes.ccm.l - 1) & 0x7)
81356c
-                                 | (((cctx->aes.ccm.m - 2) >> 1) & 0x7) << 3;
81356c
-        memset(cctx->aes.ccm.nonce.b + 1, 0,
81356c
-               sizeof(cctx->aes.ccm.nonce.b));
81356c
-        cctx->aes.ccm.blocks = 0;
81356c
-
81356c
         cctx->aes.ccm.key_set = 1;
81356c
     }
81356c
-
81356c
     if (iv != NULL) {
81356c
-        ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
81356c
         memcpy(ivec, iv, 15 - cctx->aes.ccm.l);
81356c
-
81356c
         cctx->aes.ccm.iv_set = 1;
81356c
     }
81356c
+    /* Store encoded m and l. */
81356c
+    cctx->aes.ccm.nonce.b[0] = ((cctx->aes.ccm.l - 1) & 0x7)
81356c
+			                 | (((cctx->aes.ccm.m - 2) >> 1) & 0x7) << 3;
81356c
+    memset(cctx->aes.ccm.nonce.b + 1, 0, sizeof(cctx->aes.ccm.nonce.b) - 1);
81356c
 
81356c
+    cctx->aes.ccm.blocks = 0;
81356c
+    cctx->aes.ccm.len_set = 0;
81356c
     return 1;
81356c
 }
81356c
 
81356c
@@ -2230,8 +2216,9 @@ static int s390x_aes_ccm_cipher(EVP_CIPH
81356c
 {
81356c
     S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
81356c
     const int enc = EVP_CIPHER_CTX_encrypting(ctx);
81356c
+    const unsigned char *ivec = EVP_CIPHER_CTX_iv(ctx);
81356c
+    unsigned char *buf;
81356c
     int rv;
81356c
-    unsigned char *buf, *ivec;
81356c
 
81356c
     if (!cctx->aes.ccm.key_set)
81356c
         return -1;
81356c
@@ -2253,7 +2240,6 @@ static int s390x_aes_ccm_cipher(EVP_CIPH
81356c
     if (out == NULL) {
81356c
         /* Update(): Pass message length. */
81356c
         if (in == NULL) {
81356c
-            ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
81356c
             s390x_aes_ccm_setiv(cctx, ivec, len);
81356c
 
81356c
             cctx->aes.ccm.len_set = 1;
81356c
@@ -2279,7 +2265,6 @@ static int s390x_aes_ccm_cipher(EVP_CIPH
81356c
          * In case message length was not previously set explicitly via
81356c
          * Update(), set it now.
81356c
          */
81356c
-        ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
81356c
         s390x_aes_ccm_setiv(cctx, ivec, len);
81356c
 
81356c
         cctx->aes.ccm.len_set = 1;
81356c
@@ -2304,9 +2289,6 @@ static int s390x_aes_ccm_cipher(EVP_CIPH
81356c
         if (rv == -1)
81356c
             OPENSSL_cleanse(out, len);
81356c
 
81356c
-        cctx->aes.ccm.iv_set = 0;
81356c
-        cctx->aes.ccm.tag_set = 0;
81356c
-        cctx->aes.ccm.len_set = 0;
81356c
         return rv;
81356c
     }
81356c
 }
81356c
@@ -2414,9 +2396,6 @@ static int s390x_aes_ccm_ctrl(EVP_CIPHER
81356c
             return 0;
81356c
 
81356c
         memcpy(ptr, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
81356c
-        cctx->aes.ccm.tag_set = 0;
81356c
-        cctx->aes.ccm.iv_set = 0;
81356c
-        cctx->aes.ccm.len_set = 0;
81356c
         return 1;
81356c
 
81356c
     case EVP_CTRL_COPY:
81356c
@@ -2453,7 +2432,7 @@ static const EVP_CIPHER s390x_aes_##keyl
81356c
     nid##_##keylen##_##nmode,blocksize,					\
81356c
     keylen / 8,								\
81356c
     ivlen,								\
81356c
-    flags | EVP_CIPH_##MODE##_MODE,					\
81356c
+    flags | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_##MODE##_MODE,		\
81356c
     s390x_aes_##mode##_init_key,					\
81356c
     s390x_aes_##mode##_cipher,						\
81356c
     NULL,								\
81356c
@@ -2490,7 +2469,7 @@ static const EVP_CIPHER s390x_aes_##keyl
81356c
     blocksize,								\
81356c
     (EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8,	\
81356c
     ivlen,								\
81356c
-    flags | EVP_CIPH_##MODE##_MODE,					\
81356c
+    flags | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_##MODE##_MODE,		\
81356c
     s390x_aes_##mode##_init_key,					\
81356c
     s390x_aes_##mode##_cipher,						\
81356c
     s390x_aes_##mode##_cleanup,						\