33 |
33 |
34 #include "crypto.h" |
34 #include "crypto.h" |
35 |
35 |
36 #include <ucx/string.h> |
36 #include <ucx/string.h> |
37 #include <ucx/utils.h> |
37 #include <ucx/utils.h> |
|
38 #include <ucx/buffer.h> |
38 #include <libidav/utils.h> |
39 #include <libidav/utils.h> |
|
40 #include <libidav/crypto.h> |
39 |
41 |
40 static DavKey keys256[16]; |
42 static DavKey keys256[16]; |
41 static DavKey keys128[16]; |
43 static DavKey keys128[16]; |
42 |
44 |
43 static char *strings[] = { |
45 static char *strings[] = { |
242 } |
244 } |
243 } |
245 } |
244 |
246 |
245 UCX_TEST_END; |
247 UCX_TEST_END; |
246 } |
248 } |
|
249 |
|
250 UCX_TEST(test_crypto_stream) { |
|
251 UcxBuffer *data = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
|
252 UcxBuffer *cbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
|
253 UcxBuffer *pbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
|
254 |
|
255 UCX_TEST_BEGIN; |
|
256 |
|
257 for(int i=0;i<32;i++) { |
|
258 DavKey *key = i < 16 ? &keys256[i] : &keys128[i%16]; |
|
259 for(int j=0;j<20;j++) { |
|
260 data->pos = 0; |
|
261 data->size = 0; |
|
262 size_t slen = strlen(strings[j]); |
|
263 ucx_buffer_write(strings[j], 1, slen, data); |
|
264 ucx_buffer_seek(data, 0, SEEK_SET); |
|
265 |
|
266 cbuf->pos = 0; |
|
267 cbuf->size = 0; |
|
268 pbuf->pos = 0; |
|
269 pbuf->size = 0; |
|
270 |
|
271 AESEncrypter *enc = aes_encrypter_new(key, data, (dav_read_func)ucx_buffer_read); |
|
272 char buf[1024]; |
|
273 size_t r = 0; |
|
274 while((r = aes_read(buf, 1, 1024, enc)) != 0) { |
|
275 ucx_buffer_write(buf, 1, r, cbuf); |
|
276 } |
|
277 aes_encrypter_close(enc); |
|
278 |
|
279 AESDecrypter *dec = aes_decrypter_new(key, pbuf, (dav_write_func)ucx_buffer_write); |
|
280 aes_write(cbuf->space, 1, cbuf->pos, dec); |
|
281 aes_decrypter_shutdown(dec); |
|
282 aes_decrypter_close(dec); |
|
283 |
|
284 UCX_TEST_ASSERT(slen == pbuf->pos, "wrong length after enc-dec"); |
|
285 UCX_TEST_ASSERT(!memcmp(strings[j], pbuf->space, slen), "wrong content after enc-dec"); |
|
286 } |
|
287 } |
|
288 |
|
289 UCX_TEST_END; |
|
290 } |