cryptmodes_64.ml 14.5 KB
Newer Older
gerd's avatar
gerd committed
1
(* $Id: cryptmodes_64.ml,v 1.2 2001/03/10 16:43:21 gerd Exp $
gerd's avatar
gerd committed
2 3 4 5
 * ----------------------------------------------------------------------
 * This module is part of the cryptgps package by Gerd Stolpmann.
 *)

gerd's avatar
gerd committed
6 7
open Crypt_aux

gerd's avatar
gerd committed
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

module type T =
    sig
      
      type key

      val encrypt_cbc :
	  key -> (int * int * int * int) -> string -> 
	    ((int * int * int * int) * string)

      val decrypt_cbc :
	  key -> (int * int * int * int) -> string -> 
	    ((int * int * int * int) * string)

      val encrypt_cfb8 :
	  key -> (int * int * int * int) -> string -> 
	    ((int * int * int * int) * string)

      val decrypt_cfb8 :
	  key -> (int * int * int * int) -> string -> 
	    ((int * int * int * int) * string)

      val encrypt_cfb64 :
	  key -> (int * int * int * int) -> int -> string -> 
	    ((int * int * int * int) * int * string)

      val decrypt_cfb64 :
	  key -> (int * int * int * int) -> int -> string -> 
	    ((int * int * int * int) * int * string)

      val crypt_ofb :
	  key -> (int * int * int * int) -> int -> string -> 
	    ((int * int * int * int) * int * string)
    end
;;


