Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

849 linhas
51 KiB

  1. """passlib.crypto.des -- DES block encryption routines
  2. History
  3. =======
  4. These routines (which have since been drastically modified for python)
  5. are based on a Java implementation of the des-crypt algorithm,
  6. found at `<http://www.dynamic.net.au/christos/crypt/UnixCrypt2.txt>`_.
  7. The copyright & license for that source is as follows::
  8. UnixCrypt.java 0.9 96/11/25
  9. Copyright (c) 1996 Aki Yoshida. All rights reserved.
  10. Permission to use, copy, modify and distribute this software
  11. for non-commercial or commercial purposes and without fee is
  12. hereby granted provided that this copyright notice appears in
  13. all copies.
  14. ---
  15. Unix crypt(3C) utility
  16. @version 0.9, 11/25/96
  17. @author Aki Yoshida
  18. ---
  19. modified April 2001
  20. by Iris Van den Broeke, Daniel Deville
  21. ---
  22. Unix Crypt.
  23. Implements the one way cryptography used by Unix systems for
  24. simple password protection.
  25. @version $Id: UnixCrypt2.txt,v 1.1.1.1 2005/09/13 22:20:13 christos Exp $
  26. @author Greg Wilkins (gregw)
  27. The netbsd des-crypt implementation has some nice notes on how this all works -
  28. http://fxr.googlebit.com/source/lib/libcrypt/crypt.c?v=NETBSD-CURRENT
  29. """
  30. # TODO: could use an accelerated C version of this module to speed up lmhash,
  31. # des-crypt, and ext-des-crypt
  32. #=============================================================================
  33. # imports
  34. #=============================================================================
  35. # core
  36. import struct
  37. # pkg
  38. from passlib import exc
  39. from passlib.utils.compat import join_byte_values, byte_elem_value, \
  40. irange, irange, int_types
  41. # local
  42. __all__ = [
  43. "expand_des_key",
  44. "des_encrypt_block",
  45. ]
  46. #=============================================================================
  47. # constants
  48. #=============================================================================
  49. # masks/upper limits for various integer sizes
  50. INT_24_MASK = 0xffffff
  51. INT_56_MASK = 0xffffffffffffff
  52. INT_64_MASK = 0xffffffffffffffff
  53. # mask to clear parity bits from 64-bit key
  54. _KDATA_MASK = 0xfefefefefefefefe
  55. _KPARITY_MASK = 0x0101010101010101
  56. # mask used to setup key schedule
  57. _KS_MASK = 0xfcfcfcfcffffffff
  58. #=============================================================================
  59. # static DES tables
  60. #=============================================================================
  61. # placeholders filled in by _load_tables()
  62. PCXROT = IE3264 = SPE = CF6464 = None
  63. def _load_tables():
  64. """delay loading tables until they are actually needed"""
  65. global PCXROT, IE3264, SPE, CF6464
  66. #---------------------------------------------------------------
  67. # Initial key schedule permutation
  68. # PC1ROT - bit reverse, then PC1, then Rotate, then PC2
  69. #---------------------------------------------------------------
  70. # NOTE: this was reordered from original table to make perm3264 logic simpler
  71. PC1ROT=(
  72. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000002000, 0x0000000000002000,
  73. 0x0000000000000020, 0x0000000000000020, 0x0000000000002020, 0x0000000000002020,
  74. 0x0000000000000400, 0x0000000000000400, 0x0000000000002400, 0x0000000000002400,
  75. 0x0000000000000420, 0x0000000000000420, 0x0000000000002420, 0x0000000000002420, ),
  76. ( 0x0000000000000000, 0x2000000000000000, 0x0000000400000000, 0x2000000400000000,
  77. 0x0000800000000000, 0x2000800000000000, 0x0000800400000000, 0x2000800400000000,
  78. 0x0008000000000000, 0x2008000000000000, 0x0008000400000000, 0x2008000400000000,
  79. 0x0008800000000000, 0x2008800000000000, 0x0008800400000000, 0x2008800400000000, ),
  80. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000040, 0x0000000000000040,
  81. 0x0000000020000000, 0x0000000020000000, 0x0000000020000040, 0x0000000020000040,
  82. 0x0000000000200000, 0x0000000000200000, 0x0000000000200040, 0x0000000000200040,
  83. 0x0000000020200000, 0x0000000020200000, 0x0000000020200040, 0x0000000020200040, ),
  84. ( 0x0000000000000000, 0x0002000000000000, 0x0800000000000000, 0x0802000000000000,
  85. 0x0100000000000000, 0x0102000000000000, 0x0900000000000000, 0x0902000000000000,
  86. 0x4000000000000000, 0x4002000000000000, 0x4800000000000000, 0x4802000000000000,
  87. 0x4100000000000000, 0x4102000000000000, 0x4900000000000000, 0x4902000000000000, ),
  88. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000040000, 0x0000000000040000,
  89. 0x0000020000000000, 0x0000020000000000, 0x0000020000040000, 0x0000020000040000,
  90. 0x0000000000000004, 0x0000000000000004, 0x0000000000040004, 0x0000000000040004,
  91. 0x0000020000000004, 0x0000020000000004, 0x0000020000040004, 0x0000020000040004, ),
  92. ( 0x0000000000000000, 0x0000400000000000, 0x0200000000000000, 0x0200400000000000,
  93. 0x0080000000000000, 0x0080400000000000, 0x0280000000000000, 0x0280400000000000,
  94. 0x0000008000000000, 0x0000408000000000, 0x0200008000000000, 0x0200408000000000,
  95. 0x0080008000000000, 0x0080408000000000, 0x0280008000000000, 0x0280408000000000, ),
  96. ( 0x0000000000000000, 0x0000000000000000, 0x0000000010000000, 0x0000000010000000,
  97. 0x0000000000001000, 0x0000000000001000, 0x0000000010001000, 0x0000000010001000,
  98. 0x0000000040000000, 0x0000000040000000, 0x0000000050000000, 0x0000000050000000,
  99. 0x0000000040001000, 0x0000000040001000, 0x0000000050001000, 0x0000000050001000, ),
  100. ( 0x0000000000000000, 0x0000001000000000, 0x0000080000000000, 0x0000081000000000,
  101. 0x1000000000000000, 0x1000001000000000, 0x1000080000000000, 0x1000081000000000,
  102. 0x0004000000000000, 0x0004001000000000, 0x0004080000000000, 0x0004081000000000,
  103. 0x1004000000000000, 0x1004001000000000, 0x1004080000000000, 0x1004081000000000, ),
  104. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000080, 0x0000000000000080,
  105. 0x0000000000080000, 0x0000000000080000, 0x0000000000080080, 0x0000000000080080,
  106. 0x0000000000800000, 0x0000000000800000, 0x0000000000800080, 0x0000000000800080,
  107. 0x0000000000880000, 0x0000000000880000, 0x0000000000880080, 0x0000000000880080, ),
  108. ( 0x0000000000000000, 0x0000000008000000, 0x0000002000000000, 0x0000002008000000,
  109. 0x0000100000000000, 0x0000100008000000, 0x0000102000000000, 0x0000102008000000,
  110. 0x0000200000000000, 0x0000200008000000, 0x0000202000000000, 0x0000202008000000,
  111. 0x0000300000000000, 0x0000300008000000, 0x0000302000000000, 0x0000302008000000, ),
  112. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000400000, 0x0000000000400000,
  113. 0x0000000004000000, 0x0000000004000000, 0x0000000004400000, 0x0000000004400000,
  114. 0x0000000000000800, 0x0000000000000800, 0x0000000000400800, 0x0000000000400800,
  115. 0x0000000004000800, 0x0000000004000800, 0x0000000004400800, 0x0000000004400800, ),
  116. ( 0x0000000000000000, 0x0000000000008000, 0x0040000000000000, 0x0040000000008000,
  117. 0x0000004000000000, 0x0000004000008000, 0x0040004000000000, 0x0040004000008000,
  118. 0x8000000000000000, 0x8000000000008000, 0x8040000000000000, 0x8040000000008000,
  119. 0x8000004000000000, 0x8000004000008000, 0x8040004000000000, 0x8040004000008000, ),
  120. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000004000, 0x0000000000004000,
  121. 0x0000000000000008, 0x0000000000000008, 0x0000000000004008, 0x0000000000004008,
  122. 0x0000000000000010, 0x0000000000000010, 0x0000000000004010, 0x0000000000004010,
  123. 0x0000000000000018, 0x0000000000000018, 0x0000000000004018, 0x0000000000004018, ),
  124. ( 0x0000000000000000, 0x0000000200000000, 0x0001000000000000, 0x0001000200000000,
  125. 0x0400000000000000, 0x0400000200000000, 0x0401000000000000, 0x0401000200000000,
  126. 0x0020000000000000, 0x0020000200000000, 0x0021000000000000, 0x0021000200000000,
  127. 0x0420000000000000, 0x0420000200000000, 0x0421000000000000, 0x0421000200000000, ),
  128. ( 0x0000000000000000, 0x0000000000000000, 0x0000010000000000, 0x0000010000000000,
  129. 0x0000000100000000, 0x0000000100000000, 0x0000010100000000, 0x0000010100000000,
  130. 0x0000000000100000, 0x0000000000100000, 0x0000010000100000, 0x0000010000100000,
  131. 0x0000000100100000, 0x0000000100100000, 0x0000010100100000, 0x0000010100100000, ),
  132. ( 0x0000000000000000, 0x0000000080000000, 0x0000040000000000, 0x0000040080000000,
  133. 0x0010000000000000, 0x0010000080000000, 0x0010040000000000, 0x0010040080000000,
  134. 0x0000000800000000, 0x0000000880000000, 0x0000040800000000, 0x0000040880000000,
  135. 0x0010000800000000, 0x0010000880000000, 0x0010040800000000, 0x0010040880000000, ),
  136. )
  137. #---------------------------------------------------------------
  138. # Subsequent key schedule rotation permutations
  139. # PC2ROT - PC2 inverse, then Rotate, then PC2
  140. #---------------------------------------------------------------
  141. # NOTE: this was reordered from original table to make perm3264 logic simpler
  142. PC2ROTA=(
  143. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  144. 0x0000000000200000, 0x0000000000200000, 0x0000000000200000, 0x0000000000200000,
  145. 0x0000000004000000, 0x0000000004000000, 0x0000000004000000, 0x0000000004000000,
  146. 0x0000000004200000, 0x0000000004200000, 0x0000000004200000, 0x0000000004200000, ),
  147. ( 0x0000000000000000, 0x0000000000000800, 0x0000010000000000, 0x0000010000000800,
  148. 0x0000000000002000, 0x0000000000002800, 0x0000010000002000, 0x0000010000002800,
  149. 0x0000000010000000, 0x0000000010000800, 0x0000010010000000, 0x0000010010000800,
  150. 0x0000000010002000, 0x0000000010002800, 0x0000010010002000, 0x0000010010002800, ),
  151. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  152. 0x0000000100000000, 0x0000000100000000, 0x0000000100000000, 0x0000000100000000,
  153. 0x0000000000800000, 0x0000000000800000, 0x0000000000800000, 0x0000000000800000,
  154. 0x0000000100800000, 0x0000000100800000, 0x0000000100800000, 0x0000000100800000, ),
  155. ( 0x0000000000000000, 0x0000020000000000, 0x0000000080000000, 0x0000020080000000,
  156. 0x0000000000400000, 0x0000020000400000, 0x0000000080400000, 0x0000020080400000,
  157. 0x0000000008000000, 0x0000020008000000, 0x0000000088000000, 0x0000020088000000,
  158. 0x0000000008400000, 0x0000020008400000, 0x0000000088400000, 0x0000020088400000, ),
  159. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  160. 0x0000000000000040, 0x0000000000000040, 0x0000000000000040, 0x0000000000000040,
  161. 0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
  162. 0x0000000000001040, 0x0000000000001040, 0x0000000000001040, 0x0000000000001040, ),
  163. ( 0x0000000000000000, 0x0000000000000010, 0x0000000000000400, 0x0000000000000410,
  164. 0x0000000000000080, 0x0000000000000090, 0x0000000000000480, 0x0000000000000490,
  165. 0x0000000040000000, 0x0000000040000010, 0x0000000040000400, 0x0000000040000410,
  166. 0x0000000040000080, 0x0000000040000090, 0x0000000040000480, 0x0000000040000490, ),
  167. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  168. 0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
  169. 0x0000000000100000, 0x0000000000100000, 0x0000000000100000, 0x0000000000100000,
  170. 0x0000000000180000, 0x0000000000180000, 0x0000000000180000, 0x0000000000180000, ),
  171. ( 0x0000000000000000, 0x0000000000040000, 0x0000000000000020, 0x0000000000040020,
  172. 0x0000000000000004, 0x0000000000040004, 0x0000000000000024, 0x0000000000040024,
  173. 0x0000000200000000, 0x0000000200040000, 0x0000000200000020, 0x0000000200040020,
  174. 0x0000000200000004, 0x0000000200040004, 0x0000000200000024, 0x0000000200040024, ),
  175. ( 0x0000000000000000, 0x0000000000000008, 0x0000000000008000, 0x0000000000008008,
  176. 0x0010000000000000, 0x0010000000000008, 0x0010000000008000, 0x0010000000008008,
  177. 0x0020000000000000, 0x0020000000000008, 0x0020000000008000, 0x0020000000008008,
  178. 0x0030000000000000, 0x0030000000000008, 0x0030000000008000, 0x0030000000008008, ),
  179. ( 0x0000000000000000, 0x0000400000000000, 0x0000080000000000, 0x0000480000000000,
  180. 0x0000100000000000, 0x0000500000000000, 0x0000180000000000, 0x0000580000000000,
  181. 0x4000000000000000, 0x4000400000000000, 0x4000080000000000, 0x4000480000000000,
  182. 0x4000100000000000, 0x4000500000000000, 0x4000180000000000, 0x4000580000000000, ),
  183. ( 0x0000000000000000, 0x0000000000004000, 0x0000000020000000, 0x0000000020004000,
  184. 0x0001000000000000, 0x0001000000004000, 0x0001000020000000, 0x0001000020004000,
  185. 0x0200000000000000, 0x0200000000004000, 0x0200000020000000, 0x0200000020004000,
  186. 0x0201000000000000, 0x0201000000004000, 0x0201000020000000, 0x0201000020004000, ),
  187. ( 0x0000000000000000, 0x1000000000000000, 0x0004000000000000, 0x1004000000000000,
  188. 0x0002000000000000, 0x1002000000000000, 0x0006000000000000, 0x1006000000000000,
  189. 0x0000000800000000, 0x1000000800000000, 0x0004000800000000, 0x1004000800000000,
  190. 0x0002000800000000, 0x1002000800000000, 0x0006000800000000, 0x1006000800000000, ),
  191. ( 0x0000000000000000, 0x0040000000000000, 0x2000000000000000, 0x2040000000000000,
  192. 0x0000008000000000, 0x0040008000000000, 0x2000008000000000, 0x2040008000000000,
  193. 0x0000001000000000, 0x0040001000000000, 0x2000001000000000, 0x2040001000000000,
  194. 0x0000009000000000, 0x0040009000000000, 0x2000009000000000, 0x2040009000000000, ),
  195. ( 0x0000000000000000, 0x0400000000000000, 0x8000000000000000, 0x8400000000000000,
  196. 0x0000002000000000, 0x0400002000000000, 0x8000002000000000, 0x8400002000000000,
  197. 0x0100000000000000, 0x0500000000000000, 0x8100000000000000, 0x8500000000000000,
  198. 0x0100002000000000, 0x0500002000000000, 0x8100002000000000, 0x8500002000000000, ),
  199. ( 0x0000000000000000, 0x0000800000000000, 0x0800000000000000, 0x0800800000000000,
  200. 0x0000004000000000, 0x0000804000000000, 0x0800004000000000, 0x0800804000000000,
  201. 0x0000000400000000, 0x0000800400000000, 0x0800000400000000, 0x0800800400000000,
  202. 0x0000004400000000, 0x0000804400000000, 0x0800004400000000, 0x0800804400000000, ),
  203. ( 0x0000000000000000, 0x0080000000000000, 0x0000040000000000, 0x0080040000000000,
  204. 0x0008000000000000, 0x0088000000000000, 0x0008040000000000, 0x0088040000000000,
  205. 0x0000200000000000, 0x0080200000000000, 0x0000240000000000, 0x0080240000000000,
  206. 0x0008200000000000, 0x0088200000000000, 0x0008240000000000, 0x0088240000000000, ),
  207. )
  208. # NOTE: this was reordered from original table to make perm3264 logic simpler
  209. PC2ROTB=(
  210. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  211. 0x0000000000000400, 0x0000000000000400, 0x0000000000000400, 0x0000000000000400,
  212. 0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
  213. 0x0000000000080400, 0x0000000000080400, 0x0000000000080400, 0x0000000000080400, ),
  214. ( 0x0000000000000000, 0x0000000000800000, 0x0000000000004000, 0x0000000000804000,
  215. 0x0000000080000000, 0x0000000080800000, 0x0000000080004000, 0x0000000080804000,
  216. 0x0000000000040000, 0x0000000000840000, 0x0000000000044000, 0x0000000000844000,
  217. 0x0000000080040000, 0x0000000080840000, 0x0000000080044000, 0x0000000080844000, ),
  218. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  219. 0x0000000000000008, 0x0000000000000008, 0x0000000000000008, 0x0000000000000008,
  220. 0x0000000040000000, 0x0000000040000000, 0x0000000040000000, 0x0000000040000000,
  221. 0x0000000040000008, 0x0000000040000008, 0x0000000040000008, 0x0000000040000008, ),
  222. ( 0x0000000000000000, 0x0000000020000000, 0x0000000200000000, 0x0000000220000000,
  223. 0x0000000000000080, 0x0000000020000080, 0x0000000200000080, 0x0000000220000080,
  224. 0x0000000000100000, 0x0000000020100000, 0x0000000200100000, 0x0000000220100000,
  225. 0x0000000000100080, 0x0000000020100080, 0x0000000200100080, 0x0000000220100080, ),
  226. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  227. 0x0000000000002000, 0x0000000000002000, 0x0000000000002000, 0x0000000000002000,
  228. 0x0000020000000000, 0x0000020000000000, 0x0000020000000000, 0x0000020000000000,
  229. 0x0000020000002000, 0x0000020000002000, 0x0000020000002000, 0x0000020000002000, ),
  230. ( 0x0000000000000000, 0x0000000000000800, 0x0000000100000000, 0x0000000100000800,
  231. 0x0000000010000000, 0x0000000010000800, 0x0000000110000000, 0x0000000110000800,
  232. 0x0000000000000004, 0x0000000000000804, 0x0000000100000004, 0x0000000100000804,
  233. 0x0000000010000004, 0x0000000010000804, 0x0000000110000004, 0x0000000110000804, ),
  234. ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  235. 0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
  236. 0x0000000000000010, 0x0000000000000010, 0x0000000000000010, 0x0000000000000010,
  237. 0x0000000000001010, 0x0000000000001010, 0x0000000000001010, 0x0000000000001010, ),
  238. ( 0x0000000000000000, 0x0000000000000040, 0x0000010000000000, 0x0000010000000040,
  239. 0x0000000000200000, 0x0000000000200040, 0x0000010000200000, 0x0000010000200040,
  240. 0x0000000000008000, 0x0000000000008040, 0x0000010000008000, 0x0000010000008040,
  241. 0x0000000000208000, 0x0000000000208040, 0x0000010000208000, 0x0000010000208040, ),
  242. ( 0x0000000000000000, 0x0000000004000000, 0x0000000008000000, 0x000000000c000000,
  243. 0x0400000000000000, 0x0400000004000000, 0x0400000008000000, 0x040000000c000000,
  244. 0x8000000000000000, 0x8000000004000000, 0x8000000008000000, 0x800000000c000000,
  245. 0x8400000000000000, 0x8400000004000000, 0x8400000008000000, 0x840000000c000000, ),
  246. ( 0x0000000000000000, 0x0002000000000000, 0x0200000000000000, 0x0202000000000000,
  247. 0x1000000000000000, 0x1002000000000000, 0x1200000000000000, 0x1202000000000000,
  248. 0x0008000000000000, 0x000a000000000000, 0x0208000000000000, 0x020a000000000000,
  249. 0x1008000000000000, 0x100a000000000000, 0x1208000000000000, 0x120a000000000000, ),
  250. ( 0x0000000000000000, 0x0000000000400000, 0x0000000000000020, 0x0000000000400020,
  251. 0x0040000000000000, 0x0040000000400000, 0x0040000000000020, 0x0040000000400020,
  252. 0x0800000000000000, 0x0800000000400000, 0x0800000000000020, 0x0800000000400020,
  253. 0x0840000000000000, 0x0840000000400000, 0x0840000000000020, 0x0840000000400020, ),
  254. ( 0x0000000000000000, 0x0080000000000000, 0x0000008000000000, 0x0080008000000000,
  255. 0x2000000000000000, 0x2080000000000000, 0x2000008000000000, 0x2080008000000000,
  256. 0x0020000000000000, 0x00a0000000000000, 0x0020008000000000, 0x00a0008000000000,
  257. 0x2020000000000000, 0x20a0000000000000, 0x2020008000000000, 0x20a0008000000000, ),
  258. ( 0x0000000000000000, 0x0000002000000000, 0x0000040000000000, 0x0000042000000000,
  259. 0x4000000000000000, 0x4000002000000000, 0x4000040000000000, 0x4000042000000000,
  260. 0x0000400000000000, 0x0000402000000000, 0x0000440000000000, 0x0000442000000000,
  261. 0x4000400000000000, 0x4000402000000000, 0x4000440000000000, 0x4000442000000000, ),
  262. ( 0x0000000000000000, 0x0000004000000000, 0x0000200000000000, 0x0000204000000000,
  263. 0x0000080000000000, 0x0000084000000000, 0x0000280000000000, 0x0000284000000000,
  264. 0x0000800000000000, 0x0000804000000000, 0x0000a00000000000, 0x0000a04000000000,
  265. 0x0000880000000000, 0x0000884000000000, 0x0000a80000000000, 0x0000a84000000000, ),
  266. ( 0x0000000000000000, 0x0000000800000000, 0x0000000400000000, 0x0000000c00000000,
  267. 0x0000100000000000, 0x0000100800000000, 0x0000100400000000, 0x0000100c00000000,
  268. 0x0010000000000000, 0x0010000800000000, 0x0010000400000000, 0x0010000c00000000,
  269. 0x0010100000000000, 0x0010100800000000, 0x0010100400000000, 0x0010100c00000000, ),
  270. ( 0x0000000000000000, 0x0100000000000000, 0x0001000000000000, 0x0101000000000000,
  271. 0x0000001000000000, 0x0100001000000000, 0x0001001000000000, 0x0101001000000000,
  272. 0x0004000000000000, 0x0104000000000000, 0x0005000000000000, 0x0105000000000000,
  273. 0x0004001000000000, 0x0104001000000000, 0x0005001000000000, 0x0105001000000000, ),
  274. )
  275. #---------------------------------------------------------------
  276. # PCXROT - PC1ROT, PC2ROTA, PC2ROTB listed in order
  277. # of the PC1 rotation schedule, as used by des_setkey
  278. #---------------------------------------------------------------
  279. ##ROTATES = (1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1)
  280. ##PCXROT = (
  281. ## PC1ROT, PC2ROTA, PC2ROTB, PC2ROTB,
  282. ## PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTB,
  283. ## PC2ROTA, PC2ROTB, PC2ROTB, PC2ROTB,
  284. ## PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTA,
  285. ## )
  286. # NOTE: modified PCXROT to contain entrys broken into pairs,
  287. # to help generate them in format best used by encoder.
  288. PCXROT = (
  289. (PC1ROT, PC2ROTA), (PC2ROTB, PC2ROTB),
  290. (PC2ROTB, PC2ROTB), (PC2ROTB, PC2ROTB),
  291. (PC2ROTA, PC2ROTB), (PC2ROTB, PC2ROTB),
  292. (PC2ROTB, PC2ROTB), (PC2ROTB, PC2ROTA),
  293. )
  294. #---------------------------------------------------------------
  295. # Bit reverse, intial permupation, expantion
  296. # Initial permutation/expansion table
  297. #---------------------------------------------------------------
  298. # NOTE: this was reordered from original table to make perm3264 logic simpler
  299. IE3264=(
  300. ( 0x0000000000000000, 0x0000000000800800, 0x0000000000008008, 0x0000000000808808,
  301. 0x0000008008000000, 0x0000008008800800, 0x0000008008008008, 0x0000008008808808,
  302. 0x0000000080080000, 0x0000000080880800, 0x0000000080088008, 0x0000000080888808,
  303. 0x0000008088080000, 0x0000008088880800, 0x0000008088088008, 0x0000008088888808, ),
  304. ( 0x0000000000000000, 0x0080080000000000, 0x0000800800000000, 0x0080880800000000,
  305. 0x0800000000000080, 0x0880080000000080, 0x0800800800000080, 0x0880880800000080,
  306. 0x8008000000000000, 0x8088080000000000, 0x8008800800000000, 0x8088880800000000,
  307. 0x8808000000000080, 0x8888080000000080, 0x8808800800000080, 0x8888880800000080, ),
  308. ( 0x0000000000000000, 0x0000000000001000, 0x0000000000000010, 0x0000000000001010,
  309. 0x0000000010000000, 0x0000000010001000, 0x0000000010000010, 0x0000000010001010,
  310. 0x0000000000100000, 0x0000000000101000, 0x0000000000100010, 0x0000000000101010,
  311. 0x0000000010100000, 0x0000000010101000, 0x0000000010100010, 0x0000000010101010, ),
  312. ( 0x0000000000000000, 0x0000100000000000, 0x0000001000000000, 0x0000101000000000,
  313. 0x1000000000000000, 0x1000100000000000, 0x1000001000000000, 0x1000101000000000,
  314. 0x0010000000000000, 0x0010100000000000, 0x0010001000000000, 0x0010101000000000,
  315. 0x1010000000000000, 0x1010100000000000, 0x1010001000000000, 0x1010101000000000, ),
  316. ( 0x0000000000000000, 0x0000000000002000, 0x0000000000000020, 0x0000000000002020,
  317. 0x0000000020000000, 0x0000000020002000, 0x0000000020000020, 0x0000000020002020,
  318. 0x0000000000200000, 0x0000000000202000, 0x0000000000200020, 0x0000000000202020,
  319. 0x0000000020200000, 0x0000000020202000, 0x0000000020200020, 0x0000000020202020, ),
  320. ( 0x0000000000000000, 0x0000200000000000, 0x0000002000000000, 0x0000202000000000,
  321. 0x2000000000000000, 0x2000200000000000, 0x2000002000000000, 0x2000202000000000,
  322. 0x0020000000000000, 0x0020200000000000, 0x0020002000000000, 0x0020202000000000,
  323. 0x2020000000000000, 0x2020200000000000, 0x2020002000000000, 0x2020202000000000, ),
  324. ( 0x0000000000000000, 0x0000000000004004, 0x0400000000000040, 0x0400000000004044,
  325. 0x0000000040040000, 0x0000000040044004, 0x0400000040040040, 0x0400000040044044,
  326. 0x0000000000400400, 0x0000000000404404, 0x0400000000400440, 0x0400000000404444,
  327. 0x0000000040440400, 0x0000000040444404, 0x0400000040440440, 0x0400000040444444, ),
  328. ( 0x0000000000000000, 0x0000400400000000, 0x0000004004000000, 0x0000404404000000,
  329. 0x4004000000000000, 0x4004400400000000, 0x4004004004000000, 0x4004404404000000,
  330. 0x0040040000000000, 0x0040440400000000, 0x0040044004000000, 0x0040444404000000,
  331. 0x4044040000000000, 0x4044440400000000, 0x4044044004000000, 0x4044444404000000, ),
  332. )
  333. #---------------------------------------------------------------
  334. # Table that combines the S, P, and E operations.
  335. #---------------------------------------------------------------
  336. SPE=(
  337. ( 0x0080088008200000, 0x0000008008000000, 0x0000000000200020, 0x0080088008200020,
  338. 0x0000000000200000, 0x0080088008000020, 0x0000008008000020, 0x0000000000200020,
  339. 0x0080088008000020, 0x0080088008200000, 0x0000008008200000, 0x0080080000000020,
  340. 0x0080080000200020, 0x0000000000200000, 0x0000000000000000, 0x0000008008000020,
  341. 0x0000008008000000, 0x0000000000000020, 0x0080080000200000, 0x0080088008000000,
  342. 0x0080088008200020, 0x0000008008200000, 0x0080080000000020, 0x0080080000200000,
  343. 0x0000000000000020, 0x0080080000000000, 0x0080088008000000, 0x0000008008200020,
  344. 0x0080080000000000, 0x0080080000200020, 0x0000008008200020, 0x0000000000000000,
  345. 0x0000000000000000, 0x0080088008200020, 0x0080080000200000, 0x0000008008000020,
  346. 0x0080088008200000, 0x0000008008000000, 0x0080080000000020, 0x0080080000200000,
  347. 0x0000008008200020, 0x0080080000000000, 0x0080088008000000, 0x0000000000200020,
  348. 0x0080088008000020, 0x0000000000000020, 0x0000000000200020, 0x0000008008200000,
  349. 0x0080088008200020, 0x0080088008000000, 0x0000008008200000, 0x0080080000200020,
  350. 0x0000000000200000, 0x0080080000000020, 0x0000008008000020, 0x0000000000000000,
  351. 0x0000008008000000, 0x0000000000200000, 0x0080080000200020, 0x0080088008200000,
  352. 0x0000000000000020, 0x0000008008200020, 0x0080080000000000, 0x0080088008000020, ),
  353. ( 0x1000800810004004, 0x0000000000000000, 0x0000800810000000, 0x0000000010004004,
  354. 0x1000000000004004, 0x1000800800000000, 0x0000800800004004, 0x0000800810000000,
  355. 0x0000800800000000, 0x1000000010004004, 0x1000000000000000, 0x0000800800004004,
  356. 0x1000000010000000, 0x0000800810004004, 0x0000000010004004, 0x1000000000000000,
  357. 0x0000000010000000, 0x1000800800004004, 0x1000000010004004, 0x0000800800000000,
  358. 0x1000800810000000, 0x0000000000004004, 0x0000000000000000, 0x1000000010000000,
  359. 0x1000800800004004, 0x1000800810000000, 0x0000800810004004, 0x1000000000004004,
  360. 0x0000000000004004, 0x0000000010000000, 0x1000800800000000, 0x1000800810004004,
  361. 0x1000000010000000, 0x0000800810004004, 0x0000800800004004, 0x1000800810000000,
  362. 0x1000800810004004, 0x1000000010000000, 0x1000000000004004, 0x0000000000000000,
  363. 0x0000000000004004, 0x1000800800000000, 0x0000000010000000, 0x1000000010004004,
  364. 0x0000800800000000, 0x0000000000004004, 0x1000800810000000, 0x1000800800004004,
  365. 0x0000800810004004, 0x0000800800000000, 0x0000000000000000, 0x1000000000004004,
  366. 0x1000000000000000, 0x1000800810004004, 0x0000800810000000, 0x0000000010004004,
  367. 0x1000000010004004, 0x0000000010000000, 0x1000800800000000, 0x0000800800004004,
  368. 0x1000800800004004, 0x1000000000000000, 0x0000000010004004, 0x0000800810000000, ),
  369. ( 0x0000000000400410, 0x0010004004400400, 0x0010000000000000, 0x0010000000400410,
  370. 0x0000004004000010, 0x0000000000400400, 0x0010000000400410, 0x0010004004000000,
  371. 0x0010000000400400, 0x0000004004000000, 0x0000004004400400, 0x0000000000000010,
  372. 0x0010004004400410, 0x0010000000000010, 0x0000000000000010, 0x0000004004400410,
  373. 0x0000000000000000, 0x0000004004000010, 0x0010004004400400, 0x0010000000000000,
  374. 0x0010000000000010, 0x0010004004400410, 0x0000004004000000, 0x0000000000400410,
  375. 0x0000004004400410, 0x0010000000400400, 0x0010004004000010, 0x0000004004400400,
  376. 0x0010004004000000, 0x0000000000000000, 0x0000000000400400, 0x0010004004000010,
  377. 0x0010004004400400, 0x0010000000000000, 0x0000000000000010, 0x0000004004000000,
  378. 0x0010000000000010, 0x0000004004000010, 0x0000004004400400, 0x0010000000400410,
  379. 0x0000000000000000, 0x0010004004400400, 0x0010004004000000, 0x0000004004400410,
  380. 0x0000004004000010, 0x0000000000400400, 0x0010004004400410, 0x0000000000000010,
  381. 0x0010004004000010, 0x0000000000400410, 0x0000000000400400, 0x0010004004400410,
  382. 0x0000004004000000, 0x0010000000400400, 0x0010000000400410, 0x0010004004000000,
  383. 0x0010000000400400, 0x0000000000000000, 0x0000004004400410, 0x0010000000000010,
  384. 0x0000000000400410, 0x0010004004000010, 0x0010000000000000, 0x0000004004400400, ),
  385. ( 0x0800100040040080, 0x0000100000001000, 0x0800000000000080, 0x0800100040041080,
  386. 0x0000000000000000, 0x0000000040041000, 0x0800100000001080, 0x0800000040040080,
  387. 0x0000100040041000, 0x0800000000001080, 0x0000000000001000, 0x0800100000000080,
  388. 0x0800000000001080, 0x0800100040040080, 0x0000000040040000, 0x0000000000001000,
  389. 0x0800000040041080, 0x0000100040040000, 0x0000100000000000, 0x0800000000000080,
  390. 0x0000100040040000, 0x0800100000001080, 0x0000000040041000, 0x0000100000000000,
  391. 0x0800100000000080, 0x0000000000000000, 0x0800000040040080, 0x0000100040041000,
  392. 0x0000100000001000, 0x0800000040041080, 0x0800100040041080, 0x0000000040040000,
  393. 0x0800000040041080, 0x0800100000000080, 0x0000000040040000, 0x0800000000001080,
  394. 0x0000100040040000, 0x0000100000001000, 0x0800000000000080, 0x0000000040041000,
  395. 0x0800100000001080, 0x0000000000000000, 0x0000100000000000, 0x0800000040040080,
  396. 0x0000000000000000, 0x0800000040041080, 0x0000100040041000, 0x0000100000000000,
  397. 0x0000000000001000, 0x0800100040041080, 0x0800100040040080, 0x0000000040040000,
  398. 0x0800100040041080, 0x0800000000000080, 0x0000100000001000, 0x0800100040040080,
  399. 0x0800000040040080, 0x0000100040040000, 0x0000000040041000, 0x0800100000001080,
  400. 0x0800100000000080, 0x0000000000001000, 0x0800000000001080, 0x0000100040041000, ),
  401. ( 0x0000000000800800, 0x0000001000000000, 0x0040040000000000, 0x2040041000800800,
  402. 0x2000001000800800, 0x0040040000800800, 0x2040041000000000, 0x0000001000800800,
  403. 0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040041000000000,
  404. 0x2040040000800800, 0x2000001000800800, 0x0040041000800800, 0x0000000000000000,
  405. 0x0040041000000000, 0x0000000000800800, 0x2000001000000000, 0x2040040000000000,
  406. 0x0040040000800800, 0x2040041000000000, 0x0000000000000000, 0x2000000000800800,
  407. 0x2000000000000000, 0x2040040000800800, 0x2040041000800800, 0x2000001000000000,
  408. 0x0000001000800800, 0x0040040000000000, 0x2040040000000000, 0x0040041000800800,
  409. 0x0040041000800800, 0x2040040000800800, 0x2000001000000000, 0x0000001000800800,
  410. 0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040040000800800,
  411. 0x0000000000800800, 0x0040041000000000, 0x2040041000800800, 0x0000000000000000,
  412. 0x2040041000000000, 0x0000000000800800, 0x0040040000000000, 0x2000001000000000,
  413. 0x2040040000800800, 0x0040040000000000, 0x0000000000000000, 0x2040041000800800,
  414. 0x2000001000800800, 0x0040041000800800, 0x2040040000000000, 0x0000001000000000,
  415. 0x0040041000000000, 0x2000001000800800, 0x0040040000800800, 0x2040040000000000,
  416. 0x2000000000000000, 0x2040041000000000, 0x0000001000800800, 0x2000000000800800, ),
  417. ( 0x4004000000008008, 0x4004000020000000, 0x0000000000000000, 0x0000200020008008,
  418. 0x4004000020000000, 0x0000200000000000, 0x4004200000008008, 0x0000000020000000,
  419. 0x4004200000000000, 0x4004200020008008, 0x0000200020000000, 0x0000000000008008,
  420. 0x0000200000008008, 0x4004000000008008, 0x0000000020008008, 0x4004200020000000,
  421. 0x0000000020000000, 0x4004200000008008, 0x4004000020008008, 0x0000000000000000,
  422. 0x0000200000000000, 0x4004000000000000, 0x0000200020008008, 0x4004000020008008,
  423. 0x4004200020008008, 0x0000000020008008, 0x0000000000008008, 0x4004200000000000,
  424. 0x4004000000000000, 0x0000200020000000, 0x4004200020000000, 0x0000200000008008,
  425. 0x4004200000000000, 0x0000000000008008, 0x0000200000008008, 0x4004200020000000,
  426. 0x0000200020008008, 0x4004000020000000, 0x0000000000000000, 0x0000200000008008,
  427. 0x0000000000008008, 0x0000200000000000, 0x4004000020008008, 0x0000000020000000,
  428. 0x4004000020000000, 0x4004200020008008, 0x0000200020000000, 0x4004000000000000,
  429. 0x4004200020008008, 0x0000200020000000, 0x0000000020000000, 0x4004200000008008,
  430. 0x4004000000008008, 0x0000000020008008, 0x4004200020000000, 0x0000000000000000,
  431. 0x0000200000000000, 0x4004000000008008, 0x4004200000008008, 0x0000200020008008,
  432. 0x0000000020008008, 0x4004200000000000, 0x4004000000000000, 0x4004000020008008, ),
  433. ( 0x0000400400000000, 0x0020000000000000, 0x0020000000100000, 0x0400000000100040,
  434. 0x0420400400100040, 0x0400400400000040, 0x0020400400000000, 0x0000000000000000,
  435. 0x0000000000100000, 0x0420000000100040, 0x0420000000000040, 0x0000400400100000,
  436. 0x0400000000000040, 0x0020400400100000, 0x0000400400100000, 0x0420000000000040,
  437. 0x0420000000100040, 0x0000400400000000, 0x0400400400000040, 0x0420400400100040,
  438. 0x0000000000000000, 0x0020000000100000, 0x0400000000100040, 0x0020400400000000,
  439. 0x0400400400100040, 0x0420400400000040, 0x0020400400100000, 0x0400000000000040,
  440. 0x0420400400000040, 0x0400400400100040, 0x0020000000000000, 0x0000000000100000,
  441. 0x0420400400000040, 0x0000400400100000, 0x0400400400100040, 0x0420000000000040,
  442. 0x0000400400000000, 0x0020000000000000, 0x0000000000100000, 0x0400400400100040,
  443. 0x0420000000100040, 0x0420400400000040, 0x0020400400000000, 0x0000000000000000,
  444. 0x0020000000000000, 0x0400000000100040, 0x0400000000000040, 0x0020000000100000,
  445. 0x0000000000000000, 0x0420000000100040, 0x0020000000100000, 0x0020400400000000,
  446. 0x0420000000000040, 0x0000400400000000, 0x0420400400100040, 0x0000000000100000,
  447. 0x0020400400100000, 0x0400000000000040, 0x0400400400000040, 0x0420400400100040,
  448. 0x0400000000100040, 0x0020400400100000, 0x0000400400100000, 0x0400400400000040, ),
  449. ( 0x8008000080082000, 0x0000002080082000, 0x8008002000000000, 0x0000000000000000,
  450. 0x0000002000002000, 0x8008000080080000, 0x0000000080082000, 0x8008002080082000,
  451. 0x8008000000000000, 0x0000000000002000, 0x0000002080080000, 0x8008002000000000,
  452. 0x8008002080080000, 0x8008002000002000, 0x8008000000002000, 0x0000000080082000,
  453. 0x0000002000000000, 0x8008002080080000, 0x8008000080080000, 0x0000002000002000,
  454. 0x8008002080082000, 0x8008000000002000, 0x0000000000000000, 0x0000002080080000,
  455. 0x0000000000002000, 0x0000000080080000, 0x8008002000002000, 0x8008000080082000,
  456. 0x0000000080080000, 0x0000002000000000, 0x0000002080082000, 0x8008000000000000,
  457. 0x0000000080080000, 0x0000002000000000, 0x8008000000002000, 0x8008002080082000,
  458. 0x8008002000000000, 0x0000000000002000, 0x0000000000000000, 0x0000002080080000,
  459. 0x8008000080082000, 0x8008002000002000, 0x0000002000002000, 0x8008000080080000,
  460. 0x0000002080082000, 0x8008000000000000, 0x8008000080080000, 0x0000002000002000,
  461. 0x8008002080082000, 0x0000000080080000, 0x0000000080082000, 0x8008000000002000,
  462. 0x0000002080080000, 0x8008002000000000, 0x8008002000002000, 0x0000000080082000,
  463. 0x8008000000000000, 0x0000002080082000, 0x8008002080080000, 0x0000000000000000,
  464. 0x0000000000002000, 0x8008000080082000, 0x0000002000000000, 0x8008002080080000, ),
  465. )
  466. #---------------------------------------------------------------
  467. # compressed/interleaved => final permutation table
  468. # Compression, final permutation, bit reverse
  469. #---------------------------------------------------------------
  470. # NOTE: this was reordered from original table to make perm6464 logic simpler
  471. CF6464=(
  472. ( 0x0000000000000000, 0x0000002000000000, 0x0000200000000000, 0x0000202000000000,
  473. 0x0020000000000000, 0x0020002000000000, 0x0020200000000000, 0x0020202000000000,
  474. 0x2000000000000000, 0x2000002000000000, 0x2000200000000000, 0x2000202000000000,
  475. 0x2020000000000000, 0x2020002000000000, 0x2020200000000000, 0x2020202000000000, ),
  476. ( 0x0000000000000000, 0x0000000200000000, 0x0000020000000000, 0x0000020200000000,
  477. 0x0002000000000000, 0x0002000200000000, 0x0002020000000000, 0x0002020200000000,
  478. 0x0200000000000000, 0x0200000200000000, 0x0200020000000000, 0x0200020200000000,
  479. 0x0202000000000000, 0x0202000200000000, 0x0202020000000000, 0x0202020200000000, ),
  480. ( 0x0000000000000000, 0x0000000000000020, 0x0000000000002000, 0x0000000000002020,
  481. 0x0000000000200000, 0x0000000000200020, 0x0000000000202000, 0x0000000000202020,
  482. 0x0000000020000000, 0x0000000020000020, 0x0000000020002000, 0x0000000020002020,
  483. 0x0000000020200000, 0x0000000020200020, 0x0000000020202000, 0x0000000020202020, ),
  484. ( 0x0000000000000000, 0x0000000000000002, 0x0000000000000200, 0x0000000000000202,
  485. 0x0000000000020000, 0x0000000000020002, 0x0000000000020200, 0x0000000000020202,
  486. 0x0000000002000000, 0x0000000002000002, 0x0000000002000200, 0x0000000002000202,
  487. 0x0000000002020000, 0x0000000002020002, 0x0000000002020200, 0x0000000002020202, ),
  488. ( 0x0000000000000000, 0x0000008000000000, 0x0000800000000000, 0x0000808000000000,
  489. 0x0080000000000000, 0x0080008000000000, 0x0080800000000000, 0x0080808000000000,
  490. 0x8000000000000000, 0x8000008000000000, 0x8000800000000000, 0x8000808000000000,
  491. 0x8080000000000000, 0x8080008000000000, 0x8080800000000000, 0x8080808000000000, ),
  492. ( 0x0000000000000000, 0x0000000800000000, 0x0000080000000000, 0x0000080800000000,
  493. 0x0008000000000000, 0x0008000800000000, 0x0008080000000000, 0x0008080800000000,
  494. 0x0800000000000000, 0x0800000800000000, 0x0800080000000000, 0x0800080800000000,
  495. 0x0808000000000000, 0x0808000800000000, 0x0808080000000000, 0x0808080800000000, ),
  496. ( 0x0000000000000000, 0x0000000000000080, 0x0000000000008000, 0x0000000000008080,
  497. 0x0000000000800000, 0x0000000000800080, 0x0000000000808000, 0x0000000000808080,
  498. 0x0000000080000000, 0x0000000080000080, 0x0000000080008000, 0x0000000080008080,
  499. 0x0000000080800000, 0x0000000080800080, 0x0000000080808000, 0x0000000080808080, ),
  500. ( 0x0000000000000000, 0x0000000000000008, 0x0000000000000800, 0x0000000000000808,
  501. 0x0000000000080000, 0x0000000000080008, 0x0000000000080800, 0x0000000000080808,
  502. 0x0000000008000000, 0x0000000008000008, 0x0000000008000800, 0x0000000008000808,
  503. 0x0000000008080000, 0x0000000008080008, 0x0000000008080800, 0x0000000008080808, ),
  504. ( 0x0000000000000000, 0x0000001000000000, 0x0000100000000000, 0x0000101000000000,
  505. 0x0010000000000000, 0x0010001000000000, 0x0010100000000000, 0x0010101000000000,
  506. 0x1000000000000000, 0x1000001000000000, 0x1000100000000000, 0x1000101000000000,
  507. 0x1010000000000000, 0x1010001000000000, 0x1010100000000000, 0x1010101000000000, ),
  508. ( 0x0000000000000000, 0x0000000100000000, 0x0000010000000000, 0x0000010100000000,
  509. 0x0001000000000000, 0x0001000100000000, 0x0001010000000000, 0x0001010100000000,
  510. 0x0100000000000000, 0x0100000100000000, 0x0100010000000000, 0x0100010100000000,
  511. 0x0101000000000000, 0x0101000100000000, 0x0101010000000000, 0x0101010100000000, ),
  512. ( 0x0000000000000000, 0x0000000000000010, 0x0000000000001000, 0x0000000000001010,
  513. 0x0000000000100000, 0x0000000000100010, 0x0000000000101000, 0x0000000000101010,
  514. 0x0000000010000000, 0x0000000010000010, 0x0000000010001000, 0x0000000010001010,
  515. 0x0000000010100000, 0x0000000010100010, 0x0000000010101000, 0x0000000010101010, ),
  516. ( 0x0000000000000000, 0x0000000000000001, 0x0000000000000100, 0x0000000000000101,
  517. 0x0000000000010000, 0x0000000000010001, 0x0000000000010100, 0x0000000000010101,
  518. 0x0000000001000000, 0x0000000001000001, 0x0000000001000100, 0x0000000001000101,
  519. 0x0000000001010000, 0x0000000001010001, 0x0000000001010100, 0x0000000001010101, ),
  520. ( 0x0000000000000000, 0x0000004000000000, 0x0000400000000000, 0x0000404000000000,
  521. 0x0040000000000000, 0x0040004000000000, 0x0040400000000000, 0x0040404000000000,
  522. 0x4000000000000000, 0x4000004000000000, 0x4000400000000000, 0x4000404000000000,
  523. 0x4040000000000000, 0x4040004000000000, 0x4040400000000000, 0x4040404000000000, ),
  524. ( 0x0000000000000000, 0x0000000400000000, 0x0000040000000000, 0x0000040400000000,
  525. 0x0004000000000000, 0x0004000400000000, 0x0004040000000000, 0x0004040400000000,
  526. 0x0400000000000000, 0x0400000400000000, 0x0400040000000000, 0x0400040400000000,
  527. 0x0404000000000000, 0x0404000400000000, 0x0404040000000000, 0x0404040400000000, ),
  528. ( 0x0000000000000000, 0x0000000000000040, 0x0000000000004000, 0x0000000000004040,
  529. 0x0000000000400000, 0x0000000000400040, 0x0000000000404000, 0x0000000000404040,
  530. 0x0000000040000000, 0x0000000040000040, 0x0000000040004000, 0x0000000040004040,
  531. 0x0000000040400000, 0x0000000040400040, 0x0000000040404000, 0x0000000040404040, ),
  532. ( 0x0000000000000000, 0x0000000000000004, 0x0000000000000400, 0x0000000000000404,
  533. 0x0000000000040000, 0x0000000000040004, 0x0000000000040400, 0x0000000000040404,
  534. 0x0000000004000000, 0x0000000004000004, 0x0000000004000400, 0x0000000004000404,
  535. 0x0000000004040000, 0x0000000004040004, 0x0000000004040400, 0x0000000004040404, ),
  536. )
  537. #===================================================================
  538. # eof _load_tables()
  539. #===================================================================
  540. #=============================================================================
  541. # support
  542. #=============================================================================
  543. def _permute(c, p):
  544. """Returns the permutation of the given 32-bit or 64-bit code with
  545. the specified permutation table."""
  546. # NOTE: only difference between 32 & 64 bit permutations
  547. # is that len(p)==8 for 32 bit, and len(p)==16 for 64 bit.
  548. out = 0
  549. for r in p:
  550. out |= r[c&0xf]
  551. c >>= 4
  552. return out
  553. #=============================================================================
  554. # packing & unpacking
  555. #=============================================================================
  556. # FIXME: more properly named _uint8_struct...
  557. _uint64_struct = struct.Struct(">Q")
  558. def _pack64(value):
  559. return _uint64_struct.pack(value)
  560. def _unpack64(value):
  561. return _uint64_struct.unpack(value)[0]
  562. def _pack56(value):
  563. return _uint64_struct.pack(value)[1:]
  564. def _unpack56(value):
  565. return _uint64_struct.unpack(b'\x00' + value)[0]
  566. #=============================================================================
  567. # 56->64 key manipulation
  568. #=============================================================================
  569. ##def expand_7bit(value):
  570. ## "expand 7-bit integer => 7-bits + 1 odd-parity bit"
  571. ## # parity calc adapted from 32-bit even parity alg found at
  572. ## # http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
  573. ## assert 0 <= value < 0x80, "value out of range"
  574. ## return (value<<1) | (0x9669 >> ((value ^ (value >> 4)) & 0xf)) & 1
  575. _EXPAND_ITER = irange(49,-7,-7)
  576. def expand_des_key(key):
  577. """convert DES from 7 bytes to 8 bytes (by inserting empty parity bits)"""
  578. if isinstance(key, bytes):
  579. if len(key) != 7:
  580. raise ValueError("key must be 7 bytes in size")
  581. elif isinstance(key, int_types):
  582. if key < 0 or key > INT_56_MASK:
  583. raise ValueError("key must be 56-bit non-negative integer")
  584. return _unpack64(expand_des_key(_pack56(key)))
  585. else:
  586. raise exc.ExpectedTypeError(key, "bytes or int", "key")
  587. key = _unpack56(key)
  588. # NOTE: the following would insert correctly-valued parity bits in each key,
  589. # but the parity bit would just be ignored in des_encrypt_block(),
  590. # so not bothering to use it.
  591. # XXX: could make parity-restoring optionally available via flag
  592. ##return join_byte_values(expand_7bit((key >> shift) & 0x7f)
  593. ## for shift in _EXPAND_ITER)
  594. return join_byte_values(((key>>shift) & 0x7f)<<1 for shift in _EXPAND_ITER)
  595. def shrink_des_key(key):
  596. """convert DES key from 8 bytes to 7 bytes (by discarding the parity bits)"""
  597. if isinstance(key, bytes):
  598. if len(key) != 8:
  599. raise ValueError("key must be 8 bytes in size")
  600. return _pack56(shrink_des_key(_unpack64(key)))
  601. elif isinstance(key, int_types):
  602. if key < 0 or key > INT_64_MASK:
  603. raise ValueError("key must be 64-bit non-negative integer")
  604. else:
  605. raise exc.ExpectedTypeError(key, "bytes or int", "key")
  606. key >>= 1
  607. result = 0
  608. offset = 0
  609. while offset < 56:
  610. result |= (key & 0x7f)<<offset
  611. key >>= 8
  612. offset += 7
  613. assert not (result & ~INT_64_MASK)
  614. return result
  615. #=============================================================================
  616. # des encryption
  617. #=============================================================================
  618. def des_encrypt_block(key, input, salt=0, rounds=1):
  619. """encrypt single block of data using DES, operates on 8-byte strings.
  620. :arg key:
  621. DES key as 7 byte string, or 8 byte string with parity bits
  622. (parity bit values are ignored).
  623. :arg input:
  624. plaintext block to encrypt, as 8 byte string.
  625. :arg salt:
  626. Optional 24-bit integer used to mutate the base DES algorithm in a
  627. manner specific to :class:`~passlib.hash.des_crypt` and its variants.
  628. The default value ``0`` provides the normal (unsalted) DES behavior.
  629. The salt functions as follows:
  630. if the ``i``'th bit of ``salt`` is set,
  631. bits ``i`` and ``i+24`` are swapped in the DES E-box output.
  632. :arg rounds:
  633. Optional number of rounds of to apply the DES key schedule.
  634. the default (``rounds=1``) provides the normal DES behavior,
  635. but :class:`~passlib.hash.des_crypt` and its variants use
  636. alternate rounds values.
  637. :raises TypeError: if any of the provided args are of the wrong type.
  638. :raises ValueError:
  639. if any of the input blocks are the wrong size,
  640. or the salt/rounds values are out of range.
  641. :returns:
  642. resulting 8-byte ciphertext block.
  643. """
  644. # validate & unpack key
  645. if isinstance(key, bytes):
  646. if len(key) == 7:
  647. key = expand_des_key(key)
  648. elif len(key) != 8:
  649. raise ValueError("key must be 7 or 8 bytes")
  650. key = _unpack64(key)
  651. else:
  652. raise exc.ExpectedTypeError(key, "bytes", "key")
  653. # validate & unpack input
  654. if isinstance(input, bytes):
  655. if len(input) != 8:
  656. raise ValueError("input block must be 8 bytes")
  657. input = _unpack64(input)
  658. else:
  659. raise exc.ExpectedTypeError(input, "bytes", "input")
  660. # hand things off to other func
  661. result = des_encrypt_int_block(key, input, salt, rounds)
  662. # repack result
  663. return _pack64(result)
  664. def des_encrypt_int_block(key, input, salt=0, rounds=1):
  665. """encrypt single block of data using DES, operates on 64-bit integers.
  666. this function is essentially the same as :func:`des_encrypt_block`,
  667. except that it operates on integers, and will NOT automatically
  668. expand 56-bit keys if provided (since there's no way to detect them).
  669. :arg key:
  670. DES key as 64-bit integer (the parity bits are ignored).
  671. :arg input:
  672. input block as 64-bit integer
  673. :arg salt:
  674. optional 24-bit integer used to mutate the base DES algorithm.
  675. defaults to ``0`` (no mutation applied).
  676. :arg rounds:
  677. optional number of rounds of to apply the DES key schedule.
  678. defaults to ``1``.
  679. :raises TypeError: if any of the provided args are of the wrong type.
  680. :raises ValueError:
  681. if any of the input blocks are the wrong size,
  682. or the salt/rounds values are out of range.
  683. :returns:
  684. resulting ciphertext as 64-bit integer.
  685. """
  686. #---------------------------------------------------------------
  687. # input validation
  688. #---------------------------------------------------------------
  689. # validate salt, rounds
  690. if rounds < 1:
  691. raise ValueError("rounds must be positive integer")
  692. if salt < 0 or salt > INT_24_MASK:
  693. raise ValueError("salt must be 24-bit non-negative integer")
  694. # validate & unpack key
  695. if not isinstance(key, int_types):
  696. raise exc.ExpectedTypeError(key, "int", "key")
  697. elif key < 0 or key > INT_64_MASK:
  698. raise ValueError("key must be 64-bit non-negative integer")
  699. # validate & unpack input
  700. if not isinstance(input, int_types):
  701. raise exc.ExpectedTypeError(input, "int", "input")
  702. elif input < 0 or input > INT_64_MASK:
  703. raise ValueError("input must be 64-bit non-negative integer")
  704. #---------------------------------------------------------------
  705. # DES setup
  706. #---------------------------------------------------------------
  707. # load tables if not already done
  708. global SPE, PCXROT, IE3264, CF6464
  709. if PCXROT is None:
  710. _load_tables()
  711. # load SPE into local vars to speed things up and remove an array access call
  712. SPE0, SPE1, SPE2, SPE3, SPE4, SPE5, SPE6, SPE7 = SPE
  713. # NOTE: parity bits are ignored completely
  714. # (UTs do fuzz testing to ensure this)
  715. # generate key schedule
  716. # NOTE: generation was modified to output two elements at a time,
  717. # so that per-round loop could do two passes at once.
  718. def _iter_key_schedule(ks_odd):
  719. """given 64-bit key, iterates over the 8 (even,odd) key schedule pairs"""
  720. for p_even, p_odd in PCXROT:
  721. ks_even = _permute(ks_odd, p_even)
  722. ks_odd = _permute(ks_even, p_odd)
  723. yield ks_even & _KS_MASK, ks_odd & _KS_MASK
  724. ks_list = list(_iter_key_schedule(key))
  725. # expand 24 bit salt -> 32 bit per des_crypt & bsdi_crypt
  726. salt = (
  727. ((salt & 0x00003f) << 26) |
  728. ((salt & 0x000fc0) << 12) |
  729. ((salt & 0x03f000) >> 2) |
  730. ((salt & 0xfc0000) >> 16)
  731. )
  732. # init L & R
  733. if input == 0:
  734. L = R = 0
  735. else:
  736. L = ((input >> 31) & 0xaaaaaaaa) | (input & 0x55555555)
  737. L = _permute(L, IE3264)
  738. R = ((input >> 32) & 0xaaaaaaaa) | ((input >> 1) & 0x55555555)
  739. R = _permute(R, IE3264)
  740. #---------------------------------------------------------------
  741. # main DES loop - run for specified number of rounds
  742. #---------------------------------------------------------------
  743. while rounds:
  744. rounds -= 1
  745. # run over each part of the schedule, 2 parts at a time
  746. for ks_even, ks_odd in ks_list:
  747. k = ((R>>32) ^ R) & salt # use the salt to flip specific bits
  748. B = (k<<32) ^ k ^ R ^ ks_even
  749. L ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
  750. SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^
  751. SPE4[(B>>26)&0x3f] ^ SPE5[(B>>18)&0x3f] ^
  752. SPE6[(B>>10)&0x3f] ^ SPE7[(B>>2)&0x3f])
  753. k = ((L>>32) ^ L) & salt # use the salt to flip specific bits
  754. B = (k<<32) ^ k ^ L ^ ks_odd
  755. R ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
  756. SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^
  757. SPE4[(B>>26)&0x3f] ^ SPE5[(B>>18)&0x3f] ^
  758. SPE6[(B>>10)&0x3f] ^ SPE7[(B>>2)&0x3f])
  759. # swap L and R
  760. L, R = R, L
  761. #---------------------------------------------------------------
  762. # return final result
  763. #---------------------------------------------------------------
  764. C = (
  765. ((L>>3) & 0x0f0f0f0f00000000)
  766. |
  767. ((L<<33) & 0xf0f0f0f000000000)
  768. |
  769. ((R>>35) & 0x000000000f0f0f0f)
  770. |
  771. ((R<<1) & 0x00000000f0f0f0f0)
  772. )
  773. return _permute(C, CF6464)
  774. #=============================================================================
  775. # eof
  776. #=============================================================================