File: _gen_files.py

package info (click to toggle)
python-passlib 1.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,184 kB
  • sloc: python: 26,132; makefile: 7
file content (142 lines) | stat: -rw-r--r-- 4,227 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""passlib.utils.scrypt._gen_files - meta script that generates _salsa.py"""

import os

_SALSA_OPS = [
    # row = (target idx, source idx 1, source idx 2, rotate)
    # interpreted as salsa operation over uint32...
    #   target = (source1+source2)<<rotate
    ##/* Operate on columns. */
    ##define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
    ##x[ 4] ^= R(x[ 0]+x[12], 7);  x[ 8] ^= R(x[ 4]+x[ 0], 9);
    ##x[12] ^= R(x[ 8]+x[ 4],13);  x[ 0] ^= R(x[12]+x[ 8],18);
    (4, 0, 12, 7),
    (8, 4, 0, 9),
    (12, 8, 4, 13),
    (0, 12, 8, 18),
    ##x[ 9] ^= R(x[ 5]+x[ 1], 7);  x[13] ^= R(x[ 9]+x[ 5], 9);
    ##x[ 1] ^= R(x[13]+x[ 9],13);  x[ 5] ^= R(x[ 1]+x[13],18);
    (9, 5, 1, 7),
    (13, 9, 5, 9),
    (1, 13, 9, 13),
    (5, 1, 13, 18),
    ##x[14] ^= R(x[10]+x[ 6], 7);  x[ 2] ^= R(x[14]+x[10], 9);
    ##x[ 6] ^= R(x[ 2]+x[14],13);  x[10] ^= R(x[ 6]+x[ 2],18);
    (14, 10, 6, 7),
    (2, 14, 10, 9),
    (6, 2, 14, 13),
    (10, 6, 2, 18),
    ##x[ 3] ^= R(x[15]+x[11], 7);  x[ 7] ^= R(x[ 3]+x[15], 9);
    ##x[11] ^= R(x[ 7]+x[ 3],13);  x[15] ^= R(x[11]+x[ 7],18);
    (3, 15, 11, 7),
    (7, 3, 15, 9),
    (11, 7, 3, 13),
    (15, 11, 7, 18),
    ##/* Operate on rows. */
    ##x[ 1] ^= R(x[ 0]+x[ 3], 7);  x[ 2] ^= R(x[ 1]+x[ 0], 9);
    ##x[ 3] ^= R(x[ 2]+x[ 1],13);  x[ 0] ^= R(x[ 3]+x[ 2],18);
    (1, 0, 3, 7),
    (2, 1, 0, 9),
    (3, 2, 1, 13),
    (0, 3, 2, 18),
    ##x[ 6] ^= R(x[ 5]+x[ 4], 7);  x[ 7] ^= R(x[ 6]+x[ 5], 9);
    ##x[ 4] ^= R(x[ 7]+x[ 6],13);  x[ 5] ^= R(x[ 4]+x[ 7],18);
    (6, 5, 4, 7),
    (7, 6, 5, 9),
    (4, 7, 6, 13),
    (5, 4, 7, 18),
    ##x[11] ^= R(x[10]+x[ 9], 7);  x[ 8] ^= R(x[11]+x[10], 9);
    ##x[ 9] ^= R(x[ 8]+x[11],13);  x[10] ^= R(x[ 9]+x[ 8],18);
    (11, 10, 9, 7),
    (8, 11, 10, 9),
    (9, 8, 11, 13),
    (10, 9, 8, 18),
    ##x[12] ^= R(x[15]+x[14], 7);  x[13] ^= R(x[12]+x[15], 9);
    ##x[14] ^= R(x[13]+x[12],13);  x[15] ^= R(x[14]+x[13],18);
    (12, 15, 14, 7),
    (13, 12, 15, 9),
    (14, 13, 12, 13),
    (15, 14, 13, 18),
]


def main():
    target = os.path.join(os.path.dirname(__file__), "_salsa.py")
    with open(target, "w") as fh:
        VNAMES = ["v%d" % i for i in range(16)]

        PAD = " " * 4
        TLIST = ", ".join("b%d" % i for i in range(16))
        VLIST = ", ".join(VNAMES)
        kwds = dict(
            VLIST=VLIST,
            TLIST=TLIST,
        )

        fh.write(
            '''\
    """passlib.utils.scrypt._salsa - salsa 20/8 core, autogenerated by _gen_salsa.py"""
    #=================================================================
    # salsa function
    #=================================================================

    def salsa20(input):
        \"""apply the salsa20/8 core to the provided input

        :args input: input list containing 16 32-bit integers
        :returns: result list containing 16 32-bit integers
        \"""

        {TLIST} = input
        {VLIST} = \\
            {TLIST}

        i = 0
        while i < 4:
    '''.format(**kwds)
        )

        for idx, (target, source1, source2, rotate) in enumerate(_SALSA_OPS):
            fh.write(
                """\
            # salsa op %(idx)d: [%(it)d] ^= ([%(is1)d]+[%(is2)d])<<<%(rot1)d
            t = (%(src1)s + %(src2)s) & 0xffffffff
            %(dst)s ^= ((t & 0x%(rmask)08x) << %(rot1)d) | (t >> %(rot2)d)

    """
                % dict(
                    idx=idx,
                    is1=source1,
                    is2=source2,
                    it=target,
                    src1=VNAMES[source1],
                    src2=VNAMES[source2],
                    dst=VNAMES[target],
                    rmask=(1 << (32 - rotate)) - 1,
                    rot1=rotate,
                    rot2=32 - rotate,
                )
            )

        fh.write("""\
            i += 1

    """)

        for idx in range(16):
            fh.write(PAD + "b%d = (b%d + v%d) & 0xffffffff\n" % (idx, idx, idx))

        fh.write(
            """\

        return {TLIST}

    #=================================================================
    # eof
    #=================================================================
    """.format(**kwds)
        )


if __name__ == "__main__":
    main()