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, 12 insertions, 32 deletions
diff --git a/src/lib/libcrypto/aes/aes_cbc.c b/src/lib/libcrypto/aes/aes_cbc.c
index d2ba6bcdb4..1222a21002 100644
--- a/src/lib/libcrypto/aes/aes_cbc.c
+++ b/src/lib/libcrypto/aes/aes_cbc.c
@@ -66,7 +66,6 @@ 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;
70 69
71 assert(in && out && key && ivec); 70 assert(in && out && key && ivec);
72 assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc)); 71 assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));
@@ -74,39 +73,22 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
74 if (AES_ENCRYPT == enc) { 73 if (AES_ENCRYPT == enc) {
75 while (len >= AES_BLOCK_SIZE) { 74 while (len >= AES_BLOCK_SIZE) {
76 for(n=0; n < AES_BLOCK_SIZE; ++n) 75 for(n=0; n < AES_BLOCK_SIZE; ++n)
77 out[n] = in[n] ^ iv[n]; 76 tmp[n] = in[n] ^ ivec[n];
78 AES_encrypt(out, out, key); 77 AES_encrypt(tmp, out, key);
79 iv = out; 78 memcpy(ivec, out, AES_BLOCK_SIZE);
80 len -= AES_BLOCK_SIZE; 79 len -= AES_BLOCK_SIZE;
81 in += AES_BLOCK_SIZE; 80 in += AES_BLOCK_SIZE;
82 out += AES_BLOCK_SIZE; 81 out += AES_BLOCK_SIZE;
83 } 82 }
84 if (len) { 83 if (len) {
85 for(n=0; n < len; ++n) 84 for(n=0; n < len; ++n)
86 out[n] = in[n] ^ iv[n]; 85 tmp[n] = in[n] ^ ivec[n];
87 for(n=len; n < AES_BLOCK_SIZE; ++n) 86 for(n=len; n < AES_BLOCK_SIZE; ++n)
88 out[n] = iv[n]; 87 tmp[n] = ivec[n];
89 AES_encrypt(out, out, key); 88 AES_encrypt(tmp, tmp, key);
90 iv = out; 89 memcpy(out, tmp, AES_BLOCK_SIZE);
91 } 90 memcpy(ivec, tmp, AES_BLOCK_SIZE);
92 memcpy(ivec,iv,AES_BLOCK_SIZE); 91 }
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);
110 } else { 92 } else {
111 while (len >= AES_BLOCK_SIZE) { 93 while (len >= AES_BLOCK_SIZE) {
112 memcpy(tmp, in, AES_BLOCK_SIZE); 94 memcpy(tmp, in, AES_BLOCK_SIZE);
@@ -120,12 +102,10 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
120 } 102 }
121 if (len) { 103 if (len) {
122 memcpy(tmp, in, AES_BLOCK_SIZE); 104 memcpy(tmp, in, AES_BLOCK_SIZE);
123 AES_decrypt(tmp, out, key); 105 AES_decrypt(tmp, tmp, key);
124 for(n=0; n < len; ++n) 106 for(n=0; n < len; ++n)
125 out[n] ^= ivec[n]; 107 out[n] = tmp[n] ^ ivec[n];
126 for(n=len; n < AES_BLOCK_SIZE; ++n)
127 out[n] = tmp[n];
128 memcpy(ivec, tmp, AES_BLOCK_SIZE); 108 memcpy(ivec, tmp, AES_BLOCK_SIZE);
129 } 109 }
130 } 110 }
131} 111}