changes from 0.9.8
[openssl.git] / crypto / engine / eng_padlock.c
index ca311657870c4fb6474cd8bd18028be3d6c35efe..cc9f9dc41e4adaaf75a9b718c5cefcfe25b418e5 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
+#include <openssl/opensslconf.h>
 #include <openssl/crypto.h>
 #include <openssl/dso.h>
 #include <openssl/engine.h>
 #include <openssl/evp.h>
+#ifndef OPENSSL_NO_AES
 #include <openssl/aes.h>
+#endif
 #include <openssl/rand.h>
 
 #ifndef OPENSSL_NO_HW
    compiler choice is limited to GCC and Microsoft C. */
 #undef COMPILE_HW_PADLOCK
 #if !defined(I386_ONLY) && !defined(OPENSSL_NO_INLINE_ASM)
-# if defined(__i386__) || defined(__i386) || defined(_M_IX86)
+# if (defined(__GNUC__) && (defined(__i386__) || defined(__i386))) || \
+     (defined(_MSC_VER) && defined(_M_IX86))
 #  define COMPILE_HW_PADLOCK
 static ENGINE *ENGINE_padlock (void);
 # endif
@@ -118,9 +122,11 @@ void ENGINE_load_padlock (void)
 #ifdef COMPILE_HW_PADLOCK
 /* We do these includes here to avoid header problems on platforms that
    do not have the VIA padlock anyway... */
-#include <malloc.h>
 #ifdef _MSC_VER
+# include <malloc.h>
 # define alloca _alloca
+#else
+# include <stdlib.h>
 #endif
 
 /* Function for ENGINE detection and control */
@@ -131,7 +137,9 @@ static int padlock_init(ENGINE *e);
 static RAND_METHOD padlock_rand;
 
 /* Cipher Stuff */
+#ifndef OPENSSL_NO_AES
 static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid);
+#endif
 
 /* Engine names */
 static const char *padlock_id = "padlock";
@@ -140,7 +148,9 @@ static char padlock_name[100];
 /* Available features */
 static int padlock_use_ace = 0;        /* Advanced Cryptography Engine */
 static int padlock_use_rng = 0;        /* Random Number Generator */
+#ifndef OPENSSL_NO_AES
 static int padlock_aes_align_required = 1;
+#endif
 
 /* ===== Engine "management" functions ===== */
 
@@ -166,8 +176,9 @@ padlock_bind_helper(ENGINE *e)
            !ENGINE_set_name(e, padlock_name) ||
 
            !ENGINE_set_init_function(e, padlock_init) ||
-
+#ifndef OPENSSL_NO_AES
            (padlock_use_ace && !ENGINE_set_ciphers (e, padlock_ciphers)) ||
+#endif
            (padlock_use_rng && !ENGINE_set_RAND (e, &padlock_rand))) {
                return 0;
        }
@@ -225,6 +236,7 @@ IMPLEMENT_DYNAMIC_BIND_FN (padlock_bind_fn);
 
 /* ===== Here comes the "real" engine ===== */
 
+#ifndef OPENSSL_NO_AES
 /* Some AES-related constants */
 #define AES_BLOCK_SIZE         16
 #define AES_KEY_SIZE_128       16
@@ -261,6 +273,7 @@ struct padlock_cipher_data
  * so we accept the penatly...
  */
 static volatile struct padlock_cipher_data *padlock_saved_context;
+#endif
 
 /*
  * =======================================================
@@ -352,18 +365,20 @@ padlock_available(void)
        return padlock_use_ace + padlock_use_rng;
 }
 
+#ifndef OPENSSL_NO_AES
 /* Our own htonl()/ntohl() */
 static inline void
 padlock_bswapl(AES_KEY *ks)
 {
        size_t i = sizeof(ks->rd_key)/sizeof(ks->rd_key[0]);
-       unsigned long *key = ks->rd_key;
+       unsigned int *key = ks->rd_key;
 
        while (i--) {
                asm volatile ("bswapl %0" : "+r"(*key));
                key++;
        }
 }
+#endif
 
 /* Force key reload from memory to the CPU microcode.
    Loading EFLAGS from the stack clears EFLAGS[30] 
@@ -374,6 +389,7 @@ padlock_reload_key(void)
        asm volatile ("pushfl; popfl");
 }
 
+#ifndef OPENSSL_NO_AES
 /*
  * This is heuristic key context tracing. At first one
  * believes that one should use atomic swap instructions,
@@ -388,14 +404,14 @@ padlock_verify_context(struct padlock_cipher_data *cdata)
 {
        asm volatile (
        "pushfl\n"
-"      bt      $30,(%%esp)\n"
+"      btl     $30,(%%esp)\n"
 "      jnc     1f\n"
-"      cmp     %2,%1\n"
+"      cmpl    %2,%1\n"
 "      je      1f\n"
-"      mov     %2,%0\n"
 "      popfl\n"
-"      sub     $4,%%esp\n"
-"1:    add     $4,%%esp"
+"      subl    $4,%%esp\n"
+"1:    addl    $4,%%esp\n"
+"      movl    %2,%0"
        :"+m"(padlock_saved_context)
        : "r"(padlock_saved_context), "r"(cdata) : "cc");
 }
@@ -427,6 +443,7 @@ PADLOCK_XCRYPT_ASM(padlock_xcrypt_ecb, ".byte 0xf3,0x0f,0xa7,0xc8") /* rep xcryp
 PADLOCK_XCRYPT_ASM(padlock_xcrypt_cbc, ".byte 0xf3,0x0f,0xa7,0xd0")    /* rep xcryptcbc */
 PADLOCK_XCRYPT_ASM(padlock_xcrypt_cfb, ".byte 0xf3,0x0f,0xa7,0xe0")    /* rep xcryptcfb */
 PADLOCK_XCRYPT_ASM(padlock_xcrypt_ofb, ".byte 0xf3,0x0f,0xa7,0xe8")    /* rep xcryptofb */
+#endif
 
 /* The RNG call itself */
 static inline unsigned int
@@ -518,10 +535,10 @@ padlock_verify_context(void *cdata)
                jnc     skip
                cmp     ecx,padlock_saved_context
                je      skip
-               mov     padlock_saved_context,ecx
                popfd
                sub     esp,4
        skip:   add     esp,4
+               mov     padlock_saved_context,ecx
                }
 }
 
@@ -597,6 +614,7 @@ padlock_bswapl(void *key)
 #endif
 
 /* ===== AES encryption/decryption ===== */
+#ifndef OPENSSL_NO_AES
 
 #if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
 #define NID_aes_128_cfb        NID_aes_128_cfb128
@@ -788,10 +806,10 @@ padlock_aes_init_key (EVP_CIPHER_CTX *ctx, const unsigned char *key,
                                AES_set_encrypt_key(key, key_len, &cdata->ks);
                        else
                                AES_set_decrypt_key(key, key_len, &cdata->ks);
-
-                       /* OpenSSL internal functions use byte-swapped extended key. */
+#ifndef AES_ASM
+                       /* OpenSSL C functions use byte-swapped extended key. */
                        padlock_bswapl(&cdata->ks);
-
+#endif
                        cdata->cword.b.keygen = 1;
                        break;
 
@@ -1027,6 +1045,8 @@ padlock_aes_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
        return 1;
 }
 
+#endif /* OPENSSL_NO_AES */
+
 /* ===== Random Number Generator ===== */
 /*
  * This code is not engaged. The reason is that it does not comply