Loading src/crypto/rsa.cpp +70 −0 Original line number Diff line number Diff line Loading @@ -793,4 +793,74 @@ namespace netplus { return final_res; } rsa::bigInt rsa::bytesToBigIntBE(const std::vector<uint8_t>& bytes) { netplus::rsa::bigInt out(64); // 64 limbs = 2048 bits; adjust if needed out.used = 1; out.data[0] = 0; for (uint8_t b : bytes) { // out = out << 8 uint32_t carry = 0; for (size_t i = 0; i < out.used; ++i) { uint32_t newCarry = out.data[i] >> 24; out.data[i] = (out.data[i] << 8) | carry; carry = newCarry; } if (carry != 0) { out.data[out.used++] = carry; } // out = out + b uint64_t sum = uint64_t(out.data[0]) + b; out.data[0] = uint32_t(sum); uint32_t c = uint32_t(sum >> 32); size_t idx = 1; while (c != 0 && idx < out.used) { uint64_t s = uint64_t(out.data[idx]) + c; out.data[idx] = uint32_t(s); c = uint32_t(s >> 32); ++idx; } if (c != 0) { out.data[out.used++] = c; } } // trim leading zero limbs (optional safety) while (out.used > 1 && out.data[out.used - 1] == 0) out.used--; return out; } rsa::bigInt rsa::bigIntFromBytesBE (const uint8_t* bytes, size_t len){ size_t words = (len + 3) / 4; rsa::bigInt x(words + 1); x.used = words; std::fill(x.data.get(), x.data.get() + x.capacity, 0); for (size_t i = 0; i < len; ++i) { uint8_t byte = bytes[len - 1 - i]; // LSB first size_t wordIdx = i / 4; size_t bitShift = (i % 4) * 8; x.data[wordIdx] |= (static_cast<uint32_t>(byte) << bitShift); } while (x.used > 1 && x.data[x.used - 1] == 0) x.used--; return x; }; std::vector<uint8_t> rsa::bigIntToBytesBE(const bigInt& x, size_t outLen){ std::vector<uint8_t> out(outLen, 0x00); bigInt t = x; for (size_t i = 0; i < outLen; i++) { out[outLen - 1 - i] = uint8_t(t.data[0] & 0xFF); t.shiftRight(8); } return out; } }; src/crypto/rsa.h +3 −1 Original line number Diff line number Diff line Loading @@ -111,7 +111,9 @@ namespace netplus { static void multiply(const bigInt& a, const bigInt& b, bigInt& res); static void divide(const bigInt& dividend, const bigInt& divisor, bigInt& quotient, bigInt& remainder); static int compare(const bigInt& a, const bigInt& b); static bigInt bytesToBigIntBE(const std::vector<uint8_t>& bytes); static bigInt bigIntFromBytesBE (const uint8_t* bytes, size_t len); static std::vector<uint8_t> bigIntToBytesBE(const bigInt& x, size_t outLen); private: bigInt n, e, d; bool isProbablyPrime(const bigInt& n, int k); Loading src/crypto/rsa_pss_sha256.cpp +2 −7 Original line number Diff line number Diff line Loading @@ -108,18 +108,13 @@ std::vector<uint8_t> rsa_pss_sha256::sign(netplus::rsa& key, if (EM.empty()) return {}; extern netplus::rsa::bigInt bytesToBigIntBE(const std::vector<uint8_t>&); netplus::rsa::bigInt m = bytesToBigIntBE(EM); netplus::rsa::bigInt m = rsa::bytesToBigIntBE(EM); // s = m^d mod n (raw RSA) netplus::rsa::bigInt s = netplus::rsa::modPow(m, key.d, key.n); extern std::vector<uint8_t> bigIntToBytesBE(const netplus::rsa::bigInt&, size_t outLen); size_t k = (modBits + 7) / 8; return bigIntToBytesBE(s, k); return rsa::bigIntToBytesBE(s, k); } } // namespace netplus Loading src/crypto/sha.cpp +1 −1 Original line number Diff line number Diff line #include "sha.h" std::vector<uint8_t> netplus::_sha256_hash(const std::vector<uint8_t>& input) { std::vector<uint8_t> netplus::sha256_hash(const std::vector<uint8_t>& input) { // 1. Initial State (First 32 bits of the fractional parts of the square roots of the first 8 primes) uint32_t h[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, Loading src/crypto/sha.h +1 −1 Original line number Diff line number Diff line Loading @@ -12,5 +12,5 @@ #define EPS1(x) (SHA256_ROR(x, 6) ^ SHA256_ROR(x, 11) ^ SHA256_ROR(x, 25)) namespace netplus { extern std::vector<uint8_t> _sha256_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha256_hash(const std::vector<uint8_t>& input); }; Loading
src/crypto/rsa.cpp +70 −0 Original line number Diff line number Diff line Loading @@ -793,4 +793,74 @@ namespace netplus { return final_res; } rsa::bigInt rsa::bytesToBigIntBE(const std::vector<uint8_t>& bytes) { netplus::rsa::bigInt out(64); // 64 limbs = 2048 bits; adjust if needed out.used = 1; out.data[0] = 0; for (uint8_t b : bytes) { // out = out << 8 uint32_t carry = 0; for (size_t i = 0; i < out.used; ++i) { uint32_t newCarry = out.data[i] >> 24; out.data[i] = (out.data[i] << 8) | carry; carry = newCarry; } if (carry != 0) { out.data[out.used++] = carry; } // out = out + b uint64_t sum = uint64_t(out.data[0]) + b; out.data[0] = uint32_t(sum); uint32_t c = uint32_t(sum >> 32); size_t idx = 1; while (c != 0 && idx < out.used) { uint64_t s = uint64_t(out.data[idx]) + c; out.data[idx] = uint32_t(s); c = uint32_t(s >> 32); ++idx; } if (c != 0) { out.data[out.used++] = c; } } // trim leading zero limbs (optional safety) while (out.used > 1 && out.data[out.used - 1] == 0) out.used--; return out; } rsa::bigInt rsa::bigIntFromBytesBE (const uint8_t* bytes, size_t len){ size_t words = (len + 3) / 4; rsa::bigInt x(words + 1); x.used = words; std::fill(x.data.get(), x.data.get() + x.capacity, 0); for (size_t i = 0; i < len; ++i) { uint8_t byte = bytes[len - 1 - i]; // LSB first size_t wordIdx = i / 4; size_t bitShift = (i % 4) * 8; x.data[wordIdx] |= (static_cast<uint32_t>(byte) << bitShift); } while (x.used > 1 && x.data[x.used - 1] == 0) x.used--; return x; }; std::vector<uint8_t> rsa::bigIntToBytesBE(const bigInt& x, size_t outLen){ std::vector<uint8_t> out(outLen, 0x00); bigInt t = x; for (size_t i = 0; i < outLen; i++) { out[outLen - 1 - i] = uint8_t(t.data[0] & 0xFF); t.shiftRight(8); } return out; } };
src/crypto/rsa.h +3 −1 Original line number Diff line number Diff line Loading @@ -111,7 +111,9 @@ namespace netplus { static void multiply(const bigInt& a, const bigInt& b, bigInt& res); static void divide(const bigInt& dividend, const bigInt& divisor, bigInt& quotient, bigInt& remainder); static int compare(const bigInt& a, const bigInt& b); static bigInt bytesToBigIntBE(const std::vector<uint8_t>& bytes); static bigInt bigIntFromBytesBE (const uint8_t* bytes, size_t len); static std::vector<uint8_t> bigIntToBytesBE(const bigInt& x, size_t outLen); private: bigInt n, e, d; bool isProbablyPrime(const bigInt& n, int k); Loading
src/crypto/rsa_pss_sha256.cpp +2 −7 Original line number Diff line number Diff line Loading @@ -108,18 +108,13 @@ std::vector<uint8_t> rsa_pss_sha256::sign(netplus::rsa& key, if (EM.empty()) return {}; extern netplus::rsa::bigInt bytesToBigIntBE(const std::vector<uint8_t>&); netplus::rsa::bigInt m = bytesToBigIntBE(EM); netplus::rsa::bigInt m = rsa::bytesToBigIntBE(EM); // s = m^d mod n (raw RSA) netplus::rsa::bigInt s = netplus::rsa::modPow(m, key.d, key.n); extern std::vector<uint8_t> bigIntToBytesBE(const netplus::rsa::bigInt&, size_t outLen); size_t k = (modBits + 7) / 8; return bigIntToBytesBE(s, k); return rsa::bigIntToBytesBE(s, k); } } // namespace netplus Loading
src/crypto/sha.cpp +1 −1 Original line number Diff line number Diff line #include "sha.h" std::vector<uint8_t> netplus::_sha256_hash(const std::vector<uint8_t>& input) { std::vector<uint8_t> netplus::sha256_hash(const std::vector<uint8_t>& input) { // 1. Initial State (First 32 bits of the fractional parts of the square roots of the first 8 primes) uint32_t h[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, Loading
src/crypto/sha.h +1 −1 Original line number Diff line number Diff line Loading @@ -12,5 +12,5 @@ #define EPS1(x) (SHA256_ROR(x, 6) ^ SHA256_ROR(x, 11) ^ SHA256_ROR(x, 25)) namespace netplus { extern std::vector<uint8_t> _sha256_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha256_hash(const std::vector<uint8_t>& input); };