Skip to content
Snippets Groups Projects
aes.hh 1.97 KiB
#ifndef __VEREIGN_CRYPTO_AES_HH
#define __VEREIGN_CRYPTO_AES_HH

#include <vereign/bytes/buffer.hh>

/**
 * Provides utilities for AES encryption/decryption.
 */
namespace vereign::crypto::aes {

/**
 * Encrypt given bytes with AES256-GCM.
 *
 * Example:
 * @code
 * const std::string input{"foo bar"};
 * auto key = crypto::Rand(32); // 256 bits
 *
 * bytes::Buffer iv;
 * bytes::Buffer tag;
 * bytes::Buffer encrypted;
 *
 * crypto::aes::GCM256Encrypt(bytes::View(input), key.View(), iv, tag, encrypted);
 * @endcode
 *
 * @param src The bytes that will be encrypted.
 * @param key The AES 256 bit key.
 * @param iv The initialization vector that was used during the encryption.
 * @param tag The authentication tag that was produced during the encryption.
 * @param encrypted The result of the encryption.
 *
 * @throws crypto::OpenSSLError on failure.
 */
void GCM256Encrypt(
  bytes::View src,
  bytes::View key,
  bytes::Buffer& iv,
  bytes::Buffer& tag,
  bytes::Buffer& encrypted
);

/**
 * Decrypts given bytes with AES256-GCM.
 *
 * Example:
 * @code
 * const std::string input{"foo bar"};
 * auto key = crypto::Rand(32); // 256 bits
 *
 * bytes::Buffer iv;
 * bytes::Buffer tag;
 * bytes::Buffer encrypted;
 *
 * crypto::aes::GCM256Encrypt(bytes::View(input), key.View(), iv, tag, encrypted);
 *
 * bytes::Buffer decrypted;
 * crypto::aes::GCM256Decrypt(encrypted.View(), key.View(), iv.View(), tag.View(), decrypted);
 *
 * assert(input == decrypted.View().String());
 * @endcode
 *
 * @param src The bytes that will be decrypted.
 * @param key The AES 256 bit key.
 * @param iv The initialization vector that was used during the encryption.
 * @param tag The authentication tag that was produced during the encryption.
 * @param encrypted The result of the decryption.
 *
 * @throws crypto::OpenSSLError on failure.
 */
void GCM256Decrypt(
  bytes::View src,
  bytes::View key,
  bytes::View iv,
  bytes::View tag,
  bytes::Buffer& decrypted
);

} // vereign::crypto::aes

#endif // __VEREIGN_CRYPTO_AES_HH