Skip to content

Commit 8b14bb4

Browse files
Fix NULL pointer crash in OQS KEM encaps/decaps error handling (#184)
* Fix NULL pointer crash in OQS KEM encaps/decaps error handling Signed-off-by: Andrew Younkers <[email protected]> * initialize r at top of functions Signed-off-by: Andrew Younkers <[email protected]> --------- Signed-off-by: Andrew Younkers <[email protected]>
1 parent 3eca538 commit 8b14bb4

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

kexoqs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int kex_kem_generic_keypair(OQS_KEM *kem, struct kex *kex)
4242
{
4343
struct sshbuf *buf = NULL;
4444
u_char *cp = NULL;
45-
int r;
45+
int r = SSH_ERR_INTERNAL_ERROR;
4646
if ((buf = sshbuf_new()) == NULL) {
4747
return SSH_ERR_ALLOC_FAIL;
4848
}
@@ -71,7 +71,7 @@ static int kex_kem_generic_enc(OQS_KEM *kem, struct kex *kex,
7171
struct sshbuf *buf = NULL;
7272
const u_char *client_pub;
7373
u_char *kem_key = NULL, *ciphertext;
74-
int r;
74+
int r = SSH_ERR_INTERNAL_ERROR;
7575
*server_blobp = NULL;
7676
*shared_secretp = NULL;
7777
if (sshbuf_len(client_blob) != kem->length_public_key) {
@@ -125,7 +125,7 @@ static int kex_kem_generic_dec(OQS_KEM *kem,
125125
struct sshbuf *buf = NULL;
126126
u_char *kem_key = NULL;
127127
const u_char *ciphertext;
128-
int r;
128+
int r = SSH_ERR_INTERNAL_ERROR;
129129
*shared_secretp = NULL;
130130
if (sshbuf_len(server_blob) != kem->length_ciphertext) {
131131
r = SSH_ERR_SIGNATURE_INVALID;

kexoqsecdh.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static int kex_kem_generic_with_ec_enc(OQS_KEM *kem,
184184
struct sshbuf *ecdh_server_blob = NULL;
185185
struct sshbuf *ecdh_shared_secret;
186186
u_char hash[SSH_DIGEST_MAX_LENGTH];
187-
int r;
187+
int r = SSH_ERR_INTERNAL_ERROR;
188188
*server_blobp = NULL;
189189
*shared_secretp = NULL;
190190

@@ -220,6 +220,7 @@ static int kex_kem_generic_with_ec_enc(OQS_KEM *kem,
220220

221221
/* generate and encrypt KEM key with client key */
222222
if (OQS_KEM_encaps(kem, ciphertext, kem_key, client_pub) != OQS_SUCCESS) {
223+
r = SSH_ERR_LIBCRYPTO_ERROR;
223224
goto out;
224225
}
225226

@@ -294,7 +295,7 @@ static int kex_kem_generic_with_ec_dec(OQS_KEM *kem,
294295
struct sshbuf *ecdh_shared_secret;
295296
struct sshbuf *ecdh_server_blob = NULL;
296297
u_char hash[SSH_DIGEST_MAX_LENGTH];
297-
int r;
298+
int r = SSH_ERR_INTERNAL_ERROR;
298299
*shared_secretp = NULL;
299300

300301
/* server_blob contains both KEM and ECDH server keys */
@@ -319,6 +320,7 @@ static int kex_kem_generic_with_ec_dec(OQS_KEM *kem,
319320
goto out;
320321
/* decapsulate the post-quantum secret */
321322
if (OQS_KEM_decaps(kem, kem_key, ciphertext, kex->oqs_client_key) != OQS_SUCCESS) {
323+
r = SSH_ERR_LIBCRYPTO_ERROR;
322324
goto out;
323325
}
324326

kexoqsx25519.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static int kex_kem_generic_with_x25519_enc(OQS_KEM *kem, struct kex *kex,
8282
u_char server_key[CURVE25519_SIZE];
8383
u_char hash[SSH_DIGEST_MAX_LENGTH];
8484
size_t needed = 0;
85-
int r;
85+
int r = SSH_ERR_INTERNAL_ERROR;
8686

8787
*server_blobp = NULL;
8888
*shared_secretp = NULL;
@@ -116,6 +116,7 @@ static int kex_kem_generic_with_x25519_enc(OQS_KEM *kem, struct kex *kex,
116116
/* generate and encrypt KEM key with client key */
117117
if (OQS_KEM_encaps(kem, public_key, private_key, client_pub)
118118
!= OQS_SUCCESS) {
119+
r = SSH_ERR_LIBCRYPTO_ERROR;
119120
goto out;
120121
}
121122
client_pub += kem->length_public_key;
@@ -162,7 +163,7 @@ static int kex_kem_generic_with_x25519_dec(OQS_KEM *kem, struct kex *kex,
162163
size_t needed = 0;
163164
/* x25519 values */
164165
u_char hash[SSH_DIGEST_MAX_LENGTH];
165-
int r;
166+
int r = SSH_ERR_INTERNAL_ERROR;
166167

167168
*shared_secretp = NULL;
168169

@@ -183,6 +184,7 @@ static int kex_kem_generic_with_x25519_dec(OQS_KEM *kem, struct kex *kex,
183184
/* decapsulate the post-quantum secret */
184185
if (OQS_KEM_decaps(kem, private_key, public_key,
185186
kex->oqs_client_key) != OQS_SUCCESS) {
187+
r = SSH_ERR_LIBCRYPTO_ERROR;
186188
goto out;
187189
}
188190
public_key += kem->length_ciphertext;

0 commit comments

Comments
 (0)