Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 24, 2024
1 parent 5ea3836 commit c2632c6
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 90 deletions.
81 changes: 41 additions & 40 deletions oqsprov/oqs_hyb_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ static int oqs_evp_kem_encaps_keyslot(void *vpkemctx, unsigned char *ct,
}

const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
if (!pkemctx || !pkemctx->kem || !pkemctx->kem->oqsx_provider_ctx.oqsx_evp_ctx) {
if (!pkemctx || !pkemctx->kem ||
!pkemctx->kem->oqsx_provider_ctx.oqsx_evp_ctx) {
return -1;
}
const OQSX_EVP_CTX *evp_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_evp_ctx;
Expand Down Expand Up @@ -221,56 +222,56 @@ static int oqs_hyb_kem_encaps(void *vpkemctx, unsigned char *ct, size_t *ctlen,
}

static int oqs_hyb_kem_decaps(void *vpkemctx, unsigned char *secret,
size_t *secretlen, const unsigned char *ct,
size_t ctlen) {
int ret = OQS_SUCCESS;
const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
const OQSX_EVP_CTX *evp_ctx;
const OQS_KEM *qs_ctx;
size_t *secretlen, const unsigned char *ct,
size_t ctlen) {
int ret = OQS_SUCCESS;
const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
const OQSX_EVP_CTX *evp_ctx;
const OQS_KEM *qs_ctx;

size_t secretLen0 = 0, secretLen1 = 0;
size_t ctLen0 = 0, ctLen1 = 0;
const unsigned char *ct0 = NULL, *ct1 = NULL;
unsigned char *secret0 = NULL, *secret1 = NULL;
size_t secretLen0 = 0, secretLen1 = 0;
size_t ctLen0 = 0, ctLen1 = 0;
const unsigned char *ct0 = NULL, *ct1 = NULL;
unsigned char *secret0 = NULL, *secret1 = NULL;

if (!vpkemctx || !secretlen || !ct) {
return OQS_ERROR;
}
if (!vpkemctx || !secretlen || !ct) {
return OQS_ERROR;
}

evp_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_evp_ctx;
qs_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem;
evp_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_evp_ctx;
qs_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem;

if (!evp_ctx || !qs_ctx) {
return OQS_ERROR;
}
if (!evp_ctx || !qs_ctx) {
return OQS_ERROR;
}

ret = oqs_evp_kem_decaps_keyslot(vpkemctx, NULL, &secretLen0, NULL, 0, 0);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);
ret = oqs_qs_kem_decaps_keyslot(vpkemctx, NULL, &secretLen1, NULL, 0, 1);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);
ret = oqs_evp_kem_decaps_keyslot(vpkemctx, NULL, &secretLen0, NULL, 0, 0);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);
ret = oqs_qs_kem_decaps_keyslot(vpkemctx, NULL, &secretLen1, NULL, 0, 1);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);

*secretlen = secretLen0 + secretLen1;
*secretlen = secretLen0 + secretLen1;

if (secret == NULL)
return 1;
if (secret == NULL)
return 1;

ctLen0 = evp_ctx->evp_info->length_public_key;
ctLen1 = qs_ctx->length_ciphertext;
ctLen0 = evp_ctx->evp_info->length_public_key;
ctLen1 = qs_ctx->length_ciphertext;

ON_ERR_SET_GOTO(ctLen0 + ctLen1 != ctlen, ret, OQS_ERROR, err);
ON_ERR_SET_GOTO(ctLen0 + ctLen1 != ctlen, ret, OQS_ERROR, err);

ct0 = ct;
ct1 = ct + ctLen0;
secret0 = secret;
secret1 = secret + secretLen0;
ct0 = ct;
ct1 = ct + ctLen0;
secret0 = secret;
secret1 = secret + secretLen0;

ret = oqs_evp_kem_decaps_keyslot(vpkemctx, secret0, &secretLen0, ct0,
ctLen0, 0);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);
ret = oqs_qs_kem_decaps_keyslot(vpkemctx, secret1, &secretLen1, ct1, ctLen1,
1);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);
ret = oqs_evp_kem_decaps_keyslot(vpkemctx, secret0, &secretLen0, ct0,
ctLen0, 0);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);
ret = oqs_qs_kem_decaps_keyslot(vpkemctx, secret1, &secretLen1, ct1, ctLen1,
1);
ON_ERR_SET_GOTO(ret <= 0, ret, OQS_ERROR, err);

err:
return ret;
return ret;
}
9 changes: 5 additions & 4 deletions oqsprov/oqs_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static OSSL_FUNC_kem_decapsulate_fn oqs_qs_kem_decaps;
static OSSL_FUNC_kem_freectx_fn oqs_kem_freectx;

/*
* What's passed as an actual key is defined by the KEYMGMT interface.
*/
* What's passed as an actual key is defined by the KEYMGMT interface.
*/
typedef struct {
OSSL_LIB_CTX *libctx;
OQSX_KEY *kem;
Expand Down Expand Up @@ -121,7 +121,7 @@ static int oqs_kem_decaps_init(void *vpkemctx, void *vkem,
}

/// Quantum-Safe KEM functions (OQS)
static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out,
static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out,
size_t *outlen, unsigned char *secret,
size_t *secretlen, int keyslot) {
const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
Expand Down Expand Up @@ -172,7 +172,8 @@ static int oqs_kem_decaps_init(void *vpkemctx, void *vkem,
*outlen = kem_ctx->length_ciphertext;
*secretlen = kem_ctx->length_shared_secret;

ret = OQS_KEM_encaps(kem_ctx, out, secret, pkemctx->kem->comp_pubkey[keyslot]);
ret = OQS_KEM_encaps(kem_ctx, out, secret,
pkemctx->kem->comp_pubkey[keyslot]);
if (ret != OQS_SUCCESS) {
OPENSSL_cleanse(secret, *secretlen);
}
Expand Down
Loading

0 comments on commit c2632c6

Please sign in to comment.