module Make_modes (M : Cryptsystem_64.T) =
  struct

    type key = M.key
    
    let encrypt_cbc k iv data =
      let l = String.length data in
      if l mod 8 <> 0 then failwith "encrypt_cbc";

      let n = l / 8 in
      let data' = String.create l in

      let v = ref iv in
      for i = 0 to n-1 do
	let j = 8*i in

	let x3 = (Char.code(data.[j]) lsl 8) lor (Char.code(data.[j+1])) in
	let x2 = (Char.code(data.[j+2]) lsl 8) lor (Char.code(data.[j+3])) in
	let x1 = (Char.code(data.[j+4]) lsl 8) lor (Char.code(data.[j+5])) in
	let x0 = (Char.code(data.[j+6]) lsl 8) lor (Char.code(data.[j+7])) in

	let (v3,v2,v1,v0) = !v in

	v := M.encrypt_ecb k (v3 lxor x3, v2 lxor x2, v1 lxor x1, v0 lxor x0);

	let (v3',v2',v1',v0') = !v in

	data'.[j]   <- Char.chr(v3' lsr 8);
	data'.[j+1] <- Char.chr(v3' land 0xff);
	data'.[j+2] <- Char.chr(v2' lsr 8);
	data'.[j+3] <- Char.chr(v2' land 0xff);
	data'.[j+4] <- Char.chr(v1' lsr 8);
	data'.[j+5] <- Char.chr(v1' land 0xff);
	data'.[j+6] <- Char.chr(v0' lsr 8);
	data'.[j+7] <- Char.chr(v0' land 0xff);
      done;

      !v, data'


    let decrypt_cbc k iv data =
      let l = String.length data in
      if l mod 8 <> 0 then failwith "decrypt_cbc";

      let n = l / 8 in
      let data' = String.create l in

      let v = ref iv in
      for i = 0 to n-1 do
	let j = 8*i in

	let x3 = (Char.code(data.[j]) lsl 8) lor (Char.code(data.[j+1])) in
	let x2 = (Char.code(data.[j+2]) lsl 8) lor (Char.code(data.[j+3])) in
	let x1 = (Char.code(data.[j+4]) lsl 8) lor (Char.code(data.[j+5])) in
	let x0 = (Char.code(data.[j+6]) lsl 8) lor (Char.code(data.[j+7])) in

	let (y3,y2,y1,y0) = M.decrypt_ecb k (x3,x2,x1,x0) in

	let (v3,v2,v1,v0) = !v in

	let z3 = y3 lxor v3 in
	let z2 = y2 lxor v2 in
	let z1 = y1 lxor v1 in
	let z0 = y0 lxor v0 in

	data'.[j]   <- Char.chr(z3 lsr 8);
	data'.[j+1] <- Char.chr(z3 land 0xff);
	data'.[j+2] <- Char.chr(z2 lsr 8);
	data'.[j+3] <- Char.chr(z2 land 0xff);
	data'.[j+4] <- Char.chr(z1 lsr 8);
	data'.[j+5] <- Char.chr(z1 land 0xff);
	data'.[j+6] <- Char.chr(z0 lsr 8);
	data'.[j+7] <- Char.chr(z0 land 0xff);

	v := (x3,x2,x1,x0);
      done;

      !v, data'


    let encrypt_cfb8 k iv data =
      let l = String.length data in
      let data' = String.create l in

      let sr = ref iv in (* shift register *)
      
      for i = 0 to l-1 do
	let (v,_,_,_) = M.encrypt_ecb k !sr in
	let c = Char.code(data.[i]) lxor (v lsr 8) in
	data'.[i] <- Char.chr c;
	let (sr3, sr2, sr1, sr0) = !sr in
	sr := (((sr3 lsl 8) land 0xff00) lor (sr2 lsr 8),
	       ((sr2 lsl 8) land 0xff00) lor (sr1 lsr 8),
	       ((sr1 lsl 8) land 0xff00) lor (sr0 lsr 8),
	       ((sr0 lsl 8) land 0xff00) lor c);
      done;

      !sr, data'


    let decrypt_cfb8 k iv data = 
      let l = String.length data in
      let data' = String.create l in

      let sr = ref iv in (* shift register *)
      
      for i = 0 to l-1 do
	let (v,_,_,_) = M.encrypt_ecb k !sr in  (* sic! *)
	let c = Char.code(data.[i]) in
	let p = c lxor (v lsr 8) in
	data'.[i] <- Char.chr p;
	let (sr3, sr2, sr1, sr0) = !sr in
	sr := (((sr3 lsl 8) land 0xff00) lor (sr2 lsr 8),
	       ((sr2 lsl 8) land 0xff00) lor (sr1 lsr 8),
	       ((sr1 lsl 8) land 0xff00) lor (sr0 lsr 8),
	       ((sr0 lsl 8) land 0xff00) lor c);
      done;

      !sr, data'


    let array_of_quadrupel (n3,n2,n1,n0) =
      [| n3 lsr 8;
	 n3 land 0xff;
	 n2 lsr 8;
	 n2 land 0xff;
	 n1 lsr 8;
	 n1 land 0xff;
	 n0 lsr 8;
	 n0 land 0xff |]

    let quadrupel_of_array a =
      ((a.(0) lsl 8) lor a.(1),
       (a.(2) lsl 8) lor a.(3),
       (a.(4) lsl 8) lor a.(5),
       (a.(6) lsl 8) lor a.(7))

    let encrypt_cfb64 k iv j data =
      if j < 0 or j > 7 then failwith "encrypt_cfb64";
      let l = String.length data in
      let data' = String.create l in

      let sr_a = ref (array_of_quadrupel iv) in
      let jc = ref j in

      for i = 0 to l-1 do
	if !jc = 0 then
	  sr_a := array_of_quadrupel 
	            (M.encrypt_ecb k (quadrupel_of_array !sr_a));

	let sr_jc = (!sr_a).(!jc) in
	let c = Char.code(data.[i]) lxor sr_jc in
	data'.[i] <- Char.chr c;
	(!sr_a).(!jc) <- c;
	jc := (!jc + 1) mod 8;
      done;

      let sr = quadrupel_of_array !sr_a in
      sr, !jc, data'


    let decrypt_cfb64 k iv j data =
      if j < 0 or j > 7 then failwith "decrypt_cfb64";
      let l = String.length data in
      let data' = String.create l in

      let sr_a = ref (array_of_quadrupel iv) in
      let jc = ref j in

      for i = 0 to l-1 do
	if !jc = 0 then
	  sr_a := array_of_quadrupel 
	            (M.encrypt_ecb k (quadrupel_of_array !sr_a));

	let sr_jc = (!sr_a).(!jc) in
	let c = Char.code(data.[i]) in
	data'.[i] <- Char.chr (c lxor sr_jc);
	(!sr_a).(!jc) <- c;
	jc := (!jc + 1) mod 8;
      done;

      let sr = quadrupel_of_array !sr_a in
      sr, !jc, data'


    let crypt_ofb k iv j data =
      if j < 0 or j > 7 then failwith "crypt_ofb";
      let l = String.length data in
      let data' = String.create l in

      let sr_a = ref (array_of_quadrupel iv) in
      let jc = ref j in

      for i = 0 to l-1 do
	if !jc = 0 then
	  sr_a := array_of_quadrupel 
	            (M.encrypt_ecb k (quadrupel_of_array !sr_a));

	let sr_jc = (!sr_a).(!jc) in
	let c = Char.code(data.[i]) lxor sr_jc in
	data'.[i] <- Char.chr c;
	jc := (!jc + 1) mod 8;
      done;

      let sr = quadrupel_of_array !sr_a in
      sr, !jc, data'

  end
