-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvectors2h.py
65 lines (53 loc) · 1.22 KB
/
vectors2h.py
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
import json
def hexy(s):
return ', '.join(['0x%02x' % c for c in s])
def produce_c_vectors(vectors):
entries = []
for v in vectors:
entry = '''
{
%d, %d, %d, %d, %d, %d,
// key
{%s},
// nonce
{%s},
// header
{%s},
// plaintext
{%s},
// cyphertext
{%s},
// tag
{%s},
}''' % tuple([len(h) for h in v] + [hexy(h) for h in v])
entries.append(entry)
template = '''
typedef struct
{
int keylen;
int noncelen;
int headerlen;
int ptlen;
int ctlen;
int taglen;
uint8_t key[16];
uint8_t nonce[256];
uint8_t header[256];
uint8_t pt[256];
uint8_t ct[256];
uint8_t tag[16];
} testvector_t;
const testvector_t testvectors[] =
{
%s
};
''' % (',\n'.join(entries))
return template
with open('vectors_eax_xtea.json', 'r') as f:
vectors = json.load(f)
with open('vectors_eax_xtea.h', 'w') as f:
f.write(produce_c_vectors(vectors))
with open('vectors_eax_aes.json', 'r') as f:
vectors = json.load(f)
with open('vectors_eax_aes.h', 'w') as f:
f.write(produce_c_vectors(vectors))