Просмотр исходного кода

prefix structs with pqc_

tags/v0.0.1
Henry Case 3 лет назад
Родитель
Сommit
4e10c0925f
4 измененных файлов: 146 добавлений и 121 удалений
  1. +21
    -35
      public/pqc/pqc.h
  2. +35
    -15
      src/capi/pqapi.c
  3. +80
    -61
      src/rustapi/pqc-sys/src/bindings.rs
  4. +10
    -10
      test/ut.cpp

+ 21
- 35
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
}


+ 35
- 15
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; i<PQC_ALG_KEM_MAX; i++) {
if (kems[i].p.alg_id == id) {
return (params_t*)&kems[i];
return (pqc_ctx_t*)&kems[i];
}
}
return 0;
}

const params_t *pqc_sig_alg_by_id(uint8_t id) {
const pqc_ctx_t *pqc_sig_alg_by_id(uint8_t id) {
int i;
for(i=0; i<PQC_ALG_SIG_MAX; i++) {
if (sigs[i].p.alg_id == id) {
return (params_t*)&sigs[i];
return (pqc_ctx_t*)&sigs[i];
}
}
return 0;
}

bool pqc_keygen(const params_t *p,
bool pqc_keygen(const pqc_ctx_t *p,
uint8_t *pk, uint8_t *sk) {
return !p->keygen(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));


+ 80
- 61
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::<params_t>(),
::std::mem::size_of::<pqc_ctx_t>(),
40usize,
concat!("Size of: ", stringify!(params_t))
concat!("Size of: ", stringify!(pqc_ctx_t))
);
assert_eq!(
::std::mem::align_of::<params_t>(),
::std::mem::align_of::<pqc_ctx_t>(),
8usize,
concat!("Alignment of ", stringify!(params_t))
concat!("Alignment of ", stringify!(pqc_ctx_t))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<params_t>())).alg_id as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_ctx_t>())).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::<params_t>())).alg_name as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_ctx_t>())).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::<params_t>())).prv_key_bsz as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_ctx_t>())).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::<params_t>())).pub_key_bsz as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_ctx_t>())).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::<params_t>())).is_kem as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_ctx_t>())).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::<params_t>())).keygen as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_ctx_t>())).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::<kem_params_t>(),
::std::mem::size_of::<pqc_kem_ctx_t>(),
64usize,
concat!("Size of: ", stringify!(kem_params_t))
concat!("Size of: ", stringify!(pqc_kem_ctx_t))
);
assert_eq!(
::std::mem::align_of::<kem_params_t>(),
::std::mem::align_of::<pqc_kem_ctx_t>(),
8usize,
concat!("Alignment of ", stringify!(kem_params_t))
concat!("Alignment of ", stringify!(pqc_kem_ctx_t))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<kem_params_t>())).p as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_kem_ctx_t>())).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::<kem_params_t>())).ciphertext_bsz as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_kem_ctx_t>())).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::<kem_params_t>())).secret_bsz as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_kem_ctx_t>())).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::<kem_params_t>())).encapsulate as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_kem_ctx_t>())).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::<kem_params_t>())).decapsulate as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_kem_ctx_t>())).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::<sig_params_t>(),
::std::mem::size_of::<pqc_sig_ctx_t>(),
64usize,
concat!("Size of: ", stringify!(sig_params_t))
concat!("Size of: ", stringify!(pqc_sig_ctx_t))
);
assert_eq!(
::std::mem::align_of::<sig_params_t>(),
::std::mem::align_of::<pqc_sig_ctx_t>(),
8usize,
concat!("Alignment of ", stringify!(sig_params_t))
concat!("Alignment of ", stringify!(pqc_sig_ctx_t))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<sig_params_t>())).p as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_sig_ctx_t>())).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::<sig_params_t>())).sign_bsz as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_sig_ctx_t>())).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::<sig_params_t>())).sign as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_sig_ctx_t>())).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::<sig_params_t>())).verify as *const _ as usize },
unsafe { &(*(::std::ptr::null::<pqc_sig_ctx_t>())).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 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_sig_alg_by_id(id: u8) -> *const params_t;
pub fn pqc_private_key_bsz(p: *const pqc_ctx_t) -> u32;
}

+ 10
- 10
test/ut.cpp Просмотреть файл

@@ -6,13 +6,13 @@
TEST(Kyber,KEMOneOff) {

for (int i=0; i<PQC_ALG_KEM_MAX; i++) {
const params_t *p = pqc_kem_alg_by_id(i);
const pqc_ctx_t *p = pqc_kem_alg_by_id(i);

std::vector<uint8_t> ct(ciphertext_bsz(p));
std::vector<uint8_t> ss1(shared_secret_bsz(p));
std::vector<uint8_t> ss2(shared_secret_bsz(p));
std::vector<uint8_t> sk(private_key_bsz(p));
std::vector<uint8_t> pk(public_key_bsz(p));
std::vector<uint8_t> ct(pqc_ciphertext_bsz(p));
std::vector<uint8_t> ss1(pqc_shared_secret_bsz(p));
std::vector<uint8_t> ss2(pqc_shared_secret_bsz(p));
std::vector<uint8_t> sk(pqc_private_key_bsz(p));
std::vector<uint8_t> 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<PQC_ALG_SIG_MAX; i++) {
const params_t *p = pqc_sig_alg_by_id(i);
const pqc_ctx_t *p = pqc_sig_alg_by_id(i);

uint8_t msg[1234];
std::vector<uint8_t> sig(signature_bsz(p));
std::vector<uint8_t> sk(private_key_bsz(p));
std::vector<uint8_t> pk(public_key_bsz(p));
std::vector<uint8_t> sig(pqc_signature_bsz(p));
std::vector<uint8_t> sk(pqc_private_key_bsz(p));
std::vector<uint8_t> pk(pqc_public_key_bsz(p));

ASSERT_TRUE(
pqc_keygen(p, pk.data(), sk.data()));


Загрузка…
Отмена
Сохранить