Loading src/CMakeLists.txt +2 −0 Changes for src/CMakeLists.txt: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -12,6 +12,7 @@ list(APPEND netplussrc crypto/hkdf.cpp crypto/rsa_pss_sha256.cpp crypto/rsa_pss_sha384.cpp crypto/rsa_pkcs1_sha256.cpp crypto/curve25519.cpp crypto/tls.cpp exception.cpp Loading Loading @@ -66,6 +67,7 @@ set(headers crypto/hkdf.h crypto/rsa_pss_sha256.h crypto/rsa_pss_sha384.h crypto/rsa_pkcs1_sha256.h crypto/sha.h crypto/tls.h crypto/curve25519.h Loading src/crypto/rsa.h +1 −0 Changes for src/crypto/rsa.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -144,5 +144,6 @@ namespace netplus { friend class tls; friend class rsa_pss_sha256; friend class rsa_pss_sha384; friend class rsa_pkcs1_sha256; }; }; src/crypto/rsa_pkcs1_sha256.cpp 0 → 100644 +76 −0 Changes for src/crypto/rsa_pkcs1_sha256.cpp: 76 added lines, 0 removed lines. Original line number Diff line number Diff line #include "rsa_pkcs1_sha256.h" #include "sha.h" #include <cstring> namespace netplus { // DER encoding of DigestInfo for SHA-256 (RFC 8017 Section 9.2, Note 1) // SEQUENCE { SEQUENCE { OID sha256, NULL }, OCTET STRING hash } static const uint8_t SHA256_DIGEST_INFO_PREFIX[] = { 0x30, 0x31, // SEQUENCE (49 bytes) 0x30, 0x0d, // SEQUENCE (13 bytes) 0x06, 0x09, // OID (9 bytes) = 2.16.840.1.101.3.4.2.1 (SHA-256) 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, // NULL 0x04, 0x20 // OCTET STRING (32 bytes = SHA-256 hash follows) }; // EMSA-PKCS1-v1_5-ENCODE (RFC 8017 Section 9.2) std::vector<uint8_t> rsa_pkcs1_sha256::emsa_pkcs1_v15_encode( const std::vector<uint8_t>& mHash, size_t emLen) { // T = DigestInfo prefix || Hash const size_t tLen = sizeof(SHA256_DIGEST_INFO_PREFIX) + mHash.size(); if (emLen < tLen + 11) return {}; // EM = 0x00 || 0x01 || PS || 0x00 || T // PS = 0xFF bytes, length = emLen - tLen - 3 std::vector<uint8_t> EM(emLen); size_t idx = 0; EM[idx++] = 0x00; EM[idx++] = 0x01; size_t psLen = emLen - tLen - 3; std::memset(&EM[idx], 0xFF, psLen); idx += psLen; EM[idx++] = 0x00; std::memcpy(&EM[idx], SHA256_DIGEST_INFO_PREFIX, sizeof(SHA256_DIGEST_INFO_PREFIX)); idx += sizeof(SHA256_DIGEST_INFO_PREFIX); std::memcpy(&EM[idx], mHash.data(), mHash.size()); return EM; } // RSASSA-PKCS1-v1_5-SIGN (RFC 8017 Section 8.2.1) std::vector<uint8_t> rsa_pkcs1_sha256::sign(netplus::rsa& key, const std::vector<uint8_t>& message) { // Step 1: Hash the message with SHA-256 std::vector<uint8_t> mHash = sha256_hash(message); if (mHash.size() != 32) return {}; // Step 2: Encode to EMSA-PKCS1-v1_5 size_t modBits = key.n.bitLength(); size_t k = (modBits + 7) / 8; // RSA modulus length in bytes std::vector<uint8_t> EM = emsa_pkcs1_v15_encode(mHash, k); if (EM.empty()) return {}; // Step 3: RSA signature primitive: s = m^d mod n rsa::bigInt m = rsa::bytesToBigIntBE(EM); rsa::bigInt s = rsa::modPow(m, key.d, key.n); // Step 4: Convert to byte string std::vector<uint8_t> out = rsa::bigIntToBytesBE(s, k); return out; } } // namespace netplus src/crypto/rsa_pkcs1_sha256.h 0 → 100644 +20 −0 Changes for src/crypto/rsa_pkcs1_sha256.h: 20 added lines, 0 removed lines. Original line number Diff line number Diff line #pragma once #include <vector> #include <cstdint> #include "rsa.h" namespace netplus { class rsa_pkcs1_sha256 { public: // RSASSA-PKCS1-v1_5 sign with SHA-256 (RS256 for JWT) static std::vector<uint8_t> sign(netplus::rsa& key, const std::vector<uint8_t>& message); 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, size_t emLen); }; } // namespace netplus Loading
src/CMakeLists.txt +2 −0 Changes for src/CMakeLists.txt: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -12,6 +12,7 @@ list(APPEND netplussrc crypto/hkdf.cpp crypto/rsa_pss_sha256.cpp crypto/rsa_pss_sha384.cpp crypto/rsa_pkcs1_sha256.cpp crypto/curve25519.cpp crypto/tls.cpp exception.cpp Loading Loading @@ -66,6 +67,7 @@ set(headers crypto/hkdf.h crypto/rsa_pss_sha256.h crypto/rsa_pss_sha384.h crypto/rsa_pkcs1_sha256.h crypto/sha.h crypto/tls.h crypto/curve25519.h Loading
src/crypto/rsa.h +1 −0 Changes for src/crypto/rsa.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -144,5 +144,6 @@ namespace netplus { friend class tls; friend class rsa_pss_sha256; friend class rsa_pss_sha384; friend class rsa_pkcs1_sha256; }; };
src/crypto/rsa_pkcs1_sha256.cpp 0 → 100644 +76 −0 Changes for src/crypto/rsa_pkcs1_sha256.cpp: 76 added lines, 0 removed lines. Original line number Diff line number Diff line #include "rsa_pkcs1_sha256.h" #include "sha.h" #include <cstring> namespace netplus { // DER encoding of DigestInfo for SHA-256 (RFC 8017 Section 9.2, Note 1) // SEQUENCE { SEQUENCE { OID sha256, NULL }, OCTET STRING hash } static const uint8_t SHA256_DIGEST_INFO_PREFIX[] = { 0x30, 0x31, // SEQUENCE (49 bytes) 0x30, 0x0d, // SEQUENCE (13 bytes) 0x06, 0x09, // OID (9 bytes) = 2.16.840.1.101.3.4.2.1 (SHA-256) 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, // NULL 0x04, 0x20 // OCTET STRING (32 bytes = SHA-256 hash follows) }; // EMSA-PKCS1-v1_5-ENCODE (RFC 8017 Section 9.2) std::vector<uint8_t> rsa_pkcs1_sha256::emsa_pkcs1_v15_encode( const std::vector<uint8_t>& mHash, size_t emLen) { // T = DigestInfo prefix || Hash const size_t tLen = sizeof(SHA256_DIGEST_INFO_PREFIX) + mHash.size(); if (emLen < tLen + 11) return {}; // EM = 0x00 || 0x01 || PS || 0x00 || T // PS = 0xFF bytes, length = emLen - tLen - 3 std::vector<uint8_t> EM(emLen); size_t idx = 0; EM[idx++] = 0x00; EM[idx++] = 0x01; size_t psLen = emLen - tLen - 3; std::memset(&EM[idx], 0xFF, psLen); idx += psLen; EM[idx++] = 0x00; std::memcpy(&EM[idx], SHA256_DIGEST_INFO_PREFIX, sizeof(SHA256_DIGEST_INFO_PREFIX)); idx += sizeof(SHA256_DIGEST_INFO_PREFIX); std::memcpy(&EM[idx], mHash.data(), mHash.size()); return EM; } // RSASSA-PKCS1-v1_5-SIGN (RFC 8017 Section 8.2.1) std::vector<uint8_t> rsa_pkcs1_sha256::sign(netplus::rsa& key, const std::vector<uint8_t>& message) { // Step 1: Hash the message with SHA-256 std::vector<uint8_t> mHash = sha256_hash(message); if (mHash.size() != 32) return {}; // Step 2: Encode to EMSA-PKCS1-v1_5 size_t modBits = key.n.bitLength(); size_t k = (modBits + 7) / 8; // RSA modulus length in bytes std::vector<uint8_t> EM = emsa_pkcs1_v15_encode(mHash, k); if (EM.empty()) return {}; // Step 3: RSA signature primitive: s = m^d mod n rsa::bigInt m = rsa::bytesToBigIntBE(EM); rsa::bigInt s = rsa::modPow(m, key.d, key.n); // Step 4: Convert to byte string std::vector<uint8_t> out = rsa::bigIntToBytesBE(s, k); return out; } } // namespace netplus
src/crypto/rsa_pkcs1_sha256.h 0 → 100644 +20 −0 Changes for src/crypto/rsa_pkcs1_sha256.h: 20 added lines, 0 removed lines. Original line number Diff line number Diff line #pragma once #include <vector> #include <cstdint> #include "rsa.h" namespace netplus { class rsa_pkcs1_sha256 { public: // RSASSA-PKCS1-v1_5 sign with SHA-256 (RS256 for JWT) static std::vector<uint8_t> sign(netplus::rsa& key, const std::vector<uint8_t>& message); 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, size_t emLen); }; } // namespace netplus