Skip to content

Commit

Permalink
Update low-level crypto tests with more cases for better coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Jan 8, 2025
1 parent 81127c0 commit 21c0822
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/tests/cipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,27 @@ 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"));

/* 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));

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 21c0822

Please sign in to comment.