Statistics
| Branch: | Tag: | Revision:

root / vncauthproxy / d3des.py @ b129b0c0

History | View | Annotate | Download (13.7 kB)

1 66d17b40 Apollon Oikonomopoulos
#!/usr/bin/env python
2 66d17b40 Apollon Oikonomopoulos
##
3 66d17b40 Apollon Oikonomopoulos
##  d3des.py - DES implementation
4 66d17b40 Apollon Oikonomopoulos
##
5 66d17b40 Apollon Oikonomopoulos
##  Copyright (c) 2009 by Yusuke Shinyama
6 66d17b40 Apollon Oikonomopoulos
##
7 66d17b40 Apollon Oikonomopoulos
8 66d17b40 Apollon Oikonomopoulos
# This is a Python rewrite of d3des.c by Richard Outerbridge.
9 66d17b40 Apollon Oikonomopoulos
#
10 66d17b40 Apollon Oikonomopoulos
# I referred to the original VNC viewer code for the changes that
11 66d17b40 Apollon Oikonomopoulos
# is necessary to maintain the exact behavior of the VNC protocol.
12 66d17b40 Apollon Oikonomopoulos
# Two constants and two functions were added to the original d3des
13 66d17b40 Apollon Oikonomopoulos
# code.  These added parts were written in Python and marked
14 66d17b40 Apollon Oikonomopoulos
# below.  I believe that the added parts do not make this program
15 66d17b40 Apollon Oikonomopoulos
# a "derivative work" of the VNC viewer (which is GPL'ed and
16 66d17b40 Apollon Oikonomopoulos
# written in C), but if there's any problem, let me know.
17 66d17b40 Apollon Oikonomopoulos
#
18 66d17b40 Apollon Oikonomopoulos
# Yusuke Shinyama (yusuke at cs dot nyu dot edu)
19 66d17b40 Apollon Oikonomopoulos
20 66d17b40 Apollon Oikonomopoulos
21 66d17b40 Apollon Oikonomopoulos
#  D3DES (V5.09) -
22 66d17b40 Apollon Oikonomopoulos
#
23 66d17b40 Apollon Oikonomopoulos
#  A portable, public domain, version of the Data Encryption Standard.
24 66d17b40 Apollon Oikonomopoulos
#
25 66d17b40 Apollon Oikonomopoulos
#  Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
26 66d17b40 Apollon Oikonomopoulos
#  Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
27 66d17b40 Apollon Oikonomopoulos
#  code;  Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
28 66d17b40 Apollon Oikonomopoulos
#  Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
29 66d17b40 Apollon Oikonomopoulos
#  for humouring me on.
30 66d17b40 Apollon Oikonomopoulos
#
31 66d17b40 Apollon Oikonomopoulos
#  Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
32 66d17b40 Apollon Oikonomopoulos
#  (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
33 66d17b40 Apollon Oikonomopoulos
#
34 66d17b40 Apollon Oikonomopoulos
35 66d17b40 Apollon Oikonomopoulos
from struct import pack, unpack
36 66d17b40 Apollon Oikonomopoulos
37 66d17b40 Apollon Oikonomopoulos
38 66d17b40 Apollon Oikonomopoulos
###################################################
39 66d17b40 Apollon Oikonomopoulos
###
40 66d17b40 Apollon Oikonomopoulos
###  start: changes made for VNC.
41 66d17b40 Apollon Oikonomopoulos
###
42 66d17b40 Apollon Oikonomopoulos
43 66d17b40 Apollon Oikonomopoulos
# This constant was taken from vncviewer/rfb/vncauth.c:
44 66d17b40 Apollon Oikonomopoulos
vnckey = [ 23,82,107,6,35,78,88,7 ]
45 66d17b40 Apollon Oikonomopoulos
46 66d17b40 Apollon Oikonomopoulos
# This is a departure from the original code.
47 66d17b40 Apollon Oikonomopoulos
#bytebit = [ 0200, 0100, 040, 020, 010, 04, 02, 01 ] # original
48 66d17b40 Apollon Oikonomopoulos
bytebit = [ 01, 02, 04, 010, 020, 040, 0100, 0200 ] # VNC version
49 66d17b40 Apollon Oikonomopoulos
50 66d17b40 Apollon Oikonomopoulos
# two password functions for VNC protocol.
51 66d17b40 Apollon Oikonomopoulos
def decrypt_passwd(data):
52 66d17b40 Apollon Oikonomopoulos
    dk = deskey(pack('8B', *vnckey), True)
