1 // Copyright (c) 2015-2019 The Limenka developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 //
5 // C++ wrapper around ctaes, a constant-time AES implementation
6
7 #ifndef LIMENKA_CRYPTO_AES_H
8 #define LIMENKA_CRYPTO_AES_H
9
10 extern "C" {
11 #include <crypto/ctaes/ctaes.h>
12 }
13
14 static const int AES_BLOCKSIZE = 16;
15 static const int AES256_KEYSIZE = 32;
16
17 /** An encryption class for AES-256. */
18 class AES256Encrypt
19 {
20 private:
21 AES256_ctx ctx;
22
23 public:
24 explicit AES256Encrypt(const unsigned char key[32]);
25 ~AES256Encrypt();
26 void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
27 };
28
29 /** A decryption class for AES-256. */
30 class AES256Decrypt
31 {
32 private:
33 AES256_ctx ctx;
34
35 public:
36 explicit AES256Decrypt(const unsigned char key[32]);
37 ~AES256Decrypt();
38 void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
39 };
40
41 class AES256CBCEncrypt
42 {
43 public:
44 AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
45 ~AES256CBCEncrypt();
46 int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
47
48 private:
49 const AES256Encrypt enc;
50 const bool pad;
51 unsigned char iv[AES_BLOCKSIZE];
52 };
53
54 class AES256CBCDecrypt
55 {
56 public:
57 AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
58 ~AES256CBCDecrypt();
59 int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
60
61 private:
62 const AES256Decrypt dec;
63 const bool pad;
64 unsigned char iv[AES_BLOCKSIZE];
65 };
66
67 #endif // LIMENKA_CRYPTO_AES_H
68