algorithms.py: Support SHA512/224 and SHA512/256.
[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
28'''.split()
29streamciphers = '''
30rc4 seal
31'''.split()
3f4f64b8 32latindances = '''
f9041075
MW
33salsa20 salsa20/12 salsa20/8
34salsa20-ietf salsa20/12-ietf salsa20/8-ietf
35xsalsa20 xsalsa20/12 xsalsa20/8
36chacha20 chacha12 chacha8
37chacha20-ietf chacha12-ietf chacha8-ietf
38xchacha20 xchacha12 xchacha8
3f4f64b8
MW
39'''.split()
40streamciphers += map(lambda s: s.translate(None, '/'), latindances)
d7ab1bab 41hashes = '''
42md2 md4 md5 tiger has160
cae28129 43sha sha224 sha256 sha512/224 sha512/256 sha384 sha512
d7ab1bab 44rmd128 rmd160 rmd256 rmd320
45whirlpool whirlpool256
46'''.split()
47hmodes = '''
48mgf hmac
49'''.split()
50
51print '/* algorithms.h [generated] */'
52print
53
54for i in prps:
a7f2e389 55 print '#include <catacomb/%s.h>' % i.replace('/', '-')
d7ab1bab 56 for j in pmodes:
a7f2e389 57 print '#include <catacomb/%s-%s.h>' % (i.replace('/', '-'), j)
d7ab1bab 58for i in streamciphers:
a7f2e389 59 print '#include <catacomb/%s.h>' % i.replace('/', '-')
d7ab1bab 60print
61for i in hashes:
a7f2e389 62 print '#include <catacomb/%s.h>' % i.replace('/', '-')
d7ab1bab 63 for j in hmodes:
a7f2e389 64 print '#include <catacomb/%s-%s.h>' % (i.replace('/', '-'), j)
d7ab1bab 65print
66
03ed9abb 67print '#define PRPS(_) \\'
d7ab1bab 68for i in prps:
eb0f76ed
MW
69 print '\t_(%s, %s) \\' % (i.upper(), i)
70print '\t/* end */'
03ed9abb
MW
71print
72
73print '#define RNGS(_) \\'
74for i in (cross(prps, ['ofb', 'counter'])):
a7f2e389
MW
75 print ('\t_("%(prim)s-%(mode)s", %(primid)s_keysz, ' +
76 '%(primid)s_%(mode)srand, RNG_PLAIN, 0) \\') % \
77 {'prim': i[0], 'mode': i[1],
78 'primid': i[0].replace('-', '_').replace('/', '_')}
03ed9abb 79for i in (cross(hashes, 'mgf')):
a7f2e389
MW
80 print ('\t_("%(prim)s-%(mode)s", %(primid)s_%(mode)skeysz, ' +
81 '%(primid)s_%(mode)srand, RNG_PLAIN, 0) \\') % \
82 {'prim': i[0], 'mode': i[1],
83 'primid': i[0].replace('-', '_').replace('/', '_')}
3f4f64b8 84print '\t_("rc4", rc4_keysz, rc4_rand, 0, 0) \\'
34825452 85print '\t_("seal", seal_keysz, seal_rand, RNG_SEAL, 0) \\'
3f4f64b8
MW
86for i in latindances:
87 for r in ['salsa20', 'xsalsa20', 'chacha', 'xchacha']:
88 if i.startswith(r):
89 root = r
90 break
91 else:
92 raise ValueError, 'failed to find root name for %s' % i
f9041075 93 if i.endswith('-ietf'): root += '_ietf'
3f4f64b8 94 print ('\t_("%(name)s", %(root)s_keysz, %(id)s_rand, ' +
34825452 95 'RNG_LATIN, %(ROOT)s_NONCESZ) \\') % \
f9041075 96 {'name': i, 'id': i.translate(None, '/').replace('-', '_'),
3f4f64b8 97 'root': root, 'ROOT': root.upper()}
eb0f76ed 98print '\t/* end */'
d7ab1bab 99print