Skip to content

Commit

Permalink
more variation for test_ffi_encrypt_pk_with_v6_key test, issue #21
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ-91 committed Apr 21, 2023
1 parent 7e6a5d3 commit 723dcac
Showing 1 changed file with 62 additions and 87 deletions.
149 changes: 62 additions & 87 deletions src/tests/ffi-enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,96 +756,71 @@ TEST_F(rnp_tests, test_ffi_encrypt_pk_with_v6_key)
//RNP_LOG_HEX("key id", ffi->secring->keys.front().keyid().data(), ffi->secring->keys.front().keyid().size());
//RNP_LOG_HEX("key id", ffi->secring->keys.back().keyid().data(), ffi->secring->keys.back().keyid().size());

// write out some data
FILE *fp = fopen("plaintext", "wb");
assert_non_null(fp);
assert_int_equal(1, fwrite(plaintext, strlen(plaintext), 1, fp));
assert_int_equal(0, fclose(fp));

// create input+output
assert_rnp_success(rnp_input_from_path(&input, "plaintext"));
assert_non_null(input);
assert_rnp_success(rnp_output_to_path(&output, "encrypted"));
assert_non_null(output);
// create encrypt operation
assert_rnp_success(rnp_op_encrypt_create(&op, ffi, input, output));
// add recipients
rnp_key_handle_t key = NULL;
assert_rnp_success(rnp_locate_key(ffi, "keyid", "d1db378da9930885", &key));
assert_non_null(key);

//assert_rnp_failure(rnp_op_encrypt_add_recipient(op, NULL)); // what for ?
assert_rnp_success(rnp_op_encrypt_add_recipient(op, key));
assert_rnp_success(rnp_op_encrypt_enable_pkesk_v6(op));
rnp_key_handle_destroy(key);
key = NULL;
/*assert_rnp_success(rnp_locate_key(ffi, "userid", "key1-uid1", &key));
assert_rnp_success(rnp_op_encrypt_add_recipient(op, key));
rnp_key_handle_destroy(key);
key = NULL;*/
// set the data encryption cipher
assert_rnp_success(rnp_op_encrypt_set_cipher(op, "AES256")); // TODOMTG: AES256 should be enforced
// execute the operation
assert_rnp_success(rnp_op_encrypt_execute(op));

// make sure the output file was created
assert_true(rnp_file_exists("encrypted"));

// cleanup
assert_rnp_success(rnp_input_destroy(input));
input = NULL;
assert_rnp_success(rnp_output_destroy(output));
output = NULL;
assert_rnp_success(rnp_op_encrypt_destroy(op));
op = NULL;
std::vector<std::string> ciphers = {"AES128", "AES192", "AES256"};
std::vector<std::string> aead_modes = {"None", "EAX", "OCB"};

for(auto aead : aead_modes)
{
for(auto cipher : ciphers)
{
// write out some data
FILE *fp = fopen("plaintext", "wb");
assert_non_null(fp);
assert_int_equal(1, fwrite(plaintext, strlen(plaintext), 1, fp));
assert_int_equal(0, fclose(fp));

// create input+output
assert_rnp_success(rnp_input_from_path(&input, "plaintext"));
assert_non_null(input);
assert_rnp_success(rnp_output_to_path(&output, "encrypted"));
assert_non_null(output);
// create encrypt operation
assert_rnp_success(rnp_op_encrypt_create(&op, ffi, input, output));
// add recipients
rnp_key_handle_t key = NULL;
assert_rnp_success(rnp_locate_key(ffi, "keyid", "d1db378da9930885", &key));
assert_non_null(key);

//assert_rnp_failure(rnp_op_encrypt_add_recipient(op, NULL)); // what for ?
assert_rnp_success(rnp_op_encrypt_add_recipient(op, key));
assert_rnp_success(rnp_op_encrypt_enable_pkesk_v6(op));
rnp_key_handle_destroy(key);
key = NULL;

// set the data encryption cipher
assert_rnp_success(rnp_op_encrypt_set_aead(op, aead.c_str()));
assert_rnp_success(rnp_op_encrypt_set_cipher(op, cipher.c_str()));
// execute the operation
assert_rnp_success(rnp_op_encrypt_execute(op));

// make sure the output file was created
assert_true(rnp_file_exists("encrypted"));

// cleanup
assert_rnp_success(rnp_input_destroy(input));
input = NULL;
assert_rnp_success(rnp_output_destroy(output));
output = NULL;
assert_rnp_success(rnp_op_encrypt_destroy(op));
op = NULL;

/* decrypt */
/* decrypt */

// decrypt
assert_rnp_success(rnp_input_from_path(&input, "encrypted"));
assert_non_null(input);
assert_rnp_success(rnp_output_to_path(&output, "decrypted"));
assert_non_null(output);
assert_rnp_success(rnp_ffi_set_pass_provider(ffi, NULL, NULL));
assert_rnp_success(rnp_decrypt(ffi, input, output));
// cleanup
rnp_input_destroy(input);
input = NULL;
rnp_output_destroy(output);
output = NULL;
#if 0
// decrypt (wrong pass, should fail)
assert_rnp_success(rnp_input_from_path(&input, "encrypted"));
assert_non_null(input);
assert_rnp_success(rnp_output_to_path(&output, "decrypted"));
assert_non_null(output);
const char *pass = "wrong1";
assert_rnp_success(rnp_ffi_set_pass_provider(ffi, getpasscb_once, &pass));
assert_rnp_failure(rnp_decrypt(ffi, input, output));
// cleanup
rnp_input_destroy(input);
input = NULL;
rnp_output_destroy(output);
output = NULL;

// decrypt
assert_rnp_success(rnp_input_from_path(&input, "encrypted"));
assert_non_null(input);
assert_rnp_success(rnp_output_to_path(&output, "decrypted"));
assert_non_null(output);
assert_rnp_success(
rnp_ffi_set_pass_provider(ffi, ffi_string_password_provider, (void *) "password"));
assert_rnp_success(rnp_decrypt(ffi, input, output));
// cleanup
rnp_input_destroy(input);
input = NULL;
rnp_output_destroy(output);
output = NULL;
// read in the decrypted file
assert_string_equal(file_to_str("decrypted").c_str(), plaintext);
// final cleanup

#endif
// decrypt
assert_rnp_success(rnp_input_from_path(&input, "encrypted"));
assert_non_null(input);
assert_rnp_success(rnp_output_to_path(&output, "decrypted"));
assert_non_null(output);
assert_rnp_success(rnp_ffi_set_pass_provider(ffi, NULL, NULL));
assert_rnp_success(rnp_decrypt(ffi, input, output));
// cleanup
rnp_input_destroy(input);
input = NULL;
rnp_output_destroy(output);
output = NULL;
}
}
rnp_ffi_destroy(ffi);
}
#endif
Expand Down

0 comments on commit 723dcac

Please sign in to comment.