12#ifndef OPENVPN_CRYPTO_TOKENENCRYPT_H
13#define OPENVPN_CRYPTO_TOKENENCRYPT_H
19#include <openssl/evp.h>
37 static constexpr size_t SIZE = 16;
58 ctx = EVP_CIPHER_CTX_new();
59 EVP_CIPHER_CTX_reset(
ctx);
60 if (!EVP_CipherInit_ex(
ctx, EVP_aes_128_ecb(),
nullptr, key.
data,
nullptr, mode))
62 EVP_CIPHER_CTX_free(
ctx);
65 EVP_CIPHER_CTX_set_padding(
ctx, 0);
70 EVP_CIPHER_CTX_free(
ctx);
74 void operator()(std::uint8_t *dest,
const std::uint8_t *src,
const int size)
79 if (size != EVP_CIPHER_CTX_block_size(
ctx))
80 throw Exception(
"TokenEncrypt: encrypt/decrypt data must be equal to AES block size");
82 if (!EVP_CipherInit_ex(
ctx,
nullptr,
nullptr,
nullptr,
nullptr, -1))
84 if (!EVP_CipherUpdate(
ctx, dest, &outlen, src, size))
88 throw Exception(
"TokenEncrypt: unexpected output length=" + std::to_string(outlen) +
" expected=" + std::to_string(size));
virtual void rand_bytes(unsigned char *buf, size_t size)=0
Fill a buffer with random bytes.
Abstract base class for cryptographically strong random number generators.
Key(StrongRandomAPI &rng)
static constexpr size_t SIZE
TokenEncrypt & operator=(const TokenEncrypt &)=delete
void operator()(std::uint8_t *dest, const std::uint8_t *src, const int size)
TokenEncrypt(const Key &key, const int mode)
TokenEncrypt(const TokenEncrypt &)=delete
Implementation of the base classes for random number generators.
TokenEncryptDecrypt(const TokenEncrypt::Key &key)