Engage AES for UltraSPARC in sparcv9 targets.
[openssl.git] / crypto / aes / aes_cbc.c
index 8974dc3..d2ba6bc 100644 (file)
@@ -63,49 +63,69 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
                     const unsigned long length, const AES_KEY *key,
                     unsigned char *ivec, const int enc) {
 
-       int n;
+       unsigned long n;
        unsigned long len = length;
-       unsigned char tmp[16];
+       unsigned char tmp[AES_BLOCK_SIZE];
+       const unsigned char *iv = ivec;
 
        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 < 16; ++n)
-                               tmp[n] = in[n] ^ ivec[n];
-                       AES_encrypt(tmp, out, key);
-                       memcpy(ivec, out, 16);
-                       len -= 16;
-                       in += 16;
-                       out += 16;
+                       for(n=0; n < AES_BLOCK_SIZE; ++n)
+                               out[n] = in[n] ^ iv[n];
+                       AES_encrypt(out, out, key);
+                       iv = out;
+                       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];
+                               out[n] = in[n] ^ iv[n];
                        for(n=len; n < AES_BLOCK_SIZE; ++n)
-                               tmp[n] = ivec[n];
-                       AES_encrypt(tmp, tmp, key);
-                       memcpy(out, tmp, len);
-                       memcpy(ivec, tmp, 16);
-               }                       
+                               out[n] = iv[n];
+                       AES_encrypt(out, out, key);
+                       iv = out;
+               }
+               memcpy(ivec,iv,AES_BLOCK_SIZE);
+       } else if (in != out) {
+               while (len >= AES_BLOCK_SIZE) {
+                       AES_decrypt(in, out, key);
+                       for(n=0; n < AES_BLOCK_SIZE; ++n)
+                               out[n] ^= iv[n];
+                       iv = in;
+                       len -= AES_BLOCK_SIZE;
+                       in  += AES_BLOCK_SIZE;
+                       out += AES_BLOCK_SIZE;
+               }
+               if (len) {
+                       AES_decrypt(in,tmp,key);
+                       for(n=0; n < len; ++n)
+                               out[n] = tmp[n] ^ iv[n];
+                       iv = in;
+               }
+               memcpy(ivec,iv,AES_BLOCK_SIZE);
        } else {
                while (len >= AES_BLOCK_SIZE) {
-                       memcpy(tmp, in, 16);
+                       memcpy(tmp, in, AES_BLOCK_SIZE);
                        AES_decrypt(in, out, key);
-                       for(n=0; n < 16; ++n)
+                       for(n=0; n < AES_BLOCK_SIZE; ++n)
                                out[n] ^= ivec[n];
-                       memcpy(ivec, tmp, 16);
-                       len -= 16;
-                       in += 16;
-                       out += 16;
+                       memcpy(ivec, tmp, AES_BLOCK_SIZE);
+                       len -= AES_BLOCK_SIZE;
+                       in += AES_BLOCK_SIZE;
+                       out += AES_BLOCK_SIZE;
                }
                if (len) {
-                       memcpy(tmp, in, 16);
-                       AES_decrypt(tmp, tmp, key);
+                       memcpy(tmp, in, AES_BLOCK_SIZE);
+                       AES_decrypt(tmp, out, key);
                        for(n=0; n < len; ++n)
                                out[n] ^= ivec[n];
-                       memcpy(ivec, tmp, 16);
-               }                       
+                       for(n=len; n < AES_BLOCK_SIZE; ++n)
+                               out[n] = tmp[n];
+                       memcpy(ivec, tmp, AES_BLOCK_SIZE);
+               }
        }
 }