From c72ccc5d942eec9dbaf30cb0acfe31893fa882df Mon Sep 17 00:00:00 2001 From: Felipe Ventura Date: Mon, 11 Dec 2023 09:24:40 -0600 Subject: [PATCH] changed to OpenSSL coding style --- oqsprov/oqs_decode_der2key.c | 107 ++- oqsprov/oqs_encode_key2any.c | 355 +++++----- oqsprov/oqs_kem.c | 9 +- oqsprov/oqs_kmgmt.c | 163 ++--- oqsprov/oqs_prov.h | 400 +++++++---- oqsprov/oqs_sig.c | 1253 ++++++++++++++++++---------------- oqsprov/oqsdecoders.inc | 35 +- oqsprov/oqsencoders.inc | 106 ++- oqsprov/oqsprov.c | 48 +- oqsprov/oqsprov_keys.c | 803 +++++++++++----------- 10 files changed, 1704 insertions(+), 1575 deletions(-) diff --git a/oqsprov/oqs_decode_der2key.c b/oqsprov/oqs_decode_der2key.c index 5ea20a50..8a0629c8 100644 --- a/oqsprov/oqs_decode_der2key.c +++ b/oqsprov/oqs_decode_der2key.c @@ -731,68 +731,47 @@ MAKE_DECODER(, "rsa3072_sphincsshake128fsimple", rsa3072_sphincsshake128fsimple, MAKE_DECODER(, "rsa3072_sphincsshake128fsimple", rsa3072_sphincsshake128fsimple, oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium3_rsa3072", dilithium3_rsa3072, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium3_rsa3072", dilithium3_rsa3072, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium3_p256", dilithium3_p256, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium3_p256", dilithium3_p256, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "falcon512_p256", falcon512_p256, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "falcon512_p256", falcon512_p256, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium5_p384", dilithium5_p384, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium5_p384", dilithium5_p384, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium3_bp256", dilithium3_bp256, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium3_bp256", dilithium3_bp256, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium3_ed25519", dilithium3_ed25519, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium3_ed25519", dilithium3_ed25519, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium5_bp384", dilithium5_bp384, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium5_bp384", dilithium5_bp384, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium5_ed448", dilithium5_ed448, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium5_ed448", dilithium5_ed448, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "falcon512_bp256", falcon512_bp256, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "falcon512_bp256", falcon512_bp256, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "falcon512_ed25519", falcon512_ed25519, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "falcon512_ed25519", falcon512_ed25519, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium3_pss3072", dilithium3_pss3072, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium3_pss3072", dilithium3_pss3072, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium2_pss2048", dilithium2_pss2048, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium2_pss2048", dilithium2_pss2048, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium2_rsa2048", dilithium2_rsa2048, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium2_rsa2048", dilithium2_rsa2048, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium2_ed25519", dilithium2_ed25519, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium2_ed25519", dilithium2_ed25519, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium2_p256", dilithium2_p256, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium2_p256", dilithium2_p256, - oqsx, SubjectPublicKeyInfo); -MAKE_DECODER(, "dilithium2_bp256", dilithium2_bp256, - oqsx, PrivateKeyInfo); -MAKE_DECODER(, "dilithium2_bp256", dilithium2_bp256, - oqsx, SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium3_rsa3072", dilithium3_rsa3072, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium3_rsa3072", dilithium3_rsa3072, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium3_p256", dilithium3_p256, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium3_p256", dilithium3_p256, oqsx, SubjectPublicKeyInfo); +MAKE_DECODER(, "falcon512_p256", falcon512_p256, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "falcon512_p256", falcon512_p256, oqsx, SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium5_p384", dilithium5_p384, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium5_p384", dilithium5_p384, oqsx, SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium3_bp256", dilithium3_bp256, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium3_bp256", dilithium3_bp256, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium3_ed25519", dilithium3_ed25519, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium3_ed25519", dilithium3_ed25519, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium5_bp384", dilithium5_bp384, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium5_bp384", dilithium5_bp384, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium5_ed448", dilithium5_ed448, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium5_ed448", dilithium5_ed448, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "falcon512_bp256", falcon512_bp256, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "falcon512_bp256", falcon512_bp256, oqsx, SubjectPublicKeyInfo); +MAKE_DECODER(, "falcon512_ed25519", falcon512_ed25519, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "falcon512_ed25519", falcon512_ed25519, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium3_pss3072", dilithium3_pss3072, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium3_pss3072", dilithium3_pss3072, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium2_pss2048", dilithium2_pss2048, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium2_pss2048", dilithium2_pss2048, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium2_rsa2048", dilithium2_rsa2048, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium2_rsa2048", dilithium2_rsa2048, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium2_ed25519", dilithium2_ed25519, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium2_ed25519", dilithium2_ed25519, oqsx, + SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium2_p256", dilithium2_p256, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium2_p256", dilithium2_p256, oqsx, SubjectPublicKeyInfo); +MAKE_DECODER(, "dilithium2_bp256", dilithium2_bp256, oqsx, PrivateKeyInfo); +MAKE_DECODER(, "dilithium2_bp256", dilithium2_bp256, oqsx, + SubjectPublicKeyInfo); ///// OQS_TEMPLATE_FRAGMENT_DECODER_MAKE_END diff --git a/oqsprov/oqs_encode_key2any.c b/oqsprov/oqs_encode_key2any.c index 199188e8..6d424017 100644 --- a/oqsprov/oqs_encode_key2any.c +++ b/oqsprov/oqs_encode_key2any.c @@ -9,6 +9,7 @@ */ #include "oqs_endecoder_local.h" +#include "oqs_prov.h" #include #include #include @@ -21,7 +22,6 @@ #include #include #include -#include "oqs_prov.h" #include #include @@ -57,7 +57,6 @@ struct key2any_ctx_st { void *pwcbarg; }; - typedef int check_key_type_fn(const void *key, int nid); typedef int key_to_paramstring_fn(const void *key, int nid, int save, void **str, int *strtype); @@ -302,11 +301,11 @@ static int key_to_pki_pem_priv_bio(BIO *out, const void *key, int key_nid, if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; - p8info = key_to_p8info(key, key_nid, str, strtype, k2d); - if (p8info != NULL) - ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info); - else - free_asn1_data(strtype, str); + p8info = key_to_p8info(key, key_nid, str, strtype, k2d); + if (p8info != NULL) + ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info); + else + free_asn1_data(strtype, str); PKCS8_PRIV_KEY_INFO_free(p8info); @@ -329,10 +328,8 @@ static int key_to_spki_der_pub_bio(BIO *out, const void *key, int key_nid, if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; - xpk = oqsx_key_to_pubkey(key, key_nid, str, strtype, k2d); - if (xpk != NULL) ret = i2d_X509_PUBKEY_bio(out, xpk); @@ -513,7 +510,7 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder) ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER); return 0; } - if (oqsxkey->keytype != KEY_TYPE_CMP_SIG){ + if (oqsxkey->keytype != KEY_TYPE_CMP_SIG) { #ifdef USE_ENCODING_LIB if (oqsxkey->oqsx_encoding_ctx.encoding_ctx != NULL && oqsxkey->oqsx_encoding_ctx.encoding_impl != NULL) { @@ -525,8 +522,8 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder) buf = OPENSSL_secure_zalloc(buflen); ret = qsc_encode(encoding_ctx->encoding_ctx, - encoding_ctx->encoding_impl, oqsxkey->pubkey, &buf, 0, - 0, 1); + encoding_ctx->encoding_impl, oqsxkey->pubkey, &buf, + 0, 0, 1); if (ret != QSC_ENC_OK) return -1; @@ -541,20 +538,25 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder) } *pder = keyblob; return oqsxkey->pubkeylen; - #ifdef USE_ENCODING_LIB +#ifdef USE_ENCODING_LIB } - #endif - }else{ - ASN1_TYPE **aType = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE)); - ASN1_STRING **aString = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); - ASN1_STRING **tempOct = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); - unsigned char **temp = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); - unsigned char **cbuf = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); +#endif + } else { + ASN1_TYPE **aType + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE)); + ASN1_STRING **aString + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); + ASN1_STRING **tempOct + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); + unsigned char **temp + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); + unsigned char **cbuf + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); int len, i; - if((sk = sk_ASN1_TYPE_new_null()) == NULL) + if ((sk = sk_ASN1_TYPE_new_null()) == NULL) return -1; - for (i = 0; i < oqsxkey->numkeys; i++){ + for (i = 0; i < oqsxkey->numkeys; i++) { aType[i] = ASN1_TYPE_new(); aString[i] = ASN1_OCTET_STRING_new(); tempOct[i] = ASN1_OCTET_STRING_new(); @@ -567,8 +569,8 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder) ASN1_STRING_set0(aString[i], temp[i], keybloblen); ASN1_TYPE_set(aType[i], V_ASN1_SEQUENCE, aString[i]); - if (!sk_ASN1_TYPE_push(sk, aType[i])){ - for (i = 0; i < oqsxkey->numkeys; i++){ + if (!sk_ASN1_TYPE_push(sk, aType[i])) { + for (i = 0; i < oqsxkey->numkeys; i++) { OPENSSL_free(temp[i]); OPENSSL_free(cbuf[i]); OPENSSL_free(aType[i]); @@ -581,7 +583,7 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder) } keybloblen = i2d_ASN1_SEQUENCE_ANY(sk, pder); - for (i = 0; i < oqsxkey->numkeys; i++){ + for (i = 0; i < oqsxkey->numkeys; i++) { OPENSSL_free(temp[i]); OPENSSL_free(cbuf[i]); OPENSSL_free(aType[i]); @@ -589,11 +591,9 @@ static int oqsx_spki_pub_to_der(const void *vxkey, unsigned char **pder) OPENSSL_free(tempOct[i]); } OPENSSL_free(sk); - + return keybloblen; } - - } static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) @@ -604,7 +604,7 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) ASN1_OCTET_STRING oct; int keybloblen, nid; STACK_OF(ASN1_TYPE) *sk = NULL; - char* name; + char *name; OQS_ENC_PRINTF("OQS ENC provider: oqsx_pki_priv_to_der called\n"); @@ -623,7 +623,7 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) // only concatenate private classic key (if any) and OQS private and public // key NOT saving public classic key component (if any) - if (oqsxkey->keytype != KEY_TYPE_CMP_SIG){ + if (oqsxkey->keytype != KEY_TYPE_CMP_SIG) { privkeylen = oqsxkey->privkeylen; if (oqsxkey->numkeys > 1) { // hybrid int actualprivkeylen; @@ -635,51 +635,51 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) privkeylen -= (oqsxkey->evp_info->length_private_key - actualprivkeylen); } - #ifdef USE_ENCODING_LIB +#ifdef USE_ENCODING_LIB if (oqsxkey->oqsx_encoding_ctx.encoding_ctx != NULL && oqsxkey->oqsx_encoding_ctx.encoding_impl != NULL) { const OQSX_ENCODING_CTX *encoding_ctx = &oqsxkey->oqsx_encoding_ctx; int ret = 0; - # ifdef NOPUBKEY_IN_PRIVKEY - int withoptional - = (encoding_ctx->encoding_ctx->raw_private_key_encodes_public_key - ? 1 - : 0); - # else +# ifdef NOPUBKEY_IN_PRIVKEY + int withoptional = (encoding_ctx->encoding_ctx + ->raw_private_key_encodes_public_key + ? 1 + : 0); +# else int withoptional = 1; - # endif - buflen - = (withoptional ? encoding_ctx->encoding_impl->crypto_secretkeybytes - : encoding_ctx->encoding_impl - ->crypto_secretkeybytes_nooptional); +# endif + buflen = (withoptional + ? encoding_ctx->encoding_impl->crypto_secretkeybytes + : encoding_ctx->encoding_impl + ->crypto_secretkeybytes_nooptional); buf = OPENSSL_secure_zalloc(buflen); ret = qsc_encode(encoding_ctx->encoding_ctx, - encoding_ctx->encoding_impl, - oqsxkey->comp_pubkey[oqsxkey->numkeys - 1], 0, - oqsxkey->privkey, &buf, withoptional); + encoding_ctx->encoding_impl, + oqsxkey->comp_pubkey[oqsxkey->numkeys - 1], 0, + oqsxkey->privkey, &buf, withoptional); if (ret != QSC_ENC_OK) return -1; } else { - #endif - #ifdef NOPUBKEY_IN_PRIVKEY +#endif +#ifdef NOPUBKEY_IN_PRIVKEY buflen = privkeylen; buf = OPENSSL_secure_malloc(buflen); OQS_ENC_PRINTF2("OQS ENC provider: saving privkey of length %d\n", buflen); memcpy(buf, oqsxkey->privkey, privkeylen); - #else +#else buflen = privkeylen + oqsx_key_get_oqs_public_key_len(oqsxkey); buf = OPENSSL_secure_malloc(buflen); 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)); - #endif - #ifdef USE_ENCODING_LIB + oqsx_key_get_oqs_public_key_len(oqsxkey)); +#endif +#ifdef USE_ENCODING_LIB } - #endif +#endif oct.data = buf; oct.length = buflen; @@ -693,25 +693,31 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); keybloblen = 0; // signal error } - }else{ - ASN1_TYPE **aType = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE)); - ASN1_STRING **aString = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); - ASN1_STRING **tempOct = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); - unsigned char **temp = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); - unsigned char **cbuf = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); + } else { + ASN1_TYPE **aType + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_TYPE)); + ASN1_STRING **aString + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); + ASN1_STRING **tempOct + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(ASN1_STRING)); + unsigned char **temp + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); + unsigned char **cbuf + = OPENSSL_malloc(oqsxkey->numkeys * sizeof(void *)); int i; - if((sk = sk_ASN1_TYPE_new_null()) == NULL) + if ((sk = sk_ASN1_TYPE_new_null()) == NULL) return -1; - for (i = 0; i < oqsxkey->numkeys; i++){ + for (i = 0; i < oqsxkey->numkeys; i++) { aType[i] = ASN1_TYPE_new(); aString[i] = ASN1_OCTET_STRING_new(); tempOct[i] = ASN1_OCTET_STRING_new(); temp[i] = NULL; - if ((name = get_cmpname(OBJ_sn2nid(oqsxkey->tls_name), i)) == NULL){ + if ((name = get_cmpname(OBJ_sn2nid(oqsxkey->tls_name), i)) + == NULL) { OPENSSL_free(name); - for (i = 0; i < oqsxkey->numkeys; i++){ + for (i = 0; i < oqsxkey->numkeys; i++) { OPENSSL_free(temp[i]); OPENSSL_free(cbuf[i]); OPENSSL_free(aType[i]); @@ -721,17 +727,20 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) OPENSSL_free(sk); return -1; } - - if(get_oqsname_fromtls(name) == 0){ - if (oqsxkey->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype == EVP_PKEY_RSA){ //get the RSA real key size - unsigned char* enc_len = OPENSSL_strndup(oqsxkey->comp_privkey[i], 4); + + if (get_oqsname_fromtls(name) == 0) { + if (oqsxkey->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype + == EVP_PKEY_RSA) { // get the RSA real key size + unsigned char *enc_len + = OPENSSL_strndup(oqsxkey->comp_privkey[i], 4); OPENSSL_cleanse(enc_len, 2); DECODE_UINT32(buflen, enc_len); buflen += 4; OPENSSL_free(enc_len); - if (buflen > oqsxkey->privkeylen_cmp[i]){ + if (buflen > oqsxkey->privkeylen_cmp[i]) { OPENSSL_free(name); - for (i = 0; i < oqsxkey->numkeys; i++){ + for (i = 0; i < oqsxkey->numkeys; i++) { OPENSSL_free(temp[i]); OPENSSL_free(cbuf[i]); OPENSSL_free(aType[i]); @@ -742,17 +751,20 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); return -1; } - }else + } else buflen = oqsxkey->privkeylen_cmp[i]; - }else + } else buflen = oqsxkey->privkeylen_cmp[i] + oqsxkey->pubkeylen_cmp[i]; cbuf[i] = OPENSSL_malloc(buflen); memcpy(cbuf[i], oqsxkey->comp_privkey[i], buflen); - if(get_oqsname_fromtls(name) != 0){//include pubkey in privkey for PQC - memcpy(cbuf[i], oqsxkey->comp_privkey[i], oqsxkey->privkeylen_cmp[i]); - memcpy(cbuf[i] + oqsxkey->privkeylen_cmp[i], oqsxkey->comp_pubkey[i], oqsxkey->pubkeylen_cmp[i]); - }else + if (get_oqsname_fromtls(name) + != 0) { // include pubkey in privkey for PQC + memcpy(cbuf[i], oqsxkey->comp_privkey[i], + oqsxkey->privkeylen_cmp[i]); + memcpy(cbuf[i] + oqsxkey->privkeylen_cmp[i], + oqsxkey->comp_pubkey[i], oqsxkey->pubkeylen_cmp[i]); + } else memcpy(cbuf[i], oqsxkey->comp_privkey[i], buflen); ASN1_STRING_set0(tempOct[i], cbuf[i], buflen); @@ -760,8 +772,8 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) ASN1_STRING_set0(aString[i], temp[i], keybloblen); ASN1_TYPE_set(aType[i], V_ASN1_SEQUENCE, aString[i]); - if (!sk_ASN1_TYPE_push(sk, aType[i])){ - for (i = 0; i < oqsxkey->numkeys; i++){ + if (!sk_ASN1_TYPE_push(sk, aType[i])) { + for (i = 0; i < oqsxkey->numkeys; i++) { OPENSSL_free(temp[i]); OPENSSL_free(cbuf[i]); OPENSSL_free(aType[i]); @@ -776,7 +788,7 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) } keybloblen = i2d_ASN1_SEQUENCE_ANY(sk, pder); - for (i = 0; i < oqsxkey->numkeys; i++){ + for (i = 0; i < oqsxkey->numkeys; i++) { OPENSSL_free(temp[i]); OPENSSL_free(cbuf[i]); OPENSSL_free(aType[i]); @@ -1015,54 +1027,54 @@ static int oqsx_pki_priv_to_der(const void *vxkey, unsigned char **pder) #define rsa3072_sphincsshake128fsimple_input_type \ "rsa3072_sphincsshake128fsimple" #define rsa3072_sphincsshake128fsimple_pem_type "rsa3072_sphincsshake128fsimple" -# define dilithium2_pss2048_evp_type 0 -# define dilithium2_pss2048_input_type "dilithium2_pss2048" -# define dilithium2_pss2048_pem_type "dilithium2_pss2048" -# define dilithium2_rsa2048_evp_type 0 -# define dilithium2_rsa2048_input_type "dilithium2_rsa2048" -# define dilithium2_rsa2048_pem_type "dilithium2_rsa2048" -# define dilithium2_ed25519_evp_type 0 -# define dilithium2_ed25519_input_type "dilithium2_ed25519" -# define dilithium2_ed25519_pem_type "dilithium2_ed25519" -# define dilithium2_p256_evp_type 0 -# define dilithium2_p256_input_type "dilithium2_p256" -# define dilithium2_p256_pem_type "dilithium2_p256" -# define dilithium2_bp256_evp_type 0 -# define dilithium2_bp256_input_type "dilithium2_bp256" -# define dilithium2_bp256_pem_type "dilithium2_bp256" -# define dilithium3_rsa2048_evp_type 0 -# define dilithium3_rsa3072_input_type "dilithium3_rsa3072" -# define dilithium3_rsa3072_pem_type "dilithium3_rsa3072" -# define dilithium3_p256_evp_type 0 -# define dilithium3_p256_input_type "dilithium3_p256" -# define dilithium3_p256_pem_type "dilithium3_p256" -# define falcon512_p256_evp_type 0 -# define falcon512_p256_input_type "falcon512_p256" -# define falcon512_p256_pem_type "falcon512_p256" -# define dilithium5_p384_evp_type 0 -# define dilithium5_p384_input_type "dilithium5_p384" -# define dilithium5_p384_pem_type "dilithium5_p384" -# define dilithium3_bp256_evp_type 0 -# define dilithium3_bp256_input_type "dilithium3_bp256" -# define dilithium3_bp256_pem_type "dilithium3_bp256" -# define dilithium3_ed25519_evp_type 0 -# define dilithium3_ed25519_input_type "dilithium3_ed25519" -# define dilithium3_ed25519_pem_type "dilithium3_ed25519" -# define dilithium3_pss3072_evp_type 0 -# define dilithium3_pss3072_input_type "dilithium3_pss3072" -# define dilithium3_pss3072_pem_type "dilithium3_pss3072" -# define dilithium5_bp384_evp_type 0 -# define dilithium5_bp384_input_type "dilithium5_bp384" -# define dilithium5_bp384_pem_type "dilithium5_bp384" -# define dilithium5_ed448_evp_type 0 -# define dilithium5_ed448_input_type "dilithium5_ed448" -# define dilithium5_ed448_pem_type "dilithium5_ed448" -# define falcon512_bp256_evp_type 0 -# define falcon512_bp256_input_type "falcon512_bp256" -# define falcon512_bp256_pem_type "falcon512_bp256" -# define falcon512_ed25519_evp_type 0 -# define falcon512_ed25519_input_type "falcon512_ed25519" -# define falcon512_ed25519_pem_type "falcon512_ed25519" +#define dilithium2_pss2048_evp_type 0 +#define dilithium2_pss2048_input_type "dilithium2_pss2048" +#define dilithium2_pss2048_pem_type "dilithium2_pss2048" +#define dilithium2_rsa2048_evp_type 0 +#define dilithium2_rsa2048_input_type "dilithium2_rsa2048" +#define dilithium2_rsa2048_pem_type "dilithium2_rsa2048" +#define dilithium2_ed25519_evp_type 0 +#define dilithium2_ed25519_input_type "dilithium2_ed25519" +#define dilithium2_ed25519_pem_type "dilithium2_ed25519" +#define dilithium2_p256_evp_type 0 +#define dilithium2_p256_input_type "dilithium2_p256" +#define dilithium2_p256_pem_type "dilithium2_p256" +#define dilithium2_bp256_evp_type 0 +#define dilithium2_bp256_input_type "dilithium2_bp256" +#define dilithium2_bp256_pem_type "dilithium2_bp256" +#define dilithium3_rsa2048_evp_type 0 +#define dilithium3_rsa3072_input_type "dilithium3_rsa3072" +#define dilithium3_rsa3072_pem_type "dilithium3_rsa3072" +#define dilithium3_p256_evp_type 0 +#define dilithium3_p256_input_type "dilithium3_p256" +#define dilithium3_p256_pem_type "dilithium3_p256" +#define falcon512_p256_evp_type 0 +#define falcon512_p256_input_type "falcon512_p256" +#define falcon512_p256_pem_type "falcon512_p256" +#define dilithium5_p384_evp_type 0 +#define dilithium5_p384_input_type "dilithium5_p384" +#define dilithium5_p384_pem_type "dilithium5_p384" +#define dilithium3_bp256_evp_type 0 +#define dilithium3_bp256_input_type "dilithium3_bp256" +#define dilithium3_bp256_pem_type "dilithium3_bp256" +#define dilithium3_ed25519_evp_type 0 +#define dilithium3_ed25519_input_type "dilithium3_ed25519" +#define dilithium3_ed25519_pem_type "dilithium3_ed25519" +#define dilithium3_pss3072_evp_type 0 +#define dilithium3_pss3072_input_type "dilithium3_pss3072" +#define dilithium3_pss3072_pem_type "dilithium3_pss3072" +#define dilithium5_bp384_evp_type 0 +#define dilithium5_bp384_input_type "dilithium5_bp384" +#define dilithium5_bp384_pem_type "dilithium5_bp384" +#define dilithium5_ed448_evp_type 0 +#define dilithium5_ed448_input_type "dilithium5_ed448" +#define dilithium5_ed448_pem_type "dilithium5_ed448" +#define falcon512_bp256_evp_type 0 +#define falcon512_bp256_input_type "falcon512_bp256" +#define falcon512_bp256_pem_type "falcon512_bp256" +#define falcon512_ed25519_evp_type 0 +#define falcon512_ed25519_input_type "falcon512_ed25519" +#define falcon512_ed25519_pem_type "falcon512_ed25519" ///// OQS_TEMPLATE_FRAGMENT_ENCODER_DEFINES_END /* ---------------------------------------------------------------------- */ @@ -1476,7 +1488,8 @@ static int oqsx_to_text(BIO *out, const void *key, int selection) return 0; break; case KEY_TYPE_CMP_SIG: - if (BIO_printf(out, "%s composite private key:\n", okey->tls_name) <= 0) + if (BIO_printf(out, "%s composite private key:\n", okey->tls_name) + <= 0) return 0; break; default: @@ -1502,7 +1515,8 @@ static int oqsx_to_text(BIO *out, const void *key, int selection) return 0; break; case KEY_TYPE_CMP_SIG: - if (BIO_printf(out, "%s composite public key:\n", okey->tls_name) <= 0) + if (BIO_printf(out, "%s composite public key:\n", okey->tls_name) + <= 0) return 0; break; default: @@ -1513,58 +1527,65 @@ static int oqsx_to_text(BIO *out, const void *key, int selection) if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { if (okey->privkey) { - if (okey->keytype == KEY_TYPE_CMP_SIG){ + if (okey->keytype == KEY_TYPE_CMP_SIG) { char *name; char label[200]; int i, privlen; - for (i = 0; i < okey->numkeys; i++){ - if ((name = get_cmpname(OBJ_sn2nid(okey->tls_name), i)) == NULL){ + for (i = 0; i < okey->numkeys; i++) { + if ((name = get_cmpname(OBJ_sn2nid(okey->tls_name), i)) + == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_KEY); return 0; } sprintf(label, "%s key material:", name); - if(get_oqsname_fromtls(name) == 0 //classical key - && okey->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype == EVP_PKEY_RSA){ //get the RSA real key size - unsigned char* enc_len = OPENSSL_strndup(okey->comp_privkey[i], 4); + if (get_oqsname_fromtls(name) == 0 // classical key + && okey->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype + == EVP_PKEY_RSA) { // get the RSA real key size + unsigned char *enc_len + = OPENSSL_strndup(okey->comp_privkey[i], 4); OPENSSL_cleanse(enc_len, 2); DECODE_UINT32(privlen, enc_len); privlen += 4; OPENSSL_free(enc_len); - if (privlen > okey->privkeylen_cmp[i]){ + if (privlen > okey->privkeylen_cmp[i]) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); return 0; } - }else - privlen = okey->privkeylen_cmp[i]; - if (!print_labeled_buf(out, label, - okey->comp_privkey[i], privlen)) + } else + privlen = okey->privkeylen_cmp[i]; + if (!print_labeled_buf(out, label, okey->comp_privkey[i], + privlen)) return 0; - + OPENSSL_free(name); } - }else{ + } else { if (okey->numkeys > 1) { // hybrid key char classic_label[200]; int classic_key_len = 0; - sprintf(classic_label, - "%s key material:", OBJ_nid2sn(okey->evp_info->nid)); + sprintf(classic_label, "%s key material:", + OBJ_nid2sn(okey->evp_info->nid)); DECODE_UINT32(classic_key_len, okey->privkey); if (!print_labeled_buf(out, classic_label, - okey->comp_privkey[0], classic_key_len)) + okey->comp_privkey[0], + classic_key_len)) return 0; /* finally print pure PQ key */ - if (!print_labeled_buf(out, "PQ key material:", - okey->comp_privkey[okey->numkeys - 1], - okey->privkeylen - classic_key_len - - SIZE_OF_UINT32)) + if (!print_labeled_buf( + out, "PQ key material:", + okey->comp_privkey[okey->numkeys - 1], + okey->privkeylen - classic_key_len + - SIZE_OF_UINT32)) return 0; } else { // plain PQ key - if (!print_labeled_buf(out, "PQ key material:", - okey->comp_privkey[okey->numkeys - 1], - okey->privkeylen)) + if (!print_labeled_buf( + out, "PQ key material:", + okey->comp_privkey[okey->numkeys - 1], + okey->privkeylen)) return 0; } } @@ -1572,44 +1593,46 @@ static int oqsx_to_text(BIO *out, const void *key, int selection) } if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { if (okey->pubkey) { - if (okey->keytype == KEY_TYPE_CMP_SIG){ + if (okey->keytype == KEY_TYPE_CMP_SIG) { char *name; char label[200]; int i; - for (i = 0; i < okey->numkeys; i++){ - if ((name = get_cmpname(OBJ_sn2nid(okey->tls_name), i)) == NULL){ + for (i = 0; i < okey->numkeys; i++) { + if ((name = get_cmpname(OBJ_sn2nid(okey->tls_name), i)) + == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_KEY); return 0; } sprintf(label, "%s key material:", name); - if (!print_labeled_buf(out, label, - okey->comp_pubkey[i], okey->pubkeylen_cmp[i])) + if (!print_labeled_buf(out, label, okey->comp_pubkey[i], + okey->pubkeylen_cmp[i])) return 0; - + OPENSSL_free(name); } - }else{ + } else { if (okey->numkeys > 1) { // hybrid key char classic_label[200]; int classic_key_len = 0; DECODE_UINT32(classic_key_len, okey->pubkey); - sprintf(classic_label, - "%s key material:", OBJ_nid2sn(okey->evp_info->nid)); - if (!print_labeled_buf(out, classic_label, okey->comp_pubkey[0], - classic_key_len)) + sprintf(classic_label, "%s key material:", + OBJ_nid2sn(okey->evp_info->nid)); + if (!print_labeled_buf(out, classic_label, + okey->comp_pubkey[0], + classic_key_len)) return 0; /* finally print pure PQ key */ if (!print_labeled_buf(out, "PQ key material:", - okey->comp_pubkey[okey->numkeys - 1], - okey->pubkeylen - classic_key_len - - SIZE_OF_UINT32)) + okey->comp_pubkey[okey->numkeys - 1], + okey->pubkeylen - classic_key_len + - SIZE_OF_UINT32)) return 0; } else { // PQ key only if (!print_labeled_buf(out, "PQ key material:", - okey->comp_pubkey[okey->numkeys - 1], - okey->pubkeylen)) + okey->comp_pubkey[okey->numkeys - 1], + okey->pubkeylen)) return 0; } } diff --git a/oqsprov/oqs_kem.c b/oqsprov/oqs_kem.c index fee636bb..ce68ef39 100644 --- a/oqsprov/oqs_kem.c +++ b/oqsprov/oqs_kem.c @@ -171,7 +171,8 @@ static int oqs_evp_kem_encaps_keyslot(void *vpkemctx, unsigned char *ct, int ret = OQS_SUCCESS, ret2 = 0; const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx; - const OQSX_EVP_CTX *evp_ctx = pkemctx->kem->oqsx_provider_ctx[0].oqsx_evp_ctx; + const OQSX_EVP_CTX *evp_ctx + = pkemctx->kem->oqsx_provider_ctx[0].oqsx_evp_ctx; size_t pubkey_kexlen = 0; size_t kexDeriveLen = 0, pkeylen = 0; @@ -249,7 +250,8 @@ static int oqs_evp_kem_decaps_keyslot(void *vpkemctx, unsigned char *secret, int ret = OQS_SUCCESS, ret2 = 0; const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx; - const OQSX_EVP_CTX *evp_ctx = pkemctx->kem->oqsx_provider_ctx[0].oqsx_evp_ctx; + const OQSX_EVP_CTX *evp_ctx + = pkemctx->kem->oqsx_provider_ctx[0].oqsx_evp_ctx; size_t pubkey_kexlen = evp_ctx->evp_info->length_public_key; size_t kexDeriveLen = evp_ctx->evp_info->kex_length_secret; @@ -351,7 +353,8 @@ static int oqs_hyb_kem_decaps(void *vpkemctx, unsigned char *secret, { int ret = OQS_SUCCESS; const PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx; - const OQSX_EVP_CTX *evp_ctx = pkemctx->kem->oqsx_provider_ctx[0].oqsx_evp_ctx; + const OQSX_EVP_CTX *evp_ctx + = pkemctx->kem->oqsx_provider_ctx[0].oqsx_evp_ctx; const OQS_KEM *qs_ctx = pkemctx->kem->oqsx_provider_ctx[0].oqsx_qs_ctx.kem; size_t secretLen0 = 0, secretLen1 = 0; diff --git a/oqsprov/oqs_kmgmt.c b/oqsprov/oqs_kmgmt.c index e5abf132..dcde2dbc 100644 --- a/oqsprov/oqs_kmgmt.c +++ b/oqsprov/oqs_kmgmt.c @@ -468,10 +468,8 @@ static void *oqsx_gen_init(void *provctx, int selection, char *oqs_name, OSSL_LIB_CTX *libctx = PROV_OQS_LIBCTX_OF(provctx); struct oqsx_gen_ctx *gctx = NULL; - OQS_KM_PRINTF2("OQSKEYMGMT: gen_init called for key %s \n", oqs_name); - if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { gctx->libctx = libctx; gctx->cmp_name = NULL; @@ -865,229 +863,196 @@ static void *rsa3072_sphincsshake128fsimple_gen_init(void *provctx, "rsa3072_sphincsshake128fsimple", KEY_TYPE_HYB_SIG, 128, 22); } - static void *dilithium3_rsa3072_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, - "dilithium3_rsa3072", KEY_TYPE_CMP_SIG, NULL, 128, 23); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, + "dilithium3_rsa3072", KEY_TYPE_CMP_SIG, NULL, 128, 23); } static void *dilithium3_rsa3072_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_3, - "dilithium3_rsa3072", KEY_TYPE_CMP_SIG, 128, 23); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_3, + "dilithium3_rsa3072", KEY_TYPE_CMP_SIG, 128, 23); } static void *dilithium3_p256_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, - "dilithium3_p256", KEY_TYPE_CMP_SIG, NULL, 128, 24); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, + "dilithium3_p256", KEY_TYPE_CMP_SIG, NULL, 128, 24); } static void *dilithium3_p256_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_3, - "dilithium3_p256", KEY_TYPE_CMP_SIG, 128, 24); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_3, + "dilithium3_p256", KEY_TYPE_CMP_SIG, 128, 24); } static void *falcon512_p256_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_falcon_512, - "falcon512_p256", KEY_TYPE_CMP_SIG, NULL, 128, 25); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_falcon_512, + "falcon512_p256", KEY_TYPE_CMP_SIG, NULL, 128, 25); } static void *falcon512_p256_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_falcon_512, - "falcon512_p256", KEY_TYPE_CMP_SIG, 128, 25); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_falcon_512, + "falcon512_p256", KEY_TYPE_CMP_SIG, 128, 25); } static void *dilithium5_p384_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_5, - "dilithium5_p384", KEY_TYPE_CMP_SIG, NULL, 192, 26); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_5, + "dilithium5_p384", KEY_TYPE_CMP_SIG, NULL, 192, 26); } static void *dilithium5_p384_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_5, - "dilithium5_p384", KEY_TYPE_CMP_SIG, 192, 26); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_5, + "dilithium5_p384", KEY_TYPE_CMP_SIG, 192, 26); } static void *dilithium3_bp256_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, - "dilithium3_bp256", KEY_TYPE_CMP_SIG, NULL, 256, 27); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, + "dilithium3_bp256", KEY_TYPE_CMP_SIG, NULL, 256, 27); } static void *dilithium3_bp256_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_3, - "dilithium3_bp256", KEY_TYPE_CMP_SIG, 256, 27); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_3, + "dilithium3_bp256", KEY_TYPE_CMP_SIG, 256, 27); } static void *dilithium3_ed25519_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, - "dilithium3_ed25519", KEY_TYPE_CMP_SIG, NULL, 128, 28); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, + "dilithium3_ed25519", KEY_TYPE_CMP_SIG, NULL, 128, 28); } static void *dilithium3_ed25519_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_3, - "dilithium3_ed25519", KEY_TYPE_CMP_SIG, 128, 28); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_3, + "dilithium3_ed25519", KEY_TYPE_CMP_SIG, 128, 28); } static void *dilithium5_bp384_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_5, - "dilithium5_bp384", KEY_TYPE_CMP_SIG, NULL, 384, 29); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_5, + "dilithium5_bp384", KEY_TYPE_CMP_SIG, NULL, 384, 29); } static void *dilithium5_bp384_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_5, - "dilithium5_bp384", KEY_TYPE_CMP_SIG, 384, 29); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_5, + "dilithium5_bp384", KEY_TYPE_CMP_SIG, 384, 29); } static void *dilithium5_ed448_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_5, - "dilithium5_ed448", KEY_TYPE_CMP_SIG, NULL, 192, 30); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_5, + "dilithium5_ed448", KEY_TYPE_CMP_SIG, NULL, 192, 30); } static void *dilithium5_ed448_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_5, - "dilithium5_ed448", KEY_TYPE_CMP_SIG, 192, 30); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_5, + "dilithium5_ed448", KEY_TYPE_CMP_SIG, 192, 30); } static void *falcon512_bp256_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_falcon_512, - "falcon512_bp256", KEY_TYPE_CMP_SIG, NULL, 256, 31); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_falcon_512, + "falcon512_bp256", KEY_TYPE_CMP_SIG, NULL, 256, 31); } static void *falcon512_bp256_gen_init(void *provctx, int selection) { - return oqsx_gen_init - (provctx, selection, OQS_SIG_alg_falcon_512, - "falcon512_bp256", KEY_TYPE_CMP_SIG, 256, 31); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_falcon_512, + "falcon512_bp256", KEY_TYPE_CMP_SIG, 256, 31); } static void *falcon512_ed25519_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_falcon_512, - "falcon512_ed25519", KEY_TYPE_CMP_SIG, NULL, 128, 32); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_falcon_512, + "falcon512_ed25519", KEY_TYPE_CMP_SIG, NULL, 128, 32); } static void *falcon512_ed25519_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_falcon_512, - "falcon512_ed25519", KEY_TYPE_CMP_SIG, 128, 32); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_falcon_512, + "falcon512_ed25519", KEY_TYPE_CMP_SIG, 128, 32); } static void *dilithium3_pss3072_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, - "dilithium3_pss3072", KEY_TYPE_CMP_SIG, NULL, 128, 33); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_3, + "dilithium3_pss3072", KEY_TYPE_CMP_SIG, NULL, 128, 33); } static void *dilithium3_pss3072_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_3, - "dilithium3_pss3072", KEY_TYPE_CMP_SIG, 128, 33); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_3, + "dilithium3_pss3072", KEY_TYPE_CMP_SIG, 128, 33); } static void *dilithium2_pss2048_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, - "dilithium2_pss2048", KEY_TYPE_CMP_SIG, NULL, 112, 34); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, + "dilithium2_pss2048", KEY_TYPE_CMP_SIG, NULL, 112, 34); } static void *dilithium2_pss2048_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_2, - "dilithium2_pss2048", KEY_TYPE_CMP_SIG, 112, 34); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_2, + "dilithium2_pss2048", KEY_TYPE_CMP_SIG, 112, 34); } static void *dilithium2_rsa2048_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, - "dilithium2_rsa2048", KEY_TYPE_CMP_SIG, NULL, 112, 35); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, + "dilithium2_rsa2048", KEY_TYPE_CMP_SIG, NULL, 112, 35); } static void *dilithium2_rsa2048_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_2, - "dilithium2_rsa2048", KEY_TYPE_CMP_SIG, 112, 35); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_2, + "dilithium2_rsa2048", KEY_TYPE_CMP_SIG, 112, 35); } static void *dilithium2_ed25519_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, - "dilithium2_ed25519", KEY_TYPE_CMP_SIG, NULL, 128, 36); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, + "dilithium2_ed25519", KEY_TYPE_CMP_SIG, NULL, 128, 36); } static void *dilithium2_ed25519_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_2, - "dilithium2_ed25519", KEY_TYPE_CMP_SIG, 128, 36); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_2, + "dilithium2_ed25519", KEY_TYPE_CMP_SIG, 128, 36); } static void *dilithium2_p256_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, - "dilithium2_p256", KEY_TYPE_CMP_SIG, NULL, 128, 37); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, + "dilithium2_p256", KEY_TYPE_CMP_SIG, NULL, 128, 37); } static void *dilithium2_p256_gen_init(void *provctx, int selection) { - return oqsx_gen_init( - provctx, selection, OQS_SIG_alg_dilithium_2, - "dilithium2_p256", KEY_TYPE_CMP_SIG, 128, 37); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_2, + "dilithium2_p256", KEY_TYPE_CMP_SIG, 128, 37); } static void *dilithium2_bp256_new_key(void *provctx) { - return oqsx_key_new( - PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, - "dilithium2_bp256", KEY_TYPE_CMP_SIG, NULL, 256, 38); + return oqsx_key_new(PROV_OQS_LIBCTX_OF(provctx), OQS_SIG_alg_dilithium_2, + "dilithium2_bp256", KEY_TYPE_CMP_SIG, NULL, 256, 38); } static void *dilithium2_bp256_gen_init(void *provctx, int selection) { - return oqsx_gen_init - (provctx, selection, OQS_SIG_alg_dilithium_2, - "dilithium2_bp256", KEY_TYPE_CMP_SIG, 256, 38); + return oqsx_gen_init(provctx, selection, OQS_SIG_alg_dilithium_2, + "dilithium2_bp256", KEY_TYPE_CMP_SIG, 256, 38); } ///// OQS_TEMPLATE_FRAGMENT_KEYMGMT_CONSTRUCTORS_END diff --git a/oqsprov/oqs_prov.h b/oqsprov/oqs_prov.h index fb7a7ee6..79d5edaa 100644 --- a/oqsprov/oqs_prov.h +++ b/oqsprov/oqs_prov.h @@ -158,7 +158,7 @@ struct oqsx_key_st { #ifdef USE_ENCODING_LIB OQSX_ENCODING_CTX oqsx_encoding_ctx; #endif - EVP_PKEY** cmp_classical_pkey; + EVP_PKEY **cmp_classical_pkey; EVP_PKEY *classical_pkey; // for hybrid sigs const OQSX_EVP_INFO *evp_info; size_t numkeys; @@ -177,8 +177,8 @@ struct oqsx_key_st { #endif int references; - /* point to actual priv key material -- classic key, if present, first, unless is composite - * i.e., OQS key always at comp_*key[numkeys-1] + /* point to actual priv key material -- classic key, if present, first, + * unless is composite i.e., OQS key always at comp_*key[numkeys-1] */ void **comp_privkey; void **comp_pubkey; @@ -192,17 +192,17 @@ struct oqsx_key_st { typedef struct oqsx_key_st OQSX_KEY; -//composite signature -struct SignatureModel{ - ASN1_BIT_STRING *sig1; - ASN1_BIT_STRING *sig2; +// composite signature +struct SignatureModel { + ASN1_BIT_STRING *sig1; + ASN1_BIT_STRING *sig2; }; typedef struct SignatureModel CompositeSignature; char *get_oqsname_fromtls(char *tlsname); char *get_oqsname(int nid); -char* get_cmpname(int nid, int index); +char *get_cmpname(int nid, int index); int get_oqsalg_idx(int nid); /* Register given NID with tlsname in OSSL3 registry */ @@ -1405,150 +1405,278 @@ extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_rsa3072_sphincsshake128fsimple_decoder_functions []; -extern const OSSL_DISPATCH oqs_dilithium3_rsa3072_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_rsa3072_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_rsa3072_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_rsa3072_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_rsa3072_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_rsa3072_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_rsa3072_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_rsa3072_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_rsa3072_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_rsa3072_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_rsa3072_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_rsa3072_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium3_rsa3072_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium3_rsa3072_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium3_rsa3072_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_p256_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_p256_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_p256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_p256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_p256_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_p256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium3_rsa3072_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium3_rsa3072_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_p256_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_p256_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_p256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_p256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_p256_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_p256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium3_p256_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium3_p256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium3_p256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_p256_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_p256_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_p256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_p256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_p256_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_p256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium3_p256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium3_p256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_p256_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_p256_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_p256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_p256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_p256_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_p256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_falcon512_p256_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_falcon512_p256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_falcon512_p256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_p384_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_p384_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_p384_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_p384_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_p384_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_p384_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_falcon512_p256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_falcon512_p256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_p384_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_p384_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_p384_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_p384_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_p384_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_p384_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium5_p384_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium5_p384_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium5_p384_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_bp256_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_bp256_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_bp256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_bp256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_bp256_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_bp256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium5_p384_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium5_p384_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_bp256_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_bp256_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_bp256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_bp256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_bp256_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_bp256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium3_bp256_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium3_bp256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium3_bp256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_ed25519_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_ed25519_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_ed25519_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_ed25519_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_ed25519_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_ed25519_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium3_bp256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium3_bp256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_ed25519_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_ed25519_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_ed25519_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_ed25519_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_ed25519_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_ed25519_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium3_ed25519_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium3_ed25519_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium3_ed25519_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_bp384_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_bp384_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_bp384_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_bp384_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_bp384_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_bp384_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium3_ed25519_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium3_ed25519_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_bp384_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_bp384_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_bp384_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_bp384_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_bp384_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_bp384_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium5_bp384_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium5_bp384_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium5_bp384_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_ed448_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_ed448_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_ed448_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_ed448_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_ed448_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium5_ed448_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium5_bp384_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium5_bp384_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_ed448_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_ed448_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_ed448_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_ed448_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_ed448_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium5_ed448_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium5_ed448_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium5_ed448_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium5_ed448_decoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_bp256_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_bp256_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_bp256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_bp256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_bp256_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_bp256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium5_ed448_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium5_ed448_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_bp256_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_bp256_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_bp256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_bp256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_bp256_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_bp256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_falcon512_bp256_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_falcon512_bp256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_falcon512_bp256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_ed25519_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_ed25519_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_ed25519_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_ed25519_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_ed25519_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_falcon512_ed25519_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_falcon512_bp256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_falcon512_bp256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_ed25519_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_ed25519_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_ed25519_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_ed25519_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_ed25519_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_falcon512_ed25519_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_falcon512_ed25519_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_falcon512_ed25519_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_falcon512_ed25519_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_pss3072_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_pss3072_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_pss3072_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_pss3072_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_pss3072_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium3_pss3072_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_falcon512_ed25519_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_falcon512_ed25519_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_pss3072_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_pss3072_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_pss3072_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_pss3072_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_pss3072_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium3_pss3072_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium3_pss3072_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium3_pss3072_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium3_pss3072_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_pss2048_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_pss2048_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_pss2048_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_pss2048_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_pss2048_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_pss2048_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium3_pss3072_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium3_pss3072_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_pss2048_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_pss2048_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_pss2048_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_pss2048_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_pss2048_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_pss2048_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium2_pss2048_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium2_pss2048_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium2_pss2048_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_rsa2048_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_rsa2048_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_rsa2048_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_rsa2048_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_rsa2048_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_rsa2048_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium2_pss2048_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium2_pss2048_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_rsa2048_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_rsa2048_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_rsa2048_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_rsa2048_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_rsa2048_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_rsa2048_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium2_rsa2048_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium2_rsa2048_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium2_rsa2048_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_ed25519_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_ed25519_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_ed25519_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_ed25519_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_ed25519_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_ed25519_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium2_rsa2048_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium2_rsa2048_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_ed25519_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_ed25519_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_ed25519_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_ed25519_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_ed25519_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_ed25519_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium2_ed25519_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium2_ed25519_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium2_ed25519_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_p256_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_p256_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_p256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_p256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_p256_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_p256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium2_ed25519_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium2_ed25519_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_p256_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_p256_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_p256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_p256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_p256_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_p256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium2_p256_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium2_p256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium2_p256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_bp256_to_PrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_bp256_to_PrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_bp256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_bp256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_bp256_to_SubjectPublicKeyInfo_der_encoder_functions[]; -extern const OSSL_DISPATCH oqs_dilithium2_bp256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium2_p256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium2_p256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_bp256_to_PrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_bp256_to_PrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_bp256_to_EncryptedPrivateKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_bp256_to_EncryptedPrivateKeyInfo_pem_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_bp256_to_SubjectPublicKeyInfo_der_encoder_functions[]; +extern const OSSL_DISPATCH + oqs_dilithium2_bp256_to_SubjectPublicKeyInfo_pem_encoder_functions[]; extern const OSSL_DISPATCH oqs_dilithium2_bp256_to_text_encoder_functions[]; -extern const OSSL_DISPATCH oqs_PrivateKeyInfo_der_to_dilithium2_bp256_decoder_functions[]; -extern const OSSL_DISPATCH oqs_SubjectPublicKeyInfo_der_to_dilithium2_bp256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_PrivateKeyInfo_der_to_dilithium2_bp256_decoder_functions[]; +extern const OSSL_DISPATCH + oqs_SubjectPublicKeyInfo_der_to_dilithium2_bp256_decoder_functions[]; ///// OQS_TEMPLATE_FRAGMENT_ENDECODER_FUNCTIONS_END ///// OQS_TEMPLATE_FRAGMENT_ALG_FUNCTIONS_START diff --git a/oqsprov/oqs_sig.c b/oqsprov/oqs_sig.c index 8a9bec70..1dddeb4e 100644 --- a/oqsprov/oqs_sig.c +++ b/oqsprov/oqs_sig.c @@ -27,13 +27,13 @@ // TBD: Review what we really need/want: For now go with OSSL settings: #define OSSL_MAX_NAME_SIZE 50 #define OSSL_MAX_PROPQUERY_SIZE 256 /* Property query strings */ -#ifdef OQS_KEM_ENCODERS /*idx to the first composite in the composite idx block*/ -# define COMPOSITE_IDX_ADJUST 65 +#ifdef OQS_KEM_ENCODERS /*idx to the first composite in the composite idx \ + block*/ +# define COMPOSITE_IDX_ADJUST 65 #else -# define COMPOSITE_IDX_ADJUST 23 +# define COMPOSITE_IDX_ADJUST 23 #endif - #ifdef NDEBUG # define OQS_SIG_PRINTF(a) # define OQS_SIG_PRINTF2(a, b) @@ -104,19 +104,19 @@ IMPLEMENT_ASN1_FUNCTIONS(CompositeSignature) typedef struct { - OSSL_LIB_CTX *libctx; - char *propq; - OQSX_KEY *sig; + OSSL_LIB_CTX *libctx; + char *propq; + OQSX_KEY *sig; - /* - * Flag to determine if the hash function can be changed (1) or not (0) - * Because it's dangerous to change during a DigestSign or DigestVerify - * operation, this flag is cleared by their Init function, and set again - * by their Final function. - */ - unsigned int flag_allow_md : 1; + /* + * Flag to determine if the hash function can be changed (1) or not (0) + * Because it's dangerous to change during a DigestSign or DigestVerify + * operation, this flag is cleared by their Init function, and set again + * by their Final function. + */ + unsigned int flag_allow_md : 1; - char mdname[OSSL_MAX_NAME_SIZE]; + char mdname[OSSL_MAX_NAME_SIZE]; /* The Algorithm Identifier of the combined signature algorithm */ unsigned char *aid; @@ -131,17 +131,15 @@ typedef struct int operation; } PROV_OQSSIG_CTX; - - static void *oqs_sig_newctx(void *provctx, const char *propq) { - PROV_OQSSIG_CTX *poqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx; OQS_SIG_PRINTF2("OQS SIG provider: newctx called with propq %s\n", propq); - poqs_sigctx = OPENSSL_zalloc(sizeof(PROV_OQSSIG_CTX)); - if (poqs_sigctx == NULL) - return NULL; + poqs_sigctx = OPENSSL_zalloc(sizeof(PROV_OQSSIG_CTX)); + if (poqs_sigctx == NULL) + return NULL; poqs_sigctx->libctx = ((PROV_OQS_CTX *)provctx)->libctx; if (propq != NULL && (poqs_sigctx->propq = OPENSSL_strdup(propq)) == NULL) { @@ -160,9 +158,8 @@ static int oqs_sig_setup_md(PROV_OQSSIG_CTX *ctx, const char *mdname, if (mdprops == NULL) mdprops = ctx->propq; - if (mdname != NULL) - { - EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); + if (mdname != NULL) { + EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); if ((md == NULL) || (EVP_MD_nid(md) == NID_undef)) { if (md == NULL) @@ -177,10 +174,10 @@ static int oqs_sig_setup_md(PROV_OQSSIG_CTX *ctx, const char *mdname, EVP_MD_free(ctx->md); ctx->md = NULL; - if (ctx->aid) - OPENSSL_free(ctx->aid); - ctx->aid = NULL; // ensure next function allocates memory - ctx->aid_len = get_aid(&(ctx->aid), ctx->sig->tls_name); + if (ctx->aid) + OPENSSL_free(ctx->aid); + ctx->aid = NULL; // ensure next function allocates memory + ctx->aid_len = get_aid(&(ctx->aid), ctx->sig->tls_name); ctx->md = md; OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); @@ -191,7 +188,7 @@ static int oqs_sig_setup_md(PROV_OQSSIG_CTX *ctx, const char *mdname, static int oqs_sig_signverify_init(void *vpoqs_sigctx, void *voqssig, int operation) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; OQS_SIG_PRINTF("OQS SIG provider: signverify_init called\n"); if (poqs_sigctx == NULL || voqssig == NULL || !oqsx_key_up_ref(voqssig)) @@ -211,53 +208,53 @@ static int oqs_sig_signverify_init(void *vpoqs_sigctx, void *voqssig, static int oqs_sig_sign_init(void *vpoqs_sigctx, void *voqssig, const OSSL_PARAM params[]) { - OQS_SIG_PRINTF("OQS SIG provider: sign_init called\n"); - return oqs_sig_signverify_init(vpoqs_sigctx, voqssig, EVP_PKEY_OP_SIGN); + OQS_SIG_PRINTF("OQS SIG provider: sign_init called\n"); + return oqs_sig_signverify_init(vpoqs_sigctx, voqssig, EVP_PKEY_OP_SIGN); } static int oqs_sig_verify_init(void *vpoqs_sigctx, void *voqssig, const OSSL_PARAM params[]) { - OQS_SIG_PRINTF("OQS SIG provider: verify_init called\n"); - return oqs_sig_signverify_init(vpoqs_sigctx, voqssig, EVP_PKEY_OP_VERIFY); + OQS_SIG_PRINTF("OQS SIG provider: verify_init called\n"); + return oqs_sig_signverify_init(vpoqs_sigctx, voqssig, EVP_PKEY_OP_VERIFY); } static const char *composite_OID_prefix[] = { - "69642D4D4C44534136352D525341333037322D504B435331352D534841323536", //dilithium3_rsa3072 - "69642D4D4C44534136352D45434453412D503235362D534841323536", //dilithium3_p256 - "69642D46616C6F6E3531322D45434453412D503235362D534841323536", //falcon512_p256 - "69642D4D4C44534138372D45434453412D503338342D534841333834", //dilithium5_p384 - "69642D4D4C44534136352D45434453412D627261696E706F6F6C5032353672312D534841323536", //dilithium3_bp256 - "69642D4D4C44534136352D456432353531392D534841353132", //dilithium3_ed25519 - "69642D4D4C44534138372D45434453412D627261696E706F6F6C5033383472312D534841333834", //dilithium5_bp384 - "69642D4D4C44534138372D45643434382D5348414B45323536", //dilithium5_ed448 - "69642D46616C636F6E3531322D45434453412D627261696E706F6F6C5032353672312D534841323536", //falcon512_bp256 - "69642D46616C636F6E3531322D456432353531392D534841353132", //falcon512_ed25519 - "69642D4D4C44534136352D525341333037322D5053532D534841323536", //dilithium3_pss3072 - "69642D4D4C44534134342D525341323034382D5053532D534841323536", //dilithium2_pss2048 - "69642D4D4C44534134342D525341323034382D504B435331352D534841323536", //dilithium2_rsa2048 - "69642D4D4C44534134342D456432353531392D534841353132", //dilithium2_ed25519 - "69642D4D4C44534134342D45434453412D503235362D534841323536", //dilithium2_p256 - "69642D4D4C44534134342D45434453412D627261696E706F6F6C5032353672312D534841323536", //dilithium2_bp256 + "69642D4D4C44534136352D525341333037322D504B435331352D534841323536", // dilithium3_rsa3072 + "69642D4D4C44534136352D45434453412D503235362D534841323536", // dilithium3_p256 + "69642D46616C6F6E3531322D45434453412D503235362D534841323536", // falcon512_p256 + "69642D4D4C44534138372D45434453412D503338342D534841333834", // dilithium5_p384 + "69642D4D4C44534136352D45434453412D627261696E706F6F6C5032353672312D534841323536", // dilithium3_bp256 + "69642D4D4C44534136352D456432353531392D534841353132", // dilithium3_ed25519 + "69642D4D4C44534138372D45434453412D627261696E706F6F6C5033383472312D534841333834", // dilithium5_bp384 + "69642D4D4C44534138372D45643434382D5348414B45323536", // dilithium5_ed448 + "69642D46616C636F6E3531322D45434453412D627261696E706F6F6C5032353672312D534841323536", // falcon512_bp256 + "69642D46616C636F6E3531322D456432353531392D534841353132", // falcon512_ed25519 + "69642D4D4C44534136352D525341333037322D5053532D534841323536", // dilithium3_pss3072 + "69642D4D4C44534134342D525341323034382D5053532D534841323536", // dilithium2_pss2048 + "69642D4D4C44534134342D525341323034382D504B435331352D534841323536", // dilithium2_rsa2048 + "69642D4D4C44534134342D456432353531392D534841353132", // dilithium2_ed25519 + "69642D4D4C44534134342D45434453412D503235362D534841323536", // dilithium2_p256 + "69642D4D4C44534134342D45434453412D627261696E706F6F6C5032353672312D534841323536", // dilithium2_bp256 }; static const size_t composite_OID_prefix_len[] = { - 64, //dilithium3_rsa3072 - 56, //dilithium3_p256 - 58, //falcon512_p256 - 56, //dilithium5_p384 - 78, //dilithium3_bp256 - 50, //dilithium3_ed25519 - 78, //dilithium5_bp384 - 50, //dilithium5_ed448 - 82, //falcon512_bp256 - 54, //falcon512_ed25519 - 58, //dilithium3_pss3072 - 58, //dilithium2_pss2048 - 64, //dilithium2_rsa2048 - 50, //dilithium2_ed25519 - 56, //dilithium2_p256 - 78, //dilithium2_bp256 + 64, // dilithium3_rsa3072 + 56, // dilithium3_p256 + 58, // falcon512_p256 + 56, // dilithium5_p384 + 78, // dilithium3_bp256 + 50, // dilithium3_ed25519 + 78, // dilithium5_bp384 + 50, // dilithium5_ed448 + 82, // falcon512_bp256 + 54, // falcon512_ed25519 + 58, // dilithium3_pss3072 + 58, // dilithium2_pss2048 + 64, // dilithium2_rsa2048 + 50, // dilithium2_ed25519 + 56, // dilithium2_p256 + 78, // dilithium2_bp256 }; /* On entry to this function, data to be signed (tbs) might have been hashed @@ -267,45 +264,40 @@ static const size_t composite_OID_prefix_len[] = { static int oqs_sig_sign(void *vpoqs_sigctx, unsigned char *sig, size_t *siglen, size_t sigsize, const unsigned char *tbs, size_t tbslen) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - OQSX_KEY *oqsxkey = poqs_sigctx->sig; - OQS_SIG *oqs_key = poqs_sigctx->sig->oqsx_provider_ctx[0].oqsx_qs_ctx.sig; - EVP_PKEY *oqs_key_classic = NULL; - EVP_PKEY *cmp_key_classic = NULL; - EVP_PKEY *evpkey = oqsxkey->classical_pkey; // if this value is not NULL, we're running hybrid - EVP_PKEY_CTX *classical_ctx_sign = NULL; - - OQS_SIG_PRINTF2("OQS SIG provider: sign called for %ld bytes\n", tbslen); - - int is_hybrid = evpkey != NULL; - int is_composite = (oqsxkey->keytype == KEY_TYPE_CMP_SIG); - size_t max_sig_len = 0; - size_t classical_sig_len = 0, oqs_sig_len = 0; - size_t actual_classical_sig_len = 0; - size_t index = 0; - int rv = 0; - - if (!oqsxkey || !(oqs_key || oqs_key_classic) || !oqsxkey->privkey) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_NO_PRIVATE_KEY); - return rv; - } + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + OQSX_KEY *oqsxkey = poqs_sigctx->sig; + OQS_SIG *oqs_key = poqs_sigctx->sig->oqsx_provider_ctx[0].oqsx_qs_ctx.sig; + EVP_PKEY *oqs_key_classic = NULL; + EVP_PKEY *cmp_key_classic = NULL; + EVP_PKEY *evpkey = oqsxkey->classical_pkey; // if this value is not NULL, + // we're running hybrid + EVP_PKEY_CTX *classical_ctx_sign = NULL; + + OQS_SIG_PRINTF2("OQS SIG provider: sign called for %ld bytes\n", tbslen); - if (is_composite) - { - max_sig_len = oqsx_key_maxsize(oqsxkey); - }else - { - max_sig_len += oqs_key->length_signature; - } + int is_hybrid = evpkey != NULL; + int is_composite = (oqsxkey->keytype == KEY_TYPE_CMP_SIG); + size_t max_sig_len = 0; + size_t classical_sig_len = 0, oqs_sig_len = 0; + size_t actual_classical_sig_len = 0; + size_t index = 0; + int rv = 0; - if (is_hybrid) - { - actual_classical_sig_len = oqsxkey->evp_info->length_signature; - max_sig_len += (SIZE_OF_UINT32 + actual_classical_sig_len); - } + if (!oqsxkey || !(oqs_key || oqs_key_classic) || !oqsxkey->privkey) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_NO_PRIVATE_KEY); + return rv; + } - + if (is_composite) { + max_sig_len = oqsx_key_maxsize(oqsxkey); + } else { + max_sig_len += oqs_key->length_signature; + } + + if (is_hybrid) { + actual_classical_sig_len = oqsxkey->evp_info->length_signature; + max_sig_len += (SIZE_OF_UINT32 + actual_classical_sig_len); + } if (sig == NULL) { *siglen = max_sig_len; @@ -391,237 +383,265 @@ static int oqs_sig_sign(void *vpoqs_sigctx, unsigned char *sig, size_t *siglen, index += classical_sig_len; } - if (is_composite) - { - unsigned char *buf; - CompositeSignature *compsig = CompositeSignature_new(); - int i; - int nid = OBJ_sn2nid(oqsxkey->tls_name); - const char *oid_prefix = composite_OID_prefix[get_oqsalg_idx(nid) - COMPOSITE_IDX_ADJUST]; - const size_t oid_prefix_len = composite_OID_prefix_len[get_oqsalg_idx(nid) - COMPOSITE_IDX_ADJUST]; - char *final_tbs; - size_t final_tbslen = oid_prefix_len; - - //prepare the pre hash - for (i = 0; i < oqsxkey->numkeys; i++){ - char *name; - if ((name = get_cmpname(nid, i)) == NULL){ - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - goto endsign; - } - unsigned char *tbs_hash; - if (!get_oqsname_fromtls(name)){ - if (name[0] == 'e'){//ed25519 or ed448 - if(name[2] == '2'){//ed25519 - tbs_hash = OPENSSL_malloc(SHA512_DIGEST_LENGTH); - SHA512(tbs, tbslen, tbs_hash); - final_tbslen += SHA512_DIGEST_LENGTH; - }else{//ed4448 - EVP_MD_CTX *shake = EVP_MD_CTX_new(); - unsigned int tbs_hash_len = EVP_MAX_MD_SIZE; - tbs_hash = OPENSSL_malloc(tbs_hash_len); - - if ((EVP_DigestInit_ex(shake, EVP_shake256(), NULL) <= 0) - || (EVP_DigestUpdate(shake, tbs, tbslen) <= 0) - || (EVP_DigestFinalXOF(shake, tbs_hash, tbs_hash_len) <= 0 )){ - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - goto endsign; - } - final_tbslen += tbs_hash_len; - EVP_MD_CTX_free(shake); - } - }else if ((name[0] == 'p') - || (name[0] == 'b') - || (name[0] == 'r')){ //p256 or p384 or bp256 or bp384 or pss or rsa3072 - int aux; - if (name[0] == 'b') - aux = 2; - else - aux = 1; - switch(name[aux]){ - case 's'://pss or rsa - case '2'://p256 or bp256 - tbs_hash = OPENSSL_malloc(SHA256_DIGEST_LENGTH); - SHA256(tbs, tbslen, tbs_hash); - final_tbslen += SHA256_DIGEST_LENGTH; - break; - case '3'://p384 or bp384 - tbs_hash = OPENSSL_malloc(SHA384_DIGEST_LENGTH); - SHA384(tbs, tbslen, tbs_hash); - final_tbslen += SHA384_DIGEST_LENGTH; - break; - default: + if (is_composite) { + unsigned char *buf; + CompositeSignature *compsig = CompositeSignature_new(); + int i; + int nid = OBJ_sn2nid(oqsxkey->tls_name); + const char *oid_prefix + = composite_OID_prefix[get_oqsalg_idx(nid) - COMPOSITE_IDX_ADJUST]; + const size_t oid_prefix_len + = composite_OID_prefix_len[get_oqsalg_idx(nid) + - COMPOSITE_IDX_ADJUST]; + char *final_tbs; + size_t final_tbslen = oid_prefix_len; + + // prepare the pre hash + for (i = 0; i < oqsxkey->numkeys; i++) { + char *name; + if ((name = get_cmpname(nid, i)) == NULL) { ERR_raise(ERR_LIB_USER, ERR_R_FATAL); OPENSSL_free(name); goto endsign; } - }else{ - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - goto endsign; - } - final_tbs = OPENSSL_malloc(final_tbslen); - memcpy(final_tbs, oid_prefix, oid_prefix_len); - memcpy(final_tbs + oid_prefix_len, tbs_hash, final_tbslen - oid_prefix_len); - OPENSSL_free(tbs_hash); - } - OPENSSL_free(name); - } - - for (i = 0; i < oqsxkey->numkeys; i++){ - char *name; - if((name = get_cmpname(nid, i)) == NULL){ - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - goto endsign; - } - - if (get_oqsname_fromtls(name)){ //PQC signing - oqs_sig_len = oqsxkey->oqsx_provider_ctx[i].oqsx_qs_ctx.sig->length_signature; - buf = OPENSSL_malloc(oqs_sig_len); - if (OQS_SIG_sign(oqs_key, buf, &oqs_sig_len, final_tbs, final_tbslen, oqsxkey->comp_privkey[i]) != OQS_SUCCESS) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_SIGNING_FAILED); - OPENSSL_free(name); - OPENSSL_free(buf); - goto endsign; + unsigned char *tbs_hash; + if (!get_oqsname_fromtls(name)) { + if (name[0] == 'e') { // ed25519 or ed448 + if (name[2] == '2') { // ed25519 + tbs_hash = OPENSSL_malloc(SHA512_DIGEST_LENGTH); + SHA512(tbs, tbslen, tbs_hash); + final_tbslen += SHA512_DIGEST_LENGTH; + } else { // ed4448 + EVP_MD_CTX *shake = EVP_MD_CTX_new(); + unsigned int tbs_hash_len = EVP_MAX_MD_SIZE; + tbs_hash = OPENSSL_malloc(tbs_hash_len); + + if ((EVP_DigestInit_ex(shake, EVP_shake256(), NULL) + <= 0) + || (EVP_DigestUpdate(shake, tbs, tbslen) <= 0) + || (EVP_DigestFinalXOF(shake, tbs_hash, + tbs_hash_len) + <= 0)) { + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + goto endsign; + } + final_tbslen += tbs_hash_len; + EVP_MD_CTX_free(shake); + } + } else if ((name[0] == 'p') || (name[0] == 'b') + || (name[0] == 'r')) { // p256 or p384 or bp256 or + // bp384 or pss or rsa3072 + int aux; + if (name[0] == 'b') + aux = 2; + else + aux = 1; + switch (name[aux]) { + case 's': // pss or rsa + case '2': // p256 or bp256 + tbs_hash = OPENSSL_malloc(SHA256_DIGEST_LENGTH); + SHA256(tbs, tbslen, tbs_hash); + final_tbslen += SHA256_DIGEST_LENGTH; + break; + case '3': // p384 or bp384 + tbs_hash = OPENSSL_malloc(SHA384_DIGEST_LENGTH); + SHA384(tbs, tbslen, tbs_hash); + final_tbslen += SHA384_DIGEST_LENGTH; + break; + default: + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + goto endsign; + } + } else { + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + goto endsign; + } + final_tbs = OPENSSL_malloc(final_tbslen); + memcpy(final_tbs, oid_prefix, oid_prefix_len); + memcpy(final_tbs + oid_prefix_len, tbs_hash, + final_tbslen - oid_prefix_len); + OPENSSL_free(tbs_hash); + } + OPENSSL_free(name); } - }else - { // sign non PQC key on oqs_key - oqs_key_classic = oqsxkey->cmp_classical_pkey[i]; - oqs_sig_len = oqsxkey->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->length_signature; - buf = OPENSSL_malloc(oqs_sig_len); - const EVP_MD *classical_md; - EVP_MD_CTX* evp_ctx = EVP_MD_CTX_new(); - int digest_len; - unsigned char digest[SHA512_DIGEST_LENGTH]; /* init with max length */ - if (name[0] == 'e'){ //ed25519 or ed448 - if ((EVP_DigestSignInit(evp_ctx, NULL, NULL, NULL, oqs_key_classic) <= 0 ) - || (EVP_DigestSign(evp_ctx, buf, &oqs_sig_len, final_tbs, final_tbslen) <= 0)){ - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - OPENSSL_free(buf); - goto endsign; - } - }else { - if ((classical_ctx_sign = EVP_PKEY_CTX_new(oqs_key_classic, NULL)) == NULL - || (EVP_PKEY_sign_init(classical_ctx_sign) <= 0)) - { - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - OPENSSL_free(buf); - goto endsign; - } - - if (!strncmp(name, "pss", 3)) - { - if ((EVP_PKEY_CTX_set_rsa_padding(classical_ctx_sign, RSA_PKCS1_PSS_PADDING) <= 0) - || (EVP_PKEY_CTX_set_rsa_pss_saltlen(classical_ctx_sign, 64) <= 0) - || (EVP_PKEY_CTX_set_rsa_mgf1_md(classical_ctx_sign, EVP_sha256()) <= 0)) - { - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - OPENSSL_free(buf); - goto endsign; - } - } else if (oqsxkey->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype == EVP_PKEY_RSA) - { - if (EVP_PKEY_CTX_set_rsa_padding(classical_ctx_sign, RSA_PKCS1_PADDING) <= 0) - { - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - OPENSSL_free(buf); - goto endsign; - } - } - - if ((name[0] == 'p') - || (name[0] == 'b') - || (name[0] == 'r')) - { - int aux; - if(name[0] == 'b'){ - aux = 2; - }else { - aux = 1; - } - switch(name[aux]){ - case 's'://pss or rsa - case '2'://p256 or bp256 - classical_md = EVP_sha256(); - digest_len = SHA256_DIGEST_LENGTH; - SHA256(final_tbs, final_tbslen, (unsigned char *)&digest); - break; - case '3'://p384 or bp384 - classical_md = EVP_sha384(); - digest_len = SHA384_DIGEST_LENGTH; - SHA384(final_tbs, final_tbslen, (unsigned char *)&digest); - break; - case '5'://p512 - classical_md = EVP_sha512(); - digest_len = SHA512_DIGEST_LENGTH; - SHA512(final_tbs, final_tbslen, (unsigned char *)&digest); - break; - default: + for (i = 0; i < oqsxkey->numkeys; i++) { + char *name; + if ((name = get_cmpname(nid, i)) == NULL) { ERR_raise(ERR_LIB_USER, ERR_R_FATAL); OPENSSL_free(name); - OPENSSL_free(buf); goto endsign; } - } - if ((EVP_PKEY_CTX_set_signature_md(classical_ctx_sign, classical_md) <= 0) - || (EVP_PKEY_sign(classical_ctx_sign, buf, &oqs_sig_len, digest, digest_len) <= 0)) - { - ERR_raise(ERR_LIB_USER, ERR_R_FATAL); - OPENSSL_free(name); - OPENSSL_free(buf); - goto endsign; - } + if (get_oqsname_fromtls(name)) { // PQC signing + oqs_sig_len = oqsxkey->oqsx_provider_ctx[i] + .oqsx_qs_ctx.sig->length_signature; + buf = OPENSSL_malloc(oqs_sig_len); + if (OQS_SIG_sign(oqs_key, buf, &oqs_sig_len, final_tbs, + final_tbslen, oqsxkey->comp_privkey[i]) + != OQS_SUCCESS) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_SIGNING_FAILED); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + } else { // sign non PQC key on oqs_key + oqs_key_classic = oqsxkey->cmp_classical_pkey[i]; + oqs_sig_len = oqsxkey->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->length_signature; + buf = OPENSSL_malloc(oqs_sig_len); + const EVP_MD *classical_md; + EVP_MD_CTX *evp_ctx = EVP_MD_CTX_new(); + int digest_len; + unsigned char + digest[SHA512_DIGEST_LENGTH]; /* init with max length */ + + if (name[0] == 'e') { // ed25519 or ed448 + if ((EVP_DigestSignInit(evp_ctx, NULL, NULL, NULL, + oqs_key_classic) + <= 0) + || (EVP_DigestSign(evp_ctx, buf, &oqs_sig_len, + final_tbs, final_tbslen) + <= 0)) { + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + } else { + if ((classical_ctx_sign + = EVP_PKEY_CTX_new(oqs_key_classic, NULL)) + == NULL + || (EVP_PKEY_sign_init(classical_ctx_sign) <= 0)) { + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + + if (!strncmp(name, "pss", 3)) { + if ((EVP_PKEY_CTX_set_rsa_padding(classical_ctx_sign, + RSA_PKCS1_PSS_PADDING) + <= 0) + || (EVP_PKEY_CTX_set_rsa_pss_saltlen( + classical_ctx_sign, 64) + <= 0) + || (EVP_PKEY_CTX_set_rsa_mgf1_md(classical_ctx_sign, + EVP_sha256()) + <= 0)) { + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + } else if (oqsxkey->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype + == EVP_PKEY_RSA) { + if (EVP_PKEY_CTX_set_rsa_padding(classical_ctx_sign, + RSA_PKCS1_PADDING) + <= 0) { + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + } + + if ((name[0] == 'p') || (name[0] == 'b') + || (name[0] == 'r')) { + int aux; + if (name[0] == 'b') { + aux = 2; + } else { + aux = 1; + } + switch (name[aux]) { + case 's': // pss or rsa + case '2': // p256 or bp256 + classical_md = EVP_sha256(); + digest_len = SHA256_DIGEST_LENGTH; + SHA256(final_tbs, final_tbslen, + (unsigned char *)&digest); + break; + case '3': // p384 or bp384 + classical_md = EVP_sha384(); + digest_len = SHA384_DIGEST_LENGTH; + SHA384(final_tbs, final_tbslen, + (unsigned char *)&digest); + break; + case '5': // p512 + classical_md = EVP_sha512(); + digest_len = SHA512_DIGEST_LENGTH; + SHA512(final_tbs, final_tbslen, + (unsigned char *)&digest); + break; + default: + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + } + + if ((EVP_PKEY_CTX_set_signature_md(classical_ctx_sign, + classical_md) + <= 0) + || (EVP_PKEY_sign(classical_ctx_sign, buf, &oqs_sig_len, + digest, digest_len) + <= 0)) { + ERR_raise(ERR_LIB_USER, ERR_R_FATAL); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + + if (oqs_sig_len + > oqsxkey->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->length_signature) { + /* sig is bigger than expected */ + ERR_raise(ERR_LIB_USER, OQSPROV_R_BUFFER_LENGTH_WRONG); + OPENSSL_free(name); + OPENSSL_free(buf); + goto endsign; + } + } + } + + if (i == 0) { + compsig->sig1->data = OPENSSL_memdup(buf, oqs_sig_len); + compsig->sig1->length = oqs_sig_len; + compsig->sig1->flags + = 8; // set as 8 to not check for unused bits + } else { + compsig->sig2->data = OPENSSL_memdup(buf, oqs_sig_len); + compsig->sig2->length = oqs_sig_len; + compsig->sig2->flags + = 8; // set as 8 to not check for unused bits + } - if (oqs_sig_len > oqsxkey->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->length_signature) - { - /* sig is bigger than expected */ - ERR_raise(ERR_LIB_USER, OQSPROV_R_BUFFER_LENGTH_WRONG); - OPENSSL_free(name); OPENSSL_free(buf); - goto endsign; - } + OPENSSL_free(name); } - } - - if (i == 0){ - compsig->sig1->data = OPENSSL_memdup(buf, oqs_sig_len); - compsig->sig1->length = oqs_sig_len; - compsig->sig1->flags = 8; //set as 8 to not check for unused bits - }else{ - compsig->sig2->data = OPENSSL_memdup(buf, oqs_sig_len); - compsig->sig2->length = oqs_sig_len; - compsig->sig2->flags = 8; //set as 8 to not check for unused bits - } - - OPENSSL_free(buf); - OPENSSL_free(name); + oqs_sig_len = i2d_CompositeSignature(compsig, &sig); + + OPENSSL_free(compsig->sig1->data); + OPENSSL_free(compsig->sig2->data); + OPENSSL_free(compsig); + OPENSSL_free(final_tbs); + } else if (OQS_SIG_sign(oqs_key, sig + index, &oqs_sig_len, tbs, tbslen, + oqsxkey->comp_privkey[oqsxkey->numkeys - 1]) + != OQS_SUCCESS) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_SIGNING_FAILED); + goto endsign; } - oqs_sig_len = i2d_CompositeSignature(compsig, &sig); - - OPENSSL_free(compsig->sig1->data); - OPENSSL_free(compsig->sig2->data); - OPENSSL_free(compsig); - OPENSSL_free(final_tbs); - } - else if (OQS_SIG_sign(oqs_key, sig + index, &oqs_sig_len, tbs, tbslen, oqsxkey->comp_privkey[oqsxkey->numkeys - 1]) != OQS_SUCCESS) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_SIGNING_FAILED); - goto endsign; - } - - *siglen = classical_sig_len + oqs_sig_len; - OQS_SIG_PRINTF2("OQS SIG provider: signing completes with size %ld\n", *siglen); - rv = 1; /* success */ + + *siglen = classical_sig_len + oqs_sig_len; + OQS_SIG_PRINTF2("OQS SIG provider: signing completes with size %ld\n", + *siglen); + rv = 1; /* success */ endsign: if (classical_ctx_sign) { @@ -646,7 +666,7 @@ static int oqs_sig_verify(void *vpoqs_sigctx, const unsigned char *sig, size_t classical_sig_len = 0, oqs_sig_len = 0; size_t index = 0; int rv = 0; - ASN1_BIT_STRING *comp_sig; + ASN1_BIT_STRING *comp_sig; OQS_SIG_PRINTF3( "OQS SIG provider: verify called with siglen %ld bytes and tbslen %ld\n", @@ -724,216 +744,234 @@ static int oqs_sig_verify(void *vpoqs_sigctx, const unsigned char *sig, classical_sig_len = SIZE_OF_UINT32 + actual_classical_sig_len; index += classical_sig_len; } - if(is_composite){ - CompositeSignature* compsig; - int i; - int nid = OBJ_sn2nid(oqsxkey->tls_name); - unsigned char *buf; - size_t buf_len; - const char *oid_prefix = composite_OID_prefix[get_oqsalg_idx(nid) - COMPOSITE_IDX_ADJUST]; - const size_t oid_prefix_len = composite_OID_prefix_len[get_oqsalg_idx(nid) - COMPOSITE_IDX_ADJUST]; - char *final_tbs; - size_t final_tbslen = oid_prefix_len; - - if((compsig = d2i_CompositeSignature(NULL, &sig, siglen)) == NULL) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - goto endverify; - } - - //prepare the pre-hash - for (i = 0; i < oqsxkey->numkeys; i++){ - char *name; - if ((name = get_cmpname(nid, i)) == NULL){ - OPENSSL_free(name); - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - goto endverify; + if (is_composite) { + CompositeSignature *compsig; + int i; + int nid = OBJ_sn2nid(oqsxkey->tls_name); + unsigned char *buf; + size_t buf_len; + const char *oid_prefix + = composite_OID_prefix[get_oqsalg_idx(nid) - COMPOSITE_IDX_ADJUST]; + const size_t oid_prefix_len + = composite_OID_prefix_len[get_oqsalg_idx(nid) + - COMPOSITE_IDX_ADJUST]; + char *final_tbs; + size_t final_tbslen = oid_prefix_len; + + if ((compsig = d2i_CompositeSignature(NULL, &sig, siglen)) == NULL) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + goto endverify; } - unsigned char *tbs_hash; - if (!get_oqsname_fromtls(name)){ - if (name[0] == 'e'){//ed25519 or ed448 - if(name[2] == '2'){//ed25519 - tbs_hash = OPENSSL_malloc(SHA512_DIGEST_LENGTH); - SHA512(tbs, tbslen, tbs_hash); - final_tbslen += SHA512_DIGEST_LENGTH; - }else{//ed4448 - EVP_MD_CTX *shake = EVP_MD_CTX_new(); - unsigned int tbs_hash_len = EVP_MAX_MD_SIZE; - tbs_hash = OPENSSL_malloc(tbs_hash_len); - - if ((EVP_DigestInit_ex(shake, EVP_shake256(), NULL) <= 0) - || (EVP_DigestUpdate(shake, tbs, tbslen) <= 0) - || (EVP_DigestFinalXOF(shake, tbs_hash, tbs_hash_len) <= 0 )){ + + // prepare the pre-hash + for (i = 0; i < oqsxkey->numkeys; i++) { + char *name; + if ((name = get_cmpname(nid, i)) == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); goto endverify; } - final_tbslen += tbs_hash_len; - EVP_MD_CTX_free(shake); + unsigned char *tbs_hash; + if (!get_oqsname_fromtls(name)) { + if (name[0] == 'e') { // ed25519 or ed448 + if (name[2] == '2') { // ed25519 + tbs_hash = OPENSSL_malloc(SHA512_DIGEST_LENGTH); + SHA512(tbs, tbslen, tbs_hash); + final_tbslen += SHA512_DIGEST_LENGTH; + } else { // ed4448 + EVP_MD_CTX *shake = EVP_MD_CTX_new(); + unsigned int tbs_hash_len = EVP_MAX_MD_SIZE; + tbs_hash = OPENSSL_malloc(tbs_hash_len); + + if ((EVP_DigestInit_ex(shake, EVP_shake256(), NULL) + <= 0) + || (EVP_DigestUpdate(shake, tbs, tbslen) <= 0) + || (EVP_DigestFinalXOF(shake, tbs_hash, + tbs_hash_len) + <= 0)) { + OPENSSL_free(name); + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + goto endverify; + } + final_tbslen += tbs_hash_len; + EVP_MD_CTX_free(shake); + } + } else if ((name[0] == 'p') || (name[0] == 'b') + || (name[0] == 'r')) { // p256 or p384 or bp256 or + // bp384 or pss or rsa3072 + int aux; + if (name[0] == 'b') + aux = 2; + else + aux = 1; + switch (name[aux]) { + case 's': // pss or rsa + case '2': // p256 or bp256 + tbs_hash = OPENSSL_malloc(SHA256_DIGEST_LENGTH); + SHA256(tbs, tbslen, tbs_hash); + final_tbslen += SHA256_DIGEST_LENGTH; + break; + case '3': // p384 or bp384 + tbs_hash = OPENSSL_malloc(SHA384_DIGEST_LENGTH); + SHA384(tbs, tbslen, tbs_hash); + final_tbslen += SHA384_DIGEST_LENGTH; + break; + default: + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + OPENSSL_free(name); + goto endverify; + } + } else { + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + OPENSSL_free(name); + goto endverify; + } + final_tbs = OPENSSL_malloc(final_tbslen); + memcpy(final_tbs, oid_prefix, oid_prefix_len); + memcpy(final_tbs + oid_prefix_len, tbs_hash, + final_tbslen - oid_prefix_len); + OPENSSL_free(tbs_hash); } - }else if ((name[0] == 'p') - || (name[0] == 'b') - || (name[0] == 'r')){ //p256 or p384 or bp256 or bp384 or pss or rsa3072 - int aux; - if (name[0] == 'b') - aux = 2; - else - aux = 1; - switch(name[aux]){ - case 's'://pss or rsa - case '2'://p256 or bp256 - tbs_hash = OPENSSL_malloc(SHA256_DIGEST_LENGTH); - SHA256(tbs, tbslen, tbs_hash); - final_tbslen += SHA256_DIGEST_LENGTH; - break; - case '3'://p384 or bp384 - tbs_hash = OPENSSL_malloc(SHA384_DIGEST_LENGTH); - SHA384(tbs, tbslen, tbs_hash); - final_tbslen += SHA384_DIGEST_LENGTH; - break; - default: - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - OPENSSL_free(name); - goto endverify; - } - }else{ - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); OPENSSL_free(name); - goto endverify; - } - final_tbs = OPENSSL_malloc(final_tbslen); - memcpy(final_tbs, oid_prefix, oid_prefix_len); - memcpy(final_tbs + oid_prefix_len, tbs_hash, final_tbslen - oid_prefix_len); - OPENSSL_free(tbs_hash); - } - OPENSSL_free(name); - } - - for(i = 0; i < oqsxkey->numkeys; i++){ - if (i == 0){ - buf = compsig->sig1->data; - buf_len = compsig->sig1->length; - }else{ - buf = compsig->sig2->data; - buf_len = compsig->sig2->length; - } - - char *name; - if((name = get_cmpname(nid, i)) == NULL){ - OPENSSL_free(name); - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - goto endverify; } - if (get_oqsname_fromtls(name)){ - if (OQS_SIG_verify(oqs_key, final_tbs, final_tbslen, buf, buf_len, oqsxkey->comp_pubkey[i]) != OQS_SUCCESS) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - OPENSSL_free(name); - goto endverify; - } - }else{ - const EVP_MD *classical_md; - int digest_len; - int aux; - EVP_MD_CTX* evp_ctx = EVP_MD_CTX_new(); - unsigned char digest[SHA512_DIGEST_LENGTH]; /* init with max length */ - - if(name[0] == 'e'){ //ed25519 or ed448 - if((EVP_DigestVerifyInit(evp_ctx, NULL, NULL, NULL, oqsxkey->cmp_classical_pkey[i]) <= 0) - || (EVP_DigestVerify(evp_ctx, buf, buf_len, final_tbs, final_tbslen) <= 0)){ - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - OPENSSL_free(name); - goto endverify; - } - } else { - if (((ctx_verify = EVP_PKEY_CTX_new(oqsxkey->cmp_classical_pkey[i], NULL)) == NULL) - || (EVP_PKEY_verify_init(ctx_verify) <= 0)) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - OPENSSL_free(name); - goto endverify; + for (i = 0; i < oqsxkey->numkeys; i++) { + if (i == 0) { + buf = compsig->sig1->data; + buf_len = compsig->sig1->length; + } else { + buf = compsig->sig2->data; + buf_len = compsig->sig2->length; } - if (!strncmp(name, "pss", 3)) - { - if ((EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PSS_PADDING) <= 0) - || (EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx_verify, 64) <= 0) - || (EVP_PKEY_CTX_set_rsa_mgf1_md(ctx_verify, EVP_sha256()) <= 0)) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_WRONG_PARAMETERS); - OPENSSL_free(name); - goto endverify; - } - } else if (oqsxkey->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype == EVP_PKEY_RSA) - { - if (EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING) <= 0) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_WRONG_PARAMETERS); - OPENSSL_free(name); - goto endverify; - } - } - if ((name[0] == 'p') - || (name[0] == 'b') - || (name[0] == 'r')) - { - int aux; - if(name[0] == 'b') - aux = 2; - else - aux = 1; - switch(name[aux]){ - case 's'://pss or rsa - case '2'://p256 or bp256 - classical_md = EVP_sha256(); - digest_len = SHA256_DIGEST_LENGTH; - SHA256(final_tbs, final_tbslen, (unsigned char *)&digest); - break; - case '3'://p384 or bp384 - classical_md = EVP_sha384(); - digest_len = SHA384_DIGEST_LENGTH; - SHA384(final_tbs, final_tbslen, (unsigned char *)&digest); - break; - case '5'://p512 - classical_md = EVP_sha512(); - digest_len = SHA512_DIGEST_LENGTH; - SHA512(final_tbs, final_tbslen, (unsigned char *)&digest); - break; - default: - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + + char *name; + if ((name = get_cmpname(nid, i)) == NULL) { OPENSSL_free(name); + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); goto endverify; } - } - if ((EVP_PKEY_CTX_set_signature_md(ctx_verify, classical_md) <= 0) - || (EVP_PKEY_verify(ctx_verify, buf, buf_len, digest, digest_len) <= 0)) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + + if (get_oqsname_fromtls(name)) { + if (OQS_SIG_verify(oqs_key, final_tbs, final_tbslen, buf, + buf_len, oqsxkey->comp_pubkey[i]) + != OQS_SUCCESS) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + OPENSSL_free(name); + goto endverify; + } + } else { + const EVP_MD *classical_md; + int digest_len; + int aux; + EVP_MD_CTX *evp_ctx = EVP_MD_CTX_new(); + unsigned char + digest[SHA512_DIGEST_LENGTH]; /* init with max length */ + + if (name[0] == 'e') { // ed25519 or ed448 + if ((EVP_DigestVerifyInit(evp_ctx, NULL, NULL, NULL, + oqsxkey->cmp_classical_pkey[i]) + <= 0) + || (EVP_DigestVerify(evp_ctx, buf, buf_len, final_tbs, + final_tbslen) + <= 0)) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + OPENSSL_free(name); + goto endverify; + } + } else { + if (((ctx_verify = EVP_PKEY_CTX_new( + oqsxkey->cmp_classical_pkey[i], NULL)) + == NULL) + || (EVP_PKEY_verify_init(ctx_verify) <= 0)) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + OPENSSL_free(name); + goto endverify; + } + if (!strncmp(name, "pss", 3)) { + if ((EVP_PKEY_CTX_set_rsa_padding(ctx_verify, + RSA_PKCS1_PSS_PADDING) + <= 0) + || (EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx_verify, 64) + <= 0) + || (EVP_PKEY_CTX_set_rsa_mgf1_md(ctx_verify, + EVP_sha256()) + <= 0)) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_WRONG_PARAMETERS); + OPENSSL_free(name); + goto endverify; + } + } else if (oqsxkey->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype + == EVP_PKEY_RSA) { + if (EVP_PKEY_CTX_set_rsa_padding(ctx_verify, + RSA_PKCS1_PADDING) + <= 0) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_WRONG_PARAMETERS); + OPENSSL_free(name); + goto endverify; + } + } + if ((name[0] == 'p') || (name[0] == 'b') + || (name[0] == 'r')) { + int aux; + if (name[0] == 'b') + aux = 2; + else + aux = 1; + switch (name[aux]) { + case 's': // pss or rsa + case '2': // p256 or bp256 + classical_md = EVP_sha256(); + digest_len = SHA256_DIGEST_LENGTH; + SHA256(final_tbs, final_tbslen, + (unsigned char *)&digest); + break; + case '3': // p384 or bp384 + classical_md = EVP_sha384(); + digest_len = SHA384_DIGEST_LENGTH; + SHA384(final_tbs, final_tbslen, + (unsigned char *)&digest); + break; + case '5': // p512 + classical_md = EVP_sha512(); + digest_len = SHA512_DIGEST_LENGTH; + SHA512(final_tbs, final_tbslen, + (unsigned char *)&digest); + break; + default: + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + OPENSSL_free(name); + goto endverify; + } + } + if ((EVP_PKEY_CTX_set_signature_md(ctx_verify, classical_md) + <= 0) + || (EVP_PKEY_verify(ctx_verify, buf, buf_len, digest, + digest_len) + <= 0)) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + OPENSSL_free(name); + goto endverify; + } + } + } + OPENSSL_free(name); + } + OPENSSL_free(compsig); + OPENSSL_free(final_tbs); + } else { + if (!oqsxkey->comp_pubkey[oqsxkey->numkeys - 1]) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_WRONG_PARAMETERS); goto endverify; - } - } - } - - OPENSSL_free(name); - } - OPENSSL_free(compsig); - OPENSSL_free(final_tbs); - }else - { - if (!oqsxkey->comp_pubkey[oqsxkey->numkeys - 1]) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_WRONG_PARAMETERS); - goto endverify; - } - if (OQS_SIG_verify(oqs_key, tbs, tbslen, sig + index, - siglen - classical_sig_len, - oqsxkey->comp_pubkey[oqsxkey->numkeys-1]) - != OQS_SUCCESS) - { - ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); - goto endverify; - } + } + if (OQS_SIG_verify(oqs_key, tbs, tbslen, sig + index, + siglen - classical_sig_len, + oqsxkey->comp_pubkey[oqsxkey->numkeys - 1]) + != OQS_SUCCESS) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_VERIFY_ERROR); + goto endverify; + } } rv = 1; @@ -949,7 +987,7 @@ static int oqs_sig_digest_signverify_init(void *vpoqs_sigctx, const char *mdname, void *voqssig, int operation) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; OQS_SIG_PRINTF2( "OQS SIG provider: digest_signverify_init called for mdname %s\n", @@ -959,8 +997,8 @@ static int oqs_sig_digest_signverify_init(void *vpoqs_sigctx, if (!oqs_sig_signverify_init(vpoqs_sigctx, voqssig, operation)) return 0; - if (!oqs_sig_setup_md(poqs_sigctx, mdname, NULL)) - return 0; + if (!oqs_sig_setup_md(poqs_sigctx, mdname, NULL)) + return 0; if (mdname != NULL) { poqs_sigctx->mdctx = EVP_MD_CTX_new(); @@ -971,7 +1009,7 @@ static int oqs_sig_digest_signverify_init(void *vpoqs_sigctx, goto error; } - return 1; + return 1; error: EVP_MD_CTX_free(poqs_sigctx->mdctx); @@ -1001,9 +1039,9 @@ static int oqs_sig_digest_verify_init(void *vpoqs_sigctx, const char *mdname, int oqs_sig_digest_signverify_update(void *vpoqs_sigctx, const unsigned char *data, size_t datalen) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - OQS_SIG_PRINTF("OQS SIG provider: digest_signverify_update called\n"); + OQS_SIG_PRINTF("OQS SIG provider: digest_signverify_update called\n"); if (poqs_sigctx == NULL) return 0; @@ -1039,13 +1077,13 @@ int oqs_sig_digest_signverify_update(void *vpoqs_sigctx, int oqs_sig_digest_sign_final(void *vpoqs_sigctx, unsigned char *sig, size_t *siglen, size_t sigsize) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - unsigned char digest[EVP_MAX_MD_SIZE]; - unsigned int dlen = 0; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + unsigned char digest[EVP_MAX_MD_SIZE]; + unsigned int dlen = 0; - OQS_SIG_PRINTF("OQS SIG provider: digest_sign_final called\n"); - if (poqs_sigctx == NULL) - return 0; + OQS_SIG_PRINTF("OQS SIG provider: digest_sign_final called\n"); + if (poqs_sigctx == NULL) + return 0; /* * If sig is NULL then we're just finding out the sig size. Other fields @@ -1062,7 +1100,7 @@ int oqs_sig_digest_sign_final(void *vpoqs_sigctx, unsigned char *sig, return 0; } - poqs_sigctx->flag_allow_md = 1; + poqs_sigctx->flag_allow_md = 1; if (poqs_sigctx->mdctx != NULL) return oqs_sig_sign(vpoqs_sigctx, sig, siglen, sigsize, digest, @@ -1075,13 +1113,13 @@ int oqs_sig_digest_sign_final(void *vpoqs_sigctx, unsigned char *sig, int oqs_sig_digest_verify_final(void *vpoqs_sigctx, const unsigned char *sig, size_t siglen) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - unsigned char digest[EVP_MAX_MD_SIZE]; - unsigned int dlen = 0; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + unsigned char digest[EVP_MAX_MD_SIZE]; + unsigned int dlen = 0; - OQS_SIG_PRINTF("OQS SIG provider: digest_verify_final called\n"); - if (poqs_sigctx == NULL) - return 0; + OQS_SIG_PRINTF("OQS SIG provider: digest_verify_final called\n"); + if (poqs_sigctx == NULL) + return 0; // TBC for hybrids: if (poqs_sigctx->mdctx) { @@ -1098,7 +1136,7 @@ int oqs_sig_digest_verify_final(void *vpoqs_sigctx, const unsigned char *sig, static void oqs_sig_freectx(void *vpoqs_sigctx) { - PROV_OQSSIG_CTX *ctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *ctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; OQS_SIG_PRINTF("OQS SIG provider: freectx called\n"); OPENSSL_free(ctx->propq); @@ -1119,27 +1157,27 @@ static void oqs_sig_freectx(void *vpoqs_sigctx) static void *oqs_sig_dupctx(void *vpoqs_sigctx) { - PROV_OQSSIG_CTX *srcctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - PROV_OQSSIG_CTX *dstctx; + PROV_OQSSIG_CTX *srcctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *dstctx; - OQS_SIG_PRINTF("OQS SIG provider: dupctx called\n"); + OQS_SIG_PRINTF("OQS SIG provider: dupctx called\n"); - dstctx = OPENSSL_zalloc(sizeof(*srcctx)); - if (dstctx == NULL) - return NULL; + dstctx = OPENSSL_zalloc(sizeof(*srcctx)); + if (dstctx == NULL) + return NULL; - *dstctx = *srcctx; - dstctx->sig = NULL; - dstctx->md = NULL; - dstctx->mdctx = NULL; + *dstctx = *srcctx; + dstctx->sig = NULL; + dstctx->md = NULL; + dstctx->mdctx = NULL; - if (srcctx->sig != NULL && !oqsx_key_up_ref(srcctx->sig)) - goto err; - dstctx->sig = srcctx->sig; + if (srcctx->sig != NULL && !oqsx_key_up_ref(srcctx->sig)) + goto err; + dstctx->sig = srcctx->sig; - if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) - goto err; - dstctx->md = srcctx->md; + if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) + goto err; + dstctx->md = srcctx->md; if (srcctx->mdctx != NULL) { dstctx->mdctx = EVP_MD_CTX_new(); @@ -1176,14 +1214,14 @@ static void *oqs_sig_dupctx(void *vpoqs_sigctx) static int oqs_sig_get_ctx_params(void *vpoqs_sigctx, OSSL_PARAM *params) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - OSSL_PARAM *p; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + OSSL_PARAM *p; - OQS_SIG_PRINTF("OQS SIG provider: get_ctx_params called\n"); - if (poqs_sigctx == NULL || params == NULL) - return 0; + OQS_SIG_PRINTF("OQS SIG provider: get_ctx_params called\n"); + if (poqs_sigctx == NULL || params == NULL) + return 0; - p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); + p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); if (poqs_sigctx->aid == NULL) { poqs_sigctx->aid_len @@ -1195,11 +1233,11 @@ static int oqs_sig_get_ctx_params(void *vpoqs_sigctx, OSSL_PARAM *params) poqs_sigctx->aid_len)) return 0; - p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); - if (p != NULL && !OSSL_PARAM_set_utf8_string(p, poqs_sigctx->mdname)) - return 0; + p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); + if (p != NULL && !OSSL_PARAM_set_utf8_string(p, poqs_sigctx->mdname)) + return 0; - return 1; + return 1; } static const OSSL_PARAM known_gettable_ctx_params[] @@ -1211,17 +1249,17 @@ static const OSSL_PARAM * oqs_sig_gettable_ctx_params(ossl_unused void *vpoqs_sigctx, ossl_unused void *vctx) { - OQS_SIG_PRINTF("OQS SIG provider: gettable_ctx_params called\n"); - return known_gettable_ctx_params; + OQS_SIG_PRINTF("OQS SIG provider: gettable_ctx_params called\n"); + return known_gettable_ctx_params; } static int oqs_sig_set_ctx_params(void *vpoqs_sigctx, const OSSL_PARAM params[]) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - const OSSL_PARAM *p; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + const OSSL_PARAM *p; - OQS_SIG_PRINTF("OQS SIG provider: set_ctx_params called\n"); - if (poqs_sigctx == NULL || params == NULL) - return 0; + OQS_SIG_PRINTF("OQS SIG provider: set_ctx_params called\n"); + if (poqs_sigctx == NULL || params == NULL) + return 0; p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); /* Not allowed during certain operations */ @@ -1233,13 +1271,14 @@ static int oqs_sig_set_ctx_params(void *vpoqs_sigctx, const OSSL_PARAM params[]) const OSSL_PARAM *propsp = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PROPERTIES); - if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) - return 0; - if (propsp != NULL && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) - return 0; - if (!oqs_sig_setup_md(poqs_sigctx, mdname, mdprops)) - return 0; - } + if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) + return 0; + if (propsp != NULL + && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) + return 0; + if (!oqs_sig_setup_md(poqs_sigctx, mdname, mdprops)) + return 0; + } // not passing in parameters we can act on is no error return 1; @@ -1253,65 +1292,65 @@ static const OSSL_PARAM known_settable_ctx_params[] static const OSSL_PARAM *oqs_sig_settable_ctx_params(ossl_unused void *vpsm2ctx, ossl_unused void *provctx) { - /* - * TODO(3.0): Should this function return a different set of settable ctx - * params if the ctx is being used for a DigestSign/DigestVerify? In that - * case it is not allowed to set the digest size/digest name because the - * digest is explicitly set as part of the init. - * NOTE: Ideally we would check poqs_sigctx->flag_allow_md, but this is - * problematic because there is no nice way of passing the - * PROV_OQSSIG_CTX down to this function... - * Because we have API's that dont know about their parent.. - * e.g: EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig). - * We could pass NULL for that case (but then how useful is the check?). - */ - OQS_SIG_PRINTF("OQS SIG provider: settable_ctx_params called\n"); - return known_settable_ctx_params; + /* + * TODO(3.0): Should this function return a different set of settable ctx + * params if the ctx is being used for a DigestSign/DigestVerify? In that + * case it is not allowed to set the digest size/digest name because the + * digest is explicitly set as part of the init. + * NOTE: Ideally we would check poqs_sigctx->flag_allow_md, but this is + * problematic because there is no nice way of passing the + * PROV_OQSSIG_CTX down to this function... + * Because we have API's that dont know about their parent.. + * e.g: EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig). + * We could pass NULL for that case (but then how useful is the check?). + */ + OQS_SIG_PRINTF("OQS SIG provider: settable_ctx_params called\n"); + return known_settable_ctx_params; } static int oqs_sig_get_ctx_md_params(void *vpoqs_sigctx, OSSL_PARAM *params) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - OQS_SIG_PRINTF("OQS SIG provider: get_ctx_md_params called\n"); - if (poqs_sigctx->mdctx == NULL) - return 0; + OQS_SIG_PRINTF("OQS SIG provider: get_ctx_md_params called\n"); + if (poqs_sigctx->mdctx == NULL) + return 0; - return EVP_MD_CTX_get_params(poqs_sigctx->mdctx, params); + return EVP_MD_CTX_get_params(poqs_sigctx->mdctx, params); } static const OSSL_PARAM *oqs_sig_gettable_ctx_md_params(void *vpoqs_sigctx) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - OQS_SIG_PRINTF("OQS SIG provider: gettable_ctx_md_params called\n"); - if (poqs_sigctx->md == NULL) - return 0; + OQS_SIG_PRINTF("OQS SIG provider: gettable_ctx_md_params called\n"); + if (poqs_sigctx->md == NULL) + return 0; - return EVP_MD_gettable_ctx_params(poqs_sigctx->md); + return EVP_MD_gettable_ctx_params(poqs_sigctx->md); } static int oqs_sig_set_ctx_md_params(void *vpoqs_sigctx, const OSSL_PARAM params[]) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - OQS_SIG_PRINTF("OQS SIG provider: set_ctx_md_params called\n"); - if (poqs_sigctx->mdctx == NULL) - return 0; + OQS_SIG_PRINTF("OQS SIG provider: set_ctx_md_params called\n"); + if (poqs_sigctx->mdctx == NULL) + return 0; - return EVP_MD_CTX_set_params(poqs_sigctx->mdctx, params); + return EVP_MD_CTX_set_params(poqs_sigctx->mdctx, params); } static const OSSL_PARAM *oqs_sig_settable_ctx_md_params(void *vpoqs_sigctx) { - PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; + PROV_OQSSIG_CTX *poqs_sigctx = (PROV_OQSSIG_CTX *)vpoqs_sigctx; - if (poqs_sigctx->md == NULL) - return 0; + if (poqs_sigctx->md == NULL) + return 0; - OQS_SIG_PRINTF("OQS SIG provider: settable_ctx_md_params called\n"); - return EVP_MD_settable_ctx_params(poqs_sigctx->md); + OQS_SIG_PRINTF("OQS SIG provider: settable_ctx_md_params called\n"); + return EVP_MD_settable_ctx_params(poqs_sigctx->md); } const OSSL_DISPATCH oqs_signature_functions[] diff --git a/oqsprov/oqsdecoders.inc b/oqsprov/oqsdecoders.inc index e300a86c..17d73480 100644 --- a/oqsprov/oqsdecoders.inc +++ b/oqsprov/oqsdecoders.inc @@ -261,15 +261,15 @@ DECODER_w_structure("frodo640aes", der, PrivateKeyInfo, frodo640aes), dilithium3_p256), DECODER_w_structure("dilithium3_bp256", der, PrivateKeyInfo, dilithium3_bp256), - DECODER_w_structure("dilithium3_bp256", der, SubjectPublicKeyInfo, + DECODER_w_structure("dilithium3_bp256", der, SubjectPublicKeyInfo, dilithium3_bp256), - DECODER_w_structure("dilithium3_ed25519", der, PrivateKeyInfo, + DECODER_w_structure("dilithium3_ed25519", der, PrivateKeyInfo, dilithium3_ed25519), - DECODER_w_structure("dilithium3_ed25519", der, SubjectPublicKeyInfo, + DECODER_w_structure("dilithium3_ed25519", der, SubjectPublicKeyInfo, dilithium3_ed25519), - DECODER_w_structure("dilithium3_pss3072", der, PrivateKeyInfo, + DECODER_w_structure("dilithium3_pss3072", der, PrivateKeyInfo, dilithium3_pss3072), - DECODER_w_structure("dilithium3_pss3072", der, SubjectPublicKeyInfo, + DECODER_w_structure("dilithium3_pss3072", der, SubjectPublicKeyInfo, dilithium3_pss3072), #endif @@ -280,17 +280,17 @@ DECODER_w_structure("frodo640aes", der, PrivateKeyInfo, frodo640aes), p521_dilithium5), DECODER_w_structure("p521_dilithium5", der, SubjectPublicKeyInfo, p521_dilithium5), - DECODER_w_structure("dilithium5_p384", der, PrivateKeyInfo, + DECODER_w_structure("dilithium5_p384", der, PrivateKeyInfo, dilithium5_p384), - DECODER_w_structure("dilithium5_p384", der, SubjectPublicKeyInfo, + DECODER_w_structure("dilithium5_p384", der, SubjectPublicKeyInfo, dilithium5_p384), - DECODER_w_structure("dilithium5_bp384", der, PrivateKeyInfo, + DECODER_w_structure("dilithium5_bp384", der, PrivateKeyInfo, dilithium5_bp384), - DECODER_w_structure("dilithium5_bp384", der, SubjectPublicKeyInfo, + DECODER_w_structure("dilithium5_bp384", der, SubjectPublicKeyInfo, dilithium5_bp384), - DECODER_w_structure("dilithium5_ed448", der, PrivateKeyInfo, + DECODER_w_structure("dilithium5_ed448", der, PrivateKeyInfo, dilithium5_ed448), - DECODER_w_structure("dilithium5_ed448", der, SubjectPublicKeyInfo, + DECODER_w_structure("dilithium5_ed448", der, SubjectPublicKeyInfo, dilithium5_ed448), #endif #ifdef OQS_ENABLE_SIG_falcon_512 @@ -305,17 +305,16 @@ DECODER_w_structure("frodo640aes", der, PrivateKeyInfo, frodo640aes), rsa3072_falcon512), DECODER_w_structure("falcon512_p256", der, SubjectPublicKeyInfo, falcon512_p256), - DECODER_w_structure("falcon512_p256", der, PrivateKeyInfo, - falcon512_p256), + DECODER_w_structure("falcon512_p256", der, PrivateKeyInfo, falcon512_p256), DECODER_w_structure("falcon512_p256", der, SubjectPublicKeyInfo, falcon512_p256), - DECODER_w_structure("falcon512_bp256", der, PrivateKeyInfo, + DECODER_w_structure("falcon512_bp256", der, PrivateKeyInfo, falcon512_bp256), - DECODER_w_structure("falcon512_bp256", der, SubjectPublicKeyInfo, + DECODER_w_structure("falcon512_bp256", der, SubjectPublicKeyInfo, falcon512_bp256), - DECODER_w_structure("falcon512_ed25519", der, PrivateKeyInfo, + DECODER_w_structure("falcon512_ed25519", der, PrivateKeyInfo, falcon512_ed25519), - DECODER_w_structure("falcon512_ed25519", der, SubjectPublicKeyInfo, + DECODER_w_structure("falcon512_ed25519", der, SubjectPublicKeyInfo, falcon512_ed25519), #endif #ifdef OQS_ENABLE_SIG_falcon_1024 @@ -378,4 +377,4 @@ DECODER_w_structure("frodo640aes", der, PrivateKeyInfo, frodo640aes), DECODER_w_structure("rsa3072_sphincsshake128fsimple", der, SubjectPublicKeyInfo, rsa3072_sphincsshake128fsimple), #endif -///// OQS_TEMPLATE_FRAGMENT_MAKE_END + ///// OQS_TEMPLATE_FRAGMENT_MAKE_END diff --git a/oqsprov/oqsencoders.inc b/oqsprov/oqsencoders.inc index e12e45cb..165fb62d 100644 --- a/oqsprov/oqsencoders.inc +++ b/oqsprov/oqsencoders.inc @@ -677,7 +677,7 @@ ENCODER_w_structure("frodo640aes", frodo640aes, der, PrivateKeyInfo), SubjectPublicKeyInfo), ENCODER_w_structure("dilithium3_rsa3072", dilithium3_rsa3072, pem, SubjectPublicKeyInfo), - ENCODER_TEXT("dilithium3_rsa3072", dilithium3_rsa3072), + ENCODER_TEXT("dilithium3_rsa3072", dilithium3_rsa3072), ENCODER_w_structure("dilithium3_p256", dilithium3_p256, der, PrivateKeyInfo), ENCODER_w_structure("dilithium3_p256", dilithium3_p256, pem, @@ -691,43 +691,43 @@ ENCODER_w_structure("frodo640aes", frodo640aes, der, PrivateKeyInfo), ENCODER_w_structure("dilithium3_p256", dilithium3_p256, pem, SubjectPublicKeyInfo), ENCODER_TEXT("dilithium3_p256", dilithium3_p256), - ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, der, + ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, der, PrivateKeyInfo), - ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, pem, + ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, pem, PrivateKeyInfo), - ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, der, + ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, pem, + ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, der, + ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, der, SubjectPublicKeyInfo), - ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, pem, + ENCODER_w_structure("dilithium3_bp256", dilithium3_bp256, pem, SubjectPublicKeyInfo), ENCODER_TEXT("dilithium3_bp256", dilithium3_bp256), - ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, der, + ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, der, PrivateKeyInfo), - ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, pem, + ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, pem, PrivateKeyInfo), - ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, der, + ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, pem, + ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, der, + ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, der, SubjectPublicKeyInfo), - ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, pem, + ENCODER_w_structure("dilithium3_ed25519", dilithium3_ed25519, pem, SubjectPublicKeyInfo), ENCODER_TEXT("dilithium3_ed25519", dilithium3_ed25519), - ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, der, + ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, der, PrivateKeyInfo), - ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, pem, + ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, pem, PrivateKeyInfo), - ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, der, + ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, pem, + ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, der, + ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, der, SubjectPublicKeyInfo), - ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, pem, + ENCODER_w_structure("dilithium3_pss3072", dilithium3_pss3072, pem, SubjectPublicKeyInfo), ENCODER_TEXT("dilithium3_pss3072", dilithium3_pss3072), @@ -753,43 +753,43 @@ ENCODER_w_structure("frodo640aes", frodo640aes, der, PrivateKeyInfo), ENCODER_w_structure("p521_dilithium5", p521_dilithium5, pem, SubjectPublicKeyInfo), ENCODER_TEXT("p521_dilithium5", p521_dilithium5), - ENCODER_w_structure("dilithium5_p384", dilithium5_p384, der, + ENCODER_w_structure("dilithium5_p384", dilithium5_p384, der, PrivateKeyInfo), - ENCODER_w_structure("dilithium5_p384", dilithium5_p384, pem, + ENCODER_w_structure("dilithium5_p384", dilithium5_p384, pem, PrivateKeyInfo), - ENCODER_w_structure("dilithium5_p384", dilithium5_p384, der, + ENCODER_w_structure("dilithium5_p384", dilithium5_p384, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium5_p384", dilithium5_p384, pem, + ENCODER_w_structure("dilithium5_p384", dilithium5_p384, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium5_p384", dilithium5_p384, der, + ENCODER_w_structure("dilithium5_p384", dilithium5_p384, der, SubjectPublicKeyInfo), - ENCODER_w_structure("dilithium5_p384", dilithium5_p384, pem, + ENCODER_w_structure("dilithium5_p384", dilithium5_p384, pem, SubjectPublicKeyInfo), ENCODER_TEXT("dilithium5_p384", dilithium5_p384), - ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, der, + ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, der, PrivateKeyInfo), - ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, pem, + ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, pem, PrivateKeyInfo), - ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, der, + ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, pem, + ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, der, + ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, der, SubjectPublicKeyInfo), - ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, pem, + ENCODER_w_structure("dilithium5_bp384", dilithium5_bp384, pem, SubjectPublicKeyInfo), ENCODER_TEXT("dilithium5_bp384", dilithium5_bp384), - ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, der, + ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, der, PrivateKeyInfo), - ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, pem, + ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, pem, PrivateKeyInfo), - ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, der, + ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, pem, + ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, der, + ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, der, SubjectPublicKeyInfo), - ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, pem, + ENCODER_w_structure("dilithium5_ed448", dilithium5_ed448, pem, SubjectPublicKeyInfo), ENCODER_TEXT("dilithium5_ed448", dilithium5_ed448), #endif @@ -825,10 +825,8 @@ ENCODER_w_structure("frodo640aes", frodo640aes, der, PrivateKeyInfo), ENCODER_w_structure("rsa3072_falcon512", rsa3072_falcon512, pem, SubjectPublicKeyInfo), ENCODER_TEXT("rsa3072_falcon512", rsa3072_falcon512), - ENCODER_w_structure("falcon512_p256", falcon512_p256, der, - PrivateKeyInfo), - ENCODER_w_structure("falcon512_p256", falcon512_p256, pem, - PrivateKeyInfo), + ENCODER_w_structure("falcon512_p256", falcon512_p256, der, PrivateKeyInfo), + ENCODER_w_structure("falcon512_p256", falcon512_p256, pem, PrivateKeyInfo), ENCODER_w_structure("falcon512_p256", falcon512_p256, der, EncryptedPrivateKeyInfo), ENCODER_w_structure("falcon512_p256", falcon512_p256, pem, @@ -838,30 +836,30 @@ ENCODER_w_structure("frodo640aes", frodo640aes, der, PrivateKeyInfo), ENCODER_w_structure("falcon512_p256", falcon512_p256, pem, SubjectPublicKeyInfo), ENCODER_TEXT("falcon512_p256", falcon512_p256), - ENCODER_w_structure("falcon512_bp256", falcon512_bp256, der, + ENCODER_w_structure("falcon512_bp256", falcon512_bp256, der, PrivateKeyInfo), - ENCODER_w_structure("falcon512_bp256", falcon512_bp256, pem, + ENCODER_w_structure("falcon512_bp256", falcon512_bp256, pem, PrivateKeyInfo), - ENCODER_w_structure("falcon512_bp256", falcon512_bp256, der, + ENCODER_w_structure("falcon512_bp256", falcon512_bp256, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("falcon512_bp256", falcon512_bp256, pem, + ENCODER_w_structure("falcon512_bp256", falcon512_bp256, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("falcon512_bp256", falcon512_bp256, der, + ENCODER_w_structure("falcon512_bp256", falcon512_bp256, der, SubjectPublicKeyInfo), - ENCODER_w_structure("falcon512_bp256", falcon512_bp256, pem, + ENCODER_w_structure("falcon512_bp256", falcon512_bp256, pem, SubjectPublicKeyInfo), ENCODER_TEXT("falcon512_bp256", falcon512_bp256), - ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, der, + ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, der, PrivateKeyInfo), - ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, pem, + ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, pem, PrivateKeyInfo), - ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, der, + ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, der, EncryptedPrivateKeyInfo), - ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, pem, + ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, pem, EncryptedPrivateKeyInfo), - ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, der, + ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, der, SubjectPublicKeyInfo), - ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, pem, + ENCODER_w_structure("falcon512_ed25519", falcon512_ed25519, pem, SubjectPublicKeyInfo), ENCODER_TEXT("falcon512_ed25519", falcon512_ed25519), #endif @@ -1061,4 +1059,4 @@ ENCODER_w_structure("frodo640aes", frodo640aes, der, PrivateKeyInfo), ENCODER_TEXT("rsa3072_sphincsshake128fsimple", rsa3072_sphincsshake128fsimple), #endif -///// OQS_TEMPLATE_FRAGMENT_MAKE_END + ///// OQS_TEMPLATE_FRAGMENT_MAKE_END diff --git a/oqsprov/oqsprov.c b/oqsprov/oqsprov.c index bf7ad14e..24d02400 100644 --- a/oqsprov/oqsprov.c +++ b/oqsprov/oqsprov.c @@ -193,34 +193,34 @@ const char *oqs_oid_alg_list[OQS_OID_CNT] = { "2.16.840.1.114027.80.7.1.7", "dilithium3_rsa3072", "2.16.840.1.114027.80.7.1.8", - "dilithium3_p256", - "2.16.840.1.114027.80.7.1.9", - "dilithium3_bp256", - "2.16.840.1.114027.80.7.1.10", - "dilithium3_ed25519", - "2.16.840.1.114027.80.7.1.11", - "dilithium5_p384", - "2.16.840.1.114027.80.7.1.12", - "dilithium5_bp384", - "2.16.840.1.114027.80.7.1.13", - "dilithium5_ed448", - "2.16.840.1.114027.80.7.1.14", - "falcon512_p256", - "2.16.840.1.114027.80.7.1.15", - "falcon512_bp256", - "2.16.840.1.114027.80.7.1.16", - "falcon512_ed25519", - "2.16.840.1.114027.80.7.1.6", - "dilithium3_pss3072", - "2.16.840.1.114027.80.7.1.1", + "dilithium3_p256", + "2.16.840.1.114027.80.7.1.9", + "dilithium3_bp256", + "2.16.840.1.114027.80.7.1.10", + "dilithium3_ed25519", + "2.16.840.1.114027.80.7.1.11", + "dilithium5_p384", + "2.16.840.1.114027.80.7.1.12", + "dilithium5_bp384", + "2.16.840.1.114027.80.7.1.13", + "dilithium5_ed448", + "2.16.840.1.114027.80.7.1.14", + "falcon512_p256", + "2.16.840.1.114027.80.7.1.15", + "falcon512_bp256", + "2.16.840.1.114027.80.7.1.16", + "falcon512_ed25519", + "2.16.840.1.114027.80.7.1.6", + "dilithium3_pss3072", + "2.16.840.1.114027.80.7.1.1", "dilithium2_pss2048", - "2.16.840.1.114027.80.7.1.2", + "2.16.840.1.114027.80.7.1.2", "dilithium2_rsa2048", - "2.16.840.1.114027.80.7.1.3", + "2.16.840.1.114027.80.7.1.3", "dilithium2_ed25519", - "2.16.840.1.114027.80.7.1.4", + "2.16.840.1.114027.80.7.1.4", "dilithium2_p256", - "2.16.840.1.114027.80.7.1.5", + "2.16.840.1.114027.80.7.1.5", "dilithium2_bp256", ///// OQS_TEMPLATE_FRAGMENT_ASSIGN_SIG_OIDS_END }; diff --git a/oqsprov/oqsprov_keys.c b/oqsprov/oqsprov_keys.c index 36612370..76087dfc 100644 --- a/oqsprov/oqsprov_keys.c +++ b/oqsprov/oqsprov_keys.c @@ -17,7 +17,6 @@ #include #include - #ifdef NDEBUG # define OQS_KEY_PRINTF(a) # define OQS_KEY_PRINTF2(a, b) @@ -34,8 +33,7 @@ printf(a, b, c) #endif // NDEBUG -typedef enum -{ +typedef enum { KEY_OP_PUBLIC, KEY_OP_PRIVATE, KEY_OP_KEYGEN @@ -43,8 +41,7 @@ typedef enum /// NID/name table -typedef struct -{ +typedef struct { int nid; char *tlsname; char *oqsname; @@ -153,38 +150,22 @@ static oqs_nid_name_t nid_names[NID_TABLE_LEN] = { KEY_TYPE_HYB_SIG, 128}, {0, "rsa3072_sphincsshake128fsimple", OQS_SIG_alg_sphincs_shake_128f_simple, KEY_TYPE_HYB_SIG, 128}, - {0, "dilithium3_rsa3072", OQS_SIG_alg_dilithium_3, - KEY_TYPE_CMP_SIG, 128}, - {0, "dilithium3_p256", OQS_SIG_alg_dilithium_3, - KEY_TYPE_CMP_SIG, 128}, - {0, "falcon512_p256", OQS_SIG_alg_falcon_512, - KEY_TYPE_CMP_SIG, 128}, - {0, "dilithium5_p384", OQS_SIG_alg_dilithium_5, - KEY_TYPE_CMP_SIG, 192}, - {0, "dilithium3_bp256", OQS_SIG_alg_dilithium_3, - KEY_TYPE_CMP_SIG, 256}, - {0, "dilithium3_ed25519", OQS_SIG_alg_dilithium_3, - KEY_TYPE_CMP_SIG, 128}, - {0, "dilithium5_bp384", OQS_SIG_alg_dilithium_5, - KEY_TYPE_CMP_SIG, 384}, - {0, "dilithium5_ed448", OQS_SIG_alg_dilithium_5, - KEY_TYPE_CMP_SIG, 192}, - {0, "falcon512_bp256", OQS_SIG_alg_falcon_512, - KEY_TYPE_CMP_SIG, 256}, - {0, "falcon512_ed25519", OQS_SIG_alg_falcon_512, - KEY_TYPE_CMP_SIG, 128}, - {0, "dilithium3_pss3072", OQS_SIG_alg_dilithium_3, - KEY_TYPE_CMP_SIG, 128}, - {0, "dilithium2_pss2048", OQS_SIG_alg_dilithium_2, - KEY_TYPE_CMP_SIG, 112}, - {0, "dilithium2_rsa2048", OQS_SIG_alg_dilithium_2, - KEY_TYPE_CMP_SIG, 112}, - {0, "dilithium2_ed25519", OQS_SIG_alg_dilithium_2, - KEY_TYPE_CMP_SIG, 128}, - {0, "dilithium2_p256", OQS_SIG_alg_dilithium_2, - KEY_TYPE_CMP_SIG, 128}, - {0, "dilithium2_bp256", OQS_SIG_alg_dilithium_2, - KEY_TYPE_CMP_SIG, 256}, + {0, "dilithium3_rsa3072", OQS_SIG_alg_dilithium_3, KEY_TYPE_CMP_SIG, 128}, + {0, "dilithium3_p256", OQS_SIG_alg_dilithium_3, KEY_TYPE_CMP_SIG, 128}, + {0, "falcon512_p256", OQS_SIG_alg_falcon_512, KEY_TYPE_CMP_SIG, 128}, + {0, "dilithium5_p384", OQS_SIG_alg_dilithium_5, KEY_TYPE_CMP_SIG, 192}, + {0, "dilithium3_bp256", OQS_SIG_alg_dilithium_3, KEY_TYPE_CMP_SIG, 256}, + {0, "dilithium3_ed25519", OQS_SIG_alg_dilithium_3, KEY_TYPE_CMP_SIG, 128}, + {0, "dilithium5_bp384", OQS_SIG_alg_dilithium_5, KEY_TYPE_CMP_SIG, 384}, + {0, "dilithium5_ed448", OQS_SIG_alg_dilithium_5, KEY_TYPE_CMP_SIG, 192}, + {0, "falcon512_bp256", OQS_SIG_alg_falcon_512, KEY_TYPE_CMP_SIG, 256}, + {0, "falcon512_ed25519", OQS_SIG_alg_falcon_512, KEY_TYPE_CMP_SIG, 128}, + {0, "dilithium3_pss3072", OQS_SIG_alg_dilithium_3, KEY_TYPE_CMP_SIG, 128}, + {0, "dilithium2_pss2048", OQS_SIG_alg_dilithium_2, KEY_TYPE_CMP_SIG, 112}, + {0, "dilithium2_rsa2048", OQS_SIG_alg_dilithium_2, KEY_TYPE_CMP_SIG, 112}, + {0, "dilithium2_ed25519", OQS_SIG_alg_dilithium_2, KEY_TYPE_CMP_SIG, 128}, + {0, "dilithium2_p256", OQS_SIG_alg_dilithium_2, KEY_TYPE_CMP_SIG, 128}, + {0, "dilithium2_bp256", OQS_SIG_alg_dilithium_2, KEY_TYPE_CMP_SIG, 256}, ///// OQS_TEMPLATE_FRAGMENT_OQSNAMES_END }; @@ -220,19 +201,17 @@ static int get_keytype(int nid) return 0; } - -char *get_oqsname_fromtls(char *tlsname) +char *get_oqsname_fromtls(char *tlsname) { int i; - for (i = 0; i < NID_TABLE_LEN; i++) - { - if (nid_names[i].keytype == KEY_TYPE_SIG) - { - if (!strcmp(nid_names[i].oqsname, tlsname) || !strcmp(nid_names[i].tlsname, tlsname)) + for (i = 0; i < NID_TABLE_LEN; i++) { + if (nid_names[i].keytype == KEY_TYPE_SIG) { + if (!strcmp(nid_names[i].oqsname, tlsname) + || !strcmp(nid_names[i].tlsname, tlsname)) return nid_names[i].oqsname; } } - return 0; //classical + return 0; // classical } char *get_oqsname(int nid) @@ -245,49 +224,49 @@ char *get_oqsname(int nid) return 0; } -char* get_cmpname(int nid, int index) +char *get_cmpname(int nid, int index) { int i, j; - char* name; - char* first_token; - char* token; - char* s; + char *name; + char *first_token; + char *token; + char *s; if ((i = get_oqsalg_idx(nid)) == -1) return NULL; s = OPENSSL_strdup(nid_names[i].tlsname); first_token = strtok_r(s, "_", &s); - if (index == 0){ - name = OPENSSL_strdup(first_token); - }else{ - for (j = 0; j < index; j ++) - token = strtok_r(s, "_", &s); + if (index == 0) { + name = OPENSSL_strdup(first_token); + } else { + for (j = 0; j < index; j++) + token = strtok_r(s, "_", &s); name = OPENSSL_strdup(token); } OPENSSL_free(first_token); return name; } -//count the amount of keys in the structure +// count the amount of keys in the structure int get_qntcmp(int nid) { int i; int index = 1; if ((i = get_oqsalg_idx(nid)) == -1) return -1; - if (nid_names[i].keytype == KEY_TYPE_CMP_SIG){ - char* s = OPENSSL_strdup(nid_names[i].tlsname); - char* first_token = strtok_r(s, "_", &s); - char* token; + if (nid_names[i].keytype == KEY_TYPE_CMP_SIG) { + char *s = OPENSSL_strdup(nid_names[i].tlsname); + char *first_token = strtok_r(s, "_", &s); + char *token; index = 0; - while (token != NULL){ + while (token != NULL) { token = strtok_r(s, "_", &s); index++; } OPENSSL_free(first_token); - }else{ + } else { if ((nid_names[i].keytype == KEY_TYPE_HYB_SIG) - ||(nid_names[i].keytype == KEY_TYPE_ECP_HYB_KEM) - ||(nid_names[i].keytype == KEY_TYPE_ECX_HYB_KEM)){ + || (nid_names[i].keytype == KEY_TYPE_ECP_HYB_KEM) + || (nid_names[i].keytype == KEY_TYPE_ECX_HYB_KEM)) { index = 2; } } @@ -314,56 +293,47 @@ static int oqsx_key_set_composites(OQSX_KEY *key) if (key->numkeys == 1) { key->comp_privkey[0] = key->privkey; key->comp_pubkey[0] = key->pubkey; - } - else - { // TBD: extend for more than 1 classic key or first OQS for composite: - if (key->keytype == KEY_TYPE_CMP_SIG){ + } else { // TBD: extend for more than 1 classic key or first OQS for + // composite: + if (key->keytype == KEY_TYPE_CMP_SIG) { int i; int privlen = 0; int publen = 0; - for (i = 0; i < key->numkeys; i++){ - if (key->privkey) - { + for (i = 0; i < key->numkeys; i++) { + if (key->privkey) { key->comp_privkey[i] = (char *)key->privkey + privlen; privlen += key->privkeylen_cmp[i]; - } - else - { + } else { key->comp_privkey[i] = NULL; } - if (key->pubkey) - { + if (key->pubkey) { key->comp_pubkey[i] = (char *)key->pubkey + publen; publen += key->pubkeylen_cmp[i]; - } - else - { + } else { key->comp_pubkey[i] = NULL; } } - }else{ - int classic_pubkey_len, classic_privkey_len; - - if (key->privkey) { - key->comp_privkey[0] = (char *)key->privkey + SIZE_OF_UINT32; - DECODE_UINT32(classic_privkey_len, key->privkey); - key->comp_privkey[1] - = (char *)key->privkey + classic_privkey_len + SIZE_OF_UINT32; - } - else { - key->comp_privkey[0] = NULL; - key->comp_privkey[1] = NULL; - } - if (key->pubkey) { - key->comp_pubkey[0] = (char *)key->pubkey + SIZE_OF_UINT32; - DECODE_UINT32(classic_pubkey_len, key->pubkey); - key->comp_pubkey[1] - = (char *)key->pubkey + classic_pubkey_len + SIZE_OF_UINT32; - } - else { - key->comp_pubkey[0] = NULL; - key->comp_pubkey[1] = NULL; - } + } else { + int classic_pubkey_len, classic_privkey_len; + + if (key->privkey) { + key->comp_privkey[0] = (char *)key->privkey + SIZE_OF_UINT32; + DECODE_UINT32(classic_privkey_len, key->privkey); + key->comp_privkey[1] = (char *)key->privkey + + classic_privkey_len + SIZE_OF_UINT32; + } else { + key->comp_privkey[0] = NULL; + key->comp_privkey[1] = NULL; + } + if (key->pubkey) { + key->comp_pubkey[0] = (char *)key->pubkey + SIZE_OF_UINT32; + DECODE_UINT32(classic_pubkey_len, key->pubkey); + key->comp_pubkey[1] + = (char *)key->pubkey + classic_pubkey_len + SIZE_OF_UINT32; + } else { + key->comp_pubkey[0] = NULL; + key->comp_pubkey[1] = NULL; + } } } err: @@ -423,13 +393,11 @@ EVP_PKEY *setECParams(EVP_PKEY *eck, int nid) = {0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22}; const unsigned char p521params[] = {0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23}; - const char ed25519params[] - = {0x06, 0x03, 0x2b, 0x65, 0x70}; - const char ed448params[] - = {0x06, 0x03, 0x2b, 0x65, 0x71}; - const char bp256params[] + const char ed25519params[] = {0x06, 0x03, 0x2b, 0x65, 0x70}; + const char ed448params[] = {0x06, 0x03, 0x2b, 0x65, 0x71}; + const char bp256params[] = {0x06, 0x09, 0x2b, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07}; - const char bp384params[] + const char bp384params[] = {0x06, 0x09, 0x2b, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0b}; const unsigned char *params; @@ -464,15 +432,15 @@ EVP_PKEY *setECParams(EVP_PKEY *eck, int nid) static const OQSX_EVP_INFO nids_sig[] = { {EVP_PKEY_EC, NID_X9_62_prime256v1, 0, 65, 121, 32, 72}, // 128 bit - {EVP_PKEY_EC, NID_secp384r1, 0, 97, 167, 48, 104}, // 192 bit + {EVP_PKEY_EC, NID_secp384r1, 0, 97, 167, 48, 104}, // 192 bit {EVP_PKEY_EC, NID_secp521r1, 0, 133, 223, 66, 141}, // 256 bit {EVP_PKEY_EC, NID_brainpoolP256r1, 0, 65, 122, 32, 72}, // 256 bit {EVP_PKEY_EC, NID_brainpoolP384r1, 0, 97, 171, 48, 104}, // 384 bit {EVP_PKEY_RSA, NID_rsaEncryption, 0, 398, 1770, 0, 384}, // 128 bit {EVP_PKEY_RSA, NID_rsaEncryption, 0, 270, 1193, 0, 256}, // 112 bit - {EVP_PKEY_ED25519, NID_ED25519, 1 , 32, 32, 32, 72}, // 128 bit - {EVP_PKEY_ED448, NID_ED448, 1 , 57, 57, 57, 122}, // 192 bit - + {EVP_PKEY_ED25519, NID_ED25519, 1, 32, 32, 32, 72}, // 128 bit + {EVP_PKEY_ED448, NID_ED448, 1, 57, 57, 57, 122}, // 192 bit + }; // These two array need to stay synced: static const char *OQSX_ECP_NAMES[] = {"p256", "p384", "p521", 0}; @@ -497,18 +465,15 @@ static int oqsx_hybsig_init(int bit_security, OQSX_EVP_CTX *evp_ctx, int idx = (bit_security - 128) / 64; ON_ERR_GOTO(idx < 0 || idx > 5, err); - if (!strncmp(algname, "rsa", 3) || !strncmp(algname, "pss", 3)){ + if (!strncmp(algname, "rsa", 3) || !strncmp(algname, "pss", 3)) { idx += 5; if (bit_security == 112) idx += 1; - } else if (algname[0] != 'p' && algname[0] != 'e') - { - if (algname[0] == 'b'){ //bp - if (algname[2] == '2') //bp256 + } else if (algname[0] != 'p' && algname[0] != 'e') { + if (algname[0] == 'b') { // bp + if (algname[2] == '2') // bp256 idx += 1; - } - else - { + } else { OQS_KEY_PRINTF2("OQS KEY: Incorrect hybrid name: %s\n", algname); ret = 0; goto err; @@ -517,7 +482,7 @@ static int oqsx_hybsig_init(int bit_security, OQSX_EVP_CTX *evp_ctx, ON_ERR_GOTO(idx < 0 || idx > 6, err); - if(algname[0] == 'e') //ED25519 or ED448 + if (algname[0] == 'e') // ED25519 or ED448 { evp_ctx->evp_info = &nids_sig[idx + 7]; @@ -534,18 +499,17 @@ static int oqsx_hybsig_init(int bit_security, OQSX_EVP_CTX *evp_ctx, evp_ctx->ctx = EVP_PKEY_CTX_new_id(evp_ctx->evp_info->keytype, NULL); ON_ERR_GOTO(!evp_ctx->ctx, err); - - if (idx < 5) - { // EC - ret = EVP_PKEY_paramgen_init(evp_ctx->ctx); - ON_ERR_GOTO(ret <= 0, err); - - ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(evp_ctx->ctx, - evp_ctx->evp_info->nid); - ON_ERR_GOTO(ret <= 0, free_evp_ctx); - - ret = EVP_PKEY_paramgen(evp_ctx->ctx, &evp_ctx->keyParam); - ON_ERR_GOTO(ret <= 0 || !evp_ctx->keyParam, free_evp_ctx); + + if (idx < 5) { // EC + ret = EVP_PKEY_paramgen_init(evp_ctx->ctx); + ON_ERR_GOTO(ret <= 0, err); + + ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid( + evp_ctx->ctx, evp_ctx->evp_info->nid); + ON_ERR_GOTO(ret <= 0, free_evp_ctx); + + ret = EVP_PKEY_paramgen(evp_ctx->ctx, &evp_ctx->keyParam); + ON_ERR_GOTO(ret <= 0 || !evp_ctx->keyParam, free_evp_ctx); } } // RSA bit length set only during keygen @@ -627,29 +591,25 @@ static OQSX_KEY *oqsx_key_op(const X509_ALGOR *palg, const unsigned char *p, int ret = 0; OQS_KEY_PRINTF2("OQSX KEY: key_op called with data of len %d\n", plen); - if (palg != NULL) - { + if (palg != NULL) { int ptype; /* Algorithm parameters must be absent */ X509_ALGOR_get0(NULL, &ptype, NULL, palg); - if (ptype != V_ASN1_UNDEF || !palg || !palg->algorithm) - { + if (ptype != V_ASN1_UNDEF || !palg || !palg->algorithm) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); return 0; } nid = OBJ_obj2nid(palg->algorithm); } - if (p == NULL || nid == EVP_PKEY_NONE || nid == NID_undef) - { + if (p == NULL || nid == EVP_PKEY_NONE || nid == NID_undef) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); return 0; } key = oqsx_key_new_from_nid(libctx, propq, nid); - if (key == NULL) - { + if (key == NULL) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); return 0; } @@ -697,182 +657,193 @@ static OQSX_KEY *oqsx_key_op(const X509_ALGOR *palg, const unsigned char *p, size_t actualprivkeylen = key->privkeylen; // for hybrid keys, we expect classic priv key||OQS priv key||OQS pub // key classic pub key must/can be re-created from classic private key - if (key->keytype == KEY_TYPE_CMP_SIG){ + if (key->keytype == KEY_TYPE_CMP_SIG) { size_t privlen = 0; size_t publen = 0; size_t previous_privlen = 0; size_t previous_publen = 0; int pqc_pub_enc = 0; int i; - - //check if key is the right size - for (i = 0; i < key->numkeys; i++){ + + // check if key is the right size + for (i = 0; i < key->numkeys; i++) { char *name; - if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) == NULL){ + if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) + == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); goto err; } privlen = key->privkeylen_cmp[i]; - if (get_oqsname_fromtls(name) == 0){//classical key + if (get_oqsname_fromtls(name) == 0) { // classical key publen = 0; - }else{//PQC key - publen = key->pubkeylen_cmp[i]; //pubkey in PQC privkey is OPTIONAL + } else { // PQC key + publen = key->pubkeylen_cmp[i]; // pubkey in PQC privkey is + // OPTIONAL } previous_privlen += privlen; previous_publen += publen; OPENSSL_free(name); - } - if (previous_privlen != plen) - { - //is ok, PQC pubkey might be in privkey + } + if (previous_privlen != plen) { + // is ok, PQC pubkey might be in privkey pqc_pub_enc = 1; - if (previous_privlen + previous_publen != plen){ + if (previous_privlen + previous_publen != plen) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); goto err; } - if (oqsx_key_allocate_keymaterial(key, 0)) - { + if (oqsx_key_allocate_keymaterial(key, 0)) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); goto err; } } - if (oqsx_key_allocate_keymaterial(key, 1)) - { + if (oqsx_key_allocate_keymaterial(key, 1)) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); goto err; } previous_privlen = 0; previous_publen = 0; - for (i = 0; i < key->numkeys; i++){ + for (i = 0; i < key->numkeys; i++) { size_t classic_publen = 0; char *name; - if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) == NULL){ + if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) + == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); goto err; } - if (get_oqsname_fromtls(name) == 0){//classical key - publen = 0; //no pubkey encoded with privkey on classical keys. will recreate the pubkey later - if(key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype == EVP_PKEY_RSA){ //get the RSA real key size - unsigned char* enc_len = OPENSSL_strndup(p + previous_privlen + previous_publen, 4); + if (get_oqsname_fromtls(name) == 0) { // classical key + publen = 0; // no pubkey encoded with privkey on classical + // keys. will recreate the pubkey later + if (key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype + == EVP_PKEY_RSA) { // get the RSA real key size + unsigned char *enc_len = OPENSSL_strndup( + p + previous_privlen + previous_publen, 4); OPENSSL_cleanse(enc_len, 2); DECODE_UINT32(privlen, enc_len); privlen += 4; OPENSSL_free(enc_len); - if (privlen > key->privkeylen_cmp[i]){ + if (privlen > key->privkeylen_cmp[i]) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); goto err; } - }else - privlen = key->privkeylen_cmp[i]; - }else{//PQC key + } else + privlen = key->privkeylen_cmp[i]; + } else { // PQC key privlen = key->privkeylen_cmp[i]; if (pqc_pub_enc) publen = key->pubkeylen_cmp[i]; else publen = 0; - - } - memcpy(key->privkey + previous_privlen, p + previous_privlen + previous_publen, privlen); - memcpy(key->pubkey + previous_publen, p + privlen + previous_privlen + previous_publen, publen); - previous_privlen += privlen; - previous_publen += publen; - OPENSSL_free(name); + } + memcpy(key->privkey + previous_privlen, + p + previous_privlen + previous_publen, privlen); + memcpy(key->pubkey + previous_publen, + p + privlen + previous_privlen + previous_publen, + publen); + previous_privlen += privlen; + previous_publen += publen; + OPENSSL_free(name); } - }else{ - if (key->numkeys == 2) { - DECODE_UINT32(classical_privatekey_len, - p); // actual classic key len - // adjust expected size - if (classical_privatekey_len > key->evp_info->length_private_key) { - ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); - goto err; + } else { + if (key->numkeys == 2) { + DECODE_UINT32(classical_privatekey_len, + p); // actual classic key len + // adjust expected size + if (classical_privatekey_len + > key->evp_info->length_private_key) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); + goto err; + } + actualprivkeylen -= (key->evp_info->length_private_key + - classical_privatekey_len); } - actualprivkeylen -= (key->evp_info->length_private_key - - classical_privatekey_len); - } #ifdef USE_ENCODING_LIB - if (key->oqsx_encoding_ctx.encoding_ctx - && key->oqsx_encoding_ctx.encoding_impl) { - const qsc_encoding_t *encoding_ctx - = key->oqsx_encoding_ctx.encoding_ctx; + if (key->oqsx_encoding_ctx.encoding_ctx + && key->oqsx_encoding_ctx.encoding_impl) { + const qsc_encoding_t *encoding_ctx + = key->oqsx_encoding_ctx.encoding_ctx; # ifdef NOPUBKEY_IN_PRIVKEY - // if the raw private key includes the public key, the optional part - // is needed, otherwise not. - int withoptional - = (encoding_ctx->raw_private_key_encodes_public_key ? 1 : 0); + // if the raw private key includes the public key, the optional + // part is needed, otherwise not. + int withoptional + = (encoding_ctx->raw_private_key_encodes_public_key ? 1 + : 0); # else - int withoptional = 1; + int withoptional = 1; # endif - int pubkey_available = withoptional; - if (oqsx_key_allocate_keymaterial(key, 1)) { - ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); - goto err; - } - if (pubkey_available) { - if (oqsx_key_allocate_keymaterial(key, 0)) { + int pubkey_available = withoptional; + if (oqsx_key_allocate_keymaterial(key, 1)) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); goto err; } - } + if (pubkey_available) { + if (oqsx_key_allocate_keymaterial(key, 0)) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + goto err; + } + } - if (qsc_decode( - encoding_ctx, key->oqsx_encoding_ctx.encoding_impl, 0, - (pubkey_available ? (unsigned char **)&key->pubkey : 0), p, - (unsigned char **)&key->privkey, withoptional) - != QSC_ENC_OK) { - ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); - goto err; - } + if (qsc_decode( + encoding_ctx, key->oqsx_encoding_ctx.encoding_impl, 0, + (pubkey_available ? (unsigned char **)&key->pubkey : 0), + p, (unsigned char **)&key->privkey, withoptional) + != QSC_ENC_OK) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); + goto err; + } - } else { + } else { #endif #ifdef NOPUBKEY_IN_PRIVKEY - if (actualprivkeylen != plen) { + if (actualprivkeylen != plen) { + OQS_KEY_PRINTF3( + "OQSX KEY: private key with unexpected length %d vs %d\n", + plen, (int)(actualprivkeylen)); +#else + if (actualprivkeylen + oqsx_key_get_oqs_public_key_len(key) + != plen) { OQS_KEY_PRINTF3( "OQSX KEY: private key with unexpected length %d vs %d\n", - plen, (int)(actualprivkeylen)); -#else - if (actualprivkeylen + oqsx_key_get_oqs_public_key_len(key) != plen) { - OQS_KEY_PRINTF3( - "OQSX KEY: private key with unexpected length %d vs %d\n", plen, - (int)(actualprivkeylen + oqsx_key_get_oqs_public_key_len(key))); + plen, + (int)(actualprivkeylen + + oqsx_key_get_oqs_public_key_len(key))); #endif - ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); - goto err; - } - if (oqsx_key_allocate_keymaterial(key, 1) + ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); + goto err; + } + if (oqsx_key_allocate_keymaterial(key, 1) #ifndef NOPUBKEY_IN_PRIVKEY - || oqsx_key_allocate_keymaterial(key, 0) + || oqsx_key_allocate_keymaterial(key, 0) #endif - ) { - ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); - goto err; - } - // first populate private key data - memcpy(key->privkey, p, actualprivkeylen); + ) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + goto err; + } + // first populate private key data + memcpy(key->privkey, p, actualprivkeylen); #ifndef NOPUBKEY_IN_PRIVKEY - // only enough data to fill public OQS key component - if (oqsx_key_get_oqs_public_key_len(key) - != plen - actualprivkeylen) { - ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); - goto err; - } - // populate OQS public key structure - 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); - } else - memcpy(key->pubkey, p + key->privkeylen, - plen - key->privkeylen); + // only enough data to fill public OQS key component + if (oqsx_key_get_oqs_public_key_len(key) + != plen - actualprivkeylen) { + ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); + goto err; + } + // populate OQS public key structure + 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); + } else + memcpy(key->pubkey, p + key->privkeylen, + plen - key->privkeylen); #endif } - } + } #ifdef USE_ENCODING_LIB } #endif @@ -891,8 +862,7 @@ static int oqsx_key_recreate_classickey(OQSX_KEY *key, oqsx_key_op_t op) { if (key->keytype == KEY_TYPE_HYB_SIG) { // hybrid key int classical_pubkey_len, classical_privkey_len; - if (!key->evp_info) - { + if (!key->evp_info) { ERR_raise(ERR_LIB_USER, OQSPROV_R_EVPINFO_MISSING); goto rec_err; } @@ -967,74 +937,98 @@ static int oqsx_key_recreate_classickey(OQSX_KEY *key, oqsx_key_op_t op) } } } - if (key->keytype == KEY_TYPE_CMP_SIG){ + if (key->keytype == KEY_TYPE_CMP_SIG) { int i; - if (op == KEY_OP_PUBLIC){ + if (op == KEY_OP_PUBLIC) { - for (i = 0; i < key->numkeys; i++){ + for (i = 0; i < key->numkeys; i++) { char *name; - if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) == NULL){ + if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) + == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); goto rec_err; } - if (get_oqsname_fromtls(name) == 0){ + if (get_oqsname_fromtls(name) == 0) { EVP_PKEY *npk = EVP_PKEY_new(); - if (key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype != EVP_PKEY_RSA ) - { - npk = setECParams(npk, key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->nid); + if (key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype + != EVP_PKEY_RSA) { + npk = setECParams(npk, + key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->nid); } const unsigned char *enc_pubkey = key->comp_pubkey[i]; - if (!key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->raw_key_support){ - key->cmp_classical_pkey[i] = d2i_PublicKey(key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype, &npk, &enc_pubkey, key->pubkeylen_cmp[i]); - }else - key->cmp_classical_pkey[i] = EVP_PKEY_new_raw_public_key(key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype, NULL, enc_pubkey, key->pubkeylen_cmp[i]); - if (!key->cmp_classical_pkey[i]) - { + if (!key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->raw_key_support) { + key->cmp_classical_pkey[i] = d2i_PublicKey( + key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype, + &npk, &enc_pubkey, key->pubkeylen_cmp[i]); + } else + key->cmp_classical_pkey[i] + = EVP_PKEY_new_raw_public_key( + key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype, + NULL, enc_pubkey, key->pubkeylen_cmp[i]); + if (!key->cmp_classical_pkey[i]) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); OPENSSL_free(name); goto rec_err; } } OPENSSL_free(name); - } } - if (op == KEY_OP_PRIVATE){ + if (op == KEY_OP_PRIVATE) { - for (i = 0; i < key->numkeys; i++){ + for (i = 0; i < key->numkeys; i++) { char *name; - if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) == NULL){ + if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) + == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); goto rec_err; } - if (get_oqsname_fromtls(name) == 0){ + if (get_oqsname_fromtls(name) == 0) { const unsigned char *enc_privkey = key->comp_privkey[i]; - if (!key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->raw_key_support) - key->cmp_classical_pkey[i] = d2i_PrivateKey(key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype, NULL, &enc_privkey, key->privkeylen_cmp[i]); + if (!key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->raw_key_support) + key->cmp_classical_pkey[i] = d2i_PrivateKey( + key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype, + NULL, &enc_privkey, key->privkeylen_cmp[i]); else - key->cmp_classical_pkey[i] = EVP_PKEY_new_raw_private_key(key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->keytype, NULL, enc_privkey, key->privkeylen_cmp[i]); - if (!key->cmp_classical_pkey[i]) - { + key->cmp_classical_pkey[i] + = EVP_PKEY_new_raw_private_key( + key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->keytype, + NULL, enc_privkey, key->privkeylen_cmp[i]); + if (!key->cmp_classical_pkey[i]) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); OPENSSL_free(name); goto rec_err; } - if (!key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->raw_key_support){ - unsigned char* comp_pubkey = key->comp_pubkey[i]; - int pubkeylen = i2d_PublicKey(key->cmp_classical_pkey[i], &comp_pubkey); - if (pubkeylen != key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->length_public_key){ + if (!key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->raw_key_support) { + unsigned char *comp_pubkey = key->comp_pubkey[i]; + int pubkeylen = i2d_PublicKey( + key->cmp_classical_pkey[i], &comp_pubkey); + if (pubkeylen + != key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->length_public_key) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); OPENSSL_free(name); goto rec_err; } - }else{ + } else { size_t pubkeylen = key->pubkeylen_cmp[i]; - int ret = EVP_PKEY_get_raw_public_key(key->cmp_classical_pkey[i], key->comp_pubkey[i], &pubkeylen); - if (ret <= 0){ + int ret = EVP_PKEY_get_raw_public_key( + key->cmp_classical_pkey[i], key->comp_pubkey[i], + &pubkeylen); + if (ret <= 0) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); OPENSSL_free(name); goto rec_err; @@ -1067,29 +1061,28 @@ OQSX_KEY *oqsx_key_from_x509pubkey(const X509_PUBKEY *xpk, OSSL_LIB_CTX *libctx, unsigned char *concat_key; int count, aux, i, buflen; - if (!xpk || (!X509_PUBKEY_get0_param(NULL, &p, &plen, &palg, xpk))) - { + if (!xpk || (!X509_PUBKEY_get0_param(NULL, &p, &plen, &palg, xpk))) { return NULL; } - if (get_keytype(OBJ_obj2nid(palg->algorithm)) == KEY_TYPE_CMP_SIG){ + if (get_keytype(OBJ_obj2nid(palg->algorithm)) == KEY_TYPE_CMP_SIG) { sk = d2i_ASN1_SEQUENCE_ANY(NULL, &p, plen); - if (sk == NULL){ + if (sk == NULL) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); return NULL; - }else{ + } else { count = sk_ASN1_TYPE_num(sk); concat_key = OPENSSL_secure_malloc(plen); aux = 0; - for (i = 0; i < count; i++){ - aType = sk_ASN1_TYPE_pop(sk); + for (i = 0; i < count; i++) { + aType = sk_ASN1_TYPE_pop(sk); buf = aType->value.sequence->data; - buflen = aType->value.sequence->length; + buflen = aType->value.sequence->length; aux += buflen; memcpy(concat_key + plen - aux, buf, buflen); } - p = OPENSSL_memdup (concat_key + plen - aux, aux); + p = OPENSSL_memdup(concat_key + plen - aux, aux); plen = aux; OPENSSL_free(concat_key); } @@ -1117,46 +1110,46 @@ OQSX_KEY *oqsx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf)) return 0; - if (get_keytype(OBJ_obj2nid(palg->algorithm)) != KEY_TYPE_CMP_SIG){ + if (get_keytype(OBJ_obj2nid(palg->algorithm)) != KEY_TYPE_CMP_SIG) { oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen); - if (oct == NULL) - { + if (oct == NULL) { p = NULL; plen = 0; - } - else - { + } else { p = ASN1_STRING_get0_data(oct); plen = ASN1_STRING_length(oct); } - }else{ + } else { sk = d2i_ASN1_SEQUENCE_ANY(NULL, &p, plen); - if (sk == NULL){ + if (sk == NULL) { ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); return NULL; - }else{ + } else { count = sk_ASN1_TYPE_num(sk); concat_key = OPENSSL_secure_malloc(plen); aux = 0; - for (i = 0; i < count; i++){ - aType = sk_ASN1_TYPE_pop(sk); + for (i = 0; i < count; i++) { + aType = sk_ASN1_TYPE_pop(sk); char *name; - if ((name = get_cmpname(OBJ_obj2nid(palg->algorithm), count - 1 - i)) == NULL){ + if ((name + = get_cmpname(OBJ_obj2nid(palg->algorithm), count - 1 - i)) + == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); return NULL; } buf = aType->value.sequence->data; - buflen = aType->value.sequence->length; + buflen = aType->value.sequence->length; aux += buflen; memcpy(concat_key + plen - aux, buf, buflen); - //if is a RSA key the actual encoding size might be different from max size - //we calculate that difference for to facilitate the key reconstruction - if(!strncmp(name, "rsa", 3) || !strncmp(name, "pss", 3)) { - if (name[3] == '3') //3072 - rsa_diff = nids_sig[5].length_private_key - buflen; - else //2048 + // if is a RSA key the actual encoding size might be different + // from max size we calculate that difference for to facilitate + // the key reconstruction + if (!strncmp(name, "rsa", 3) || !strncmp(name, "pss", 3)) { + if (name[3] == '3') // 3072 + rsa_diff = nids_sig[5].length_private_key - buflen; + else // 2048 rsa_diff = nids_sig[6].length_private_key - buflen; } OPENSSL_free(name); @@ -1166,14 +1159,12 @@ OQSX_KEY *oqsx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, plen = aux; } } - if (rsa_diff > 4){//diff is too big, this means an decoding error + if (rsa_diff > 4) { // diff is too big, this means an decoding error ASN1_OCTET_STRING_free(oct); return NULL; } - - oqsx = oqsx_key_op(palg, p, plen + rsa_diff, KEY_OP_PRIVATE, - libctx, propq); + oqsx = oqsx_key_op(palg, p, plen + rsa_diff, KEY_OP_PRIVATE, libctx, propq); ASN1_OCTET_STRING_free(oct); return oqsx; } @@ -1204,14 +1195,12 @@ OQSX_KEY *oqsx_key_new(OSSL_LIB_CTX *libctx, char *oqs_name, char *tls_name, } #endif - if (oqs_name == NULL) - { + if (oqs_name == NULL) { OQS_KEY_PRINTF("OQSX_KEY: Fatal error: No OQS key name provided:\n"); goto err; } - if (tls_name == NULL) - { + if (tls_name == NULL) { OQS_KEY_PRINTF("OQSX_KEY: Fatal error: No TLS key name provided:\n"); goto err; } @@ -1347,49 +1336,55 @@ OQSX_KEY *oqsx_key_new(OSSL_LIB_CTX *libctx, char *oqs_name, char *tls_name, ret->numkeys = get_qntcmp(OBJ_sn2nid(tls_name)); ret->privkeylen = 0; ret->pubkeylen = 0; - ret->oqsx_provider_ctx = OPENSSL_malloc(ret->numkeys * sizeof(OQSX_PROVIDER_CTX)); + ret->oqsx_provider_ctx + = OPENSSL_malloc(ret->numkeys * sizeof(OQSX_PROVIDER_CTX)); ret->privkeylen_cmp = OPENSSL_malloc(ret->numkeys * sizeof(void *)); ret->pubkeylen_cmp = OPENSSL_malloc(ret->numkeys * sizeof(void *)); ret->comp_privkey = OPENSSL_malloc(ret->numkeys * sizeof(void *)); ret->comp_pubkey = OPENSSL_malloc(ret->numkeys * sizeof(void *)); ret->cmp_classical_pkey = OPENSSL_malloc(ret->numkeys * sizeof(void *)); - for (i = 0; i < ret->numkeys; i++){ + for (i = 0; i < ret->numkeys; i++) { char *name; - if ((name = get_cmpname(OBJ_sn2nid(tls_name), i)) == NULL){ + if ((name = get_cmpname(OBJ_sn2nid(tls_name), i)) == NULL) { OPENSSL_free(name); ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING); goto err; } - if (get_oqsname_fromtls(name) != 0) - { - ret->oqsx_provider_ctx[i].oqsx_qs_ctx.sig = OQS_SIG_new(get_oqsname_fromtls(name)); - if (!ret->oqsx_provider_ctx[i].oqsx_qs_ctx.sig) - { - fprintf(stderr, "Could not create OQS signature algorithm %s. Enabled in liboqs?A\n", name); + if (get_oqsname_fromtls(name) != 0) { + ret->oqsx_provider_ctx[i].oqsx_qs_ctx.sig + = OQS_SIG_new(get_oqsname_fromtls(name)); + if (!ret->oqsx_provider_ctx[i].oqsx_qs_ctx.sig) { + fprintf( + stderr, + "Could not create OQS signature algorithm %s. Enabled in liboqs?A\n", + name); goto err; } - ret->privkeylen_cmp[i] = ret->oqsx_provider_ctx[i].oqsx_qs_ctx.sig->length_secret_key; - ret->pubkeylen_cmp[i] = ret->oqsx_provider_ctx[i].oqsx_qs_ctx.sig->length_public_key; - } - else - { + ret->privkeylen_cmp[i] + = ret->oqsx_provider_ctx[i] + .oqsx_qs_ctx.sig->length_secret_key; + ret->pubkeylen_cmp[i] = ret->oqsx_provider_ctx[i] + .oqsx_qs_ctx.sig->length_public_key; + } else { evp_ctx = OPENSSL_zalloc(sizeof(OQSX_EVP_CTX)); ON_ERR_GOTO(!evp_ctx, err); ret2 = oqsx_hybsig_init(bit_security, evp_ctx, name); ON_ERR_GOTO(ret2 <= 0 || !evp_ctx->ctx, err); ret->oqsx_provider_ctx[i].oqsx_evp_ctx = evp_ctx; - ret->privkeylen_cmp[i] = ret->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->length_private_key; - ret->pubkeylen_cmp[i] = ret->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->length_public_key; + ret->privkeylen_cmp[i] + = ret->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->length_private_key; + ret->pubkeylen_cmp[i] + = ret->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->length_public_key; } ret->privkeylen += ret->privkeylen_cmp[i]; - ret->pubkeylen += ret->pubkeylen_cmp[i]; + ret->pubkeylen += ret->pubkeylen_cmp[i]; OPENSSL_free(name); } ret->keytype = primitive; - - break; default: @@ -1403,8 +1398,7 @@ OQSX_KEY *oqsx_key_new(OSSL_LIB_CTX *libctx, char *oqs_name, char *tls_name, ret->tls_name = OPENSSL_strdup(tls_name); ret->bit_security = bit_security; - if (propq != NULL) - { + if (propq != NULL) { ret->propq = OPENSSL_strdup(propq); ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); if (ret->propq == NULL) @@ -1454,32 +1448,30 @@ void oqsx_key_free(OQSX_KEY *key) else if (key->keytype == KEY_TYPE_ECP_HYB_KEM || key->keytype == KEY_TYPE_ECX_HYB_KEM) { OQS_KEM_free(key->oqsx_provider_ctx[0].oqsx_qs_ctx.kem); - } - else if(key->keytype == KEY_TYPE_CMP_SIG){ + } else if (key->keytype == KEY_TYPE_CMP_SIG) { int i; - for (i = 0; i < key->numkeys; i ++){ + for (i = 0; i < key->numkeys; i++) { char *name = get_cmpname(OBJ_sn2nid(key->tls_name), i); if (get_oqsname_fromtls(name)) OQS_SIG_free(key->oqsx_provider_ctx[i].oqsx_qs_ctx.sig); - else{ + else { EVP_PKEY_free(key->classical_pkey); EVP_PKEY_CTX_free(key->oqsx_provider_ctx[i].oqsx_evp_ctx->ctx); EVP_PKEY_free(key->oqsx_provider_ctx[i].oqsx_evp_ctx->keyParam); OPENSSL_free(key->oqsx_provider_ctx[i].oqsx_evp_ctx); } OPENSSL_free(name); - } - }else{ + } + } else { OQS_SIG_free(key->oqsx_provider_ctx[0].oqsx_qs_ctx.sig); if (key->oqsx_provider_ctx[0].oqsx_evp_ctx) { EVP_PKEY_CTX_free(key->oqsx_provider_ctx[0].oqsx_evp_ctx->ctx); EVP_PKEY_free(key->oqsx_provider_ctx[0].oqsx_evp_ctx->keyParam); OPENSSL_free(key->oqsx_provider_ctx[0].oqsx_evp_ctx); - } + } } OPENSSL_free(key->tls_name); - - + #ifdef OQS_PROVIDER_NOATOMIC CRYPTO_THREAD_lock_free(key->lock); #endif @@ -1515,8 +1507,7 @@ int oqsx_key_allocate_keymaterial(OQSX_KEY *key, int include_private) if (key->keytype != KEY_TYPE_CMP_SIG) aux = SIZE_OF_UINT32; - if (!key->privkey && include_private) - { + if (!key->privkey && include_private) { key->privkey = OPENSSL_secure_zalloc(key->privkeylen + aux); ON_ERR_SET_GOTO(!key->privkey, ret, 1, err); } @@ -1583,16 +1574,17 @@ int oqsx_key_fromdata(OQSX_KEY *key, const OSSL_PARAM params[], } // OQS key always the last of the numkeys comp keys -static int oqsx_key_gen_oqs(OQSX_KEY *key, int gen_kem) { - if (gen_kem) - return OQS_KEM_keypair(key->oqsx_provider_ctx[0].oqsx_qs_ctx.kem, - key->comp_pubkey[key->numkeys-1], - key->comp_privkey[key->numkeys-1]); - else { - return OQS_SIG_keypair(key->oqsx_provider_ctx[0].oqsx_qs_ctx.sig, - key->comp_pubkey[key->numkeys-1], - key->comp_privkey[key->numkeys-1]); - } +static int oqsx_key_gen_oqs(OQSX_KEY *key, int gen_kem) +{ + if (gen_kem) + return OQS_KEM_keypair(key->oqsx_provider_ctx[0].oqsx_qs_ctx.kem, + key->comp_pubkey[key->numkeys - 1], + key->comp_privkey[key->numkeys - 1]); + else { + return OQS_SIG_keypair(key->oqsx_provider_ctx[0].oqsx_qs_ctx.sig, + key->comp_pubkey[key->numkeys - 1], + key->comp_privkey[key->numkeys - 1]); + } } /* Generate classic keys, store length in leading SIZE_OF_UINT32 bytes of @@ -1621,8 +1613,7 @@ static EVP_PKEY *oqsx_key_gen_evp_key(OQSX_EVP_CTX *ctx, unsigned char *pubkey, ret2 = EVP_PKEY_keygen_init(kgctx); ON_ERR_SET_GOTO(ret2 <= 0, ret, -1, errhyb); - if (ctx->evp_info->keytype == EVP_PKEY_RSA) - { + if (ctx->evp_info->keytype == EVP_PKEY_RSA) { if (ctx->evp_info->length_public_key > 270) ret2 = EVP_PKEY_CTX_set_rsa_keygen_bits(kgctx, 3072); else @@ -1632,23 +1623,25 @@ static EVP_PKEY *oqsx_key_gen_evp_key(OQSX_EVP_CTX *ctx, unsigned char *pubkey, ret2 = EVP_PKEY_keygen(kgctx, &pkey); ON_ERR_SET_GOTO(ret2 <= 0, ret, -2, errhyb); - - if (ctx->evp_info->raw_key_support) - { + if (ctx->evp_info->raw_key_support) { // TODO: If available, use preallocated memory - if (ctx->evp_info->nid != NID_ED25519 && ctx->evp_info->nid != NID_ED448){ + if (ctx->evp_info->nid != NID_ED25519 + && ctx->evp_info->nid != NID_ED448) { pubkeylen = EVP_PKEY_get1_encoded_public_key(pkey, &pubkey_encoded); - ON_ERR_SET_GOTO(pubkeylen != ctx->evp_info->length_public_key || !pubkey_encoded, ret, -3, errhyb); + ON_ERR_SET_GOTO(pubkeylen != ctx->evp_info->length_public_key + || !pubkey_encoded, + ret, -3, errhyb); memcpy(pubkey + aux, pubkey_encoded, pubkeylen); - }else{ + } else { pubkeylen = ctx->evp_info->length_public_key; ret2 = EVP_PKEY_get_raw_public_key(pkey, pubkey + aux, &pubkeylen); - ON_ERR_SET_GOTO(ret2 <= 0 || pubkeylen != ctx->evp_info->length_public_key, ret, -3, errhyb); + ON_ERR_SET_GOTO( + ret2 <= 0 || pubkeylen != ctx->evp_info->length_public_key, ret, + -3, errhyb); } privkeylen = ctx->evp_info->length_private_key; - ret2 = EVP_PKEY_get_raw_private_key(pkey, privkey + aux, - &privkeylen); + ret2 = EVP_PKEY_get_raw_private_key(pkey, privkey + aux, &privkeylen); ON_ERR_SET_GOTO(ret2 <= 0 || privkeylen != ctx->evp_info->length_private_key, ret, -4, errhyb); @@ -1656,7 +1649,9 @@ static EVP_PKEY *oqsx_key_gen_evp_key(OQSX_EVP_CTX *ctx, unsigned char *pubkey, unsigned char *pubkey_enc = pubkey + aux; const unsigned char *pubkey_enc2 = pubkey + aux; pubkeylen = i2d_PublicKey(pkey, &pubkey_enc); - ON_ERR_SET_GOTO(!pubkey_enc || pubkeylen > (int)ctx->evp_info->length_public_key, ret, -11, errhyb); + ON_ERR_SET_GOTO( + !pubkey_enc || pubkeylen > (int)ctx->evp_info->length_public_key, + ret, -11, errhyb); unsigned char *privkey_enc = privkey + aux; const unsigned char *privkey_enc2 = privkey + aux; privkeylen = i2d_PrivateKey(pkey, &privkey_enc); @@ -1669,11 +1664,13 @@ static EVP_PKEY *oqsx_key_gen_evp_key(OQSX_EVP_CTX *ctx, unsigned char *pubkey, ON_ERR_SET_GOTO(!ck2, ret, -14, errhyb); EVP_PKEY_free(ck2); } - if (encode){ + if (encode) { ENCODE_UINT32(pubkey, pubkeylen); ENCODE_UINT32(privkey, privkeylen); } - OQS_KEY_PRINTF3("OQSKM: Storing classical privkeylen: %ld & pubkeylen: %ld\n", privkeylen, pubkeylen); + OQS_KEY_PRINTF3( + "OQSKM: Storing classical privkeylen: %ld & pubkeylen: %ld\n", + privkeylen, pubkeylen); EVP_PKEY_CTX_free(kgctx); OPENSSL_free(pubkey_encoded); @@ -1715,42 +1712,36 @@ int oqsx_key_gen(OQSX_KEY *key) key->classical_pkey = pkey; ret = oqsx_key_gen_oqs(key, key->keytype != KEY_TYPE_HYB_SIG); - } else if (key->keytype == KEY_TYPE_CMP_SIG) - { + } else if (key->keytype == KEY_TYPE_CMP_SIG) { int i; ret = oqsx_key_set_composites(key); - for (i = 0; i < key->numkeys; i++){ + for (i = 0; i < key->numkeys; i++) { char *name; - if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) == NULL){ + if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) == NULL) { OPENSSL_free(name); ON_ERR_GOTO(ret, err); } - if (get_oqsname_fromtls(name) == 0) - { - pkey = oqsx_key_gen_evp_key(key->oqsx_provider_ctx[i].oqsx_evp_ctx, key->comp_pubkey[i], key->comp_privkey[i], 0); + if (get_oqsname_fromtls(name) == 0) { + pkey = oqsx_key_gen_evp_key( + key->oqsx_provider_ctx[i].oqsx_evp_ctx, key->comp_pubkey[i], + key->comp_privkey[i], 0); OPENSSL_free(name); ON_ERR_GOTO(pkey == NULL, err); key->cmp_classical_pkey[i] = pkey; - } - else - { - ret = OQS_SIG_keypair(key->oqsx_provider_ctx[i].oqsx_qs_ctx.sig, key->comp_pubkey[i], key->comp_privkey[i]); + } else { + ret = OQS_SIG_keypair(key->oqsx_provider_ctx[i].oqsx_qs_ctx.sig, + key->comp_pubkey[i], + key->comp_privkey[i]); OPENSSL_free(name); ON_ERR_GOTO(ret, err); - } + } } - - - } - else if (key->keytype == KEY_TYPE_SIG) - { - ret = !oqsx_key_set_composites(key); + } else if (key->keytype == KEY_TYPE_SIG) { + ret = !oqsx_key_set_composites(key); ON_ERR_GOTO(ret, err); - ret = oqsx_key_gen_oqs(key, 0); - } - else - { + ret = oqsx_key_gen_oqs(key, 0); + } else { ret = 1; } err: @@ -1773,27 +1764,31 @@ int oqsx_key_maxsize(OQSX_KEY *key) return key->oqsx_provider_ctx[0].oqsx_qs_ctx.kem->length_shared_secret; case KEY_TYPE_ECP_HYB_KEM: case KEY_TYPE_ECX_HYB_KEM: - return key->oqsx_provider_ctx[0].oqsx_evp_ctx->evp_info->kex_length_secret - + key->oqsx_provider_ctx[0].oqsx_qs_ctx.kem->length_shared_secret; + return key->oqsx_provider_ctx[0] + .oqsx_evp_ctx->evp_info->kex_length_secret + + key->oqsx_provider_ctx[0] + .oqsx_qs_ctx.kem->length_shared_secret; case KEY_TYPE_SIG: return key->oqsx_provider_ctx[0].oqsx_qs_ctx.sig->length_signature; case KEY_TYPE_HYB_SIG: return key->oqsx_provider_ctx[0].oqsx_qs_ctx.sig->length_signature - + key->oqsx_provider_ctx[0].oqsx_evp_ctx->evp_info->length_signature + + key->oqsx_provider_ctx[0] + .oqsx_evp_ctx->evp_info->length_signature + SIZE_OF_UINT32; - case KEY_TYPE_CMP_SIG: - { + case KEY_TYPE_CMP_SIG: { int aux = sizeof(CompositeSignature); int i; - for (i = 0; i < key->numkeys; i ++){ + for (i = 0; i < key->numkeys; i++) { char *name = get_cmpname(OBJ_sn2nid(key->tls_name), i); if (get_oqsname_fromtls(name) == 0) - aux += key->oqsx_provider_ctx[i].oqsx_evp_ctx->evp_info->length_signature; + aux += key->oqsx_provider_ctx[i] + .oqsx_evp_ctx->evp_info->length_signature; else - aux += key->oqsx_provider_ctx[i].oqsx_qs_ctx.sig->length_signature; - OPENSSL_free(name); + aux += key->oqsx_provider_ctx[i] + .oqsx_qs_ctx.sig->length_signature; + OPENSSL_free(name); } - + return aux; } default: