/* * Do an RSA operation and check the message digest */ int rsa_pkcs1_verify( rsa_context *ctx, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig ) { switch( ctx->padding ) { case RSA_PKCS_V15: return rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, hash_id, hashlen, hash, sig ); #if defined(POLARSSL_PKCS1_V21) case RSA_PKCS_V21: return rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, hash_id, hashlen, hash, sig ); #endif default: return( POLARSSL_ERR_RSA_INVALID_PADDING ); } }
int EsSign::RsaVerify(const u8* hash, const u8* modulus, const u8* signature) { static const u8 public_exponent[3] = { 0x01, 0x00, 0x01 }; int ret; EsSignType type; rsa_context rsa; int hash_id = 0; int hash_len = 0; rsa_init(&rsa, RSA_PKCS_V15, hash_id); if (hash == NULL || modulus == NULL || signature == NULL) return 1; // get signature type type = (EsSignType)be_word(*((u32*)(signature))); switch (type) { case(ES_SIGN_RSA4096_SHA1) : case(ES_SIGN_RSA4096_SHA256) : { rsa.len = Crypto::kRsa4096Size; hash_id = (type == ES_SIGN_RSA4096_SHA1) ? SIG_RSA_SHA1 : SIG_RSA_SHA256; hash_len = (type == ES_SIGN_RSA4096_SHA1) ? Crypto::kSha1HashLen : Crypto::kSha256HashLen; break; } case(ES_SIGN_RSA2048_SHA1) : case(ES_SIGN_RSA2048_SHA256) : { rsa.len = Crypto::kRsa2048Size; hash_id = (type == ES_SIGN_RSA2048_SHA1) ? SIG_RSA_SHA1 : SIG_RSA_SHA256; hash_len = (type == ES_SIGN_RSA2048_SHA1) ? Crypto::kSha1HashLen : Crypto::kSha256HashLen; break; } default: return 1; } mpi_read_binary(&rsa.E, public_exponent, sizeof(public_exponent)); mpi_read_binary(&rsa.N, modulus, rsa.len); ret = rsa_rsassa_pkcs1_v15_verify(&rsa, RSA_PRIVATE, hash_id, hash_len, hash, signature + 4); rsa_free(&rsa); return ret; }
int Crypto::VerifyRsa2048Sha256(const u8 modulus[kRsa2048Size], const u8 hash[kSha256HashLen], const u8 signature[kRsa2048Size]) { static const u8 public_exponent[3] = { 0x01, 0x00, 0x01 }; int ret; rsa_context ctx; rsa_init(&ctx, RSA_PKCS_V15, 0); ctx.len = kRsa2048Size; mpi_read_binary(&ctx.E, public_exponent, sizeof(public_exponent)); mpi_read_binary(&ctx.N, modulus, ctx.len); ret = rsa_rsassa_pkcs1_v15_verify(&ctx, RSA_PUBLIC, SIG_RSA_SHA256, kSha256HashLen, hash, signature); rsa_free(&ctx); return ret; }