import copy
bcm_list = ['NONE', 'ECB', 'CTR', 'CBC', 'GCM',
- 'CFB', 'CFB1', 'CFB8','OFB', 'CCM']
+ 'CFB', 'CFB1', 'CFB8','OFB', 'CCM', 'WRAP']
key_length_list = [8, 40, 64, 80, 128,
192, 256, 512, 1024, 2048, 4096,]
iv = 'd17d7ace9acea556527b1037ee3aa824' #128
def default_iv_len(algo, bcm):
- if bcm == 'ECB' or bcm == 'NONE':
+ if bcm == 'ECB' or bcm == 'NONE' or (algo == '3DES_3TDEA' and bcm == 'WRAP'):
iv_len = 0
- elif algo == 'AES' and bcm != 'CCM':
+ elif algo == 'AES' and bcm != 'CCM' and bcm != 'WRAP':
iv_len = 128
else:
iv_len = 64
('GCM', Bcm(range(24, 256 + 8, 8))),
('CCM', Bcm(range(56, 104 + 8, 8))),
('CTR', Bcm([128])),
+ ('WRAP', Bcm([64])),
]))
des = Algorithm([64])
('CFB1', Bcm([64])),
('CFB8', Bcm([64])),
('ECB', Bcm([0])),
+ ('WRAP', Bcm([0])),
]))
rc2 = Algorithm(range(8, 1024 + 8, 8))
Buffer output;
size_t len = 0;
auto final_ptr = out_buf_alloc(m_decCtxPtr, 0, len);
- YACA_SUCCESS(finalize(ctx_ptr->get(), final_ptr.get(), &len));
- output.insert(output.end(), final_ptr.get(), final_ptr.get() + len);
-
+ if (final_ptr != nullptr) {
+ YACA_SUCCESS(finalize(ctx_ptr->get(), final_ptr.get(), &len));
+ output.insert(output.end(), final_ptr.get(), final_ptr.get() + len);
+ }
return output;
}
};
}
}
-void test_vector_encrypt_decrypt(yaca_encrypt_algorithm_e algo,
+void test_vector_encrypt_decrypt(const Buffer &input,
+ yaca_encrypt_algorithm_e algo,
yaca_block_cipher_mode_e bcm,
size_t key_len,
size_t iv_len)
{
- std::string s = "abcdefghijklmnoprstuvwxyz0123456789";
- Buffer input(s.begin(), s.end());
std::stringstream message;
auto key_ptr = generate_key(algo_to_key_type(algo), key_len);
<< message.str());
}
+void test_encryption_decryption(std::string filename)
+{
+ Buffer input;
+ std::string s = "abcdefghijklmnoprstuvwxyz0123456789";
+
+ auto tvv = loadTestVector(filename);
+
+ for (const auto& tv : tvv) {
+ yaca_encrypt_algorithm_e algo;
+ yaca_block_cipher_mode_e bcm;
+ size_t key_len;
+ size_t iv_len;
+ size_t key_data_len = 0;
+
+ tv.get("algo", algo);
+ tv.get("bcm", bcm);
+ tv.get("key_len", key_len);
+ tv.get("iv_len", iv_len);
+ if (bcm == YACA_BCM_WRAP)
+ tv.get("key_data_len", key_data_len);
+
+ if (key_data_len > 0)
+ input = random_buffer(key_data_len / 8);
+ else
+ std::copy(s.begin(), s.end(), std::back_inserter(input));
+
+ test_vector_encrypt_decrypt(input, algo, bcm, key_len, iv_len);
+ }
+}
+
}//namespace anonymous
RUNNER_TEST_GROUP_INIT(T3000_YACA_ENCRYPT);
YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, DATA.size(),
ciphertext_ptr.get(), &ciphertext_len));
+ YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, 0,
+ ciphertext_ptr.get(), &ciphertext_len));
+
YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), 0,
ciphertext_ptr.get(), &ciphertext_len));
YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), 0,
plaintext_ptr.get(), &plaintext_len));
+ YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, 0,
+ plaintext_ptr.get(), &plaintext_len));
+
YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
nullptr, &plaintext_len));
KEY_LEN, nullptr));
}
+RUNNER_TEST(T3075_yaca_key_wrap_unwrap_invalid_param, YacaTest)
+{
+ size_t wrapped_len = 0;
+ size_t unwrapped_len = 0;
+
+ KeyPtr key = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_192BIT);
+ KeyPtr iv = null_key();
+ Buffer key_data = random_buffer(YACA_KEY_LENGTH_192BIT / 8);
+
+ CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_WRAP, key, iv);
+ CtxPtr dec_ctx = decrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_WRAP, key, iv);
+
+ ChrPtr wrapped = out_buf_alloc(enc_ctx, key_data.size(), wrapped_len);
+ ChrPtr unwrapped = out_buf_alloc(dec_ctx, wrapped_len, unwrapped_len);
+
+ YACA_INVALID_PARAM(yaca_encrypt_finalize(enc_ctx.get(), wrapped.get(), &wrapped_len));
+ YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), YACA_KEY_LENGTH_UNSAFE_64BIT / 8,
+ wrapped.get(), &wrapped_len));
+ YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), YACA_KEY_LENGTH_512BIT / 8,
+ wrapped.get(), &wrapped_len));
+ YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), key_data.data(), key_data.size(),
+ wrapped.get(), &wrapped_len));
+
+ // only a single update is allowed
+ YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), key_data.size(),
+ wrapped.get(), &wrapped_len));
+
+ YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx.get(), wrapped.get(), &wrapped_len));
+ YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len - 1,
+ unwrapped.get(),&unwrapped_len));
+ YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len,
+ unwrapped.get(), &unwrapped_len));
+
+ // only a single update is allowed
+ YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len,
+ unwrapped.get(), &unwrapped_len));
+}
+
RUNNER_TEST(T3080_yaca_encrypt_decrypt_init_param_comb, YacaTest)
{
auto tvv = loadTestVector("encrypt_param_comb.txt");
test_encryption_output("encrypt_output_comparison.txt");
test_encryption_output("encrypt_output_comparison_rc2_cast5_nopad.txt", false);
test_encryption_output("encrypt_output_comparison_rc4.txt");
+ test_encryption_output("encrypt_output_comparison_wrap.txt");
}
RUNNER_TEST(T3100_yaca_encrypt_decrypt_comparison, YacaTest)
{
- auto tvv = loadTestVector("encrypt_valid_param.txt");
-
- for (const auto& tv : tvv) {
- yaca_encrypt_algorithm_e algo;
- yaca_block_cipher_mode_e bcm;
- size_t key_len;
- size_t iv_len;
-
- tv.get("algo", algo);
- tv.get("bcm", bcm);
- tv.get("key_len", key_len);
- tv.get("iv_len", iv_len);
-
- test_vector_encrypt_decrypt(algo, bcm, key_len, iv_len);
- }
+ test_encryption_decryption("encrypt_valid_param.txt");
+ test_encryption_decryption("encrypt_valid_param_wrap.txt");
}