src/utils/crypto_utils/src/crypto_lib.c
bool VerifyRS256Signature(
    const uint8_t* signature,
    const size_t sigLength,
    const uint8_t* blob,
    const size_t blobLength,
    CryptoKeyHandle keyToSign)
{
    bool success = false;
    EVP_MD_CTX* mdctx = NULL;
    EVP_PKEY_CTX* ctx = NULL;

    const size_t digest_len = 32;
    uint8_t digest[digest_len];

    if ((mdctx = EVP_MD_CTX_new()) == NULL)
    {
        goto done;
    }

    const EVP_MD* hash_alg = EVP_sha256();
    if (EVP_DigestInit_ex(mdctx, hash_alg, NULL) != 1)
    {
        goto done;
    }

    if (EVP_DigestUpdate(mdctx, blob, blobLength) != 1)
    {
        goto done;
    }

    unsigned int digest_len_temp = (unsigned int)digest_len;
    if (EVP_DigestFinal_ex(mdctx, digest, &digest_len_temp) != 1)
    {
        goto done;
    }

    EVP_PKEY* pu_key = CryptoKeyHandleToEVP_PKEY(keyToSign);
    ctx = EVP_PKEY_CTX_new(pu_key, NULL);

    if (ctx == NULL)
    {
        goto done;
    }

    if (EVP_PKEY_verify_init(ctx) <= 0)
    {
        goto done;
    }

    if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
    {
        goto done;
    }

    if (EVP_PKEY_CTX_set_signature_md(ctx, hash_alg) <= 0)
    {
        goto done;
    }

    if (EVP_PKEY_verify(ctx, signature, sigLength, digest, digest_len) == 1)
    {
        success = true;
    }

done:

    if (mdctx != NULL)
    {
        EVP_MD_CTX_free(mdctx);
    }

    if (ctx != NULL)
    {
        EVP_PKEY_CTX_free(ctx);
    }

    return success;
}
/**
 * @brief Verifies the @p signature using RS256 on the @p blob and the @p key.
 * @details This RS256 implementation uses the RSA_PKCS1_PADDING type.
 *
 * @param signature the expected signature to compare against the one computed from @p blob using RS256 and the @p key
 * @param sigLength the total length of the signature
 * @param blob the data for which the RS256 encoded hash will be computed from
 * @param blobLength the size of buffer @p blob
 * @param keyToSign the public key for the RS256 validation of the expected signature against blob
 * @returns True if @p signature equals the one computer from the blob and key using RS256, False otherwise
 */
@brief Verifies the @p signature using RS256 on the @p blob and the @p key.
@details This RS256 implementation uses the RSA_PKCS1_PADDING type.


@brief Verifies the @p signature using RS256 on the @p blob and the @p key . @details This RS256 implementation uses the RSA_PKCS1_PADDING type .
bool VerifyRS256Signature(
    const uint8_t* signature,
    const size_t sigLength,
    const uint8_t* blob,
    const size_t blobLength,
    CryptoKeyHandle keyToSign)
{
    bool success = false;
    EVP_MD_CTX* mdctx = NULL;
    EVP_PKEY_CTX* ctx = NULL;
    const size_t digest_len = 32;
    uint8_t digest[digest_len];
    if ((mdctx = EVP_MD_CTX_new()) == NULL)
    {
        goto done;
    }
    const EVP_MD* hash_alg = EVP_sha256();
    if (EVP_DigestInit_ex(mdctx, hash_alg, NULL) != 1)
    {
        goto done;
    }
    if (EVP_DigestUpdate(mdctx, blob, blobLength) != 1)
    {
        goto done;
    }
    unsigned int digest_len_temp = (unsigned int)digest_len;
    if (EVP_DigestFinal_ex(mdctx, digest, &digest_len_temp) != 1)
    {
        goto done;
    }
    EVP_PKEY* pu_key = CryptoKeyHandleToEVP_PKEY(keyToSign);
    ctx = EVP_PKEY_CTX_new(pu_key, NULL);
    if (ctx == NULL)
    {
        goto done;
    }
    if (EVP_PKEY_verify_init(ctx) <= 0)
    {
        goto done;
    }
    if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
    {
        goto done;
    }
    if (EVP_PKEY_CTX_set_signature_md(ctx, hash_alg) <= 0)
    {
        goto done;
    }
    if (EVP_PKEY_verify(ctx, signature, sigLength, digest, digest_len) == 1)
    {
        success = true;
    }
done:
    if (mdctx != NULL)
    {
        EVP_MD_CTX_free(mdctx);
    }
    if (ctx != NULL)
    {
        EVP_PKEY_CTX_free(ctx);
    }
    return success;
}
bool VerifyRS256Signature ( const uint8_t * signature , const size_t sigLength , const uint8_t * blob , const size_t blobLength , CryptoKeyHandle keyToSign ) { bool success = false ; EVP_MD_CTX * mdctx = NULL ; EVP_PKEY_CTX * ctx = NULL ; const size_t digest_len = 32 ; uint8_t digest [ digest_len ] ; if ( ( mdctx = EVP_MD_CTX_new ( ) ) == NULL ) { goto done ; } const EVP_MD * hash_alg = EVP_sha256 ( ) ; if ( EVP_DigestInit_ex ( mdctx , hash_alg , NULL ) != 1 ) { goto done ; } if ( EVP_DigestUpdate ( mdctx , blob , blobLength ) != 1 ) { goto done ; } unsigned int digest_len_temp = ( unsigned int ) digest_len ; if ( EVP_DigestFinal_ex ( mdctx , digest , & digest_len_temp ) != 1 ) { goto done ; } EVP_PKEY * pu_key = CryptoKeyHandleToEVP_PKEY ( keyToSign ) ; ctx = EVP_PKEY_CTX_new ( pu_key , NULL ) ; if ( ctx == NULL ) { goto done ; } if ( EVP_PKEY_verify_init ( ctx ) <= 0 ) { goto done ; } if ( EVP_PKEY_CTX_set_rsa_padding ( ctx , RSA_PKCS1_PADDING ) <= 0 ) { goto done ; } if ( EVP_PKEY_CTX_set_signature_md ( ctx , hash_alg ) <= 0 ) { goto done ; } if ( EVP_PKEY_verify ( ctx , signature , sigLength , digest , digest_len ) == 1 ) { success = true ; } done : if ( mdctx != NULL ) { EVP_MD_CTX_free ( mdctx ) ; } if ( ctx != NULL ) { EVP_PKEY_CTX_free ( ctx ) ; } return success ; }
@brief Verifies the @p signature using RS256 on the @p blob and the @p key.
@brief Verifies the @p signature using RS256 on the @p blob and the @p key .