1 // Copyright (c) 2015-2018 The Bitcoin Core 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 BITCOIN_CRYPTO_AES_H
8 #define BITCOIN_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 AES128_KEYSIZE = 16;
16 static const int AES256_KEYSIZE = 32;
17 
18 /** An encryption class for AES-128. */
19 class AES128Encrypt
20 {
21 private:
22     AES128_ctx ctx;
23 
24 public:
25     explicit AES128Encrypt(const unsigned char key[16]);
26     ~AES128Encrypt();
27     void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
28 };
29 
30 /** A decryption class for AES-128. */
31 class AES128Decrypt
32 {
33 private:
34     AES128_ctx ctx;
35 
36 public:
37     explicit AES128Decrypt(const unsigned char key[16]);
38     ~AES128Decrypt();
39     void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
40 };
41 
42 /** An encryption class for AES-256. */
43 class AES256Encrypt
44 {
45 private:
46     AES256_ctx ctx;
47 
48 public:
49     explicit AES256Encrypt(const unsigned char key[32]);
50     ~AES256Encrypt();
51     void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
52 };
53 
54 /** A decryption class for AES-256. */
55 class AES256Decrypt
56 {
57 private:
58     AES256_ctx ctx;
59 
60 public:
61     explicit AES256Decrypt(const unsigned char key[32]);
62     ~AES256Decrypt();
63     void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
64 };
65 
66 class AES256CBCEncrypt
67 {
68 public:
69     AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
70     ~AES256CBCEncrypt();
71     int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
72 
73 private:
74     const AES256Encrypt enc;
75     const bool pad;
76     unsigned char iv[AES_BLOCKSIZE];
77 };
78 
79 class AES256CBCDecrypt
80 {
81 public:
82     AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
83     ~AES256CBCDecrypt();
84     int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
85 
86 private:
87     const AES256Decrypt dec;
88     const bool pad;
89     unsigned char iv[AES_BLOCKSIZE];
90 };
91 
92 class AES128CBCEncrypt
93 {
94 public:
95     AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
96     ~AES128CBCEncrypt();
97     int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
98 
99 private:
100     const AES128Encrypt enc;
101     const bool pad;
102     unsigned char iv[AES_BLOCKSIZE];
103 };
104 
105 class AES128CBCDecrypt
106 {
107 public:
108     AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
109     ~AES128CBCDecrypt();
110     int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
111 
112 private:
113     const AES128Decrypt dec;
114     const bool pad;
115     unsigned char iv[AES_BLOCKSIZE];
116 };
117 
118 #endif // BITCOIN_CRYPTO_AES_H
119