summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/aes/aes_cbc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/aes/aes_cbc.c')
-rw-r--r--src/lib/libcrypto/aes/aes_cbc.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/lib/libcrypto/aes/aes_cbc.c b/src/lib/libcrypto/aes/aes_cbc.c
index 1222a21002..d2ba6bcdb4 100644
--- a/src/lib/libcrypto/aes/aes_cbc.c
+++ b/src/lib/libcrypto/aes/aes_cbc.c
@@ -66,6 +66,7 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
66 unsigned long n; 66 unsigned long n;
67 unsigned long len = length; 67 unsigned long len = length;
68 unsigned char tmp[AES_BLOCK_SIZE]; 68 unsigned char tmp[AES_BLOCK_SIZE];
69 const unsigned char *iv = ivec;
69 70
70 assert(in && out && key && ivec); 71 assert(in && out && key && ivec);
71 assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc)); 72 assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));
@@ -73,22 +74,39 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
73 if (AES_ENCRYPT == enc) { 74 if (AES_ENCRYPT == enc) {
74 while (len >= AES_BLOCK_SIZE) { 75 while (len >= AES_BLOCK_SIZE) {
75 for(n=0; n < AES_BLOCK_SIZE; ++n) 76 for(n=0; n < AES_BLOCK_SIZE; ++n)
76 tmp[n] = in[n] ^ ivec[n]; 77 out[n] = in[n] ^ iv[n];
77 AES_encrypt(tmp, out, key); 78 AES_encrypt(out, out, key);
78 memcpy(ivec, out, AES_BLOCK_SIZE); 79 iv = out;
79 len -= AES_BLOCK_SIZE; 80 len -= AES_BLOCK_SIZE;
80 in += AES_BLOCK_SIZE; 81 in += AES_BLOCK_SIZE;
81 out += AES_BLOCK_SIZE; 82 out += AES_BLOCK_SIZE;
82 } 83 }
83 if (len) { 84 if (len) {
84 for(n=0; n < len; ++n) 85 for(n=0; n < len; ++n)
85 tmp[n] = in[n] ^ ivec[n]; 86 out[n] = in[n] ^ iv[n];
86 for(n=len; n < AES_BLOCK_SIZE; ++n) 87 for(n=len; n < AES_BLOCK_SIZE; ++n)
87 tmp[n] = ivec[n]; 88 out[n] = iv[n];
88 AES_encrypt(tmp, tmp, key); 89 AES_encrypt(out, out, key);
89 memcpy(out, tmp, AES_BLOCK_SIZE); 90 iv = out;
90 memcpy(ivec, tmp, AES_BLOCK_SIZE); 91 }
91 } 92 memcpy(ivec,iv,AES_BLOCK_SIZE);
93 } else if (in != out) {
94 while (len >= AES_BLOCK_SIZE) {
95 AES_decrypt(in, out, key);
96 for(n=0; n < AES_BLOCK_SIZE; ++n)
97 out[n] ^= iv[n];
98 iv = in;
99 len -= AES_BLOCK_SIZE;
100 in += AES_BLOCK_SIZE;
101 out += AES_BLOCK_SIZE;
102 }
103 if (len) {
104 AES_decrypt(in,tmp,key);
105 for(n=0; n < len; ++n)
106 out[n] = tmp[n] ^ iv[n];
107 iv = in;
108 }
109 memcpy(ivec,iv,AES_BLOCK_SIZE);
92 } else { 110 } else {
93 while (len >= AES_BLOCK_SIZE) { 111 while (len >= AES_BLOCK_SIZE) {
94 memcpy(tmp, in, AES_BLOCK_SIZE); 112 memcpy(tmp, in, AES_BLOCK_SIZE);
@@ -102,10 +120,12 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
102 } 120 }
103 if (len) { 121 if (len) {
104 memcpy(tmp, in, AES_BLOCK_SIZE); 122 memcpy(tmp, in, AES_BLOCK_SIZE);
105 AES_decrypt(tmp, tmp, key); 123 AES_decrypt(tmp, out, key);
106 for(n=0; n < len; ++n) 124 for(n=0; n < len; ++n)
107 out[n] = tmp[n] ^ ivec[n]; 125 out[n] ^= ivec[n];
126 for(n=len; n < AES_BLOCK_SIZE; ++n)
127 out[n] = tmp[n];
108 memcpy(ivec, tmp, AES_BLOCK_SIZE); 128 memcpy(ivec, tmp, AES_BLOCK_SIZE);
109 } 129 }
110 } 130 }
111} 131}