Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pgp_key_material_t to C++. #2250

Merged
merged 8 commits into from
Aug 24, 2024
Merged

Conversation

ni4
Copy link
Contributor

@ni4 ni4 commented Jul 16, 2024

To avoid anonymous opaque unions and bring more flexibility to the codebase.

Comment on lines 2081 to 2180
!obj_add_mpi_json(jso, "g", eg.g(), ctx.dump_mpi) ||
!obj_add_mpi_json(jso, "y", eg.y(), ctx.dump_mpi)) {
return false; // LCOV_EXCL_LINE
}
break;
return true;
}
case PGP_PKA_ECDSA:
case PGP_PKA_EDDSA:
case PGP_PKA_SM2: {
const ec_curve_desc_t *cdesc = get_curve_desc(key.material.ec.curve);
if (!obj_add_mpi_json(material, "p", key.material.ec.p, ctx->dump_mpi)) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}
if (!json_add(material, "curve", cdesc ? cdesc->pgp_name : "unknown")) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}
break;
}
case PGP_PKA_SM2:
case PGP_PKA_ECDH: {
const ec_curve_desc_t *cdesc = get_curve_desc(key.material.ec.curve);
if (!obj_add_mpi_json(material, "p", key.material.ec.p, ctx->dump_mpi)) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
auto &ec = dynamic_cast<const pgp::ECKeyMaterial &>(*material);
auto cdesc = get_curve_desc(ec.curve());
/* Common EC fields */
if (!obj_add_mpi_json(jso, "p", ec.p(), ctx.dump_mpi)) {
return false; // LCOV_EXCL_LINE
}
if (!json_add(material, "curve", cdesc ? cdesc->pgp_name : "unknown")) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
if (!json_add(jso, "curve", cdesc ? cdesc->pgp_name : "unknown")) {
return false; // LCOV_EXCL_LINE
}
if (!obj_add_intstr_json(
material, "hash algorithm", key.material.ec.kdf_hash_alg, hash_alg_map)) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
if (material->alg() != PGP_PKA_ECDH) {
return true;
}
/* ECDH-only fields */
auto &ecdh = dynamic_cast<const pgp::ECDHKeyMaterial &>(*material);
if (!obj_add_intstr_json(jso, "hash algorithm", ecdh.kdf_hash_alg(), hash_alg_map)) {
return false; // LCOV_EXCL_LINE
}
if (!obj_add_intstr_json(
material, "key wrap algorithm", key.material.ec.key_wrap_alg, symm_alg_map)) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
jso, "key wrap algorithm", ecdh.key_wrap_alg(), symm_alg_map)) {
return false; // LCOV_EXCL_LINE
}
break;
return true;
}
#if defined(ENABLE_CRYPTO_REFRESH)
case PGP_PKA_ED25519:
case PGP_PKA_X25519:
/* TODO */
break;
return true;
#endif
#if defined(ENABLE_PQC)
case PGP_PKA_KYBER768_X25519:
FALLTHROUGH_STATEMENT;
// TODO add case PGP_PKA_KYBER1024_X448: FALLTHROUGH_STATEMENT;
case PGP_PKA_KYBER768_P256:
FALLTHROUGH_STATEMENT;
case PGP_PKA_KYBER1024_P384:
FALLTHROUGH_STATEMENT;
case PGP_PKA_KYBER768_BP256:
FALLTHROUGH_STATEMENT;
case PGP_PKA_KYBER1024_BP384:
// TODO
break;
return true;
case PGP_PKA_DILITHIUM3_ED25519:
FALLTHROUGH_STATEMENT;
// TODO: add case PGP_PKA_DILITHIUM5_ED448: FALLTHROUGH_STATEMENT;
case PGP_PKA_DILITHIUM3_P256:
FALLTHROUGH_STATEMENT;
case PGP_PKA_DILITHIUM5_P384:
FALLTHROUGH_STATEMENT;
case PGP_PKA_DILITHIUM3_BP256:
FALLTHROUGH_STATEMENT;
case PGP_PKA_DILITHIUM5_BP384:
/* TODO */
break;
return true;
case PGP_PKA_SPHINCSPLUS_SHA2:
FALLTHROUGH_STATEMENT;
case PGP_PKA_SPHINCSPLUS_SHAKE:
/* TODO */
break;
return true;
#endif
default:
break;
return false;
}

Check notice

Code scanning / CodeQL

Long switch case Note

Switch has at least one case that is too long:
PGP_PKA_ECDH (60 lines)
.
src/lib/key_material.cpp Fixed Show fixed Hide fixed
src/lib/key_material.cpp Fixed Show fixed Hide fixed
@ni4 ni4 force-pushed the ni4-refactor-pgp_key_material_t branch from 55b0225 to 79bde7e Compare July 22, 2024 12:07
@ni4 ni4 force-pushed the ni4-refactor-pgp_key_material_t branch from 79bde7e to ac46b51 Compare July 22, 2024 13:40
Copy link

codecov bot commented Jul 22, 2024

Codecov Report

Attention: Patch coverage is 83.64706% with 139 lines in your changes missing coverage. Please review.

Project coverage is 83.75%. Comparing base (e3662a7) to head (878e89b).
Report is 10 commits behind head on main.

