···5757 Uses mirage-crypto-ec for constant-time operations. *)
58585959module P256 = struct
6060- module Ec = Crypto_ec.P256.Point
6060+ module Dsa = Crypto_ec.P256.Dsa
61616262 (** The curve order for scalar arithmetic *)
6363 let order =
···6969 Z.of_string
7070 "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"
71717272- type point = Ec.point
7272+ type point = Dsa.pub
73737474 (** SPAKE2 M point in SEC1 uncompressed format *)
7575 let m_bytes =
···8080 "\x04\xd8\xbb\xd6\xc6\x39\xc6\x29\x37\xb0\x4d\x99\x7f\x38\xc3\x77\x07\x19\xc6\x29\xd7\x01\x4d\x49\xa2\x4b\x4f\x98\xba\xa1\x29\x2b\x49\x07\xd6\x0a\xa6\xbf\xad\xe4\x50\x08\xa6\x36\x33\x7f\x51\x68\xc6\x4d\x9b\xd3\x60\x34\x80\x8c\xd5\x64\x49\x0b\x1e\x65\x6e\xdb\xe7"
81818282 let m =
8383- match Ec.of_octets m_bytes with
8383+ match Dsa.pub_of_octets m_bytes with
8484 | Ok p -> p
8585 | Error _ -> failwith "Invalid SPAKE2 M constant"
86868787 let n =
8888- match Ec.of_octets n_bytes with
8888+ match Dsa.pub_of_octets n_bytes with
8989 | Ok p -> p
9090 | Error _ -> failwith "Invalid SPAKE2 N constant"
91919292- let generator = Ec.generator
9393- let add = Ec.add
9494- let scalar_mult scalar pt = Ec.scalar_mult scalar pt
9595- let scalar_mult_base scalar = Ec.scalar_mult scalar Ec.generator
9696- let to_bytes pt = Ec.to_octets pt
9292+ let generator = Dsa.Primitive.generator
9393+ let add = Dsa.Primitive.add
9494+ let scalar_mult scalar pt = Dsa.Primitive.scalar_mult scalar pt
9595+9696+ let scalar_mult_base scalar =
9797+ Dsa.Primitive.scalar_mult scalar Dsa.Primitive.generator
9898+9999+ let to_bytes pt = Dsa.pub_to_octets pt
9710098101 (** Negate a point: -P = (x, p - y). We parse the SEC1 encoding, negate the
99102 y-coordinate, and re-encode. This is a single arithmetic operation, not
100103 timing-sensitive. *)
101104 let negate pt =
102102- let octets = Ec.to_octets pt in
105105+ let octets = Dsa.pub_to_octets pt in
103106 if String.length octets = 1 && octets.[0] = '\x00' then pt
104107 else
105108 let x_bytes = String.sub octets 1 32 in
···108111 let neg_y = Z.sub prime y in
109112 let neg_y_bytes = z_to_bytes32 neg_y in
110113 let new_octets = "\x04" ^ x_bytes ^ neg_y_bytes in
111111- match Ec.of_octets new_octets with
114114+ match Dsa.pub_of_octets new_octets with
112115 | Ok p -> p
113116 | Error _ -> failwith "negate: invalid result"
114117115118 let err_ec e = Error (Fmt.str "%a" Crypto_ec.pp_error e)
116119117120 let of_bytes s =
118118- match Ec.of_octets s with Ok p -> Ok p | Error e -> err_ec e
121121+ match Dsa.pub_of_octets s with Ok p -> Ok p | Error e -> err_ec e
119122120123 (** Convert a scalar represented as Z.t to the constant-time scalar type *)
121124 let scalar_of_z z =
122125 let z = Z.erem z order in
123126 let z = if Z.lt z Z.zero then Z.add z order else z in
124127 let bytes = z_to_bytes32 z in
125125- match Ec.scalar_of_octets bytes with
128128+ match Dsa.priv_of_octets bytes with
126129 | Ok s -> s
127130 | Error _ -> (
128131 (* If scalar is 0, use 1 instead (edge case) *)
129129- match Ec.scalar_of_octets (z_to_bytes32 Z.one) with
132132+ match Dsa.priv_of_octets (z_to_bytes32 Z.one) with
130133 | Ok s -> s
131134 | Error _ -> failwith "scalar_of_z: cannot create scalar")
132135···134137 let random_scalar () =
135138 let rec try_generate () =
136139 let bytes = Crypto_rng.generate 32 in
137137- match Ec.scalar_of_octets bytes with
140140+ match Dsa.priv_of_octets bytes with
138141 | Ok s -> s
139142 | Error _ -> try_generate ()
140143 in
···145148146149type state = {
147150 role : role;
148148- w : P256.Ec.scalar;
149149- scalar : P256.Ec.scalar;
151151+ w : P256.Dsa.priv;
152152+ scalar : P256.Dsa.priv;
150153 my_share : string;
151154}
152155···233236 type prover_state = {
234237 w0 : string;
235238 w1 : string;
236236- x : P256.Ec.scalar;
239239+ x : P256.Dsa.priv;
237240 pa : string;
238241 context : string;
239242 }
···241244 type verifier_state = {
242245 w0 : string;
243246 l : string;
244244- y : P256.Ec.scalar;
247247+ y : P256.Dsa.priv;
245248 pb : string;
246249 context : string;
247250 }
+1-1
lib/spake2.mli
···95959696 (** {2 Types} *)
97979898- type point = Crypto_ec.P256.Point.point
9898+ type point = Crypto_ec.P256.Dsa.pub
9999 (** Opaque point type from mirage-crypto-ec. *)
100100101101 (** {2 Constants} *)