Loading src/crypto/rsa.cpp +346 −347 File changed.Preview size limit exceeded, changes collapsed. Show changes src/crypto/rsa.h +51 −18 Changes for src/crypto/rsa.h: 51 added lines, 18 removed lines. Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <vector> #include <iostream> #include <memory> #include <string> Loading @@ -33,30 +34,51 @@ namespace netplus { class rsa { public: struct bigInt { std::unique_ptr<int[]> digits; size_t size; class bigInt { public: std::unique_ptr<uint32_t[]> data; size_t used; // number of 32-bit words used size_t capacity; // Constructors & Memory Management bigInt(size_t cap = 512); bigInt(const bigInt& other); // Copy Constructor bigInt(bigInt&& other) noexcept; // Move Constructor explicit bigInt(size_t cap_words = 64) : used(0), capacity(cap_words) { data = std::make_unique<uint32_t[]>(capacity); std::fill(data.get(), data.get() + capacity, 0); } // Assignment Operators (Fixes unique_ptr errors) bigInt& operator=(const bigInt& other); bigInt& operator=(bigInt&& other) noexcept; bigInt(uint32_t val, size_t cap) : used(val > 0 ? 1 : 0), capacity(cap) { data = std::make_unique<uint32_t[]>(capacity); std::fill(data.get(), data.get() + capacity, 0); if (used > 0) data[0] = val; } // Helper Methods void fromString(const std::string& s); std::string toString() const; bool isZero() const; bool isEven() const; void divideByTwo(); bigInt(const bigInt& other); netplus::rsa::bigInt& operator=(const bigInt& other); // Helper to check bit state for modPow bool isBitSet(size_t bit) const; bool isEven() const { return used > 0 && (data[0] & 1) == 0; } bool isZero() const { return used == 0 || (used == 1 && data[0] == 0); } size_t bitLength() const; void shiftLeft(size_t n); void reserve(size_t new_cap); void shiftRight(size_t n); void divideByTwo(); void fromUint64(uint64_t val); bool equalsUint64(uint64_t val) const; std::string toHexString() const; }; rsa(); rsa(const rsa &src); // RSA Core Methods void generateKeys(size_t digit_count); Loading @@ -64,6 +86,16 @@ namespace netplus { bigInt decrypt(const bigInt& cipher); // Static Math Engine void reserve(size_t new_cap); void shiftLeft(size_t n); void shiftRight(size_t n); void setRsaKeyFromRaw( const std::vector<uint8_t>& nBE, const std::vector<uint8_t>& eBE, const std::vector<uint8_t>& dBE ); static void add(const bigInt& a, const bigInt& b, bigInt& res); static void subtract(const bigInt& a, const bigInt& b, bigInt& res); static void multiply(const bigInt& a, const bigInt& b, bigInt& res); Loading @@ -75,9 +107,10 @@ namespace netplus { bool isProbablyPrime(const bigInt& n, int k); void findPrime(bigInt& p, size_t digits); void generateSecureRandom(bigInt& n, size_t requested_digits); static bigInt modPow(bigInt base, bigInt exp, const bigInt& mod); static bigInt modInverse(bigInt e, bigInt phi); static bigInt modPow(const bigInt& base, const bigInt& exp, const bigInt& mod); static bigInt modInverse(const bigInt& e, const bigInt& phi); static bigInt gcd(bigInt a, bigInt b); friend class x509cert; friend class ssl; }; }; src/crypto/x509.cpp +4 −12 Changes for src/crypto/x509.cpp: 4 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -229,20 +229,12 @@ size_t netplus::x509cert::parseInternal(const uint8_t* data, size_t size, ASN1No } void netplus::x509cert::bytesToBigInt(const std::vector<uint8_t>& bytes, netplus::rsa::bigInt& out) { out.fromString("0"); // Ensure it starts clean netplus::rsa::bigInt base256; base256.fromString("256"); out.reserve((bytes.size() / 4) + 1); out.used = 1; out.data[0] = 0; for (uint8_t b : bytes) { netplus::rsa::bigInt tempMul, tempAdd, byteVal; // If 'out' or 'tempMul' reaches 'capacity' without resizing, // you get the 'free(): invalid next size' error. netplus::rsa::multiply(out, base256, tempMul); byteVal.fromString(std::to_string((int)b)); netplus::rsa::add(tempMul, byteVal, out); out.shiftLeft(8); out.data[0] |= b; } } Loading src/crypto/x509.h +1 −0 Changes for src/crypto/x509.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -56,6 +56,7 @@ namespace netplus { std::string getSubjectCN(); bool extractPublicKey(netplus::rsa& outRsa); std::vector<std::string> getSubjectAltNames(); private: Loading src/socket.h +9 −2 Changes for src/socket.h: 9 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -214,6 +214,7 @@ namespace netplus { size_t sendData(buffer &data, int flags = 0) override; size_t recvData(buffer &data, int flags = 0) override; bool loadServerPrivateKeyDer(const std::string& keyDerPath); private: // --- Cryptographic Components --- netplus::x509cert _cert; Loading @@ -235,8 +236,14 @@ namespace netplus { uint64_t _send_seq = 0; uint64_t _recv_seq = 0; std::vector<uint8_t> _sha1_hash(const std::vector<uint8_t>& input) ; std::vector<uint8_t> _handshake_transcript; std::vector<uint8_t> _sha1_hash(const std::vector<uint8_t>& input); std::vector<uint8_t> _sha256_hash(const std::vector<uint8_t>& input) ; std::vector<uint8_t> _hmac_sha256( const std::vector<uint8_t>& key, const std::vector<uint8_t>& data ); bool _handshakeDone; // --- Internal Handshake Helpers --- Loading @@ -245,7 +252,7 @@ namespace netplus { const std::vector<uint8_t>& data, std::unique_ptr<socket>& csock ); void _sendServerHello(std::unique_ptr<socket> &csock); void _sendServerHello(std::unique_ptr<socket> &csock, bool secureReneg); // PRF (Pseudo-Random Function) for Key Derivation std::vector<uint8_t> _prf( Loading Loading
src/crypto/rsa.cpp +346 −347 File changed.Preview size limit exceeded, changes collapsed. Show changes
src/crypto/rsa.h +51 −18 Changes for src/crypto/rsa.h: 51 added lines, 18 removed lines. Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <vector> #include <iostream> #include <memory> #include <string> Loading @@ -33,30 +34,51 @@ namespace netplus { class rsa { public: struct bigInt { std::unique_ptr<int[]> digits; size_t size; class bigInt { public: std::unique_ptr<uint32_t[]> data; size_t used; // number of 32-bit words used size_t capacity; // Constructors & Memory Management bigInt(size_t cap = 512); bigInt(const bigInt& other); // Copy Constructor bigInt(bigInt&& other) noexcept; // Move Constructor explicit bigInt(size_t cap_words = 64) : used(0), capacity(cap_words) { data = std::make_unique<uint32_t[]>(capacity); std::fill(data.get(), data.get() + capacity, 0); } // Assignment Operators (Fixes unique_ptr errors) bigInt& operator=(const bigInt& other); bigInt& operator=(bigInt&& other) noexcept; bigInt(uint32_t val, size_t cap) : used(val > 0 ? 1 : 0), capacity(cap) { data = std::make_unique<uint32_t[]>(capacity); std::fill(data.get(), data.get() + capacity, 0); if (used > 0) data[0] = val; } // Helper Methods void fromString(const std::string& s); std::string toString() const; bool isZero() const; bool isEven() const; void divideByTwo(); bigInt(const bigInt& other); netplus::rsa::bigInt& operator=(const bigInt& other); // Helper to check bit state for modPow bool isBitSet(size_t bit) const; bool isEven() const { return used > 0 && (data[0] & 1) == 0; } bool isZero() const { return used == 0 || (used == 1 && data[0] == 0); } size_t bitLength() const; void shiftLeft(size_t n); void reserve(size_t new_cap); void shiftRight(size_t n); void divideByTwo(); void fromUint64(uint64_t val); bool equalsUint64(uint64_t val) const; std::string toHexString() const; }; rsa(); rsa(const rsa &src); // RSA Core Methods void generateKeys(size_t digit_count); Loading @@ -64,6 +86,16 @@ namespace netplus { bigInt decrypt(const bigInt& cipher); // Static Math Engine void reserve(size_t new_cap); void shiftLeft(size_t n); void shiftRight(size_t n); void setRsaKeyFromRaw( const std::vector<uint8_t>& nBE, const std::vector<uint8_t>& eBE, const std::vector<uint8_t>& dBE ); static void add(const bigInt& a, const bigInt& b, bigInt& res); static void subtract(const bigInt& a, const bigInt& b, bigInt& res); static void multiply(const bigInt& a, const bigInt& b, bigInt& res); Loading @@ -75,9 +107,10 @@ namespace netplus { bool isProbablyPrime(const bigInt& n, int k); void findPrime(bigInt& p, size_t digits); void generateSecureRandom(bigInt& n, size_t requested_digits); static bigInt modPow(bigInt base, bigInt exp, const bigInt& mod); static bigInt modInverse(bigInt e, bigInt phi); static bigInt modPow(const bigInt& base, const bigInt& exp, const bigInt& mod); static bigInt modInverse(const bigInt& e, const bigInt& phi); static bigInt gcd(bigInt a, bigInt b); friend class x509cert; friend class ssl; }; };
src/crypto/x509.cpp +4 −12 Changes for src/crypto/x509.cpp: 4 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -229,20 +229,12 @@ size_t netplus::x509cert::parseInternal(const uint8_t* data, size_t size, ASN1No } void netplus::x509cert::bytesToBigInt(const std::vector<uint8_t>& bytes, netplus::rsa::bigInt& out) { out.fromString("0"); // Ensure it starts clean netplus::rsa::bigInt base256; base256.fromString("256"); out.reserve((bytes.size() / 4) + 1); out.used = 1; out.data[0] = 0; for (uint8_t b : bytes) { netplus::rsa::bigInt tempMul, tempAdd, byteVal; // If 'out' or 'tempMul' reaches 'capacity' without resizing, // you get the 'free(): invalid next size' error. netplus::rsa::multiply(out, base256, tempMul); byteVal.fromString(std::to_string((int)b)); netplus::rsa::add(tempMul, byteVal, out); out.shiftLeft(8); out.data[0] |= b; } } Loading
src/crypto/x509.h +1 −0 Changes for src/crypto/x509.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -56,6 +56,7 @@ namespace netplus { std::string getSubjectCN(); bool extractPublicKey(netplus::rsa& outRsa); std::vector<std::string> getSubjectAltNames(); private: Loading
src/socket.h +9 −2 Changes for src/socket.h: 9 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -214,6 +214,7 @@ namespace netplus { size_t sendData(buffer &data, int flags = 0) override; size_t recvData(buffer &data, int flags = 0) override; bool loadServerPrivateKeyDer(const std::string& keyDerPath); private: // --- Cryptographic Components --- netplus::x509cert _cert; Loading @@ -235,8 +236,14 @@ namespace netplus { uint64_t _send_seq = 0; uint64_t _recv_seq = 0; std::vector<uint8_t> _sha1_hash(const std::vector<uint8_t>& input) ; std::vector<uint8_t> _handshake_transcript; std::vector<uint8_t> _sha1_hash(const std::vector<uint8_t>& input); std::vector<uint8_t> _sha256_hash(const std::vector<uint8_t>& input) ; std::vector<uint8_t> _hmac_sha256( const std::vector<uint8_t>& key, const std::vector<uint8_t>& data ); bool _handshakeDone; // --- Internal Handshake Helpers --- Loading @@ -245,7 +252,7 @@ namespace netplus { const std::vector<uint8_t>& data, std::unique_ptr<socket>& csock ); void _sendServerHello(std::unique_ptr<socket> &csock); void _sendServerHello(std::unique_ptr<socket> &csock, bool secureReneg); // PRF (Pseudo-Random Function) for Key Derivation std::vector<uint8_t> _prf( Loading