Files

142 lines
3.6 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//
// C++ wrapper around ctaes, a constant-time AES implementation
2018-01-16 10:47:52 +00:00
#ifndef FLOWEE_CRYPTO_AES_H
#define FLOWEE_CRYPTO_AES_H
extern "C" {
2018-02-10 16:57:46 +01:00
#include "ctaes/ctaes.h"
}
2022-07-12 15:05:12 +02:00
#include <vector>
static const int AES_BLOCKSIZE = 16;
static const int AES128_KEYSIZE = 16;
static const int AES256_KEYSIZE = 32;
/** An encryption class for AES-128. */
class AES128Encrypt
{
private:
AES128_ctx ctx;
public:
2022-05-11 23:37:51 +02:00
AES128Encrypt(const char key[16]);
~AES128Encrypt();
2022-05-11 23:37:51 +02:00
void encrypt(char ciphertext[16], const char plaintext[16]) const;
};
/** A decryption class for AES-128. */
class AES128Decrypt
{
private:
AES128_ctx ctx;
public:
2022-05-11 23:37:51 +02:00
AES128Decrypt(const char key[16]);
~AES128Decrypt();
2022-05-11 23:37:51 +02:00
void decrypt(char plaintext[16], const char ciphertext[16]) const;
};
/** An encryption class for AES-256. */
class AES256Encrypt
{
private:
AES256_ctx ctx;
public:
2022-05-11 23:37:51 +02:00
AES256Encrypt(const char key[32]);
~AES256Encrypt();
2022-05-11 23:37:51 +02:00
void encrypt(char ciphertext[16], const char plaintext[16]) const;
};
/** A decryption class for AES-256. */
class AES256Decrypt
{
private:
AES256_ctx ctx;
public:
AES256Decrypt(const char key[32]);
~AES256Decrypt();
2022-05-11 23:37:51 +02:00
void decrypt(char plaintext[16], const char ciphertext[16]) const;
};
2015-03-20 00:49:13 -04:00
class AES256CBCEncrypt
{
public:
2022-05-11 23:37:51 +02:00
AES256CBCEncrypt(const char key[AES256_KEYSIZE], const char ivIn[AES_BLOCKSIZE], bool padIn);
2015-03-20 00:49:13 -04:00
~AES256CBCEncrypt();
2022-07-12 15:05:12 +02:00
/// encrypt the \a data using the preferences passed in the constructor.
2022-05-11 23:37:51 +02:00
int encrypt(const char *data, int size, char *out) const;
2022-07-12 15:05:12 +02:00
/// convenience method
int encrypt(const std::vector<unsigned char> &data, char *out) const;
/// convenience method
int encrypt(const std::vector<char> &data, char *out) const;
2015-03-20 00:49:13 -04:00
private:
const AES256Encrypt enc;
const bool pad;
unsigned char iv[AES_BLOCKSIZE];
};
class AES256CBCDecrypt
{
public:
2022-05-11 23:37:51 +02:00
AES256CBCDecrypt(const char key[AES256_KEYSIZE], const char ivIn[AES_BLOCKSIZE], bool padIn);
2015-03-20 00:49:13 -04:00
~AES256CBCDecrypt();
2022-05-11 23:37:51 +02:00
int decrypt(const char *data, int size, char *out) const;
2022-07-12 15:05:12 +02:00
int decrypt(const std::vector<char> &data, char *out) const;
int decrypt(const std::vector<unsigned char> &data, char *out) const;
2015-03-20 00:49:13 -04:00
private:
const AES256Decrypt dec;
const bool pad;
2022-05-11 23:37:51 +02:00
char iv[AES_BLOCKSIZE];
2015-03-20 00:49:13 -04:00
};
class AES128CBCEncrypt
{
public:
2022-05-11 23:37:51 +02:00
AES128CBCEncrypt(const char key[AES128_KEYSIZE], const char ivIn[AES_BLOCKSIZE], bool padIn);
2015-03-20 00:49:13 -04:00
~AES128CBCEncrypt();
2022-05-11 23:37:51 +02:00
int encrypt(const char *data, int size, char *out) const;
2015-03-20 00:49:13 -04:00
private:
const AES128Encrypt enc;
const bool pad;
unsigned char iv[AES_BLOCKSIZE];
};
class AES128CBCDecrypt
{
public:
2022-05-11 23:37:51 +02:00
AES128CBCDecrypt(const char key[AES128_KEYSIZE], const char ivIn[AES_BLOCKSIZE], bool padIn);
2015-03-20 00:49:13 -04:00
~AES128CBCDecrypt();
2022-05-11 23:37:51 +02:00
int decrypt(const char *data, int size, char *out) const;
2015-03-20 00:49:13 -04:00
private:
const AES128Decrypt dec;
const bool pad;
2022-05-11 23:37:51 +02:00
char iv[AES_BLOCKSIZE];
2015-03-20 00:49:13 -04:00
};
2018-01-16 10:47:52 +00:00
#endif