53 66d17b40 Apollon Oikonomopoulos
    return desfunc(data, dk)
54 66d17b40 Apollon Oikonomopoulos
55 66d17b40 Apollon Oikonomopoulos
def generate_response(passwd, challange):
56 66d17b40 Apollon Oikonomopoulos
    ek = deskey((passwd+'\x00'*8)[:8], False)
57 66d17b40 Apollon Oikonomopoulos
    return desfunc(challange[:8], ek) + desfunc(challange[8:], ek)
58 66d17b40 Apollon Oikonomopoulos
59 66d17b40 Apollon Oikonomopoulos
###
60 66d17b40 Apollon Oikonomopoulos
###  end: changes made for VNC.
61 66d17b40 Apollon Oikonomopoulos
###
62 66d17b40 Apollon Oikonomopoulos
###################################################
63 66d17b40 Apollon Oikonomopoulos
64 66d17b40 Apollon Oikonomopoulos
65 66d17b40 Apollon Oikonomopoulos
bigbyte = [
66 66d17b40 Apollon Oikonomopoulos
  0x800000L,    0x400000L,      0x200000L,      0x100000L,
67 66d17b40 Apollon Oikonomopoulos
  0x80000L,     0x40000L,       0x20000L,       0x10000L,
68 66d17b40 Apollon Oikonomopoulos
  0x8000L,      0x4000L,        0x2000L,        0x1000L,
69 66d17b40 Apollon Oikonomopoulos
  0x800L,       0x400L,         0x200L,         0x100L,
70 66d17b40 Apollon Oikonomopoulos
  0x80L,        0x40L,          0x20L,          0x10L,
71 66d17b40 Apollon Oikonomopoulos
  0x8L,         0x4L,           0x2L,           0x1L
72 66d17b40 Apollon Oikonomopoulos
  ]
73 66d17b40 Apollon Oikonomopoulos
74 66d17b40 Apollon Oikonomopoulos
# Use the key schedule specified in the Standard (ANSI X3.92-1981).
75 66d17b40 Apollon Oikonomopoulos
76 66d17b40 Apollon Oikonomopoulos
pc1 = [
77 66d17b40 Apollon Oikonomopoulos
  56, 48, 40, 32, 24, 16,  8,    0, 57, 49, 41, 33, 25, 17,
78 66d17b40 Apollon Oikonomopoulos
   9,  1, 58, 50, 42, 34, 26,   18, 10,  2, 59, 51, 43, 35,
79 66d17b40 Apollon Oikonomopoulos
  62, 54, 46, 38, 30, 22, 14,    6, 61, 53, 45, 37, 29, 21,
80 66d17b40 Apollon Oikonomopoulos
  13,  5, 60, 52, 44, 36, 28,   20, 12,  4, 27, 19, 11,  3
81 66d17b40 Apollon Oikonomopoulos
  ]
82 66d17b40 Apollon Oikonomopoulos
83 66d17b40 Apollon Oikonomopoulos
totrot = [ 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 ]
84 66d17b40 Apollon Oikonomopoulos
85 66d17b40 Apollon Oikonomopoulos
pc2 = [
86 66d17b40 Apollon Oikonomopoulos
  13, 16, 10, 23,  0,  4,  2, 27, 14,  5, 20,  9,
87 66d17b40 Apollon Oikonomopoulos
  22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
88 66d17b40 Apollon Oikonomopoulos
  40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
89 66d17b40 Apollon Oikonomopoulos
  43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31
90 66d17b40 Apollon Oikonomopoulos
  ]
91 66d17b40 Apollon Oikonomopoulos
92 66d17b40 Apollon Oikonomopoulos
def deskey(key, decrypt):      # Thanks to James Gillogly & Phil Karn!
93 66d17b40 Apollon Oikonomopoulos
    key = unpack('8B', key)
94 66d17b40 Apollon Oikonomopoulos
95 66d17b40 Apollon Oikonomopoulos
    pc1m = [0]*56
96 66d17b40 Apollon Oikonomopoulos
    pcr = [0]*56
97 66d17b40 Apollon Oikonomopoulos
    kn = [0L]*32
98 66d17b40 Apollon Oikonomopoulos
99 66d17b40 Apollon Oikonomopoulos
    for j in range(56):
