Loading src/crypto/rsa.cpp +55 −15 Changes for src/crypto/rsa.cpp: 55 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -562,31 +562,71 @@ namespace netplus { return true; } // Try to parse as public key (might be SubjectPublicKeyInfo with BIT STRING, or just RSAPublicKey SEQUENCE) // Try to parse as a public key: either a bare RSAPublicKey SEQUENCE { n, e }, // or a full X.509 SubjectPublicKeyInfo SEQUENCE { AlgorithmIdentifier, BIT STRING { RSAPublicKey } } // (this is the DER a DKIM "p=" tag or a PEM "PUBLIC KEY" block decodes to). if (data[cursor] == 0x30) { // Could be either RSAPublicKey or algorithm identifier cursor++; size_t innerSeqLength = 0; if (!parseDERLength(data + cursor, size - cursor, innerSeqLength, lenBytesRead)) { // AlgorithmIdentifier SEQUENCE -- skip over it entirely (OID + params), we don't // need to inspect it since this path is only ever reached for RSA keys. size_t algContentLen = 0, algHeaderLen = 0; size_t p = cursor + 1; if (!parseDERLength(data + p, size - p, algContentLen, algHeaderLen)) { return false; } cursor += lenBytesRead; p += algHeaderLen + algContentLen; if (cursor + innerSeqLength > size) { // BIT STRING wrapping the nested RSAPublicKey DER if (p >= size || data[p] != 0x03) { return false; } p++; size_t bitStrLen = 0, bitStrLenBytes = 0; if (!parseDERLength(data + p, size - p, bitStrLen, bitStrLenBytes)) { return false; } p += bitStrLenBytes; if (bitStrLen < 1 || p + bitStrLen > size) { return false; } // First byte of a BIT STRING is the "unused bits" count; DER-encoded key // material is always byte-aligned, so it must be 0. if (data[p] != 0x00) { return false; } p++; // Nested RSAPublicKey SEQUENCE { INTEGER n, INTEGER e } if (data[p] != 0x30) { return false; } size_t rsaPubContentLen = 0, rsaPubHeaderLen = 0; size_t q = p + 1; if (!parseDERLength(data + q, size - q, rsaPubContentLen, rsaPubHeaderLen)) { return false; } q += rsaPubHeaderLen; // Try to parse modulus and exponent size_t pubCursor = cursor; size_t intBytesRead = 0; if (parseDERInteger(data + pubCursor, innerSeqLength - (pubCursor - cursor), n, intBytesRead)) { pubCursor += intBytesRead; if (!parseDERInteger(data + q, size - q, n, intBytesRead)) return false; q += intBytesRead; if (!parseDERInteger(data + q, size - q, e, intBytesRead)) return false; if (parseDERInteger(data + pubCursor, innerSeqLength - (pubCursor - cursor), e, intBytesRead)) { if (!n.empty() && !e.empty()) { // Create a public-only key d.clear(); // No private exponent out_rsa.setRsaKeyFromRaw(n, e, d); out_rsa.setRsaKeyFromRaw(n, e, {0x00}); // no private exponent for a public key return true; } return false; } // Bare RSAPublicKey SEQUENCE { INTEGER n, INTEGER e }, no SubjectPublicKeyInfo wrapper. if (data[cursor] == 0x02) { size_t intBytesRead = 0; size_t pubCursor = cursor; if (parseDERInteger(data + pubCursor, size - pubCursor, n, intBytesRead)) { pubCursor += intBytesRead; if (parseDERInteger(data + pubCursor, size - pubCursor, e, intBytesRead)) { if (!n.empty() && !e.empty()) { out_rsa.setRsaKeyFromRaw(n, e, {0x00}); return true; } } Loading src/crypto/rsa_pkcs1_sha256.cpp +26 −0 Changes for src/crypto/rsa_pkcs1_sha256.cpp: 26 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -73,4 +73,30 @@ std::vector<uint8_t> rsa_pkcs1_sha256::sign(netplus::rsa& key, return out; } // RSASSA-PKCS1-v1_5-VERIFY (RFC 8017 Section 8.2.2) bool rsa_pkcs1_sha256::verify(netplus::rsa& key, const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) { std::vector<uint8_t> mHash = sha256_hash(message); if (mHash.size() != 32) return false; size_t modBits = key.n.bitLength(); size_t k = (modBits + 7) / 8; if (signature.empty() || signature.size() != k) return false; std::vector<uint8_t> EM = emsa_pkcs1_v15_encode(mHash, k); if (EM.empty()) return false; // RSA verification primitive: m = s^e mod n rsa::bigInt s = rsa::bytesToBigIntBE(signature); rsa::bigInt m = rsa::modPow(s, key.e, key.n); std::vector<uint8_t> EM2 = rsa::bigIntToBytesBE(m, k); return EM2 == EM; } } // namespace netplus src/crypto/rsa_pkcs1_sha256.h +6 −0 Changes for src/crypto/rsa_pkcs1_sha256.h: 6 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -11,6 +11,12 @@ public: static std::vector<uint8_t> sign(netplus::rsa& key, const std::vector<uint8_t>& message); // RSASSA-PKCS1-v1_5 verify with SHA-256 (RFC 8017 Section 8.2.2). `key` needs only // n/e (a public key loaded via the SubjectPublicKeyInfo path is sufficient). static bool verify(netplus::rsa& key, const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature); private: // EMSA-PKCS1-v1_5-ENCODE (RFC 8017 Section 9.2) static std::vector<uint8_t> emsa_pkcs1_v15_encode(const std::vector<uint8_t>& mHash, Loading test/rsa_pkcs1_sha256_test.cpp +27 −0 Changes for test/rsa_pkcs1_sha256_test.cpp: 27 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -77,6 +77,33 @@ int main() { std::cout << "PASS: signature changes when message changes\n"; } // verify() must accept the known-good NIST vector signature... rsa pub; pub.setRsaKeyFromRaw(hex_to_bytes(N), hex_to_bytes(E), {0x00}); if (rsa_pkcs1_sha256::verify(pub, msg, hex_to_bytes(SIG_EXPECTED))) { std::cout << "PASS: verify() accepts the NIST vector signature\n"; } else { failed++; std::cout << "FAIL: verify() rejected the NIST vector signature\n"; } // ...and reject it once either the message or the signature is tampered with. if (!rsa_pkcs1_sha256::verify(pub, msg2, hex_to_bytes(SIG_EXPECTED))) { std::cout << "PASS: verify() rejects a signature over the wrong message\n"; } else { failed++; std::cout << "FAIL: verify() accepted a signature over the wrong message\n"; } std::vector<uint8_t> sigTampered = hex_to_bytes(SIG_EXPECTED); sigTampered[0] ^= 0x01; if (!rsa_pkcs1_sha256::verify(pub, msg, sigTampered)) { std::cout << "PASS: verify() rejects a tampered signature\n"; } else { failed++; std::cout << "FAIL: verify() accepted a tampered signature\n"; } std::cout << (failed == 0 ? "\nAll RSA-PKCS1-SHA256 tests PASSED\n" : "\nRSA-PKCS1-SHA256 tests FAILED\n"); return failed == 0 ? 0 : 1; Loading Loading
src/crypto/rsa.cpp +55 −15 Changes for src/crypto/rsa.cpp: 55 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -562,31 +562,71 @@ namespace netplus { return true; } // Try to parse as public key (might be SubjectPublicKeyInfo with BIT STRING, or just RSAPublicKey SEQUENCE) // Try to parse as a public key: either a bare RSAPublicKey SEQUENCE { n, e }, // or a full X.509 SubjectPublicKeyInfo SEQUENCE { AlgorithmIdentifier, BIT STRING { RSAPublicKey } } // (this is the DER a DKIM "p=" tag or a PEM "PUBLIC KEY" block decodes to). if (data[cursor] == 0x30) { // Could be either RSAPublicKey or algorithm identifier cursor++; size_t innerSeqLength = 0; if (!parseDERLength(data + cursor, size - cursor, innerSeqLength, lenBytesRead)) { // AlgorithmIdentifier SEQUENCE -- skip over it entirely (OID + params), we don't // need to inspect it since this path is only ever reached for RSA keys. size_t algContentLen = 0, algHeaderLen = 0; size_t p = cursor + 1; if (!parseDERLength(data + p, size - p, algContentLen, algHeaderLen)) { return false; } cursor += lenBytesRead; p += algHeaderLen + algContentLen; if (cursor + innerSeqLength > size) { // BIT STRING wrapping the nested RSAPublicKey DER if (p >= size || data[p] != 0x03) { return false; } p++; size_t bitStrLen = 0, bitStrLenBytes = 0; if (!parseDERLength(data + p, size - p, bitStrLen, bitStrLenBytes)) { return false; } p += bitStrLenBytes; if (bitStrLen < 1 || p + bitStrLen > size) { return false; } // First byte of a BIT STRING is the "unused bits" count; DER-encoded key // material is always byte-aligned, so it must be 0. if (data[p] != 0x00) { return false; } p++; // Nested RSAPublicKey SEQUENCE { INTEGER n, INTEGER e } if (data[p] != 0x30) { return false; } size_t rsaPubContentLen = 0, rsaPubHeaderLen = 0; size_t q = p + 1; if (!parseDERLength(data + q, size - q, rsaPubContentLen, rsaPubHeaderLen)) { return false; } q += rsaPubHeaderLen; // Try to parse modulus and exponent size_t pubCursor = cursor; size_t intBytesRead = 0; if (parseDERInteger(data + pubCursor, innerSeqLength - (pubCursor - cursor), n, intBytesRead)) { pubCursor += intBytesRead; if (!parseDERInteger(data + q, size - q, n, intBytesRead)) return false; q += intBytesRead; if (!parseDERInteger(data + q, size - q, e, intBytesRead)) return false; if (parseDERInteger(data + pubCursor, innerSeqLength - (pubCursor - cursor), e, intBytesRead)) { if (!n.empty() && !e.empty()) { // Create a public-only key d.clear(); // No private exponent out_rsa.setRsaKeyFromRaw(n, e, d); out_rsa.setRsaKeyFromRaw(n, e, {0x00}); // no private exponent for a public key return true; } return false; } // Bare RSAPublicKey SEQUENCE { INTEGER n, INTEGER e }, no SubjectPublicKeyInfo wrapper. if (data[cursor] == 0x02) { size_t intBytesRead = 0; size_t pubCursor = cursor; if (parseDERInteger(data + pubCursor, size - pubCursor, n, intBytesRead)) { pubCursor += intBytesRead; if (parseDERInteger(data + pubCursor, size - pubCursor, e, intBytesRead)) { if (!n.empty() && !e.empty()) { out_rsa.setRsaKeyFromRaw(n, e, {0x00}); return true; } } Loading
src/crypto/rsa_pkcs1_sha256.cpp +26 −0 Changes for src/crypto/rsa_pkcs1_sha256.cpp: 26 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -73,4 +73,30 @@ std::vector<uint8_t> rsa_pkcs1_sha256::sign(netplus::rsa& key, return out; } // RSASSA-PKCS1-v1_5-VERIFY (RFC 8017 Section 8.2.2) bool rsa_pkcs1_sha256::verify(netplus::rsa& key, const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) { std::vector<uint8_t> mHash = sha256_hash(message); if (mHash.size() != 32) return false; size_t modBits = key.n.bitLength(); size_t k = (modBits + 7) / 8; if (signature.empty() || signature.size() != k) return false; std::vector<uint8_t> EM = emsa_pkcs1_v15_encode(mHash, k); if (EM.empty()) return false; // RSA verification primitive: m = s^e mod n rsa::bigInt s = rsa::bytesToBigIntBE(signature); rsa::bigInt m = rsa::modPow(s, key.e, key.n); std::vector<uint8_t> EM2 = rsa::bigIntToBytesBE(m, k); return EM2 == EM; } } // namespace netplus
src/crypto/rsa_pkcs1_sha256.h +6 −0 Changes for src/crypto/rsa_pkcs1_sha256.h: 6 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -11,6 +11,12 @@ public: static std::vector<uint8_t> sign(netplus::rsa& key, const std::vector<uint8_t>& message); // RSASSA-PKCS1-v1_5 verify with SHA-256 (RFC 8017 Section 8.2.2). `key` needs only // n/e (a public key loaded via the SubjectPublicKeyInfo path is sufficient). static bool verify(netplus::rsa& key, const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature); private: // EMSA-PKCS1-v1_5-ENCODE (RFC 8017 Section 9.2) static std::vector<uint8_t> emsa_pkcs1_v15_encode(const std::vector<uint8_t>& mHash, Loading
test/rsa_pkcs1_sha256_test.cpp +27 −0 Changes for test/rsa_pkcs1_sha256_test.cpp: 27 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -77,6 +77,33 @@ int main() { std::cout << "PASS: signature changes when message changes\n"; } // verify() must accept the known-good NIST vector signature... rsa pub; pub.setRsaKeyFromRaw(hex_to_bytes(N), hex_to_bytes(E), {0x00}); if (rsa_pkcs1_sha256::verify(pub, msg, hex_to_bytes(SIG_EXPECTED))) { std::cout << "PASS: verify() accepts the NIST vector signature\n"; } else { failed++; std::cout << "FAIL: verify() rejected the NIST vector signature\n"; } // ...and reject it once either the message or the signature is tampered with. if (!rsa_pkcs1_sha256::verify(pub, msg2, hex_to_bytes(SIG_EXPECTED))) { std::cout << "PASS: verify() rejects a signature over the wrong message\n"; } else { failed++; std::cout << "FAIL: verify() accepted a signature over the wrong message\n"; } std::vector<uint8_t> sigTampered = hex_to_bytes(SIG_EXPECTED); sigTampered[0] ^= 0x01; if (!rsa_pkcs1_sha256::verify(pub, msg, sigTampered)) { std::cout << "PASS: verify() rejects a tampered signature\n"; } else { failed++; std::cout << "FAIL: verify() accepted a tampered signature\n"; } std::cout << (failed == 0 ? "\nAll RSA-PKCS1-SHA256 tests PASSED\n" : "\nRSA-PKCS1-SHA256 tests FAILED\n"); return failed == 0 ? 0 : 1; Loading