BIO_free should call method->destroy before free'ing member fields
[openssl.git] / crypto / ppccap.c
index c8d012ea5a7bff479af836a43d93294f00f6b27f..2c98daf5b4df785d88269b8519dd9cbcc2942e18 100644 (file)
@@ -79,6 +79,45 @@ void sha512_block_data_order(void *ctx, const void *inp, size_t len)
         sha512_block_ppc(ctx, inp, len);
 }
 
+void ChaCha20_ctr32_int(unsigned char *out, const unsigned char *inp,
+                        size_t len, const unsigned int key[8],
+                        const unsigned int counter[4]);
+void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp,
+                        size_t len, const unsigned int key[8],
+                        const unsigned int counter[4]);
+void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
+                    size_t len, const unsigned int key[8],
+                    const unsigned int counter[4])
+{
+    OPENSSL_ppccap_P & PPC_ALTIVEC
+        ? ChaCha20_ctr32_vmx(out, inp, len, key, counter)
+        : ChaCha20_ctr32_int(out, inp, len, key, counter);
+}
+
+void poly1305_init_int(void *ctx, const unsigned char key[16]);
+void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
+                         unsigned int padbit);
+void poly1305_emit(void *ctx, unsigned char mac[16],
+                       const unsigned int nonce[4]);
+void poly1305_init_fpu(void *ctx, const unsigned char key[16]);
+void poly1305_blocks_fpu(void *ctx, const unsigned char *inp, size_t len,
+                         unsigned int padbit);
+void poly1305_emit_fpu(void *ctx, unsigned char mac[16],
+                       const unsigned int nonce[4]);
+int poly1305_init(void *ctx, const unsigned char key[16], void *func[2])
+{
+    if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) {
+        poly1305_init_fpu(ctx,key);
+        func[0] = poly1305_blocks_fpu;
+        func[1] = poly1305_emit_fpu;
+    } else {
+        poly1305_init_int(ctx,key);
+        func[0] = poly1305_blocks;
+        func[1] = poly1305_emit;
+    }
+    return 1;
+}
+
 static sigjmp_buf ill_jmp;
 static void ill_handler(int sig)
 {
@@ -112,6 +151,7 @@ static unsigned long (*getauxval) (unsigned long) = NULL;
 
 #define HWCAP2                  26      /* AT_HWCAP2 */
 #define HWCAP_VEC_CRYPTO        (1U << 25)
+#define HWCAP_ARCH_3_00         (1U << 23)
 
 # if defined(__GNUC__) && __GNUC__>=2
 __attribute__ ((constructor))
@@ -169,6 +209,9 @@ void OPENSSL_cpuid_setup(void)
     if (__power_set(0xffffffffU<<16))           /* POWER8 and later */
         OPENSSL_ppccap_P |= PPC_CRYPTO207;
 
+    if (__power_set(0xffffffffU<<17))           /* POWER9 and later */
+        OPENSSL_ppccap_P |= PPC_MADD300;
+
     return;
 # endif
 #endif
@@ -197,6 +240,10 @@ void OPENSSL_cpuid_setup(void)
                 OPENSSL_ppccap_P |= PPC_CRYPTO207;
         }
 
+        if (hwcap & HWCAP_ARCH_3_00) {
+            OPENSSL_ppccap_P |= PPC_MADD300;
+        }
+
         return;
     }
 
@@ -246,6 +293,11 @@ void OPENSSL_cpuid_setup(void)
         }
     }
 
+    if (sigsetjmp(ill_jmp, 1) == 0) {
+        OPENSSL_madd300_probe();
+        OPENSSL_ppccap_P |= PPC_MADD300;
+    }
+
     sigaction(SIGILL, &ill_oact, NULL);
     sigprocmask(SIG_SETMASK, &oset, NULL);
 }