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