Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds constant time equal functions for strings and bytes #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/cryptokit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2024,3 +2024,21 @@ let mod_mult a b c =
Bn.to_bytes ~numbits:(String.length c * 8)
(Bn.mod_ (Bn.mult (Bn.of_bytes a) (Bn.of_bytes b))
(Bn.of_bytes c))

let const_time_eq eq len a b =
if len a = len b
then
let l = len a in
let lst = List.init l (fun x -> x) in
let fold acc i =
acc && (eq a b i) in
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is a difference in the middle of the string, then for bytes after that point, eq will no longer be called (because && is a "short-circuiting" operator). This will be a small difference, but may still be detectable with high resolution timing attacks or cache-line shenanigans.

List.fold_left fold true lst
else false

let const_time_eq_str = const_time_eq (fun a b i ->
String.get a i = String.get b i) String.length


let const_time_eq_bytes = const_time_eq (fun a b i ->
Bytes.get a i = Bytes.get b i) Bytes.length

9 changes: 9 additions & 0 deletions src/cryptokit.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 +1249,12 @@ val mod_mult: string -> string -> string -> string
(** [mod_mult a b c] computes [a*b mod c], where the
strings [a], [b], [c] and the result are viewed as
arbitrary-precision integers in big-endian format. *)

val const_time_eq_str : string -> string -> bool
(** [const_time_eq_str a b] compares the two strings [a] and [b]
in constant time *)

val const_time_eq_bytes : bytes -> bytes -> bool
(** [const_time_eq_bytes a b] compares the two byte strings [a] and [b]
in constant time *)

20 changes: 19 additions & 1 deletion test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,25 @@ let _ =
end


let _ =
testing_function "Constant time equal";
test 1 (const_time_eq_str "abc" "ab") false;

test 2 (const_time_eq_bytes (Bytes.of_string "abc") (Bytes.of_string "ab")) false;

test 3 (const_time_eq_str "coolcoolcool" "coolcoolnice") false;

test 4 (const_time_eq_bytes
(Bytes.of_string "coolcoolcool")
(Bytes.of_string "coolcoolnice")) false;

test 5 (const_time_eq_str "coolcoolcool" "coolcoolcool") true;

test 6 (const_time_eq_bytes
(Bytes.of_string "coolcoolcool")
(Bytes.of_string "coolcoolcool")) true
;;

(* End of tests *)

let _ =
Expand All @@ -956,4 +975,3 @@ let _ =
printf "All tests successful.\n";
exit 0
end