038bb32
diff -up openssl-1.0.2a/engines/e_padlock.c.padlock64 openssl-1.0.2a/engines/e_padlock.c
038bb32
--- openssl-1.0.2a/engines/e_padlock.c.padlock64	2015-03-19 14:19:00.000000000 +0100
038bb32
+++ openssl-1.0.2a/engines/e_padlock.c	2015-04-22 16:23:44.105617468 +0200
038bb32
@@ -101,7 +101,10 @@
038bb32
  */
038bb32
 #  undef COMPILE_HW_PADLOCK
038bb32
 #  if !defined(I386_ONLY) && !defined(OPENSSL_NO_INLINE_ASM)
038bb32
-#   if (defined(__GNUC__) && (defined(__i386__) || defined(__i386))) || \
038bb32
+#  if (defined(__GNUC__) && __GNUC__>=2 && \
038bb32
+       (defined(__i386__) || defined(__i386) || \
038bb32
+        defined(__x86_64__) || defined(__x86_64)) \
038bb32
+     ) || \
038bb32
      (defined(_MSC_VER) && defined(_M_IX86))
038bb32
 #    define COMPILE_HW_PADLOCK
038bb32
 #   endif
038bb32
@@ -140,7 +143,7 @@ void ENGINE_load_padlock(void)
038bb32
 #    endif
038bb32
 #   elif defined(__GNUC__)
038bb32
 #    ifndef alloca
038bb32
-#     define alloca(s) __builtin_alloca(s)
038bb32
+#     define alloca(s) __builtin_alloca((s))
038bb32
 #    endif
038bb32
 #   endif
038bb32
 
038bb32
@@ -303,6 +306,7 @@ static volatile struct padlock_cipher_da
038bb32
  * =======================================================
038bb32
  */
038bb32
 #   if defined(__GNUC__) && __GNUC__>=2
038bb32
+#    if defined(__i386__) || defined(__i386)
038bb32
 /*
038bb32
  * As for excessive "push %ebx"/"pop %ebx" found all over.
038bb32
  * When generating position-independent code GCC won't let
038bb32
@@ -379,22 +383,6 @@ static int padlock_available(void)
038bb32
     return padlock_use_ace + padlock_use_rng;
038bb32
 }
038bb32
 
038bb32
-#    ifndef OPENSSL_NO_AES
038bb32
-#     ifndef AES_ASM
038bb32
-/* Our own htonl()/ntohl() */
038bb32
-static inline void padlock_bswapl(AES_KEY *ks)
038bb32
-{
038bb32
-    size_t i = sizeof(ks->rd_key) / sizeof(ks->rd_key[0]);
038bb32
-    unsigned int *key = ks->rd_key;
038bb32
-
038bb32
-    while (i--) {
038bb32
-        asm volatile ("bswapl %0":"+r" (*key));
038bb32
-        key++;
038bb32
-    }
038bb32
-}
038bb32
-#     endif
038bb32
-#    endif
038bb32
-
038bb32
 /*
038bb32
  * Force key reload from memory to the CPU microcode. Loading EFLAGS from the
038bb32
  * stack clears EFLAGS[30] which does the trick.
038bb32
@@ -404,7 +392,7 @@ static inline void padlock_reload_key(vo
038bb32
     asm volatile ("pushfl; popfl");
038bb32
 }
038bb32
 
038bb32
-#    ifndef OPENSSL_NO_AES
038bb32
+#     ifndef OPENSSL_NO_AES
038bb32
 /*
038bb32
  * This is heuristic key context tracing. At first one
038bb32
  * believes that one should use atomic swap instructions,
038bb32
@@ -448,6 +436,101 @@ static inline void *name(size_t cnt,
038bb32
                 : "edx", "cc", "memory");       \
038bb32
         return iv;                              \
038bb32
 }
038bb32
+#     endif
038bb32
+
038bb32
+#    elif defined(__x86_64__) || defined(__x86_64)
038bb32
+
038bb32
+/* Load supported features of the CPU to see if
038bb32
+   the PadLock is available. */
038bb32
+static int padlock_available(void)
038bb32
+{
038bb32
+    char vendor_string[16];
038bb32
+    unsigned int eax, edx;
038bb32
+
038bb32
+    /* Are we running on the Centaur (VIA) CPU? */
038bb32
+    eax = 0x00000000;
038bb32
+    vendor_string[12] = 0;
038bb32
+    asm volatile ("cpuid\n"
038bb32
+                  "movl   %%ebx,(%1)\n"
038bb32
+                  "movl   %%edx,4(%1)\n"
038bb32
+                  "movl   %%ecx,8(%1)\n":"+a" (eax):"r"(vendor_string):"rbx",
038bb32
+                  "rcx", "rdx");
038bb32
+    if (strcmp(vendor_string, "CentaurHauls") != 0)
038bb32
+        return 0;
038bb32
+
038bb32
+    /* Check for Centaur Extended Feature Flags presence */
038bb32
+    eax = 0xC0000000;
038bb32
+    asm volatile ("cpuid":"+a" (eax)::"rbx", "rcx", "rdx");
038bb32
+    if (eax < 0xC0000001)
038bb32
+        return 0;
038bb32
+
038bb32
+    /* Read the Centaur Extended Feature Flags */
038bb32
+    eax = 0xC0000001;
038bb32
+    asm volatile ("cpuid":"+a" (eax), "=d"(edx)::"rbx", "rcx");
038bb32
+
038bb32
+    /* Fill up some flags */
038bb32
+    padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
038bb32
+    padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
038bb32
+
038bb32
+    return padlock_use_ace + padlock_use_rng;
038bb32
+}
038bb32
+
038bb32
+/* Force key reload from memory to the CPU microcode.
038bb32
+   Loading EFLAGS from the stack clears EFLAGS[30]
038bb32
+   which does the trick. */
038bb32
+static inline void padlock_reload_key(void)
038bb32
+{
038bb32
+    asm volatile ("pushfq; popfq");
038bb32
+}
038bb32
+
038bb32
+#     ifndef OPENSSL_NO_AES
038bb32
+/*
038bb32
+ * This is heuristic key context tracing. At first one
038bb32
+ * believes that one should use atomic swap instructions,
038bb32
+ * but it's not actually necessary. Point is that if
038bb32
+ * padlock_saved_context was changed by another thread
038bb32
+ * after we've read it and before we compare it with cdata,
038bb32
+ * our key *shall* be reloaded upon thread context switch
038bb32
+ * and we are therefore set in either case...
038bb32
+ */
038bb32
+static inline void padlock_verify_context(struct padlock_cipher_data *cdata)
038bb32
+{
038bb32
+    asm volatile ("pushfq\n"
038bb32
+                  "       btl     $30,(%%rsp)\n"
038bb32
+                  "       jnc     1f\n"
038bb32
+                  "       cmpq    %2,%1\n"
038bb32
+                  "       je      1f\n"
038bb32
+                  "       popfq\n"
038bb32
+                  "       subq    $8,%%rsp\n"
038bb32
+                  "1:     addq    $8,%%rsp\n"
038bb32
+                  "       movq    %2,%0":"+m" (padlock_saved_context)
038bb32
+                  :"r"(padlock_saved_context), "r"(cdata):"cc");
038bb32
+}
038bb32
+
038bb32
+/* Template for padlock_xcrypt_* modes */
038bb32
+/* BIG FAT WARNING:
038bb32
+ *      The offsets used with 'leal' instructions
038bb32
+ *      describe items of the 'padlock_cipher_data'
038bb32
+ *      structure.
038bb32
+ */
038bb32
+#      define PADLOCK_XCRYPT_ASM(name,rep_xcrypt)     \
038bb32
+static inline void *name(size_t cnt,            \
038bb32
+        struct padlock_cipher_data *cdata,      \
038bb32
+        void *out, const void *inp)             \
038bb32
+{       void *iv;                               \
038bb32
+        asm volatile ( "leaq    16(%0),%%rdx\n" \
038bb32
+                "       leaq    32(%0),%%rbx\n" \
038bb32
+                        rep_xcrypt "\n"         \
038bb32
+                : "=a"(iv), "=c"(cnt), "=D"(out), "=S"(inp) \
038bb32
+                : "0"(cdata), "1"(cnt), "2"(out), "3"(inp)  \
038bb32
+                : "rbx", "rdx", "cc", "memory");        \
038bb32
+        return iv;                              \
038bb32
+}
038bb32
+#     endif
038bb32
+
038bb32
+#    endif                      /* cpu */
038bb32
+
038bb32
+#    ifndef OPENSSL_NO_AES
038bb32
 
