Commit b0357fc7 authored by gerd's avatar gerd

The 'xperm' permutation is done by bit-shifting instead of

an array lookup.


git-svn-id: https://godirepo.camlcity.org/svn/lib-cryptgps/trunk@5 b101cce4-44db-0310-b718-db4b2d8d2e05
parent f7cb270f
(* $Id: crypt_des.ml,v 1.3 1999/06/17 15:57:02 gerd Exp $ (* $Id: crypt_des.ml,v 1.4 1999/06/17 16:51:33 gerd Exp $
* ---------------------------------------------------------------------- * ----------------------------------------------------------------------
* *
*) *)
...@@ -375,19 +375,30 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -375,19 +375,30 @@ module Cryptsystem : Cryptsystem_64.T =
(* an S-box is an array with 64 numbers from 0 to 15 *) (* an S-box is an array with 64 numbers from 0 to 15 *)
let mk_sbox shift a = let mk_sbox p32 shift a =
(* transform the S-box notation found in the literature to a lookup table *) (* transform the S-box notation found in the literature to a lookup table *)
let a' = Array.create 64 0 in (* p32: a 32-bit permutation which should be applied immediately
* shift: either 0,4,8,12,16,20,24, or 28.
*)
let a' = Array.create 64 (0,0) in
for k' = 0 to 63 do for k' = 0 to 63 do
let k = ((k' land 0x1e) lsr 1) lor (k' land 0x20) lor ((k' land 1) lsl 4) in let k = ((k' land 0x1e) lsr 1) lor (k' land 0x20) lor ((k' land 1) lsl 4) in
a'.( k' ) <- a.(k) lsl shift let x = a.(k) in
let x' =
if shift < 16 then
(0, x lsl shift)
else
(x lsl (shift-16), 0)
in
a'.( k' ) <- do_perm32 p32 x'
done; done;
a' a'
let sbox1 = let sbox1 =
lazy lazy
(mk_sbox 12 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 28
[| 14; 4; 13; 1; 2; 15; 11; 8; 3; 10; 6; 12; 5; 9; 0; 7; [| 14; 4; 13; 1; 2; 15; 11; 8; 3; 10; 6; 12; 5; 9; 0; 7;
0; 15; 7; 4; 14; 2; 13; 1; 10; 6; 12; 11; 9; 5; 3; 8; 0; 15; 7; 4; 14; 2; 13; 1; 10; 6; 12; 11; 9; 5; 3; 8;
4; 1; 14; 8; 13; 6; 2; 11; 15; 12; 9; 7; 3; 10; 5; 0; 4; 1; 14; 8; 13; 6; 2; 11; 15; 12; 9; 7; 3; 10; 5; 0;
...@@ -397,7 +408,8 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -397,7 +408,8 @@ module Cryptsystem : Cryptsystem_64.T =
let sbox2 = let sbox2 =
lazy lazy
(mk_sbox 8 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 24
[| 15; 1; 8; 14; 6; 11; 3; 4; 9; 7; 2; 13; 12; 0; 5; 10; [| 15; 1; 8; 14; 6; 11; 3; 4; 9; 7; 2; 13; 12; 0; 5; 10;
3; 13; 4; 7; 15; 2; 8; 14; 12; 0; 1; 10; 6; 9; 11; 5; 3; 13; 4; 7; 15; 2; 8; 14; 12; 0; 1; 10; 6; 9; 11; 5;
0; 14; 7; 11; 10; 4; 13; 1; 5; 8; 12; 6; 9; 3; 2; 15; 0; 14; 7; 11; 10; 4; 13; 1; 5; 8; 12; 6; 9; 3; 2; 15;
...@@ -407,7 +419,8 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -407,7 +419,8 @@ module Cryptsystem : Cryptsystem_64.T =
let sbox3 = let sbox3 =
lazy lazy
(mk_sbox 4 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 20
[| 10; 0; 9; 14; 6; 3; 15; 5; 1; 13; 12; 7; 11; 4; 2; 8; [| 10; 0; 9; 14; 6; 3; 15; 5; 1; 13; 12; 7; 11; 4; 2; 8;
13; 7; 0; 9; 3; 4; 6; 10; 2; 8; 5; 14; 12; 11; 15; 1; 13; 7; 0; 9; 3; 4; 6; 10; 2; 8; 5; 14; 12; 11; 15; 1;
13; 6; 4; 9; 8; 15; 3; 0; 11; 1; 2; 12; 5; 10; 14; 7; 13; 6; 4; 9; 8; 15; 3; 0; 11; 1; 2; 12; 5; 10; 14; 7;
...@@ -417,7 +430,8 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -417,7 +430,8 @@ module Cryptsystem : Cryptsystem_64.T =
let sbox4 = let sbox4 =
lazy lazy
(mk_sbox 0 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 16
[| 7; 13; 14; 3; 0; 6; 9; 10; 1; 2; 8; 5; 11; 12; 4; 15; [| 7; 13; 14; 3; 0; 6; 9; 10; 1; 2; 8; 5; 11; 12; 4; 15;
13; 8; 11; 5; 6; 15; 0; 3; 4; 7; 2; 12; 1; 10; 14; 9; 13; 8; 11; 5; 6; 15; 0; 3; 4; 7; 2; 12; 1; 10; 14; 9;
10; 6; 9; 0; 12; 11; 7; 13; 15; 1; 3; 14; 5; 2; 8; 4; 10; 6; 9; 0; 12; 11; 7; 13; 15; 1; 3; 14; 5; 2; 8; 4;
...@@ -427,7 +441,8 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -427,7 +441,8 @@ module Cryptsystem : Cryptsystem_64.T =
let sbox5 = let sbox5 =
lazy lazy
(mk_sbox 12 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 12
[| 2; 12; 4; 1; 7; 10; 11; 6; 8; 5; 3; 15; 13; 0; 14; 9; [| 2; 12; 4; 1; 7; 10; 11; 6; 8; 5; 3; 15; 13; 0; 14; 9;
14; 11; 2; 12; 4; 7; 13; 1; 5; 0; 15; 10; 3; 9; 8; 6; 14; 11; 2; 12; 4; 7; 13; 1; 5; 0; 15; 10; 3; 9; 8; 6;
4; 2; 1; 11; 10; 13; 7; 8; 15; 9; 12; 5; 6; 3; 0; 14; 4; 2; 1; 11; 10; 13; 7; 8; 15; 9; 12; 5; 6; 3; 0; 14;
...@@ -437,7 +452,8 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -437,7 +452,8 @@ module Cryptsystem : Cryptsystem_64.T =
let sbox6 = let sbox6 =
lazy lazy
(mk_sbox 8 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 8
[| 12; 1; 10; 15; 9; 2; 6; 8; 0; 13; 3; 4; 14; 7; 5; 11; [| 12; 1; 10; 15; 9; 2; 6; 8; 0; 13; 3; 4; 14; 7; 5; 11;
10; 15; 4; 2; 7; 12; 9; 5; 6; 1; 13; 14; 0; 11; 3; 8; 10; 15; 4; 2; 7; 12; 9; 5; 6; 1; 13; 14; 0; 11; 3; 8;
9; 14; 15; 5; 2; 8; 12; 3; 7; 0; 4; 10; 1; 13; 11; 6; 9; 14; 15; 5; 2; 8; 12; 3; 7; 0; 4; 10; 1; 13; 11; 6;
...@@ -447,7 +463,8 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -447,7 +463,8 @@ module Cryptsystem : Cryptsystem_64.T =
let sbox7 = let sbox7 =
lazy lazy
(mk_sbox 4 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 4
[| 4; 11; 2; 14; 15; 0; 8; 13; 3; 12; 9; 7; 5; 10; 6; 1; [| 4; 11; 2; 14; 15; 0; 8; 13; 3; 12; 9; 7; 5; 10; 6; 1;
13; 0; 11; 7; 4; 9; 1; 10; 14; 3; 5; 12; 2; 15; 8; 6; 13; 0; 11; 7; 4; 9; 1; 10; 14; 3; 5; 12; 2; 15; 8; 6;
1; 4; 11; 13; 12; 3; 7; 14; 10; 15; 6; 8; 0; 5; 9; 2; 1; 4; 11; 13; 12; 3; 7; 14; 10; 15; 6; 8; 0; 5; 9; 2;
...@@ -457,7 +474,8 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -457,7 +474,8 @@ module Cryptsystem : Cryptsystem_64.T =
let sbox8 = let sbox8 =
lazy lazy
(mk_sbox 0 (let pboxperm = Lazy.force des_pboxperm in
mk_sbox pboxperm 0
[| 13; 2; 8; 4; 6; 15; 11; 1; 10; 9; 3; 14; 5; 0; 12; 7; [| 13; 2; 8; 4; 6; 15; 11; 1; 10; 9; 3; 14; 5; 0; 12; 7;
1; 15; 13; 8; 10; 3; 7; 4; 12; 5; 6; 11; 0; 14; 9; 2; 1; 15; 13; 8; 10; 3; 7; 4; 12; 5; 6; 11; 0; 14; 9; 2;
7; 11; 4; 1; 9; 12; 14; 2; 0; 6; 10; 13; 15; 3; 5; 8; 7; 11; 4; 1; 9; 12; 14; 2; 0; 6; 10; 13; 15; 3; 5; 8;
...@@ -505,14 +523,21 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -505,14 +523,21 @@ module Cryptsystem : Cryptsystem_64.T =
let l32_1 = blast1 in let l32_1 = blast1 in
let r32_0 = last0 in let r32_0 = last0 in
let r32_1 = last1 in let r32_1 = last1 in
(* --------- manually inlined code ---------- *) (* ---------------- begin -------------------- *)
(* OLD: let (r48_0, r48_1) = do_perm48 xperm (r32_0,r32_1) in *) (* OLD: let (r48_0, r48_1) = do_perm48 xperm (r32_0,r32_1) in *)
let (a0,b0) = xperm.( r32_0 lsr 16 ) in (* The permutation can be expressed by logical operations directly: *)
let (a1,b1) = xperm.( 256 + ((r32_0 lsr 8) land 0xff)) in let a0 = (r32_1 land 1) lsl 23 in
let (a2,b2) = xperm.( 512 + (r32_0 land 0xff) ) in let a1 = (r32_0 land 0xf800) lsl 7 in
let (a3,b3) = xperm.( 768 + ( r32_1 lsr 16 ) ) in let a2 = (r32_0 land 0x1f80) lsl 5 in
let (a4,b4) = xperm.( 1024 + ((r32_1 lsr 8) land 0xff) ) in let a3 = (r32_0 land 0x01f8) lsl 3 in
let (a5,b5) = xperm.( 1280 + (r32_1 land 0xff) ) in let a4 = (r32_0 land 0x001f) lsl 1 in
let a5 = r32_1 lsr 15 in
let b0 = (r32_0 land 1) lsl 23 in
let b1 = (r32_1 land 0xf800) lsl 7 in
let b2 = (r32_1 land 0x1f80) lsl 5 in
let b3 = (r32_1 land 0x01f8) lsl 3 in
let b4 = (r32_1 land 0x001f) lsl 1 in
let b5 = r32_0 lsr 15 in
let r48_0 = a0 lor a1 lor a2 lor a3 lor a4 lor a5 in let r48_0 = a0 lor a1 lor a2 lor a3 lor a4 lor a5 in
let r48_1 = b0 lor b1 lor b2 lor b3 lor b4 lor b5 in let r48_1 = b0 lor b1 lor b2 lor b3 lor b4 lor b5 in
(* ------------------ end -------------------- *) (* ------------------ end -------------------- *)
...@@ -520,25 +545,17 @@ module Cryptsystem : Cryptsystem_64.T = ...@@ -520,25 +545,17 @@ module Cryptsystem : Cryptsystem_64.T =
let r'48_0 = k48_0 lxor r48_0 in let r'48_0 = k48_0 lxor r48_0 in
let r'48_1 = k48_1 lxor r48_1 in let r'48_1 = k48_1 lxor r48_1 in
let x0 = s1.( r'48_0 lsr 18 ) in let x00,x01 = s1.( r'48_0 lsr 18 ) in
let x1 = s2.( (r'48_0 lsr 12) land 63 ) in let x10,x11 = s2.( (r'48_0 lsr 12) land 63 ) in
let x2 = s3.( (r'48_0 lsr 6) land 63 ) in let x20,x21 = s3.( (r'48_0 lsr 6) land 63 ) in
let x3 = s4.( r'48_0 land 63 ) in let x30,x31 = s4.( r'48_0 land 63 ) in
let x4 = s5.( r'48_1 lsr 18 ) in let x40,x41 = s5.( r'48_1 lsr 18 ) in
let x5 = s6.( (r'48_1 lsr 12) land 63 ) in let x50,x51 = s6.( (r'48_1 lsr 12) land 63 ) in
let x6 = s7.( (r'48_1 lsr 6) land 63 ) in let x60,x61 = s7.( (r'48_1 lsr 6) land 63 ) in
let x7 = s8.( r'48_1 land 63 ) in let x70,x71 = s8.( r'48_1 land 63 ) in
let y0 = x0 lor x1 lor x2 lor x3 in let p0 =x00 lor x10 lor x20 lor x30 lor x40 lor x50 lor x60 lor x70 in
let y1 = x4 lor x5 lor x6 lor x7 in let p1 =x01 lor x11 lor x21 lor x31 lor x41 lor x51 lor x61 lor x71 in
(* --------- manually inlined code ---------- *)
(* OLD: let p0,p1 = do_perm32 pboxperm (y0,y1) in *)
let (a0,b0) = pboxperm.( y0 lsr 8 ) in
let (a1,b1) = pboxperm.( 256 + (y0 land 0xff)) in
let (a2,b2) = pboxperm.( 512 + (y1 lsr 8) ) in
let (a3,b3) = pboxperm.( 768 + (y1 land 0xff)) in
let p0 = a0 lor a1 lor a2 lor a3 in
let p1 = b0 lor b1 lor b2 lor b3 in
(* ------------------ end -------------------- *)
do_rounds (i+1) last0 last1 (p0 lxor l32_0) (p1 lxor l32_1) do_rounds (i+1) last0 last1 (p0 lxor l32_0) (p1 lxor l32_1)
else else
(* --------- manually inlined code ---------- *) (* --------- manually inlined code ---------- *)
...@@ -756,6 +773,10 @@ module Cryptmodes = Cryptmodes_64.Make_modes(Cryptsystem) ...@@ -756,6 +773,10 @@ module Cryptmodes = Cryptmodes_64.Make_modes(Cryptsystem)
* history: * history:
* *
* $Log: crypt_des.ml,v $ * $Log: crypt_des.ml,v $
* Revision 1.4 1999/06/17 16:51:33 gerd
* The 'xperm' permutation is done by bit-shifting instead of
* an array lookup.
*
* Revision 1.3 1999/06/17 15:57:02 gerd * Revision 1.3 1999/06/17 15:57:02 gerd
* Invocations of 'do_perm32', 'do_perm48', and 'do_perm64' have * Invocations of 'do_perm32', 'do_perm48', and 'do_perm64' have
* been manually inlined. This speeds the algorithm up from 55 sec per * been manually inlined. This speeds the algorithm up from 55 sec per
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment