summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/notes.txt39
-rw-r--r--passlib/lanman.py50
-rw-r--r--passlib/utils/__init__.py32
-rw-r--r--passlib/utils/_slow_des_crypt.py707
-rw-r--r--passlib/utils/des.py724
-rw-r--r--passlib/utils/md4.py6
6 files changed, 832 insertions, 726 deletions
diff --git a/docs/notes.txt b/docs/notes.txt
index 0a1fe7a..2c14955 100644
--- a/docs/notes.txt
+++ b/docs/notes.txt
@@ -190,6 +190,45 @@ nt-hash
http://search.cpan.org/~zefram/Authen-Passphrase-0.007/lib/Authen/Passphrase.pm
======================================================================
+OS notes
+
+ summary from http://www.dribin.org/dave/blog/archives/2006/04/28/os_x_passwords_2/
+
+osx < 10.2 used /etc/passwd w/ DES-CRYPT
+
+osx 10.3 hash file (passwd "macintosh")
+
+D47F3AF827A48F7DFA4F2C1F12D68CD6 <-- nthash
+08460EB13C5CA0C4CA9516712F7FED95 <-- lmhash
+01424f955c11f92efef0b79d7fa3fb6be56a9f99 <-- sha1
+
+osx 10.4 hash file (passwd "macintosh")
+00000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000E6A48F765D0FFFFF6247FA80D748E615F91DD0C7431E4D9000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000000000000000000000\
+0000000000000000000000000000000000000000
+
+offset 0-64 - nt hash + lm hash OR all zeros
+offset 64 - 40 chars - raw sha1 password OR all zeroes (if from upgraded from 10.3)
+offset 169-216 ( 48 chars) - salted sha1 hash - unhex first 8 chars + password | sha1 -> hexdigest
+
+
+
+======================
+
policy file format
diff --git a/passlib/lanman.py b/passlib/lanman.py
index ead8f07..3640dbd 100644
--- a/passlib/lanman.py
+++ b/passlib/lanman.py
@@ -1,37 +1,31 @@
"""
-
-lanman
-
-macintosh
-D47F 3AF8 27A4 8F7D FA4F 2C1F 12D6 8CD6
-
-08460EB13C5CA0C4CA9516712F7FED95
-
-ntlm
-
-
-lanman
-
-C234 13A8 A1E7 665f
-AAD3 B435 B514 04EE
-welcome
"""
from binascii import hexlify
-from bps.numeric import int_to_base
-from passlib.utils._slow_des_crypt import des_encrypt_block
+from passlib.utils.des import des_encrypt_block
LM_MAGIC = "KGS!@#$%"
def lmhash(secret):
#XXX: encoding should be oem ascii
ns = secret.upper()[:14] + "\x00" * (14-len(secret))
- return hexlify(des_encrypt_block(expand_des_key(ns[:7]), LM_MAGIC) + des_encrypt_block(expand_des_key(ns[7:]), LM_MAGIC))
-
-for secret, hash in [
- #hashes from http://msdn.microsoft.com/en-us/library/cc245828(v=prot.10).aspx
- ("OLDPASSWORD", "c9b81d939d6fd80cd408e6b105741864"),
- ("NEWPASSWORD", '09eeab5aa415d6e4d408e6b105741864'),
- ("welcome", "c23413a8a1e7665faad3b435b51404ee"),
- ]:
-
- print secret, lmhash(secret), hash == lmhash(secret)
+ return hexlify(des_encrypt_block(ns[:7], LM_MAGIC) + des_encrypt_block(ns[7:], LM_MAGIC))
+
+###hashes from http://msdn.microsoft.com/en-us/library/cc245828(v=prot.10).aspx
+### among other places
+##for secret, hash in [
+## ("OLDPASSWORD", "c9b81d939d6fd80cd408e6b105741864"),
+## ("NEWPASSWORD", '09eeab5aa415d6e4d408e6b105741864'),
+## ("welcome", "c23413a8a1e7665faad3b435b51404ee"),
+## ]:
+##
+## print secret, lmhash(secret), hash == lmhash(secret)
+
+from passlib.utils.md4 import md4
+def nthash(secret):
+ return md4(secret.encode("utf-16le")).hexdigest()
+
+##for secret, hash in [
+## ("OLDPASSWORD", "6677b2c394311355b54f25eec5bfacf5"),
+## ("NEWPASSWORD", "256781a62031289d3c2c98c14f1efc8c"),
+## ]:
+## print secret, lmhash(secret), hash == nthash(secret)
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index 9b8a472..3b93df1 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -73,8 +73,21 @@ Undef = object() #singleton used as default kwd value in some functions
#numeric helpers
#=================================================================================
-##def int_to_bytes(value, count=None):
-## """encode a integer into a string of bytes"""
+##def int_to_bytes(value, count=None, order="big"):
+## """encode a integer into a string of bytes
+##
+## :arg value: the integer
+## :arg count: optional number of bytes to expose, uses minimum needed if count not specified
+## :param order: the byte ordering; "big" (the default), "little", or "native"
+##
+## :raises ValueError:
+## * if count specified and integer too large to fit.
+## * if integer is negative
+##
+## :returns:
+## bytes encoding integer
+## """
+##
##
##def bytes_to_int(value, order="big"):
## """decode a byte string into an integer representation of it's binary value.
@@ -95,6 +108,21 @@ Undef = object() #singleton used as default kwd value in some functions
## out = (out<<8) | ord(v)
## return out
+def bytes_to_int(value):
+ "decode bytes as single big-endian integer"
+ out = 0
+ for v in value:
+ out = (out<<8) | ord(v)
+ return out
+
+def int_to_bytes(value, count):
+ "encode integer into single big-endian byte string"
+ assert value < (1<<(8*count)), "value too large for %d bytes: %d" % (count, value)
+ return ''.join(
+ chr((value>>s) & 0xff)
+ for s in xrange(8*count-8,-8,-8)
+ )
+
#TODO: rename 'bytes' kwd for py30 compat purposes
def list_to_bytes(value, bytes=None, order="big"):
"""Returns a multi-character string corresponding to a list of byte values.
diff --git a/passlib/utils/_slow_des_crypt.py b/passlib/utils/_slow_des_crypt.py
index 9ebf518..08e2960 100644
--- a/passlib/utils/_slow_des_crypt.py
+++ b/passlib/utils/_slow_des_crypt.py
@@ -1,40 +1,5 @@
"""passlib._slow_unix_crypt -- fallback pure-python unix crypt() implementation
-History
-=======
-This is a pure-python implementation of the unix "crypt" algorithm,
-based on the Java implementation found at http://www.dynamic.net.au/christos/crypt/UnixCrypt2.txt
-
-The copyright & license for that source is as follows::
-
- UnixCrypt.java 0.9 96/11/25
- Copyright (c) 1996 Aki Yoshida. All rights reserved.
- Permission to use, copy, modify and distribute this software
- for non-commercial or commercial purposes and without fee is
- hereby granted provided that this copyright notice appears in
- all copies.
-
- ---
-
- Unix crypt(3C) utility
- @version 0.9, 11/25/96
- @author Aki Yoshida
-
- ---
-
- modified April 2001
- by Iris Van den Broeke, Daniel Deville
-
- ---
- Unix Crypt.
- Implements the one way cryptography used by Unix systems for
- simple password protection.
- @version $Id: UnixCrypt2.txt,v 1.1.1.1 2005/09/13 22:20:13 christos Exp $
- @author Greg Wilkins (gregw)
-
-This python implementation was adapted from this source by Eli Collins <elic@astllc.org>,
-and released under the BSD license as part of PassLib.
-
This module is mainly meant as a fallback when stdlib does not supply a ``crypt`` implementation,
such as on windows systems. As such, it attempts to have a public interface
which is compatible with stdlib, so it can be used as a drop-in replacement.
@@ -42,17 +7,25 @@ which is compatible with stdlib, so it can be used as a drop-in replacement.
#=========================================================
#imports
#=========================================================
+#pkg
+from passlib.utils import H64_CHARS
+from passlib.utils.des import mdes_encrypt_int_block
+#local
+__all__ = [
+ "crypt",
+ "raw_ext_crypt",
+ "ext_crypt",
+]
#=========================================================
#crypt-style base64 encoding / decoding
#=========================================================
#base64 char sequence
-CHARS = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
-b64_encode_6bit = CHARS.__getitem__ # int -> char
+b64_encode_6bit = H64_CHARS.__getitem__ # int -> char
#inverse map (char->value)
-CHARIDX = dict( (c,i) for i,c in enumerate(CHARS))
+CHARIDX = dict( (c,i) for i,c in enumerate(H64_CHARS))
b64_decode_6bit = CHARIDX.__getitem__ # char -> int
##def b64_to_int(value):
@@ -101,658 +74,6 @@ def b64_encode_int64(value):
return "".join(out)
#=========================================================
-#precalculated iteration ranges & constants
-#=========================================================
-R8 = range(8)
-RR8 = range(7, -1, -1)
-RR4 = range(3, -1, -1)
-RR12_1 = range(11, 1, -1)
-RR9_1 = range(9,-1,-1)
-
-RR6_S2 = range(6, -1, -2)
-RR14_S2 = range(14, -1, -2)
-R16_S2 = range(0, 16, 2)
-
-INT_24_MAX = 0xffffffL
-INT_64_MAX = 0xffffffffL
-INT_64_MAX = 0xffffffffffffffffL
-
-#=========================================================
-# static tables for des
-#=========================================================
-PCXROT = IE3264 = SPE = CF6464 = None #placeholders filled in by load_tables
-
-def load_tables():
- "delay loading tables until they are actually needed"
- global PCXROT, IE3264, SPE, CF6464
-
- #---------------------------------------------------
- # Initial key schedule permutation
- # PC1ROT - bit reverse, then PC1, then Rotate, then PC2
- #---------------------------------------------------
- #NOTE: this was reordered from original table to make perm3264 logic simpler
- PC1ROT=(
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000002000, 0x0000000000002000,
- 0x0000000000000020, 0x0000000000000020, 0x0000000000002020, 0x0000000000002020,
- 0x0000000000000400, 0x0000000000000400, 0x0000000000002400, 0x0000000000002400,
- 0x0000000000000420, 0x0000000000000420, 0x0000000000002420, 0x0000000000002420, ),
- ( 0x0000000000000000, 0x2000000000000000, 0x0000000400000000, 0x2000000400000000,
- 0x0000800000000000, 0x2000800000000000, 0x0000800400000000, 0x2000800400000000,
- 0x0008000000000000, 0x2008000000000000, 0x0008000400000000, 0x2008000400000000,
- 0x0008800000000000, 0x2008800000000000, 0x0008800400000000, 0x2008800400000000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000040, 0x0000000000000040,
- 0x0000000020000000, 0x0000000020000000, 0x0000000020000040, 0x0000000020000040,
- 0x0000000000200000, 0x0000000000200000, 0x0000000000200040, 0x0000000000200040,
- 0x0000000020200000, 0x0000000020200000, 0x0000000020200040, 0x0000000020200040, ),
- ( 0x0000000000000000, 0x0002000000000000, 0x0800000000000000, 0x0802000000000000,
- 0x0100000000000000, 0x0102000000000000, 0x0900000000000000, 0x0902000000000000,
- 0x4000000000000000, 0x4002000000000000, 0x4800000000000000, 0x4802000000000000,
- 0x4100000000000000, 0x4102000000000000, 0x4900000000000000, 0x4902000000000000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000040000, 0x0000000000040000,
- 0x0000020000000000, 0x0000020000000000, 0x0000020000040000, 0x0000020000040000,
- 0x0000000000000004, 0x0000000000000004, 0x0000000000040004, 0x0000000000040004,
- 0x0000020000000004, 0x0000020000000004, 0x0000020000040004, 0x0000020000040004, ),
- ( 0x0000000000000000, 0x0000400000000000, 0x0200000000000000, 0x0200400000000000,
- 0x0080000000000000, 0x0080400000000000, 0x0280000000000000, 0x0280400000000000,
- 0x0000008000000000, 0x0000408000000000, 0x0200008000000000, 0x0200408000000000,
- 0x0080008000000000, 0x0080408000000000, 0x0280008000000000, 0x0280408000000000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000010000000, 0x0000000010000000,
- 0x0000000000001000, 0x0000000000001000, 0x0000000010001000, 0x0000000010001000,
- 0x0000000040000000, 0x0000000040000000, 0x0000000050000000, 0x0000000050000000,
- 0x0000000040001000, 0x0000000040001000, 0x0000000050001000, 0x0000000050001000, ),
- ( 0x0000000000000000, 0x0000001000000000, 0x0000080000000000, 0x0000081000000000,
- 0x1000000000000000, 0x1000001000000000, 0x1000080000000000, 0x1000081000000000,
- 0x0004000000000000, 0x0004001000000000, 0x0004080000000000, 0x0004081000000000,
- 0x1004000000000000, 0x1004001000000000, 0x1004080000000000, 0x1004081000000000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000080, 0x0000000000000080,
- 0x0000000000080000, 0x0000000000080000, 0x0000000000080080, 0x0000000000080080,
- 0x0000000000800000, 0x0000000000800000, 0x0000000000800080, 0x0000000000800080,
- 0x0000000000880000, 0x0000000000880000, 0x0000000000880080, 0x0000000000880080, ),
- ( 0x0000000000000000, 0x0000000008000000, 0x0000002000000000, 0x0000002008000000,
- 0x0000100000000000, 0x0000100008000000, 0x0000102000000000, 0x0000102008000000,
- 0x0000200000000000, 0x0000200008000000, 0x0000202000000000, 0x0000202008000000,
- 0x0000300000000000, 0x0000300008000000, 0x0000302000000000, 0x0000302008000000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000400000, 0x0000000000400000,
- 0x0000000004000000, 0x0000000004000000, 0x0000000004400000, 0x0000000004400000,
- 0x0000000000000800, 0x0000000000000800, 0x0000000000400800, 0x0000000000400800,
- 0x0000000004000800, 0x0000000004000800, 0x0000000004400800, 0x0000000004400800, ),
- ( 0x0000000000000000, 0x0000000000008000, 0x0040000000000000, 0x0040000000008000,
- 0x0000004000000000, 0x0000004000008000, 0x0040004000000000, 0x0040004000008000,
- 0x8000000000000000, 0x8000000000008000, 0x8040000000000000, 0x8040000000008000,
- 0x8000004000000000, 0x8000004000008000, 0x8040004000000000, 0x8040004000008000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000004000, 0x0000000000004000,
- 0x0000000000000008, 0x0000000000000008, 0x0000000000004008, 0x0000000000004008,
- 0x0000000000000010, 0x0000000000000010, 0x0000000000004010, 0x0000000000004010,
- 0x0000000000000018, 0x0000000000000018, 0x0000000000004018, 0x0000000000004018, ),
- ( 0x0000000000000000, 0x0000000200000000, 0x0001000000000000, 0x0001000200000000,
- 0x0400000000000000, 0x0400000200000000, 0x0401000000000000, 0x0401000200000000,
- 0x0020000000000000, 0x0020000200000000, 0x0021000000000000, 0x0021000200000000,
- 0x0420000000000000, 0x0420000200000000, 0x0421000000000000, 0x0421000200000000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000010000000000, 0x0000010000000000,
- 0x0000000100000000, 0x0000000100000000, 0x0000010100000000, 0x0000010100000000,
- 0x0000000000100000, 0x0000000000100000, 0x0000010000100000, 0x0000010000100000,
- 0x0000000100100000, 0x0000000100100000, 0x0000010100100000, 0x0000010100100000, ),
- ( 0x0000000000000000, 0x0000000080000000, 0x0000040000000000, 0x0000040080000000,
- 0x0010000000000000, 0x0010000080000000, 0x0010040000000000, 0x0010040080000000,
- 0x0000000800000000, 0x0000000880000000, 0x0000040800000000, 0x0000040880000000,
- 0x0010000800000000, 0x0010000880000000, 0x0010040800000000, 0x0010040880000000, ),
- )
- #---------------------------------------------------
- # Subsequent key schedule rotation permutations
- # PC2ROT - PC2 inverse, then Rotate, then PC2
- #---------------------------------------------------
- #NOTE: this was reordered from original table to make perm3264 logic simpler
- PC2ROTA=(
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000000200000, 0x0000000000200000, 0x0000000000200000, 0x0000000000200000,
- 0x0000000004000000, 0x0000000004000000, 0x0000000004000000, 0x0000000004000000,
- 0x0000000004200000, 0x0000000004200000, 0x0000000004200000, 0x0000000004200000, ),
- ( 0x0000000000000000, 0x0000000000000800, 0x0000010000000000, 0x0000010000000800,
- 0x0000000000002000, 0x0000000000002800, 0x0000010000002000, 0x0000010000002800,
- 0x0000000010000000, 0x0000000010000800, 0x0000010010000000, 0x0000010010000800,
- 0x0000000010002000, 0x0000000010002800, 0x0000010010002000, 0x0000010010002800, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000100000000, 0x0000000100000000, 0x0000000100000000, 0x0000000100000000,
- 0x0000000000800000, 0x0000000000800000, 0x0000000000800000, 0x0000000000800000,
- 0x0000000100800000, 0x0000000100800000, 0x0000000100800000, 0x0000000100800000, ),
- ( 0x0000000000000000, 0x0000020000000000, 0x0000000080000000, 0x0000020080000000,
- 0x0000000000400000, 0x0000020000400000, 0x0000000080400000, 0x0000020080400000,
- 0x0000000008000000, 0x0000020008000000, 0x0000000088000000, 0x0000020088000000,
- 0x0000000008400000, 0x0000020008400000, 0x0000000088400000, 0x0000020088400000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000000000040, 0x0000000000000040, 0x0000000000000040, 0x0000000000000040,
- 0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
- 0x0000000000001040, 0x0000000000001040, 0x0000000000001040, 0x0000000000001040, ),
- ( 0x0000000000000000, 0x0000000000000010, 0x0000000000000400, 0x0000000000000410,
- 0x0000000000000080, 0x0000000000000090, 0x0000000000000480, 0x0000000000000490,
- 0x0000000040000000, 0x0000000040000010, 0x0000000040000400, 0x0000000040000410,
- 0x0000000040000080, 0x0000000040000090, 0x0000000040000480, 0x0000000040000490, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
- 0x0000000000100000, 0x0000000000100000, 0x0000000000100000, 0x0000000000100000,
- 0x0000000000180000, 0x0000000000180000, 0x0000000000180000, 0x0000000000180000, ),
- ( 0x0000000000000000, 0x0000000000040000, 0x0000000000000020, 0x0000000000040020,
- 0x0000000000000004, 0x0000000000040004, 0x0000000000000024, 0x0000000000040024,
- 0x0000000200000000, 0x0000000200040000, 0x0000000200000020, 0x0000000200040020,
- 0x0000000200000004, 0x0000000200040004, 0x0000000200000024, 0x0000000200040024, ),
- ( 0x0000000000000000, 0x0000000000000008, 0x0000000000008000, 0x0000000000008008,
- 0x0010000000000000, 0x0010000000000008, 0x0010000000008000, 0x0010000000008008,
- 0x0020000000000000, 0x0020000000000008, 0x0020000000008000, 0x0020000000008008,
- 0x0030000000000000, 0x0030000000000008, 0x0030000000008000, 0x0030000000008008, ),
- ( 0x0000000000000000, 0x0000400000000000, 0x0000080000000000, 0x0000480000000000,
- 0x0000100000000000, 0x0000500000000000, 0x0000180000000000, 0x0000580000000000,
- 0x4000000000000000, 0x4000400000000000, 0x4000080000000000, 0x4000480000000000,
- 0x4000100000000000, 0x4000500000000000, 0x4000180000000000, 0x4000580000000000, ),
- ( 0x0000000000000000, 0x0000000000004000, 0x0000000020000000, 0x0000000020004000,
- 0x0001000000000000, 0x0001000000004000, 0x0001000020000000, 0x0001000020004000,
- 0x0200000000000000, 0x0200000000004000, 0x0200000020000000, 0x0200000020004000,
- 0x0201000000000000, 0x0201000000004000, 0x0201000020000000, 0x0201000020004000, ),
- ( 0x0000000000000000, 0x1000000000000000, 0x0004000000000000, 0x1004000000000000,
- 0x0002000000000000, 0x1002000000000000, 0x0006000000000000, 0x1006000000000000,
- 0x0000000800000000, 0x1000000800000000, 0x0004000800000000, 0x1004000800000000,
- 0x0002000800000000, 0x1002000800000000, 0x0006000800000000, 0x1006000800000000, ),
- ( 0x0000000000000000, 0x0040000000000000, 0x2000000000000000, 0x2040000000000000,
- 0x0000008000000000, 0x0040008000000000, 0x2000008000000000, 0x2040008000000000,
- 0x0000001000000000, 0x0040001000000000, 0x2000001000000000, 0x2040001000000000,
- 0x0000009000000000, 0x0040009000000000, 0x2000009000000000, 0x2040009000000000, ),
- ( 0x0000000000000000, 0x0400000000000000, 0x8000000000000000, 0x8400000000000000,
- 0x0000002000000000, 0x0400002000000000, 0x8000002000000000, 0x8400002000000000,
- 0x0100000000000000, 0x0500000000000000, 0x8100000000000000, 0x8500000000000000,
- 0x0100002000000000, 0x0500002000000000, 0x8100002000000000, 0x8500002000000000, ),
- ( 0x0000000000000000, 0x0000800000000000, 0x0800000000000000, 0x0800800000000000,
- 0x0000004000000000, 0x0000804000000000, 0x0800004000000000, 0x0800804000000000,
- 0x0000000400000000, 0x0000800400000000, 0x0800000400000000, 0x0800800400000000,
- 0x0000004400000000, 0x0000804400000000, 0x0800004400000000, 0x0800804400000000, ),
- ( 0x0000000000000000, 0x0080000000000000, 0x0000040000000000, 0x0080040000000000,
- 0x0008000000000000, 0x0088000000000000, 0x0008040000000000, 0x0088040000000000,
- 0x0000200000000000, 0x0080200000000000, 0x0000240000000000, 0x0080240000000000,
- 0x0008200000000000, 0x0088200000000000, 0x0008240000000000, 0x0088240000000000, ),
- )
-
- #NOTE: this was reordered from original table to make perm3264 logic simpler
- PC2ROTB=(
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000000000400, 0x0000000000000400, 0x0000000000000400, 0x0000000000000400,
- 0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
- 0x0000000000080400, 0x0000000000080400, 0x0000000000080400, 0x0000000000080400, ),
- ( 0x0000000000000000, 0x0000000000800000, 0x0000000000004000, 0x0000000000804000,
- 0x0000000080000000, 0x0000000080800000, 0x0000000080004000, 0x0000000080804000,
- 0x0000000000040000, 0x0000000000840000, 0x0000000000044000, 0x0000000000844000,
- 0x0000000080040000, 0x0000000080840000, 0x0000000080044000, 0x0000000080844000, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000000000008, 0x0000000000000008, 0x0000000000000008, 0x0000000000000008,
- 0x0000000040000000, 0x0000000040000000, 0x0000000040000000, 0x0000000040000000,
- 0x0000000040000008, 0x0000000040000008, 0x0000000040000008, 0x0000000040000008, ),
- ( 0x0000000000000000, 0x0000000020000000, 0x0000000200000000, 0x0000000220000000,
- 0x0000000000000080, 0x0000000020000080, 0x0000000200000080, 0x0000000220000080,
- 0x0000000000100000, 0x0000000020100000, 0x0000000200100000, 0x0000000220100000,
- 0x0000000000100080, 0x0000000020100080, 0x0000000200100080, 0x0000000220100080, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000000002000, 0x0000000000002000, 0x0000000000002000, 0x0000000000002000,
- 0x0000020000000000, 0x0000020000000000, 0x0000020000000000, 0x0000020000000000,
- 0x0000020000002000, 0x0000020000002000, 0x0000020000002000, 0x0000020000002000, ),
- ( 0x0000000000000000, 0x0000000000000800, 0x0000000100000000, 0x0000000100000800,
- 0x0000000010000000, 0x0000000010000800, 0x0000000110000000, 0x0000000110000800,
- 0x0000000000000004, 0x0000000000000804, 0x0000000100000004, 0x0000000100000804,
- 0x0000000010000004, 0x0000000010000804, 0x0000000110000004, 0x0000000110000804, ),
- ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
- 0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
- 0x0000000000000010, 0x0000000000000010, 0x0000000000000010, 0x0000000000000010,
- 0x0000000000001010, 0x0000000000001010, 0x0000000000001010, 0x0000000000001010, ),
- ( 0x0000000000000000, 0x0000000000000040, 0x0000010000000000, 0x0000010000000040,
- 0x0000000000200000, 0x0000000000200040, 0x0000010000200000, 0x0000010000200040,
- 0x0000000000008000, 0x0000000000008040, 0x0000010000008000, 0x0000010000008040,
- 0x0000000000208000, 0x0000000000208040, 0x0000010000208000, 0x0000010000208040, ),
- ( 0x0000000000000000, 0x0000000004000000, 0x0000000008000000, 0x000000000c000000,
- 0x0400000000000000, 0x0400000004000000, 0x0400000008000000, 0x040000000c000000,
- 0x8000000000000000, 0x8000000004000000, 0x8000000008000000, 0x800000000c000000,
- 0x8400000000000000, 0x8400000004000000, 0x8400000008000000, 0x840000000c000000, ),
- ( 0x0000000000000000, 0x0002000000000000, 0x0200000000000000, 0x0202000000000000,
- 0x1000000000000000, 0x1002000000000000, 0x1200000000000000, 0x1202000000000000,
- 0x0008000000000000, 0x000a000000000000, 0x0208000000000000, 0x020a000000000000,
- 0x1008000000000000, 0x100a000000000000, 0x1208000000000000, 0x120a000000000000, ),
- ( 0x0000000000000000, 0x0000000000400000, 0x0000000000000020, 0x0000000000400020,
- 0x0040000000000000, 0x0040000000400000, 0x0040000000000020, 0x0040000000400020,
- 0x0800000000000000, 0x0800000000400000, 0x0800000000000020, 0x0800000000400020,
- 0x0840000000000000, 0x0840000000400000, 0x0840000000000020, 0x0840000000400020, ),
- ( 0x0000000000000000, 0x0080000000000000, 0x0000008000000000, 0x0080008000000000,
- 0x2000000000000000, 0x2080000000000000, 0x2000008000000000, 0x2080008000000000,
- 0x0020000000000000, 0x00a0000000000000, 0x0020008000000000, 0x00a0008000000000,
- 0x2020000000000000, 0x20a0000000000000, 0x2020008000000000, 0x20a0008000000000, ),
- ( 0x0000000000000000, 0x0000002000000000, 0x0000040000000000, 0x0000042000000000,
- 0x4000000000000000, 0x4000002000000000, 0x4000040000000000, 0x4000042000000000,
- 0x0000400000000000, 0x0000402000000000, 0x0000440000000000, 0x0000442000000000,
- 0x4000400000000000, 0x4000402000000000, 0x4000440000000000, 0x4000442000000000, ),
- ( 0x0000000000000000, 0x0000004000000000, 0x0000200000000000, 0x0000204000000000,
- 0x0000080000000000, 0x0000084000000000, 0x0000280000000000, 0x0000284000000000,
- 0x0000800000000000, 0x0000804000000000, 0x0000a00000000000, 0x0000a04000000000,
- 0x0000880000000000, 0x0000884000000000, 0x0000a80000000000, 0x0000a84000000000, ),
- ( 0x0000000000000000, 0x0000000800000000, 0x0000000400000000, 0x0000000c00000000,
- 0x0000100000000000, 0x0000100800000000, 0x0000100400000000, 0x0000100c00000000,
- 0x0010000000000000, 0x0010000800000000, 0x0010000400000000, 0x0010000c00000000,
- 0x0010100000000000, 0x0010100800000000, 0x0010100400000000, 0x0010100c00000000, ),
- ( 0x0000000000000000, 0x0100000000000000, 0x0001000000000000, 0x0101000000000000,
- 0x0000001000000000, 0x0100001000000000, 0x0001001000000000, 0x0101001000000000,
- 0x0004000000000000, 0x0104000000000000, 0x0005000000000000, 0x0105000000000000,
- 0x0004001000000000, 0x0104001000000000, 0x0005001000000000, 0x0105001000000000, ),
- )
- #---------------------------------------------------
- #PCXROT - PC1ROT, PC2ROTA, PC2ROTB listed in order
- # of the PC1 rotation schedule, as used by des_setkey
- #---------------------------------------------------
- ##ROTATES = (1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1)
- ##PCXROT = (
- ## PC1ROT, PC2ROTA, PC2ROTB, PC2ROTB,
- ## PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTB,
- ## PC2ROTA, PC2ROTB, PC2ROTB, PC2ROTB,
- ## PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTA,
- ## )
-
- #NOTE: modified PCXROT to contain entrys broken into pairs,
- # to helper generate them in format best used by encoder.
- PCXROT = (
- (PC1ROT, PC2ROTA), (PC2ROTB, PC2ROTB),
- (PC2ROTB, PC2ROTB), (PC2ROTB, PC2ROTB),
- (PC2ROTA, PC2ROTB), (PC2ROTB, PC2ROTB),
- (PC2ROTB, PC2ROTB), (PC2ROTB, PC2ROTA),
- )
-
- #---------------------------------------------------
- # Bit reverse, intial permupation, expantion
- # Initial permutation/expansion table
- #---------------------------------------------------
- #NOTE: this was reordered from original table to make perm3264 logic simpler
- IE3264=(
- ( 0x0000000000000000, 0x0000000000800800, 0x0000000000008008, 0x0000000000808808,
- 0x0000008008000000, 0x0000008008800800, 0x0000008008008008, 0x0000008008808808,
- 0x0000000080080000, 0x0000000080880800, 0x0000000080088008, 0x0000000080888808,
- 0x0000008088080000, 0x0000008088880800, 0x0000008088088008, 0x0000008088888808, ),
- ( 0x0000000000000000, 0x0080080000000000, 0x0000800800000000, 0x0080880800000000,
- 0x0800000000000080, 0x0880080000000080, 0x0800800800000080, 0x0880880800000080,
- 0x8008000000000000, 0x8088080000000000, 0x8008800800000000, 0x8088880800000000,
- 0x8808000000000080, 0x8888080000000080, 0x8808800800000080, 0x8888880800000080, ),
- ( 0x0000000000000000, 0x0000000000001000, 0x0000000000000010, 0x0000000000001010,
- 0x0000000010000000, 0x0000000010001000, 0x0000000010000010, 0x0000000010001010,
- 0x0000000000100000, 0x0000000000101000, 0x0000000000100010, 0x0000000000101010,
- 0x0000000010100000, 0x0000000010101000, 0x0000000010100010, 0x0000000010101010, ),
- ( 0x0000000000000000, 0x0000100000000000, 0x0000001000000000, 0x0000101000000000,
- 0x1000000000000000, 0x1000100000000000, 0x1000001000000000, 0x1000101000000000,
- 0x0010000000000000, 0x0010100000000000, 0x0010001000000000, 0x0010101000000000,
- 0x1010000000000000, 0x1010100000000000, 0x1010001000000000, 0x1010101000000000, ),
- ( 0x0000000000000000, 0x0000000000002000, 0x0000000000000020, 0x0000000000002020,
- 0x0000000020000000, 0x0000000020002000, 0x0000000020000020, 0x0000000020002020,
- 0x0000000000200000, 0x0000000000202000, 0x0000000000200020, 0x0000000000202020,
- 0x0000000020200000, 0x0000000020202000, 0x0000000020200020, 0x0000000020202020, ),
- ( 0x0000000000000000, 0x0000200000000000, 0x0000002000000000, 0x0000202000000000,
- 0x2000000000000000, 0x2000200000000000, 0x2000002000000000, 0x2000202000000000,
- 0x0020000000000000, 0x0020200000000000, 0x0020002000000000, 0x0020202000000000,
- 0x2020000000000000, 0x2020200000000000, 0x2020002000000000, 0x2020202000000000, ),
- ( 0x0000000000000000, 0x0000000000004004, 0x0400000000000040, 0x0400000000004044,
- 0x0000000040040000, 0x0000000040044004, 0x0400000040040040, 0x0400000040044044,
- 0x0000000000400400, 0x0000000000404404, 0x0400000000400440, 0x0400000000404444,
- 0x0000000040440400, 0x0000000040444404, 0x0400000040440440, 0x0400000040444444, ),
- ( 0x0000000000000000, 0x0000400400000000, 0x0000004004000000, 0x0000404404000000,
- 0x4004000000000000, 0x4004400400000000, 0x4004004004000000, 0x4004404404000000,
- 0x0040040000000000, 0x0040440400000000, 0x0040044004000000, 0x0040444404000000,
- 0x4044040000000000, 0x4044440400000000, 0x4044044004000000, 0x4044444404000000, ),
- )
-
- #---------------------------------------------------
- # Table that combines the S, P, and E operations.
- #---------------------------------------------------
- SPE=(
- ( 0x0080088008200000, 0x0000008008000000, 0x0000000000200020, 0x0080088008200020,
- 0x0000000000200000, 0x0080088008000020, 0x0000008008000020, 0x0000000000200020,
- 0x0080088008000020, 0x0080088008200000, 0x0000008008200000, 0x0080080000000020,
- 0x0080080000200020, 0x0000000000200000, 0x0000000000000000, 0x0000008008000020,
- 0x0000008008000000, 0x0000000000000020, 0x0080080000200000, 0x0080088008000000,
- 0x0080088008200020, 0x0000008008200000, 0x0080080000000020, 0x0080080000200000,
- 0x0000000000000020, 0x0080080000000000, 0x0080088008000000, 0x0000008008200020,
- 0x0080080000000000, 0x0080080000200020, 0x0000008008200020, 0x0000000000000000,
- 0x0000000000000000, 0x0080088008200020, 0x0080080000200000, 0x0000008008000020,
- 0x0080088008200000, 0x0000008008000000, 0x0080080000000020, 0x0080080000200000,
- 0x0000008008200020, 0x0080080000000000, 0x0080088008000000, 0x0000000000200020,
- 0x0080088008000020, 0x0000000000000020, 0x0000000000200020, 0x0000008008200000,
- 0x0080088008200020, 0x0080088008000000, 0x0000008008200000, 0x0080080000200020,
- 0x0000000000200000, 0x0080080000000020, 0x0000008008000020, 0x0000000000000000,
- 0x0000008008000000, 0x0000000000200000, 0x0080080000200020, 0x0080088008200000,
- 0x0000000000000020, 0x0000008008200020, 0x0080080000000000, 0x0080088008000020, ),
- ( 0x1000800810004004, 0x0000000000000000, 0x0000800810000000, 0x0000000010004004,
- 0x1000000000004004, 0x1000800800000000, 0x0000800800004004, 0x0000800810000000,
- 0x0000800800000000, 0x1000000010004004, 0x1000000000000000, 0x0000800800004004,
- 0x1000000010000000, 0x0000800810004004, 0x0000000010004004, 0x1000000000000000,
- 0x0000000010000000, 0x1000800800004004, 0x1000000010004004, 0x0000800800000000,
- 0x1000800810000000, 0x0000000000004004, 0x0000000000000000, 0x1000000010000000,
- 0x1000800800004004, 0x1000800810000000, 0x0000800810004004, 0x1000000000004004,
- 0x0000000000004004, 0x0000000010000000, 0x1000800800000000, 0x1000800810004004,
- 0x1000000010000000, 0x0000800810004004, 0x0000800800004004, 0x1000800810000000,
- 0x1000800810004004, 0x1000000010000000, 0x1000000000004004, 0x0000000000000000,
- 0x0000000000004004, 0x1000800800000000, 0x0000000010000000, 0x1000000010004004,
- 0x0000800800000000, 0x0000000000004004, 0x1000800810000000, 0x1000800800004004,
- 0x0000800810004004, 0x0000800800000000, 0x0000000000000000, 0x1000000000004004,
- 0x1000000000000000, 0x1000800810004004, 0x0000800810000000, 0x0000000010004004,
- 0x1000000010004004, 0x0000000010000000, 0x1000800800000000, 0x0000800800004004,
- 0x1000800800004004, 0x1000000000000000, 0x0000000010004004, 0x0000800810000000, ),
- ( 0x0000000000400410, 0x0010004004400400, 0x0010000000000000, 0x0010000000400410,
- 0x0000004004000010, 0x0000000000400400, 0x0010000000400410, 0x0010004004000000,
- 0x0010000000400400, 0x0000004004000000, 0x0000004004400400, 0x0000000000000010,
- 0x0010004004400410, 0x0010000000000010, 0x0000000000000010, 0x0000004004400410,
- 0x0000000000000000, 0x0000004004000010, 0x0010004004400400, 0x0010000000000000,
- 0x0010000000000010, 0x0010004004400410, 0x0000004004000000, 0x0000000000400410,
- 0x0000004004400410, 0x0010000000400400, 0x0010004004000010, 0x0000004004400400,
- 0x0010004004000000, 0x0000000000000000, 0x0000000000400400, 0x0010004004000010,
- 0x0010004004400400, 0x0010000000000000, 0x0000000000000010, 0x0000004004000000,
- 0x0010000000000010, 0x0000004004000010, 0x0000004004400400, 0x0010000000400410,
- 0x0000000000000000, 0x0010004004400400, 0x0010004004000000, 0x0000004004400410,
- 0x0000004004000010, 0x0000000000400400, 0x0010004004400410, 0x0000000000000010,
- 0x0010004004000010, 0x0000000000400410, 0x0000000000400400, 0x0010004004400410,
- 0x0000004004000000, 0x0010000000400400, 0x0010000000400410, 0x0010004004000000,
- 0x0010000000400400, 0x0000000000000000, 0x0000004004400410, 0x0010000000000010,
- 0x0000000000400410, 0x0010004004000010, 0x0010000000000000, 0x0000004004400400, ),
- ( 0x0800100040040080, 0x0000100000001000, 0x0800000000000080, 0x0800100040041080,
- 0x0000000000000000, 0x0000000040041000, 0x0800100000001080, 0x0800000040040080,
- 0x0000100040041000, 0x0800000000001080, 0x0000000000001000, 0x0800100000000080,
- 0x0800000000001080, 0x0800100040040080, 0x0000000040040000, 0x0000000000001000,
- 0x0800000040041080, 0x0000100040040000, 0x0000100000000000, 0x0800000000000080,
- 0x0000100040040000, 0x0800100000001080, 0x0000000040041000, 0x0000100000000000,
- 0x0800100000000080, 0x0000000000000000, 0x0800000040040080, 0x0000100040041000,
- 0x0000100000001000, 0x0800000040041080, 0x0800100040041080, 0x0000000040040000,
- 0x0800000040041080, 0x0800100000000080, 0x0000000040040000, 0x0800000000001080,
- 0x0000100040040000, 0x0000100000001000, 0x0800000000000080, 0x0000000040041000,
- 0x0800100000001080, 0x0000000000000000, 0x0000100000000000, 0x0800000040040080,
- 0x0000000000000000, 0x0800000040041080, 0x0000100040041000, 0x0000100000000000,
- 0x0000000000001000, 0x0800100040041080, 0x0800100040040080, 0x0000000040040000,
- 0x0800100040041080, 0x0800000000000080, 0x0000100000001000, 0x0800100040040080,
- 0x0800000040040080, 0x0000100040040000, 0x0000000040041000, 0x0800100000001080,
- 0x0800100000000080, 0x0000000000001000, 0x0800000000001080, 0x0000100040041000, ),
- ( 0x0000000000800800, 0x0000001000000000, 0x0040040000000000, 0x2040041000800800,
- 0x2000001000800800, 0x0040040000800800, 0x2040041000000000, 0x0000001000800800,
- 0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040041000000000,
- 0x2040040000800800, 0x2000001000800800, 0x0040041000800800, 0x0000000000000000,
- 0x0040041000000000, 0x0000000000800800, 0x2000001000000000, 0x2040040000000000,
- 0x0040040000800800, 0x2040041000000000, 0x0000000000000000, 0x2000000000800800,
- 0x2000000000000000, 0x2040040000800800, 0x2040041000800800, 0x2000001000000000,
- 0x0000001000800800, 0x0040040000000000, 0x2040040000000000, 0x0040041000800800,
- 0x0040041000800800, 0x2040040000800800, 0x2000001000000000, 0x0000001000800800,
- 0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040040000800800,
- 0x0000000000800800, 0x0040041000000000, 0x2040041000800800, 0x0000000000000000,
- 0x2040041000000000, 0x0000000000800800, 0x0040040000000000, 0x2000001000000000,
- 0x2040040000800800, 0x0040040000000000, 0x0000000000000000, 0x2040041000800800,
- 0x2000001000800800, 0x0040041000800800, 0x2040040000000000, 0x0000001000000000,
- 0x0040041000000000, 0x2000001000800800, 0x0040040000800800, 0x2040040000000000,
- 0x2000000000000000, 0x2040041000000000, 0x0000001000800800, 0x2000000000800800, ),
- ( 0x4004000000008008, 0x4004000020000000, 0x0000000000000000, 0x0000200020008008,
- 0x4004000020000000, 0x0000200000000000, 0x4004200000008008, 0x0000000020000000,
- 0x4004200000000000, 0x4004200020008008, 0x0000200020000000, 0x0000000000008008,
- 0x0000200000008008, 0x4004000000008008, 0x0000000020008008, 0x4004200020000000,
- 0x0000000020000000, 0x4004200000008008, 0x4004000020008008, 0x0000000000000000,
- 0x0000200000000000, 0x4004000000000000, 0x0000200020008008, 0x4004000020008008,
- 0x4004200020008008, 0x0000000020008008, 0x0000000000008008, 0x4004200000000000,
- 0x4004000000000000, 0x0000200020000000, 0x4004200020000000, 0x0000200000008008,
- 0x4004200000000000, 0x0000000000008008, 0x0000200000008008, 0x4004200020000000,
- 0x0000200020008008, 0x4004000020000000, 0x0000000000000000, 0x0000200000008008,
- 0x0000000000008008, 0x0000200000000000, 0x4004000020008008, 0x0000000020000000,
- 0x4004000020000000, 0x4004200020008008, 0x0000200020000000, 0x4004000000000000,
- 0x4004200020008008, 0x0000200020000000, 0x0000000020000000, 0x4004200000008008,
- 0x4004000000008008, 0x0000000020008008, 0x4004200020000000, 0x0000000000000000,
- 0x0000200000000000, 0x4004000000008008, 0x4004200000008008, 0x0000200020008008,
- 0x0000000020008008, 0x4004200000000000, 0x4004000000000000, 0x4004000020008008, ),
- ( 0x0000400400000000, 0x0020000000000000, 0x0020000000100000, 0x0400000000100040,
- 0x0420400400100040, 0x0400400400000040, 0x0020400400000000, 0x0000000000000000,
- 0x0000000000100000, 0x0420000000100040, 0x0420000000000040, 0x0000400400100000,
- 0x0400000000000040, 0x0020400400100000, 0x0000400400100000, 0x0420000000000040,
- 0x0420000000100040, 0x0000400400000000, 0x0400400400000040, 0x0420400400100040,
- 0x0000000000000000, 0x0020000000100000, 0x0400000000100040, 0x0020400400000000,
- 0x0400400400100040, 0x0420400400000040, 0x0020400400100000, 0x0400000000000040,
- 0x0420400400000040, 0x0400400400100040, 0x0020000000000000, 0x0000000000100000,
- 0x0420400400000040, 0x0000400400100000, 0x0400400400100040, 0x0420000000000040,
- 0x0000400400000000, 0x0020000000000000, 0x0000000000100000, 0x0400400400100040,
- 0x0420000000100040, 0x0420400400000040, 0x0020400400000000, 0x0000000000000000,
- 0x0020000000000000, 0x0400000000100040, 0x0400000000000040, 0x0020000000100000,
- 0x0000000000000000, 0x0420000000100040, 0x0020000000100000, 0x0020400400000000,
- 0x0420000000000040, 0x0000400400000000, 0x0420400400100040, 0x0000000000100000,
- 0x0020400400100000, 0x0400000000000040, 0x0400400400000040, 0x0420400400100040,
- 0x0400000000100040, 0x0020400400100000, 0x0000400400100000, 0x0400400400000040, ),
- ( 0x8008000080082000, 0x0000002080082000, 0x8008002000000000, 0x0000000000000000,
- 0x0000002000002000, 0x8008000080080000, 0x0000000080082000, 0x8008002080082000,
- 0x8008000000000000, 0x0000000000002000, 0x0000002080080000, 0x8008002000000000,
- 0x8008002080080000, 0x8008002000002000, 0x8008000000002000, 0x0000000080082000,
- 0x0000002000000000, 0x8008002080080000, 0x8008000080080000, 0x0000002000002000,
- 0x8008002080082000, 0x8008000000002000, 0x0000000000000000, 0x0000002080080000,
- 0x0000000000002000, 0x0000000080080000, 0x8008002000002000, 0x8008000080082000,
- 0x0000000080080000, 0x0000002000000000, 0x0000002080082000, 0x8008000000000000,
- 0x0000000080080000, 0x0000002000000000, 0x8008000000002000, 0x8008002080082000,
- 0x8008002000000000, 0x0000000000002000, 0x0000000000000000, 0x0000002080080000,
- 0x8008000080082000, 0x8008002000002000, 0x0000002000002000, 0x8008000080080000,
- 0x0000002080082000, 0x8008000000000000, 0x8008000080080000, 0x0000002000002000,
- 0x8008002080082000, 0x0000000080080000, 0x0000000080082000, 0x8008000000002000,
- 0x0000002080080000, 0x8008002000000000, 0x8008002000002000, 0x0000000080082000,
- 0x8008000000000000, 0x0000002080082000, 0x8008002080080000, 0x0000000000000000,
- 0x0000000000002000, 0x8008000080082000, 0x0000002000000000, 0x8008002080080000, ),
- )
-
- #---------------------------------------------------
- # compressed/interleaved => final permutation table
- # Compression, final permutation, bit reverse
- #---------------------------------------------------
- #NOTE: this was reordered from original table to make perm6464 logic simpler
- CF6464=(
- ( 0x0000000000000000, 0x0000002000000000, 0x0000200000000000, 0x0000202000000000,
- 0x0020000000000000, 0x0020002000000000, 0x0020200000000000, 0x0020202000000000,
- 0x2000000000000000, 0x2000002000000000, 0x2000200000000000, 0x2000202000000000,
- 0x2020000000000000, 0x2020002000000000, 0x2020200000000000, 0x2020202000000000, ),
- ( 0x0000000000000000, 0x0000000200000000, 0x0000020000000000, 0x0000020200000000,
- 0x0002000000000000, 0x0002000200000000, 0x0002020000000000, 0x0002020200000000,
- 0x0200000000000000, 0x0200000200000000, 0x0200020000000000, 0x0200020200000000,
- 0x0202000000000000, 0x0202000200000000, 0x0202020000000000, 0x0202020200000000, ),
- ( 0x0000000000000000, 0x0000000000000020, 0x0000000000002000, 0x0000000000002020,
- 0x0000000000200000, 0x0000000000200020, 0x0000000000202000, 0x0000000000202020,
- 0x0000000020000000, 0x0000000020000020, 0x0000000020002000, 0x0000000020002020,
- 0x0000000020200000, 0x0000000020200020, 0x0000000020202000, 0x0000000020202020, ),
- ( 0x0000000000000000, 0x0000000000000002, 0x0000000000000200, 0x0000000000000202,
- 0x0000000000020000, 0x0000000000020002, 0x0000000000020200, 0x0000000000020202,
- 0x0000000002000000, 0x0000000002000002, 0x0000000002000200, 0x0000000002000202,
- 0x0000000002020000, 0x0000000002020002, 0x0000000002020200, 0x0000000002020202, ),
- ( 0x0000000000000000, 0x0000008000000000, 0x0000800000000000, 0x0000808000000000,
- 0x0080000000000000, 0x0080008000000000, 0x0080800000000000, 0x0080808000000000,
- 0x8000000000000000, 0x8000008000000000, 0x8000800000000000, 0x8000808000000000,
- 0x8080000000000000, 0x8080008000000000, 0x8080800000000000, 0x8080808000000000, ),
- ( 0x0000000000000000, 0x0000000800000000, 0x0000080000000000, 0x0000080800000000,
- 0x0008000000000000, 0x0008000800000000, 0x0008080000000000, 0x0008080800000000,
- 0x0800000000000000, 0x0800000800000000, 0x0800080000000000, 0x0800080800000000,
- 0x0808000000000000, 0x0808000800000000, 0x0808080000000000, 0x0808080800000000, ),
- ( 0x0000000000000000, 0x0000000000000080, 0x0000000000008000, 0x0000000000008080,
- 0x0000000000800000, 0x0000000000800080, 0x0000000000808000, 0x0000000000808080,
- 0x0000000080000000, 0x0000000080000080, 0x0000000080008000, 0x0000000080008080,
- 0x0000000080800000, 0x0000000080800080, 0x0000000080808000, 0x0000000080808080, ),
- ( 0x0000000000000000, 0x0000000000000008, 0x0000000000000800, 0x0000000000000808,
- 0x0000000000080000, 0x0000000000080008, 0x0000000000080800, 0x0000000000080808,
- 0x0000000008000000, 0x0000000008000008, 0x0000000008000800, 0x0000000008000808,
- 0x0000000008080000, 0x0000000008080008, 0x0000000008080800, 0x0000000008080808, ),
- ( 0x0000000000000000, 0x0000001000000000, 0x0000100000000000, 0x0000101000000000,
- 0x0010000000000000, 0x0010001000000000, 0x0010100000000000, 0x0010101000000000,
- 0x1000000000000000, 0x1000001000000000, 0x1000100000000000, 0x1000101000000000,
- 0x1010000000000000, 0x1010001000000000, 0x1010100000000000, 0x1010101000000000, ),
- ( 0x0000000000000000, 0x0000000100000000, 0x0000010000000000, 0x0000010100000000,
- 0x0001000000000000, 0x0001000100000000, 0x0001010000000000, 0x0001010100000000,
- 0x0100000000000000, 0x0100000100000000, 0x0100010000000000, 0x0100010100000000,
- 0x0101000000000000, 0x0101000100000000, 0x0101010000000000, 0x0101010100000000, ),
- ( 0x0000000000000000, 0x0000000000000010, 0x0000000000001000, 0x0000000000001010,
- 0x0000000000100000, 0x0000000000100010, 0x0000000000101000, 0x0000000000101010,
- 0x0000000010000000, 0x0000000010000010, 0x0000000010001000, 0x0000000010001010,
- 0x0000000010100000, 0x0000000010100010, 0x0000000010101000, 0x0000000010101010, ),
- ( 0x0000000000000000, 0x0000000000000001, 0x0000000000000100, 0x0000000000000101,
- 0x0000000000010000, 0x0000000000010001, 0x0000000000010100, 0x0000000000010101,
- 0x0000000001000000, 0x0000000001000001, 0x0000000001000100, 0x0000000001000101,
- 0x0000000001010000, 0x0000000001010001, 0x0000000001010100, 0x0000000001010101, ),
- ( 0x0000000000000000, 0x0000004000000000, 0x0000400000000000, 0x0000404000000000,
- 0x0040000000000000, 0x0040004000000000, 0x0040400000000000, 0x0040404000000000,
- 0x4000000000000000, 0x4000004000000000, 0x4000400000000000, 0x4000404000000000,
- 0x4040000000000000, 0x4040004000000000, 0x4040400000000000, 0x4040404000000000, ),
- ( 0x0000000000000000, 0x0000000400000000, 0x0000040000000000, 0x0000040400000000,
- 0x0004000000000000, 0x0004000400000000, 0x0004040000000000, 0x0004040400000000,
- 0x0400000000000000, 0x0400000400000000, 0x0400040000000000, 0x0400040400000000,
- 0x0404000000000000, 0x0404000400000000, 0x0404040000000000, 0x0404040400000000, ),
- ( 0x0000000000000000, 0x0000000000000040, 0x0000000000004000, 0x0000000000004040,
- 0x0000000000400000, 0x0000000000400040, 0x0000000000404000, 0x0000000000404040,
- 0x0000000040000000, 0x0000000040000040, 0x0000000040004000, 0x0000000040004040,
- 0x0000000040400000, 0x0000000040400040, 0x0000000040404000, 0x0000000040404040, ),
- ( 0x0000000000000000, 0x0000000000000004, 0x0000000000000400, 0x0000000000000404,
- 0x0000000000040000, 0x0000000000040004, 0x0000000000040400, 0x0000000000040404,
- 0x0000000004000000, 0x0000000004000004, 0x0000000004000400, 0x0000000004000404,
- 0x0000000004040000, 0x0000000004040004, 0x0000000004040400, 0x0000000004040404, ),
- )
- #=========================================================
- #eof load_data
- #=========================================================
-
-#=========================================================
-#des frontend
-#=========================================================
-def permute(c, p):
- """Returns the permutation of the given 32-bit or 64-bit code with
- the specified permutataion table."""
- #NOTE: only difference between 32 & 64 bit permutations
- #is that len(p)==8 for 32 bit, and len(p)==16 for 64 bit.
- out = 0
- for r in p:
- out |= r[c&0xf]
- c >>= 4
- return out
-
-def bytes_to_int(value):
- out = 0
- for v in value:
- out = (out<<8) | ord(v)
- return out
-
-def int_to_bytes(value, count):
- return ''.join(
- chr((value>>s) & 0xff)
- for s in xrange(8*count-8,-8,-8)
- )
-
-def des_encrypt_block(key, input):
- "do traditional encryption of a single DES block"
- assert len(input) == 8
- assert len(key) == 8
- input = bytes_to_int(input)
- key = bytes_to_int(key)
- out = des_encrypt_rounds(input, 0, 1, key)
- return int_to_bytes(out, 8)
-
-def expand_des_key(source):
- "convert 7 byte des key to 8 byte des key (by adding parity bit every 7 bits)"
- #NOTE: could probably do this much more cleverly and efficiently,
- # but no need really given it's use
- assert len(source) == 7
-
- def iter_bits(source):
- for c in source:
- v = ord(c)
- for i in xrange(7,-1,-1):
- yield (v>>i) & 1
-
- out = 0
- p = 1
- for i, b in enumerate(iter_bits(source)):
- out = (out<<1) + b
- p ^= b
- if i % 7 == 6:
- out = (out<<1) + p
- p = 1
-
- return ''.join(
- chr((out>>s) & 0xFF)
- for s in xrange(8*7,-8,-8)
- )
-
-def des_encrypt_rounds(input, salt, rounds, key):
- """returns modified DES for single block of input, used by des-crypt algorithm"""
- global SPE, PCXROT, IE3264, CF6464
-
- #bounds check
- assert 0 <= input <= INT_64_MAX, "input value out of range"
- assert 0 <= salt <= INT_24_MAX, "salt value out of range"
- assert rounds >= 0
- assert 0 <= key <= INT_64_MAX, "key value out of range"
-
- #load tables if not already done
- if PCXROT is None:
- load_tables()
-
- #convert key int -> key schedule
- #NOTE: generation was modified to output two elements at a time,
- #to optimize for per-round algorithm below.
- mask = ~0x0303030300000000
- def _gen(K):
- for p_even, p_odd in PCXROT:
- K1 = permute(K, p_even)
- K = permute(K1, p_odd)
- yield K1 & mask, K & mask
- ks_list = list(_gen(key))
-
- #expand 24 bit salt -> 32 bit
- salt = (
- ((salt & 0x00003f) << 26) |
- ((salt & 0x000fc0) << 12) |
- ((salt & 0x03f000) >> 2) |
- ((salt & 0xfc0000) >> 16)
- )
-
- #init L & R
- if input == 0:
- L = R = 0
- else:
- L = ((input >> 31) & 0xaaaaaaaa) | (input & 0x55555555)
- L = permute(L, IE3264)
-
- R = ((input >> 32) & 0xaaaaaaaa) | ((input >> 1) & 0x55555555)
- R = permute(R, IE3264)
-
- #load SPE into local vars to speed things up and remove an array access
- SPE0, SPE1, SPE2, SPE3, SPE4, SPE5, SPE6, SPE7 = SPE
-
- #run specified number of passed
- while rounds:
- rounds -= 1
-
- #run over each part of the schedule
- for ks_even, ks_odd in ks_list:
- k = ((R>>32) ^ R) & salt
- B = (k<<32) ^ k ^ R ^ ks_even
-
- L ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
- SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^
- SPE4[(B>>26)&0x3f] ^ SPE5[(B>>18)&0x3f] ^
- SPE6[(B>>10)&0x3f] ^ SPE7[(B>>2)&0x3f])
-
- k = ((L>>32) ^ L) & salt
- B = (k<<32) ^ k ^ L ^ ks_odd
-
- R ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
- SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^
- SPE4[(B>>26)&0x3f] ^ SPE5[(B>>18)&0x3f] ^
- SPE6[(B>>10)&0x3f] ^ SPE7[(B>>2)&0x3f])
-
- # swap L and R
- L, R = R, L
-
- C = (
- ((L>>3) & 0x0f0f0f0f00000000L)
- |
- ((L<<33) & 0xf0f0f0f000000000L)
- |
- ((R>>35) & 0x000000000f0f0f0fL)
- |
- ((R<<1) & 0x00000000f0f0f0f0L)
- )
-
- C = permute(C, CF6464)
-
- return C
-
-#=========================================================
#crypt frontend
#=========================================================
def _crypt_secret_to_key(secret):
@@ -789,7 +110,7 @@ def crypt(secret, config):
key_value = _crypt_secret_to_key(secret)
#run data through des using input of 0
- result = des_encrypt_rounds(0, salt_value, 25, key_value)
+ result = mdes_encrypt_int_rounds(key_value, 0, salt=salt_value, rounds=25)
#run h64 encode on result
return salt + b64_encode_int64(result)
@@ -820,12 +141,12 @@ def raw_ext_crypt(secret, salt, rounds):
key_value = _crypt_secret_to_key(secret)
while len(secret) > 8:
secret = secret[8:]
- key_value = des_encrypt_rounds(key_value, 0, 1, key_value)
+ key_value = mdes_encrypt_rounds(key_value, key_value, salt=0, rounds=1)
for i,c in enumerate(secret[:8]):
key_value ^= (ord(c)&0x7f)<<(57-8*i)
#run data through des using input of 0
- result = des_encrypt_rounds(0, salt_value, rounds, key_value)
+ result = mdes_encrypt_int_rounds(key_value, 0, salt=salt_value, rounds=rounds)
#run h64 encode on result
return b64_encode_int64(result)
diff --git a/passlib/utils/des.py b/passlib/utils/des.py
new file mode 100644
index 0000000..e899cd5
--- /dev/null
+++ b/passlib/utils/des.py
@@ -0,0 +1,724 @@
+"""passlib.utils.des -- DES encryption routines
+
+This module contains routines for encrypting blocks of data using the DES algorithm.
+
+They do not support multi-block operation or decryption,
+since they are designed for use in password hash algorithms
+such as ``lmhash`` and ``des-crypt``.
+
+.. function:: des_expand_key
+.. function:: des_encrypt_block
+.. function:: mdes_encrypt_int_block
+"""
+
+"""
+History
+=======
+These routines (which have since been drastically modified for python)
+are based on a Java implementation of the des-crypt algorithm,
+found at `<http://www.dynamic.net.au/christos/crypt/UnixCrypt2.txt>`_.
+
+The copyright & license for that source is as follows::
+
+ UnixCrypt.java 0.9 96/11/25
+ Copyright (c) 1996 Aki Yoshida. All rights reserved.
+ Permission to use, copy, modify and distribute this software
+ for non-commercial or commercial purposes and without fee is
+ hereby granted provided that this copyright notice appears in
+ all copies.
+
+ ---
+
+ Unix crypt(3C) utility
+ @version 0.9, 11/25/96
+ @author Aki Yoshida
+
+ ---
+
+ modified April 2001
+ by Iris Van den Broeke, Daniel Deville
+
+ ---
+ Unix Crypt.
+ Implements the one way cryptography used by Unix systems for
+ simple password protection.
+ @version $Id: UnixCrypt2.txt,v 1.1.1.1 2005/09/13 22:20:13 christos Exp $
+ @author Greg Wilkins (gregw)
+"""
+#=========================================================
+#imports
+#=========================================================
+#pkg
+from passlib.utils import bytes_to_int, int_to_bytes
+#local
+__all__ = [
+ "expand_des_key",
+ "des_encrypt_block",
+ "mdes_encrypt_int_block",
+]
+
+#=========================================================
+#precalculated iteration ranges & constants
+#=========================================================
+R8 = range(8)
+RR8 = range(7, -1, -1)
+RR4 = range(3, -1, -1)
+RR12_1 = range(11, 1, -1)
+RR9_1 = range(9,-1,-1)
+
+RR6_S2 = range(6, -1, -2)
+RR14_S2 = range(14, -1, -2)
+R16_S2 = range(0, 16, 2)
+
+INT_24_MAX = 0xffffffL
+INT_64_MAX = 0xffffffffL
+INT_64_MAX = 0xffffffffffffffffL
+
+#=========================================================
+# static tables for des
+#=========================================================
+PCXROT = IE3264 = SPE = CF6464 = None #placeholders filled in by load_tables
+
+def load_tables():
+ "delay loading tables until they are actually needed"
+ global PCXROT, IE3264, SPE, CF6464
+
+ #---------------------------------------------------
+ # Initial key schedule permutation
+ # PC1ROT - bit reverse, then PC1, then Rotate, then PC2
+ #---------------------------------------------------
+ #NOTE: this was reordered from original table to make perm3264 logic simpler
+ PC1ROT=(
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000002000, 0x0000000000002000,
+ 0x0000000000000020, 0x0000000000000020, 0x0000000000002020, 0x0000000000002020,
+ 0x0000000000000400, 0x0000000000000400, 0x0000000000002400, 0x0000000000002400,
+ 0x0000000000000420, 0x0000000000000420, 0x0000000000002420, 0x0000000000002420, ),
+ ( 0x0000000000000000, 0x2000000000000000, 0x0000000400000000, 0x2000000400000000,
+ 0x0000800000000000, 0x2000800000000000, 0x0000800400000000, 0x2000800400000000,
+ 0x0008000000000000, 0x2008000000000000, 0x0008000400000000, 0x2008000400000000,
+ 0x0008800000000000, 0x2008800000000000, 0x0008800400000000, 0x2008800400000000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000040, 0x0000000000000040,
+ 0x0000000020000000, 0x0000000020000000, 0x0000000020000040, 0x0000000020000040,
+ 0x0000000000200000, 0x0000000000200000, 0x0000000000200040, 0x0000000000200040,
+ 0x0000000020200000, 0x0000000020200000, 0x0000000020200040, 0x0000000020200040, ),
+ ( 0x0000000000000000, 0x0002000000000000, 0x0800000000000000, 0x0802000000000000,
+ 0x0100000000000000, 0x0102000000000000, 0x0900000000000000, 0x0902000000000000,
+ 0x4000000000000000, 0x4002000000000000, 0x4800000000000000, 0x4802000000000000,
+ 0x4100000000000000, 0x4102000000000000, 0x4900000000000000, 0x4902000000000000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000040000, 0x0000000000040000,
+ 0x0000020000000000, 0x0000020000000000, 0x0000020000040000, 0x0000020000040000,
+ 0x0000000000000004, 0x0000000000000004, 0x0000000000040004, 0x0000000000040004,
+ 0x0000020000000004, 0x0000020000000004, 0x0000020000040004, 0x0000020000040004, ),
+ ( 0x0000000000000000, 0x0000400000000000, 0x0200000000000000, 0x0200400000000000,
+ 0x0080000000000000, 0x0080400000000000, 0x0280000000000000, 0x0280400000000000,
+ 0x0000008000000000, 0x0000408000000000, 0x0200008000000000, 0x0200408000000000,
+ 0x0080008000000000, 0x0080408000000000, 0x0280008000000000, 0x0280408000000000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000010000000, 0x0000000010000000,
+ 0x0000000000001000, 0x0000000000001000, 0x0000000010001000, 0x0000000010001000,
+ 0x0000000040000000, 0x0000000040000000, 0x0000000050000000, 0x0000000050000000,
+ 0x0000000040001000, 0x0000000040001000, 0x0000000050001000, 0x0000000050001000, ),
+ ( 0x0000000000000000, 0x0000001000000000, 0x0000080000000000, 0x0000081000000000,
+ 0x1000000000000000, 0x1000001000000000, 0x1000080000000000, 0x1000081000000000,
+ 0x0004000000000000, 0x0004001000000000, 0x0004080000000000, 0x0004081000000000,
+ 0x1004000000000000, 0x1004001000000000, 0x1004080000000000, 0x1004081000000000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000080, 0x0000000000000080,
+ 0x0000000000080000, 0x0000000000080000, 0x0000000000080080, 0x0000000000080080,
+ 0x0000000000800000, 0x0000000000800000, 0x0000000000800080, 0x0000000000800080,
+ 0x0000000000880000, 0x0000000000880000, 0x0000000000880080, 0x0000000000880080, ),
+ ( 0x0000000000000000, 0x0000000008000000, 0x0000002000000000, 0x0000002008000000,
+ 0x0000100000000000, 0x0000100008000000, 0x0000102000000000, 0x0000102008000000,
+ 0x0000200000000000, 0x0000200008000000, 0x0000202000000000, 0x0000202008000000,
+ 0x0000300000000000, 0x0000300008000000, 0x0000302000000000, 0x0000302008000000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000400000, 0x0000000000400000,
+ 0x0000000004000000, 0x0000000004000000, 0x0000000004400000, 0x0000000004400000,
+ 0x0000000000000800, 0x0000000000000800, 0x0000000000400800, 0x0000000000400800,
+ 0x0000000004000800, 0x0000000004000800, 0x0000000004400800, 0x0000000004400800, ),
+ ( 0x0000000000000000, 0x0000000000008000, 0x0040000000000000, 0x0040000000008000,
+ 0x0000004000000000, 0x0000004000008000, 0x0040004000000000, 0x0040004000008000,
+ 0x8000000000000000, 0x8000000000008000, 0x8040000000000000, 0x8040000000008000,
+ 0x8000004000000000, 0x8000004000008000, 0x8040004000000000, 0x8040004000008000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000004000, 0x0000000000004000,
+ 0x0000000000000008, 0x0000000000000008, 0x0000000000004008, 0x0000000000004008,
+ 0x0000000000000010, 0x0000000000000010, 0x0000000000004010, 0x0000000000004010,
+ 0x0000000000000018, 0x0000000000000018, 0x0000000000004018, 0x0000000000004018, ),
+ ( 0x0000000000000000, 0x0000000200000000, 0x0001000000000000, 0x0001000200000000,
+ 0x0400000000000000, 0x0400000200000000, 0x0401000000000000, 0x0401000200000000,
+ 0x0020000000000000, 0x0020000200000000, 0x0021000000000000, 0x0021000200000000,
+ 0x0420000000000000, 0x0420000200000000, 0x0421000000000000, 0x0421000200000000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000010000000000, 0x0000010000000000,
+ 0x0000000100000000, 0x0000000100000000, 0x0000010100000000, 0x0000010100000000,
+ 0x0000000000100000, 0x0000000000100000, 0x0000010000100000, 0x0000010000100000,
+ 0x0000000100100000, 0x0000000100100000, 0x0000010100100000, 0x0000010100100000, ),
+ ( 0x0000000000000000, 0x0000000080000000, 0x0000040000000000, 0x0000040080000000,
+ 0x0010000000000000, 0x0010000080000000, 0x0010040000000000, 0x0010040080000000,
+ 0x0000000800000000, 0x0000000880000000, 0x0000040800000000, 0x0000040880000000,
+ 0x0010000800000000, 0x0010000880000000, 0x0010040800000000, 0x0010040880000000, ),
+ )
+ #---------------------------------------------------
+ # Subsequent key schedule rotation permutations
+ # PC2ROT - PC2 inverse, then Rotate, then PC2
+ #---------------------------------------------------
+ #NOTE: this was reordered from original table to make perm3264 logic simpler
+ PC2ROTA=(
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000000200000, 0x0000000000200000, 0x0000000000200000, 0x0000000000200000,
+ 0x0000000004000000, 0x0000000004000000, 0x0000000004000000, 0x0000000004000000,
+ 0x0000000004200000, 0x0000000004200000, 0x0000000004200000, 0x0000000004200000, ),
+ ( 0x0000000000000000, 0x0000000000000800, 0x0000010000000000, 0x0000010000000800,
+ 0x0000000000002000, 0x0000000000002800, 0x0000010000002000, 0x0000010000002800,
+ 0x0000000010000000, 0x0000000010000800, 0x0000010010000000, 0x0000010010000800,
+ 0x0000000010002000, 0x0000000010002800, 0x0000010010002000, 0x0000010010002800, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000100000000, 0x0000000100000000, 0x0000000100000000, 0x0000000100000000,
+ 0x0000000000800000, 0x0000000000800000, 0x0000000000800000, 0x0000000000800000,
+ 0x0000000100800000, 0x0000000100800000, 0x0000000100800000, 0x0000000100800000, ),
+ ( 0x0000000000000000, 0x0000020000000000, 0x0000000080000000, 0x0000020080000000,
+ 0x0000000000400000, 0x0000020000400000, 0x0000000080400000, 0x0000020080400000,
+ 0x0000000008000000, 0x0000020008000000, 0x0000000088000000, 0x0000020088000000,
+ 0x0000000008400000, 0x0000020008400000, 0x0000000088400000, 0x0000020088400000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000000000040, 0x0000000000000040, 0x0000000000000040, 0x0000000000000040,
+ 0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
+ 0x0000000000001040, 0x0000000000001040, 0x0000000000001040, 0x0000000000001040, ),
+ ( 0x0000000000000000, 0x0000000000000010, 0x0000000000000400, 0x0000000000000410,
+ 0x0000000000000080, 0x0000000000000090, 0x0000000000000480, 0x0000000000000490,
+ 0x0000000040000000, 0x0000000040000010, 0x0000000040000400, 0x0000000040000410,
+ 0x0000000040000080, 0x0000000040000090, 0x0000000040000480, 0x0000000040000490, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
+ 0x0000000000100000, 0x0000000000100000, 0x0000000000100000, 0x0000000000100000,
+ 0x0000000000180000, 0x0000000000180000, 0x0000000000180000, 0x0000000000180000, ),
+ ( 0x0000000000000000, 0x0000000000040000, 0x0000000000000020, 0x0000000000040020,
+ 0x0000000000000004, 0x0000000000040004, 0x0000000000000024, 0x0000000000040024,
+ 0x0000000200000000, 0x0000000200040000, 0x0000000200000020, 0x0000000200040020,
+ 0x0000000200000004, 0x0000000200040004, 0x0000000200000024, 0x0000000200040024, ),
+ ( 0x0000000000000000, 0x0000000000000008, 0x0000000000008000, 0x0000000000008008,
+ 0x0010000000000000, 0x0010000000000008, 0x0010000000008000, 0x0010000000008008,
+ 0x0020000000000000, 0x0020000000000008, 0x0020000000008000, 0x0020000000008008,
+ 0x0030000000000000, 0x0030000000000008, 0x0030000000008000, 0x0030000000008008, ),
+ ( 0x0000000000000000, 0x0000400000000000, 0x0000080000000000, 0x0000480000000000,
+ 0x0000100000000000, 0x0000500000000000, 0x0000180000000000, 0x0000580000000000,
+ 0x4000000000000000, 0x4000400000000000, 0x4000080000000000, 0x4000480000000000,
+ 0x4000100000000000, 0x4000500000000000, 0x4000180000000000, 0x4000580000000000, ),
+ ( 0x0000000000000000, 0x0000000000004000, 0x0000000020000000, 0x0000000020004000,
+ 0x0001000000000000, 0x0001000000004000, 0x0001000020000000, 0x0001000020004000,
+ 0x0200000000000000, 0x0200000000004000, 0x0200000020000000, 0x0200000020004000,
+ 0x0201000000000000, 0x0201000000004000, 0x0201000020000000, 0x0201000020004000, ),
+ ( 0x0000000000000000, 0x1000000000000000, 0x0004000000000000, 0x1004000000000000,
+ 0x0002000000000000, 0x1002000000000000, 0x0006000000000000, 0x1006000000000000,
+ 0x0000000800000000, 0x1000000800000000, 0x0004000800000000, 0x1004000800000000,
+ 0x0002000800000000, 0x1002000800000000, 0x0006000800000000, 0x1006000800000000, ),
+ ( 0x0000000000000000, 0x0040000000000000, 0x2000000000000000, 0x2040000000000000,
+ 0x0000008000000000, 0x0040008000000000, 0x2000008000000000, 0x2040008000000000,
+ 0x0000001000000000, 0x0040001000000000, 0x2000001000000000, 0x2040001000000000,
+ 0x0000009000000000, 0x0040009000000000, 0x2000009000000000, 0x2040009000000000, ),
+ ( 0x0000000000000000, 0x0400000000000000, 0x8000000000000000, 0x8400000000000000,
+ 0x0000002000000000, 0x0400002000000000, 0x8000002000000000, 0x8400002000000000,
+ 0x0100000000000000, 0x0500000000000000, 0x8100000000000000, 0x8500000000000000,
+ 0x0100002000000000, 0x0500002000000000, 0x8100002000000000, 0x8500002000000000, ),
+ ( 0x0000000000000000, 0x0000800000000000, 0x0800000000000000, 0x0800800000000000,
+ 0x0000004000000000, 0x0000804000000000, 0x0800004000000000, 0x0800804000000000,
+ 0x0000000400000000, 0x0000800400000000, 0x0800000400000000, 0x0800800400000000,
+ 0x0000004400000000, 0x0000804400000000, 0x0800004400000000, 0x0800804400000000, ),
+ ( 0x0000000000000000, 0x0080000000000000, 0x0000040000000000, 0x0080040000000000,
+ 0x0008000000000000, 0x0088000000000000, 0x0008040000000000, 0x0088040000000000,
+ 0x0000200000000000, 0x0080200000000000, 0x0000240000000000, 0x0080240000000000,
+ 0x0008200000000000, 0x0088200000000000, 0x0008240000000000, 0x0088240000000000, ),
+ )
+
+ #NOTE: this was reordered from original table to make perm3264 logic simpler
+ PC2ROTB=(
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000000000400, 0x0000000000000400, 0x0000000000000400, 0x0000000000000400,
+ 0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
+ 0x0000000000080400, 0x0000000000080400, 0x0000000000080400, 0x0000000000080400, ),
+ ( 0x0000000000000000, 0x0000000000800000, 0x0000000000004000, 0x0000000000804000,
+ 0x0000000080000000, 0x0000000080800000, 0x0000000080004000, 0x0000000080804000,
+ 0x0000000000040000, 0x0000000000840000, 0x0000000000044000, 0x0000000000844000,
+ 0x0000000080040000, 0x0000000080840000, 0x0000000080044000, 0x0000000080844000, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000000000008, 0x0000000000000008, 0x0000000000000008, 0x0000000000000008,
+ 0x0000000040000000, 0x0000000040000000, 0x0000000040000000, 0x0000000040000000,
+ 0x0000000040000008, 0x0000000040000008, 0x0000000040000008, 0x0000000040000008, ),
+ ( 0x0000000000000000, 0x0000000020000000, 0x0000000200000000, 0x0000000220000000,
+ 0x0000000000000080, 0x0000000020000080, 0x0000000200000080, 0x0000000220000080,
+ 0x0000000000100000, 0x0000000020100000, 0x0000000200100000, 0x0000000220100000,
+ 0x0000000000100080, 0x0000000020100080, 0x0000000200100080, 0x0000000220100080, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000000002000, 0x0000000000002000, 0x0000000000002000, 0x0000000000002000,
+ 0x0000020000000000, 0x0000020000000000, 0x0000020000000000, 0x0000020000000000,
+ 0x0000020000002000, 0x0000020000002000, 0x0000020000002000, 0x0000020000002000, ),
+ ( 0x0000000000000000, 0x0000000000000800, 0x0000000100000000, 0x0000000100000800,
+ 0x0000000010000000, 0x0000000010000800, 0x0000000110000000, 0x0000000110000800,
+ 0x0000000000000004, 0x0000000000000804, 0x0000000100000004, 0x0000000100000804,
+ 0x0000000010000004, 0x0000000010000804, 0x0000000110000004, 0x0000000110000804, ),
+ ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
+ 0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
+ 0x0000000000000010, 0x0000000000000010, 0x0000000000000010, 0x0000000000000010,
+ 0x0000000000001010, 0x0000000000001010, 0x0000000000001010, 0x0000000000001010, ),
+ ( 0x0000000000000000, 0x0000000000000040, 0x0000010000000000, 0x0000010000000040,
+ 0x0000000000200000, 0x0000000000200040, 0x0000010000200000, 0x0000010000200040,
+ 0x0000000000008000, 0x0000000000008040, 0x0000010000008000, 0x0000010000008040,
+ 0x0000000000208000, 0x0000000000208040, 0x0000010000208000, 0x0000010000208040, ),
+ ( 0x0000000000000000, 0x0000000004000000, 0x0000000008000000, 0x000000000c000000,
+ 0x0400000000000000, 0x0400000004000000, 0x0400000008000000, 0x040000000c000000,
+ 0x8000000000000000, 0x8000000004000000, 0x8000000008000000, 0x800000000c000000,
+ 0x8400000000000000, 0x8400000004000000, 0x8400000008000000, 0x840000000c000000, ),
+ ( 0x0000000000000000, 0x0002000000000000, 0x0200000000000000, 0x0202000000000000,
+ 0x1000000000000000, 0x1002000000000000, 0x1200000000000000, 0x1202000000000000,
+ 0x0008000000000000, 0x000a000000000000, 0x0208000000000000, 0x020a000000000000,
+ 0x1008000000000000, 0x100a000000000000, 0x1208000000000000, 0x120a000000000000, ),
+ ( 0x0000000000000000, 0x0000000000400000, 0x0000000000000020, 0x0000000000400020,
+ 0x0040000000000000, 0x0040000000400000, 0x0040000000000020, 0x0040000000400020,
+ 0x0800000000000000, 0x0800000000400000, 0x0800000000000020, 0x0800000000400020,
+ 0x0840000000000000, 0x0840000000400000, 0x0840000000000020, 0x0840000000400020, ),
+ ( 0x0000000000000000, 0x0080000000000000, 0x0000008000000000, 0x0080008000000000,
+ 0x2000000000000000, 0x2080000000000000, 0x2000008000000000, 0x2080008000000000,
+ 0x0020000000000000, 0x00a0000000000000, 0x0020008000000000, 0x00a0008000000000,
+ 0x2020000000000000, 0x20a0000000000000, 0x2020008000000000, 0x20a0008000000000, ),
+ ( 0x0000000000000000, 0x0000002000000000, 0x0000040000000000, 0x0000042000000000,
+ 0x4000000000000000, 0x4000002000000000, 0x4000040000000000, 0x4000042000000000,
+ 0x0000400000000000, 0x0000402000000000, 0x0000440000000000, 0x0000442000000000,
+ 0x4000400000000000, 0x4000402000000000, 0x4000440000000000, 0x4000442000000000, ),
+ ( 0x0000000000000000, 0x0000004000000000, 0x0000200000000000, 0x0000204000000000,
+ 0x0000080000000000, 0x0000084000000000, 0x0000280000000000, 0x0000284000000000,
+ 0x0000800000000000, 0x0000804000000000, 0x0000a00000000000, 0x0000a04000000000,
+ 0x0000880000000000, 0x0000884000000000, 0x0000a80000000000, 0x0000a84000000000, ),
+ ( 0x0000000000000000, 0x0000000800000000, 0x0000000400000000, 0x0000000c00000000,
+ 0x0000100000000000, 0x0000100800000000, 0x0000100400000000, 0x0000100c00000000,
+ 0x0010000000000000, 0x0010000800000000, 0x0010000400000000, 0x0010000c00000000,
+ 0x0010100000000000, 0x0010100800000000, 0x0010100400000000, 0x0010100c00000000, ),
+ ( 0x0000000000000000, 0x0100000000000000, 0x0001000000000000, 0x0101000000000000,
+ 0x0000001000000000, 0x0100001000000000, 0x0001001000000000, 0x0101001000000000,
+ 0x0004000000000000, 0x0104000000000000, 0x0005000000000000, 0x0105000000000000,
+ 0x0004001000000000, 0x0104001000000000, 0x0005001000000000, 0x0105001000000000, ),
+ )
+ #---------------------------------------------------
+ #PCXROT - PC1ROT, PC2ROTA, PC2ROTB listed in order
+ # of the PC1 rotation schedule, as used by des_setkey
+ #---------------------------------------------------
+ ##ROTATES = (1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1)
+ ##PCXROT = (
+ ## PC1ROT, PC2ROTA, PC2ROTB, PC2ROTB,
+ ## PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTB,
+ ## PC2ROTA, PC2ROTB, PC2ROTB, PC2ROTB,
+ ## PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTA,
+ ## )
+
+ #NOTE: modified PCXROT to contain entrys broken into pairs,
+ # to help generate them in format best used by encoder.
+ PCXROT = (
+ (PC1ROT, PC2ROTA), (PC2ROTB, PC2ROTB),
+ (PC2ROTB, PC2ROTB), (PC2ROTB, PC2ROTB),
+ (PC2ROTA, PC2ROTB), (PC2ROTB, PC2ROTB),
+ (PC2ROTB, PC2ROTB), (PC2ROTB, PC2ROTA),
+ )
+
+ #---------------------------------------------------
+ # Bit reverse, intial permupation, expantion
+ # Initial permutation/expansion table
+ #---------------------------------------------------
+ #NOTE: this was reordered from original table to make perm3264 logic simpler
+ IE3264=(
+ ( 0x0000000000000000, 0x0000000000800800, 0x0000000000008008, 0x0000000000808808,
+ 0x0000008008000000, 0x0000008008800800, 0x0000008008008008, 0x0000008008808808,
+ 0x0000000080080000, 0x0000000080880800, 0x0000000080088008, 0x0000000080888808,
+ 0x0000008088080000, 0x0000008088880800, 0x0000008088088008, 0x0000008088888808, ),
+ ( 0x0000000000000000, 0x0080080000000000, 0x0000800800000000, 0x0080880800000000,
+ 0x0800000000000080, 0x0880080000000080, 0x0800800800000080, 0x0880880800000080,
+ 0x8008000000000000, 0x8088080000000000, 0x8008800800000000, 0x8088880800000000,
+ 0x8808000000000080, 0x8888080000000080, 0x8808800800000080, 0x8888880800000080, ),
+ ( 0x0000000000000000, 0x0000000000001000, 0x0000000000000010, 0x0000000000001010,
+ 0x0000000010000000, 0x0000000010001000, 0x0000000010000010, 0x0000000010001010,
+ 0x0000000000100000, 0x0000000000101000, 0x0000000000100010, 0x0000000000101010,
+ 0x0000000010100000, 0x0000000010101000, 0x0000000010100010, 0x0000000010101010, ),
+ ( 0x0000000000000000, 0x0000100000000000, 0x0000001000000000, 0x0000101000000000,
+ 0x1000000000000000, 0x1000100000000000, 0x1000001000000000, 0x1000101000000000,
+ 0x0010000000000000, 0x0010100000000000, 0x0010001000000000, 0x0010101000000000,
+ 0x1010000000000000, 0x1010100000000000, 0x1010001000000000, 0x1010101000000000, ),
+ ( 0x0000000000000000, 0x0000000000002000, 0x0000000000000020, 0x0000000000002020,
+ 0x0000000020000000, 0x0000000020002000, 0x0000000020000020, 0x0000000020002020,
+ 0x0000000000200000, 0x0000000000202000, 0x0000000000200020, 0x0000000000202020,
+ 0x0000000020200000, 0x0000000020202000, 0x0000000020200020, 0x0000000020202020, ),
+ ( 0x0000000000000000, 0x0000200000000000, 0x0000002000000000, 0x0000202000000000,
+ 0x2000000000000000, 0x2000200000000000, 0x2000002000000000, 0x2000202000000000,
+ 0x0020000000000000, 0x0020200000000000, 0x0020002000000000, 0x0020202000000000,
+ 0x2020000000000000, 0x2020200000000000, 0x2020002000000000, 0x2020202000000000, ),
+ ( 0x0000000000000000, 0x0000000000004004, 0x0400000000000040, 0x0400000000004044,
+ 0x0000000040040000, 0x0000000040044004, 0x0400000040040040, 0x0400000040044044,
+ 0x0000000000400400, 0x0000000000404404, 0x0400000000400440, 0x0400000000404444,
+ 0x0000000040440400, 0x0000000040444404, 0x0400000040440440, 0x0400000040444444, ),
+ ( 0x0000000000000000, 0x0000400400000000, 0x0000004004000000, 0x0000404404000000,
+ 0x4004000000000000, 0x4004400400000000, 0x4004004004000000, 0x4004404404000000,
+ 0x0040040000000000, 0x0040440400000000, 0x0040044004000000, 0x0040444404000000,
+ 0x4044040000000000, 0x4044440400000000, 0x4044044004000000, 0x4044444404000000, ),
+ )
+
+ #---------------------------------------------------
+ # Table that combines the S, P, and E operations.
+ #---------------------------------------------------
+ SPE=(
+ ( 0x0080088008200000, 0x0000008008000000, 0x0000000000200020, 0x0080088008200020,
+ 0x0000000000200000, 0x0080088008000020, 0x0000008008000020, 0x0000000000200020,
+ 0x0080088008000020, 0x0080088008200000, 0x0000008008200000, 0x0080080000000020,
+ 0x0080080000200020, 0x0000000000200000, 0x0000000000000000, 0x0000008008000020,
+ 0x0000008008000000, 0x0000000000000020, 0x0080080000200000, 0x0080088008000000,
+ 0x0080088008200020, 0x0000008008200000, 0x0080080000000020, 0x0080080000200000,
+ 0x0000000000000020, 0x0080080000000000, 0x0080088008000000, 0x0000008008200020,
+ 0x0080080000000000, 0x0080080000200020, 0x0000008008200020, 0x0000000000000000,
+ 0x0000000000000000, 0x0080088008200020, 0x0080080000200000, 0x0000008008000020,
+ 0x0080088008200000, 0x0000008008000000, 0x0080080000000020, 0x0080080000200000,
+ 0x0000008008200020, 0x0080080000000000, 0x0080088008000000, 0x0000000000200020,
+ 0x0080088008000020, 0x0000000000000020, 0x0000000000200020, 0x0000008008200000,
+ 0x0080088008200020, 0x0080088008000000, 0x0000008008200000, 0x0080080000200020,
+ 0x0000000000200000, 0x0080080000000020, 0x0000008008000020, 0x0000000000000000,
+ 0x0000008008000000, 0x0000000000200000, 0x0080080000200020, 0x0080088008200000,
+ 0x0000000000000020, 0x0000008008200020, 0x0080080000000000, 0x0080088008000020, ),
+ ( 0x1000800810004004, 0x0000000000000000, 0x0000800810000000, 0x0000000010004004,
+ 0x1000000000004004, 0x1000800800000000, 0x0000800800004004, 0x0000800810000000,
+ 0x0000800800000000, 0x1000000010004004, 0x1000000000000000, 0x0000800800004004,
+ 0x1000000010000000, 0x0000800810004004, 0x0000000010004004, 0x1000000000000000,
+ 0x0000000010000000, 0x1000800800004004, 0x1000000010004004, 0x0000800800000000,
+ 0x1000800810000000, 0x0000000000004004, 0x0000000000000000, 0x1000000010000000,
+ 0x1000800800004004, 0x1000800810000000, 0x0000800810004004, 0x1000000000004004,
+ 0x0000000000004004, 0x0000000010000000, 0x1000800800000000, 0x1000800810004004,
+ 0x1000000010000000, 0x0000800810004004, 0x0000800800004004, 0x1000800810000000,
+ 0x1000800810004004, 0x1000000010000000, 0x1000000000004004, 0x0000000000000000,
+ 0x0000000000004004, 0x1000800800000000, 0x0000000010000000, 0x1000000010004004,
+ 0x0000800800000000, 0x0000000000004004, 0x1000800810000000, 0x1000800800004004,
+ 0x0000800810004004, 0x0000800800000000, 0x0000000000000000, 0x1000000000004004,
+ 0x1000000000000000, 0x1000800810004004, 0x0000800810000000, 0x0000000010004004,
+ 0x1000000010004004, 0x0000000010000000, 0x1000800800000000, 0x0000800800004004,
+ 0x1000800800004004, 0x1000000000000000, 0x0000000010004004, 0x0000800810000000, ),
+ ( 0x0000000000400410, 0x0010004004400400, 0x0010000000000000, 0x0010000000400410,
+ 0x0000004004000010, 0x0000000000400400, 0x0010000000400410, 0x0010004004000000,
+ 0x0010000000400400, 0x0000004004000000, 0x0000004004400400, 0x0000000000000010,
+ 0x0010004004400410, 0x0010000000000010, 0x0000000000000010, 0x0000004004400410,
+ 0x0000000000000000, 0x0000004004000010, 0x0010004004400400, 0x0010000000000000,
+ 0x0010000000000010, 0x0010004004400410, 0x0000004004000000, 0x0000000000400410,
+ 0x0000004004400410, 0x0010000000400400, 0x0010004004000010, 0x0000004004400400,
+ 0x0010004004000000, 0x0000000000000000, 0x0000000000400400, 0x0010004004000010,
+ 0x0010004004400400, 0x0010000000000000, 0x0000000000000010, 0x0000004004000000,
+ 0x0010000000000010, 0x0000004004000010, 0x0000004004400400, 0x0010000000400410,
+ 0x0000000000000000, 0x0010004004400400, 0x0010004004000000, 0x0000004004400410,
+ 0x0000004004000010, 0x0000000000400400, 0x0010004004400410, 0x0000000000000010,
+ 0x0010004004000010, 0x0000000000400410, 0x0000000000400400, 0x0010004004400410,
+ 0x0000004004000000, 0x0010000000400400, 0x0010000000400410, 0x0010004004000000,
+ 0x0010000000400400, 0x0000000000000000, 0x0000004004400410, 0x0010000000000010,
+ 0x0000000000400410, 0x0010004004000010, 0x0010000000000000, 0x0000004004400400, ),
+ ( 0x0800100040040080, 0x0000100000001000, 0x0800000000000080, 0x0800100040041080,
+ 0x0000000000000000, 0x0000000040041000, 0x0800100000001080, 0x0800000040040080,
+ 0x0000100040041000, 0x0800000000001080, 0x0000000000001000, 0x0800100000000080,
+ 0x0800000000001080, 0x0800100040040080, 0x0000000040040000, 0x0000000000001000,
+ 0x0800000040041080, 0x0000100040040000, 0x0000100000000000, 0x0800000000000080,
+ 0x0000100040040000, 0x0800100000001080, 0x0000000040041000, 0x0000100000000000,
+ 0x0800100000000080, 0x0000000000000000, 0x0800000040040080, 0x0000100040041000,
+ 0x0000100000001000, 0x0800000040041080, 0x0800100040041080, 0x0000000040040000,
+ 0x0800000040041080, 0x0800100000000080, 0x0000000040040000, 0x0800000000001080,
+ 0x0000100040040000, 0x0000100000001000, 0x0800000000000080, 0x0000000040041000,
+ 0x0800100000001080, 0x0000000000000000, 0x0000100000000000, 0x0800000040040080,
+ 0x0000000000000000, 0x0800000040041080, 0x0000100040041000, 0x0000100000000000,
+ 0x0000000000001000, 0x0800100040041080, 0x0800100040040080, 0x0000000040040000,
+ 0x0800100040041080, 0x0800000000000080, 0x0000100000001000, 0x0800100040040080,
+ 0x0800000040040080, 0x0000100040040000, 0x0000000040041000, 0x0800100000001080,
+ 0x0800100000000080, 0x0000000000001000, 0x0800000000001080, 0x0000100040041000, ),
+ ( 0x0000000000800800, 0x0000001000000000, 0x0040040000000000, 0x2040041000800800,
+ 0x2000001000800800, 0x0040040000800800, 0x2040041000000000, 0x0000001000800800,
+ 0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040041000000000,
+ 0x2040040000800800, 0x2000001000800800, 0x0040041000800800, 0x0000000000000000,
+ 0x0040041000000000, 0x0000000000800800, 0x2000001000000000, 0x2040040000000000,
+ 0x0040040000800800, 0x2040041000000000, 0x0000000000000000, 0x2000000000800800,
+ 0x2000000000000000, 0x2040040000800800, 0x2040041000800800, 0x2000001000000000,
+ 0x0000001000800800, 0x0040040000000000, 0x2040040000000000, 0x0040041000800800,
+ 0x0040041000800800, 0x2040040000800800, 0x2000001000000000, 0x0000001000800800,
+ 0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040040000800800,
+ 0x0000000000800800, 0x0040041000000000, 0x2040041000800800, 0x0000000000000000,
+ 0x2040041000000000, 0x0000000000800800, 0x0040040000000000, 0x2000001000000000,
+ 0x2040040000800800, 0x0040040000000000, 0x0000000000000000, 0x2040041000800800,
+ 0x2000001000800800, 0x0040041000800800, 0x2040040000000000, 0x0000001000000000,
+ 0x0040041000000000, 0x2000001000800800, 0x0040040000800800, 0x2040040000000000,
+ 0x2000000000000000, 0x2040041000000000, 0x0000001000800800, 0x2000000000800800, ),
+ ( 0x4004000000008008, 0x4004000020000000, 0x0000000000000000, 0x0000200020008008,
+ 0x4004000020000000, 0x0000200000000000, 0x4004200000008008, 0x0000000020000000,
+ 0x4004200000000000, 0x4004200020008008, 0x0000200020000000, 0x0000000000008008,
+ 0x0000200000008008, 0x4004000000008008, 0x0000000020008008, 0x4004200020000000,
+ 0x0000000020000000, 0x4004200000008008, 0x4004000020008008, 0x0000000000000000,
+ 0x0000200000000000, 0x4004000000000000, 0x0000200020008008, 0x4004000020008008,
+ 0x4004200020008008, 0x0000000020008008, 0x0000000000008008, 0x4004200000000000,
+ 0x4004000000000000, 0x0000200020000000, 0x4004200020000000, 0x0000200000008008,
+ 0x4004200000000000, 0x0000000000008008, 0x0000200000008008, 0x4004200020000000,
+ 0x0000200020008008, 0x4004000020000000, 0x0000000000000000, 0x0000200000008008,
+ 0x0000000000008008, 0x0000200000000000, 0x4004000020008008, 0x0000000020000000,
+ 0x4004000020000000, 0x4004200020008008, 0x0000200020000000, 0x4004000000000000,
+ 0x4004200020008008, 0x0000200020000000, 0x0000000020000000, 0x4004200000008008,
+ 0x4004000000008008, 0x0000000020008008, 0x4004200020000000, 0x0000000000000000,
+ 0x0000200000000000, 0x4004000000008008, 0x4004200000008008, 0x0000200020008008,
+ 0x0000000020008008, 0x4004200000000000, 0x4004000000000000, 0x4004000020008008, ),
+ ( 0x0000400400000000, 0x0020000000000000, 0x0020000000100000, 0x0400000000100040,
+ 0x0420400400100040, 0x0400400400000040, 0x0020400400000000, 0x0000000000000000,
+ 0x0000000000100000, 0x0420000000100040, 0x0420000000000040, 0x0000400400100000,
+ 0x0400000000000040, 0x0020400400100000, 0x0000400400100000, 0x0420000000000040,
+ 0x0420000000100040, 0x0000400400000000, 0x0400400400000040, 0x0420400400100040,
+ 0x0000000000000000, 0x0020000000100000, 0x0400000000100040, 0x0020400400000000,
+ 0x0400400400100040, 0x0420400400000040, 0x0020400400100000, 0x0400000000000040,
+ 0x0420400400000040, 0x0400400400100040, 0x0020000000000000, 0x0000000000100000,
+ 0x0420400400000040, 0x0000400400100000, 0x0400400400100040, 0x0420000000000040,
+ 0x0000400400000000, 0x0020000000000000, 0x0000000000100000, 0x0400400400100040,
+ 0x0420000000100040, 0x0420400400000040, 0x0020400400000000, 0x0000000000000000,
+ 0x0020000000000000, 0x0400000000100040, 0x0400000000000040, 0x0020000000100000,
+ 0x0000000000000000, 0x0420000000100040, 0x0020000000100000, 0x0020400400000000,
+ 0x0420000000000040, 0x0000400400000000, 0x0420400400100040, 0x0000000000100000,
+ 0x0020400400100000, 0x0400000000000040, 0x0400400400000040, 0x0420400400100040,
+ 0x0400000000100040, 0x0020400400100000, 0x0000400400100000, 0x0400400400000040, ),
+ ( 0x8008000080082000, 0x0000002080082000, 0x8008002000000000, 0x0000000000000000,
+ 0x0000002000002000, 0x8008000080080000, 0x0000000080082000, 0x8008002080082000,
+ 0x8008000000000000, 0x0000000000002000, 0x0000002080080000, 0x8008002000000000,
+ 0x8008002080080000, 0x8008002000002000, 0x8008000000002000, 0x0000000080082000,
+ 0x0000002000000000, 0x8008002080080000, 0x8008000080080000, 0x0000002000002000,
+ 0x8008002080082000, 0x8008000000002000, 0x0000000000000000, 0x0000002080080000,
+ 0x0000000000002000, 0x0000000080080000, 0x8008002000002000, 0x8008000080082000,
+ 0x0000000080080000, 0x0000002000000000, 0x0000002080082000, 0x8008000000000000,
+ 0x0000000080080000, 0x0000002000000000, 0x8008000000002000, 0x8008002080082000,
+ 0x8008002000000000, 0x0000000000002000, 0x0000000000000000, 0x0000002080080000,
+ 0x8008000080082000, 0x8008002000002000, 0x0000002000002000, 0x8008000080080000,
+ 0x0000002080082000, 0x8008000000000000, 0x8008000080080000, 0x0000002000002000,
+ 0x8008002080082000, 0x0000000080080000, 0x0000000080082000, 0x8008000000002000,
+ 0x0000002080080000, 0x8008002000000000, 0x8008002000002000, 0x0000000080082000,
+ 0x8008000000000000, 0x0000002080082000, 0x8008002080080000, 0x0000000000000000,
+ 0x0000000000002000, 0x8008000080082000, 0x0000002000000000, 0x8008002080080000, ),
+ )
+
+ #---------------------------------------------------
+ # compressed/interleaved => final permutation table
+ # Compression, final permutation, bit reverse
+ #---------------------------------------------------
+ #NOTE: this was reordered from original table to make perm6464 logic simpler
+ CF6464=(
+ ( 0x0000000000000000, 0x0000002000000000, 0x0000200000000000, 0x0000202000000000,
+ 0x0020000000000000, 0x0020002000000000, 0x0020200000000000, 0x0020202000000000,
+ 0x2000000000000000, 0x2000002000000000, 0x2000200000000000, 0x2000202000000000,
+ 0x2020000000000000, 0x2020002000000000, 0x2020200000000000, 0x2020202000000000, ),
+ ( 0x0000000000000000, 0x0000000200000000, 0x0000020000000000, 0x0000020200000000,
+ 0x0002000000000000, 0x0002000200000000, 0x0002020000000000, 0x0002020200000000,
+ 0x0200000000000000, 0x0200000200000000, 0x0200020000000000, 0x0200020200000000,
+ 0x0202000000000000, 0x0202000200000000, 0x0202020000000000, 0x0202020200000000, ),
+ ( 0x0000000000000000, 0x0000000000000020, 0x0000000000002000, 0x0000000000002020,
+ 0x0000000000200000, 0x0000000000200020, 0x0000000000202000, 0x0000000000202020,
+ 0x0000000020000000, 0x0000000020000020, 0x0000000020002000, 0x0000000020002020,
+ 0x0000000020200000, 0x0000000020200020, 0x0000000020202000, 0x0000000020202020, ),
+ ( 0x0000000000000000, 0x0000000000000002, 0x0000000000000200, 0x0000000000000202,
+ 0x0000000000020000, 0x0000000000020002, 0x0000000000020200, 0x0000000000020202,
+ 0x0000000002000000, 0x0000000002000002, 0x0000000002000200, 0x0000000002000202,
+ 0x0000000002020000, 0x0000000002020002, 0x0000000002020200, 0x0000000002020202, ),
+ ( 0x0000000000000000, 0x0000008000000000, 0x0000800000000000, 0x0000808000000000,
+ 0x0080000000000000, 0x0080008000000000, 0x0080800000000000, 0x0080808000000000,
+ 0x8000000000000000, 0x8000008000000000, 0x8000800000000000, 0x8000808000000000,
+ 0x8080000000000000, 0x8080008000000000, 0x8080800000000000, 0x8080808000000000, ),
+ ( 0x0000000000000000, 0x0000000800000000, 0x0000080000000000, 0x0000080800000000,
+ 0x0008000000000000, 0x0008000800000000, 0x0008080000000000, 0x0008080800000000,
+ 0x0800000000000000, 0x0800000800000000, 0x0800080000000000, 0x0800080800000000,
+ 0x0808000000000000, 0x0808000800000000, 0x0808080000000000, 0x0808080800000000, ),
+ ( 0x0000000000000000, 0x0000000000000080, 0x0000000000008000, 0x0000000000008080,
+ 0x0000000000800000, 0x0000000000800080, 0x0000000000808000, 0x0000000000808080,
+ 0x0000000080000000, 0x0000000080000080, 0x0000000080008000, 0x0000000080008080,
+ 0x0000000080800000, 0x0000000080800080, 0x0000000080808000, 0x0000000080808080, ),
+ ( 0x0000000000000000, 0x0000000000000008, 0x0000000000000800, 0x0000000000000808,
+ 0x0000000000080000, 0x0000000000080008, 0x0000000000080800, 0x0000000000080808,
+ 0x0000000008000000, 0x0000000008000008, 0x0000000008000800, 0x0000000008000808,
+ 0x0000000008080000, 0x0000000008080008, 0x0000000008080800, 0x0000000008080808, ),
+ ( 0x0000000000000000, 0x0000001000000000, 0x0000100000000000, 0x0000101000000000,
+ 0x0010000000000000, 0x0010001000000000, 0x0010100000000000, 0x0010101000000000,
+ 0x1000000000000000, 0x1000001000000000, 0x1000100000000000, 0x1000101000000000,
+ 0x1010000000000000, 0x1010001000000000, 0x1010100000000000, 0x1010101000000000, ),
+ ( 0x0000000000000000, 0x0000000100000000, 0x0000010000000000, 0x0000010100000000,
+ 0x0001000000000000, 0x0001000100000000, 0x0001010000000000, 0x0001010100000000,
+ 0x0100000000000000, 0x0100000100000000, 0x0100010000000000, 0x0100010100000000,
+ 0x0101000000000000, 0x0101000100000000, 0x0101010000000000, 0x0101010100000000, ),
+ ( 0x0000000000000000, 0x0000000000000010, 0x0000000000001000, 0x0000000000001010,
+ 0x0000000000100000, 0x0000000000100010, 0x0000000000101000, 0x0000000000101010,
+ 0x0000000010000000, 0x0000000010000010, 0x0000000010001000, 0x0000000010001010,
+ 0x0000000010100000, 0x0000000010100010, 0x0000000010101000, 0x0000000010101010, ),
+ ( 0x0000000000000000, 0x0000000000000001, 0x0000000000000100, 0x0000000000000101,
+ 0x0000000000010000, 0x0000000000010001, 0x0000000000010100, 0x0000000000010101,
+ 0x0000000001000000, 0x0000000001000001, 0x0000000001000100, 0x0000000001000101,
+ 0x0000000001010000, 0x0000000001010001, 0x0000000001010100, 0x0000000001010101, ),
+ ( 0x0000000000000000, 0x0000004000000000, 0x0000400000000000, 0x0000404000000000,
+ 0x0040000000000000, 0x0040004000000000, 0x0040400000000000, 0x0040404000000000,
+ 0x4000000000000000, 0x4000004000000000, 0x4000400000000000, 0x4000404000000000,
+ 0x4040000000000000, 0x4040004000000000, 0x4040400000000000, 0x4040404000000000, ),
+ ( 0x0000000000000000, 0x0000000400000000, 0x0000040000000000, 0x0000040400000000,
+ 0x0004000000000000, 0x0004000400000000, 0x0004040000000000, 0x0004040400000000,
+ 0x0400000000000000, 0x0400000400000000, 0x0400040000000000, 0x0400040400000000,
+ 0x0404000000000000, 0x0404000400000000, 0x0404040000000000, 0x0404040400000000, ),
+ ( 0x0000000000000000, 0x0000000000000040, 0x0000000000004000, 0x0000000000004040,
+ 0x0000000000400000, 0x0000000000400040, 0x0000000000404000, 0x0000000000404040,
+ 0x0000000040000000, 0x0000000040000040, 0x0000000040004000, 0x0000000040004040,
+ 0x0000000040400000, 0x0000000040400040, 0x0000000040404000, 0x0000000040404040, ),
+ ( 0x0000000000000000, 0x0000000000000004, 0x0000000000000400, 0x0000000000000404,
+ 0x0000000000040000, 0x0000000000040004, 0x0000000000040400, 0x0000000000040404,
+ 0x0000000004000000, 0x0000000004000004, 0x0000000004000400, 0x0000000004000404,
+ 0x0000000004040000, 0x0000000004040004, 0x0000000004040400, 0x0000000004040404, ),
+ )
+ #=========================================================
+ #eof load_data
+ #=========================================================
+
+def permute(c, p):
+ """Returns the permutation of the given 32-bit or 64-bit code with
+ the specified permutation table."""
+ #NOTE: only difference between 32 & 64 bit permutations
+ #is that len(p)==8 for 32 bit, and len(p)==16 for 64 bit.
+ out = 0
+ for r in p:
+ out |= r[c&0xf]
+ c >>= 4
+ return out
+
+#=========================================================
+#des frontend
+#=========================================================
+def expand_des_key(source):
+ "convert 7 byte des key to 8 byte des key (by adding parity bit every 7 bits)"
+ #NOTE: could probably do this much more cleverly and efficiently,
+ # but no need really given it's use
+ assert len(source) == 7
+
+ def iter_bits(source):
+ for c in source:
+ v = ord(c)
+ for i in xrange(7,-1,-1):
+ yield (v>>i) & 1
+
+ out = 0
+ p = 1
+ for i, b in enumerate(iter_bits(source)):
+ out = (out<<1) + b
+ p ^= b
+ if i % 7 == 6:
+ out = (out<<1) + p
+ p = 1
+
+ return ''.join(
+ chr((out>>s) & 0xFF)
+ for s in xrange(8*7,-8,-8)
+ )
+
+def des_encrypt_block(key, input):
+ """do traditional encryption of a single DES block
+
+ :arg key: 8 byte des key string
+ :arg input: 8 byte plaintext string
+ :returns: 8 byte ciphertext string
+ """
+ assert len(input) == 8
+ if len(key) == 7:
+ key = expand_des_key(key)
+ assert len(key) == 8
+ input = bytes_to_int(input)
+ key = bytes_to_int(key)
+ out = mdes_encrypt_int_block(key, input, 0, 1)
+ return int_to_bytes(out, 8)
+
+def mdes_encrypt_int_block(key, input, salt=0, rounds=1):
+ """do modified multi-round DES encryption of single DES block.
+
+ the function implemented the salted, variable-round version
+ of DES used by des-crypt and ext-des-crypt.
+
+ it also can perform regular DES encryption
+ by setting salt=0 and rounds=1
+
+ :arg key: 8 byte des key as integer
+ :arg input: 8 byte plaintext block as integer
+ :arg salt: 12 or 24 bit salt as integer, used to mutate output
+ :arg rounds: number of rounds of DES encryption to apply.
+
+ :returns:
+ resulting block as 8 byte integer
+ """
+ global SPE, PCXROT, IE3264, CF6464
+
+ #bounds check
+ assert 0 <= input <= INT_64_MAX, "input value out of range"
+ assert 0 <= salt <= INT_24_MAX, "salt value out of range"
+ assert rounds >= 0, "rounds out of range"
+ assert 0 <= key <= INT_64_MAX, "key value out of range"
+
+ #load tables if not already done
+ if PCXROT is None:
+ load_tables()
+
+ #convert key int -> key schedule
+ #NOTE: generation was modified to output two elements at a time,
+ #to optimize for per-round algorithm below.
+ mask = ~0x0303030300000000
+ def _gen(K):
+ for p_even, p_odd in PCXROT:
+ K1 = permute(K, p_even)
+ K = permute(K1, p_odd)
+ yield K1 & mask, K & mask
+ ks_list = list(_gen(key))
+
+ #expand 24 bit salt -> 32 bit
+ salt = (
+ ((salt & 0x00003f) << 26) |
+ ((salt & 0x000fc0) << 12) |
+ ((salt & 0x03f000) >> 2) |
+ ((salt & 0xfc0000) >> 16)
+ )
+
+ #init L & R
+ if input == 0:
+ L = R = 0
+ else:
+ L = ((input >> 31) & 0xaaaaaaaa) | (input & 0x55555555)
+ L = permute(L, IE3264)
+
+ R = ((input >> 32) & 0xaaaaaaaa) | ((input >> 1) & 0x55555555)
+ R = permute(R, IE3264)
+
+ #load SPE into local vars to speed things up and remove an array access call
+ SPE0, SPE1, SPE2, SPE3, SPE4, SPE5, SPE6, SPE7 = SPE
+
+ #run specified number of passed
+ while rounds:
+ rounds -= 1
+
+ #run over each part of the schedule
+ for ks_even, ks_odd in ks_list:
+ k = ((R>>32) ^ R) & salt
+ B = (k<<32) ^ k ^ R ^ ks_even
+
+ L ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
+ SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^
+ SPE4[(B>>26)&0x3f] ^ SPE5[(B>>18)&0x3f] ^
+ SPE6[(B>>10)&0x3f] ^ SPE7[(B>>2)&0x3f])
+
+ k = ((L>>32) ^ L) & salt
+ B = (k<<32) ^ k ^ L ^ ks_odd
+
+ R ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
+ SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^
+ SPE4[(B>>26)&0x3f] ^ SPE5[(B>>18)&0x3f] ^
+ SPE6[(B>>10)&0x3f] ^ SPE7[(B>>2)&0x3f])
+
+ # swap L and R
+ L, R = R, L
+
+ C = (
+ ((L>>3) & 0x0f0f0f0f00000000L)
+ |
+ ((L<<33) & 0xf0f0f0f000000000L)
+ |
+ ((R>>35) & 0x000000000f0f0f0fL)
+ |
+ ((R<<1) & 0x00000000f0f0f0f0L)
+ )
+
+ C = permute(C, CF6464)
+
+ return C
+
+#=========================================================
+#eof
+#=========================================================
diff --git a/passlib/utils/md4.py b/passlib/utils/md4.py
index 2eb2c95..550eff8 100644
--- a/passlib/utils/md4.py
+++ b/passlib/utils/md4.py
@@ -120,17 +120,17 @@ class md4(object):
orig = self._state
state = list(orig)
- #round 1
+ #round 1 - F function - (x&y)|(~x & z)
for a,b,c,d,k,s in self._round1:
t = (state[a] + F(state[b],state[c],state[d]) + X[k]) & MASK_32
state[a] = ((t<<s) & MASK_32) + (t>>(32-s))
- #round 2
+ #round 2 - G function
for a,b,c,d,k,s in self._round2:
t = (state[a] + G(state[b],state[c],state[d]) + X[k] + 0x5a827999) & MASK_32
state[a] = ((t<<s) & MASK_32) + (t>>(32-s))
- #round 3
+ #round 3 - H function - x ^ y ^ z
for a,b,c,d,k,s in self._round3:
t = (state[a] + (state[b] ^ state[c] ^ state[d]) + X[k] + 0x6ed9eba1) & MASK_32
state[a] = ((t<<s) & MASK_32) + (t>>(32-s))