100 66d17b40 Apollon Oikonomopoulos
        l = pc1[j]
101 66d17b40 Apollon Oikonomopoulos
        m = l & 07
102 66d17b40 Apollon Oikonomopoulos
        if key[l >> 3] & bytebit[m]:
103 66d17b40 Apollon Oikonomopoulos
            pc1m[j] = 1
104 66d17b40 Apollon Oikonomopoulos
        else:
105 66d17b40 Apollon Oikonomopoulos
            pc1m[j] = 0
106 66d17b40 Apollon Oikonomopoulos
107 66d17b40 Apollon Oikonomopoulos
    for i in range(16):
108 66d17b40 Apollon Oikonomopoulos
        if decrypt:
109 66d17b40 Apollon Oikonomopoulos
            m = (15 - i) << 1
110 66d17b40 Apollon Oikonomopoulos
        else:
111 66d17b40 Apollon Oikonomopoulos
            m = i << 1
112 66d17b40 Apollon Oikonomopoulos
        n = m + 1
113 66d17b40 Apollon Oikonomopoulos
        kn[m] = kn[n] = 0L
114 66d17b40 Apollon Oikonomopoulos
        for j in range(28):
115 66d17b40 Apollon Oikonomopoulos
            l = j + totrot[i]
116 66d17b40 Apollon Oikonomopoulos
            if l < 28:
117 66d17b40 Apollon Oikonomopoulos
                pcr[j] = pc1m[l]
118 66d17b40 Apollon Oikonomopoulos
            else:
119 66d17b40 Apollon Oikonomopoulos
                pcr[j] = pc1m[l - 28]
120 66d17b40 Apollon Oikonomopoulos
        for j in range(28, 56):
121 66d17b40 Apollon Oikonomopoulos
            l = j + totrot[i]
122 66d17b40 Apollon Oikonomopoulos
            if l < 56:
123 66d17b40 Apollon Oikonomopoulos
                pcr[j] = pc1m[l]
124 66d17b40 Apollon Oikonomopoulos
            else:
125 66d17b40 Apollon Oikonomopoulos
                pcr[j] = pc1m[l - 28]
126 66d17b40 Apollon Oikonomopoulos
        for j in range(24):
127 66d17b40 Apollon Oikonomopoulos
            if pcr[pc2[j]]:
128 66d17b40 Apollon Oikonomopoulos
                kn[m] |= bigbyte[j]
129 66d17b40 Apollon Oikonomopoulos
            if pcr[pc2[j+24]]:
130 66d17b40 Apollon Oikonomopoulos
                kn[n] |= bigbyte[j]
131 66d17b40 Apollon Oikonomopoulos
132 66d17b40 Apollon Oikonomopoulos
    return cookey(kn)
133 66d17b40 Apollon Oikonomopoulos
134 66d17b40 Apollon Oikonomopoulos
def cookey(raw):
135 66d17b40 Apollon Oikonomopoulos
    key = []
136 66d17b40 Apollon Oikonomopoulos
    for i in range(0, 32, 2):
137 66d17b40 Apollon Oikonomopoulos
        (raw0, raw1) = (raw[i], raw[i+1])
138 66d17b40 Apollon Oikonomopoulos
        k  = (raw0 & 0x00fc0000L) << 6
139 66d17b40 Apollon Oikonomopoulos
        k |= (raw0 & 0x00000fc0L) << 10
140 66d17b40 Apollon Oikonomopoulos
        k |= (raw1 & 0x00fc0000L) >> 10
141 66d17b40 Apollon Oikonomopoulos
        k |= (raw1 & 0x00000fc0L) >> 6
142 66d17b40 Apollon Oikonomopoulos
        key.append(k)
143 66d17b40 Apollon Oikonomopoulos
        k  = (raw0 & 0x0003f000L) << 12
144 66d17b40 Apollon Oikonomopoulos
        k |= (raw0 & 0x0000003fL) << 16
145 66d17b40 Apollon Oikonomopoulos
        k |= (raw1 & 0x0003f000L) >> 4
146 66d17b40 Apollon Oikonomopoulos
        k |= (raw1 & 0x0000003fL)
147 66d17b40 Apollon Oikonomopoulos
        key.append(k)
148 66d17b40 Apollon Oikonomopoulos
    return key
