1 /* |
1 /* |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * |
3 * |
4 * Copyright 2017 Olaf Wintermann. All rights reserved. |
4 * Copyright 2018 Olaf Wintermann. All rights reserved. |
5 * |
5 * |
6 * Redistribution and use in source and binary forms, with or without |
6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: |
7 * modification, are permitted provided that the following conditions are met: |
8 * |
8 * |
9 * 1. Redistributions of source code must retain the above copyright |
9 * 1. Redistributions of source code must retain the above copyright |
245 } |
245 } |
246 |
246 |
247 UCX_TEST_END; |
247 UCX_TEST_END; |
248 } |
248 } |
249 |
249 |
|
250 UCX_TEST(test_crypto_buffer) { |
|
251 UCX_TEST_BEGIN; |
|
252 |
|
253 for(int i=0;i<32;i++) { |
|
254 DavKey *key = i < 16 ? &keys256[i] : &keys128[i%16]; |
|
255 |
|
256 for(int j=0;j<20;j++) { |
|
257 UcxBuffer *content = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); |
|
258 ucx_buffer_puts(content, strings[j]); |
|
259 content->pos = 0; |
|
260 |
|
261 UcxBuffer *enc = aes_encrypt_buffer(content, key); |
|
262 UCX_TEST_ASSERT(enc->size >= content->size + 16, "aes_encrypt_buffer failed"); |
|
263 |
|
264 UcxBuffer *dec = aes_decrypt_buffer(enc, key); |
|
265 UCX_TEST_ASSERT(dec->size == content->size, "aes_decrypt_buffer failed"); |
|
266 |
|
267 UCX_TEST_ASSERT(!memcmp(content->space, dec->space, dec->size), "decrypted buffer has wrong content"); |
|
268 |
|
269 ucx_buffer_free(content); |
|
270 ucx_buffer_free(enc); |
|
271 ucx_buffer_free(dec); |
|
272 } |
|
273 } |
|
274 |
|
275 UCX_TEST_END; |
|
276 } |
|
277 |
250 UCX_TEST(test_crypto_stream) { |
278 UCX_TEST(test_crypto_stream) { |
251 UcxBuffer *data = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
279 UcxBuffer *data = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
252 UcxBuffer *cbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
280 UcxBuffer *cbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
253 UcxBuffer *pbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
281 UcxBuffer *pbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
254 |
282 |
281 aes_decrypter_shutdown(dec); |
309 aes_decrypter_shutdown(dec); |
282 aes_decrypter_close(dec); |
310 aes_decrypter_close(dec); |
283 |
311 |
284 UCX_TEST_ASSERT(slen == pbuf->pos, "wrong length after enc-dec"); |
312 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"); |
313 UCX_TEST_ASSERT(!memcmp(strings[j], pbuf->space, slen), "wrong content after enc-dec"); |
|
314 |
|
315 data->pos = 0; |
|
316 UcxBuffer *enc2 = aes_encrypt_buffer(data, key); |
|
317 UcxBuffer *dec2 = aes_decrypt_buffer(enc2, key); |
|
318 |
|
319 UCX_TEST_ASSERT(dec2->size == data->size, "dec2 has wrong size"); |
|
320 UCX_TEST_ASSERT(!memcmp(strings[j], dec2->space, dec2->size), "dec2 has wrong content"); |
286 } |
321 } |
287 } |
322 } |
288 |
323 |
289 UCX_TEST_END; |
324 UCX_TEST_END; |
290 } |
325 } |