]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
s390x assembly pack: add KMO code path for aes-ofb
authorPatrick Steuer <patrick.steuer@de.ibm.com>
Wed, 28 Mar 2018 11:54:50 +0000 (12:54 +0100)
committerAndy Polyakov <appro@openssl.org>
Wed, 28 Mar 2018 21:30:56 +0000 (23:30 +0200)
Signed-off-by: Patrick Steuer <patrick.steuer@de.ibm.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Andy Polyakov <appro@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/5250)

crypto/evp/e_aes.c
crypto/s390x_arch.h
crypto/s390xcpuid.pl

index c595f558e059d8a53f810e0d7d7b4d98bfcfeff2..9309ec954f88322a364f06e95ba9b81ea86a1820 100644 (file)
@@ -971,6 +971,24 @@ typedef struct {
     unsigned int fc;
 } S390X_AES_ECB_CTX;
 
+typedef struct {
+    union {
+        double align;
+        /*-
+         * KMO-AES parameter block - begin
+         * (see z/Architecture Principles of Operation >= SA22-7832-08)
+         */
+        struct {
+            unsigned char cv[16];
+            unsigned char k[32];
+        } param;
+        /* KMO-AES parameter block - end */
+    } kmo;
+    unsigned int fc;
+
+    int res;
+} S390X_AES_OFB_CTX;
+
 typedef struct {
     union {
         double align;
@@ -1125,16 +1143,70 @@ static int s390x_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
     return 1;
 }
 
-# define S390X_aes_128_ofb_CAPABLE     0
-# define S390X_aes_192_ofb_CAPABLE     0
-# define S390X_aes_256_ofb_CAPABLE     0
-# define S390X_AES_OFB_CTX             EVP_AES_KEY
+# define S390X_aes_128_ofb_CAPABLE (S390X_aes_128_CAPABLE &&           \
+                                    (OPENSSL_s390xcap_P.kmo[0] &       \
+                                     S390X_CAPBIT(S390X_AES_128)))
+# define S390X_aes_192_ofb_CAPABLE (S390X_aes_192_CAPABLE &&           \
+                                    (OPENSSL_s390xcap_P.kmo[0] &       \
+                                     S390X_CAPBIT(S390X_AES_192)))
+# define S390X_aes_256_ofb_CAPABLE (S390X_aes_256_CAPABLE &&           \
+                                    (OPENSSL_s390xcap_P.kmo[0] &       \
+                                     S390X_CAPBIT(S390X_AES_256)))
+
+static int s390x_aes_ofb_init_key(EVP_CIPHER_CTX *ctx,
+                                  const unsigned char *key,
+                                  const unsigned char *ivec, int enc)
+{
+    S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
+    const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
+    const int keylen = EVP_CIPHER_CTX_key_length(ctx);
+    const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
 
-# define s390x_aes_ofb_init_key aes_init_key
+    memcpy(cctx->kmo.param.cv, iv, ivlen);
+    memcpy(cctx->kmo.param.k, key, keylen);
+    cctx->fc = S390X_AES_FC(keylen);
+    cctx->res = 0;
+    return 1;
+}
 
-# define s390x_aes_ofb_cipher aes_ofb_cipher
 static int s390x_aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
-                                const unsigned char *in, size_t len);
+                                const unsigned char *in, size_t len)
+{
+    S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
+    int n = cctx->res;
+    int rem;
+
+    while (n && len) {
+        *out = *in ^ cctx->kmo.param.cv[n];
+        n = (n + 1) & 0xf;
+        --len;
+        ++in;
+        ++out;
+    }
+
+    rem = len & 0xf;
+
+    len &= ~(size_t)0xf;
+    if (len) {
+        s390x_kmo(in, len, out, cctx->fc, &cctx->kmo.param);
+
+        out += len;
+        in += len;
+    }
+
+    if (rem) {
+        s390x_km(cctx->kmo.param.cv, 16, cctx->kmo.param.cv, cctx->fc,
+                 cctx->kmo.param.k);
+
+        while (rem--) {
+            out[n] = in[n] ^ cctx->kmo.param.cv[n];
+            ++n;
+        }
+    }
+
+    cctx->res = n;
+    return 1;
+}
 
 # define S390X_aes_128_cfb_CAPABLE     0
 # define S390X_aes_192_cfb_CAPABLE     0
index 5744318231ce107565c475aec683577da06916a8..236f936b574794f02131f662c034bda04e42c0f5 100644 (file)
@@ -16,6 +16,8 @@ void s390x_km(const unsigned char *in, size_t len, unsigned char *out,
               unsigned int fc, void *param);
 void s390x_kmac(const unsigned char *in, size_t len, unsigned int fc,
                 void *param);
+void s390x_kmo(const unsigned char *in, size_t len, unsigned char *out,
+               unsigned int fc, void *param);
 void s390x_kma(const unsigned char *aad, size_t alen, const unsigned char *in,
                size_t len, unsigned char *out, unsigned int fc, void *param);
 
index b0ed9e06238e899ee4fe509011fcacb0a0adc6ee..dfef2c129a0283a74d212ac0c90098de53f83a09 100755 (executable)
@@ -304,6 +304,27 @@ s390x_kmac:
 ___
 }
 
+################
+# void s390x_kmo(const unsigned char *in, size_t len, unsigned char *out,
+#                unsigned int fc, void *param)
+{
+my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
+$code.=<<___;
+.globl s390x_kmo
+.type  s390x_kmo,\@function
+.align 16
+s390x_kmo:
+       lr      %r0,$fc
+       l${g}r  %r1,$param
+
+       .long   0xb92b0042      # kmo $out,$in
+       brc     1,.-4           # pay attention to "partial completion"
+
+       br      $ra
+.size  s390x_kmo,.-s390x_kmo
+___
+}
+
 ################
 # void s390x_kma(const unsigned char *aad, size_t alen,
 #                const unsigned char *in, size_t len,