diff options
Diffstat (limited to 'crypto/openssl/providers/implementations/encode_decode/ml_kem_codecs.c')
| -rw-r--r-- | crypto/openssl/providers/implementations/encode_decode/ml_kem_codecs.c | 271 |
1 files changed, 201 insertions, 70 deletions
diff --git a/crypto/openssl/providers/implementations/encode_decode/ml_kem_codecs.c b/crypto/openssl/providers/implementations/encode_decode/ml_kem_codecs.c index fe0c8acc7e63..482775433b62 100644 --- a/crypto/openssl/providers/implementations/encode_decode/ml_kem_codecs.c +++ b/crypto/openssl/providers/implementations/encode_decode/ml_kem_codecs.c @@ -24,16 +24,38 @@ * Private key bytes: 1632 (0x0660) */ static const ML_COMMON_SPKI_FMT ml_kem_512_spkifmt = { - { 0x30, 0x82, 0x03, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, - 0x01, 0x65, 0x03, 0x04, 0x04, 0x01, 0x03, 0x82, 0x03, 0x21, 0x00, } + { + 0x30, + 0x82, + 0x03, + 0x32, + 0x30, + 0x0b, + 0x06, + 0x09, + 0x60, + 0x86, + 0x48, + 0x01, + 0x65, + 0x03, + 0x04, + 0x04, + 0x01, + 0x03, + 0x82, + 0x03, + 0x21, + 0x00, + } }; static const ML_COMMON_PKCS8_FMT ml_kem_512_p8fmt[NUM_PKCS8_FORMATS] = { - { "seed-priv", 0x06aa, 0, 0x308206a6, 0x0440, 6, 0x40, 0x04820660, 0x4a, 0x0660, 0, 0 }, - { "priv-only", 0x0664, 0, 0x04820660, 0, 0, 0, 0, 0x04, 0x0660, 0, 0 }, - { "oqskeypair", 0x0984, 0, 0x04820980, 0, 0, 0, 0, 0x04, 0x0660, 0x0664, 0x0320 }, - { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0 }, - { "bare-priv", 0x0660, 4, 0, 0, 0, 0, 0, 0, 0x0660, 0, 0 }, - { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0 }, + { "seed-priv", 0x06aa, 0, 0x308206a6, 0x0440, 6, 0x40, 0x04820660, 0x4a, 0x0660, 0, 0 }, + { "priv-only", 0x0664, 0, 0x04820660, 0, 0, 0, 0, 0x04, 0x0660, 0, 0 }, + { "oqskeypair", 0x0984, 0, 0x04820980, 0, 0, 0, 0, 0x04, 0x0660, 0x0664, 0x0320 }, + { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0 }, + { "bare-priv", 0x0660, 4, 0, 0, 0, 0, 0, 0, 0x0660, 0, 0 }, + { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0 }, }; /*- @@ -42,16 +64,103 @@ static const ML_COMMON_PKCS8_FMT ml_kem_512_p8fmt[NUM_PKCS8_FORMATS] = { * Private key bytes: 2400 (0x0960) */ static const ML_COMMON_SPKI_FMT ml_kem_768_spkifmt = { - { 0x30, 0x82, 0x04, 0xb2, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, - 0x01, 0x65, 0x03, 0x04, 0x04, 0x02, 0x03, 0x82, 0x04, 0xa1, 0x00, } + { + 0x30, + 0x82, + 0x04, + 0xb2, + 0x30, + 0x0b, + 0x06, + 0x09, + 0x60, + 0x86, + 0x48, + 0x01, + 0x65, + 0x03, + 0x04, + 0x04, + 0x02, + 0x03, + 0x82, + 0x04, + 0xa1, + 0x00, + } }; static const ML_COMMON_PKCS8_FMT ml_kem_768_p8fmt[NUM_PKCS8_FORMATS] = { - { "seed-priv", 0x09aa, 0, 0x308209a6, 0x0440, 6, 0x40, 0x04820960, 0x4a, 0x0960, 0, 0, }, - { "priv-only", 0x0964, 0, 0x04820960, 0, 0, 0, 0, 0x04, 0x0960, 0, 0, }, - { "oqskeypair", 0x0e04, 0, 0x04820e00, 0, 0, 0, 0, 0x04, 0x0960, 0x0964, 0x04a0 }, - { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0, }, - { "bare-priv", 0x0960, 4, 0, 0, 0, 0, 0, 0, 0x0960, 0, 0, }, - { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0, }, + { + "seed-priv", + 0x09aa, + 0, + 0x308209a6, + 0x0440, + 6, + 0x40, + 0x04820960, + 0x4a, + 0x0960, + 0, + 0, + }, + { + "priv-only", + 0x0964, + 0, + 0x04820960, + 0, + 0, + 0, + 0, + 0x04, + 0x0960, + 0, + 0, + }, + { "oqskeypair", 0x0e04, 0, 0x04820e00, 0, 0, 0, 0, 0x04, 0x0960, 0x0964, 0x04a0 }, + { + "seed-only", + 0x0042, + 2, + 0x8040, + 0, + 2, + 0x40, + 0, + 0, + 0, + 0, + 0, + }, + { + "bare-priv", + 0x0960, + 4, + 0, + 0, + 0, + 0, + 0, + 0, + 0x0960, + 0, + 0, + }, + { + "bare-seed", + 0x0040, + 4, + 0, + 0, + 0, + 0x40, + 0, + 0, + 0, + 0, + 0, + }, }; /*- @@ -60,29 +169,51 @@ static const ML_COMMON_PKCS8_FMT ml_kem_768_p8fmt[NUM_PKCS8_FORMATS] = { * Public key bytes: 1568 (0x0620) */ static const ML_COMMON_SPKI_FMT ml_kem_1024_spkifmt = { - { 0x30, 0x82, 0x06, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, - 0x01, 0x65, 0x03, 0x04, 0x04, 0x03, 0x03, 0x82, 0x06, 0x21, 0x00, } + { + 0x30, + 0x82, + 0x06, + 0x32, + 0x30, + 0x0b, + 0x06, + 0x09, + 0x60, + 0x86, + 0x48, + 0x01, + 0x65, + 0x03, + 0x04, + 0x04, + 0x03, + 0x03, + 0x82, + 0x06, + 0x21, + 0x00, + } }; static const ML_COMMON_PKCS8_FMT ml_kem_1024_p8fmt[NUM_PKCS8_FORMATS] = { - { "seed-priv", 0x0caa, 0, 0x30820ca6, 0x0440, 6, 0x40, 0x04820c60, 0x4a, 0x0c60, 0, 0 }, - { "priv-only", 0x0c64, 0, 0x04820c60, 0, 0, 0, 0, 0x04, 0x0c60, 0, 0 }, - { "oqskeypair", 0x1284, 0, 0x04821280, 0, 0, 0, 0, 0x04, 0x0c60, 0x0c64, 0x0620 }, - { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0 }, - { "bare-priv", 0x0c60, 4, 0, 0, 0, 0, 0, 0, 0x0c60, 0, 0 }, - { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0 }, + { "seed-priv", 0x0caa, 0, 0x30820ca6, 0x0440, 6, 0x40, 0x04820c60, 0x4a, 0x0c60, 0, 0 }, + { "priv-only", 0x0c64, 0, 0x04820c60, 0, 0, 0, 0, 0x04, 0x0c60, 0, 0 }, + { "oqskeypair", 0x1284, 0, 0x04821280, 0, 0, 0, 0, 0x04, 0x0c60, 0x0c64, 0x0620 }, + { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0 }, + { "bare-priv", 0x0c60, 4, 0, 0, 0, 0, 0, 0, 0x0c60, 0, 0 }, + { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0 }, }; /* Indices of slots in the `codecs` table below */ -#define ML_KEM_512_CODEC 0 -#define ML_KEM_768_CODEC 1 -#define ML_KEM_1024_CODEC 2 +#define ML_KEM_512_CODEC 0 +#define ML_KEM_768_CODEC 1 +#define ML_KEM_1024_CODEC 2 /* * Per-variant fixed parameters */ static const ML_COMMON_CODEC codecs[3] = { - { &ml_kem_512_spkifmt, ml_kem_512_p8fmt }, - { &ml_kem_768_spkifmt, ml_kem_768_p8fmt }, + { &ml_kem_512_spkifmt, ml_kem_512_p8fmt }, + { &ml_kem_768_spkifmt, ml_kem_768_p8fmt }, { &ml_kem_1024_spkifmt, ml_kem_1024_p8fmt } }; @@ -102,7 +233,7 @@ static const ML_COMMON_CODEC *ml_kem_get_codec(int evp_type) ML_KEM_KEY * ossl_ml_kem_d2i_PUBKEY(const uint8_t *pubenc, int publen, int evp_type, - PROV_CTX *provctx, const char *propq) + PROV_CTX *provctx, const char *propq) { OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); const ML_KEM_VINFO *v; @@ -114,7 +245,7 @@ ossl_ml_kem_d2i_PUBKEY(const uint8_t *pubenc, int publen, int evp_type, || (codec = ml_kem_get_codec(evp_type)) == NULL) return NULL; vspki = codec->spkifmt; - if (publen != ML_COMMON_SPKI_OVERHEAD + (ossl_ssize_t) v->pubkey_bytes + if (publen != ML_COMMON_SPKI_OVERHEAD + (ossl_ssize_t)v->pubkey_bytes || memcmp(pubenc, vspki->asn1_prefix, ML_COMMON_SPKI_OVERHEAD) != 0) return NULL; publen -= ML_COMMON_SPKI_OVERHEAD; @@ -123,10 +254,10 @@ ossl_ml_kem_d2i_PUBKEY(const uint8_t *pubenc, int publen, int evp_type, if ((ret = ossl_ml_kem_key_new(libctx, propq, evp_type)) == NULL) return NULL; - if (!ossl_ml_kem_parse_public_key(pubenc, (size_t) publen, ret)) { + if (!ossl_ml_kem_parse_public_key(pubenc, (size_t)publen, ret)) { ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING, - "errror parsing %s public key from input SPKI", - v->algorithm_name); + "error parsing %s public key from input SPKI", + v->algorithm_name); ossl_ml_kem_key_free(ret); return NULL; } @@ -136,8 +267,8 @@ ossl_ml_kem_d2i_PUBKEY(const uint8_t *pubenc, int publen, int evp_type, ML_KEM_KEY * ossl_ml_kem_d2i_PKCS8(const uint8_t *prvenc, int prvlen, - int evp_type, PROV_CTX *provctx, - const char *propq) + int evp_type, PROV_CTX *provctx, + const char *propq) { const ML_KEM_VINFO *v; const ML_COMMON_CODEC *codec; @@ -171,7 +302,7 @@ ossl_ml_kem_d2i_PKCS8(const uint8_t *prvenc, int prvlen, formats = ossl_prov_ctx_get_param( provctx, OSSL_PKEY_PARAM_ML_KEM_INPUT_FORMATS, NULL); fmt_slots = ossl_ml_common_pkcs8_fmt_order(v->algorithm_name, codec->p8fmt, - "input", formats); + "input", formats); if (fmt_slots == NULL) goto end; @@ -179,8 +310,8 @@ ossl_ml_kem_d2i_PKCS8(const uint8_t *prvenc, int prvlen, X509_ALGOR_get0(NULL, &ptype, NULL, alg); if (ptype != V_ASN1_UNDEF) { ERR_raise_data(ERR_LIB_PROV, PROV_R_UNEXPECTED_KEY_PARAMETERS, - "unexpected parameters with a PKCS#8 %s private key", - v->algorithm_name); + "unexpected parameters with a PKCS#8 %s private key", + v->algorithm_name); goto end; } if ((ossl_ssize_t)len < (ossl_ssize_t)sizeof(magic)) @@ -202,8 +333,8 @@ ossl_ml_kem_d2i_PKCS8(const uint8_t *prvenc, int prvlen, || (p8fmt->priv_length > 0 && p8fmt->priv_length != v->prvkey_bytes) || (p8fmt->pub_length > 0 && p8fmt->pub_length != v->pubkey_bytes)) { ERR_raise_data(ERR_LIB_PROV, PROV_R_ML_KEM_NO_FORMAT, - "no matching enabled %s private key input formats", - v->algorithm_name); + "no matching enabled %s private key input formats", + v->algorithm_name); goto end; } @@ -246,18 +377,18 @@ ossl_ml_kem_d2i_PKCS8(const uint8_t *prvenc, int prvlen, if (p8fmt->seed_length > 0) { if (!ossl_ml_kem_set_seed(buf + p8fmt->seed_offset, - ML_KEM_SEED_BYTES, key)) { + ML_KEM_SEED_BYTES, key)) { ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR, - "error storing %s private key seed", - v->algorithm_name); + "error storing %s private key seed", + v->algorithm_name); goto end; } } if (p8fmt->priv_length > 0) { if ((key->encoded_dk = OPENSSL_malloc(p8fmt->priv_length)) == NULL) { ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, - "error parsing %s private key", - v->algorithm_name); + "error parsing %s private key", + v->algorithm_name); goto end; } memcpy(key->encoded_dk, buf + p8fmt->priv_offset, p8fmt->priv_length); @@ -265,7 +396,7 @@ ossl_ml_kem_d2i_PKCS8(const uint8_t *prvenc, int prvlen, /* Any OQS public key content is ignored */ ret = key; - end: +end: OPENSSL_free(fmt_slots); PKCS8_PRIV_KEY_INFO_free(p8inf); if (ret == NULL) @@ -280,8 +411,8 @@ int ossl_ml_kem_i2d_pubkey(const ML_KEM_KEY *key, unsigned char **out) if (!ossl_ml_kem_have_pubkey(key)) { ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY, - "no %s public key data available", - key->vinfo->algorithm_name); + "no %s public key data available", + key->vinfo->algorithm_name); return 0; } publen = key->vinfo->pubkey_bytes; @@ -291,8 +422,8 @@ int ossl_ml_kem_i2d_pubkey(const ML_KEM_KEY *key, unsigned char **out) return 0; if (!ossl_ml_kem_encode_public_key(*out, publen, key)) { ERR_raise_data(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR, - "error encoding %s public key", - key->vinfo->algorithm_name); + "error encoding %s public key", + key->vinfo->algorithm_name); OPENSSL_free(*out); return 0; } @@ -302,7 +433,7 @@ int ossl_ml_kem_i2d_pubkey(const ML_KEM_KEY *key, unsigned char **out) /* Allocate and encode PKCS#8 private key payload. */ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, - PROV_CTX *provctx) + PROV_CTX *provctx) { const ML_KEM_VINFO *v = key->vinfo; const ML_COMMON_CODEC *codec; @@ -319,15 +450,15 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, if (!ossl_ml_kem_have_prvkey(key)) { ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY, - "no %s private key data available", - key->vinfo->algorithm_name); + "no %s private key data available", + key->vinfo->algorithm_name); return 0; } formats = ossl_prov_ctx_get_param( provctx, OSSL_PKEY_PARAM_ML_KEM_OUTPUT_FORMATS, NULL); fmt_slots = ossl_ml_common_pkcs8_fmt_order(v->algorithm_name, codec->p8fmt, - "output", formats); + "output", formats); if (fmt_slots == NULL) return 0; @@ -341,8 +472,8 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, || (p8fmt->priv_length > 0 && p8fmt->priv_length != v->prvkey_bytes) || (p8fmt->pub_length > 0 && p8fmt->pub_length != v->pubkey_bytes)) { ERR_raise_data(ERR_LIB_PROV, PROV_R_ML_KEM_NO_FORMAT, - "no matching enabled %s private key output formats", - v->algorithm_name); + "no matching enabled %s private key output formats", + v->algorithm_name); goto end; } len = p8fmt->p8_bytes; @@ -352,7 +483,7 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, goto end; } - if ((pos = buf = OPENSSL_malloc((size_t) len)) == NULL) + if ((pos = buf = OPENSSL_malloc((size_t)len)) == NULL) goto end; switch (p8fmt->p8_shift) { @@ -366,8 +497,8 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, break; default: ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, - "error encoding %s private key", - v->algorithm_name); + "error encoding %s private key", + v->algorithm_name); goto end; } @@ -381,8 +512,8 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, if (pos != buf + p8fmt->seed_offset || !ossl_ml_kem_encode_seed(pos, ML_KEM_SEED_BYTES, key)) { ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, - "error encoding %s private key", - v->algorithm_name); + "error encoding %s private key", + v->algorithm_name); goto end; } pos += ML_KEM_SEED_BYTES; @@ -393,8 +524,8 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, if (pos != buf + p8fmt->priv_offset || !ossl_ml_kem_encode_private_key(pos, v->prvkey_bytes, key)) { ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, - "error encoding %s private key", - v->algorithm_name); + "error encoding %s private key", + v->algorithm_name); goto end; } pos += v->prvkey_bytes; @@ -405,8 +536,8 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, if (pos != buf + p8fmt->pub_offset || !ossl_ml_kem_encode_public_key(pos, v->pubkey_bytes, key)) { ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, - "error encoding %s private key", - v->algorithm_name); + "error encoding %s private key", + v->algorithm_name); goto end; } pos += v->pubkey_bytes; @@ -417,7 +548,7 @@ int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out, ret = len; } - end: +end: OPENSSL_free(fmt_slots); if (ret == 0) OPENSSL_free(buf); @@ -478,10 +609,10 @@ int ossl_ml_kem_key_to_text(BIO *out, const ML_KEM_KEY *key, int selection) /* If we got here, and ret == 0, there was no key material */ if (ret == 0) ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY, - "no %s key material available", - type_label); + "no %s key material available", + type_label); - end: +end: OPENSSL_free(pubenc); OPENSSL_free(prvenc); return ret; |