149 66d17b40 Apollon Oikonomopoulos
150 66d17b40 Apollon Oikonomopoulos
SP1 = [
151 66d17b40 Apollon Oikonomopoulos
  0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
152 66d17b40 Apollon Oikonomopoulos
  0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
153 66d17b40 Apollon Oikonomopoulos
  0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
154 66d17b40 Apollon Oikonomopoulos
  0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
155 66d17b40 Apollon Oikonomopoulos
  0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
156 66d17b40 Apollon Oikonomopoulos
  0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
157 66d17b40 Apollon Oikonomopoulos
  0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
158 66d17b40 Apollon Oikonomopoulos
  0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
159 66d17b40 Apollon Oikonomopoulos
  0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
160 66d17b40 Apollon Oikonomopoulos
  0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
161 66d17b40 Apollon Oikonomopoulos
  0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
162 66d17b40 Apollon Oikonomopoulos
  0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
163 66d17b40 Apollon Oikonomopoulos
  0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
164 66d17b40 Apollon Oikonomopoulos
  0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
165 66d17b40 Apollon Oikonomopoulos
  0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
166 66d17b40 Apollon Oikonomopoulos
  0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L
167 66d17b40 Apollon Oikonomopoulos
  ]
168 66d17b40 Apollon Oikonomopoulos
169 66d17b40 Apollon Oikonomopoulos
SP2 = [
170 66d17b40 Apollon Oikonomopoulos
  0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
171 66d17b40 Apollon Oikonomopoulos
  0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
172 66d17b40 Apollon Oikonomopoulos
  0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
173 66d17b40 Apollon Oikonomopoulos
  0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
174 66d17b40 Apollon Oikonomopoulos
  0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
175 66d17b40 Apollon Oikonomopoulos
  0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
176 66d17b40 Apollon Oikonomopoulos
  0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
177 66d17b40 Apollon Oikonomopoulos
  0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
178 66d17b40 Apollon Oikonomopoulos
  0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
179 66d17b40 Apollon Oikonomopoulos
  0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
180 66d17b40 Apollon Oikonomopoulos
  0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
181 66d17b40 Apollon Oikonomopoulos
  0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
182 66d17b40 Apollon Oikonomopoulos
  0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
183 66d17b40 Apollon Oikonomopoulos
  0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
184 66d17b40 Apollon Oikonomopoulos
  0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
185 66d17b40 Apollon Oikonomopoulos
  0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L
186 66d17b40 Apollon Oikonomopoulos
  ]
187 66d17b40 Apollon Oikonomopoulos
188 66d17b40 Apollon Oikonomopoulos
SP3 = [
189 66d17b40 Apollon Oikonomopoulos
  0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
190 66d17b40 Apollon Oikonomopoulos
  0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
191 66d17b40 Apollon Oikonomopoulos
  0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
192 66d17b40 Apollon Oikonomopoulos
  0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
193 66d17b40 Apollon Oikonomopoulos
  0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
194 66d17b40 Apollon Oikonomopoulos
  0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
195 66d17b40 Apollon Oikonomopoulos
  0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
196 66d17b40 Apollon Oikonomopoulos
  0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
197 66d17b40 Apollon Oikonomopoulos
  0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
198 66d17b40 Apollon Oikonomopoulos
  0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
199 66d17b40 Apollon Oikonomopoulos
  0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
200 66d17b40 Apollon Oikonomopoulos
  0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
201 66d17b40 Apollon Oikonomopoulos
  0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
202 66d17b40 Apollon Oikonomopoulos
  0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
203 66d17b40 Apollon Oikonomopoulos
  0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
204 66d17b40 Apollon Oikonomopoulos
  0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L
205 66d17b40 Apollon Oikonomopoulos
  ]
206 66d17b40 Apollon Oikonomopoulos
207 66d17b40 Apollon Oikonomopoulos
SP4 = [
208 66d17b40 Apollon Oikonomopoulos
  0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
209 66d17b40 Apollon Oikonomopoulos
  0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
210 66d17b40 Apollon Oikonomopoulos
  0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
211 66d17b40 Apollon Oikonomopoulos
  0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
212 66d17b40 Apollon Oikonomopoulos
  0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
213 66d17b40 Apollon Oikonomopoulos
  0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
214 66d17b40 Apollon Oikonomopoulos
  0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
215 66d17b40 Apollon Oikonomopoulos
  0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
216 66d17b40 Apollon Oikonomopoulos
  0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
217 66d17b40 Apollon Oikonomopoulos
  0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
218 66d17b40 Apollon Oikonomopoulos
  0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
219 66d17b40 Apollon Oikonomopoulos
  0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
220 66d17b40 Apollon Oikonomopoulos
  0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
221 66d17b40 Apollon Oikonomopoulos
  0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
222 66d17b40 Apollon Oikonomopoulos
  0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
223 66d17b40 Apollon Oikonomopoulos
  0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L
224 66d17b40 Apollon Oikonomopoulos
  ]
