// // Created by loki on 6/1/19. // #ifndef SUNSHINE_CRYPTO_H #define SUNSHINE_CRYPTO_H #include #include #include #include #include #include #include "utility.h" namespace crypto { struct creds_t { std::string x509; std::string pkey; }; constexpr std::size_t digest_size = 256; void md_ctx_destroy(EVP_MD_CTX *); using sha256_t = std::array; using aes_t = std::array; using x509_t = util::safe_ptr; using x509_store_t = util::safe_ptr; using x509_store_ctx_t = util::safe_ptr; using cipher_ctx_t = util::safe_ptr; using md_ctx_t = util::safe_ptr; using bio_t = util::safe_ptr; using pkey_t = util::safe_ptr; sha256_t hash(const std::string_view &plaintext); aes_t gen_aes_key(const std::array &salt, const std::string_view &pin); x509_t x509(const std::string_view &x); pkey_t pkey(const std::string_view &k); std::string pem(x509_t &x509); std::string pem(pkey_t &pkey); std::vector sign256(const pkey_t &pkey, const std::string_view &data); bool verify256(const x509_t &x509, const std::string_view &data, const std::string_view &signature); creds_t gen_creds(const std::string_view &cn, std::uint32_t key_bits); std::string_view signature(const x509_t &x); std::string rand(std::size_t bytes); class cert_chain_t { public: KITTY_DECL_CONSTR(cert_chain_t) void add(x509_t &&cert); const char *verify(x509_t::element_type *cert); private: std::vector> _certs; x509_store_ctx_t _cert_ctx; }; class cipher_t { public: cipher_t(const aes_t &key); cipher_t(cipher_t&&) noexcept = default; cipher_t &operator=(cipher_t&&) noexcept = default; int encrypt(const std::string_view &plaintext, std::vector &cipher); int decrypt_gcm(aes_t &iv, const std::string_view &cipher, std::vector &plaintext); int decrypt(const std::string_view &cipher, std::vector &plaintext); private: cipher_ctx_t ctx; aes_t key; public: bool padding; }; } #endif //SUNSHINE_CRYPTO_H