1 ## serpent.py - pure Python implementation of the Serpent algorithm.
2 ## Bjorn Edstrom <be@bjrn.se> 13 december 2007.
7 ## This code is a derived from an implementation by Dr Brian Gladman
8 ## (gladman@seven77.demon.co.uk) which is subject to the following license.
9 ## This Python implementation is not subject to any other license.
11 ##/* This is an independent implementation of the encryption algorithm:
13 ## * Serpent by Ross Anderson, Eli Biham and Lars Knudsen
15 ## * which is a candidate algorithm in the Advanced Encryption Standard
16 ## * programme of the US National Institute of Standards and Technology
18 ## * Copyright in this implementation is held by Dr B R Gladman but I
19 ## * hereby give permission for its free direct or derivative use subject
20 ## * to acknowledgment of its origin and compliance with any conditions
21 ## * that the originators of the algorithm place on its exploitation.
23 ## * Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999
26 ## The above copyright notice must not be removed.
31 ## Anyone thinking of using this code should reconsider. It's slow.
32 ## Try python-mcrypt instead. In case a faster library is not installed
33 ## on the target system, this code can be used as a portable fallback.
46 def __init__(self
, key
=None):
53 def set_key(self
, key
):
59 raise KeyError, "key not a multiple of 4"
62 raise KeyError, "key_len > 32"
64 self
.key_context
= [0] * 140
69 key_word32
[i
] = struct
.unpack("<L", key
[0:4])[0]
73 set_key(self
.key_context
, key_word32
, key_len
)
76 def decrypt(self
, block
):
80 raise ValueError, "block size must be a multiple of 16"
85 a
, b
, c
, d
= struct
.unpack("<4L", block
[:16])
87 decrypt(self
.key_context
, temp
)
88 plaintext
+= struct
.pack("<4L", *temp
)
94 def encrypt(self
, block
):
98 raise ValueError, "block size must be a multiple of 16"
103 a
, b
, c
, d
= struct
.unpack("<4L", block
[0:16])
105 encrypt(self
.key_context
, temp
)
106 ciphertext
+= struct
.pack("<4L", *temp
)
113 """Return the name of the cipher."""
118 def get_block_size(self
):
119 """Get cipher block size in bytes."""
124 def get_key_size(self
):
125 """Get cipher key size in bytes."""
138 if sys
.byteorder
== 'big':
142 return (x
>> n
) |
((x
<< (32 - n
)) & 0xFFFFFFFF)
145 return ((x
<< n
) & 0xFFFFFFFF) |
(x
>> (32 - n
))
148 return ((x
& 0xff) << 24) |
(((x
>> 8) & 0xff) << 16) | \
149 (((x
>> 16) & 0xff) << 8) |
((x
>> 24) & 0xff)
151 def set_key(l_key
, key
, key_len
):
157 lk
= (key_len
+ 31) / 32
161 l_key
[i
] = byteswap32(key
[i
])
169 lk
= 1 << (key_len
% 32)
170 l_key
[i
] = (l_key
[i
] & (lk
- 1)) | lk
171 for i
in xrange(132):
172 lk
= l_key
[i
] ^ l_key
[i
+ 3] ^ l_key
[i
+ 5] ^ l_key
[i
+ 7] ^
0x9e3779b9 ^ i
173 l_key
[i
+ 8] = ((lk
<< 11) & 0xFFFFFFFF) |
(lk
>> 21)
176 # serpent_generate.py
226 t1
= (~a
) % 0x100000000;
250 t1
= (~a
) % 0x100000000;
277 t10
= (~t3
) % 0x100000000;
280 t14
= (~t7
) % 0x100000000;
295 t1
= (~c
) % 0x100000000;
320 t1
= (~a
) % 0x100000000;
326 t13
= (~t5
) % 0x100000000;
343 t1
= (~a
) % 0x100000000;
372 t6
= (~b
) % 0x100000000;
415 t1
= (~a
) % 0x100000000;
439 t1
= (~a
) % 0x100000000;
466 t10
= (~t3
) % 0x100000000;
469 t14
= (~t7
) % 0x100000000;
484 t1
= (~c
) % 0x100000000;
509 t1
= (~a
) % 0x100000000;
515 t13
= (~t5
) % 0x100000000;
532 t1
= (~a
) % 0x100000000;
561 t6
= (~b
) % 0x100000000;
604 t1
= (~a
) % 0x100000000;
628 t1
= (~a
) % 0x100000000;
655 t10
= (~t3
) % 0x100000000;
658 t14
= (~t7
) % 0x100000000;
673 t1
= (~c
) % 0x100000000;
698 t1
= (~a
) % 0x100000000;
704 t13
= (~t5
) % 0x100000000;
721 t1
= (~a
) % 0x100000000;
750 t6
= (~b
) % 0x100000000;
793 t1
= (~a
) % 0x100000000;
817 t1
= (~a
) % 0x100000000;
844 t10
= (~t3
) % 0x100000000;
847 t14
= (~t7
) % 0x100000000;
862 t1
= (~c
) % 0x100000000;
887 t1
= (~a
) % 0x100000000;
893 t13
= (~t5
) % 0x100000000;
910 t1
= (~a
) % 0x100000000;
939 t6
= (~b
) % 0x100000000;
979 def encrypt(key
, in_blk
):
980 # serpent_generate.py
1012 c ^
= key
[4 * 0 + 10]
1013 d ^
= key
[4 * 0 + 11]
1019 t10
= (~t3
) % 0x100000000;
1022 t14
= (~t7
) % 0x100000000;
1031 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1036 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1041 g ^
= key
[4 * 1 + 10]
1042 h ^
= key
[4 * 1 + 11]
1043 t1
= (~e
) % 0x100000000;
1059 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1064 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1069 c ^
= key
[4 * 2 + 10]
1070 d ^
= key
[4 * 2 + 11]
1071 t1
= (~a
) % 0x100000000;
1089 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1094 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1099 g ^
= key
[4 * 3 + 10]
1100 h ^
= key
[4 * 3 + 11]
1120 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1125 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1130 c ^
= key
[4 * 4 + 10]
1131 d ^
= key
[4 * 4 + 11]
1137 t6
= (~b
) % 0x100000000;
1149 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1154 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1159 g ^
= key
[4 * 5 + 10]
1160 h ^
= key
[4 * 5 + 11]
1161 t1
= (~e
) % 0x100000000;
1179 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1184 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1189 c ^
= key
[4 * 6 + 10]
1190 d ^
= key
[4 * 6 + 11]
1191 t1
= (~a
) % 0x100000000;
1197 t13
= (~t5
) % 0x100000000;
1208 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1213 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1218 g ^
= key
[4 * 7 + 10]
1219 h ^
= key
[4 * 7 + 11]
1220 t1
= (~g
) % 0x100000000;
1239 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1244 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1249 c ^
= key
[4 * 8 + 10]
1250 d ^
= key
[4 * 8 + 11]
1256 t10
= (~t3
) % 0x100000000;
1259 t14
= (~t7
) % 0x100000000;
1268 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1273 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1278 g ^
= key
[4 * 9 + 10]
1279 h ^
= key
[4 * 9 + 11]
1280 t1
= (~e
) % 0x100000000;
1296 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1301 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1304 a ^
= key
[4 * 10 + 8]
1305 b ^
= key
[4 * 10 + 9]
1306 c ^
= key
[4 * 10 + 10]
1307 d ^
= key
[4 * 10 + 11]
1308 t1
= (~a
) % 0x100000000;
1326 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1331 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1334 e ^
= key
[4 * 11 + 8]
1335 f ^
= key
[4 * 11 + 9]
1336 g ^
= key
[4 * 11 + 10]
1337 h ^
= key
[4 * 11 + 11]
1357 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1362 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1365 a ^
= key
[4 * 12 + 8]
1366 b ^
= key
[4 * 12 + 9]
1367 c ^
= key
[4 * 12 + 10]
1368 d ^
= key
[4 * 12 + 11]
1374 t6
= (~b
) % 0x100000000;
1386 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1391 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1394 e ^
= key
[4 * 13 + 8]
1395 f ^
= key
[4 * 13 + 9]
1396 g ^
= key
[4 * 13 + 10]
1397 h ^
= key
[4 * 13 + 11]
1398 t1
= (~e
) % 0x100000000;
1416 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1421 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1424 a ^
= key
[4 * 14 + 8]
1425 b ^
= key
[4 * 14 + 9]
1426 c ^
= key
[4 * 14 + 10]
1427 d ^
= key
[4 * 14 + 11]
1428 t1
= (~a
) % 0x100000000;
1434 t13
= (~t5
) % 0x100000000;
1445 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1450 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1453 e ^
= key
[4 * 15 + 8]
1454 f ^
= key
[4 * 15 + 9]
1455 g ^
= key
[4 * 15 + 10]
1456 h ^
= key
[4 * 15 + 11]
1457 t1
= (~g
) % 0x100000000;
1476 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1481 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1484 a ^
= key
[4 * 16 + 8]
1485 b ^
= key
[4 * 16 + 9]
1486 c ^
= key
[4 * 16 + 10]
1487 d ^
= key
[4 * 16 + 11]
1493 t10
= (~t3
) % 0x100000000;
1496 t14
= (~t7
) % 0x100000000;
1505 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1510 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1513 e ^
= key
[4 * 17 + 8]
1514 f ^
= key
[4 * 17 + 9]
1515 g ^
= key
[4 * 17 + 10]
1516 h ^
= key
[4 * 17 + 11]
1517 t1
= (~e
) % 0x100000000;
1533 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1538 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1541 a ^
= key
[4 * 18 + 8]
1542 b ^
= key
[4 * 18 + 9]
1543 c ^
= key
[4 * 18 + 10]
1544 d ^
= key
[4 * 18 + 11]
1545 t1
= (~a
) % 0x100000000;
1563 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1568 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1571 e ^
= key
[4 * 19 + 8]
1572 f ^
= key
[4 * 19 + 9]
1573 g ^
= key
[4 * 19 + 10]
1574 h ^
= key
[4 * 19 + 11]
1594 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1599 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1602 a ^
= key
[4 * 20 + 8]
1603 b ^
= key
[4 * 20 + 9]
1604 c ^
= key
[4 * 20 + 10]
1605 d ^
= key
[4 * 20 + 11]
1611 t6
= (~b
) % 0x100000000;
1623 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1628 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1631 e ^
= key
[4 * 21 + 8]
1632 f ^
= key
[4 * 21 + 9]
1633 g ^
= key
[4 * 21 + 10]
1634 h ^
= key
[4 * 21 + 11]
1635 t1
= (~e
) % 0x100000000;
1653 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1658 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1661 a ^
= key
[4 * 22 + 8]
1662 b ^
= key
[4 * 22 + 9]
1663 c ^
= key
[4 * 22 + 10]
1664 d ^
= key
[4 * 22 + 11]
1665 t1
= (~a
) % 0x100000000;
1671 t13
= (~t5
) % 0x100000000;
1682 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1687 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1690 e ^
= key
[4 * 23 + 8]
1691 f ^
= key
[4 * 23 + 9]
1692 g ^
= key
[4 * 23 + 10]
1693 h ^
= key
[4 * 23 + 11]
1694 t1
= (~g
) % 0x100000000;
1713 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1718 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1721 a ^
= key
[4 * 24 + 8]
1722 b ^
= key
[4 * 24 + 9]
1723 c ^
= key
[4 * 24 + 10]
1724 d ^
= key
[4 * 24 + 11]
1730 t10
= (~t3
) % 0x100000000;
1733 t14
= (~t7
) % 0x100000000;
1742 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1747 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1750 e ^
= key
[4 * 25 + 8]
1751 f ^
= key
[4 * 25 + 9]
1752 g ^
= key
[4 * 25 + 10]
1753 h ^
= key
[4 * 25 + 11]
1754 t1
= (~e
) % 0x100000000;
1770 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1775 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1778 a ^
= key
[4 * 26 + 8]
1779 b ^
= key
[4 * 26 + 9]
1780 c ^
= key
[4 * 26 + 10]
1781 d ^
= key
[4 * 26 + 11]
1782 t1
= (~a
) % 0x100000000;
1800 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1805 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1808 e ^
= key
[4 * 27 + 8]
1809 f ^
= key
[4 * 27 + 9]
1810 g ^
= key
[4 * 27 + 10]
1811 h ^
= key
[4 * 27 + 11]
1831 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1836 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1839 a ^
= key
[4 * 28 + 8]
1840 b ^
= key
[4 * 28 + 9]
1841 c ^
= key
[4 * 28 + 10]
1842 d ^
= key
[4 * 28 + 11]
1848 t6
= (~b
) % 0x100000000;
1860 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1865 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1868 e ^
= key
[4 * 29 + 8]
1869 f ^
= key
[4 * 29 + 9]
1870 g ^
= key
[4 * 29 + 10]
1871 h ^
= key
[4 * 29 + 11]
1872 t1
= (~e
) % 0x100000000;
1890 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
1895 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
1898 a ^
= key
[4 * 30 + 8]
1899 b ^
= key
[4 * 30 + 9]
1900 c ^
= key
[4 * 30 + 10]
1901 d ^
= key
[4 * 30 + 11]
1902 t1
= (~a
) % 0x100000000;
1908 t13
= (~t5
) % 0x100000000;
1919 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
1924 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
1927 e ^
= key
[4 * 31 + 8]
1928 f ^
= key
[4 * 31 + 9]
1929 g ^
= key
[4 * 31 + 10]
1930 h ^
= key
[4 * 31 + 11]
1931 t1
= (~g
) % 0x100000000;
1948 a ^
= key
[4 * 32 + 8]
1949 b ^
= key
[4 * 32 + 9]
1950 c ^
= key
[4 * 32 + 10]
1951 d ^
= key
[4 * 32 + 11]
1962 def decrypt(key
, in_blk
):
1963 # serpent_generate.py
1993 a ^
= key
[4 * 32 + 8]
1994 b ^
= key
[4 * 32 + 9]
1995 c ^
= key
[4 * 32 + 10]
1996 d ^
= key
[4 * 32 + 11]
2002 t6
= (~d
) % 0x100000000;
2014 e ^
= key
[4 * 31 + 8]
2015 f ^
= key
[4 * 31 + 9]
2016 g ^
= key
[4 * 31 + 10]
2017 h ^
= key
[4 * 31 + 11]
2020 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2024 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2028 t1
= (~e
) % 0x100000000;
2043 a ^
= key
[4 * 30 + 8]
2044 b ^
= key
[4 * 30 + 9]
2045 c ^
= key
[4 * 30 + 10]
2046 d ^
= key
[4 * 30 + 11]
2049 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2053 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2057 t1
= (~c
) % 0x100000000;
2073 e ^
= key
[4 * 29 + 8]
2074 f ^
= key
[4 * 29 + 9]
2075 g ^
= key
[4 * 29 + 10]
2076 h ^
= key
[4 * 29 + 11]
2079 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2083 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2096 t10
= (~e
) % 0x100000000;
2104 a ^
= key
[4 * 28 + 8]
2105 b ^
= key
[4 * 28 + 9]
2106 c ^
= key
[4 * 28 + 10]
2107 d ^
= key
[4 * 28 + 11]
2110 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2114 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2135 e ^
= key
[4 * 27 + 8]
2136 f ^
= key
[4 * 27 + 9]
2137 g ^
= key
[4 * 27 + 10]
2138 h ^
= key
[4 * 27 + 11]
2141 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2145 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2150 t2
= (~t1
) % 0x100000000;
2156 t11
= (~t4
) % 0x100000000;
2165 a ^
= key
[4 * 26 + 8]
2166 b ^
= key
[4 * 26 + 9]
2167 c ^
= key
[4 * 26 + 10]
2168 d ^
= key
[4 * 26 + 11]
2171 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2175 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2187 t11
= (~t2
) % 0x100000000;
2196 e ^
= key
[4 * 25 + 8]
2197 f ^
= key
[4 * 25 + 9]
2198 g ^
= key
[4 * 25 + 10]
2199 h ^
= key
[4 * 25 + 11]
2202 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2206 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2210 t1
= (~e
) % 0x100000000
2225 a ^
= key
[4 * 24 + 8]
2226 b ^
= key
[4 * 24 + 9]
2227 c ^
= key
[4 * 24 + 10]
2228 d ^
= key
[4 * 24 + 11]
2231 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2235 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2244 t6
= (~d
) % 0x100000000;
2256 e ^
= key
[4 * 23 + 8]
2257 f ^
= key
[4 * 23 + 9]
2258 g ^
= key
[4 * 23 + 10]
2259 h ^
= key
[4 * 23 + 11]
2262 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2266 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2270 t1
= (~e
) % 0x100000000;
2285 a ^
= key
[4 * 22 + 8]
2286 b ^
= key
[4 * 22 + 9]
2287 c ^
= key
[4 * 22 + 10]
2288 d ^
= key
[4 * 22 + 11]
2291 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2295 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2299 t1
= (~c
) % 0x100000000;
2315 e ^
= key
[4 * 21 + 8]
2316 f ^
= key
[4 * 21 + 9]
2317 g ^
= key
[4 * 21 + 10]
2318 h ^
= key
[4 * 21 + 11]
2321 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2325 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2338 t10
= (~e
) % 0x100000000;
2346 a ^
= key
[4 * 20 + 8]
2347 b ^
= key
[4 * 20 + 9]
2348 c ^
= key
[4 * 20 + 10]
2349 d ^
= key
[4 * 20 + 11]
2352 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2356 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2377 e ^
= key
[4 * 19 + 8]
2378 f ^
= key
[4 * 19 + 9]
2379 g ^
= key
[4 * 19 + 10]
2380 h ^
= key
[4 * 19 + 11]
2383 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2387 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2392 t2
= (~t1
) % 0x100000000;
2398 t11
= (~t4
) % 0x100000000;
2407 a ^
= key
[4 * 18 + 8]
2408 b ^
= key
[4 * 18 + 9]
2409 c ^
= key
[4 * 18 + 10]
2410 d ^
= key
[4 * 18 + 11]
2413 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2417 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2429 t11
= (~t2
) % 0x100000000;
2438 e ^
= key
[4 * 17 + 8]
2439 f ^
= key
[4 * 17 + 9]
2440 g ^
= key
[4 * 17 + 10]
2441 h ^
= key
[4 * 17 + 11]
2444 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2448 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2452 t1
= (~e
) % 0x100000000
2467 a ^
= key
[4 * 16 + 8]
2468 b ^
= key
[4 * 16 + 9]
2469 c ^
= key
[4 * 16 + 10]
2470 d ^
= key
[4 * 16 + 11]
2473 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2477 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2486 t6
= (~d
) % 0x100000000;
2498 e ^
= key
[4 * 15 + 8]
2499 f ^
= key
[4 * 15 + 9]
2500 g ^
= key
[4 * 15 + 10]
2501 h ^
= key
[4 * 15 + 11]
2504 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2508 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2512 t1
= (~e
) % 0x100000000;
2527 a ^
= key
[4 * 14 + 8]
2528 b ^
= key
[4 * 14 + 9]
2529 c ^
= key
[4 * 14 + 10]
2530 d ^
= key
[4 * 14 + 11]
2533 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2537 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2541 t1
= (~c
) % 0x100000000;
2557 e ^
= key
[4 * 13 + 8]
2558 f ^
= key
[4 * 13 + 9]
2559 g ^
= key
[4 * 13 + 10]
2560 h ^
= key
[4 * 13 + 11]
2563 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2567 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2580 t10
= (~e
) % 0x100000000;
2588 a ^
= key
[4 * 12 + 8]
2589 b ^
= key
[4 * 12 + 9]
2590 c ^
= key
[4 * 12 + 10]
2591 d ^
= key
[4 * 12 + 11]
2594 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2598 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2619 e ^
= key
[4 * 11 + 8]
2620 f ^
= key
[4 * 11 + 9]
2621 g ^
= key
[4 * 11 + 10]
2622 h ^
= key
[4 * 11 + 11]
2625 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2629 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2634 t2
= (~t1
) % 0x100000000;
2640 t11
= (~t4
) % 0x100000000;
2649 a ^
= key
[4 * 10 + 8]
2650 b ^
= key
[4 * 10 + 9]
2651 c ^
= key
[4 * 10 + 10]
2652 d ^
= key
[4 * 10 + 11]
2655 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2659 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2671 t11
= (~t2
) % 0x100000000;
2682 g ^
= key
[4 * 9 + 10]
2683 h ^
= key
[4 * 9 + 11]
2686 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2690 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2694 t1
= (~e
) % 0x100000000
2711 c ^
= key
[4 * 8 + 10]
2712 d ^
= key
[4 * 8 + 11]
2715 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2719 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2728 t6
= (~d
) % 0x100000000;
2742 g ^
= key
[4 * 7 + 10]
2743 h ^
= key
[4 * 7 + 11]
2746 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2750 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2754 t1
= (~e
) % 0x100000000;
2771 c ^
= key
[4 * 6 + 10]
2772 d ^
= key
[4 * 6 + 11]
2775 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2779 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2783 t1
= (~c
) % 0x100000000;
2801 g ^
= key
[4 * 5 + 10]
2802 h ^
= key
[4 * 5 + 11]
2805 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2809 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2822 t10
= (~e
) % 0x100000000;
2832 c ^
= key
[4 * 4 + 10]
2833 d ^
= key
[4 * 4 + 11]
2836 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2840 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2863 g ^
= key
[4 * 3 + 10]
2864 h ^
= key
[4 * 3 + 11]
2867 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2871 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2876 t2
= (~t1
) % 0x100000000;
2882 t11
= (~t4
) % 0x100000000;
2893 c ^
= key
[4 * 2 + 10]
2894 d ^
= key
[4 * 2 + 11]
2897 c ^
= d ^
((b
<< 7) & 0xFFFFFFFF)
2901 d ^
= c ^
((a
<< 3) & 0xFFFFFFFF)
2913 t11
= (~t2
) % 0x100000000;
2924 g ^
= key
[4 * 1 + 10]
2925 h ^
= key
[4 * 1 + 11]
2928 g ^
= h ^
((f
<< 7) & 0xFFFFFFFF)
2932 h ^
= g ^
((e
<< 3) & 0xFFFFFFFF)
2936 t1
= (~e
) % 0x100000000
2953 c ^
= key
[4 * 0 + 10]
2954 d ^
= key
[4 * 0 + 11]
2965 __testkey
= '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
2966 __testdat
= '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
2967 assert '\xde&\x9f\xf83\xe42\xb8[.\x88\xd2p\x1c\xe7\\' == Serpent(__testkey
).encrypt(__testdat
)
2968 assert __testdat
== Serpent(__testkey
).decrypt('\xde&\x9f\xf83\xe42\xb8[.\x88\xd2p\x1c\xe7\\')