Skip to content

Commit

Permalink
fromat code
Browse files Browse the repository at this point in the history
Signed-off-by: rtjk <[email protected]>
  • Loading branch information
rtjk committed Aug 19, 2024
1 parent d5db921 commit b988894
Show file tree
Hide file tree
Showing 21 changed files with 5,471 additions and 4,023 deletions.
380 changes: 211 additions & 169 deletions oqsprov/oqs_decode_der2key.c

Large diffs are not rendered by default.

1,201 changes: 585 additions & 616 deletions oqsprov/oqs_encode_key2any.c

Large diffs are not rendered by default.

26 changes: 10 additions & 16 deletions oqsprov/oqs_endecoder_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#include <openssl/buffer.h>
#include <openssl/core.h>

OSSL_FUNC_keymgmt_new_fn *oqs_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
{
OSSL_FUNC_keymgmt_new_fn *oqs_prov_get_keymgmt_new(const OSSL_DISPATCH *fns) {
/* Pilfer the keymgmt dispatch table */
for (; fns->function_id != 0; fns++)
if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
Expand All @@ -23,8 +22,7 @@ OSSL_FUNC_keymgmt_new_fn *oqs_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
return NULL;
}

OSSL_FUNC_keymgmt_free_fn *oqs_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
{
OSSL_FUNC_keymgmt_free_fn *oqs_prov_get_keymgmt_free(const OSSL_DISPATCH *fns) {
/* Pilfer the keymgmt dispatch table */
for (; fns->function_id != 0; fns++)
if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
Expand All @@ -34,8 +32,7 @@ OSSL_FUNC_keymgmt_free_fn *oqs_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
}

OSSL_FUNC_keymgmt_import_fn *
oqs_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
{
oqs_prov_get_keymgmt_import(const OSSL_DISPATCH *fns) {
/* Pilfer the keymgmt dispatch table */
for (; fns->function_id != 0; fns++)
if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
Expand All @@ -45,8 +42,7 @@ oqs_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
}

OSSL_FUNC_keymgmt_export_fn *
oqs_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
{
oqs_prov_get_keymgmt_export(const OSSL_DISPATCH *fns) {
/* Pilfer the keymgmt dispatch table */
for (; fns->function_id != 0; fns++)
if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT)
Expand All @@ -56,26 +52,24 @@ oqs_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
}

void *oqs_prov_import_key(const OSSL_DISPATCH *fns, void *provctx,
int selection, const OSSL_PARAM params[])
{
int selection, const OSSL_PARAM params[]) {
OSSL_FUNC_keymgmt_new_fn *kmgmt_new = oqs_prov_get_keymgmt_new(fns);
OSSL_FUNC_keymgmt_free_fn *kmgmt_free = oqs_prov_get_keymgmt_free(fns);
OSSL_FUNC_keymgmt_import_fn *kmgmt_import
= oqs_prov_get_keymgmt_import(fns);
OSSL_FUNC_keymgmt_import_fn *kmgmt_import =
oqs_prov_get_keymgmt_import(fns);
void *key = NULL;

if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) {
if ((key = kmgmt_new(provctx)) == NULL
|| !kmgmt_import(key, selection, params)) {
if ((key = kmgmt_new(provctx)) == NULL ||
!kmgmt_import(key, selection, params)) {
kmgmt_free(key);
key = NULL;
}
}
return key;
}

void oqs_prov_free_key(const OSSL_DISPATCH *fns, void *key)
{
void oqs_prov_free_key(const OSSL_DISPATCH *fns, void *key) {
OSSL_FUNC_keymgmt_free_fn *kmgmt_free = oqs_prov_get_keymgmt_free(fns);

if (kmgmt_free != NULL)
Expand Down
12 changes: 4 additions & 8 deletions oqsprov/oqs_hyb_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ static OSSL_FUNC_kem_decapsulate_fn oqs_hyb_kem_decaps;

static int oqs_evp_kem_encaps_keyslot(void *vpkemctx, unsigned char *ct,
size_t *ctlen, unsigned char *secret,
size_t *secretlen, int keyslot)
{
size_t *secretlen, int keyslot) {
int ret = OQS_SUCCESS, ret2 = 0;

const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
Expand Down Expand Up @@ -91,8 +90,7 @@ static int oqs_evp_kem_encaps_keyslot(void *vpkemctx, unsigned char *ct,
static int oqs_evp_kem_decaps_keyslot(void *vpkemctx, unsigned char *secret,
size_t *secretlen,
const unsigned char *ct, size_t ctlen,
int keyslot)
{
int keyslot) {
OQS_KEM_PRINTF("OQS KEM provider called: oqs_hyb_kem_decaps\n");

int ret = OQS_SUCCESS, ret2 = 0;
Expand Down Expand Up @@ -152,8 +150,7 @@ static int oqs_evp_kem_decaps_keyslot(void *vpkemctx, unsigned char *secret,
/// Hybrid KEM functions

static int oqs_hyb_kem_encaps(void *vpkemctx, unsigned char *ct, size_t *ctlen,
unsigned char *secret, size_t *secretlen)
{
unsigned char *secret, size_t *secretlen) {
int ret = OQS_SUCCESS;
const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
size_t secretLen0 = 0, secretLen1 = 0;
Expand Down Expand Up @@ -195,8 +192,7 @@ 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)
{
size_t ctlen) {
int ret = OQS_SUCCESS;
const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
const OQSX_EVP_CTX *evp_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_evp_ctx;
Expand Down
70 changes: 30 additions & 40 deletions oqsprov/oqs_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
#include <string.h>

#ifdef NDEBUG
# define OQS_KEM_PRINTF(a)
# define OQS_KEM_PRINTF2(a, b)
# define OQS_KEM_PRINTF3(a, b, c)
#define OQS_KEM_PRINTF(a)
#define OQS_KEM_PRINTF2(a, b)
#define OQS_KEM_PRINTF3(a, b, c)
#else
# define OQS_KEM_PRINTF(a) \
if (getenv("OQSKEM")) \
printf(a)
# define OQS_KEM_PRINTF2(a, b) \
if (getenv("OQSKEM")) \
printf(a, b)
# define OQS_KEM_PRINTF3(a, b, c) \
if (getenv("OQSKEM")) \
printf(a, b, c)
#define OQS_KEM_PRINTF(a) \
if (getenv("OQSKEM")) \
printf(a)
#define OQS_KEM_PRINTF2(a, b) \
if (getenv("OQSKEM")) \
printf(a, b)
#define OQS_KEM_PRINTF3(a, b, c) \
if (getenv("OQSKEM")) \
printf(a, b, c)
#endif // NDEBUG

static OSSL_FUNC_kem_newctx_fn oqs_kem_newctx;
Expand All @@ -50,8 +50,7 @@ typedef struct {

/// Common KEM functions

static void *oqs_kem_newctx(void *provctx)
{
static void *oqs_kem_newctx(void *provctx) {
PROV_OQSKEM_CTX *pkemctx = OPENSSL_zalloc(sizeof(PROV_OQSKEM_CTX));

OQS_KEM_PRINTF("OQS KEM provider called: newctx\n");
Expand All @@ -63,17 +62,16 @@ static void *oqs_kem_newctx(void *provctx)
return pkemctx;
}

static void oqs_kem_freectx(void *vpkemctx)
{
static void oqs_kem_freectx(void *vpkemctx) {
PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;

OQS_KEM_PRINTF("OQS KEM provider called: freectx\n");
oqsx_key_free(pkemctx->kem);
OPENSSL_free(pkemctx);
}

static int oqs_kem_decapsencaps_init(void *vpkemctx, void *vkem, int operation)
{
static int oqs_kem_decapsencaps_init(void *vpkemctx, void *vkem,
int operation) {
PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;

OQS_KEM_PRINTF3("OQS KEM provider called: _init : New: %p; old: %p \n",
Expand All @@ -87,15 +85,13 @@ static int oqs_kem_decapsencaps_init(void *vpkemctx, void *vkem, int operation)
}

static int oqs_kem_encaps_init(void *vpkemctx, void *vkem,
const OSSL_PARAM params[])
{
const OSSL_PARAM params[]) {
OQS_KEM_PRINTF("OQS KEM provider called: encaps_init\n");
return oqs_kem_decapsencaps_init(vpkemctx, vkem, EVP_PKEY_OP_ENCAPSULATE);
}

static int oqs_kem_decaps_init(void *vpkemctx, void *vkem,
const OSSL_PARAM params[])
{
const OSSL_PARAM params[]) {
OQS_KEM_PRINTF("OQS KEM provider called: decaps_init\n");
return oqs_kem_decapsencaps_init(vpkemctx, vkem, EVP_PKEY_OP_DECAPSULATE);
}
Expand All @@ -104,8 +100,7 @@ static int oqs_kem_decaps_init(void *vpkemctx, void *vkem,

static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out,
size_t *outlen, unsigned char *secret,
size_t *secretlen, int keyslot)
{
size_t *secretlen, int keyslot) {
const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
const OQS_KEM *kem_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem;

Expand All @@ -114,8 +109,8 @@ static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out,
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n");
return -1;
}
if (pkemctx->kem->comp_pubkey == NULL
|| pkemctx->kem->comp_pubkey[keyslot] == NULL) {
if (pkemctx->kem->comp_pubkey == NULL ||
pkemctx->kem->comp_pubkey[keyslot] == NULL) {
OQS_KEM_PRINTF("OQS Warning: public key is NULL\n");
return -1;
}
Expand Down Expand Up @@ -150,15 +145,13 @@ static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out,
*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]);
return OQS_SUCCESS == OQS_KEM_encaps(kem_ctx, out, secret,
pkemctx->kem->comp_pubkey[keyslot]);
}

static int oqs_qs_kem_decaps_keyslot(void *vpkemctx, unsigned char *out,
size_t *outlen, const unsigned char *in,
size_t inlen, int keyslot)
{
size_t inlen, int keyslot) {
const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;
const OQS_KEM *kem_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem;

Expand All @@ -167,8 +160,8 @@ static int oqs_qs_kem_decaps_keyslot(void *vpkemctx, unsigned char *out,
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n");
return -1;
}
if (pkemctx->kem->comp_privkey == NULL
|| pkemctx->kem->comp_privkey[keyslot] == 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;
}
Expand Down Expand Up @@ -198,21 +191,18 @@ static int oqs_qs_kem_decaps_keyslot(void *vpkemctx, unsigned char *out,
}
*outlen = kem_ctx->length_shared_secret;

return OQS_SUCCESS
== OQS_KEM_decaps(kem_ctx, out, in,
pkemctx->kem->comp_privkey[keyslot]);
return OQS_SUCCESS == OQS_KEM_decaps(kem_ctx, out, in,
pkemctx->kem->comp_privkey[keyslot]);
}

static int oqs_qs_kem_encaps(void *vpkemctx, unsigned char *out, size_t *outlen,
unsigned char *secret, size_t *secretlen)
{
unsigned char *secret, size_t *secretlen) {
return oqs_qs_kem_encaps_keyslot(vpkemctx, out, outlen, secret, secretlen,
0);
}

static int oqs_qs_kem_decaps(void *vpkemctx, unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen)
{
const unsigned char *in, size_t inlen) {
return oqs_qs_kem_decaps_keyslot(vpkemctx, out, outlen, in, inlen, 0);
}

Expand Down
Loading

0 comments on commit b988894

Please sign in to comment.