Skip to content

Commit

Permalink
length and null checks in en/decaps (open-quantum-safe#364)
Browse files Browse the repository at this point in the history
* length and null checks in en/decaps

Signed-off-by: Felipe Ventura <[email protected]>
  • Loading branch information
bencemali authored and feventura committed Mar 17, 2024
1 parent faacdbb commit ddaadc6
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions oqsprov/oqs_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,42 @@ static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out,
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n");
return -1;
}
*outlen = kem_ctx->length_ciphertext;
*secretlen = kem_ctx->length_shared_secret;
if (pkemctx->kem->comp_pubkey == NULL
|| pkemctx->kem->comp_pubkey[keyslot] == NULL) {
OQS_KEM_PRINTF("OQS Warning: public key is NULL\n");
return -1;
}
if (out == NULL || secret == NULL) {
OQS_KEM_PRINTF3("KEM returning lengths %ld and %ld\n", *outlen,
*secretlen);
if (outlen != NULL) {
*outlen = kem_ctx->length_ciphertext;
}
if (secretlen != NULL) {
*secretlen = kem_ctx->length_shared_secret;
}
OQS_KEM_PRINTF3("KEM returning lengths %ld and %ld\n",
kem_ctx->length_ciphertext,
kem_ctx->length_shared_secret);
return 1;
}
if (outlen == NULL) {
OQS_KEM_PRINTF("OQS Warning: outlen is NULL\n");
return -1;
}
if (secretlen == NULL) {
OQS_KEM_PRINTF("OQS Warning: secretlen is NULL\n");
return -1;
}
if (*outlen < kem_ctx->length_ciphertext) {
OQS_KEM_PRINTF("OQS Warning: out buffer too small\n");
return -1;
}
if (*secretlen < kem_ctx->length_shared_secret) {
OQS_KEM_PRINTF("OQS Warning: secret buffer too small\n");
return -1;
}
*outlen = kem_ctx->length_ciphertext;
*secretlen = kem_ctx->length_shared_secret;

return OQS_SUCCESS
== OQS_KEM_encaps(kem_ctx, out, secret,
pkemctx->kem->comp_pubkey[keyslot]);
Expand All @@ -140,9 +169,36 @@ static int oqs_qs_kem_decaps_keyslot(void *vpkemctx, unsigned char *out,
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n");
return -1;
}
*outlen = kem_ctx->length_shared_secret;
if (out == NULL)
if (pkemctx->kem->comp_privkey == NULL
|| pkemctx->kem->comp_privkey[keyslot] == NULL) {
OQS_KEM_PRINTF("OQS Warning: private key is NULL\n");
return -1;
}
if (out == NULL) {
if (outlen != NULL) {
*outlen = kem_ctx->length_shared_secret;
}
OQS_KEM_PRINTF2("KEM returning length %ld\n",
kem_ctx->length_shared_secret);
return 1;
}
if (inlen != kem_ctx->length_ciphertext) {
OQS_KEM_PRINTF("OQS Warning: wrong input length\n");
return 0;
}
if (in == NULL) {
OQS_KEM_PRINTF("OQS Warning: in is NULL\n");
return -1;
}
if (outlen == NULL) {
OQS_KEM_PRINTF("OQS Warning: outlen is NULL\n");
return -1;
}
if (*outlen < kem_ctx->length_shared_secret) {
OQS_KEM_PRINTF("OQS Warning: out buffer too small\n");
return -1;
}
*outlen = kem_ctx->length_shared_secret;

return OQS_SUCCESS
== OQS_KEM_decaps(kem_ctx, out, in,
Expand Down

0 comments on commit ddaadc6

Please sign in to comment.