Files Patch % Lines
src/lib/key_material.cpp 81.40% 93 Missing ⚠️
src/lib/rnp.cpp 50.00% 16 Missing ⚠️
src/librekey/key_store_g10.cpp 86.66% 12 Missing ⚠️
src/librepgp/stream-dump.cpp 94.04% 5 Missing ⚠️
src/lib/key_material.hpp 84.61% 4 Missing ⚠️
src/lib/pgp-key.cpp 83.33% 4 Missing ⚠️
src/lib/crypto.cpp 60.00% 2 Missing ⚠️
src/librepgp/stream-key.cpp 88.23% 2 Missing ⚠️
src/lib/crypto/signatures.cpp 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2250      +/-   ##
==========================================
+ Coverage   83.58%   83.75%   +0.17%     
==========================================
  Files         107      113       +6     
  Lines       23163    23246      +83     
==========================================
+ Hits        19360    19470     +110     
+ Misses       3803     3776      -27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ni4 ni4 force-pushed the ni4-refactor-pgp_key_material_t branch 6 times, most recently from 6270dd6 to c5f6c60 Compare July 25, 2024 09:33
src/tests/key-protect.cpp Fixed Show fixed Hide fixed
src/tests/key-protect.cpp Fixed Show fixed Hide fixed
src/tests/key-protect.cpp Fixed Show fixed Hide fixed
@ni4 ni4 force-pushed the ni4-refactor-pgp_key_material_t branch 4 times, most recently from 36b9735 to f0d7691 Compare July 25, 2024 13:29
src/tests/key-protect.cpp Fixed Show fixed Hide fixed
src/tests/key-protect.cpp Fixed Show fixed Hide fixed
@ni4 ni4 force-pushed the ni4-refactor-pgp_key_material_t branch from 487f57e to bc71992 Compare July 26, 2024 12:48
@ni4 ni4 marked this pull request as ready for review July 26, 2024 13:37
@ni4
Copy link
Contributor Author

ni4 commented Jul 26, 2024

@falko-strenzke @TJ-91 Possibly you'd want to take a look at this PR as it changes some of the PQC code.

Copy link
Contributor

@falko-strenzke falko-strenzke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice move towards more object orientation 👍 I left some comments.

}

bool
KeyMaterial::equals(const KeyMaterial &value) noexcept
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function should be const as well.

void set_validity(const pgp_validity_t &val);
void reset_validity();
bool valid() const;
virtual bool equals(const KeyMaterial &value) noexcept;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function hould be const

virtual void clear_secret();
virtual bool parse(pgp_packet_body_t &pkt) noexcept = 0;
virtual bool parse_secret(pgp_packet_body_t &pkt) noexcept = 0;
virtual void write(pgp_packet_body_t &pkt) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function should be `const` ?

virtual void write(pgp_packet_body_t &pkt) = 0;
virtual void write_secret(pgp_packet_body_t &pkt) = 0;
virtual bool generate(const rnp_keygen_crypto_params_t &params);
virtual rnp_result_t encrypt(rnp::SecurityContext & ctx,
Copy link
Contributor

@falko-strenzke falko-strenzke Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All crypto functions (en- decrypt, sign, verify) should be const, shouldn't they?
One exception could indeed be sign(), as for a stateful hash-based signature scheme the key would actually change during signing. However, stateful hash-based schemes have not been standardized for OpenPGP and most likely never will be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing at all this! Updated code with const, will push once addressed other comments.

#endif

#if defined(ENABLE_PQC)
class KyberKeyMaterial : public KeyMaterial {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly, this class should be named to reflect the algorithm, for instance something like "KyberWithECDH". Otherwise, if pure Kyber/ML-KEM is ever added, there will be confusion and naming conflicts.

It might also be a good chance here to start introducing the NIST names, i.e., name the class "MLKEMWithECDH". We plan to rename the algorithms to the NIST names throughout the code-base as well once we reach a state where all PQC-related PRs merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would MLKEMECDHKeyMaterial look good to you? Okay, it doesn't look good at all with 9 letters at start, but would it look comparable well as MLKEMWithECDHKeyMaterial? Just don't want to have too long names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to save the "with" I suggest to use MlkemEcdhKeyMaterial.

const pgp_kyber_ecdh_composite_private_key_t &priv() const noexcept;
};

class DilithiumKeyMaterial : public KeyMaterial {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned for Kyber, also this class should be name "DilithiumWithECC" or similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would DilithiumECCKeyMaterial look good to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if you choose my suggestion for ML-KEM above, then it might be more consistent to use DilithiumEccKeyMaterial.

const pgp_dilithium_exdsa_composite_private_key_t &priv() const noexcept;
};

class SphincsPlusKeyMaterial : public KeyMaterial {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to call it "SLHDSAKeyMaterial".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on this, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, for a consistent camel case naming it could rather be SlhdsaKeyMaterial also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@falko-strenzke Done, thanks for the suggestions!

@ni4 ni4 force-pushed the ni4-refactor-pgp_key_material_t branch from bc71992 to e43ffcf Compare July 30, 2024 09:29
@ni4 ni4 requested a review from falko-strenzke July 30, 2024 09:29
@ni4
Copy link
Contributor Author

ni4 commented Aug 3, 2024

@ronaldtse @desvxx Ping for review.

@ni4 ni4 force-pushed the ni4-refactor-pgp_key_material_t branch from e43ffcf to 878e89b Compare August 22, 2024 15:33
@ni4 ni4 requested review from maxirmx and falko-strenzke and removed request for falko-strenzke August 22, 2024 18:24
Copy link
Contributor

@ronaldtse ronaldtse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ni4 !

@ronaldtse ronaldtse merged commit 1aa98ed into main Aug 24, 2024
123 checks passed
@ronaldtse ronaldtse deleted the ni4-refactor-pgp_key_material_t branch August 24, 2024 02:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants