File: crypt_3des.ml

package info (click to toggle)
cryptgps 0.2.1-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 280 kB
  • ctags: 420
  • sloc: ml: 2,627; ansic: 172; sh: 169; makefile: 118
file content (142 lines) | stat: -rw-r--r-- 4,715 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(* $Id: crypt_3des.ml,v 1.2 2001/03/10 16:43:21 gerd Exp $
 * ----------------------------------------------------------------------
 * This module is part of the cryptgps package by Gerd Stolpmann.
 *)

open Crypt_aux

let check_parity key =
  match String.length key with
     8 -> Crypt_des.check_parity key
  | 16 -> Crypt_des.check_parity (String.sub key 0 8);
          Crypt_des.check_parity (String.sub key 8 8)
  | 24 -> Crypt_des.check_parity (String.sub key 0 8);
          Crypt_des.check_parity (String.sub key 8 8);
          Crypt_des.check_parity (String.sub key 16 8)
  | _ -> failwith "Crypt_3des: invalid key length"
;;


let set_parity key =
  match String.length key with
     8 -> Crypt_des.set_parity key
  | 16 -> Crypt_des.set_parity (String.sub key 0 8) ^
          Crypt_des.set_parity (String.sub key 8 8)
  | 24 -> Crypt_des.set_parity (String.sub key 0 8) ^
          Crypt_des.set_parity (String.sub key 8 8) ^
          Crypt_des.set_parity (String.sub key 16 8)
  | _ -> failwith "Crypt_3des: invalid key length"
;;


module Cryptsystem : Cryptsystem_64.T =
  struct
    
    type key =
	{ k1 : Crypt_des.Cryptsystem.key;
	  k2 : Crypt_des.Cryptsystem.key;
	  k3 : Crypt_des.Cryptsystem.key;
	  n_keys : int
	} 


    let encrypt_ecb k x =
      match k.n_keys with
	1 -> Crypt_des.Cryptsystem.encrypt_ecb k.k1 x
      |	2 -> let x'  = Crypt_des.Cryptsystem.encrypt_ecb k.k1 x in
	     let x'' = Crypt_des.Cryptsystem.decrypt_ecb k.k2 x' in
	     Crypt_des.Cryptsystem.encrypt_ecb k.k1 x''
      |	3 -> let x'  = Crypt_des.Cryptsystem.encrypt_ecb k.k1 x in
	     let x'' = Crypt_des.Cryptsystem.decrypt_ecb k.k2 x' in
	     Crypt_des.Cryptsystem.encrypt_ecb k.k3 x''
      | _ -> failwith "Crypt_3des: invalid key length"


    let encrypt_ecb_int32 k xl xr ret_xl ret_xr =
      let x = quadruple_of_int32 xl xr in
      let y = encrypt_ecb k x in
      int32_of_quadruple y ret_xl ret_xr


    let decrypt_ecb k x =
      match k.n_keys with
	1 -> Crypt_des.Cryptsystem.decrypt_ecb k.k1 x
      |	2 -> let x'  = Crypt_des.Cryptsystem.decrypt_ecb k.k1 x in
	     let x'' = Crypt_des.Cryptsystem.encrypt_ecb k.k2 x' in
	     Crypt_des.Cryptsystem.decrypt_ecb k.k1 x''
      |	3 -> let x'  = Crypt_des.Cryptsystem.decrypt_ecb k.k1 x in
	     let x'' = Crypt_des.Cryptsystem.encrypt_ecb k.k2 x' in
	     Crypt_des.Cryptsystem.decrypt_ecb k.k3 x''
      | _ -> failwith "Crypt_3des: invalid key length"


    let decrypt_ecb_int32 k xl xr ret_xl ret_xr =
      let x = quadruple_of_int32 xl xr in
      let y = decrypt_ecb k x in
      int32_of_quadruple y ret_xl ret_xr


    let prepare key =
      let l_key = String.length key in
      match l_key with
	 8 -> let k = Crypt_des.Cryptsystem.prepare key in
	      { k1=k; k2=k; k3=k; n_keys=1 }
      |	16 -> let k1 = Crypt_des.Cryptsystem.prepare (String.sub key 0 8) in
	      let k2 = Crypt_des.Cryptsystem.prepare (String.sub key 8 8) in
	      { k1=k1; k2=k2; k3=k1; n_keys=2 }
      |	24 -> let k1 = Crypt_des.Cryptsystem.prepare (String.sub key 0 8) in
	      let k2 = Crypt_des.Cryptsystem.prepare (String.sub key 8 8) in
	      let k3 = Crypt_des.Cryptsystem.prepare (String.sub key 16 8) in
	      { k1=k1; k2=k2; k3=k3; n_keys=3 }
      |	_ -> failwith "Crypt_3des: invalid key length"


    let textkey k =
      match k.n_keys with
	1 -> Crypt_des.Cryptsystem.textkey k.k1
      |	2 -> Crypt_des.Cryptsystem.textkey k.k1 ^ 
	     Crypt_des.Cryptsystem.textkey k.k2
      |	3 -> Crypt_des.Cryptsystem.textkey k.k1 ^ 
	     Crypt_des.Cryptsystem.textkey k.k2 ^
	     Crypt_des.Cryptsystem.textkey k.k3
      | _ -> failwith "Crypt_3des: invalid key length"


    let is_weak k =
      match k.n_keys with
	1 -> Crypt_des.Cryptsystem.is_weak k.k1
      |	2 -> Crypt_des.Cryptsystem.is_weak k.k1 or
	     Crypt_des.Cryptsystem.is_weak k.k2 or
	     Crypt_des.Cryptsystem.textkey k.k1 = 
	       Crypt_des.Cryptsystem.textkey k.k2
      |	3 -> Crypt_des.Cryptsystem.is_weak k.k1 or
	     Crypt_des.Cryptsystem.is_weak k.k2 or
	     Crypt_des.Cryptsystem.is_weak k.k3 or
	     Crypt_des.Cryptsystem.textkey k.k1 = 
	       Crypt_des.Cryptsystem.textkey k.k2 or
	     Crypt_des.Cryptsystem.textkey k.k1 = 
	       Crypt_des.Cryptsystem.textkey k.k3 or
	     Crypt_des.Cryptsystem.textkey k.k2 = 
	       Crypt_des.Cryptsystem.textkey k.k3
      | _ -> failwith "Crypt_3des: invalid key length"

  end
;;


module Cryptmodes = Cryptmodes_64.Make_modes(Cryptsystem)
;;


(* ======================================================================
 * History:
 * 
 * $Log: crypt_3des.ml,v $
 * Revision 1.2  2001/03/10 16:43:21  gerd
 * 	int32 experiments
 *
 * Revision 1.1  1999/06/18 00:23:58  gerd
 * 	First release.
 *
 * 
 *)