Skip to content

Commit

Permalink
fixup! Implement cmp KEM combiner and cmp KEM encaps/decaps fns
Browse files Browse the repository at this point in the history
Signed-off-by: Pravek Sharma <[email protected]>
  • Loading branch information
praveksharma committed Dec 3, 2024
1 parent 89b6b0c commit 8fb226b
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions oqsprov/oqs_hyb_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ static int oqs_evp_kem_encaps_keyslot(void *vpkemctx, unsigned char *ct,
kexDeriveLen = evp_ctx->evp_info->kex_length_secret;

if (keytype == EVP_PKEY_RSA) {
*ctlen = evp_ctx->evp_info->kex_length_secret;
*secretlen = 32;
if (ct == NULL || secret == NULL) {
OQS_KEM_PRINTF3("EVP KEM returning lengths %ld and %ld\n", *ctlen,
*secretlen);
return 1;
}
// *ctlen = evp_ctx->evp_info->kex_length_secret;
*secretlen = (size_t)32;

pkey = d2i_PublicKey(keytype, NULL, (const unsigned char **)&pubkey_kex,
pubkey_kexlen);
Expand All @@ -70,14 +65,23 @@ static int oqs_evp_kem_encaps_keyslot(void *vpkemctx, unsigned char *ct,
ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, NULL, 0);
ON_ERR_SET_GOTO(ret <= 0, ret, -1, err);

ret = EVP_PKEY_encrypt(ctx, NULL, ctlen, NULL, *secretlen);
ON_ERR_SET_GOTO(ret <= 0, ret, -1, err);

if (ct == NULL || secret == NULL) {
OQS_KEM_PRINTF3("EVP KEM returning lengths %ld and %ld\n", *ctlen,
*secretlen);
return 1;
}

// generate random secret, 256 bits = 32 bytes
if (RAND_priv_bytes(secret, 32) <= 0) {
if (RAND_priv_bytes(secret, *secretlen) <= 0) {
ret = -1;
goto err;
}

outlen = kexDeriveLen;
ret = EVP_PKEY_encrypt(ctx, ct, &outlen, secret, 32);
// outlen = kexDeriveLen;
ret = EVP_PKEY_encrypt(ctx, ct, ctlen, secret, *secretlen);
ON_ERR_SET_GOTO(ret <= 0, ret, -1, err);

} else {
Expand Down Expand Up @@ -159,11 +163,7 @@ static int oqs_evp_kem_decaps_keyslot(void *vpkemctx, unsigned char *secret,

if (keytype == EVP_PKEY_RSA) {
*secretlen = 32;
size_t outlen = 32; // expected secret length (256 bits)
if (secret == NULL) {
OQS_KEM_PRINTF2("EVP KEM returning lengths %ld\n", *secretlen);
return 1;
}
// size_t outlen = 32; // expected secret length (256 bits)

pkey =
d2i_PrivateKey(keytype, NULL, (const unsigned char **)&privkey_kex,
Expand All @@ -189,8 +189,15 @@ static int oqs_evp_kem_decaps_keyslot(void *vpkemctx, unsigned char *secret,
ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, NULL, 0);
ON_ERR_SET_GOTO(ret <= 0, ret, -7, err);

ret = EVP_PKEY_decrypt(ctx, NULL, &outlen, ct, ctlen);
ret = EVP_PKEY_decrypt(ctx, secret, &outlen, ct, ctlen);
ret = EVP_PKEY_decrypt(ctx, NULL, secretlen, NULL, ctlen);
ON_ERR_SET_GOTO(ret <= 0, ret, -8, err);

if (secret == NULL) {
OQS_KEM_PRINTF2("EVP KEM returning lengths %ld\n", *secretlen);
return 1;
}

ret = EVP_PKEY_decrypt(ctx, secret, secretlen, ct, ctlen);
ON_ERR_SET_GOTO(ret <= 0, ret, -8, err);

} else {
Expand Down

0 comments on commit 8fb226b

Please sign in to comment.