delegate RSA verify to the RSA module

This commit is contained in:
Sergio R. Caprile committed 2026-07-01 12:31:11 -03:00
1 parent f055e45755
commit f8816d67c3
6 files changed
+125 -20

No files matched your search

+1 -10
View File
@@ -1704,7 +1704,6 @@ static int mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
return 0;
}
} else {
uint8_t r;
const uint8_t *n;
size_t nlen;
uint8_t sig2[512]; // 4096 bits
@@ -1724,15 +1723,7 @@ static int mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
return 0;
}
r = 0;
{
const uint8_t *p, *q;
size_t i;
p = sig2 + nlen - cert->tbshashsz;
q = cert->tbshash;
for (i = 0; i < cert->tbshashsz; i++) r |= (uint8_t)(*p++ ^ *q++);
}
return r == 0;
return mg_rsa_pkcs_verify(sig2, nlen, cert->tbshash, cert->tbshashsz);
}
}
+37
View File
@@ -950,4 +950,41 @@ bool mg_rsa_verify(const uint8_t *em, size_t nlen, const uint8_t *mhash) {
return pss_verify_sha256(em, nlen, mhash);
}
// Full RSASSA-PKCS1-v1_5-VERIFY for rsa_pkcs1_sha256/sha384 (RFC 8017 9.2)
// em - output of RSA public-key primitive (nlen bytes)
// nlen - modulus byte length
// hash - SHA-256 or SHA-384 of the certificate TBS data
// hashlen - hash length, 32 for SHA-256 or 48 for SHA-384
static bool pkcs1_v15_verify(const uint8_t *em, size_t nlen,
const uint8_t *hash, size_t hashlen) {
static const uint8_t sha256_di[] = {
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
static const uint8_t sha384_di[] = {
0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30};
const uint8_t *di = hashlen == 32 ? sha256_di : sha384_di;
size_t disz = hashlen == 32 ? sizeof(sha256_di) : sizeof(sha384_di);
size_t pslen, i;
uint8_t bad = 0;
if (hashlen != 32 && hashlen != 48) return false;
if (nlen < 3 + 8 + disz + hashlen) return false;
pslen = nlen - 3 - disz - hashlen;
bad |= (uint8_t) (em[0] | (em[1] ^ 0x01));
for (i = 0; i < pslen; i++) bad |= (uint8_t) (em[2 + i] ^ 0xff);
bad |= em[2 + pslen];
for (i = 0; i < disz; i++) bad |= (uint8_t) (em[3 + pslen + i] ^ di[i]);
for (i = 0; i < hashlen; i++)
bad |= (uint8_t) (em[3 + pslen + disz + i] ^ hash[i]);
return bad == 0;
}
bool mg_rsa_pkcs_verify(const uint8_t *em, size_t nlen, const uint8_t *hash,
size_t hashlen) {
return pkcs1_v15_verify(em, nlen, hash, hashlen);
}
#endif /* MG_TLS == MG_TLS_BUILTIN */
+2
View File
@@ -10,5 +10,7 @@ int mg_rsa_crt_sign(const uint8_t *em, size_t em_len,
const uint8_t *q, size_t q_len,
const uint8_t *qInv, size_t qInv_len,
uint8_t *signature, size_t sig_len);
bool mg_rsa_pkcs_verify(const uint8_t *em, size_t nlen, const uint8_t *hash,
size_t hashlen);
bool mg_rsa_verify(const uint8_t *em, size_t nlen, const uint8_t *mhash);
#endif // TLS_RSA_H