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 (238 lines) | stat: -rw-r--r-- 6,091 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""passlib.crypto._blowfish._gen_files - meta script that generates unrolled.py"""

import os
import textwrap


def varlist(name, count):
    return ", ".join(name + str(x) for x in range(count))


def indent_block(block, padding):
    """ident block of text"""
    lines = block.split("\n")
    return "\n".join(padding + line if line else "" for line in lines)


BFSTR = """\
                ((((S0[l >> 24] + S1[(l >> 16) & 0xff]) ^ S2[(l >> 8) & 0xff]) +
                  S3[l & 0xff]) & 0xffffffff)
""".strip()


def render_encipher(write, indent=0):
    for i in range(0, 15, 2):
        write(
            indent,
            """\
            # Feistel substitution on left word (round %(i)d)
            r ^= %(left)s ^ p%(i1)d

            # Feistel substitution on right word (round %(i1)d)
            l ^= %(right)s ^ p%(i2)d
        """,
            i=i,
            i1=i + 1,
            i2=i + 2,
            left=BFSTR,
            right=BFSTR.replace("l", "r"),
        )


def write_encipher_function(write, indent=0):
    write(
        indent,
        """\
        def encipher(self, l, r):
            \"""blowfish encipher a single 64-bit block encoded as two 32-bit ints\"""

            (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9,
              p10, p11, p12, p13, p14, p15, p16, p17) = self.P
            S0, S1, S2, S3 = self.S

            l ^= p0

            """,
    )
    render_encipher(write, indent + 1)

    write(
        indent + 1,
        """\

        return r ^ p17, l

        """,
    )


def write_expand_function(write, indent=0):
    write(
        indent,
        """\
        def expand(self, key_words):
            \"""unrolled version of blowfish key expansion\"""
            ##assert len(key_words) >= 18, "size of key_words must be >= 18"

            P, S = self.P, self.S
            S0, S1, S2, S3 = S

            #=============================================================
            # integrate key
            #=============================================================
        """,
    )
    for i in range(18):
        write(
            indent + 1,
            """\
            p%(i)d = P[%(i)d] ^ key_words[%(i)d]
        """,
            i=i,
        )
    write(
        indent + 1,
        """\

        #=============================================================
        # update P
        #=============================================================

        #------------------------------------------------
        # update P[0] and P[1]
        #------------------------------------------------
        l, r = p0, 0

        """,
    )

    render_encipher(write, indent + 1)

    write(
        indent + 1,
        """\

        p0, p1 = l, r = r ^ p17, l

        """,
    )

    for i in range(2, 18, 2):
        write(
            indent + 1,
            """\
            #------------------------------------------------
            # update P[%(i)d] and P[%(i1)d]
            #------------------------------------------------
            l ^= p0

            """,
            i=i,
            i1=i + 1,
        )

        render_encipher(write, indent + 1)

        write(
            indent + 1,
            """\
            p%(i)d, p%(i1)d = l, r = r ^ p17, l

            """,
            i=i,
            i1=i + 1,
        )

    write(
        indent + 1,
        """\

        #------------------------------------------------
        # save changes to original P array
        #------------------------------------------------
        P[:] = (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9,
          p10, p11, p12, p13, p14, p15, p16, p17)

        #=============================================================
        # update S
        #=============================================================

        for box in S:
            j = 0
            while j < 256:
                l ^= p0

        """,
    )

    render_encipher(write, indent + 3)

    write(
        indent + 3,
        """\

                box[j], box[j+1] = l, r = r ^ p17, l
                j += 2
        """,
    )


def main():
    target = os.path.join(os.path.dirname(__file__), "unrolled.py")
    with open(target, "w") as fh:

        def write(indent, msg, **kwds):
            literal = kwds.pop("literal", False)
            if kwds:
                msg %= kwds
            if not literal:
                msg = textwrap.dedent(msg.rstrip(" "))
            if indent:
                msg = indent_block(msg, " " * (indent * 4))
            fh.write(msg)

        write(
            0,
            """\
            \"""passlib.crypto._blowfish.unrolled - unrolled loop implementation of bcrypt,
            autogenerated by _gen_files.py

            currently this override the encipher() and expand() methods
            with optimized versions, and leaves the other base.py methods alone.
            \"""
            #=================================================================
            # imports
            #=================================================================
            # pkg
            from passlib.crypto._blowfish.base import BlowfishEngine as _BlowfishEngine
            # local
            __all__ = [
                "BlowfishEngine",
            ]
            #=================================================================
            #
            #=================================================================
            class BlowfishEngine(_BlowfishEngine):

            """,
        )

        write_encipher_function(write, indent=1)
        write_expand_function(write, indent=1)

        write(
            0,
            """\
                #=================================================================
                # eoc
                #=================================================================

            #=================================================================
            # eof
            #=================================================================
            """,
        )


if __name__ == "__main__":
    main()