Skip to content

Commit

Permalink
fixing encoders
Browse files Browse the repository at this point in the history
Signed-off-by: Basil Hess <[email protected]>
  • Loading branch information
bhess committed Sep 23, 2024
1 parent 965f235 commit 1572ebb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 63 deletions.
4 changes: 2 additions & 2 deletions oqs-template/oqsprov/oqsprov_keys.c/oqsnames.fragment
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
static oqs_nid_name_t nid_names[NID_TABLE_LEN] = {
#ifdef OQS_KEM_ENCODERS
{% for kem in config['kems'] %}
{ 0, "{{ kem['name_group'] }}", {{ kem['oqs_alg'] }}, KEY_TYPE_KEM, {{ kem['bit_security'] }} },
{ 0, "{{ kem['name_group'] }}", {{ kem['oqs_alg'] }}, KEY_TYPE_KEM, {{ kem['bit_security'] }}, 0 },
{%- for hybrid in kem['hybrids'] %}
{ 0, "{% if 'standard_name' in hybrid %}{{hybrid['standard_name']}}{% else %}{{ hybrid['hybrid_group'] }}_{{ kem['name_group'] }}{% endif %}", {{ kem['oqs_alg'] }}, {% if hybrid['hybrid_group'].startswith('p') -%} KEY_TYPE_ECP_HYB_KEM {% else %} KEY_TYPE_ECX_HYB_KEM {% endif %}, {{ kem['bit_security'] }} },
{ 0, "{% if 'standard_name' in hybrid %}{{hybrid['standard_name']}}{% else %}{{ hybrid['hybrid_group'] }}_{{ kem['name_group'] }}{% endif %}", {{ kem['oqs_alg'] }}, {% if hybrid['hybrid_group'].startswith('p') -%} KEY_TYPE_ECP_HYB_KEM {% else %} KEY_TYPE_ECX_HYB_KEM {% endif %}, {{ kem['bit_security'] }}, {% if 'fips_standard' in kem and not hybrid['hybrid_group'].startswith('p') %}1{% else %}0{% endif %} },
{%- endfor -%}
{%- endfor %}

Expand Down
9 changes: 7 additions & 2 deletions oqsprov/oqs_encode_key2any.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,13 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) {
OQS_ENC_PRINTF2("OQS ENC provider: saving priv+pubkey of length %d\n",
buflen);
memcpy(buf, oqsxkey->privkey, privkeylen);
memcpy(buf + privkeylen, oqsxkey->comp_pubkey[oqsxkey->numkeys - 1],
oqsx_key_get_oqs_public_key_len(oqsxkey));
if (oqsxkey->reverse_share) {
memcpy(buf + privkeylen, oqsxkey->comp_pubkey[0],
oqsx_key_get_oqs_public_key_len(oqsxkey));
} else {
memcpy(buf + privkeylen, oqsxkey->comp_pubkey[oqsxkey->numkeys - 1],
oqsx_key_get_oqs_public_key_len(oqsxkey));
}
#endif

oct.data = buf;
Expand Down
135 changes: 78 additions & 57 deletions oqsprov/oqsprov_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct {
char *oqsname;
int keytype;
int secbits;
int reverseshare;
} oqs_nid_name_t;

static int oqsx_key_recreate_classickey(OQSX_KEY *key, oqsx_key_op_t op);
Expand All @@ -60,69 +61,74 @@ static int oqsx_key_recreate_classickey(OQSX_KEY *key, oqsx_key_op_t op);
static oqs_nid_name_t nid_names[NID_TABLE_LEN] = {
#ifdef OQS_KEM_ENCODERS

{0, "frodo640aes", OQS_KEM_alg_frodokem_640_aes, KEY_TYPE_KEM, 128},
{0, "frodo640aes", OQS_KEM_alg_frodokem_640_aes, KEY_TYPE_KEM, 128, 0},
{0, "p256_frodo640aes", OQS_KEM_alg_frodokem_640_aes, KEY_TYPE_ECP_HYB_KEM,
128},
128, 0},
{0, "x25519_frodo640aes", OQS_KEM_alg_frodokem_640_aes,
KEY_TYPE_ECX_HYB_KEM, 128},
{0, "frodo640shake", OQS_KEM_alg_frodokem_640_shake, KEY_TYPE_KEM, 128},
KEY_TYPE_ECX_HYB_KEM, 128, 0},
{0, "frodo640shake", OQS_KEM_alg_frodokem_640_shake, KEY_TYPE_KEM, 128, 0},
{0, "p256_frodo640shake", OQS_KEM_alg_frodokem_640_shake,
KEY_TYPE_ECP_HYB_KEM, 128},
KEY_TYPE_ECP_HYB_KEM, 128, 0},
{0, "x25519_frodo640shake", OQS_KEM_alg_frodokem_640_shake,
KEY_TYPE_ECX_HYB_KEM, 128},
{0, "frodo976aes", OQS_KEM_alg_frodokem_976_aes, KEY_TYPE_KEM, 192},
KEY_TYPE_ECX_HYB_KEM, 128, 0},
{0, "frodo976aes", OQS_KEM_alg_frodokem_976_aes, KEY_TYPE_KEM, 192, 0},
{0, "p384_frodo976aes", OQS_KEM_alg_frodokem_976_aes, KEY_TYPE_ECP_HYB_KEM,
192},
192, 0},
{0, "x448_frodo976aes", OQS_KEM_alg_frodokem_976_aes, KEY_TYPE_ECX_HYB_KEM,
192},
{0, "frodo976shake", OQS_KEM_alg_frodokem_976_shake, KEY_TYPE_KEM, 192},
192, 0},
{0, "frodo976shake", OQS_KEM_alg_frodokem_976_shake, KEY_TYPE_KEM, 192, 0},
{0, "p384_frodo976shake", OQS_KEM_alg_frodokem_976_shake,
KEY_TYPE_ECP_HYB_KEM, 192},
KEY_TYPE_ECP_HYB_KEM, 192, 0},
{0, "x448_frodo976shake", OQS_KEM_alg_frodokem_976_shake,
KEY_TYPE_ECX_HYB_KEM, 192},
{0, "frodo1344aes", OQS_KEM_alg_frodokem_1344_aes, KEY_TYPE_KEM, 256},
KEY_TYPE_ECX_HYB_KEM, 192, 0},
{0, "frodo1344aes", OQS_KEM_alg_frodokem_1344_aes, KEY_TYPE_KEM, 256, 0},
{0, "p521_frodo1344aes", OQS_KEM_alg_frodokem_1344_aes,
KEY_TYPE_ECP_HYB_KEM, 256},
{0, "frodo1344shake", OQS_KEM_alg_frodokem_1344_shake, KEY_TYPE_KEM, 256},
KEY_TYPE_ECP_HYB_KEM, 256, 0},
{0, "frodo1344shake", OQS_KEM_alg_frodokem_1344_shake, KEY_TYPE_KEM, 256,
0},
{0, "p521_frodo1344shake", OQS_KEM_alg_frodokem_1344_shake,
KEY_TYPE_ECP_HYB_KEM, 256},
{0, "kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_KEM, 128},
{0, "p256_kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_KEM, 192},
{0, "p384_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "x25519_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "p256_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "kyber1024", OQS_KEM_alg_kyber_1024, KEY_TYPE_KEM, 256},
{0, "p521_kyber1024", OQS_KEM_alg_kyber_1024, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_KEM, 128},
{0, "p256_mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_KEM, 192},
{0, "p384_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "X25519MLKEM768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "SecP256r1MLKEM768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_KEM, 256},
{0, "p521_mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "p384_mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_KEM, 128},
{0, "p256_bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_KEM, 192},
{0, "p384_bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "bikel5", OQS_KEM_alg_bike_l5, KEY_TYPE_KEM, 256},
{0, "p521_bikel5", OQS_KEM_alg_bike_l5, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_KEM, 128},
{0, "p256_hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_KEM, 192},
{0, "p384_hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "hqc256", OQS_KEM_alg_hqc_256, KEY_TYPE_KEM, 256},
{0, "p521_hqc256", OQS_KEM_alg_hqc_256, KEY_TYPE_ECP_HYB_KEM, 256},
KEY_TYPE_ECP_HYB_KEM, 256, 0},
{0, "kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_KEM, 128, 0},
{0, "p256_kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_ECP_HYB_KEM, 128, 0},
{0, "x25519_kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_ECX_HYB_KEM, 128, 0},
{0, "kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_KEM, 192, 0},
{0, "p384_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECP_HYB_KEM, 192, 0},
{0, "x448_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECX_HYB_KEM, 192, 0},
{0, "x25519_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECX_HYB_KEM, 192, 0},
{0, "p256_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECP_HYB_KEM, 192, 0},
{0, "kyber1024", OQS_KEM_alg_kyber_1024, KEY_TYPE_KEM, 256, 0},
{0, "p521_kyber1024", OQS_KEM_alg_kyber_1024, KEY_TYPE_ECP_HYB_KEM, 256, 0},
{0, "mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_KEM, 128, 0},
{0, "p256_mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_ECP_HYB_KEM, 128, 0},
{0, "x25519_mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_ECX_HYB_KEM, 128,
1},
{0, "mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_KEM, 192, 0},
{0, "p384_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECP_HYB_KEM, 192, 0},
{0, "x448_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECX_HYB_KEM, 192, 1},
{0, "X25519MLKEM768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECX_HYB_KEM, 192, 1},
{0, "SecP256r1MLKEM768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECP_HYB_KEM, 192,
0},
{0, "mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_KEM, 256, 0},
{0, "p521_mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_ECP_HYB_KEM, 256,
0},
{0, "p384_mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_ECP_HYB_KEM, 256,
0},
{0, "bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_KEM, 128, 0},
{0, "p256_bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_ECP_HYB_KEM, 128, 0},
{0, "x25519_bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_ECX_HYB_KEM, 128, 0},
{0, "bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_KEM, 192, 0},
{0, "p384_bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_ECP_HYB_KEM, 192, 0},
{0, "x448_bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_ECX_HYB_KEM, 192, 0},
{0, "bikel5", OQS_KEM_alg_bike_l5, KEY_TYPE_KEM, 256, 0},
{0, "p521_bikel5", OQS_KEM_alg_bike_l5, KEY_TYPE_ECP_HYB_KEM, 256, 0},
{0, "hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_KEM, 128, 0},
{0, "p256_hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_ECP_HYB_KEM, 128, 0},
{0, "x25519_hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_ECX_HYB_KEM, 128, 0},
{0, "hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_KEM, 192, 0},
{0, "p384_hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_ECP_HYB_KEM, 192, 0},
{0, "x448_hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_ECX_HYB_KEM, 192, 0},
{0, "hqc256", OQS_KEM_alg_hqc_256, KEY_TYPE_KEM, 256, 0},
{0, "p521_hqc256", OQS_KEM_alg_hqc_256, KEY_TYPE_ECP_HYB_KEM, 256, 0},

#endif /* OQS_KEM_ENCODERS */
{0, "dilithium2", OQS_SIG_alg_dilithium_2, KEY_TYPE_SIG, 128},
Expand Down Expand Up @@ -218,6 +224,15 @@ static int get_secbits(int nid) {
return 0;
}

static int get_reverseshare(int nid) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
if (nid_names[i].nid == nid)
return nid_names[i].reverseshare;
}
return 0;
}

static int get_keytype(int nid) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
Expand Down Expand Up @@ -470,7 +485,8 @@ static OQSX_KEY *oqsx_key_new_from_nid(OSSL_LIB_CTX *libctx, const char *propq,
}

return oqsx_key_new(libctx, get_oqsname(nid), tls_algname, get_keytype(nid),
propq, get_secbits(nid), get_oqsalg_idx(nid), 0);
propq, get_secbits(nid), get_oqsalg_idx(nid),
get_reverseshare(nid));
}

/* Workaround for not functioning EC PARAM initialization
Expand Down Expand Up @@ -912,9 +928,14 @@ static OQSX_KEY *oqsx_key_op(const X509_ALGOR *palg, const unsigned char *p,
if (key->numkeys == 2) {
unsigned char *pubkey = (unsigned char *)key->pubkey;
ENCODE_UINT32(pubkey, key->evp_info->length_public_key);
memcpy(pubkey + SIZE_OF_UINT32 +
key->evp_info->length_public_key,
p + actualprivkeylen, plen - actualprivkeylen);
if (key->reverse_share) {
memcpy(pubkey + SIZE_OF_UINT32, p + actualprivkeylen,
plen - actualprivkeylen);
} else {
memcpy(pubkey + SIZE_OF_UINT32 +
key->evp_info->length_public_key,
p + actualprivkeylen, plen - actualprivkeylen);
}
} else
memcpy(key->pubkey, p + key->privkeylen,
plen - key->privkeylen);
Expand Down
2 changes: 0 additions & 2 deletions test/oqs_test_evp_pkey_params.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ int main(int argc, char **argv) {
}

for (; algs->algorithm_names != NULL; ++algs) {
// if (strcmp(algs->algorithm_names, "x25519_mlkem768")) continue;
if (!is_signature_algorithm_hybrid(algs->algorithm_names)) {
continue;
}
Expand All @@ -583,7 +582,6 @@ int main(int argc, char **argv) {
goto unload_oqs_provider;
}
for (; algs->algorithm_names != NULL; ++algs) {
// if (strcmp(algs->algorithm_names, "x25519_mlkem768")) continue;
if (!is_kem_algorithm_hybrid(algs->algorithm_names)) {
continue;
}
Expand Down

0 comments on commit 1572ebb

Please sign in to comment.