Skip to content

Commit 3fe4fb5

Browse files
committed
fix pqc signature hash binding
1 parent d47072e commit 3fe4fb5

File tree

8 files changed

+53
-28
lines changed

8 files changed

+53
-28
lines changed

src/lib/crypto/dilithium.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626

2727
#include "dilithium.h"
28+
#include "logging.h"
29+
#include "types.h"
2830
#include <cassert>
2931

3032
namespace {
@@ -119,19 +121,45 @@ pgp_dilithium_private_key_t::is_valid(rnp::RNG *rng) const
119121
}
120122

121123
bool
122-
dilithium_hash_allowed(pgp_hash_alg_t hash_alg)
124+
dilithium_hash_allowed(pgp_pubkey_alg_t pk_alg, pgp_hash_alg_t hash_alg)
123125
{
124-
switch (hash_alg) {
125-
case PGP_HASH_SHA3_256:
126-
case PGP_HASH_SHA3_512:
127-
return true;
126+
switch (pk_alg) {
127+
case PGP_PKA_DILITHIUM3_ED25519:
128+
FALLTHROUGH_STATEMENT;
129+
case PGP_PKA_DILITHIUM3_P256:
130+
FALLTHROUGH_STATEMENT;
131+
case PGP_PKA_DILITHIUM3_BP256:
132+
return hash_alg == PGP_HASH_SHA3_256;
133+
case PGP_PKA_DILITHIUM5_ED448:
134+
FALLTHROUGH_STATEMENT;
135+
case PGP_PKA_DILITHIUM5_P384:
136+
FALLTHROUGH_STATEMENT;
137+
case PGP_PKA_DILITHIUM5_BP384:
138+
return hash_alg == PGP_HASH_SHA3_512;
128139
default:
129-
return false;
140+
RNP_LOG("invalid algorithm ID given");
141+
throw rnp::rnp_exception(RNP_ERROR_BAD_STATE);
130142
}
131143
}
132144

133145
pgp_hash_alg_t
134-
dilithium_default_hash_alg()
146+
dilithium_default_hash_alg(pgp_pubkey_alg_t pk_alg)
135147
{
136-
return PGP_HASH_SHA3_256;
148+
switch (pk_alg) {
149+
case PGP_PKA_DILITHIUM3_ED25519:
150+
FALLTHROUGH_STATEMENT;
151+
case PGP_PKA_DILITHIUM3_P256:
152+
FALLTHROUGH_STATEMENT;
153+
case PGP_PKA_DILITHIUM3_BP256:
154+
return PGP_HASH_SHA3_256;
155+
case PGP_PKA_DILITHIUM5_ED448:
156+
FALLTHROUGH_STATEMENT;
157+
case PGP_PKA_DILITHIUM5_P384:
158+
FALLTHROUGH_STATEMENT;
159+
case PGP_PKA_DILITHIUM5_BP384:
160+
return PGP_HASH_SHA3_512;
161+
default:
162+
RNP_LOG("invalid algorithm ID given");
163+
throw rnp::rnp_exception(RNP_ERROR_BAD_STATE);
164+
}
137165
}

src/lib/crypto/dilithium.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ class pgp_dilithium_public_key_t {
109109
std::pair<pgp_dilithium_public_key_t, pgp_dilithium_private_key_t> dilithium_generate_keypair(
110110
rnp::RNG *rng, dilithium_parameter_e dilithium_param);
111111

112-
bool dilithium_hash_allowed(pgp_hash_alg_t hash_alg);
112+
bool dilithium_hash_allowed(pgp_pubkey_alg_t pk_alg, pgp_hash_alg_t hash_alg);
113113

114-
pgp_hash_alg_t dilithium_default_hash_alg();
114+
pgp_hash_alg_t dilithium_default_hash_alg(pgp_pubkey_alg_t pk_alg);
115115

116116
#endif

src/lib/crypto/sphincsplus.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,6 @@ pgp_sphincsplus_generate(rnp::RNG *rng, pgp_sphincsplus_key_t *material, pgp_pub
148148
return RNP_SUCCESS;
149149
}
150150

151-
bool
152-
pgp_sphincsplus_public_key_t::validate_signature_hash_requirements(
153-
pgp_hash_alg_t hash_alg) const
154-
{
155-
/* check if key is allowed with the hash algorithm */
156-
return sphincsplus_hash_allowed(pk_alg_, hash_alg);
157-
}
158-
159151
bool
160152
pgp_sphincsplus_public_key_t::is_valid(rnp::RNG *rng) const
161153
{
@@ -244,7 +236,7 @@ sphincsplus_hash_allowed(pgp_pubkey_alg_t pk_alg, pgp_hash_alg_t hash_alg)
244236
return hash_alg == PGP_HASH_SHA3_512;
245237
default:
246238
RNP_LOG("invalid algorithm ID given");
247-
throw rnp::rnp_exception(RNP_ERROR_BAD_PARAMETERS);
239+
throw rnp::rnp_exception(RNP_ERROR_BAD_STATE);
248240
}
249241
}
250242

@@ -260,6 +252,6 @@ sphincsplus_default_hash_alg(pgp_pubkey_alg_t alg)
260252
return PGP_HASH_SHA3_512;
261253
default:
262254
RNP_LOG("invalid algorithm ID given");
263-
throw rnp::rnp_exception(RNP_ERROR_BAD_PARAMETERS);
255+
throw rnp::rnp_exception(RNP_ERROR_BAD_STATE);
264256
}
265257
}

