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