Loading src/crypto/aes.cpp +218 −0 Changes for src/crypto/aes.cpp: 218 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -292,6 +292,124 @@ std::vector<uint8_t> aes128::decryptCBC(const std::vector<uint8_t>& ct, return out; } // CTS mode (Cipher Text Stealing) - RFC 3962 for Kerberos // CBC-CS3 variant: last two ciphertext blocks are swapped. // Input must be >= 16 bytes. Output size == input size (no padding). std::vector<uint8_t> aes128::encryptCTS(const std::vector<uint8_t>& pt, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("encryptCTS: iv must be 16 bytes"); if (pt.size() < 16) throw std::invalid_argument("encryptCTS: plaintext must be >= 16 bytes"); size_t n = pt.size(); // Special case: exactly one block - same as ECB if (n == 16) { uint8_t blk[16], out[16]; for (int i = 0; i < 16; i++) blk[i] = pt[i] ^ iv[i]; encrypt_block(blk, out); return std::vector<uint8_t>(out, out + 16); } // Pad plaintext to multiple of 16 with zeros for CBC processing size_t nBlocks = (n + 15) / 16; std::vector<uint8_t> padded = pt; padded.resize(nBlocks * 16, 0); // Standard CBC encrypt std::vector<uint8_t> cbcOut(nBlocks * 16); uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < nBlocks * 16; off += 16) { uint8_t blk[16]; for (int i = 0; i < 16; i++) blk[i] = padded[off + i] ^ prev[i]; encrypt_block(blk, &cbcOut[off]); std::memcpy(prev, &cbcOut[off], 16); } // CTS swap: output = CBC[0..n-3] || CBC[n-1] (full) || CBC[n-2] (truncated) std::vector<uint8_t> result(n); size_t penultimateOff = (nBlocks - 2) * 16; size_t lastOff = (nBlocks - 1) * 16; size_t lastPlainLen = n - penultimateOff - 16; // bytes in last partial block // Copy all complete blocks except last two if (nBlocks > 2) std::memcpy(result.data(), cbcOut.data(), penultimateOff); // Swap: last full ciphertext block, then truncated penultimate std::memcpy(result.data() + penultimateOff, &cbcOut[lastOff], 16); std::memcpy(result.data() + penultimateOff + 16, &cbcOut[penultimateOff], lastPlainLen); return result; } std::vector<uint8_t> aes128::decryptCTS(const std::vector<uint8_t>& ct, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("decryptCTS: iv must be 16 bytes"); if (ct.size() < 16) throw std::invalid_argument("decryptCTS: ciphertext must be >= 16 bytes"); size_t n = ct.size(); // Special case: exactly one block if (n == 16) { uint8_t dec[16]; decrypt_block(ct.data(), dec); std::vector<uint8_t> out(16); for (int i = 0; i < 16; i++) out[i] = dec[i] ^ iv[i]; return out; } size_t nBlocks = (n + 15) / 16; size_t penultimateOff = (nBlocks - 2) * 16; size_t lastPlainLen = n - penultimateOff - 16; // Un-swap CTS: ct has [blocks 0..n-3] || Cn-1 (full 16) || Cn-2 (truncated lastPlainLen) // Actual CBC order: Cn-2 is the full block, Cn-1 is the padded truncated one uint8_t Cn_1[16]; // the swapped full block (at penultimateOff in ct) std::memcpy(Cn_1, ct.data() + penultimateOff, 16); uint8_t Cn_2_trunc[16] = {}; // the truncated block (at penultimateOff+16 in ct) std::memcpy(Cn_2_trunc, ct.data() + penultimateOff + 16, lastPlainLen); // Decrypt Cn_1 (the swapped block) to get intermediate uint8_t decCn1[16]; decrypt_block(Cn_1, decCn1); // Reconstruct the full Cn_2 block: truncated bytes from ct, rest from decrypted Cn_1 uint8_t Cn_2_full[16]; std::memcpy(Cn_2_full, Cn_2_trunc, lastPlainLen); std::memcpy(Cn_2_full + lastPlainLen, decCn1 + lastPlainLen, 16 - lastPlainLen); // Now decrypt all blocks in proper CBC order std::vector<uint8_t> result(n); // Decrypt blocks 0..n-3 normally with CBC uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < penultimateOff; off += 16) { uint8_t dec[16]; decrypt_block(ct.data() + off, dec); for (int i = 0; i < 16; i++) result[off + i] = dec[i] ^ prev[i]; std::memcpy(prev, ct.data() + off, 16); } // Decrypt reconstructed Cn_2_full (penultimate in CBC order) { uint8_t dec[16]; decrypt_block(Cn_2_full, dec); for (int i = 0; i < 16; i++) result[penultimateOff + i] = dec[i] ^ prev[i]; std::memcpy(prev, Cn_2_full, 16); } // Last partial block: XOR decrypted Cn_1 with prev (Cn_2_full) for (size_t i = 0; i < lastPlainLen; i++) result[penultimateOff + 16 + i] = decCn1[i] ^ prev[i]; return result; } // GCM operations void aes128::gcm_mul(std::array<uint8_t,16>& X) const { uint64_t Xhi = load_be64(X.data()); Loading Loading @@ -721,6 +839,106 @@ std::vector<uint8_t> aes256::decryptCBC(const std::vector<uint8_t>& ct, return out; } // CTS mode (Cipher Text Stealing) - RFC 3962 for Kerberos (AES-256) std::vector<uint8_t> aes256::encryptCTS(const std::vector<uint8_t>& pt, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("encryptCTS: iv must be 16 bytes"); if (pt.size() < 16) throw std::invalid_argument("encryptCTS: plaintext must be >= 16 bytes"); size_t n = pt.size(); if (n == 16) { uint8_t blk[16], out[16]; for (int i = 0; i < 16; i++) blk[i] = pt[i] ^ iv[i]; encrypt_block(blk, out); return std::vector<uint8_t>(out, out + 16); } size_t nBlocks = (n + 15) / 16; std::vector<uint8_t> padded = pt; padded.resize(nBlocks * 16, 0); std::vector<uint8_t> cbcOut(nBlocks * 16); uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < nBlocks * 16; off += 16) { uint8_t blk[16]; for (int i = 0; i < 16; i++) blk[i] = padded[off + i] ^ prev[i]; encrypt_block(blk, &cbcOut[off]); std::memcpy(prev, &cbcOut[off], 16); } std::vector<uint8_t> result(n); size_t penultimateOff = (nBlocks - 2) * 16; size_t lastOff = (nBlocks - 1) * 16; size_t lastPlainLen = n - penultimateOff - 16; if (nBlocks > 2) std::memcpy(result.data(), cbcOut.data(), penultimateOff); std::memcpy(result.data() + penultimateOff, &cbcOut[lastOff], 16); std::memcpy(result.data() + penultimateOff + 16, &cbcOut[penultimateOff], lastPlainLen); return result; } std::vector<uint8_t> aes256::decryptCTS(const std::vector<uint8_t>& ct, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("decryptCTS: iv must be 16 bytes"); if (ct.size() < 16) throw std::invalid_argument("decryptCTS: ciphertext must be >= 16 bytes"); size_t n = ct.size(); if (n == 16) { uint8_t dec[16]; decrypt_block(ct.data(), dec); std::vector<uint8_t> out(16); for (int i = 0; i < 16; i++) out[i] = dec[i] ^ iv[i]; return out; } size_t nBlocks = (n + 15) / 16; size_t penultimateOff = (nBlocks - 2) * 16; size_t lastPlainLen = n - penultimateOff - 16; uint8_t Cn_1[16]; std::memcpy(Cn_1, ct.data() + penultimateOff, 16); uint8_t Cn_2_trunc[16] = {}; std::memcpy(Cn_2_trunc, ct.data() + penultimateOff + 16, lastPlainLen); uint8_t decCn1[16]; decrypt_block(Cn_1, decCn1); uint8_t Cn_2_full[16]; std::memcpy(Cn_2_full, Cn_2_trunc, lastPlainLen); std::memcpy(Cn_2_full + lastPlainLen, decCn1 + lastPlainLen, 16 - lastPlainLen); std::vector<uint8_t> result(n); uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < penultimateOff; off += 16) { uint8_t dec[16]; decrypt_block(ct.data() + off, dec); for (int i = 0; i < 16; i++) result[off + i] = dec[i] ^ prev[i]; std::memcpy(prev, ct.data() + off, 16); } { uint8_t dec[16]; decrypt_block(Cn_2_full, dec); for (int i = 0; i < 16; i++) result[penultimateOff + i] = dec[i] ^ prev[i]; std::memcpy(prev, Cn_2_full, 16); } for (size_t i = 0; i < lastPlainLen; i++) result[penultimateOff + 16 + i] = decCn1[i] ^ prev[i]; return result; } void aes256::gcm_mul(std::array<uint8_t,16>& X) const { uint64_t Xhi = load_be64(X.data()); uint64_t Xlo = load_be64(X.data() + 8); Loading src/crypto/aes.h +16 −0 Changes for src/crypto/aes.h: 16 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -167,6 +167,15 @@ public: std::vector<uint8_t> decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv) override; // CTS mode (RFC 3962 - Kerberos AES encryption) // Input may be any length >= 16 bytes (one AES block). // No padding is needed; output size equals input size. std::vector<uint8_t> encryptCTS(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv); std::vector<uint8_t> decryptCTS(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv); bool aes_gcm_encrypt( const uint8_t iv12[12], const uint8_t* aad, size_t aad_len, Loading Loading @@ -279,6 +288,13 @@ public: std::vector<uint8_t> decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv) override; // CTS mode (RFC 3962 - Kerberos AES encryption) std::vector<uint8_t> encryptCTS(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv); std::vector<uint8_t> decryptCTS(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv); bool aes_gcm_encrypt( const uint8_t iv12[12], const uint8_t* aad, size_t aad_len, Loading src/crypto/sha.cpp +136 −0 Changes for src/crypto/sha.cpp: 136 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -386,3 +386,139 @@ bool netplus::hmac_sha384_verify(const std::vector<uint8_t>& key, return ok; } // HMAC-SHA1 implementation (RFC 2104 with SHA-1) // Needed for Kerberos RFC 3962 PBKDF2-HMAC-SHA1 string-to-key std::vector<uint8_t> netplus::hmac_sha1(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg) { constexpr size_t B = 64; // SHA-1 block size constexpr size_t D = 20; // SHA-1 output size auto secure_zero = [](std::vector<uint8_t>& v) { volatile uint8_t* p = v.empty() ? nullptr : v.data(); for (size_t i = 0; p && i < v.size(); ++i) p[i] = 0; }; // Step 1: K0 = key normalized to block size B std::vector<uint8_t> K0; K0.reserve(B); if (key.size() > B) { K0 = sha1_hash(key); // 20 bytes K0.resize(B, 0x00); // pad to 64 } else { K0 = key; K0.resize(B, 0x00); } // Step 2: ipad/opad std::vector<uint8_t> ipad(B), opad(B); for (size_t i = 0; i < B; ++i) { ipad[i] = static_cast<uint8_t>(K0[i] ^ 0x36); opad[i] = static_cast<uint8_t>(K0[i] ^ 0x5c); } // Step 3: inner = SHA1((K0^ipad) || msg) std::vector<uint8_t> inner_input; inner_input.reserve(B + msg.size()); inner_input.insert(inner_input.end(), ipad.begin(), ipad.end()); inner_input.insert(inner_input.end(), msg.begin(), msg.end()); std::vector<uint8_t> inner = sha1_hash(inner_input); // Step 4: outer = SHA1((K0^opad) || inner) std::vector<uint8_t> outer_input; outer_input.reserve(B + D); outer_input.insert(outer_input.end(), opad.begin(), opad.end()); outer_input.insert(outer_input.end(), inner.begin(), inner.end()); std::vector<uint8_t> mac = sha1_hash(outer_input); // Wipe sensitive buffers secure_zero(K0); secure_zero(ipad); secure_zero(opad); secure_zero(inner_input); secure_zero(outer_input); secure_zero(inner); return mac; // 20 bytes } // Constant-time HMAC-SHA1 verification bool netplus::hmac_sha1_verify(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg, const std::vector<uint8_t>& mac) { auto ct_equal = [](const uint8_t* a, const uint8_t* b, size_t len) { uint8_t result = 0; for (size_t i = 0; i < len; ++i) { result |= a[i] ^ b[i]; } return result == 0; }; if (mac.size() != 20) return false; std::vector<uint8_t> expected = hmac_sha1(key, msg); bool ok = ct_equal(expected.data(), mac.data(), 20); volatile uint8_t* p = expected.data(); for (size_t i = 0; i < expected.size(); ++i) p[i] = 0; return ok; } // PBKDF2 with pluggable HMAC (RFC 2898 / RFC 8018) std::vector<uint8_t> netplus::pbkdf2( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen, size_t hmacLen, std::vector<uint8_t>(*hmacFunc)(const std::vector<uint8_t>&, const std::vector<uint8_t>&)) { size_t blocks = (dkLen + hmacLen - 1) / hmacLen; std::vector<uint8_t> result; result.reserve(blocks * hmacLen); for (size_t i = 1; i <= blocks; ++i) { // U_1 = HMAC(password, salt || INT_32_BE(i)) std::vector<uint8_t> saltBlock = salt; saltBlock.push_back(static_cast<uint8_t>((i >> 24) & 0xFF)); saltBlock.push_back(static_cast<uint8_t>((i >> 16) & 0xFF)); saltBlock.push_back(static_cast<uint8_t>((i >> 8) & 0xFF)); saltBlock.push_back(static_cast<uint8_t>(i & 0xFF)); auto u = hmacFunc(password, saltBlock); auto xorResult = u; for (uint32_t j = 1; j < iterations; ++j) { u = hmacFunc(password, u); for (size_t k = 0; k < hmacLen && k < u.size(); ++k) xorResult[k] ^= u[k]; } result.insert(result.end(), xorResult.begin(), xorResult.end()); } result.resize(dkLen); return result; } // Convenience: PBKDF2-HMAC-SHA1 (RFC 3962 Kerberos string-to-key) std::vector<uint8_t> netplus::pbkdf2_hmac_sha1( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen) { return pbkdf2(password, salt, iterations, dkLen, 20, hmac_sha1); } // Convenience: PBKDF2-HMAC-SHA256 std::vector<uint8_t> netplus::pbkdf2_hmac_sha256( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen) { return pbkdf2(password, salt, iterations, dkLen, 32, hmac_sha256); } src/crypto/sha.h +32 −0 Changes for src/crypto/sha.h: 32 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -8,6 +8,13 @@ namespace netplus { extern std::vector<uint8_t> sha256_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha384_hash(const std::vector<uint8_t>& input); // HMAC-SHA1 functions (RFC 2104, needed for Kerberos RFC 3962 PBKDF2) extern std::vector<uint8_t> hmac_sha1(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg); extern bool hmac_sha1_verify(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg, const std::vector<uint8_t>& mac); // HMAC-SHA256 functions extern std::vector<uint8_t> hmac_sha256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg); Loading @@ -18,4 +25,29 @@ namespace netplus { extern bool hmac_sha384_verify(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg, const std::vector<uint8_t>& mac); // PBKDF2 with pluggable HMAC (RFC 2898 / RFC 8018) // hmacFunc signature: hmac(key, msg) -> mac extern std::vector<uint8_t> pbkdf2( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen, size_t hmacLen, std::vector<uint8_t>(*hmacFunc)(const std::vector<uint8_t>&, const std::vector<uint8_t>&)); // Convenience: PBKDF2-HMAC-SHA1 (RFC 3962 Kerberos string-to-key) extern std::vector<uint8_t> pbkdf2_hmac_sha1( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen); // Convenience: PBKDF2-HMAC-SHA256 extern std::vector<uint8_t> pbkdf2_hmac_sha256( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen); } // namespace netplus; No newline at end of file Loading
src/crypto/aes.cpp +218 −0 Changes for src/crypto/aes.cpp: 218 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -292,6 +292,124 @@ std::vector<uint8_t> aes128::decryptCBC(const std::vector<uint8_t>& ct, return out; } // CTS mode (Cipher Text Stealing) - RFC 3962 for Kerberos // CBC-CS3 variant: last two ciphertext blocks are swapped. // Input must be >= 16 bytes. Output size == input size (no padding). std::vector<uint8_t> aes128::encryptCTS(const std::vector<uint8_t>& pt, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("encryptCTS: iv must be 16 bytes"); if (pt.size() < 16) throw std::invalid_argument("encryptCTS: plaintext must be >= 16 bytes"); size_t n = pt.size(); // Special case: exactly one block - same as ECB if (n == 16) { uint8_t blk[16], out[16]; for (int i = 0; i < 16; i++) blk[i] = pt[i] ^ iv[i]; encrypt_block(blk, out); return std::vector<uint8_t>(out, out + 16); } // Pad plaintext to multiple of 16 with zeros for CBC processing size_t nBlocks = (n + 15) / 16; std::vector<uint8_t> padded = pt; padded.resize(nBlocks * 16, 0); // Standard CBC encrypt std::vector<uint8_t> cbcOut(nBlocks * 16); uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < nBlocks * 16; off += 16) { uint8_t blk[16]; for (int i = 0; i < 16; i++) blk[i] = padded[off + i] ^ prev[i]; encrypt_block(blk, &cbcOut[off]); std::memcpy(prev, &cbcOut[off], 16); } // CTS swap: output = CBC[0..n-3] || CBC[n-1] (full) || CBC[n-2] (truncated) std::vector<uint8_t> result(n); size_t penultimateOff = (nBlocks - 2) * 16; size_t lastOff = (nBlocks - 1) * 16; size_t lastPlainLen = n - penultimateOff - 16; // bytes in last partial block // Copy all complete blocks except last two if (nBlocks > 2) std::memcpy(result.data(), cbcOut.data(), penultimateOff); // Swap: last full ciphertext block, then truncated penultimate std::memcpy(result.data() + penultimateOff, &cbcOut[lastOff], 16); std::memcpy(result.data() + penultimateOff + 16, &cbcOut[penultimateOff], lastPlainLen); return result; } std::vector<uint8_t> aes128::decryptCTS(const std::vector<uint8_t>& ct, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("decryptCTS: iv must be 16 bytes"); if (ct.size() < 16) throw std::invalid_argument("decryptCTS: ciphertext must be >= 16 bytes"); size_t n = ct.size(); // Special case: exactly one block if (n == 16) { uint8_t dec[16]; decrypt_block(ct.data(), dec); std::vector<uint8_t> out(16); for (int i = 0; i < 16; i++) out[i] = dec[i] ^ iv[i]; return out; } size_t nBlocks = (n + 15) / 16; size_t penultimateOff = (nBlocks - 2) * 16; size_t lastPlainLen = n - penultimateOff - 16; // Un-swap CTS: ct has [blocks 0..n-3] || Cn-1 (full 16) || Cn-2 (truncated lastPlainLen) // Actual CBC order: Cn-2 is the full block, Cn-1 is the padded truncated one uint8_t Cn_1[16]; // the swapped full block (at penultimateOff in ct) std::memcpy(Cn_1, ct.data() + penultimateOff, 16); uint8_t Cn_2_trunc[16] = {}; // the truncated block (at penultimateOff+16 in ct) std::memcpy(Cn_2_trunc, ct.data() + penultimateOff + 16, lastPlainLen); // Decrypt Cn_1 (the swapped block) to get intermediate uint8_t decCn1[16]; decrypt_block(Cn_1, decCn1); // Reconstruct the full Cn_2 block: truncated bytes from ct, rest from decrypted Cn_1 uint8_t Cn_2_full[16]; std::memcpy(Cn_2_full, Cn_2_trunc, lastPlainLen); std::memcpy(Cn_2_full + lastPlainLen, decCn1 + lastPlainLen, 16 - lastPlainLen); // Now decrypt all blocks in proper CBC order std::vector<uint8_t> result(n); // Decrypt blocks 0..n-3 normally with CBC uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < penultimateOff; off += 16) { uint8_t dec[16]; decrypt_block(ct.data() + off, dec); for (int i = 0; i < 16; i++) result[off + i] = dec[i] ^ prev[i]; std::memcpy(prev, ct.data() + off, 16); } // Decrypt reconstructed Cn_2_full (penultimate in CBC order) { uint8_t dec[16]; decrypt_block(Cn_2_full, dec); for (int i = 0; i < 16; i++) result[penultimateOff + i] = dec[i] ^ prev[i]; std::memcpy(prev, Cn_2_full, 16); } // Last partial block: XOR decrypted Cn_1 with prev (Cn_2_full) for (size_t i = 0; i < lastPlainLen; i++) result[penultimateOff + 16 + i] = decCn1[i] ^ prev[i]; return result; } // GCM operations void aes128::gcm_mul(std::array<uint8_t,16>& X) const { uint64_t Xhi = load_be64(X.data()); Loading Loading @@ -721,6 +839,106 @@ std::vector<uint8_t> aes256::decryptCBC(const std::vector<uint8_t>& ct, return out; } // CTS mode (Cipher Text Stealing) - RFC 3962 for Kerberos (AES-256) std::vector<uint8_t> aes256::encryptCTS(const std::vector<uint8_t>& pt, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("encryptCTS: iv must be 16 bytes"); if (pt.size() < 16) throw std::invalid_argument("encryptCTS: plaintext must be >= 16 bytes"); size_t n = pt.size(); if (n == 16) { uint8_t blk[16], out[16]; for (int i = 0; i < 16; i++) blk[i] = pt[i] ^ iv[i]; encrypt_block(blk, out); return std::vector<uint8_t>(out, out + 16); } size_t nBlocks = (n + 15) / 16; std::vector<uint8_t> padded = pt; padded.resize(nBlocks * 16, 0); std::vector<uint8_t> cbcOut(nBlocks * 16); uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < nBlocks * 16; off += 16) { uint8_t blk[16]; for (int i = 0; i < 16; i++) blk[i] = padded[off + i] ^ prev[i]; encrypt_block(blk, &cbcOut[off]); std::memcpy(prev, &cbcOut[off], 16); } std::vector<uint8_t> result(n); size_t penultimateOff = (nBlocks - 2) * 16; size_t lastOff = (nBlocks - 1) * 16; size_t lastPlainLen = n - penultimateOff - 16; if (nBlocks > 2) std::memcpy(result.data(), cbcOut.data(), penultimateOff); std::memcpy(result.data() + penultimateOff, &cbcOut[lastOff], 16); std::memcpy(result.data() + penultimateOff + 16, &cbcOut[penultimateOff], lastPlainLen); return result; } std::vector<uint8_t> aes256::decryptCTS(const std::vector<uint8_t>& ct, const std::vector<uint8_t>& iv) { if (iv.size() != 16) throw std::invalid_argument("decryptCTS: iv must be 16 bytes"); if (ct.size() < 16) throw std::invalid_argument("decryptCTS: ciphertext must be >= 16 bytes"); size_t n = ct.size(); if (n == 16) { uint8_t dec[16]; decrypt_block(ct.data(), dec); std::vector<uint8_t> out(16); for (int i = 0; i < 16; i++) out[i] = dec[i] ^ iv[i]; return out; } size_t nBlocks = (n + 15) / 16; size_t penultimateOff = (nBlocks - 2) * 16; size_t lastPlainLen = n - penultimateOff - 16; uint8_t Cn_1[16]; std::memcpy(Cn_1, ct.data() + penultimateOff, 16); uint8_t Cn_2_trunc[16] = {}; std::memcpy(Cn_2_trunc, ct.data() + penultimateOff + 16, lastPlainLen); uint8_t decCn1[16]; decrypt_block(Cn_1, decCn1); uint8_t Cn_2_full[16]; std::memcpy(Cn_2_full, Cn_2_trunc, lastPlainLen); std::memcpy(Cn_2_full + lastPlainLen, decCn1 + lastPlainLen, 16 - lastPlainLen); std::vector<uint8_t> result(n); uint8_t prev[16]; std::memcpy(prev, iv.data(), 16); for (size_t off = 0; off < penultimateOff; off += 16) { uint8_t dec[16]; decrypt_block(ct.data() + off, dec); for (int i = 0; i < 16; i++) result[off + i] = dec[i] ^ prev[i]; std::memcpy(prev, ct.data() + off, 16); } { uint8_t dec[16]; decrypt_block(Cn_2_full, dec); for (int i = 0; i < 16; i++) result[penultimateOff + i] = dec[i] ^ prev[i]; std::memcpy(prev, Cn_2_full, 16); } for (size_t i = 0; i < lastPlainLen; i++) result[penultimateOff + 16 + i] = decCn1[i] ^ prev[i]; return result; } void aes256::gcm_mul(std::array<uint8_t,16>& X) const { uint64_t Xhi = load_be64(X.data()); uint64_t Xlo = load_be64(X.data() + 8); Loading
src/crypto/aes.h +16 −0 Changes for src/crypto/aes.h: 16 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -167,6 +167,15 @@ public: std::vector<uint8_t> decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv) override; // CTS mode (RFC 3962 - Kerberos AES encryption) // Input may be any length >= 16 bytes (one AES block). // No padding is needed; output size equals input size. std::vector<uint8_t> encryptCTS(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv); std::vector<uint8_t> decryptCTS(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv); bool aes_gcm_encrypt( const uint8_t iv12[12], const uint8_t* aad, size_t aad_len, Loading Loading @@ -279,6 +288,13 @@ public: std::vector<uint8_t> decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv) override; // CTS mode (RFC 3962 - Kerberos AES encryption) std::vector<uint8_t> encryptCTS(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv); std::vector<uint8_t> decryptCTS(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv); bool aes_gcm_encrypt( const uint8_t iv12[12], const uint8_t* aad, size_t aad_len, Loading
src/crypto/sha.cpp +136 −0 Changes for src/crypto/sha.cpp: 136 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -386,3 +386,139 @@ bool netplus::hmac_sha384_verify(const std::vector<uint8_t>& key, return ok; } // HMAC-SHA1 implementation (RFC 2104 with SHA-1) // Needed for Kerberos RFC 3962 PBKDF2-HMAC-SHA1 string-to-key std::vector<uint8_t> netplus::hmac_sha1(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg) { constexpr size_t B = 64; // SHA-1 block size constexpr size_t D = 20; // SHA-1 output size auto secure_zero = [](std::vector<uint8_t>& v) { volatile uint8_t* p = v.empty() ? nullptr : v.data(); for (size_t i = 0; p && i < v.size(); ++i) p[i] = 0; }; // Step 1: K0 = key normalized to block size B std::vector<uint8_t> K0; K0.reserve(B); if (key.size() > B) { K0 = sha1_hash(key); // 20 bytes K0.resize(B, 0x00); // pad to 64 } else { K0 = key; K0.resize(B, 0x00); } // Step 2: ipad/opad std::vector<uint8_t> ipad(B), opad(B); for (size_t i = 0; i < B; ++i) { ipad[i] = static_cast<uint8_t>(K0[i] ^ 0x36); opad[i] = static_cast<uint8_t>(K0[i] ^ 0x5c); } // Step 3: inner = SHA1((K0^ipad) || msg) std::vector<uint8_t> inner_input; inner_input.reserve(B + msg.size()); inner_input.insert(inner_input.end(), ipad.begin(), ipad.end()); inner_input.insert(inner_input.end(), msg.begin(), msg.end()); std::vector<uint8_t> inner = sha1_hash(inner_input); // Step 4: outer = SHA1((K0^opad) || inner) std::vector<uint8_t> outer_input; outer_input.reserve(B + D); outer_input.insert(outer_input.end(), opad.begin(), opad.end()); outer_input.insert(outer_input.end(), inner.begin(), inner.end()); std::vector<uint8_t> mac = sha1_hash(outer_input); // Wipe sensitive buffers secure_zero(K0); secure_zero(ipad); secure_zero(opad); secure_zero(inner_input); secure_zero(outer_input); secure_zero(inner); return mac; // 20 bytes } // Constant-time HMAC-SHA1 verification bool netplus::hmac_sha1_verify(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg, const std::vector<uint8_t>& mac) { auto ct_equal = [](const uint8_t* a, const uint8_t* b, size_t len) { uint8_t result = 0; for (size_t i = 0; i < len; ++i) { result |= a[i] ^ b[i]; } return result == 0; }; if (mac.size() != 20) return false; std::vector<uint8_t> expected = hmac_sha1(key, msg); bool ok = ct_equal(expected.data(), mac.data(), 20); volatile uint8_t* p = expected.data(); for (size_t i = 0; i < expected.size(); ++i) p[i] = 0; return ok; } // PBKDF2 with pluggable HMAC (RFC 2898 / RFC 8018) std::vector<uint8_t> netplus::pbkdf2( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen, size_t hmacLen, std::vector<uint8_t>(*hmacFunc)(const std::vector<uint8_t>&, const std::vector<uint8_t>&)) { size_t blocks = (dkLen + hmacLen - 1) / hmacLen; std::vector<uint8_t> result; result.reserve(blocks * hmacLen); for (size_t i = 1; i <= blocks; ++i) { // U_1 = HMAC(password, salt || INT_32_BE(i)) std::vector<uint8_t> saltBlock = salt; saltBlock.push_back(static_cast<uint8_t>((i >> 24) & 0xFF)); saltBlock.push_back(static_cast<uint8_t>((i >> 16) & 0xFF)); saltBlock.push_back(static_cast<uint8_t>((i >> 8) & 0xFF)); saltBlock.push_back(static_cast<uint8_t>(i & 0xFF)); auto u = hmacFunc(password, saltBlock); auto xorResult = u; for (uint32_t j = 1; j < iterations; ++j) { u = hmacFunc(password, u); for (size_t k = 0; k < hmacLen && k < u.size(); ++k) xorResult[k] ^= u[k]; } result.insert(result.end(), xorResult.begin(), xorResult.end()); } result.resize(dkLen); return result; } // Convenience: PBKDF2-HMAC-SHA1 (RFC 3962 Kerberos string-to-key) std::vector<uint8_t> netplus::pbkdf2_hmac_sha1( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen) { return pbkdf2(password, salt, iterations, dkLen, 20, hmac_sha1); } // Convenience: PBKDF2-HMAC-SHA256 std::vector<uint8_t> netplus::pbkdf2_hmac_sha256( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen) { return pbkdf2(password, salt, iterations, dkLen, 32, hmac_sha256); }
src/crypto/sha.h +32 −0 Changes for src/crypto/sha.h: 32 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -8,6 +8,13 @@ namespace netplus { extern std::vector<uint8_t> sha256_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha384_hash(const std::vector<uint8_t>& input); // HMAC-SHA1 functions (RFC 2104, needed for Kerberos RFC 3962 PBKDF2) extern std::vector<uint8_t> hmac_sha1(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg); extern bool hmac_sha1_verify(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg, const std::vector<uint8_t>& mac); // HMAC-SHA256 functions extern std::vector<uint8_t> hmac_sha256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg); Loading @@ -18,4 +25,29 @@ namespace netplus { extern bool hmac_sha384_verify(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg, const std::vector<uint8_t>& mac); // PBKDF2 with pluggable HMAC (RFC 2898 / RFC 8018) // hmacFunc signature: hmac(key, msg) -> mac extern std::vector<uint8_t> pbkdf2( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen, size_t hmacLen, std::vector<uint8_t>(*hmacFunc)(const std::vector<uint8_t>&, const std::vector<uint8_t>&)); // Convenience: PBKDF2-HMAC-SHA1 (RFC 3962 Kerberos string-to-key) extern std::vector<uint8_t> pbkdf2_hmac_sha1( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen); // Convenience: PBKDF2-HMAC-SHA256 extern std::vector<uint8_t> pbkdf2_hmac_sha256( const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, uint32_t iterations, size_t dkLen); } // namespace netplus; No newline at end of file