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

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