225 66d17b40 Apollon Oikonomopoulos
226 66d17b40 Apollon Oikonomopoulos
SP5 = [
227 66d17b40 Apollon Oikonomopoulos
  0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
228 66d17b40 Apollon Oikonomopoulos
  0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
229 66d17b40 Apollon Oikonomopoulos
  0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
230 66d17b40 Apollon Oikonomopoulos
  0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
231 66d17b40 Apollon Oikonomopoulos
  0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
232 66d17b40 Apollon Oikonomopoulos
  0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
233 66d17b40 Apollon Oikonomopoulos
  0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
234 66d17b40 Apollon Oikonomopoulos
  0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
235 66d17b40 Apollon Oikonomopoulos
  0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
236 66d17b40 Apollon Oikonomopoulos
  0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
237 66d17b40 Apollon Oikonomopoulos
  0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
238 66d17b40 Apollon Oikonomopoulos
  0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
239 66d17b40 Apollon Oikonomopoulos
  0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
240 66d17b40 Apollon Oikonomopoulos
  0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
241 66d17b40 Apollon Oikonomopoulos
  0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
242 66d17b40 Apollon Oikonomopoulos
  0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L
243 66d17b40 Apollon Oikonomopoulos
  ]
244 66d17b40 Apollon Oikonomopoulos
245 66d17b40 Apollon Oikonomopoulos
SP6 = [
246 66d17b40 Apollon Oikonomopoulos
  0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
247 66d17b40 Apollon Oikonomopoulos
  0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
248 66d17b40 Apollon Oikonomopoulos
  0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
249 66d17b40 Apollon Oikonomopoulos
  0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
250 66d17b40 Apollon Oikonomopoulos
  0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
251 66d17b40 Apollon Oikonomopoulos
  0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
252 66d17b40 Apollon Oikonomopoulos
  0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
253 66d17b40 Apollon Oikonomopoulos
  0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
254 66d17b40 Apollon Oikonomopoulos
  0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
255 66d17b40 Apollon Oikonomopoulos
  0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
256 66d17b40 Apollon Oikonomopoulos
  0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
257 66d17b40 Apollon Oikonomopoulos
  0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
258 66d17b40 Apollon Oikonomopoulos
  0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
259 66d17b40 Apollon Oikonomopoulos
  0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
260 66d17b40 Apollon Oikonomopoulos
  0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
261 66d17b40 Apollon Oikonomopoulos
  0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L
262 66d17b40 Apollon Oikonomopoulos
  ]
263 66d17b40 Apollon Oikonomopoulos
264 66d17b40 Apollon Oikonomopoulos
SP7 = [
265 66d17b40 Apollon Oikonomopoulos
  0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
266 66d17b40 Apollon Oikonomopoulos
  0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
267 66d17b40 Apollon Oikonomopoulos
  0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
268 66d17b40 Apollon Oikonomopoulos
  0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
269 66d17b40 Apollon Oikonomopoulos
  0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
270 66d17b40 Apollon Oikonomopoulos
  0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
271 66d17b40 Apollon Oikonomopoulos
  0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
272 66d17b40 Apollon Oikonomopoulos
  0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
273 66d17b40 Apollon Oikonomopoulos
  0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
274 66d17b40 Apollon Oikonomopoulos
  0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
275 66d17b40 Apollon Oikonomopoulos
  0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
276 66d17b40 Apollon Oikonomopoulos
  0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
277 66d17b40 Apollon Oikonomopoulos
  0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
278 66d17b40 Apollon Oikonomopoulos
  0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
279 66d17b40 Apollon Oikonomopoulos
  0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
280 66d17b40 Apollon Oikonomopoulos
  0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L
281 66d17b40 Apollon Oikonomopoulos
  ]