src/lib/crypto/sphincsplus.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ class pgp_sphincsplus_public_key_t {
105105

106106
bool is_valid(rnp::RNG *rng) const;
107107

108-
bool validate_signature_hash_requirements(pgp_hash_alg_t hash_alg) const;
109-
110108
pgp_pubkey_alg_t
111109
alg() const
112110
{

src/lib/generate-key.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pgp_check_key_hash_requirements(const rnp_keygen_crypto_params_t &crypto)
232232
case PGP_PKA_DILITHIUM3_BP256:
233233
FALLTHROUGH_STATEMENT;
234234
case PGP_PKA_DILITHIUM5_BP384:
235-
if (!dilithium_hash_allowed(crypto.hash_alg)) {
235+
if (!dilithium_hash_allowed(crypto.key_alg, crypto.hash_alg)) {
236236
return false;
237237
}
238238
break;

src/lib/key_material.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,13 @@ DilithiumEccKeyMaterial::sign(rnp::SecurityContext & ctx,
20162016
pgp_hash_alg_t
20172017
DilithiumEccKeyMaterial::adjust_hash(pgp_hash_alg_t hash) const
20182018
{
2019-
return dilithium_default_hash_alg();
2019+
return dilithium_default_hash_alg(alg());
2020+
}
2021+
2022+
bool
2023+
DilithiumEccKeyMaterial::sig_hash_allowed(pgp_hash_alg_t hash) const
2024+
{
2025+
return dilithium_hash_allowed(alg(), hash);
20202026
}
20212027

20222028
size_t
@@ -2150,7 +2156,7 @@ SlhdsaKeyMaterial::adjust_hash(pgp_hash_alg_t hash) const
21502156
bool
21512157
SlhdsaKeyMaterial::sig_hash_allowed(pgp_hash_alg_t hash) const
21522158
{
2153-
return key_.pub.validate_signature_hash_requirements(hash);
2159+
return sphincsplus_hash_allowed(alg(), hash);
21542160
}
21552161

21562162
size_t

src/lib/key_material.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ class DilithiumEccKeyMaterial : public KeyMaterial {
529529
pgp_signature_material_t & sig,
530530
const rnp::secure_vector<uint8_t> &hash) const override;
531531
pgp_hash_alg_t adjust_hash(pgp_hash_alg_t hash) const override;
532+
bool sig_hash_allowed(pgp_hash_alg_t hash) const override;
532533
size_t bits() const noexcept override;
533534

534535
const pgp_dilithium_exdsa_composite_public_key_t & pub() const noexcept;

src/rnpkeys/tui.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ rnpkeys_ask_generate_params(rnp_cfg &cfg, FILE *input_fp)
348348
break;
349349
case 26:
350350
cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_DILITHIUM5_ED448);
351-
cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SHA3_256);
351+
cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SHA3_512);
352352
cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_KYBER1024_X448);
353353
cfg.set_str(CFG_KG_V6_KEY, "true");
354354
break;
@@ -360,7 +360,7 @@ rnpkeys_ask_generate_params(rnp_cfg &cfg, FILE *input_fp)
360360
break;
361361
case 28:
362362
cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_DILITHIUM5_P384);
363-
cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SHA3_256);
363+
cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SHA3_512);
364364
cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_KYBER1024_P384);
365365
cfg.set_str(CFG_KG_V6_KEY, "true");
366366
break;
@@ -372,7 +372,7 @@ rnpkeys_ask_generate_params(rnp_cfg &cfg, FILE *input_fp)
372372
break;
373373
case 30:
374374
cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_DILITHIUM5_BP384);
375-
cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SHA3_256);
375+
cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SHA3_512);
376376
cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_KYBER1024_BP384);
377377
cfg.set_str(CFG_KG_V6_KEY, "true");
378378
break;

0 commit comments

Comments
 (0)