194 free(enc); |
194 free(enc); |
195 } |
195 } |
196 |
196 |
197 |
197 |
198 char* aes_encrypt(char *in, DavKey *key) { |
198 char* aes_encrypt(char *in, DavKey *key) { |
199 char *iv = malloc(16); |
199 unsigned char iv[16]; |
200 if(!RAND_bytes(iv, 16)) { |
200 if(!RAND_bytes(iv, 16)) { |
201 free(iv); |
|
202 return NULL; |
201 return NULL; |
203 } |
202 } |
204 |
203 |
205 EVP_CIPHER_CTX ctx; |
204 EVP_CIPHER_CTX ctx; |
206 EVP_CIPHER_CTX_init(&ctx); |
205 EVP_CIPHER_CTX_init(&ctx); |
207 if(key->type == DAV_KEY_AES128) { |
206 if(key->type == DAV_KEY_AES128) { |
208 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key->data, iv); |
207 EVP_EncryptInit_ex( |
|
208 &ctx, |
|
209 EVP_aes_128_cbc(), |
|
210 NULL, |
|
211 (unsigned char*)key->data, |
|
212 iv); |
209 } else if(key->type == DAV_KEY_AES256) { |
213 } else if(key->type == DAV_KEY_AES256) { |
210 EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->data, iv); |
214 EVP_EncryptInit_ex( |
|
215 &ctx, |
|
216 EVP_aes_256_cbc(), |
|
217 NULL, |
|
218 (unsigned char*)key->data, |
|
219 iv); |
211 } else { |
220 } else { |
212 return NULL; |
221 return NULL; |
213 } |
222 } |
214 |
223 |
215 int len = strlen(in); |
224 int len = strlen(in); |
216 int buflen = len + 64; |
225 int buflen = len + 64; |
217 char *buf = calloc(1, buflen); |
226 unsigned char *buf = calloc(1, buflen); |
218 memcpy(buf, iv, 16); |
227 memcpy(buf, iv, 16); |
219 |
228 |
220 int l = buflen - 16; |
229 int l = buflen - 16; |
221 EVP_EncryptUpdate(&ctx, buf + 16, &l, in, len); |
230 EVP_EncryptUpdate(&ctx, buf + 16, &l, (unsigned char*)in, len); |
222 |
231 |
223 int f = 0; |
232 int f = 0; |
224 EVP_EncryptFinal_ex(&ctx, buf + 16 + l, &f); |
233 EVP_EncryptFinal_ex(&ctx, buf + 16 + l, &f); |
225 char *out = util_base64encode(buf, 16 + l + f); |
234 char *out = util_base64encode((char*)buf, 16 + l + f); |
226 free(buf); |
235 free(buf); |
227 return out; |
236 return out; |
228 } |
237 } |
229 |
238 |
230 char* aes_decrypt(char *in, DavKey *key) { |
239 char* aes_decrypt(char *in, DavKey *key) { |
231 int len; |
240 int len; |
232 char *buf = util_base64decode_len(in, &len); |
241 unsigned char *buf = (unsigned char*)util_base64decode_len(in, &len); |
233 |
242 |
234 EVP_CIPHER_CTX ctx; |
243 EVP_CIPHER_CTX ctx; |
235 EVP_CIPHER_CTX_init(&ctx); |
244 EVP_CIPHER_CTX_init(&ctx); |
236 if(key->type == DAV_KEY_AES128) { |
245 if(key->type == DAV_KEY_AES128) { |
237 EVP_DecryptInit_ex( |
246 EVP_DecryptInit_ex( |
249 buf); |
258 buf); |
250 } else { |
259 } else { |
251 return NULL; |
260 return NULL; |
252 } |
261 } |
253 |
262 |
254 char *out = malloc(len + 1); |
263 unsigned char *out = malloc(len + 1); |
255 int outlen = len; |
264 int outlen = len; |
256 char *in_buf = buf + 16; |
265 unsigned char *in_buf = buf + 16; |
257 int inlen = len - 16; |
266 int inlen = len - 16; |
258 int f = 0; |
267 int f = 0; |
259 |
|
260 |
|
261 |
268 |
262 EVP_DecryptUpdate(&ctx, out, &outlen, in_buf, inlen); |
269 EVP_DecryptUpdate(&ctx, out, &outlen, in_buf, inlen); |
263 EVP_DecryptFinal_ex(&ctx, out + outlen, &f); |
270 EVP_DecryptFinal_ex(&ctx, out + outlen, &f); |
264 out[outlen + f] = '\0'; |
271 out[outlen + f] = '\0'; |
265 free(buf); |
272 free(buf); |
266 return out; |
273 return (char*)out; |
267 } |
274 } |