Commit 7b25cd71 authored by gerd's avatar gerd

The initial iperm and xperm permutations have been combined

such that only one table lookup is necessary.


git-svn-id: https://godirepo.camlcity.org/svn/lib-cryptgps/trunk@8 b101cce4-44db-0310-b718-db4b2d8d2e05
parent 14d97770
(* $Id: crypt_des.ml,v 1.6 1999/06/17 20:26:35 gerd Exp $
(* $Id: crypt_des.ml,v 1.7 1999/06/17 20:39:46 gerd Exp $
* ----------------------------------------------------------------------
*
*)
......@@ -236,6 +236,25 @@ module Cryptsystem : Cryptsystem_64.T =
b0 lor b1 lor b2 lor b3 lor b4 lor b5 )
let combine48_and_64 (p48:perm48) (p64:perm64) : perm64 =
(* creates a 'perm64' like table which is:
* let (a,b,c,d) = do_perm64 (x0,x1) in
* let (a',b') = do_perm48 (a,b) in
* let (c',d') = do_perm48 (c,d) in
* (a',b',c',d')
* Note that the result is, strictly speaking, no value64, but
* a value96.
*)
let q = Array.create 2048 (0,0,0,0) in
for i = 0 to 2047 do
let (a,b,c,d) = p64.(i) in
let (a',b') = do_perm48 p48 (a,b) in
let (c',d') = do_perm48 p48 (c,d) in
q.(i) <- (a',b',c',d')
done;
q
let mk_perm32 p' =
(* p: a 32 element array. p(i)=j means that bit position i of the output
* is bit position j in the input.
......@@ -359,6 +378,14 @@ module Cryptsystem : Cryptsystem_64.T =
*)
let des_ixperm = (* combined iperm and xperm *)
lazy
(let iperm = Lazy.force des_iperm in
let xperm = Lazy.force des_xperm in
combine48_and_64 xperm iperm)
let des_pboxperm =
lazy
(mk_perm32
......@@ -515,6 +542,7 @@ module Cryptsystem : Cryptsystem_64.T =
let kperm = Lazy.force des_kperm in
let cperm = Lazy.force des_cperm in
let xperm = Lazy.force des_xperm in
let ixperm = Lazy.force des_ixperm in
let pboxperm = Lazy.force des_pboxperm in
let s1 = Lazy.force sbox1 in
let s2 = Lazy.force sbox2 in
......@@ -589,54 +617,22 @@ module Cryptsystem : Cryptsystem_64.T =
in
(* --------- manually inlined code ---------- *)
(* OLD: let (l00, l01, r00, r01) = do_perm64 iperm x in *)
(* OLD: let (l00_48, l01_48, r00_48, r01_48) = do_perm64 ixperm x in *)
let (a,b,c,d) = x in
let (a0,b0,c0,d0) = iperm.( a lsr 8 ) in
let (a1,b1,c1,d1) = iperm.( 256 + (a land 0xff)) in
let (a2,b2,c2,d2) = iperm.( 512 + (b lsr 8) ) in
let (a3,b3,c3,d3) = iperm.( 768 + (b land 0xff) ) in
let (a4,b4,c4,d4) = iperm.( 1024 + (c lsr 8) ) in
let (a5,b5,c5,d5) = iperm.( 1280 + (c land 0xff) ) in
let (a6,b6,c6,d6) = iperm.( 1536 + (d lsr 8) ) in
let (a7,b7,c7,d7) = iperm.( 1792 + (d land 0xff) ) in
let l00 = a0 lor a1 lor a2 lor a3 lor a4 lor a5 lor a6 lor a7 in
let l01 = b0 lor b1 lor b2 lor b3 lor b4 lor b5 lor b6 lor b7 in
let r00 = c0 lor c1 lor c2 lor c3 lor c4 lor c5 lor c6 lor c7 in
let r01 = d0 lor d1 lor d2 lor d3 lor d4 lor d5 lor d6 lor d7 in
let (a0,b0,c0,d0) = ixperm.( a lsr 8 ) in
let (a1,b1,c1,d1) = ixperm.( 256 + (a land 0xff)) in
let (a2,b2,c2,d2) = ixperm.( 512 + (b lsr 8) ) in
let (a3,b3,c3,d3) = ixperm.( 768 + (b land 0xff) ) in
let (a4,b4,c4,d4) = ixperm.( 1024 + (c lsr 8) ) in
let (a5,b5,c5,d5) = ixperm.( 1280 + (c land 0xff) ) in
let (a6,b6,c6,d6) = ixperm.( 1536 + (d lsr 8) ) in
let (a7,b7,c7,d7) = ixperm.( 1792 + (d land 0xff) ) in
let l00_48 = a0 lor a1 lor a2 lor a3 lor a4 lor a5 lor a6 lor a7 in
let l01_48 = b0 lor b1 lor b2 lor b3 lor b4 lor b5 lor b6 lor b7 in
let r00_48 = c0 lor c1 lor c2 lor c3 lor c4 lor c5 lor c6 lor c7 in
let r01_48 = d0 lor d1 lor d2 lor d3 lor d4 lor d5 lor d6 lor d7 in
(* ------------------ end -------------------- *)
(* TO COMPUTE: (l00_48, l01_48) = do_perm48 xperm (l00,l01) *)
let a0 = (l01 land 1) lsl 23 in
let a1 = (l00 land 0xf800) lsl 7 in
let a2 = (l00 land 0x1f80) lsl 5 in
let a3 = (l00 land 0x01f8) lsl 3 in
let a4 = (l00 land 0x001f) lsl 1 in
let a5 = l01 lsr 15 in
let b0 = (l00 land 1) lsl 23 in
let b1 = (l01 land 0xf800) lsl 7 in
let b2 = (l01 land 0x1f80) lsl 5 in
let b3 = (l01 land 0x01f8) lsl 3 in
let b4 = (l01 land 0x001f) lsl 1 in
let b5 = l00 lsr 15 in
let l00_48 = a0 lor a1 lor a2 lor a3 lor a4 lor a5 in
let l01_48 = b0 lor b1 lor b2 lor b3 lor b4 lor b5 in
(* TO COMPUTE: (r00_48, r01_48) = do_perm48 xperm (r00,r01) *)
let a0 = (r01 land 1) lsl 23 in
let a1 = (r00 land 0xf800) lsl 7 in
let a2 = (r00 land 0x1f80) lsl 5 in
let a3 = (r00 land 0x01f8) lsl 3 in
let a4 = (r00 land 0x001f) lsl 1 in
let a5 = r01 lsr 15 in
let b0 = (r00 land 1) lsl 23 in
let b1 = (r01 land 0xf800) lsl 7 in
let b2 = (r01 land 0x1f80) lsl 5 in
let b3 = (r01 land 0x01f8) lsl 3 in
let b4 = (r01 land 0x001f) lsl 1 in
let b5 = r00 lsr 15 in
let r00_48 = a0 lor a1 lor a2 lor a3 lor a4 lor a5 in
let r01_48 = b0 lor b1 lor b2 lor b3 lor b4 lor b5 in
do_rounds 0 l00_48 l01_48 r00_48 r01_48
......@@ -817,6 +813,10 @@ module Cryptmodes = Cryptmodes_64.Make_modes(Cryptsystem)
* history:
*
* $Log: crypt_des.ml,v $
* Revision 1.7 1999/06/17 20:39:46 gerd
* The initial iperm and xperm permutations have been combined
* such that only one table lookup is necessary.
*
* Revision 1.6 1999/06/17 20:26:35 gerd
* In previous revisions, in every 'do_rounds' loop cycle the 'xperm'
* permutation was applied again to convert 32 bit numbers to 48 bit numbers.
......
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