Skip to content

Commit 39ccb42

Browse files
Check parameters before comparing pqdsa public keys
1 parent 9651480 commit 39ccb42

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

crypto/evp_extra/p_kem_asn1.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ static int kem_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
8686
return 1;
8787
}
8888

89+
// kem_cmp_parameters returns 1 if |a| and |b| hold populated KEM keys with
90+
// the same KEM NID, 0 if their NIDs differ, or -2 if either operand is
91+
// missing its key or parameters. The tri-state return aligns with the
92+
// |EVP_PKEY_cmp| convention (1 = equal, 0 = not equal, negative = error).
8993
static int kem_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
94+
if (a == NULL || b == NULL) {
95+
return -2;
96+
}
9097
const KEM_KEY *a_key = a->pkey.kem_key;
9198
const KEM_KEY *b_key = b->pkey.kem_key;
9299
if (a_key == NULL || b_key == NULL) {

crypto/evp_extra/p_pqdsa_asn1.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,42 @@ static int pqdsa_pub_encode(CBB *out, const EVP_PKEY *pkey) {
137137
return 1;
138138
}
139139

140+
// pqdsa_cmp_parameters returns 1 if |a| and |b| hold populated PQDSA keys
141+
// with the same ML-DSA NID, 0 if their NIDs differ, or -2 if either operand
142+
// is missing its key or parameters. The tri-state return aligns with the
143+
// |EVP_PKEY_cmp| convention (1 = equal, 0 = not equal, negative = error).
144+
static int pqdsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
145+
if (a == NULL || b == NULL) {
146+
return -2;
147+
}
148+
const PQDSA_KEY *a_key = a->pkey.pqdsa_key;
149+
const PQDSA_KEY *b_key = b->pkey.pqdsa_key;
150+
if (a_key == NULL || b_key == NULL) {
151+
return -2;
152+
}
153+
154+
const PQDSA *a_pqdsa = a_key->pqdsa;
155+
const PQDSA *b_pqdsa = b_key->pqdsa;
156+
if (a_pqdsa == NULL || b_pqdsa == NULL) {
157+
return -2;
158+
}
159+
160+
return a_pqdsa->nid == b_pqdsa->nid;
161+
}
162+
140163
static int pqdsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
141-
PQDSA_KEY *a_key = a->pkey.pqdsa_key;
142-
PQDSA_KEY *b_key = b->pkey.pqdsa_key;
164+
int ret = pqdsa_cmp_parameters(a, b);
165+
if (ret <= 0) {
166+
return ret;
167+
}
143168

144-
return OPENSSL_memcmp(a_key->public_key,
145-
b_key->public_key,
146-
a->pkey.pqdsa_key->pqdsa->public_key_len) == 0;
169+
const PQDSA_KEY *a_key = a->pkey.pqdsa_key;
170+
const PQDSA_KEY *b_key = b->pkey.pqdsa_key;
171+
if (a_key->public_key == NULL || b_key->public_key == NULL) {
172+
return -2;
173+
}
174+
return OPENSSL_memcmp(a_key->public_key, b_key->public_key,
175+
a_key->pqdsa->public_key_len) == 0;
147176
}
148177

149178
static int pqdsa_priv_decode(EVP_PKEY *out, CBS *oid, CBS *params, CBS *key, CBS *pubkey) {

0 commit comments

Comments
 (0)