-
Notifications
You must be signed in to change notification settings - Fork 11
/
genvec.cpp
443 lines (398 loc) · 10.1 KB
/
genvec.cpp
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 22/11/2013
*/
#include <iomanip>
#include "genvec.h"
#include "aes.h"
/* AES based random number generator */
static unsigned char key[16] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 2, 3, 8, 4 };
static unsigned char rnd[16] = { 2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9, 0, 4, 5 };
static aes_encrypt_ctx ctx[1];
static int r_cnt = -1; /* initialised negative to trigger AES keying */
void rand_update(void)
{
if(r_cnt < 0)
aes_encrypt_key(key, 16, ctx);
/* OFB stream cipher for randomness */
aes_encrypt(rnd, rnd, ctx);
/* number of valid random bytes in buffer */
r_cnt = 16;
return;
}
/* return one pseudo random byte */
unsigned char rand8(void)
{
if(r_cnt <= 0)
rand_update();
return rnd[--r_cnt];
}
/* fill a byte array with pseudo random values */
void block_rndfill(unsigned char l[], size_t len)
{
unsigned long i;
for(i = 0; i < len; ++i)
l[i] = rand8();
}
/* input a decimal number from a C string */
char *decimal_in(const char *s, int64_t *n)
{
int g = -1;
*n = 0;
if(*s == '-')
++s;
else
g = 1;
while(isdigit(*s))
*n = 10 * *n + g * (*s++ - '0');
return (char*)s;
}
int to_hex(char ch)
{
return (isdigit(ch) ? 0 : 9) + (ch & 0x0f);
}
/* input a hexadecimal number from a C string */
char *hexadecimal_in(const char *s, int64_t *n)
{
*n = 0;
while(isxdigit(*s))
*n = (*n << 4) + to_hex(*s++);
return (char*)s;
}
/* input an 8-bit byte from two charcters in a C string */
char *hexbyte_in(const char *s, unsigned char *n)
{
if(isxdigit(*s) && isxdigit(*(s + 1)))
{
*n = (to_hex(*s) << 4) | to_hex(*(s + 1));
return (char*)(s + 2);
}
else
return 0;
}
uint64_t le_bytes_to_nbr(const unsigned char x[], size_t len)
{
uint64_t t = 0;
x += len;
while(len--)
t = 256 * t + *--x;
return t;
}
uint64_t be_bytes_to_nbr(const unsigned char x[], size_t len)
{
uint64_t t = 0;
while(len--)
t = 256 * t + *x++;
return t;
}
/*
input strings are comma separated values which are either hexadecimal
sequences or 'bracket terms' consisting of up to five comma separated
numerical values within brackets. This routine parses the latter
bracket terms and puts the number of values obtained in cnt[0] and
the values in cnt[1] .. cnt[5]. Numbers are hexadecimal unless they
are preceeded by a '#', in which case they are decimal.
*/
int get_loop_vars(char **cp, int64_t cnt[], int hex_flag)
{
char *p = *cp;
int i, retv = 0;
/* remove any leading space */
while(*p && (*p == ' ' || *p == '\t'))
++p;
/* parse up to closing bracket or a maximum of 5 inputs */
for(i = 1 ; *p != ')' && i < 6 ; ++i)
{
if(*p == '#')
p = decimal_in(p + 1, cnt + i);
else
p = hexadecimal_in(p, cnt + i);
while(*p && (*p == ' ' || *p == '\t' || *p == ','))
++p;
}
if(*p == ')' && i < 7) /* good input */
cnt[0] = retv = i - 1;
else /* bad input - remove to next closing bracket */
{
while(*p && *p != ')')
++p;
if(*p)
++p;
}
*cp = p;
return retv;
}
int init_sequence(char **cp, byte_array &arr, int64_t cnt[5])
{
int retv, i;
(*cp)++; /* step past opening bracket */
/* input the values in the bracket term */
retv = get_loop_vars(cp, cnt, 1);
switch(retv)
{
case 1:
/* (nn) - nn random bytes */
i = (int)cnt[1];
while(i--)
arr.push_back(rand8());
/* step past closing bracket */
(*cp)++;
break;
case 2:
/* (nn,hh) - nn bytes each of value hh */
cnt[3] = 0;
case 3:
/* (nn,hh,hi) - nn bytes of value hh, hh + hi, hh + 2 * hi, ... */
for(i = 0; i < cnt[1]; ++i)
{
arr.push_back((unsigned char)(cnt[2]));
cnt[2] += cnt[3];
}
/* step past closing bracket */
(*cp)++;
break;
case 4:
/* (nn,v1,v2,v3=1..4) - signal with negative cnt[5] value */
cnt[5] = -cnt[4];
case 5:
/* (nn,v1,v2,v3,v4) */
/* signal a sequence of values */
return 2;
default:
/* signal no value input */
return 0;
}
return 1;
}
void v_rec::add_to_record(const char *c)
{
const char *cp = c + strlen(c) - 1;
/* remove extraneous characters at end of input */
while(*cp <= 0x20 || *cp >= 0x7f)
--cp;
if(cp++ < c)
return;
/* if a string has not been allocated or it has but is not long enough */
if(!rp || len < strlen(rp) + strlen(c) + 1)
{
/* set a new maximum length and allocate a new string */
len = (int)((rp ? (unsigned int)strlen(rp) : 0) + cp - c + 80);
char *p = new char[len];
*p = '\0';
/* if necessary copy old content into it */
if(rp)
{
strcpy(p, rp);
delete[]rp;
}
rp = p;
}
/* if there is content in the string and the new value is not a */
/* continuation of a hexadecimal sequence, insert a comma */
if(*(rp) && *c != ',' && rp[strlen(rp) - 1] != ',' &&
(!isxdigit(rp[strlen(rp) - 1]) || !isxdigit(*c)))
strcat(rp, ",");
/* add the new content */
strncat(rp, c, cp - c);
}
int v_rec::get_value(byte_array &arr)
{
char *p = rp + pos, chr[1];
int retv = 0, i;
arr.clear();
if(rp == 0)
return 0;
/* remove white space and a ',' delimiter if present */
while(*p && (*p == ' ' || *p == '\t'))
++p;
if(*p == ',')
++p;
if(*p)
{
if(*p == '=')
{ /* use previous ciphertext */
++p;
retv = 1 | ctx_copy;
}
else if(isxdigit(*p) && isxdigit(*(p + 1)))
{ /* input a hexadecimal byte sequence */
while(hexbyte_in(p, (unsigned char*)chr))
{
arr.push_back(chr[0]);
p += 2;
}
retv = 1;
}
else if(*p == '#')
{ /* input a decimal value and assemble into */
/* little endian byte sequence */
int64_t num;
int f = 0;
if(*++p == '>' || *p == '<')
if(*p++ == '>')
f = 1;
p = decimal_in(p, &num);
while(num)
{
arr.push_back((unsigned char)(num & 0xff));
num >>= 8;
}
if(f)
arr = byte_array(arr.rbegin(), arr.rend());
retv = 1;
}
else if(*p == '(') /* initialise for a bracket term */
retv = init_sequence(&p, arr, cnt);
/* after the initialisation of a bracket term, the */
/* parse point is positioned on the final bracket */
if(retv == 2 || *p == ')')
{
if(cnt[1] == 0) /* if the sequence has finished */
{ /* go to next input value */
/* leave value in len untouched unless there is */
/* actual input because we may need the prior */
/* length in case we have to copy the previous */
/* ciphertext into the next plaintext */
pos = (unsigned int)(++p - rp);
retv = get_value(arr);
}
else
{
(cnt[1])--; /* reduce remaining item count */
i = (int)(cnt[2]); /* normal length count */
if(cnt[5] == -1 || cnt[5] == -2)
{
/* assemble sequential values v1 + n * v2 */
/* (nn,v1,v2,1) - little endian byte array */
/* (nn,v1,v2,2) - big endian byte array */
int64_t x = cnt[2];
cnt[2] += cnt[3];
while(x)
{
arr.push_back((unsigned char)x);
x >>= 8;
}
if(cnt[5] == -2)
arr = byte_array(arr.rbegin(), arr.rend());
}
else if(cnt[5] == -3)
{
/* (nn,v1,v2,3) - assemble random array of */
/* bytes of increasing length v1 + n * v2 */
while(i--)
arr.push_back(rand8());
cnt[2] += cnt[3];
}
else if(cnt[5] == -4)
{
/* (nn,v1,v2,4) - assemble random array of */
/* bytes of random length v1 <= len <= v2 */
uint64_t t = 0;
block_rndfill((unsigned char*)&t, 4);
i += (int)(((cnt[3] + 1 - cnt[2]) * t) >> 32);
while(i--)
arr.push_back(rand8());
}
else
{
/* (nn,v1,v2,h1,h2) - assemble random array */
/* bytes of increasing length v1 + n * v2 */
/* in which bytes also increment in value */
/* h1 + n * h2 withing each array generated */
int j = (int)(cnt[4]);
while(i--)
{
arr.push_back((unsigned char)j);
j += (int)(cnt[5]);
}
cnt[2] += cnt[3];
}
retv = 1;
}
}
}
/* if a value is returned signal its length */
next_pos = (unsigned int)(p - rp);
return retv;
}
bool input_test_def(std::fstream &inf, test_def &v, uint64_t *vec_no)
{
std::string line, ty;
v.reset();
/* process lines in template file */
bool in_vec = false;
while(!inf.eof())
{
std::getline(inf, line);
unsigned char *t;
if(!line.empty())
{
ty = v.set_tag(line.c_str());
if(ty == "VEC")
{
size_t l;
t = v["VEC"].get_value(&l);
*vec_no = be_bytes_to_nbr(t, l);
}
else if(ty == "END" || ty == "GEN")
{
return in_vec;
}
else if(ty == "TAG" && line.find("?") != std::string::npos)
{
if(!v.is_key("TGL") || v.is_empty("TGL"))
{
v.register_tag("TGL");
std::string t = v.set_tag("TGL #16");
}
v.register_tag(ty.c_str());
}
else
{
v.register_tag(ty.c_str());
in_vec = true;
}
}
else if(in_vec)
return true;
}
return false;
}
/* output a byte array preceeded by a descriptor */
void hex_out(std::fstream& outf, const char * desc, const unsigned char buf[], size_t len)
{
if(len == 0)
outf << std::endl << desc;
else
for(unsigned int i = 0; i < len; ++i)
{
if(i % 32 == 0)
outf << std::endl << desc << ' ';
outf << std::setw(2) << std::setfill('0') << std::hex << (unsigned int)buf[i];
}
}
void test_def::vector_out(std::fstream& outf, uint64_t vec_no)
{
outf << std::endl << std::dec << "VEC " << (int)vec_no;
for(auto p = tag_order.cbegin() ; p != tag_order.cend() ; ++p )
{
if (*p != "TGL")
{
size_t len;
const unsigned char *con(tags[*p].get_value(&len));
hex_out(outf, p->c_str(), con, len);
}
}
outf << std::endl;
}