|
@@ -118,14 +118,10 @@ test_crypto_aes(void *arg)
|
|
|
|
|
|
memset(data2, 0, 1024);
|
|
|
memset(data3, 0, 1024);
|
|
|
- env1 = crypto_cipher_new();
|
|
|
+ env1 = crypto_cipher_new(NULL);
|
|
|
test_neq(env1, 0);
|
|
|
- env2 = crypto_cipher_new();
|
|
|
+ env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
|
|
|
test_neq(env2, 0);
|
|
|
- j = crypto_cipher_generate_key(env1);
|
|
|
- crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
|
|
|
- crypto_cipher_encrypt_init_cipher(env1);
|
|
|
- crypto_cipher_decrypt_init_cipher(env2);
|
|
|
|
|
|
|
|
|
crypto_cipher_encrypt(env1, data2, data1, 512);
|
|
@@ -155,10 +151,8 @@ test_crypto_aes(void *arg)
|
|
|
env2 = NULL;
|
|
|
|
|
|
memset(data3, 0, 1024);
|
|
|
- env2 = crypto_cipher_new();
|
|
|
+ env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
|
|
|
test_neq(env2, 0);
|
|
|
- crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
|
|
|
- crypto_cipher_encrypt_init_cipher(env2);
|
|
|
for (j = 0; j < 1024-16; j += 17) {
|
|
|
crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
|
|
|
}
|
|
@@ -174,10 +168,9 @@ test_crypto_aes(void *arg)
|
|
|
env2 = NULL;
|
|
|
|
|
|
|
|
|
- env1 = crypto_cipher_new();
|
|
|
- crypto_cipher_set_key(env1, "\x80\x00\x00\x00\x00\x00\x00\x00"
|
|
|
- "\x00\x00\x00\x00\x00\x00\x00\x00");
|
|
|
- crypto_cipher_encrypt_init_cipher(env1);
|
|
|
+
|
|
|
+ env1 = crypto_cipher_new("\x80\x00\x00\x00\x00\x00\x00\x00"
|
|
|
+ "\x00\x00\x00\x00\x00\x00\x00\x00");
|
|
|
crypto_cipher_encrypt(env1, data1,
|
|
|
"\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
|
"\x00\x00\x00\x00\x00\x00\x00\x00", 16);
|
|
@@ -185,37 +178,55 @@ test_crypto_aes(void *arg)
|
|
|
|
|
|
|
|
|
* script. */
|
|
|
- crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
|
- "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
+ crypto_cipher_free(env1);
|
|
|
+ env1 = crypto_cipher_new_with_iv(
|
|
|
+ "\x80\x00\x00\x00\x00\x00\x00\x00"
|
|
|
+ "\x00\x00\x00\x00\x00\x00\x00\x00",
|
|
|
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
memset(data2, 0, 1024);
|
|
|
crypto_cipher_encrypt(env1, data1, data2, 32);
|
|
|
test_memeq_hex(data1, "335fe6da56f843199066c14a00a40231"
|
|
|
"cdd0b917dbc7186908a6bfb5ffd574d3");
|
|
|
-
|
|
|
- crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\xff\xff\xff\xff"
|
|
|
- "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
+ crypto_cipher_free(env1);
|
|
|
+ env1 = crypto_cipher_new_with_iv(
|
|
|
+ "\x80\x00\x00\x00\x00\x00\x00\x00"
|
|
|
+ "\x00\x00\x00\x00\x00\x00\x00\x00",
|
|
|
+ "\x00\x00\x00\x00\xff\xff\xff\xff"
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
memset(data2, 0, 1024);
|
|
|
crypto_cipher_encrypt(env1, data1, data2, 32);
|
|
|
test_memeq_hex(data1, "e627c6423fa2d77832a02b2794094b73"
|
|
|
"3e63c721df790d2c6469cc1953a3ffac");
|
|
|
-
|
|
|
- crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
|
- "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
+ crypto_cipher_free(env1);
|
|
|
+ env1 = crypto_cipher_new_with_iv(
|
|
|
+ "\x80\x00\x00\x00\x00\x00\x00\x00"
|
|
|
+ "\x00\x00\x00\x00\x00\x00\x00\x00",
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
memset(data2, 0, 1024);
|
|
|
crypto_cipher_encrypt(env1, data1, data2, 32);
|
|
|
test_memeq_hex(data1, "2aed2bff0de54f9328efd070bf48f70a"
|
|
|
"0EDD33D3C621E546455BD8BA1418BEC8");
|
|
|
|
|
|
|
|
|
- crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
|
- "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
+ crypto_cipher_free(env1);
|
|
|
+ env1 = crypto_cipher_new_with_iv(
|
|
|
+ "\x80\x00\x00\x00\x00\x00\x00\x00"
|
|
|
+ "\x00\x00\x00\x00\x00\x00\x00\x00",
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
crypto_cipher_crypt_inplace(env1, data2, 64);
|
|
|
test_memeq_hex(data2, "2aed2bff0de54f9328efd070bf48f70a"
|
|
|
"0EDD33D3C621E546455BD8BA1418BEC8"
|
|
|
"93e2c5243d6839eac58503919192f7ae"
|
|
|
"1908e67cafa08d508816659c2e693191");
|
|
|
- crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
|
- "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
+ crypto_cipher_free(env1);
|
|
|
+ env1 = crypto_cipher_new_with_iv(
|
|
|
+ "\x80\x00\x00\x00\x00\x00\x00\x00"
|
|
|
+ "\x00\x00\x00\x00\x00\x00\x00\x00",
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
|
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
|
|
|
crypto_cipher_crypt_inplace(env1, data2, 64);
|
|
|
test_assert(tor_mem_is_zero(data2, 64));
|
|
|
|
|
@@ -485,14 +496,11 @@ test_crypto_pk(void)
|
|
|
|
|
|
|
|
|
crypto_rand(data1, 1024);
|
|
|
- for (i = 0; i < 3; ++i) {
|
|
|
+ for (i = 0; i < 2; ++i) {
|
|
|
for (j = 85; j < 140; ++j) {
|
|
|
memset(data2,0,1024);
|
|
|
memset(data3,0,1024);
|
|
|
- if (i == 0 && j < 129)
|
|
|
- continue;
|
|
|
- p = (i==0)?PK_NO_PADDING:
|
|
|
- (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
|
|
|
+ p = (i==0)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
|
|
|
len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2),
|
|
|
data1,j,p,0);
|
|
|
test_assert(len>=0);
|
|
@@ -677,7 +685,6 @@ test_crypto_s2k(void)
|
|
|
static void
|
|
|
test_crypto_aes_iv(void *arg)
|
|
|
{
|
|
|
- crypto_cipher_t *cipher;
|
|
|
char *plain, *encrypted1, *encrypted2, *decrypted1, *decrypted2;
|
|
|
char plain_1[1], plain_15[15], plain_16[16], plain_17[17];
|
|
|
char key1[16], key2[16];
|
|
@@ -701,113 +708,76 @@ test_crypto_aes_iv(void *arg)
|
|
|
crypto_rand(plain_17, 17);
|
|
|
key1[0] = key2[0] + 128;
|
|
|
|
|
|
- cipher = crypto_create_init_cipher(key1, 1);
|
|
|
- encrypted_size = crypto_cipher_encrypt_with_iv(cipher, encrypted1, 16 + 4095,
|
|
|
+ encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
|
|
|
plain, 4095);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
+
|
|
|
test_eq(encrypted_size, 16 + 4095);
|
|
|
tt_assert(encrypted_size > 0);
|
|
|
* greater than 0, but its truth is not
|
|
|
* obvious to all analysis tools. */
|
|
|
- cipher = crypto_create_init_cipher(key1, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted1, 4095,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
|
|
|
encrypted1, encrypted_size);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
+
|
|
|
test_eq(decrypted_size, 4095);
|
|
|
tt_assert(decrypted_size > 0);
|
|
|
test_memeq(plain, decrypted1, 4095);
|
|
|
|
|
|
- cipher = crypto_create_init_cipher(key1, 1);
|
|
|
- encrypted_size = crypto_cipher_encrypt_with_iv(cipher, encrypted2, 16 + 4095,
|
|
|
+ encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
|
|
|
plain, 4095);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
+
|
|
|
test_eq(encrypted_size, 16 + 4095);
|
|
|
tt_assert(encrypted_size > 0);
|
|
|
- cipher = crypto_create_init_cipher(key1, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted2, 4095,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
|
|
|
encrypted2, encrypted_size);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(decrypted_size, 4095);
|
|
|
tt_assert(decrypted_size > 0);
|
|
|
test_memeq(plain, decrypted2, 4095);
|
|
|
test_memneq(encrypted1, encrypted2, encrypted_size);
|
|
|
|
|
|
- cipher = crypto_create_init_cipher(key2, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted2, 4095,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
|
|
|
encrypted1, encrypted_size);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_memneq(plain, decrypted2, encrypted_size);
|
|
|
|
|
|
encrypted1[0] += 42;
|
|
|
- cipher = crypto_create_init_cipher(key1, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted1, 4095,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
|
|
|
encrypted1, encrypted_size);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_memneq(plain, decrypted2, 4095);
|
|
|
|
|
|
- cipher = crypto_create_init_cipher(key1, 1);
|
|
|
- encrypted_size = crypto_cipher_encrypt_with_iv(cipher, encrypted1, 16 + 1,
|
|
|
+ encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
|
|
|
plain_1, 1);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(encrypted_size, 16 + 1);
|
|
|
tt_assert(encrypted_size > 0);
|
|
|
- cipher = crypto_create_init_cipher(key1, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted1, 1,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
|
|
|
encrypted1, encrypted_size);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(decrypted_size, 1);
|
|
|
tt_assert(decrypted_size > 0);
|
|
|
test_memeq(plain_1, decrypted1, 1);
|
|
|
|
|
|
- cipher = crypto_create_init_cipher(key1, 1);
|
|
|
- encrypted_size = crypto_cipher_encrypt_with_iv(cipher, encrypted1, 16 + 15,
|
|
|
+ encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
|
|
|
plain_15, 15);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(encrypted_size, 16 + 15);
|
|
|
tt_assert(encrypted_size > 0);
|
|
|
- cipher = crypto_create_init_cipher(key1, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted1, 15,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
|
|
|
encrypted1, encrypted_size);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(decrypted_size, 15);
|
|
|
tt_assert(decrypted_size > 0);
|
|
|
test_memeq(plain_15, decrypted1, 15);
|
|
|
|
|
|
- cipher = crypto_create_init_cipher(key1, 1);
|
|
|
- encrypted_size = crypto_cipher_encrypt_with_iv(cipher, encrypted1, 16 + 16,
|
|
|
+ encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
|
|
|
plain_16, 16);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(encrypted_size, 16 + 16);
|
|
|
tt_assert(encrypted_size > 0);
|
|
|
- cipher = crypto_create_init_cipher(key1, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted1, 16,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
|
|
|
encrypted1, encrypted_size);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(decrypted_size, 16);
|
|
|
tt_assert(decrypted_size > 0);
|
|
|
test_memeq(plain_16, decrypted1, 16);
|
|
|
|
|
|
- cipher = crypto_create_init_cipher(key1, 1);
|
|
|
- encrypted_size = crypto_cipher_encrypt_with_iv(cipher, encrypted1, 16 + 17,
|
|
|
+ encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
|
|
|
plain_17, 17);
|
|
|
- crypto_cipher_free(cipher);
|
|
|
- cipher = NULL;
|
|
|
test_eq(encrypted_size, 16 + 17);
|
|
|
tt_assert(encrypted_size > 0);
|
|
|
- cipher = crypto_create_init_cipher(key1, 0);
|
|
|
- decrypted_size = crypto_cipher_decrypt_with_iv(cipher, decrypted1, 17,
|
|
|
+ decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
|
|
|
encrypted1, encrypted_size);
|
|
|
test_eq(decrypted_size, 17);
|
|
|
tt_assert(decrypted_size > 0);
|
|
@@ -820,8 +790,6 @@ test_crypto_aes_iv(void *arg)
|
|
|
tor_free(encrypted2);
|
|
|
tor_free(decrypted1);
|
|
|
tor_free(decrypted2);
|
|
|
- if (cipher)
|
|
|
- crypto_cipher_free(cipher);
|
|
|
}
|
|
|
|
|
|
|