WIP Add AES-XTS client encryption support
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
// Copyright (c) Vitaliy Filippov, 2019+
|
||||
// License: VNPL-1.1 (see README.md for details)
|
||||
|
||||
#ifdef WITH_OPENSSL
|
||||
#include <openssl/rand.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "cluster_client_impl.h"
|
||||
#include "msgr_encrypt.h"
|
||||
|
||||
void configure_single_pg_pool(cluster_client_t *cli)
|
||||
{
|
||||
@@ -547,11 +552,76 @@ void test_writeback_merge()
|
||||
printf("[ok] writeback merge test\n");
|
||||
}
|
||||
|
||||
#ifdef WITH_OPENSSL
|
||||
void test_msgr_encrypt()
|
||||
{
|
||||
const size_t sz = 1048576;
|
||||
uint8_t *src = (uint8_t*)malloc_or_die(sz);
|
||||
for (size_t i = 0; i < sz; i++)
|
||||
src[i] = (i*0x1001) % 256;
|
||||
uint8_t *crypt = (uint8_t*)malloc_or_die(sz);
|
||||
uint8_t *decrypt = (uint8_t*)malloc_or_die(sz);
|
||||
uint8_t *crypt2 = (uint8_t*)malloc_or_die(sz);
|
||||
uint8_t *key = (uint8_t*)malloc_or_die(64);
|
||||
RAND_bytes(key, 64);
|
||||
|
||||
// Basic encrypt+decrypt and also get reference data
|
||||
|
||||
auto enc = new op_aes_xts_encrypt_t();
|
||||
enc->start(key, 4096 * 113, 4096);
|
||||
size_t in_pos = 0;
|
||||
size_t out_pos = 0;
|
||||
while (out_pos < sz)
|
||||
{
|
||||
enc->update(src+in_pos, sz-in_pos, crypt+out_pos, sz-out_pos, in_pos, out_pos);
|
||||
}
|
||||
|
||||
auto dec = new op_aes_xts_decrypt_t();
|
||||
dec->start(key, 4096 * 113, 4096);
|
||||
in_pos = out_pos = 0;
|
||||
while (out_pos < sz)
|
||||
{
|
||||
dec->update(crypt+in_pos, sz-in_pos, decrypt+out_pos, sz-out_pos, in_pos, out_pos);
|
||||
}
|
||||
|
||||
assert(memcmp(src, decrypt, sz) == 0);
|
||||
|
||||
// 4095+1 byte fragmented encrypt
|
||||
|
||||
enc->start(key, 4096 * 114, 4096);
|
||||
in_pos = out_pos = 0;
|
||||
enc->update(src+4096, 4096, crypt2, 4095, in_pos, out_pos);
|
||||
assert(in_pos == 4080);
|
||||
assert(out_pos == 4080);
|
||||
enc->update(src+4096+in_pos, 4096-in_pos, crypt2+out_pos, 4096-out_pos, in_pos, out_pos);
|
||||
assert(in_pos == 4096);
|
||||
assert(out_pos == 4096);
|
||||
assert(memcmp(crypt2, crypt+4096, 4096) == 0);
|
||||
|
||||
// FIXME 5+27+... fragmented encrypt
|
||||
|
||||
// FIXME Fragmented decrypt with small buffers
|
||||
|
||||
delete dec;
|
||||
delete enc;
|
||||
|
||||
free(key);
|
||||
free(crypt2);
|
||||
free(decrypt);
|
||||
free(crypt);
|
||||
free(src);
|
||||
printf("[ok] msgr aes-xts encryption test\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int narg, char *args[])
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
test_writeback();
|
||||
test_writeback_merge();
|
||||
#ifdef WITH_OPENSSL
|
||||
test_msgr_encrypt();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user