Skip to content

Commit

Permalink
mirage-crypto: revise DES to avoid global state in key derivation / k…
Browse files Browse the repository at this point in the history
…ey usage
  • Loading branch information
hannesm committed Mar 27, 2024
1 parent b1a794a commit 379852d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 432 deletions.
6 changes: 2 additions & 4 deletions src/cipher_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,7 @@ module DES = struct
invalid_arg "DES.of_secret: key length %u" (String.length key) ;
let key = Bytes.of_string key in
let keybuf = Bytes.create k_s in
Native.DES.des3key key direction ;
Native.DES.cp3key keybuf ;
Native.DES.des3key key direction keybuf;
Bytes.unsafe_to_string keybuf

let e_of_secret = gen_of_secret ~direction:0
Expand All @@ -438,8 +437,7 @@ module DES = struct
let of_secret secret = (e_of_secret secret, d_of_secret secret)

let encrypt ~key ~blocks src off1 dst off2 =
Native.DES.use3key key ;
Native.DES.ddes src off1 dst off2 blocks
Native.DES.ddes src off1 dst off2 blocks key

let decrypt = encrypt
end
Expand Down
6 changes: 2 additions & 4 deletions src/native.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ module AES = struct
end

module DES = struct
external ddes : string -> int -> bytes -> int -> int -> unit = "mc_des_ddes" [@@noalloc]
external des3key : bytes -> int -> unit = "mc_des_des3key" [@@noalloc]
external cp3key : bytes -> unit = "mc_des_cp3key" [@@noalloc]
external use3key : string -> unit = "mc_des_use3key" [@@noalloc]
external ddes : string -> int -> bytes -> int -> int -> string -> unit = "mc_des_ddes_bc" "mc_des_ddes" [@@noalloc]
external des3key : bytes -> int -> bytes -> unit = "mc_des_des3key" [@@noalloc]
external k_s : unit -> int = "mc_des_key_size" [@@noalloc]
end

Expand Down
Loading

0 comments on commit 379852d

Please sign in to comment.