bsaes-armv7.pl: remove byte order dependency and minor optimization.
[openssl.git] / crypto / aes / aes_cbc.c
index 86b27b10d612ab887ae3d4bbda666533783b1c0a..227f75625dba917eda4823f6533ff6c41abd437b 100644 (file)
  *
  */
 
-#ifndef AES_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
-
 #include <openssl/aes.h>
-#include "aes_locl.h"
+#include <openssl/modes.h>
 
 void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
-                    const unsigned long length, const AES_KEY *key,
+                    size_t len, const AES_KEY *key,
                     unsigned char *ivec, const int enc) {
 
-       unsigned long n;
-       unsigned long len = length;
-       unsigned char tmp[AES_BLOCK_SIZE];
-
-       assert(in && out && key && ivec);
-       assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));
-
-       if (AES_ENCRYPT == enc) {
-               while (len >= AES_BLOCK_SIZE) {
-                       for(n=0; n < AES_BLOCK_SIZE; ++n)
-                               tmp[n] = in[n] ^ ivec[n];
-                       AES_encrypt(tmp, out, key);
-                       memcpy(ivec, out, AES_BLOCK_SIZE);
-                       len -= AES_BLOCK_SIZE;
-                       in += AES_BLOCK_SIZE;
-                       out += AES_BLOCK_SIZE;
-               }
-               if (len) {
-                       for(n=0; n < len; ++n)
-                               tmp[n] = in[n] ^ ivec[n];
-                       for(n=len; n < AES_BLOCK_SIZE; ++n)
-                               tmp[n] = ivec[n];
-                       AES_encrypt(tmp, tmp, key);
-                       memcpy(out, tmp, AES_BLOCK_SIZE);
-                       memcpy(ivec, tmp, AES_BLOCK_SIZE);
-               }                       
-       } else {
-               while (len >= AES_BLOCK_SIZE) {
-                       memcpy(tmp, in, AES_BLOCK_SIZE);
-                       AES_decrypt(in, out, key);
-                       for(n=0; n < AES_BLOCK_SIZE; ++n)
-                               out[n] ^= ivec[n];
-                       memcpy(ivec, tmp, AES_BLOCK_SIZE);
-                       len -= AES_BLOCK_SIZE;
-                       in += AES_BLOCK_SIZE;
-                       out += AES_BLOCK_SIZE;
-               }
-               if (len) {
-                       memcpy(tmp, in, AES_BLOCK_SIZE);
-                       AES_decrypt(tmp, tmp, key);
-                       for(n=0; n < len; ++n)
-                               out[n] ^= ivec[n];
-                       memcpy(ivec, tmp, AES_BLOCK_SIZE);
-               }                       
-       }
+       if (enc)
+               CRYPTO_cbc128_encrypt(in,out,len,key,ivec,(block128_f)AES_encrypt);
+       else
+               CRYPTO_cbc128_decrypt(in,out,len,key,ivec,(block128_f)AES_decrypt);
 }