-
Notifications
You must be signed in to change notification settings - Fork 15
/
crypto_util.h
270 lines (223 loc) · 10.2 KB
/
crypto_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef TROB_CRYPTO_URIL_H
#define TROB_CRYPTO_URIL_H
//===- CryptoUtils.h - Cryptographically Secure Pseudo-Random
// Generator------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains includes and defines for the AES CTR PRNG
// The AES implementation has been derived and adapted
// from libtomcrypt (see http://libtom.org)
// Created on: 22 juin 2012
// Author(s): jrinaldini, pjunod
//===----------------------------------------------------------------------===//
#ifndef LLVM_CryptoUtils_H
#define LLVM_CryptoUtils_H
#include <llvm/Support/ManagedStatic.h>
#include <stdint.h>
#include <cstdio>
#include <string>
using namespace llvm;
namespace trob {
class CryptoUtils;
extern ManagedStatic<CryptoUtils> cryptoutils;
#define BYTE(x, n) (((x) >> (8 * (n))) & 0xFF)
#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
defined(INTEL_CC)
#ifndef ENDIAN_LITTLE
#define ENDIAN_LITTLE
#endif
#define ENDIAN_32BITWORD
#define UNALIGNED
#elif defined(__alpha)
#ifndef ENDIAN_LITTLE
#define ENDIAN_LITTLE
#endif
#define ENDIAN_64BITWORD
#elif defined(__x86_64__)
#ifndef ENDIAN_LITTLE
#define ENDIAN_LITTLE
#endif
#define ENDIAN_64BITWORD
#define UNALIGNED
#elif(defined(__R5900) || defined(R5900) || defined(__R5900__)) && \
(defined(_mips) || defined(__mips__) || defined(mips))
#ifndef ENDIAN_LITTLE
#define ENDIAN_LITTLE
#endif
#define ENDIAN_64BITWORD
#elif defined(__sparc)
#ifndef ENDIAN_BIG
#define ENDIAN_BIG
#endif
#if defined(__arch64__)
#define ENDIAN_64BITWORD
#else
#define ENDIAN_32BITWORD
#endif
#endif
#if defined(__BIG_ENDIAN__) || defined(_BIG_ENDIAN)
#define ENDIAN_BIG
#endif
#if !defined(ENDIAN_BIG) && !defined(ENDIAN_LITTLE)
#error \
"Unknown endianness of the compilation platform, check this header aes_encrypt.h"
#endif
#ifdef ENDIAN_LITTLE
#define STORE32H(y, x) \
{ \
(y)[0] = (uint8_t)(((x) >> 24) & 0xFF); \
(y)[1] = (uint8_t)(((x) >> 16) & 0xFF); \
(y)[2] = (uint8_t)(((x) >> 8) & 0xFF); \
(y)[3] = (uint8_t)(((x) >> 0) & 0xFF); \
}
#define LOAD32H(x, y) \
{ \
(x) = ((uint32_t)((y)[0] & 0xFF) << 24) | \
((uint32_t)((y)[1] & 0xFF) << 16) | \
((uint32_t)((y)[2] & 0xFF) << 8) | ((uint32_t)((y)[3] & 0xFF) << 0); \
}
#define LOAD64H(x, y) \
{ \
(x) = ((uint64_t)((y)[0] & 0xFF) << 56) | \
((uint64_t)((y)[1] & 0xFF) << 48) | \
((uint64_t)((y)[2] & 0xFF) << 40) | \
((uint64_t)((y)[3] & 0xFF) << 32) | \
((uint64_t)((y)[4] & 0xFF) << 24) | \
((uint64_t)((y)[5] & 0xFF) << 16) | \
((uint64_t)((y)[6] & 0xFF) << 8) | ((uint64_t)((y)[7] & 0xFF) << 0); \
}
#define STORE64H(y, x) \
{ \
(y)[0] = (uint8_t)(((x) >> 56) & 0xFF); \
(y)[1] = (uint8_t)(((x) >> 48) & 0xFF); \
(y)[2] = (uint8_t)(((x) >> 40) & 0xFF); \
(y)[3] = (uint8_t)(((x) >> 32) & 0xFF); \
(y)[4] = (uint8_t)(((x) >> 24) & 0xFF); \
(y)[5] = (uint8_t)(((x) >> 16) & 0xFF); \
(y)[6] = (uint8_t)(((x) >> 8) & 0xFF); \
(y)[7] = (uint8_t)(((x) >> 0) & 0xFF); \
}
#endif /* ENDIAN_LITTLE */
#ifdef ENDIAN_BIG
#define STORE32H(y, x) \
{ \
(y)[3] = (uint8_t)(((x) >> 24) & 0xFF); \
(y)[2] = (uint8_t)(((x) >> 16) & 0xFF); \
(y)[1] = (uint8_t)(((x) >> 8) & 0xFF); \
(y)[0] = (uint8_t)(((x) >> 0) & 0xFF); \
}
#define STORE64H(y, x) \
{ \
(y)[7] = (uint8_t)(((x) >> 56) & 0xFF); \
(y)[6] = (uint8_t)(((x) >> 48) & 0xFF); \
(y)[5] = (uint8_t)(((x) >> 40) & 0xFF); \
(y)[4] = (uint8_t)(((x) >> 32) & 0xFF); \
(y)[3] = (uint8_t)(((x) >> 24) & 0xFF); \
(y)[2] = (uint8_t)(((x) >> 16) & 0xFF); \
(y)[1] = (uint8_t)(((x) >> 8) & 0xFF); \
(y)[0] = (uint8_t)(((x) >> 0) & 0xFF); \
}
#define LOAD32H(x, y) \
{ \
(x) = ((uint32_t)((y)[3] & 0xFF) << 24) | \
((uint32_t)((y)[2] & 0xFF) << 16) | \
((uint32_t)((y)[1] & 0xFF) << 8) | ((uint32_t)((y)[0] & 0xFF) << 0); \
}
#define LOAD64H(x, y) \
{ \
(x) = ((uint64_t)((y)[7] & 0xFF) << 56) | \
((uint64_t)((y)[6] & 0xFF) << 48) | \
((uint64_t)((y)[5] & 0xFF) << 40) | \
((uint64_t)((y)[4] & 0xFF) << 32) | \
((uint64_t)((y)[3] & 0xFF) << 24) | \
((uint64_t)((y)[2] & 0xFF) << 16) | \
((uint64_t)((y)[1] & 0xFF) << 8) | ((uint64_t)((y)[0] & 0xFF) << 0); \
}
#endif /* ENDIAN_BIG */
#define AES_TE0(x) AES_PRECOMP_TE0[(x)]
#define AES_TE1(x) AES_PRECOMP_TE1[(x)]
#define AES_TE2(x) AES_PRECOMP_TE2[(x)]
#define AES_TE3(x) AES_PRECOMP_TE3[(x)]
#define AES_TE4_0(x) AES_PRECOMP_TE4_0[(x)]
#define AES_TE4_1(x) AES_PRECOMP_TE4_1[(x)]
#define AES_TE4_2(x) AES_PRECOMP_TE4_2[(x)]
#define AES_TE4_3(x) AES_PRECOMP_TE4_3[(x)]
#define CryptoUtils_POOL_SIZE (0x1 << 17) // 2^17
#define DUMP(x, l, s) \
fprintf(stderr, "%s :", (s)); \
for (int ii = 0; ii < (l); ii++) { \
fprintf(stderr, "%02hhX", *((x) + ii)); \
} \
fprintf(stderr, "\n");
// SHA256
/* Various logical functions */
#define Ch(x, y, z) (z ^ (x &(y ^ z)))
#define Maj(x, y, z) (((x | y) & z) | (x &y))
#define S(x, n) RORc((x), (n))
#define R1(x, n) (((x) & 0xFFFFFFFFUL) >> (n))
#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R1(x, 3))
#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R1(x, 10))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define RND(a, b, c, d, e, f, g, h, i, ki) \
t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
t1 = Sigma0(a) + Maj(a, b, c); \
d += t0; \
h = t0 + t1;
#define RORc(x, y) \
(((((unsigned long)(x) & 0xFFFFFFFFUL) >> (unsigned long)((y) & 31)) | \
((unsigned long)(x) << (unsigned long)(32 - ((y) & 31)))) & \
0xFFFFFFFFUL)
class CryptoUtils {
public:
CryptoUtils();
~CryptoUtils();
char *get_seed();
void get_bytes(char *buffer, const int len);
char get_char();
bool prng_seed(const std::string seed);
// Returns a uniformly distributed 8-bit value
uint8_t get_uint8_t();
// Returns a uniformly distributed 32-bit value
uint32_t get_uint32_t();
// Returns an integer uniformly distributed on [0, max[
uint32_t get_range(const uint32_t max);
// Returns a uniformly distributed 64-bit value
uint64_t get_uint64_t();
// Scramble a 32-bit value depending on a 128-bit value
unsigned scramble32(const unsigned in, const char key[16]);
int sha256(const char *msg, unsigned char *hash);
private:
uint32_t ks[44];
char key[16];
char ctr[16];
char pool[CryptoUtils_POOL_SIZE];
uint32_t idx;
std::string seed;
bool seeded;
typedef struct {
uint64_t length;
uint32_t state[8], curlen;
unsigned char buf[64];
} sha256_state;
void aes_compute_ks(uint32_t *ks, const char *k);
void aes_encrypt(char *out, const char *in, const uint32_t *ks);
bool prng_seed();
void inc_ctr();
void populate_pool();
int sha256_done(sha256_state *md, unsigned char *out);
int sha256_init(sha256_state *md);
static int sha256_compress(sha256_state *md, unsigned char *buf);
int sha256_process(sha256_state *md, const unsigned char *in,
unsigned long inlen);
};
}
#endif // LLVM_CryptoUtils_H
#endif