282 66d17b40 Apollon Oikonomopoulos
283 66d17b40 Apollon Oikonomopoulos
SP8 = [
284 66d17b40 Apollon Oikonomopoulos
  0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
285 66d17b40 Apollon Oikonomopoulos
  0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
286 66d17b40 Apollon Oikonomopoulos
  0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
287 66d17b40 Apollon Oikonomopoulos
  0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
288 66d17b40 Apollon Oikonomopoulos
  0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
289 66d17b40 Apollon Oikonomopoulos
  0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
290 66d17b40 Apollon Oikonomopoulos
  0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
291 66d17b40 Apollon Oikonomopoulos
  0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
292 66d17b40 Apollon Oikonomopoulos
  0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
293 66d17b40 Apollon Oikonomopoulos
  0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
294 66d17b40 Apollon Oikonomopoulos
  0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
295 66d17b40 Apollon Oikonomopoulos
  0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
296 66d17b40 Apollon Oikonomopoulos
  0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
297 66d17b40 Apollon Oikonomopoulos
  0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
298 66d17b40 Apollon Oikonomopoulos
  0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
299 66d17b40 Apollon Oikonomopoulos
  0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L
300 66d17b40 Apollon Oikonomopoulos
  ]
301 66d17b40 Apollon Oikonomopoulos
302 66d17b40 Apollon Oikonomopoulos
def desfunc(block, keys):
303 66d17b40 Apollon Oikonomopoulos
    (leftt, right) = unpack('>II', block)
304 66d17b40 Apollon Oikonomopoulos
305 66d17b40 Apollon Oikonomopoulos
    work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL
306 66d17b40 Apollon Oikonomopoulos
    right ^= work
307 66d17b40 Apollon Oikonomopoulos
    leftt ^= (work << 4)
308 66d17b40 Apollon Oikonomopoulos
    work = ((leftt >> 16) ^ right) & 0x0000ffffL
309 66d17b40 Apollon Oikonomopoulos
    right ^= work
310 66d17b40 Apollon Oikonomopoulos
    leftt ^= (work << 16)
311 66d17b40 Apollon Oikonomopoulos
    work = ((right >> 2) ^ leftt) & 0x33333333L
312 66d17b40 Apollon Oikonomopoulos
    leftt ^= work
313 66d17b40 Apollon Oikonomopoulos
    right ^= (work << 2)
314 66d17b40 Apollon Oikonomopoulos
    work = ((right >> 8) ^ leftt) & 0x00ff00ffL
315 66d17b40 Apollon Oikonomopoulos
    leftt ^= work
316 66d17b40 Apollon Oikonomopoulos
    right ^= (work << 8)
317 66d17b40 Apollon Oikonomopoulos
    right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL
318 66d17b40 Apollon Oikonomopoulos
    work = (leftt ^ right) & 0xaaaaaaaaL
319 66d17b40 Apollon Oikonomopoulos
    leftt ^= work
320 66d17b40 Apollon Oikonomopoulos
    right ^= work
321 66d17b40 Apollon Oikonomopoulos
    leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL
322 66d17b40 Apollon Oikonomopoulos
323 66d17b40 Apollon Oikonomopoulos
    for i in range(0, 32, 4):
324 66d17b40 Apollon Oikonomopoulos
        work  = (right << 28) | (right >> 4)
325 66d17b40 Apollon Oikonomopoulos
        work ^= keys[i]
326 66d17b40 Apollon Oikonomopoulos
        fval  = SP7[ work            & 0x3fL]
327 66d17b40 Apollon Oikonomopoulos
        fval |= SP5[(work >>  8) & 0x3fL]
328 66d17b40 Apollon Oikonomopoulos
        fval |= SP3[(work >> 16) & 0x3fL]
329 66d17b40 Apollon Oikonomopoulos
        fval |= SP1[(work >> 24) & 0x3fL]
330 66d17b40 Apollon Oikonomopoulos
        work  = right ^ keys[i+1]
331 66d17b40 Apollon Oikonomopoulos
        fval |= SP8[ work            & 0x3fL]
332 66d17b40 Apollon Oikonomopoulos
        fval |= SP6[(work >>  8) & 0x3fL]
333 66d17b40 Apollon Oikonomopoulos
        fval |= SP4[(work >> 16) & 0x3fL]
334 66d17b40 Apollon Oikonomopoulos
        fval |= SP2[(work >> 24) & 0x3fL]
335 66d17b40 Apollon Oikonomopoulos
        leftt ^= fval
336 66d17b40 Apollon Oikonomopoulos
        work  = (leftt << 28) | (leftt >> 4)
