From 4e10c0925f15f9bb9244774eb5d2b57d9fa79f40 Mon Sep 17 00:00:00 2001 From: Kris Kwiatkowski Date: Mon, 10 May 2021 16:01:48 +0100 Subject: [PATCH] prefix structs with pqc_ --- public/pqc/pqc.h | 56 +++++------ src/capi/pqapi.c | 50 +++++++--- src/rustapi/pqc-sys/src/bindings.rs | 141 ++++++++++++++++------------ test/ut.cpp | 20 ++-- 4 files changed, 146 insertions(+), 121 deletions(-) diff --git a/public/pqc/pqc.h b/public/pqc/pqc.h index 9cb862af..ff870611 100644 --- a/public/pqc/pqc.h +++ b/public/pqc/pqc.h @@ -75,7 +75,7 @@ enum { PQC_SUPPORTED_KEMS(DEFNUM) PQC_ALG_KEM_MAX }; #undef DEFNUM // Parameters of the scheme -typedef struct params_t { +typedef struct pqc_ctx_t { const uint8_t alg_id; const char* alg_name; const uint32_t prv_key_bsz; @@ -83,73 +83,59 @@ typedef struct params_t { const bool is_kem; int (*keygen)(uint8_t *sk, uint8_t *pk); -} params_t; +} pqc_ctx_t; -typedef struct kem_params_t { - params_t p; +typedef struct pqc_kem_ctx_t { + pqc_ctx_t p; const uint32_t ciphertext_bsz; const uint32_t secret_bsz; int (*encapsulate)(uint8_t *ct, uint8_t *ss, const uint8_t *pk); int (*decapsulate)(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); -} kem_params_t; +} pqc_kem_ctx_t; -typedef struct sig_params_t { - params_t p; +typedef struct pqc_sig_ctx_t { + pqc_ctx_t p; const uint32_t sign_bsz; int (*sign)(uint8_t *sig, uint64_t *siglen, const uint8_t *m, uint64_t mlen, const uint8_t *sk); int (*verify)(const uint8_t *sig, uint64_t siglen, const uint8_t *m, uint64_t mlen, const uint8_t *pk); -} sig_params_t; - -inline uint32_t ciphertext_bsz(const params_t *p) { - return ((kem_params_t *)p)->ciphertext_bsz; -} - -inline uint32_t shared_secret_bsz(const params_t *p) { - return ((kem_params_t *)p)->secret_bsz; -} - -inline uint32_t signature_bsz(const params_t *p) { - return ((sig_params_t *)p)->sign_bsz; -} - -inline uint32_t public_key_bsz(const params_t *p) { - return p->pub_key_bsz; -} - -inline uint32_t private_key_bsz(const params_t *p) { - return p->prv_key_bsz; -} +} pqc_sig_ctx_t; bool pqc_keygen( - const params_t *p, + const pqc_ctx_t *p, uint8_t *pk, uint8_t *sk); bool pqc_kem_encapsulate( - const params_t *p, + const pqc_ctx_t *p, uint8_t *ct, uint8_t *ss, const uint8_t *pk); bool pqc_kem_decapsulate( - const params_t *p, + const pqc_ctx_t *p, uint8_t *ss, const uint8_t *ct, const uint8_t *sk); bool pqc_sig_create( - const params_t *p, + const pqc_ctx_t *p, uint8_t *sig, uint64_t *siglen, const uint8_t *m, uint64_t mlen, const uint8_t *sk); bool pqc_sig_verify( - const params_t *p, + const pqc_ctx_t *p, const uint8_t *sig, uint64_t siglen, const uint8_t *m, uint64_t mlen, const uint8_t *pk); -const params_t *pqc_kem_alg_by_id(uint8_t id); -const params_t *pqc_sig_alg_by_id(uint8_t id); +const pqc_ctx_t *pqc_kem_alg_by_id(uint8_t id); +const pqc_ctx_t *pqc_sig_alg_by_id(uint8_t id); + +uint32_t pqc_ciphertext_bsz(const pqc_ctx_t *p); +uint32_t pqc_shared_secret_bsz(const pqc_ctx_t *p); +uint32_t pqc_signature_bsz(const pqc_ctx_t *p); +uint32_t pqc_public_key_bsz(const pqc_ctx_t *p); +uint32_t pqc_private_key_bsz(const pqc_ctx_t *p); #ifdef __cplusplus } diff --git a/src/capi/pqapi.c b/src/capi/pqapi.c index 3ea5a57c..ded6d724 100644 --- a/src/capi/pqapi.c +++ b/src/capi/pqapi.c @@ -68,12 +68,12 @@ }, // Registers supported KEMs -const kem_params_t kems[] = { +const pqc_kem_ctx_t kems[] = { PQC_SUPPORTED_KEMS(REG_KEM) }; // Registers supported signatures -const sig_params_t sigs[] = { +const pqc_sig_ctx_t sigs[] = { PQC_SUPPORTED_SIGS(REG_SIG) }; @@ -84,55 +84,75 @@ const X86Features * const get_cpu_caps(void) { return &CPU_CAPS; } -const params_t *pqc_kem_alg_by_id(uint8_t id) { +const pqc_ctx_t *pqc_kem_alg_by_id(uint8_t id) { int i; for(i=0; ikeygen(pk, sk); } -bool pqc_kem_encapsulate(const params_t *p, +bool pqc_kem_encapsulate(const pqc_ctx_t *p, uint8_t *ct, uint8_t *ss, const uint8_t *pk) { - return !((kem_params_t*)p)->encapsulate(ct, ss, pk); + return !((pqc_kem_ctx_t*)p)->encapsulate(ct, ss, pk); } -bool pqc_kem_decapsulate(const params_t *p, +bool pqc_kem_decapsulate(const pqc_ctx_t *p, uint8_t *ss, const uint8_t *ct, const uint8_t *sk) { - return !((kem_params_t*)p)->decapsulate(ss, ct, sk); + return !((pqc_kem_ctx_t*)p)->decapsulate(ss, ct, sk); } -bool pqc_sig_create(const params_t *p, +bool pqc_sig_create(const pqc_ctx_t *p, uint8_t *sig, uint64_t *siglen, const uint8_t *m, uint64_t mlen, const uint8_t *sk) { - return !((sig_params_t *)p)->sign(sig, siglen, m, mlen, sk); + return !((pqc_sig_ctx_t *)p)->sign(sig, siglen, m, mlen, sk); } -bool pqc_sig_verify(const params_t *p, +bool pqc_sig_verify(const pqc_ctx_t *p, const uint8_t *sig, uint64_t siglen, const uint8_t *m, uint64_t mlen, const uint8_t *pk) { - return !((sig_params_t *)p)->verify(sig, siglen, m, mlen, pk); + return !((pqc_sig_ctx_t *)p)->verify(sig, siglen, m, mlen, pk); +} + +uint32_t pqc_ciphertext_bsz(const pqc_ctx_t *p) { + return ((pqc_kem_ctx_t *)p)->ciphertext_bsz; +} + +uint32_t pqc_shared_secret_bsz(const pqc_ctx_t *p) { + return ((pqc_kem_ctx_t *)p)->secret_bsz; +} + +uint32_t pqc_signature_bsz(const pqc_ctx_t *p) { + return ((pqc_sig_ctx_t *)p)->sign_bsz; +} + +uint32_t pqc_public_key_bsz(const pqc_ctx_t *p) { + return p->pub_key_bsz; +} + +uint32_t pqc_private_key_bsz(const pqc_ctx_t *p) { + return p->prv_key_bsz; } void static_initialization(void) __attribute__((constructor)); diff --git a/src/rustapi/pqc-sys/src/bindings.rs b/src/rustapi/pqc-sys/src/bindings.rs index de93cfad..2d9c1163 100644 --- a/src/rustapi/pqc-sys/src/bindings.rs +++ b/src/rustapi/pqc-sys/src/bindings.rs @@ -261,7 +261,7 @@ pub const PQC_ALG_KEM_MAX: ::std::os::raw::c_uint = 20; pub type _bindgen_ty_2 = ::std::os::raw::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct params_t { +pub struct pqc_ctx_t { pub alg_id: u8, pub alg_name: *const ::std::os::raw::c_char, pub prv_key_bsz: u32, @@ -272,87 +272,87 @@ pub struct params_t { >, } #[test] -fn bindgen_test_layout_params_t() { +fn bindgen_test_layout_pqc_ctx_t() { assert_eq!( - ::std::mem::size_of::(), + ::std::mem::size_of::(), 40usize, - concat!("Size of: ", stringify!(params_t)) + concat!("Size of: ", stringify!(pqc_ctx_t)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(params_t)) + concat!("Alignment of ", stringify!(pqc_ctx_t)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).alg_id as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).alg_id as *const _ as usize }, 0usize, concat!( "Offset of field: ", - stringify!(params_t), + stringify!(pqc_ctx_t), "::", stringify!(alg_id) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).alg_name as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).alg_name as *const _ as usize }, 8usize, concat!( "Offset of field: ", - stringify!(params_t), + stringify!(pqc_ctx_t), "::", stringify!(alg_name) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).prv_key_bsz as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).prv_key_bsz as *const _ as usize }, 16usize, concat!( "Offset of field: ", - stringify!(params_t), + stringify!(pqc_ctx_t), "::", stringify!(prv_key_bsz) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).pub_key_bsz as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).pub_key_bsz as *const _ as usize }, 20usize, concat!( "Offset of field: ", - stringify!(params_t), + stringify!(pqc_ctx_t), "::", stringify!(pub_key_bsz) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).is_kem as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).is_kem as *const _ as usize }, 24usize, concat!( "Offset of field: ", - stringify!(params_t), + stringify!(pqc_ctx_t), "::", stringify!(is_kem) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).keygen as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).keygen as *const _ as usize }, 32usize, concat!( "Offset of field: ", - stringify!(params_t), + stringify!(pqc_ctx_t), "::", stringify!(keygen) ) ); } -impl Default for params_t { +impl Default for pqc_ctx_t { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct kem_params_t { - pub p: params_t, +pub struct pqc_kem_ctx_t { + pub p: pqc_ctx_t, pub ciphertext_bsz: u32, pub secret_bsz: u32, pub encapsulate: ::std::option::Option< @@ -363,77 +363,77 @@ pub struct kem_params_t { >, } #[test] -fn bindgen_test_layout_kem_params_t() { +fn bindgen_test_layout_pqc_kem_ctx_t() { assert_eq!( - ::std::mem::size_of::(), + ::std::mem::size_of::(), 64usize, - concat!("Size of: ", stringify!(kem_params_t)) + concat!("Size of: ", stringify!(pqc_kem_ctx_t)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(kem_params_t)) + concat!("Alignment of ", stringify!(pqc_kem_ctx_t)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).p as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).p as *const _ as usize }, 0usize, concat!( "Offset of field: ", - stringify!(kem_params_t), + stringify!(pqc_kem_ctx_t), "::", stringify!(p) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).ciphertext_bsz as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).ciphertext_bsz as *const _ as usize }, 40usize, concat!( "Offset of field: ", - stringify!(kem_params_t), + stringify!(pqc_kem_ctx_t), "::", stringify!(ciphertext_bsz) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).secret_bsz as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).secret_bsz as *const _ as usize }, 44usize, concat!( "Offset of field: ", - stringify!(kem_params_t), + stringify!(pqc_kem_ctx_t), "::", stringify!(secret_bsz) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).encapsulate as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).encapsulate as *const _ as usize }, 48usize, concat!( "Offset of field: ", - stringify!(kem_params_t), + stringify!(pqc_kem_ctx_t), "::", stringify!(encapsulate) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).decapsulate as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).decapsulate as *const _ as usize }, 56usize, concat!( "Offset of field: ", - stringify!(kem_params_t), + stringify!(pqc_kem_ctx_t), "::", stringify!(decapsulate) ) ); } -impl Default for kem_params_t { +impl Default for pqc_kem_ctx_t { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct sig_params_t { - pub p: params_t, +pub struct pqc_sig_ctx_t { + pub p: pqc_ctx_t, pub sign_bsz: u32, pub sign: ::std::option::Option< unsafe extern "C" fn( @@ -455,73 +455,77 @@ pub struct sig_params_t { >, } #[test] -fn bindgen_test_layout_sig_params_t() { +fn bindgen_test_layout_pqc_sig_ctx_t() { assert_eq!( - ::std::mem::size_of::(), + ::std::mem::size_of::(), 64usize, - concat!("Size of: ", stringify!(sig_params_t)) + concat!("Size of: ", stringify!(pqc_sig_ctx_t)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(sig_params_t)) + concat!("Alignment of ", stringify!(pqc_sig_ctx_t)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).p as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).p as *const _ as usize }, 0usize, concat!( "Offset of field: ", - stringify!(sig_params_t), + stringify!(pqc_sig_ctx_t), "::", stringify!(p) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).sign_bsz as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).sign_bsz as *const _ as usize }, 40usize, concat!( "Offset of field: ", - stringify!(sig_params_t), + stringify!(pqc_sig_ctx_t), "::", stringify!(sign_bsz) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).sign as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).sign as *const _ as usize }, 48usize, concat!( "Offset of field: ", - stringify!(sig_params_t), + stringify!(pqc_sig_ctx_t), "::", stringify!(sign) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).verify as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).verify as *const _ as usize }, 56usize, concat!( "Offset of field: ", - stringify!(sig_params_t), + stringify!(pqc_sig_ctx_t), "::", stringify!(verify) ) ); } -impl Default for sig_params_t { +impl Default for pqc_sig_ctx_t { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } extern "C" { - pub fn pqc_keygen(p: *const params_t, pk: *mut u8, sk: *mut u8) -> bool; + pub fn pqc_keygen(p: *const pqc_ctx_t, pk: *mut u8, sk: *mut u8) -> bool; } extern "C" { - pub fn pqc_kem_encapsulate(p: *const params_t, ct: *mut u8, ss: *mut u8, pk: *const u8) - -> bool; + pub fn pqc_kem_encapsulate( + p: *const pqc_ctx_t, + ct: *mut u8, + ss: *mut u8, + pk: *const u8, + ) -> bool; } extern "C" { pub fn pqc_kem_decapsulate( - p: *const params_t, + p: *const pqc_ctx_t, ss: *mut u8, ct: *const u8, sk: *const u8, @@ -529,7 +533,7 @@ extern "C" { } extern "C" { pub fn pqc_sig_create( - p: *const params_t, + p: *const pqc_ctx_t, sig: *mut u8, siglen: *mut u64, m: *const u8, @@ -539,7 +543,7 @@ extern "C" { } extern "C" { pub fn pqc_sig_verify( - p: *const params_t, + p: *const pqc_ctx_t, sig: *const u8, siglen: u64, m: *const u8, @@ -548,8 +552,23 @@ extern "C" { ) -> bool; } extern "C" { - pub fn pqc_kem_alg_by_id(id: u8) -> *const params_t; + pub fn pqc_kem_alg_by_id(id: u8) -> *const pqc_ctx_t; } extern "C" { - pub fn pqc_sig_alg_by_id(id: u8) -> *const params_t; + pub fn pqc_sig_alg_by_id(id: u8) -> *const pqc_ctx_t; +} +extern "C" { + pub fn pqc_ciphertext_bsz(p: *const pqc_ctx_t) -> u32; +} +extern "C" { + pub fn pqc_shared_secret_bsz(p: *const pqc_ctx_t) -> u32; +} +extern "C" { + pub fn pqc_signature_bsz(p: *const pqc_ctx_t) -> u32; +} +extern "C" { + pub fn pqc_public_key_bsz(p: *const pqc_ctx_t) -> u32; +} +extern "C" { + pub fn pqc_private_key_bsz(p: *const pqc_ctx_t) -> u32; } diff --git a/test/ut.cpp b/test/ut.cpp index 736f48ba..5e58a5e6 100644 --- a/test/ut.cpp +++ b/test/ut.cpp @@ -6,13 +6,13 @@ TEST(Kyber,KEMOneOff) { for (int i=0; i ct(ciphertext_bsz(p)); - std::vector ss1(shared_secret_bsz(p)); - std::vector ss2(shared_secret_bsz(p)); - std::vector sk(private_key_bsz(p)); - std::vector pk(public_key_bsz(p)); + std::vector ct(pqc_ciphertext_bsz(p)); + std::vector ss1(pqc_shared_secret_bsz(p)); + std::vector ss2(pqc_shared_secret_bsz(p)); + std::vector sk(pqc_private_key_bsz(p)); + std::vector pk(pqc_public_key_bsz(p)); ASSERT_TRUE( pqc_keygen(p, pk.data(), sk.data())); @@ -28,12 +28,12 @@ TEST(Kyber,KEMOneOff) { TEST(Kyber,SIGNOneOff) { for (int i=0; i sig(signature_bsz(p)); - std::vector sk(private_key_bsz(p)); - std::vector pk(public_key_bsz(p)); + std::vector sig(pqc_signature_bsz(p)); + std::vector sk(pqc_private_key_bsz(p)); + std::vector pk(pqc_public_key_bsz(p)); ASSERT_TRUE( pqc_keygen(p, pk.data(), sk.data()));