038bb32
 /* Generate all functions with appropriate opcodes */
038bb32
 /* rep xcryptecb */
038bb32
@@ -458,6 +541,20 @@ PADLOCK_XCRYPT_ASM(padlock_xcrypt_ecb, "
038bb32
     PADLOCK_XCRYPT_ASM(padlock_xcrypt_cfb, ".byte 0xf3,0x0f,0xa7,0xe0")
038bb32
 /* rep xcryptofb */
038bb32
     PADLOCK_XCRYPT_ASM(padlock_xcrypt_ofb, ".byte 0xf3,0x0f,0xa7,0xe8")
038bb32
+
038bb32
+#     ifndef AES_ASM
038bb32
+/* Our own htonl()/ntohl() */
038bb32
+static inline void padlock_bswapl(AES_KEY *ks)
038bb32
+{
038bb32
+    size_t i = sizeof(ks->rd_key) / sizeof(ks->rd_key[0]);
038bb32
+    unsigned int *key = ks->rd_key;
038bb32
+
038bb32
+    while (i--) {
038bb32
+        asm volatile ("bswapl %0":"+r" (*key));
038bb32
+        key++;
038bb32
+    }
038bb32
+}
038bb32
+#     endif
038bb32
 #    endif
038bb32
 /* The RNG call itself */
038bb32
 static inline unsigned int padlock_xstore(void *addr, unsigned int edx_in)
038bb32
@@ -485,8 +582,8 @@ static inline unsigned int padlock_xstor
038bb32
 static inline unsigned char *padlock_memcpy(void *dst, const void *src,
038bb32
                                             size_t n)
038bb32
 {
038bb32
-    long *d = dst;
038bb32
-    const long *s = src;
038bb32
+    size_t *d = dst;
038bb32
+    const size_t *s = src;
038bb32
 
038bb32
     n /= sizeof(*d);
038bb32
     do {