337 66d17b40 Apollon Oikonomopoulos
        work ^= keys[i+2]
338 66d17b40 Apollon Oikonomopoulos
        fval  = SP7[ work            & 0x3fL]
339 66d17b40 Apollon Oikonomopoulos
        fval |= SP5[(work >>  8) & 0x3fL]
340 66d17b40 Apollon Oikonomopoulos
        fval |= SP3[(work >> 16) & 0x3fL]
341 66d17b40 Apollon Oikonomopoulos
        fval |= SP1[(work >> 24) & 0x3fL]
342 66d17b40 Apollon Oikonomopoulos
        work  = leftt ^ keys[i+3]
343 66d17b40 Apollon Oikonomopoulos
        fval |= SP8[ work            & 0x3fL]
344 66d17b40 Apollon Oikonomopoulos
        fval |= SP6[(work >>  8) & 0x3fL]
345 66d17b40 Apollon Oikonomopoulos
        fval |= SP4[(work >> 16) & 0x3fL]
346 66d17b40 Apollon Oikonomopoulos
        fval |= SP2[(work >> 24) & 0x3fL]
347 66d17b40 Apollon Oikonomopoulos
        right ^= fval
348 66d17b40 Apollon Oikonomopoulos
349 66d17b40 Apollon Oikonomopoulos
    right = (right << 31) | (right >> 1)
350 66d17b40 Apollon Oikonomopoulos
    work = (leftt ^ right) & 0xaaaaaaaaL
351 66d17b40 Apollon Oikonomopoulos
    leftt ^= work
352 66d17b40 Apollon Oikonomopoulos
    right ^= work
353 66d17b40 Apollon Oikonomopoulos
    leftt = (leftt << 31) | (leftt >> 1)
354 66d17b40 Apollon Oikonomopoulos
    work = ((leftt >> 8) ^ right) & 0x00ff00ffL
355 66d17b40 Apollon Oikonomopoulos
    right ^= work
356 66d17b40 Apollon Oikonomopoulos
    leftt ^= (work << 8)
357 66d17b40 Apollon Oikonomopoulos
    work = ((leftt >> 2) ^ right) & 0x33333333L
358 66d17b40 Apollon Oikonomopoulos
    right ^= work
359 66d17b40 Apollon Oikonomopoulos
    leftt ^= (work << 2)
360 66d17b40 Apollon Oikonomopoulos
    work = ((right >> 16) ^ leftt) & 0x0000ffffL
361 66d17b40 Apollon Oikonomopoulos
    leftt ^= work
362 66d17b40 Apollon Oikonomopoulos
    right ^= (work << 16)
363 66d17b40 Apollon Oikonomopoulos
    work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL
364 66d17b40 Apollon Oikonomopoulos
    leftt ^= work
365 66d17b40 Apollon Oikonomopoulos
    right ^= (work << 4)
366 66d17b40 Apollon Oikonomopoulos
367 66d17b40 Apollon Oikonomopoulos
    leftt &= 0xffffffffL
368 66d17b40 Apollon Oikonomopoulos
    right &= 0xffffffffL
369 66d17b40 Apollon Oikonomopoulos
    return pack('>II', right, leftt)
370 66d17b40 Apollon Oikonomopoulos
371 66d17b40 Apollon Oikonomopoulos
372 66d17b40 Apollon Oikonomopoulos
# test
373 66d17b40 Apollon Oikonomopoulos
if __name__ == '__main__':
374 66d17b40 Apollon Oikonomopoulos
    key = '0123456789abcdef'.decode('hex')
375 66d17b40 Apollon Oikonomopoulos
    plain = '0123456789abcdef'.decode('hex')
376 66d17b40 Apollon Oikonomopoulos
    cipher = '6e09a37726dd560c'.decode('hex')
377 66d17b40 Apollon Oikonomopoulos
    ek = deskey(key, False)
378 66d17b40 Apollon Oikonomopoulos
    dk = deskey(key, True)
379 66d17b40 Apollon Oikonomopoulos
    assert desfunc(plain, ek) == cipher
380 66d17b40 Apollon Oikonomopoulos
    assert desfunc(desfunc(plain, ek), dk) == plain
381 66d17b40 Apollon Oikonomopoulos
    assert desfunc(desfunc(plain, dk), ek) == plain
382 66d17b40 Apollon Oikonomopoulos
    print 'test succeeded.'