Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update coding style and test facilities #477

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/coding_style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
container:
image: openquantumsafe/ci-ubuntu-jammy:latest
image: openquantumsafe/ci-ubuntu-latest:latest
steps:
- name: Install dependencies
run: apt-get update && apt-get install -y clang-format
Expand All @@ -17,4 +17,4 @@ jobs:
uses: actions/checkout@v2

- name: Check coding style using clang-format
run: find . -type f -and '(' -name '*.h' -or -name '*.c' -or -name '*.inc' ')' | xargs clang-format --dry-run --Werror
run: ./scripts/do_code_format.sh
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
export LIBOQS_SRC_DIR=`pwd`/liboqs && \
! pip3 install -r oqs-template/requirements.txt 2>&1 | grep ERROR && \
python3 oqs-template/generate.py && \
find . -type f -and '(' -name '*.h' -or -name '*.c' -or -name '*.inc' ')' | xargs clang-format -i && \
./scripts/do_code_format.sh --no-dry-run && \
git diff && \
! git status | grep modified
- name: Build .deb install package
Expand Down
11 changes: 5 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ Thus, any PR should revisit and possibly update this file suitably.

### Coding style

This project has adopted the [OpenSSL coding style](https://www.openssl.org/policies/technical/coding-style.html).
This project has adopted the LLVM coding style.
To check adherence of any new code to this, it therefore is highly recommended to
run the following commands in the project main directory prior to finishing a PR:

find oqsprov -type f -and '(' -name '*.h' -or -name '*.c' -or -name '*.inc' ')' | xargs clang-format --dry-run --Werror
find test -type f -and '(' -name '*.h' -or -name '*.c' -or -name '*.inc' ')' | xargs clang-format --dry-run --Werror
./scripts/do_code_format.sh

If errors are reported, consider replacing `--dry-run --Werror` with `-i` to
enable in-place correction of the coding errors, or correct the code manually
to pass this CI acceptance test.
If errors/deviations are reported, review the code or consider running the utility
script `scripts/format_code.sh` if you'd like to get the code changed to use the
exact same code style check used in CI.

### Running CI locally

Expand Down
219 changes: 103 additions & 116 deletions oqsprov/oqs_decode_der2key.c

Large diffs are not rendered by default.

1,129 changes: 547 additions & 582 deletions oqsprov/oqs_encode_key2any.c

Large diffs are not rendered by default.

29 changes: 12 additions & 17 deletions oqsprov/oqs_endecoder_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
* ToDo: Adding hybrid alg support
*/

#include "oqs_endecoder_local.h"
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/core.h>

OSSL_FUNC_keymgmt_new_fn *oqs_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
{
#include "oqs_endecoder_local.h"

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 +23,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 +33,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 +43,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 +53,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
3 changes: 2 additions & 1 deletion oqsprov/oqs_endecoder_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*
*/

#include "oqs_prov.h"
#include <openssl/core.h>
#include <openssl/core_dispatch.h>
#include <openssl/types.h>

#include "oqs_prov.h"

OSSL_FUNC_keymgmt_new_fn *oqs_prov_get_keymgmt_new(const OSSL_DISPATCH *fns);
OSSL_FUNC_keymgmt_free_fn *oqs_prov_get_keymgmt_free(const OSSL_DISPATCH *fns);
OSSL_FUNC_keymgmt_import_fn *
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
73 changes: 32 additions & 41 deletions oqsprov/oqs_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* ToDo: Adding hybrid alg support; More testing with more key types.
*/

#include "oqs_prov.h"
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
Expand All @@ -18,20 +17,22 @@
#include <openssl/params.h>
#include <string.h>

#include "oqs_prov.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 +51,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 +63,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 +86,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 +101,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 +110,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 +146,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 +161,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 +192,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
Loading