/* * crypto/cpp/slcrypto.hpp * C++ façade over the SLeeLa national-grade crypto C API (crypto/include/slcrypto.h). * * Idiomatic, header-only std::vector/std::array wrappers. Same SECURITY NOTE * applies: reference implementations, not hardened for production use. */ #ifndef SLCRYPTO_HPP #define SLCRYPTO_HPP #include "slcrypto.h" #include #include #include #include #include namespace slcrypto { using Bytes = std::vector; /* ---- Hashing ------------------------------------------------------------- */ inline std::array sha256(const Bytes& d) { std::array o{}; slc_sha256(d.data(), d.size(), o.data()); return o; } inline std::array sha384(const Bytes& d) { std::array o{}; slc_sha384(d.data(), d.size(), o.data()); return o; } inline std::array sha512(const Bytes& d) { std::array o{}; slc_sha512(d.data(), d.size(), o.data()); return o; } inline std::array sha3_256(const Bytes& d) { std::array o{}; slc_sha3_256(d.data(), d.size(), o.data()); return o; } inline std::array sha3_512(const Bytes& d) { std::array o{}; slc_sha3_512(d.data(), d.size(), o.data()); return o; } /* ---- HMAC / HKDF --------------------------------------------------------- */ inline std::array hmac_sha256(const Bytes& key, const Bytes& msg) { std::array o{}; slc_hmac_sha256(key.data(), key.size(), msg.data(), msg.size(), o.data()); return o; } inline std::array hmac_sha512(const Bytes& key, const Bytes& msg) { std::array o{}; slc_hmac_sha512(key.data(), key.size(), msg.data(), msg.size(), o.data()); return o; } inline Bytes hkdf_sha256(const Bytes& salt, const Bytes& ikm, const Bytes& info, size_t out_len) { Bytes o(out_len); slc_hkdf_sha256(salt.data(), salt.size(), ikm.data(), ikm.size(), info.data(), info.size(), o.data(), out_len); return o; } /* ---- AES-GCM ------------------------------------------------------------- */ struct GcmResult { Bytes ciphertext; Bytes tag; }; inline GcmResult aes_gcm_encrypt(const Bytes& key, const Bytes& iv, const Bytes& aad, const Bytes& pt, size_t tag_len = 16) { GcmResult r; r.ciphertext.resize(pt.size()); r.tag.resize(tag_len); int bits = static_cast(key.size() * 8); auto st = slc_aes_gcm_encrypt(key.data(), bits, iv.data(), iv.size(), aad.data(), aad.size(), pt.data(), pt.size(), r.ciphertext.data(), r.tag.data(), tag_len); if (st != SLCRYPTO_OK) throw std::runtime_error("aes_gcm_encrypt failed"); return r; } /* Returns plaintext; throws on authentication failure (bad tag). */ inline Bytes aes_gcm_decrypt(const Bytes& key, const Bytes& iv, const Bytes& aad, const Bytes& ct, const Bytes& tag) { Bytes pt(ct.size()); int bits = static_cast(key.size() * 8); auto st = slc_aes_gcm_decrypt(key.data(), bits, iv.data(), iv.size(), aad.data(), aad.size(), ct.data(), ct.size(), tag.data(), tag.size(), pt.data()); if (st == SLCRYPTO_ERR_AUTH) throw std::runtime_error("aes_gcm_decrypt: authentication failed"); if (st != SLCRYPTO_OK) throw std::runtime_error("aes_gcm_decrypt failed"); return pt; } /* ---- hex helper (test/diagnostic convenience) ---------------------------- */ template inline std::string to_hex(const C& bytes) { static const char* H = "0123456789abcdef"; std::string s; s.reserve(bytes.size() * 2); for (uint8_t b : bytes) { s.push_back(H[b >> 4]); s.push_back(H[b & 0xf]); } return s; } } // namespace slcrypto #endif // SLCRYPTO_HPP