@@ -23,81 +23,43 @@ module type DH = sig
2323end
2424
2525module X25519 : DH = struct
26- type private_key = Private_key of Z .t
27- type public_key = Public_key of Z .t
28-
29- let key_size = 32
30-
31- module A = struct
32- type element = Z .t
33- type integral = Z .t
34-
35- let p = Z. (one lsl 255 - ~$ 19 )
36-
37- let bits = 255
38-
39- let a24 = Z. of_int 121665
40-
41- let two = Z. (~$ 2 )
26+ open Hacl_star
4227
43- let constant_time_conditional_swap cond a b =
44- let c = Z. (rem cond two) in
45- let c' = Z. (one - c) in
46- let a' = Z. (c'* a + c* b) in
47- let b' = Z. (c'* b + c* a) in
48- a', b'
49- end
50-
51- module C = Curve. Make (Zfield. Zp (A ))(Z )(A )
52-
53- (* Quoth the RFC:
54- set the three least significant bits of the first byte and the most significant bit
55- of the last to zero, set the second most significant bit of the last byte to 1
56- *)
57- let sanitize_scalar =
58- let unset_this = Z. logor Z. (~$ 7 ) (Z. shift_left Z. (~$ 128 ) (8 * 31 )) in
59- let set_that = Z. shift_left Z. (~$ 64 ) (8 * 31 ) in
60- fun z ->
61- Z. (z - (logand z unset_this))
62- |> Z. logor set_that
28+ type public_key = Public_key of Hacl .C .t
29+ type private_key = Private_key of Hacl .C .t
6330
64- let public_key_of_string: string -> public_key = fun s ->
65- let p = Serde. z_of_hex s in
66- let high = Z. (logand p (~$ 128 lsl 248 )) in
67- Public_key Z. (p - high)
31+ let key_size = 32
6832
69- let public_key_of_bytes: Bytes. t -> public_key = fun buf ->
33+ let public_key_of_string s = Public_key (Serde. bytes_of_hex s)
34+ let public_key_of_bytes buf =
7035 assert (Bytes. length buf = key_size);
71- let p = Serde. z_of_bytes buf in
72- let high = Z. (logand p (~$ 128 lsl 248 )) in
73- Public_key Z. (p - high)
74-
75- let string_of_public_key: public_key -> string = function Public_key pk ->
76- Serde. hex_of_z key_size pk
77-
78- let bytes_of_public_key: public_key -> Bytes. t = function Public_key pk ->
79- Serde. bytes_of_z key_size pk
80-
81- let private_key_of_string: string -> private_key = fun s ->
82- let z = Serde. z_of_hex s |> sanitize_scalar in
83- Private_key z
84-
85- let private_key_of_bytes: Bytes. t -> private_key = fun buf ->
36+ Public_key (buf)
37+ let private_key_of_string s = Private_key (Serde. bytes_of_hex s)
38+ let private_key_of_bytes buf =
8639 assert (Bytes. length buf = key_size);
87- let z = Serde. z_of_bytes buf |> sanitize_scalar in
88- Private_key z
40+ Private_key (buf)
8941
90- let string_of_private_key: private_key -> string = function Private_key pk ->
91- Serde. hex_of_z key_size pk
42+ let base = public_key_of_string " 0900000000000000000000000000000000000000000000000000000000000000"
9243
93- let bytes_of_private_key: private_key -> Bytes. t = function Private_key pk ->
94- Serde. bytes_of_z key_size pk
44+ let string_of_public_key (Public_key k ) =
45+ Serde. hex_of_bytes k
46+ let string_of_private_key (Private_key k ) =
47+ Serde. hex_of_bytes k
48+ let bytes_of_public_key (Public_key k ) = k
49+ let bytes_of_private_key (Private_key k ) = k
9550
96- let scale (Private_key priv ) (Public_key pub ) = Public_key (C. scale priv pub)
9751
98- let base = Public_key (Z. of_int 9 )
52+ let scale (Private_key priv ) (Public_key pub ) =
53+ let out = Bytes. make 32 '\x00' in
54+ if EverCrypt.Curve25519. ecdh out priv pub then
55+ Public_key out
56+ else
57+ failwith " arrg"
58+ let public_key_of_private_key (Private_key priv ) =
59+ let out = Bytes. make 32 '\x00' in
60+ EverCrypt.Curve25519. secret_to_public out priv;
61+ Public_key out
9962
100- let public_key_of_private_key priv = scale priv base
10163end
10264
10365let x25519 ~priv ~pub =
0 commit comments