PIVX Core  5.6.99
P2P Digital Currency
aes.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 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 #include "aes.h"
6 #include "crypto/common.h"
7 
8 #include <assert.h>
9 #include <string.h>
10 
11 extern "C" {
12 #include "crypto/ctaes/ctaes.c"
13 }
14 
15 AES128Encrypt::AES128Encrypt(const unsigned char key[16])
16 {
17  AES128_init(&ctx, key);
18 }
19 
21 {
22  memset(&ctx, 0, sizeof(ctx));
23 }
24 
25 void AES128Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
26 {
27  AES128_encrypt(&ctx, 1, ciphertext, plaintext);
28 }
29 
30 AES128Decrypt::AES128Decrypt(const unsigned char key[16])
31 {
32  AES128_init(&ctx, key);
33 }
34 
36 {
37  memset(&ctx, 0, sizeof(ctx));
38 }
39 
40 void AES128Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
41 {
42  AES128_decrypt(&ctx, 1, plaintext, ciphertext);
43 }
44 
45 AES256Encrypt::AES256Encrypt(const unsigned char key[32])
46 {
47  AES256_init(&ctx, key);
48 }
49 
51 {
52  memset(&ctx, 0, sizeof(ctx));
53 }
54 
55 void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
56 {
57  AES256_encrypt(&ctx, 1, ciphertext, plaintext);
58 }
59 
60 AES256Decrypt::AES256Decrypt(const unsigned char key[32])
61 {
62  AES256_init(&ctx, key);
63 }
64 
66 {
67  memset(&ctx, 0, sizeof(ctx));
68 }
69 
70 void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
71 {
72  AES256_decrypt(&ctx, 1, plaintext, ciphertext);
73 }
74 
75 
76 template <typename T>
77 static int CBCEncrypt(const T& enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
78 {
79  int written = 0;
80  int padsize = size % AES_BLOCKSIZE;
81  unsigned char mixed[AES_BLOCKSIZE];
82 
83  if (!data || !size || !out)
84  return 0;
85 
86  if (!pad && padsize != 0)
87  return 0;
88 
89  memcpy(mixed, iv, AES_BLOCKSIZE);
90 
91  // Write all but the last block
92  while (written + AES_BLOCKSIZE <= size) {
93  for (int i = 0; i != AES_BLOCKSIZE; i++)
94  mixed[i] ^= *data++;
95  enc.Encrypt(out + written, mixed);
96  memcpy(mixed, out + written, AES_BLOCKSIZE);
97  written += AES_BLOCKSIZE;
98  }
99  if (pad) {
100  // For all that remains, pad each byte with the value of the remaining
101  // space. If there is none, pad by a full block.
102  for (int i = 0; i != padsize; i++)
103  mixed[i] ^= *data++;
104  for (int i = padsize; i != AES_BLOCKSIZE; i++)
105  mixed[i] ^= AES_BLOCKSIZE - padsize;
106  enc.Encrypt(out + written, mixed);
107  written += AES_BLOCKSIZE;
108  }
109  return written;
110 }
111 
112 template <typename T>
113 static int CBCDecrypt(const T& dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
114 {
115  unsigned char padsize = 0;
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  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 }
~AES128CBCDecrypt()
Definition: aes.cpp:209
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:115
AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:203
const bool pad
Definition: aes.h:114
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:214
const AES128Decrypt dec
Definition: aes.h:113
AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:187
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:102
const AES128Encrypt enc
Definition: aes.h:100
~AES128CBCEncrypt()
Definition: aes.cpp:193
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:198
const bool pad
Definition: aes.h:101
AES128Decrypt(const unsigned char key[16])
Definition: aes.cpp:30
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
Definition: aes.cpp:40
AES128_ctx ctx
Definition: aes.h:34
~AES128Decrypt()
Definition: aes.cpp:35
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
Definition: aes.cpp:25
AES128Encrypt(const unsigned char key[16])
Definition: aes.cpp:15
~AES128Encrypt()
Definition: aes.cpp:20
AES128_ctx ctx
Definition: aes.h:22
const bool pad
Definition: aes.h:88
AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:170
const AES256Decrypt dec
Definition: aes.h:87
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:177
~AES256CBCDecrypt()
Definition: aes.cpp:182
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:89
AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
Definition: aes.cpp:154
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:160
const bool pad
Definition: aes.h:75
~AES256CBCEncrypt()
Definition: aes.cpp:165
const AES256Encrypt enc
Definition: aes.h:74
unsigned char iv[AES_BLOCKSIZE]
Definition: aes.h:76
~AES256Decrypt()
Definition: aes.cpp:65
AES256Decrypt(const unsigned char key[32])
Definition: aes.cpp:60
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
Definition: aes.cpp:70
AES256_ctx ctx
Definition: aes.h:58
AES256Encrypt(const unsigned char key[32])
Definition: aes.cpp:45
~AES256Encrypt()
Definition: aes.cpp:50
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
Definition: aes.cpp:55
AES256_ctx ctx
Definition: aes.h:46
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
Definition: ctaes.c:501
void AES256_encrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
Definition: ctaes.c:542
void AES256_init(AES256_ctx *ctx, const unsigned char *key32)
Definition: ctaes.c:538
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
Definition: ctaes.c:550
void AES128_decrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
Definition: ctaes.c:509
void AES128_init(AES128_ctx *ctx, const unsigned char *key16)
Definition: ctaes.c:497
void * memcpy(void *a, const void *b, size_t c)
#define T(expected, seed, data)