-
Notifications
You must be signed in to change notification settings - Fork 57
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
Conversation
src/librepgp/stream-dump.cpp
Outdated
!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
PGP_PKA_ECDH (60 lines)
55b0225
to
79bde7e
Compare
79bde7e
to
ac46b51
Compare
Codecov ReportAttention: Patch coverage is
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. |
6270dd6
to
c5f6c60
Compare
36b9735
to
f0d7691
Compare
487f57e
to
bc71992
Compare
@falko-strenzke @TJ-91 Possibly you'd want to take a look at this PR as it changes some of the PQC code. |
There was a problem hiding this 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.
src/lib/key_material.cpp
Outdated
} | ||
|
||
bool | ||
KeyMaterial::equals(const KeyMaterial &value) noexcept |
There was a problem hiding this comment.
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.
src/lib/key_material.hpp
Outdated
void set_validity(const pgp_validity_t &val); | ||
void reset_validity(); | ||
bool valid() const; | ||
virtual bool equals(const KeyMaterial &value) noexcept; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function hould be const
src/lib/key_material.hpp
Outdated
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; |
There was a problem hiding this comment.
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 ¶ms); | ||
virtual rnp_result_t encrypt(rnp::SecurityContext & ctx, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/lib/key_material.hpp
Outdated
#endif | ||
|
||
#if defined(ENABLE_PQC) | ||
class KyberKeyMaterial : public KeyMaterial { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
src/lib/key_material.hpp
Outdated
const pgp_kyber_ecdh_composite_private_key_t &priv() const noexcept; | ||
}; | ||
|
||
class DilithiumKeyMaterial : public KeyMaterial { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
src/lib/key_material.hpp
Outdated
const pgp_dilithium_exdsa_composite_private_key_t &priv() const noexcept; | ||
}; | ||
|
||
class SphincsPlusKeyMaterial : public KeyMaterial { |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree on this, thanks!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
bc71992
to
e43ffcf
Compare
@ronaldtse @desvxx Ping for review. |
e43ffcf
to
878e89b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @ni4 !
To avoid anonymous opaque unions and bring more flexibility to the codebase.