summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-01-21 05:21:14 +0000
committerEli Collins <elic@assurancetechnologies.com>2011-01-21 05:21:14 +0000
commitcad5d17c97ffa7d447adc1aa307299775e620f5d (patch)
tree1bc6357d61ea05abd5c330392d0ab1bffc1ce264
parent2f9b403bf457d9694cdaa71b0744fe5a8446a95a (diff)
downloadpasslib-cad5d17c97ffa7d447adc1aa307299775e620f5d.tar.gz
_slow_des_crypt: reordered IE3264 table to make perm3264() implementation simpler
-rw-r--r--passlib/utils/_slow_des_crypt.py105
1 files changed, 57 insertions, 48 deletions
diff --git a/passlib/utils/_slow_des_crypt.py b/passlib/utils/_slow_des_crypt.py
index 3348dda..4246ad4 100644
--- a/passlib/utils/_slow_des_crypt.py
+++ b/passlib/utils/_slow_des_crypt.py
@@ -113,7 +113,9 @@ RR6_S2 = range(6, -1, -2)
RR14_S2 = range(14, -1, -2)
R16_S2 = range(0, 16, 2)
-INT_64_MAX = 0xffffffffffffffffL # 2**64-1
+INT_24_MAX = 0xffffffL
+INT_64_MAX = 0xffffffffL
+INT_64_MAX = 0xffffffffffffffffL
#=========================================================
# static tables for des
@@ -346,31 +348,8 @@ def load_tables():
# Bit reverse, intial permupation, expantion
# Initial permutation/expansion table
#---------------------------------------------------
+ #NOTE: this was reordered from original table to make perm3264 logic simpler
IE3264=(
- ( 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, ),
- ( 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, 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, 0x0000000000800800, 0x0000000000008008, 0x0000000000808808,
0x0000008008000000, 0x0000008008800800, 0x0000008008008008, 0x0000008008808808,
0x0000000080080000, 0x0000000080880800, 0x0000000080088008, 0x0000000080888808,
@@ -379,7 +358,32 @@ def load_tables():
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.
#---------------------------------------------------
@@ -513,6 +517,7 @@ def load_tables():
0x8008000000000000, 0x0000002080082000, 0x8008002080080000, 0x0000000000000000,
0x0000000000002000, 0x8008000080082000, 0x0000002000000000, 0x8008002080080000, ),
)
+
#---------------------------------------------------
# compressed/interleaved => final permutation table
# Compression, final permutation, bit reverse
@@ -603,9 +608,9 @@ def perm3264(c, p):
"""Returns the permutation of the given 32-bit code with
the specified permutataion table."""
out = 0
- for i in RR6_S2:
- out |= p[i][c&0x0f] | p[i+1][(c>>4)&0x0f]
- c >>= 8
+ for r in p:
+ out |= r[c&0xf]
+ c >>= 4
return out
def des_setkey(keyword):
@@ -617,34 +622,38 @@ def des_setkey(keyword):
yield K & ~0x0303030300000000
return list(_gen(keyword))
-def to_six_bit_int(num):
- return (
- ((num & 0x00003f) << 26) |
- ((num & 0x000fc0) << 12) |
- ((num & 0x03f000) >> 2) |
- ((num & 0xfc0000) >> 16)
- )
-
def des_cipher(input, salt, rounds, KS):
"""Returns the DES encrypted code of the given word with the specified environment."""
+ #bounds check
assert rounds >= 0
assert 0 <= input <= INT_64_MAX, "input value out of range"
+ assert 0 <= salt <= INT_24_MAX, "salt value out of range"
+
+ #expand 24 bit salt -> 32 bit
+ salt = (
+ ((salt & 0x00003f) << 26) |
+ ((salt & 0x000fc0) << 12) |
+ ((salt & 0x03f000) >> 2) |
+ ((salt & 0xfc0000) >> 16)
+ )
- salt = to_six_bit_int(salt) & 0xffffffff
-
- L = ((input >> 31) & 0xaaaaaaaa) | (input & 0x55555555)
- L = perm3264(L, IE3264)
+ #init L & R
+ if input == 0:
+ L = R = 0
+ else:
+ L = ((input >> 31) & 0xaaaaaaaa) | (input & 0x55555555)
+ L = perm3264(L, IE3264)
- R = ((input >> 32) & 0xaaaaaaaa) | ((input >> 1) & 0x55555555)
- R = perm3264(R, IE3264)
+ R = ((input >> 32) & 0xaaaaaaaa) | ((input >> 1) & 0x55555555)
+ R = perm3264(R, IE3264)
- #pre-calc kp list
- kp_list = [
+ #pre-calc ks iterator
+ ks_list = [
(KS[offset], KS[offset+1])
for offset in R16_S2
]
- #load SPE into locals
+ #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
@@ -652,9 +661,9 @@ def des_cipher(input, salt, rounds, KS):
rounds -= 1
#run over each part of the schedule
- for kp_even, kp_odd in kp_list:
+ for ks_even, ks_odd in ks_list:
k = ((R>>32) ^ R) & salt
- B = (k<<32) ^ k ^ R ^ kp_even
+ B = (k<<32) ^ k ^ R ^ ks_even
L ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^
@@ -662,7 +671,7 @@ def des_cipher(input, salt, rounds, KS):
SPE6[(B>>10)&0x3f] ^ SPE7[(B>>2)&0x3f])
k = ((L>>32) ^ L) & salt
- B = (k<<32) ^ k ^ L ^ kp_odd
+ B = (k<<32) ^ k ^ L ^ ks_odd
R ^= (SPE0[(B>>58)&0x3f] ^ SPE1[(B>>50)&0x3f] ^
SPE2[(B>>42)&0x3f] ^ SPE3[(B>>34)&0x3f] ^