/*********************************************************************************************************************************** Crypto Common ***********************************************************************************************************************************/ #include #include #include #include #include #include #include #include "common/crypto/common.h" #include "common/crypto/common.intern.h" #include "common/debug.h" #include "common/log.h" /*********************************************************************************************************************************** Flag to indicate if OpenSSL has already been initialized ***********************************************************************************************************************************/ static bool cryptoInitDone = false; /**********************************************************************************************************************************/ FN_EXTERN void cryptoError(const bool error, const char *const description) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(BOOL, error); FUNCTION_TEST_PARAM(STRINGZ, description); FUNCTION_TEST_END(); if (error) { const unsigned long code = ERR_get_error(); // Clear errors remaining on the queue ERR_clear_error(); cryptoErrorCode(code, description); } FUNCTION_TEST_RETURN_VOID(); } FN_EXTERN void cryptoErrorCode(const unsigned long code, const char *const description) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(UINT64, code); FUNCTION_TEST_PARAM(STRINGZ, description); FUNCTION_TEST_END(); const char *errorMessage = ERR_reason_error_string(code); #ifdef ERR_SYSTEM_ERROR // ERR_reason_error_string() returns NULL for system errors in OpenSSL >= 3.0. The code for a system error contains the errno to // get the message from strerror(). if (ERR_SYSTEM_ERROR(code)) errorMessage = strerror(ERR_GET_REASON(code)); #endif THROW_FMT(CryptoError, "%s: [%lu] %s", description, code, errorMessage == NULL ? "no details available" : errorMessage); FUNCTION_TEST_NO_RETURN(); } /**********************************************************************************************************************************/ FN_EXTERN void cryptoInit(void) { FUNCTION_LOG_VOID(logLevelTrace); if (!cryptoInitDone) { // Load crypto strings and algorithms OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL); // Initialization OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL); // Clear errors queued while loading the OpenSSL config ERR_clear_error(); // Mark crypto as initialized cryptoInitDone = true; } FUNCTION_LOG_RETURN_VOID(); } /**********************************************************************************************************************************/ FN_EXTERN void cryptoRandomBytes(uint8_t *const buffer, const size_t size) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM_P(BYTEDATA, buffer); FUNCTION_LOG_PARAM(SIZE, size); FUNCTION_LOG_END(); ASSERT(buffer != NULL); ASSERT(size > 0); // A failure must not go unnoticed since the caller would be left with bytes that are not random cryptoError(RAND_bytes(buffer, (int)size) != 1, "unable to generate random bytes"); FUNCTION_LOG_RETURN_VOID(); } /**********************************************************************************************************************************/ FN_EXTERN const EVP_MD * cryptoDigest(const HashType type) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(STRING_ID, type); FUNCTION_TEST_END(); char typeZ[STRID_MAX + 1]; strIdToZ(type, typeZ); const EVP_MD *const result = EVP_get_digestbyname(typeZ); if (result == NULL) THROW_FMT(AssertError, "unable to load digest '%s'", typeZ); FUNCTION_TEST_RETURN_TYPE_CONST_P(EVP_MD, result); }