Skip to content

Commit

Permalink
Remove old pgp_encrypted_material_t code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Dec 25, 2024
1 parent 05cc17a commit 4a5ed2b
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 224 deletions.
1 change: 0 additions & 1 deletion src/lib/key_material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "enc_material.hpp"

typedef struct pgp_packet_body_t pgp_packet_body_t;
typedef struct pgp_encrypted_material_t pgp_encrypted_material_t;
typedef struct pgp_signature_material_t pgp_signature_material_t;

namespace pgp {
Expand Down
17 changes: 0 additions & 17 deletions src/lib/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,6 @@ typedef struct pgp_signature_material_t {
pgp_hash_alg_t halg;
} pgp_signature_material_t;

/**
* Type to keep pk-encrypted data without any openpgp-dependent data.
*/
typedef struct pgp_encrypted_material_t {
/* Temporary: will be replaced by class */
pgp::rsa::Encrypted rsa;
pgp::eg::Encrypted eg;
pgp::sm2::Encrypted sm2;
pgp::ecdh::Encrypted ecdh;
#if defined(ENABLE_CRYPTO_REFRESH)
pgp_x25519_encrypted_t x25519; // non-trivial type cannot be member in union
#endif
#if defined(ENABLE_PQC)
pgp_kyber_ecdh_encrypted_t kyber_ecdh; // non-trivial type cannot be member in union
#endif
} pgp_encrypted_material_t;

typedef struct pgp_s2k_t {
pgp_s2k_usage_t usage{};

Expand Down
203 changes: 1 addition & 202 deletions src/librepgp/stream-packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,149 +1184,12 @@ pgp_pk_sesskey_t::parse(pgp_source_t &src)
/* we cannot fail here */
pkt.get(material_buf.data(), material_buf.size());
/* check whether it can be parsed */
pgp_encrypted_material_t material = {};
if (!parse_material(material)) {
if (!parse_material()) {
return RNP_ERROR_BAD_FORMAT;
}
return RNP_SUCCESS;
}

bool
pgp_pk_sesskey_t::parse_material(pgp_encrypted_material_t &material)
{
pgp_packet_body_t pkt(material_buf.data(), material_buf.size());
switch (alg) {
case PGP_PKA_RSA:
case PGP_PKA_RSA_ENCRYPT_ONLY:
/* RSA m */
if (!pkt.get(material.rsa.m)) {
RNP_LOG("failed to get rsa m");
return false;
}
break;
case PGP_PKA_ELGAMAL:
case PGP_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
/* ElGamal g, m */
if (!pkt.get(material.eg.g) || !pkt.get(material.eg.m)) {
RNP_LOG("failed to get elgamal mpis");
return false;
}
break;
case PGP_PKA_SM2:
/* SM2 m */
if (!pkt.get(material.sm2.m)) {
RNP_LOG("failed to get sm2 m");
return false;
}
break;
case PGP_PKA_ECDH: {
/* ECDH ephemeral point */
if (!pkt.get(material.ecdh.p)) {
RNP_LOG("failed to get ecdh p");
return false;
}
/* ECDH m */
uint8_t bt = 0;
if (!pkt.get(bt)) {
RNP_LOG("failed to get ecdh m len");
return false;
}
if (bt > ECDH_WRAPPED_KEY_SIZE) {
RNP_LOG("wrong ecdh m len");
return false;
}
material.ecdh.m.resize(bt);
if (!pkt.get(material.ecdh.m, bt)) {
RNP_LOG("failed to get ecdh m len");
return false;
}
break;
}
#if defined(ENABLE_CRYPTO_REFRESH)
case PGP_PKA_X25519: {
uint8_t bt = 0;
auto ec_desc = pgp::ec::Curve::get(PGP_CURVE_25519);
material.x25519.eph_key.resize(ec_desc->bytes());
if (!pkt.get(material.x25519.eph_key.data(), material.x25519.eph_key.size())) {
RNP_LOG("failed to parse X25519 PKESK (eph. pubkey)");
return false;
}
uint8_t enc_sesskey_len;
if (!pkt.get(enc_sesskey_len)) {
RNP_LOG("failed to parse X25519 PKESK (enc sesskey length)");
return false;
}
/* get plaintext salg if PKESKv3 */
if ((version == PGP_PKSK_V3) && !do_encrypt_pkesk_v3_alg_id(alg)) {
if (!pkt.get(bt)) {
RNP_LOG("failed to get salg");
return RNP_ERROR_BAD_FORMAT;
}
enc_sesskey_len -= 1;
salg = (pgp_symm_alg_t) bt;
}
material.x25519.enc_sess_key.resize(enc_sesskey_len);
if (!pkt.get(material.x25519.enc_sess_key.data(), enc_sesskey_len)) {
RNP_LOG("failed to parse X25519 PKESK (enc sesskey)");
return false;
}
break;
}
#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: {
uint8_t wrapped_key_len = 0;
uint8_t bt = 0;
material.kyber_ecdh.composite_ciphertext.resize(
pgp_kyber_ecdh_encrypted_t::composite_ciphertext_size(alg));
if (!pkt.get(material.kyber_ecdh.composite_ciphertext.data(),
material.kyber_ecdh.composite_ciphertext.size())) {
RNP_LOG("failed to get kyber-ecdh ciphertext");
return false;
}
if (!pkt.get(wrapped_key_len)) {
RNP_LOG("failed to get kyber-ecdh wrapped session key length");
return false;
}
/* get plaintext salg if PKESKv3 */
if ((version == PGP_PKSK_V3) && !do_encrypt_pkesk_v3_alg_id(alg)) {
if (!pkt.get(bt)) {
RNP_LOG("failed to get salg");
return RNP_ERROR_BAD_FORMAT;
}
salg = (pgp_symm_alg_t) bt;
wrapped_key_len--;
}
material.kyber_ecdh.wrapped_sesskey.resize(wrapped_key_len);
if (!pkt.get(material.kyber_ecdh.wrapped_sesskey.data(),
material.kyber_ecdh.wrapped_sesskey.size())) {
RNP_LOG("failed to get kyber-ecdh session key");
return false;
}
break;
}
#endif
default:
RNP_LOG("unknown pk alg %d", (int) alg);
return false;
}

if (pkt.left()) {
RNP_LOG("extra %d bytes in pk packet", (int) pkt.left());
return false;
}
return true;
}

std::unique_ptr<pgp::EncMaterial>
pgp_pk_sesskey_t::parse_material() const
{
Expand All @@ -1352,70 +1215,6 @@ pgp_pk_sesskey_t::write_material(const pgp::EncMaterial &material)
material_buf.assign(pktbody.data(), pktbody.data() + pktbody.size());
}

void
pgp_pk_sesskey_t::write_material(const pgp_encrypted_material_t &material)
{
pgp_packet_body_t pktbody(PGP_PKT_PK_SESSION_KEY);

switch (alg) {
case PGP_PKA_RSA:
case PGP_PKA_RSA_ENCRYPT_ONLY:
pktbody.add(material.rsa.m);
break;
case PGP_PKA_SM2:
pktbody.add(material.sm2.m);
break;
case PGP_PKA_ECDH:
pktbody.add(material.ecdh.p);
pktbody.add_byte(material.ecdh.m.size());
pktbody.add(material.ecdh.m);
break;
case PGP_PKA_ELGAMAL:
pktbody.add(material.eg.g);
pktbody.add(material.eg.m);
break;
#if defined(ENABLE_CRYPTO_REFRESH)
case PGP_PKA_X25519: {
uint8_t enc_sesskey_length_offset = ((version == PGP_PKSK_V3) ? 1 : 0);
pktbody.add(material.x25519.eph_key);
pktbody.add_byte(static_cast<uint8_t>(material.x25519.enc_sess_key.size() +
enc_sesskey_length_offset));
if (version == PGP_PKSK_V3) {
pktbody.add_byte(salg); /* added as plaintext */
}
pktbody.add(material.x25519.enc_sess_key);
break;
}
#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: {
pktbody.add(material.kyber_ecdh.composite_ciphertext);
uint8_t opt_salg_length = (version == PGP_PKSK_V3) ? 1 : 0;
pktbody.add_byte(static_cast<uint8_t>(material.kyber_ecdh.wrapped_sesskey.size()) +
opt_salg_length);
if (version == PGP_PKSK_V3) {
pktbody.add_byte(salg); /* added as plaintext */
}
pktbody.add(material.kyber_ecdh.wrapped_sesskey);
break;
}
#endif
default:
RNP_LOG("Unknown pk alg: %d", (int) alg);
throw rnp::rnp_exception(RNP_ERROR_BAD_PARAMETERS);
}
material_buf = {pktbody.data(), pktbody.data() + pktbody.size()};
}

void
pgp_one_pass_sig_t::write(pgp_dest_t &dst) const
{
Expand Down
5 changes: 1 addition & 4 deletions src/librepgp/stream-packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,13 @@ typedef struct pgp_pk_sesskey_t {
rnp_result_t parse(pgp_source_t &src);
/**
* @brief Parse encrypted material which is stored in packet in raw.
* @param material on success parsed material will be stored here.
* @return true on success or false otherwise. May also throw an exception.
* @return Parsed material or nullptr. May also throw an exception.
*/
bool parse_material(pgp_encrypted_material_t &material);
std::unique_ptr<pgp::EncMaterial> parse_material() const;
/**
* @brief Write encrypted material to the material_buf.
* @param material populated encrypted material.
*/
void write_material(const pgp_encrypted_material_t &material);
void write_material(const pgp::EncMaterial &material);
} pgp_pk_sesskey_t;

Expand Down

0 comments on commit 4a5ed2b

Please sign in to comment.