diff --git a/src/tests/cipher.cpp b/src/tests/cipher.cpp index 5f7417033..6bbda0a91 100644 --- a/src/tests/cipher.cpp +++ b/src/tests/cipher.cpp @@ -126,9 +126,13 @@ TEST_F(rnp_tests, pkcs1_rsa_test_success) assert_true(keygen.generate(seckey, true)); pgp::RSAEncMaterial enc; + pgp::EGEncMaterial enc2; + assert_rnp_failure(seckey.material->encrypt(global_ctx, enc2, ptext)); assert_rnp_success(seckey.material->encrypt(global_ctx, enc, ptext)); assert_int_equal(enc.enc.m.len, 1024 / 8); + assert_rnp_failure(seckey.material->decrypt(global_ctx, dec, enc2)); + assert_true(dec.empty()); assert_rnp_success(seckey.material->decrypt(global_ctx, dec, enc)); assert_int_equal(dec.size(), 3); assert_true(bin_eq_hex(dec.data(), 3, "616263")); @@ -136,9 +140,13 @@ TEST_F(rnp_tests, pkcs1_rsa_test_success) /* Try signing */ assert_true(keygen.generate(seckey, true)); - rnp::secure_bytes hash(32); + rnp::secure_bytes hash(32); + global_ctx.rng.get(hash.data(), hash.size()); pgp::RSASigMaterial sig(PGP_HASH_SHA256); + pgp::DSASigMaterial sig2(PGP_HASH_SHA256); + assert_rnp_failure(seckey.material->sign(global_ctx, sig2, hash)); + assert_rnp_failure(seckey.material->verify(global_ctx, sig2, hash)); assert_rnp_success(seckey.material->sign(global_ctx, sig, hash)); assert_rnp_success(seckey.material->verify(global_ctx, sig, hash)); @@ -174,12 +182,22 @@ TEST_F(rnp_tests, rnp_test_eddsa) pgp_key_pkt_t seckey; assert_true(keygen.generate(seckey, true)); - rnp::secure_bytes hash(32); - pgp::ECSigMaterial sig(PGP_HASH_SHA256); + rnp::secure_bytes hash(32); + global_ctx.rng.get(hash.data(), hash.size()); + + pgp::ECSigMaterial sig(PGP_HASH_SHA256); + pgp::RSASigMaterial sig2(PGP_HASH_SHA256); + + assert_rnp_failure(seckey.material->sign(global_ctx, sig2, hash)); + assert_rnp_failure(seckey.material->verify(global_ctx, sig2, hash)); assert_rnp_success(seckey.material->sign(global_ctx, sig, hash)); assert_rnp_success(seckey.material->verify(global_ctx, sig, hash)); + pgp::ECDHEncMaterial enc; + assert_rnp_failure(seckey.material->encrypt(global_ctx, enc, hash)); + assert_rnp_failure(seckey.material->decrypt(global_ctx, hash, enc)); + // cut one byte off hash -> invalid sig rnp::secure_bytes hash_cut(31); assert_rnp_failure(seckey.material->verify(global_ctx, sig, hash_cut)); @@ -211,13 +229,17 @@ TEST_F(rnp_tests, rnp_test_x25519) assert_rnp_success(pgp_fingerprint(fp, seckey)); rnp::secure_bytes in({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}); pgp::ECDHEncMaterial enc; + pgp::SM2EncMaterial enc2; enc.enc.fp = fp.vec(); + assert_rnp_failure(seckey.material->encrypt(global_ctx, enc2, in)); assert_rnp_success(seckey.material->encrypt(global_ctx, enc, in)); assert_true(enc.enc.m.size() > 16); assert_int_equal(enc.enc.p.mpi[0], 0x40); assert_int_equal(enc.enc.p.len, 33); /* decrypt */ rnp::secure_bytes out; + assert_rnp_failure(seckey.material->decrypt(global_ctx, out, enc2)); + assert_true(out.empty()); assert_rnp_success(seckey.material->decrypt(global_ctx, out, enc)); assert_int_equal(out.size(), 16); assert_int_equal(memcmp(in.data(), out.data(), 16), 0); @@ -237,6 +259,12 @@ TEST_F(rnp_tests, rnp_test_x25519) enc.enc.m.push_back(back); enc.enc.m.push_back(0); assert_rnp_failure(seckey.material->decrypt(global_ctx, out, enc)); + + rnp::secure_bytes hash(32); + global_ctx.rng.get(hash.data(), hash.size()); + pgp::ECSigMaterial sig(PGP_HASH_SHA256); + assert_rnp_failure(seckey.material->sign(global_ctx, sig, hash)); + assert_rnp_failure(seckey.material->verify(global_ctx, sig, hash)); } static void @@ -287,6 +315,12 @@ TEST_F(rnp_tests, ecdsa_signverify_success) assert_true(keygen.generate(seckey1, true)); assert_true(keygen.generate(seckey2, true)); + rnp::secure_bytes in({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}); + rnp::secure_bytes out; + pgp::ECDHEncMaterial enc; + assert_rnp_failure(seckey1.material->encrypt(global_ctx, enc, in)); + assert_rnp_failure(seckey1.material->decrypt(global_ctx, out, enc)); + pgp::ECSigMaterial sig(hash_alg); assert_rnp_success(seckey1.material->sign(global_ctx, sig, hash)); assert_rnp_success(seckey1.material->verify(global_ctx, sig, hash));