Raven Core  3.0.0
P2P Digital Currency
aes.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "aes.h"
7 #include "crypto/common.h"
8 
9 #include <assert.h>
10 #include <string.h>
11 
12 extern "C" {
13 #include "crypto/ctaes/ctaes.c"
14 }
15 
16 AES128Encrypt::AES128Encrypt(const unsigned char key[16])
17 {
18  AES128_init(&ctx, key);
19 }
20 
22 {
23  memset(&ctx, 0, sizeof(ctx));
24 }
25 
26 void AES128Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
27 {
28  AES128_encrypt(&ctx, 1, ciphertext, plaintext);
29 }
30 
31 AES128Decrypt::AES128Decrypt(const unsigned char key[16])
32 {
33  AES128_init(&ctx, key);
34 }
35 
37 {
38  memset(&ctx, 0, sizeof(ctx));
39 }
40 
41 void AES128Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
42 {
43  AES128_decrypt(&ctx, 1, plaintext, ciphertext);
44 }
45 
46 AES256Encrypt::AES256Encrypt(const unsigned char key[32])
47 {
48  AES256_init(&ctx, key);
49 }
50 
52 {
53  memset(&ctx, 0, sizeof(ctx));
54 }
55 
56 void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
57 {
58  AES256_encrypt(&ctx, 1, ciphertext, plaintext);
59 }
60 
61 AES256Decrypt::AES256Decrypt(const unsigned char key[32])
62 {
63  AES256_init(&ctx, key);
64 }
65 
67 {
68  memset(&ctx, 0, sizeof(ctx));
69 }
70 
71 void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
72 {
73  AES256_decrypt(&ctx, 1, plaintext, ciphertext);
74 }
75 
76 
77 template <typename T>
78 static int CBCEncrypt(const T& enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
79 {
80  int written = 0;
81  int padsize = size % AES_BLOCKSIZE;
82  unsigned char mixed[AES_BLOCKSIZE];
83 
84  if (!data || !size || !out)
85  return 0;
86 
87  if (!pad && padsize != 0)
88  return 0;
89 
90  memcpy(mixed, iv, AES_BLOCKSIZE);
91 
92  // Write all but the last block
93  while (written + AES_BLOCKSIZE <= size) {
94  for (int i = 0; i != AES_BLOCKSIZE; i++)
95  mixed[i] ^= *data++;
96  enc.Encrypt(out + written, mixed);
97  memcpy(mixed, out + written, AES_BLOCKSIZE);
98  written += AES_BLOCKSIZE;
99  }
100  if (pad) {
101  // For all that remains, pad each byte with the value of the remaining
102  // space. If there is none, pad by a full block.
103  for (int i = 0; i != padsize; i++)
104  mixed[i] ^= *data++;
105  for (int i = padsize; i != AES_BLOCKSIZE; i++)
106  mixed[i] ^= AES_BLOCKSIZE - padsize;
107  enc.Encrypt(out + written, mixed);
108  written += AES_BLOCKSIZE;
109  }
110  return written;
111 }
112 
113 template <typename T>
114 static int CBCDecrypt(const T& dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
115 {
116  int written = 0;
117  bool fail = false;
118  const unsigned char* prev = iv;
119 
120  if (!data || !size || !out)
121  return 0;
122 
123  if (size % AES_BLOCKSIZE != 0)
124  return 0;
125 
126  // Decrypt all data. Padding will be checked in the output.
127  while (written != size) {
128  dec.Decrypt(out, data + written);
129  for (int i = 0; i != AES_BLOCKSIZE; i++)
130  *out++ ^= prev[i];
131  prev = data + written;
132  written += AES_BLOCKSIZE;
133  }
134 
135  // When decrypting padding, attempt to run in constant-time
136  if (pad) {
137  // If used, padding size is the value of the last decrypted byte. For
138  // it to be valid, It must be between 1 and AES_BLOCKSIZE.
139  unsigned char padsize = *--out;
140  fail = !padsize | (padsize > AES_BLOCKSIZE);
141 
142  // If not well-formed, treat it as though there's no padding.
143  padsize *= !fail;
144 
145  // All padding must equal the last byte otherwise it's not well-formed
146  for (int i = AES_BLOCKSIZE; i != 0; i--)
147  fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
148 
149  written -= padsize;
150  }
151  return written * !fail;
152 }
153 
154 AES256CBCEncrypt::AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
155  : enc(key), pad(padIn)
156 {
157  memcpy(iv, ivIn, AES_BLOCKSIZE);
158 }
159 
160 int AES256CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
161 {
162  return CBCEncrypt(enc, iv, data, size, pad, out);
163 }
164 
166 {
167  memset(iv, 0, sizeof(iv));
168 }
169 
170 AES256CBCDecrypt::AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
171  : dec(key), pad(padIn)
172 {
173  memcpy(iv, ivIn, AES_BLOCKSIZE);
174 }
175 
176 
177 int AES256CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
178 {
179  return CBCDecrypt(dec, iv, data, size, pad, out);
180 }
181 
183 {
184  memset(iv, 0, sizeof(iv));
185 }
186 
187 AES128CBCEncrypt::AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
188  : enc(key), pad(padIn)
189 {
190  memcpy(iv, ivIn, AES_BLOCKSIZE);
191 }
192 
194 {
195  memset(iv, 0, AES_BLOCKSIZE);
196 }
197 
198 int AES128CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
199 {
200  return CBCEncrypt(enc, iv, data, size, pad, out);
201 }
202 
203 AES128CBCDecrypt::AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
204  : dec(key), pad(padIn)
205 {
206  memcpy(iv, ivIn, AES_BLOCKSIZE);
207 }
208 
210 {
211  memset(iv, 0, AES_BLOCKSIZE);
212 }
213 
214 int AES128CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
215 {
216  return CBCDecrypt(dec, iv, data, size, pad, out);
217 }
void AES256_init(AES256_ctx *ctx, const unsigned char *key32)
Definition: ctaes.c:538
AES256Encrypt(const unsigned char key[32])
Definition: aes.cpp:46
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:214
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
Definition: ctaes.c:501
AES128Encrypt(const unsigned char key[16])
Definition: aes.cpp:16
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
Definition: aes.cpp:56
const AES256Decrypt dec
Definition: aes.h:88
AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:187
void AES256_encrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
Definition: ctaes.c:542
~AES256Encrypt()
Definition: aes.cpp:51
const AES128Encrypt enc
Definition: aes.h:101
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:90
void AES128_decrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
Definition: ctaes.c:509
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:177
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
Definition: aes.cpp:71
~AES128Encrypt()
Definition: aes.cpp:21
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
Definition: aes.cpp:41
~AES128CBCDecrypt()
Definition: aes.cpp:209
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
Definition: ctaes.c:550
const bool pad
Definition: aes.h:102
const AES128Decrypt dec
Definition: aes.h:114
const bool pad
Definition: aes.h:89
~AES256CBCDecrypt()
Definition: aes.cpp:182
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:116
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:198
AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:170
void AES128_init(AES128_ctx *ctx, const unsigned char *key16)
Definition: ctaes.c:497
AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:154
~AES128Decrypt()
Definition: aes.cpp:36
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:160
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
Definition: aes.cpp:26
const AES256Encrypt enc
Definition: aes.h:75
~AES256Decrypt()
Definition: aes.cpp:66
void * memcpy(void *a, const void *b, size_t c)
AES128Decrypt(const unsigned char key[16])
Definition: aes.cpp:31
~AES256CBCEncrypt()
Definition: aes.cpp:165
~AES128CBCEncrypt()
Definition: aes.cpp:193
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:103
size_t size() const
Definition: univalue.h:70
AES128_ctx ctx
Definition: aes.h:23
const bool pad
Definition: aes.h:76
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:77
AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:203
AES256Decrypt(const unsigned char key[32])
Definition: aes.cpp:61
const bool pad
Definition: aes.h:115