7f31e604db30fa6bc19230228a46ff5be603c740
[catacomb-python] / algorithms.py
1 ## -*-python-*-
2
3 def cross(*seq):
4 if not len(seq):
5 return [(),]
6 x = seq[0]
7 if type(x) is not tuple and type(x) is not list:
8 x = x,
9 r = []
10 for i in x:
11 for j in cross(*seq[1:]):
12 r.append((i,) + j)
13 return r
14
15 prps = '''
16 des desx des3 mars
17 idea safer safersk
18 blowfish twofish
19 tea xtea
20 rc2 rc5
21 skipjack
22 cast128 cast256
23 square rijndael rijndael192 rijndael256
24 serpent noekeon
25 '''.split()
26 pmodes = '''
27 ecb cbc cfb ofb counter
28 cmac pmac1
29 '''.split()
30 streamciphers = '''
31 rc4 seal
32 '''.split()
33 latindances = '''
34 salsa20 salsa20/12 salsa20/8
35 salsa20-ietf salsa20/12-ietf salsa20/8-ietf
36 xsalsa20 xsalsa20/12 xsalsa20/8
37 chacha20 chacha12 chacha8
38 chacha20-ietf chacha12-ietf chacha8-ietf
39 xchacha20 xchacha12 xchacha8
40 '''.split()
41 streamciphers += map(lambda s: s.translate(None, '/'), latindances)
42 hashes = '''
43 md2 md4 md5 tiger has160
44 sha sha224 sha256 sha512/224 sha512/256 sha384 sha512
45 rmd128 rmd160 rmd256 rmd320
46 whirlpool whirlpool256
47 sha3-224 sha3-256 sha3-384 sha3-512
48 '''.split()
49 hmodes = '''
50 mgf hmac
51 '''.split()
52
53 print '/* algorithms.h [generated] */'
54 print
55
56 for i in prps:
57 print '#include <catacomb/%s.h>' % i.replace('/', '-')
58 for j in pmodes:
59 print '#include <catacomb/%s-%s.h>' % (i.replace('/', '-'), j)
60 for i in streamciphers:
61 print '#include <catacomb/%s.h>' % i.replace('/', '-')
62 print
63 for i in hashes:
64 print '#include <catacomb/%s.h>' % i.replace('/', '-')
65 for j in hmodes:
66 print '#include <catacomb/%s-%s.h>' % (i.replace('/', '-'), j)
67 print
68
69 print '#define PRPS(_) \\'
70 for i in prps:
71 print '\t_(%s, %s) \\' % (i.upper(), i)
72 print '\t/* end */'
73 print
74
75 print '#define RNGS(_) \\'
76 for i in (cross(prps, ['ofb', 'counter'])):
77 print ('\t_("%(prim)s-%(mode)s", %(primid)s_keysz, ' +
78 '%(primid)s_%(mode)srand, RNG_PLAIN, 0) \\') % \
79 {'prim': i[0], 'mode': i[1],
80 'primid': i[0].replace('-', '_').replace('/', '_')}
81 for i in (cross(hashes, 'mgf')):
82 print ('\t_("%(prim)s-%(mode)s", %(primid)s_%(mode)skeysz, ' +
83 '%(primid)s_%(mode)srand, RNG_PLAIN, 0) \\') % \
84 {'prim': i[0], 'mode': i[1],
85 'primid': i[0].replace('-', '_').replace('/', '_')}
86 print '\t_("rc4", rc4_keysz, rc4_rand, 0, 0) \\'
87 print '\t_("seal", seal_keysz, seal_rand, RNG_SEAL, 0) \\'
88 for i in latindances:
89 for r in ['salsa20', 'xsalsa20', 'chacha', 'xchacha']:
90 if i.startswith(r):
91 root = r
92 break
93 else:
94 raise ValueError, 'failed to find root name for %s' % i
95 if i.endswith('-ietf'): root += '_ietf'
96 print ('\t_("%(name)s", %(root)s_keysz, %(id)s_rand, ' +
97 'RNG_LATIN, %(ROOT)s_NONCESZ) \\') % \
98 {'name': i, 'id': i.translate(None, '/').replace('-', '_'),
99 'root': root, 'ROOT': root.upper()}
100 for i in [128, 256]:
101 print ('\t_("shake%(w)d", shake%(w)d_keysz, cshake%(w)d_rand, ' +
102 'RNG_SHAKE, 0) \\') % \
103 {'w': i}
104 print ('\t_("kmac%(w)d", kmac%(w)d_keysz, kmac%(w)d_rand, ' +
105 'RNG_KMAC, 0) \\') % \
106 {'w': i}
107 print '\t/* end */'
108 print