#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
#include <openssl/crypto.h>
#include <openssl/service_indicator.h>
#include "internal.h"
#include "../pqdsa/internal.h"
const char *awslc_version_string(void) { return AWSLC_VERSION_STRING; }
int is_fips_build(void) {
#if defined(AWSLC_FIPS)
return 1;
#else
return 0;
#endif
}
#if defined(AWSLC_FIPS)
#define STATE_UNLOCKED 0
#define TLS_MD_EXTENDED_MASTER_SECRET_CONST "extended master secret"
#define TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE 22
struct fips_service_indicator_state {
uint64_t lock_state;
uint64_t counter;
};
static struct fips_service_indicator_state *service_indicator_get(void) {
struct fips_service_indicator_state *indicator =
CRYPTO_get_thread_local(AWSLC_THREAD_LOCAL_FIPS_SERVICE_INDICATOR_STATE);
if (indicator == NULL) {
indicator = malloc(sizeof(struct fips_service_indicator_state));
if (indicator == NULL) {
return NULL;
}
indicator->lock_state = STATE_UNLOCKED;
indicator->counter = 0;
if (!CRYPTO_set_thread_local(
AWSLC_THREAD_LOCAL_FIPS_SERVICE_INDICATOR_STATE, indicator, free)) {
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR);
return NULL;
}
}
return indicator;
}
static uint64_t service_indicator_get_counter(void) {
struct fips_service_indicator_state *indicator = service_indicator_get();
if (indicator == NULL) {
return 0;
}
return indicator->counter;
}
uint64_t FIPS_service_indicator_before_call(void) {
return service_indicator_get_counter();
}
uint64_t FIPS_service_indicator_after_call(void) {
return service_indicator_get_counter();
}
void FIPS_service_indicator_update_state(void) {
struct fips_service_indicator_state *indicator = service_indicator_get();
if (indicator && indicator->lock_state == STATE_UNLOCKED) {
indicator->counter++;
}
}
void FIPS_service_indicator_lock_state(void) {
struct fips_service_indicator_state *indicator = service_indicator_get();
if (indicator == NULL) {
return;
}
const uint64_t new_state = indicator->lock_state + 1;
if (new_state < indicator->lock_state) {
abort();
}
indicator->lock_state = new_state;
}
void FIPS_service_indicator_unlock_state(void) {
struct fips_service_indicator_state *indicator = service_indicator_get();
if (indicator == NULL) {
return;
}
if (indicator->lock_state == 0) {
abort();
}
indicator->lock_state--;
}
void AEAD_GCM_verify_service_indicator(const EVP_AEAD_CTX *ctx) {
const size_t key_len = EVP_AEAD_key_length(ctx->aead);
if (key_len == 16 || key_len == 32) {
FIPS_service_indicator_update_state();
}
}
void AEAD_CCM_verify_service_indicator(const EVP_AEAD_CTX *ctx) {
if (EVP_AEAD_key_length(ctx->aead) == 16 && ctx->tag_len == 4) {
FIPS_service_indicator_update_state();
}
}
void AES_CMAC_verify_service_indicator(const CMAC_CTX *ctx) {
const size_t key_len = ctx->cipher_ctx.key_len;
if (key_len == 16 || key_len == 32) {
FIPS_service_indicator_update_state();
}
}
static int is_ec_fips_approved(int curve_nid) {
switch (curve_nid) {
case NID_secp224r1:
case NID_X9_62_prime256v1:
case NID_secp384r1:
case NID_secp521r1:
return 1;
default:
return 0;
}
}
static int is_md_fips_approved_for_signing(int md_type, int pkey_type) {
switch (md_type) {
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
case NID_sha3_224:
case NID_sha3_256:
case NID_sha3_384:
case NID_sha3_512:
return 1;
default:
return 0;
}
}
static int is_md_fips_approved_for_verifying(int md_type, int pkey_type) {
switch (md_type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
case NID_sha3_224:
case NID_sha3_256:
case NID_sha3_384:
case NID_sha3_512:
return 1;
default:
return 0;
}
}
static void mldsa_verify_service_indicator(const EVP_PKEY *pkey) {
if (pkey->type != EVP_PKEY_PQDSA) {
return;
}
const PQDSA *pqdsa = PQDSA_KEY_get0_dsa(pkey->pkey.pqdsa_key);
if (pqdsa == NULL) {
return;
}
switch (pqdsa->nid) {
case NID_MLDSA44:
case NID_MLDSA65:
case NID_MLDSA87:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
static int custom_meth_invoked(const EVP_PKEY_CTX *ctx) {
const int pkey_type = EVP_PKEY_id(ctx->pkey);
switch (pkey_type) {
case EVP_PKEY_RSA:
case EVP_PKEY_RSA_PSS: {
const RSA_METHOD *meth = ctx->pkey->pkey.rsa->meth;
if (ctx->operation == EVP_PKEY_OP_VERIFY) {
return meth->verify_raw ? 1 : 0;
}
if(ctx->operation == EVP_PKEY_OP_SIGN) {
return (meth->sign || meth->sign_raw) ? 1 : 0;
}
return 0; }
case EVP_PKEY_EC: {
if (ctx->operation == EVP_PKEY_OP_SIGN) {
const EC_KEY_METHOD *meth = ctx->pkey->pkey.ec->eckey_method;
return (meth->sign || meth->sign_sig) ? 1 : 0;
}
return 0;
}
default:
return 0;
}
}
static void evp_md_ctx_verify_service_indicator(const EVP_MD_CTX *ctx,
int rsa_1024_ok,
int (*md_ok)(int md_type,
int pkey_type)) {
if (EVP_MD_CTX_md(ctx) == NULL) {
if(ctx->pctx->pkey->type == EVP_PKEY_ED25519) {
FIPS_service_indicator_update_state();
return;
}
if(ctx->pctx->pkey->type == EVP_PKEY_PQDSA) {
mldsa_verify_service_indicator(ctx->pctx->pkey);
return;
}
goto err;
}
EVP_PKEY_CTX *const pctx = ctx->pctx;
const EVP_PKEY *const pkey = EVP_PKEY_CTX_get0_pkey(pctx);
const int pkey_type = EVP_PKEY_id(pkey);
const int md_type = EVP_MD_CTX_type(ctx);
if (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_RSA_PSS) {
const EVP_MD *pctx_md;
if (!EVP_PKEY_CTX_get_signature_md(pctx, &pctx_md)) {
goto err;
}
if (EVP_MD_type(pctx_md) != md_type) {
goto err;
}
int padding;
if (!EVP_PKEY_CTX_get_rsa_padding(pctx, &padding)) {
goto err;
}
if (padding == RSA_PKCS1_PSS_PADDING) {
int salt_len;
const EVP_MD *mgf1_md;
if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pctx, &salt_len) ||
!EVP_PKEY_CTX_get_rsa_mgf1_md(pctx, &mgf1_md) ||
(salt_len != -1 && salt_len != (int)EVP_MD_size(pctx_md)) ||
EVP_MD_type(mgf1_md) != md_type) {
goto err;
}
}
size_t n_bits = RSA_bits(ctx->pctx->pkey->pkey.rsa);
if (md_ok(md_type, pkey_type) &&
((rsa_1024_ok && n_bits == 1024) || (n_bits >= 2048 && n_bits % 2 == 0))
&& !custom_meth_invoked(pctx)) {
FIPS_service_indicator_update_state();
}
} else if (pkey_type == EVP_PKEY_EC) {
int curve_nid = EC_GROUP_get_curve_name(pkey->pkey.ec->group);
if (md_ok(md_type, pkey_type) && is_ec_fips_approved(curve_nid) &&
!custom_meth_invoked(pctx)) {
FIPS_service_indicator_update_state();
}
}
err:
ERR_clear_error();
}
void EC_KEY_keygen_verify_service_indicator(const EC_KEY *eckey) {
if (is_ec_fips_approved(EC_GROUP_get_curve_name(eckey->group))) {
FIPS_service_indicator_update_state();
}
}
void ECDH_verify_service_indicator(const EC_KEY *ec_key) {
if (is_ec_fips_approved(EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)))) {
FIPS_service_indicator_update_state();
}
}
void EVP_PKEY_keygen_verify_service_indicator(const EVP_PKEY *pkey) {
if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA_PSS) {
size_t n_bits = RSA_bits(pkey->pkey.rsa);
if (n_bits >= 2048 && n_bits % 128 == 0) {
FIPS_service_indicator_update_state();
}
} else if (pkey->type == EVP_PKEY_EC) {
int curve_nid = EC_GROUP_get_curve_name(pkey->pkey.ec->group);
if (is_ec_fips_approved(curve_nid)) {
FIPS_service_indicator_update_state();
}
} else if (pkey->type == EVP_PKEY_KEM) {
const KEM *kem = KEM_KEY_get0_kem(pkey->pkey.kem_key);
switch (kem->nid) {
case NID_MLKEM512:
case NID_MLKEM768:
case NID_MLKEM1024:
FIPS_service_indicator_update_state();
break;
default:
break;
}
} else if (pkey->type == EVP_PKEY_ED25519) {
FIPS_service_indicator_update_state();
} else if (pkey->type == EVP_PKEY_PQDSA) {
mldsa_verify_service_indicator(pkey);
}
}
void EVP_Cipher_verify_service_indicator(const EVP_CIPHER_CTX *ctx) {
switch (EVP_CIPHER_CTX_nid(ctx)) {
case NID_aes_128_ecb:
case NID_aes_192_ecb:
case NID_aes_256_ecb:
case NID_aes_128_cbc:
case NID_aes_192_cbc:
case NID_aes_256_cbc:
case NID_aes_128_ctr:
case NID_aes_192_ctr:
case NID_aes_256_ctr:
case NID_aes_256_xts:
FIPS_service_indicator_update_state();
}
}
void EVP_DigestVerify_verify_service_indicator(const EVP_MD_CTX *ctx) {
evp_md_ctx_verify_service_indicator(ctx, 1,
is_md_fips_approved_for_verifying);
}
void EVP_DigestSign_verify_service_indicator(const EVP_MD_CTX *ctx) {
evp_md_ctx_verify_service_indicator(ctx, 0,
is_md_fips_approved_for_signing);
}
void HMAC_verify_service_indicator(const EVP_MD *evp_md) {
switch (evp_md->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
case NID_sha3_224:
case NID_sha3_256:
case NID_sha3_384:
case NID_sha3_512:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
void HKDF_verify_service_indicator(const EVP_MD *evp_md, const uint8_t *salt,
size_t salt_len, size_t info_len) {
if ((salt != NULL && salt_len == 0) || (info_len == 0)) {
return;
}
switch (evp_md->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
void HKDFExpand_verify_service_indicator(const EVP_MD *evp_md) {
switch (evp_md->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
void PBKDF2_verify_service_indicator(const EVP_MD *evp_md, size_t password_len,
size_t salt_len, unsigned iterations) {
switch (evp_md->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
if (password_len >= 14 && salt_len >= 16 && iterations >= 1000) {
FIPS_service_indicator_update_state();
}
break;
default:
break;
}
}
void SSHKDF_verify_service_indicator(const EVP_MD *evp_md) {
switch (evp_md->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
void TLSKDF_verify_service_indicator(const EVP_MD *dgst, const char *label,
size_t label_len) {
if (dgst->type == NID_md5_sha1) {
FIPS_service_indicator_update_state();
return;
}
switch (dgst->type) {
case NID_sha256:
case NID_sha384:
case NID_sha512:
if (label_len >= TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE &&
memcmp(label, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0) {
FIPS_service_indicator_update_state();
}
break;
default:
break;
}
}
void SSKDF_digest_verify_service_indicator(const EVP_MD *dgst) {
switch (dgst->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
case NID_sha3_224:
case NID_sha3_256:
case NID_sha3_384:
case NID_sha3_512:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
void SSKDF_hmac_verify_service_indicator(const EVP_MD *dgst) {
switch (dgst->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
void KBKDF_ctr_hmac_verify_service_indicator(const EVP_MD *dgst, size_t secret_len) {
switch (dgst->type) {
case NID_sha1:
case NID_sha224:
case NID_sha256:
case NID_sha384:
case NID_sha512:
case NID_sha512_224:
case NID_sha512_256:
if (secret_len >= 14) {
FIPS_service_indicator_update_state();
}
break;
default:
break;
}
}
void EVP_PKEY_encapsulate_verify_service_indicator(const EVP_PKEY_CTX* ctx) {
if (ctx->pkey->type == EVP_PKEY_KEM) {
const KEM *kem = KEM_KEY_get0_kem(ctx->pkey->pkey.kem_key);
switch (kem->nid) {
case NID_MLKEM512:
case NID_MLKEM768:
case NID_MLKEM1024:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
}
void EVP_PKEY_decapsulate_verify_service_indicator(const EVP_PKEY_CTX* ctx) {
if (ctx->pkey->type == EVP_PKEY_KEM) {
const KEM *kem = KEM_KEY_get0_kem(ctx->pkey->pkey.kem_key);
switch (kem->nid) {
case NID_MLKEM512:
case NID_MLKEM768:
case NID_MLKEM1024:
FIPS_service_indicator_update_state();
break;
default:
break;
}
}
}
#else
uint64_t FIPS_service_indicator_before_call(void) { return 0; }
uint64_t FIPS_service_indicator_after_call(void) {
return 1;
}
#endif