algorithms.py: Support the new blockcipher-based MAC modes.
[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
d7ab1bab 29'''.split()
30streamciphers = '''
31rc4 seal
32'''.split()
3f4f64b8 33latindances = '''
f9041075
MW
34salsa20 salsa20/12 salsa20/8
35salsa20-ietf salsa20/12-ietf salsa20/8-ietf
36xsalsa20 xsalsa20/12 xsalsa20/8
37chacha20 chacha12 chacha8
38chacha20-ietf chacha12-ietf chacha8-ietf
39xchacha20 xchacha12 xchacha8
3f4f64b8
MW
40'''.split()
41streamciphers += map(lambda s: s.translate(None, '/'), latindances)
d7ab1bab 42hashes = '''
43md2 md4 md5 tiger has160
cae28129 44sha sha224 sha256 sha512/224 sha512/256 sha384 sha512
d7ab1bab 45rmd128 rmd160 rmd256 rmd320
46whirlpool whirlpool256
6bd22b53 47sha3-224 sha3-256 sha3-384 sha3-512
d7ab1bab 48'''.split()
49hmodes = '''
50mgf hmac
51'''.split()
52
53print '/* algorithms.h [generated] */'
54print
55
56for i in prps:
a7f2e389 57 print '#include <catacomb/%s.h>' % i.replace('/', '-')
d7ab1bab 58 for j in pmodes:
a7f2e389 59 print '#include <catacomb/%s-%s.h>' % (i.replace('/', '-'), j)
d7ab1bab 60for i in streamciphers:
a7f2e389 61 print '#include <catacomb/%s.h>' % i.replace('/', '-')
d7ab1bab 62print
63for i in hashes:
a7f2e389 64 print '#include <catacomb/%s.h>' % i.replace('/', '-')
d7ab1bab 65 for j in hmodes:
a7f2e389 66 print '#include <catacomb/%s-%s.h>' % (i.replace('/', '-'), j)
d7ab1bab 67print
68
03ed9abb 69print '#define PRPS(_) \\'
d7ab1bab 70for i in prps:
eb0f76ed
MW
71 print '\t_(%s, %s) \\' % (i.upper(), i)
72print '\t/* end */'
03ed9abb
MW
73print
74
75print '#define RNGS(_) \\'
76for i in (cross(prps, ['ofb', 'counter'])):
a7f2e389
MW
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('/', '_')}
03ed9abb 81for i in (cross(hashes, 'mgf')):
a7f2e389
MW
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('/', '_')}
3f4f64b8 86print '\t_("rc4", rc4_keysz, rc4_rand, 0, 0) \\'
34825452 87print '\t_("seal", seal_keysz, seal_rand, RNG_SEAL, 0) \\'
3f4f64b8
MW
88for 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
f9041075 95 if i.endswith('-ietf'): root += '_ietf'
3f4f64b8 96 print ('\t_("%(name)s", %(root)s_keysz, %(id)s_rand, ' +
34825452 97 'RNG_LATIN, %(ROOT)s_NONCESZ) \\') % \
f9041075 98 {'name': i, 'id': i.translate(None, '/').replace('-', '_'),
3f4f64b8 99 'root': root, 'ROOT': root.upper()}
6bd22b53
MW
100for 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}
eb0f76ed 107print '\t/* end */'
d7ab1bab 108print