VERY EXPERIMENTAL HMAC redirection example in OpenSSL ENGINE. Untested at this
[openssl.git] / crypto / engine / eng_aesni.c
index 70b2838b4ea13d81191e59c962c13e3b09aa5151..e9f277390aee0f0e66fe5394ed0ca9b599aadd13 100644 (file)
@@ -115,7 +115,7 @@ void ENGINE_load_aesni (void)
 typedef unsigned int u32;
 typedef unsigned char u8;
 
-#if defined(__GNUC__) && __GNUC__>=2
+#if defined(__GNUC__) && __GNUC__>=2 && !defined(PEDANTIC)
 #  define BSWAP4(x) ({ u32 ret=(x);                    \
                        asm volatile ("bswapl %0"       \
                        : "+r"(ret));   ret;            })
@@ -164,7 +164,7 @@ void aesni_cbc_encrypt(const unsigned char *in,
 void aesni_ctr32_encrypt_blocks(const unsigned char *in,
                           unsigned char *out,
                           size_t blocks,
-                          const AES_KEY *key,
+                          const void *key,
                           const unsigned char *ivec);
 
 /* Function for ENGINE detection and control */
@@ -384,6 +384,7 @@ DECLARE_AES_EVP(256,cbc,CBC);
 DECLARE_AES_EVP(256,cfb,CFB);
 DECLARE_AES_EVP(256,ofb,OFB);
 
+#if notused
 static void ctr96_inc(unsigned char *counter) {
        u32 n=12;
        u8  c;
@@ -396,60 +397,16 @@ static void ctr96_inc(unsigned char *counter) {
                if (c) return;
        } while (n);
 }
+#endif
 
 static int aesni_counter(EVP_CIPHER_CTX *ctx, unsigned char *out,
                const unsigned char *in, size_t len)
 {
        AES_KEY *key = AESNI_ALIGN(ctx->cipher_data);
-       u32 n, ctr32;
-       n = ctx->num;
-
-       while (n && len) {
-               *(out++) = *(in++) ^ ctx->buf[n];
-               --len;
-               n = (n+1) % 16;
-       }
-
-       ctr32 = GETU32(ctx->iv+12);
-       while (len>=16) {
-               size_t blocks = len/16;
-               /*
-                * 1<<24 is just a not-so-small yet not-so-large number...
-                */
-               if (blocks > (1U<<24)) blocks = (1U<<24);
-               /*
-                * As aesni_ctr32 operates on 32-bit counter, caller
-                * has to handle overflow. 'if' below detects the
-                * overflow, which is then handled by limiting the
-                * amount of blocks to the exact overflow point...
-                */
-               ctr32 += (u32)blocks;
-               if (ctr32 < blocks) {
-                       blocks -= ctr32;
-                       ctr32   = 0;
-               }
-               aesni_ctr32_encrypt_blocks(in,out,blocks,key,ctx->iv);
-               /* aesni_ctr32 does not update ctx->iv, caller does: */
-               PUTU32(ctx->iv+12,ctr32);
-               /* ... overflow was detected, propogate carry. */
-               if (ctr32 == 0) ctr96_inc(ctx->iv);
-               blocks *= 16;
-               len -= blocks;
-               out += blocks;
-               in  += blocks;
-       }
-       if (len) {
-               aesni_encrypt(ctx->iv,ctx->buf,key);
-               ++ctr32;
-               PUTU32(ctx->iv+12,ctr32);
-               if (ctr32 == 0) ctr96_inc(ctx->iv);
-               while (len--) {
-                       out[n] = in[n] ^ ctx->buf[n];
-                       ++n;
-               }
-       }
-       ctx->num = n;
 
+       CRYPTO_ctr128_encrypt_ctr32(in,out,len,key,
+                               ctx->iv,ctx->buf,(unsigned int *)&ctx->num,
+                               aesni_ctr32_encrypt_blocks);
        return 1;
 }