;;


gerd's avatar
gerd committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
module Make_modes_int32 (M : Cryptsystem_64.T) =
  struct

    type key = M.key
    
    let encrypt_cbc k iv data =
      let l = String.length data in
      if l mod 8 <> 0 then failwith "encrypt_cbc";

      let n = l / 8 in
      let data' = String.create l in

      let vl = ref Int32.zero in
      let vr = ref Int32.zero in
      int32_of_quadruple iv vl vr;

      for i = 0 to n-1 do
	let j = 8*i in

	let x3 = (Char.code(data.[j]) lsl 8) lor (Char.code(data.[j+1])) in
	let x2 = (Char.code(data.[j+2]) lsl 8) lor (Char.code(data.[j+3])) in
	let x1 = (Char.code(data.[j+4]) lsl 8) lor (Char.code(data.[j+5])) in
	let x0 = (Char.code(data.[j+6]) lsl 8) lor (Char.code(data.[j+7])) in

	let xl =
	  Int32.logor
	    (Int32.shift_left (Int32.of_int x3) 16)
	    (Int32.of_int x2)
	in
	let xr =
	  Int32.logor
	    (Int32.shift_left (Int32.of_int x1) 16)
	    (Int32.of_int x0)
	in

	let yl = Int32.logxor xl !vl in
	let yr = Int32.logxor xr !vr in

	M.encrypt_ecb_int32 k yl yr vl vr;

	let v3' = Int32.to_int (Int32.shift_right_logical !vl 16) in
	let v2' = Int32.to_int !vl in
	let v1' = Int32.to_int (Int32.shift_right_logical !vr 16) in
	let v0' = Int32.to_int !vr in

	data'.[j]   <- Char.chr((v3' lsr 8) land 0xff);
	data'.[j+1] <- Char.chr(v3' land 0xff);
	data'.[j+2] <- Char.chr((v2' lsr 8) land 0xff);
	data'.[j+3] <- Char.chr(v2' land 0xff);
	data'.[j+4] <- Char.chr((v1' lsr 8) land 0xff);
	data'.[j+5] <- Char.chr(v1' land 0xff);
	data'.[j+6] <- Char.chr((v0' lsr 8) land 0xff);
	data'.[j+7] <- Char.chr(v0' land 0xff);
      done;

      quadruple_of_int32 !vl !vr, data'


    let decrypt_cbc k iv data =
      let l = String.length data in
      if l mod 8 <> 0 then failwith "decrypt_cbc";

      let n = l / 8 in
      let data' = String.create l in

      let vl = ref Int32.zero in
      let vr = ref Int32.zero in
      int32_of_quadruple iv vl vr;

      for i = 0 to n-1 do
	let j = 8*i in

	let x3 = (Char.code(data.[j]) lsl 8) lor (Char.code(data.[j+1])) in
	let x2 = (Char.code(data.[j+2]) lsl 8) lor (Char.code(data.[j+3])) in
	let x1 = (Char.code(data.[j+4]) lsl 8) lor (Char.code(data.[j+5])) in
	let x0 = (Char.code(data.[j+6]) lsl 8) lor (Char.code(data.[j+7])) in

	let xl =
	  Int32.logor
	    (Int32.shift_left (Int32.of_int x3) 16)
	    (Int32.of_int x2)
	in
	let xr =
	  Int32.logor
	    (Int32.shift_left (Int32.of_int x1) 16)
	    (Int32.of_int x0)
	in

	let yl = ref Int32.zero in
	let yr = ref Int32.zero in

	M.decrypt_ecb_int32 k xl xr yl yr;

	let zl = Int32.logxor !yl !vl in
	let zr = Int32.logxor !yr !vr in

	let z3 = Int32.to_int (Int32.shift_right_logical zl 16) in
	let z2 = Int32.to_int zl in
	let z1 = Int32.to_int (Int32.shift_right_logical zr 16) in
	let z0 = Int32.to_int zr in

	data'.[j]   <- Char.chr((z3 lsr 8) land 0xff);
	data'.[j+1] <- Char.chr(z3 land 0xff);
	data'.[j+2] <- Char.chr((z2 lsr 8) land 0xff);
	data'.[j+3] <- Char.chr(z2 land 0xff);
	data'.[j+4] <- Char.chr((z1 lsr 8) land 0xff);
	data'.[j+5] <- Char.chr(z1 land 0xff);
	data'.[j+6] <- Char.chr((z0 lsr 8) land 0xff);
	data'.[j+7] <- Char.chr(z0 land 0xff);

	vl := xl;
	vr := xr;
      done;

      quadruple_of_int32 !vl !vr, data'


    let encrypt_cfb8 k iv data =
      let l = String.length data in
      let data' = String.create l in

      let sr_l = ref Int32.zero in   (* shift register MSB *)
      let sr_r = ref Int32.zero in   (* shift register LSB *)
      int32_of_quadruple iv sr_l sr_r;

      let out_l = ref Int32.zero in
      let out_r = ref Int32.zero in
      
      for i = 0 to l-1 do
	M.encrypt_ecb_int32 k !sr_l !sr_r out_l out_r;
	let c = Char.code(data.[i]) lxor 
			  ((Int32.to_int 
			      (Int32.shift_right_logical !out_l 24)) land 0xff)
	in
	data'.[i] <- Char.chr c;
	sr_l := Int32.logor 
	          (Int32.shift_left !sr_l 8)
	          (Int32.shift_right_logical !sr_r 24);
	sr_r := Int32.logor
	          (Int32.shift_left !sr_r 8)
	          (Int32.of_int c)
      done;

      quadruple_of_int32 !sr_l !sr_r, data'


    let decrypt_cfb8 k iv data = 
      let l = String.length data in
      let data' = String.create l in

      let sr_l = ref Int32.zero in   (* shift register MSB *)
      let sr_r = ref Int32.zero in   (* shift register LSB *)
      int32_of_quadruple iv sr_l sr_r;

      let out_l = ref Int32.zero in
      let out_r = ref Int32.zero in
      
      for i = 0 to l-1 do
	M.encrypt_ecb_int32 k !sr_l !sr_r out_l out_r;    (* sic! *)
	let c = Char.code(data.[i]) in
	let p = c lxor 
		((Int32.to_int 
		    (Int32.shift_right_logical !out_l 24)) land 0xff)
	in
	data'.[i] <- Char.chr p;
	sr_l := Int32.logor 
	          (Int32.shift_left !sr_l 8)
	          (Int32.shift_right_logical !sr_r 24);
	sr_r := Int32.logor
	          (Int32.shift_left !sr_r 8)
	          (Int32.of_int c)
      done;

      quadruple_of_int32 !sr_l !sr_r, data'


    let mask =
      [| Int32.of_string "0x00ffffff";
	 Int32.of_string "0xff00ffff";
	 Int32.of_string "0xffff00ff";
	 Int32.of_string "0xffffff00";
      |]

    let encrypt_cfb64 k iv j data =
      if j < 0 or j > 7 then failwith "encrypt_cfb64";
      let l = String.length data in
      let data' = String.create l in

      let sr_l = ref Int32.zero in   (* shift register MSB *)
      let sr_r = ref Int32.zero in   (* shift register LSB *)
      int32_of_quadruple iv sr_l sr_r;

      let jc = ref j in

      for i = 0 to l-1 do
	if !jc = 0 then
	  M.encrypt_ecb_int32 k !sr_l !sr_r sr_l sr_r;

	let jc8 = !jc lsl 3 in

	let sr_jc = 
	  if !jc < 4 then
	    (Int32.to_int
	       (Int32.shift_right_logical !sr_l (24 - jc8)))
	    land 0xff
	  else
	    (Int32.to_int
	       (Int32.shift_right_logical !sr_r (56 - jc8)))
	    land 0xff
	in
	let c = Char.code(data.[i]) lxor sr_jc in
	data'.[i] <- Char.chr c;
	( if !jc < 4 then 
	    sr_l :=
	      Int32.logor
		(Int32.logand !sr_l mask.(!jc))
		(Int32.shift_left (Int32.of_int c) (24 - jc8))
	  else
	    sr_r :=
	      Int32.logor
		(Int32.logand !sr_r mask.(!jc - 4))
		(Int32.shift_left (Int32.of_int c) (56 - jc8))
	);
	jc := (!jc + 1) mod 8;
      done;

      quadruple_of_int32 !sr_l !sr_r, !jc, data'


    let decrypt_cfb64 k iv j data =
      if j < 0 or j > 7 then failwith "decrypt_cfb64";
      let l = String.length data in
      let data' = String.create l in

      let sr_l = ref Int32.zero in   (* shift register MSB *)
      let sr_r = ref Int32.zero in   (* shift register LSB *)
      int32_of_quadruple iv sr_l sr_r;

      let jc = ref j in

      for i = 0 to l-1 do
	if !jc = 0 then
	  M.encrypt_ecb_int32 k !sr_l !sr_r sr_l sr_r;

	let jc8 = !jc lsl 3 in

	let sr_jc = 
	  if !jc < 4 then
	    (Int32.to_int
	       (Int32.shift_right_logical !sr_l (24 - jc8)))
	    land 0xff
	  else
	    (Int32.to_int
	       (Int32.shift_right_logical !sr_r (56 - jc8)))
	    land 0xff
	in
	let c = Char.code(data.[i]) in
	data'.[i] <- Char.chr (c lxor sr_jc);
	( if !jc < 4 then 
	    sr_l :=
	      Int32.logor
		(Int32.logand !sr_l mask.(!jc))
		(Int32.shift_left (Int32.of_int c) (24 - jc8))
	  else
	    sr_r :=
	      Int32.logor
		(Int32.logand !sr_r mask.(!jc - 4))
		(Int32.shift_left (Int32.of_int c) (56 - jc8))
	);
	jc := (!jc + 1) mod 8;
      done;

      quadruple_of_int32 !sr_l !sr_r, !jc, data'


    let crypt_ofb k iv j data =
      if j < 0 or j > 7 then failwith "crypt_ofb";
      let l = String.length data in
      let data' = String.create l in

      let sr_l = ref Int32.zero in   (* shift register MSB *)
      let sr_r = ref Int32.zero in   (* shift register LSB *)
      int32_of_quadruple iv sr_l sr_r;

      let jc = ref j in

      for i = 0 to l-1 do
	if !jc = 0 then
	if !jc = 0 then
	  M.encrypt_ecb_int32 k !sr_l !sr_r sr_l sr_r;

	let jc8 = !jc lsl 3 in

	let sr_jc = 
	  if !jc < 4 then
	    (Int32.to_int
	       (Int32.shift_right_logical !sr_l (24 - jc8)))
	    land 0xff
	  else
	    (Int32.to_int
	       (Int32.shift_right_logical !sr_r (56 - jc8)))
	    land 0xff
	in
	let c = Char.code(data.[i]) lxor sr_jc in
	data'.[i] <- Char.chr c;
	jc := (!jc + 1) mod 8;
      done;

      quadruple_of_int32 !sr_l !sr_r, !jc, data'

  end
;;


gerd's avatar
gerd committed
570 571 572 573
(* ======================================================================
 * History:
 * 
 * $Log: cryptmodes_64.ml,v $
gerd's avatar
gerd committed
574 575 576
 * Revision 1.2  2001/03/10 16:43:21  gerd
 * 	int32 experiments
 *
gerd's avatar
gerd committed
577 578 579 580 581
 * Revision 1.1  1999/06/04 20:42:01  gerd
 * 	Initial revision.
 *
 * 
 *)