···31313232(* bits2int for qbits=256 (leftmost 256 bits is whole 32 bytes here) *)
3333let bits2int_256 (bs : bytes) : Z.t =
3434- (* If bs > 32 bytes (not the case here), we'd truncate *)
3434+ (* if bs > 32 bytes (not the case here), we'd truncate *)
3535 let len = Bytes.length bs in
3636 let take = if len <= 32 then len else 32 in
3737 let acc = ref Z.zero in
···5050 let z2 = Z.(z1 mod q) in
5151 z_to_bytes32 z2
52525353-(* hmac sha256 using only hash function *)
5454-let hmac_sha256 ~(hash : bytes -> bytes) ~(key : bytes) (data : bytes) : bytes =
5555- let block_size = 64 in
5656- let key0 = if Bytes.length key > block_size then hash key else key in
5757- let key_block =
5858- if Bytes.length key0 = block_size then key0
5959- else
6060- let b = Bytes.make block_size '\x00' in
6161- Bytes.blit key0 0 b 0 (Bytes.length key0) ;
6262- b
6363- in
6464- let ipad = 0x36 and opad = 0x5c in
6565- let inner_pad = Bytes.create block_size
6666- and outer_pad = Bytes.create block_size in
6767- for i = 0 to block_size - 1 do
6868- let kc = Char.code (Bytes.get key_block i) in
6969- Bytes.set inner_pad i (Char.chr (kc lxor ipad)) ;
7070- Bytes.set outer_pad i (Char.chr (kc lxor opad))
7171- done ;
7272- let concat a b =
7373- let out = Bytes.create (Bytes.length a + Bytes.length b) in
7474- Bytes.blit a 0 out 0 (Bytes.length a) ;
7575- Bytes.blit b 0 out (Bytes.length a) (Bytes.length b) ;
7676- out
7777- in
7878- let inner = hash (concat inner_pad data) in
7979- hash (concat outer_pad inner)
8080-8153(* returns 32-byte k for given order q *)
8254let rfc6979_k_256_bytes ~(q : Z.t) ~(privkey : bytes) ~(msg : bytes) : bytes =
8355 if Bytes.length privkey <> 32 then invalid_arg "privkey must be 32 bytes" ;
8456 let x = bytes32_to_z privkey in
8557 if x <= Z.zero || x >= q then invalid_arg "privkey scalar out of range" ;
8686- let module H = Hacl_star.Hacl.SHA2_256 in
8787- let hash = H.hash in
8888- let hmac = hmac_sha256 ~hash in
5858+ let hash = Hacl_star.Hacl.SHA2_256.hash in
8959 let h1 = hash msg in
9060 (* 32-byte SHA-256 digest *)
9161 let x_octets = privkey in
···10474 0 parts ;
10575 out
10676 in
7777+ let hmac k v =
7878+ Digestif.SHA256.(hmac_bytes ~key:(Bytes.to_string k) v |> to_raw_string)
7979+ |> Bytes.of_string
8080+ in
10781 (* step: K = HMAC_K(V || 0x00 || x || h1); V = HMAC_K(V) *)
108108- let k = hmac ~key:k (concat [v; Bytes.of_string "\x00"; x_octets; h1_red]) in
109109- let v = hmac ~key:k v in
8282+ let k = hmac k (concat [v; Bytes.of_string "\x00"; x_octets; h1_red]) in
8383+ let v = hmac k v in
11084 (* step: K = HMAC_K(V || 0x01 || x || h1); V = HMAC_K(V) *)
111111- let k = hmac ~key:k (concat [v; Bytes.of_string "\x01"; x_octets; h1_red]) in
112112- let v = hmac ~key:k v in
8585+ let k = hmac k (concat [v; Bytes.of_string "\x01"; x_octets; h1_red]) in
8686+ let v = hmac k v in
11387 (* loop *)
11488 let rec loop k v =
11589 (* a. V = HMAC_K(V) *)
116116- let v = hmac ~key:k v in
9090+ let v = hmac k v in
11791 let t = v in
11892 let k_candidate = bits2int_256 t in
11993 if Z.(k_candidate >= one && k_candidate < q) then t
12094 else
12195 (* K = HMAC_K(V || 0x00); V = HMAC_K(V) *)
122122- let k = hmac ~key:k (concat [v; Bytes.of_string "\x00"]) in
123123- let v = hmac ~key:k v in
9696+ let k = hmac k (concat [v; Bytes.of_string "\x00"]) in
9797+ let v = hmac k v in
12498 loop k v
12599 in
126100 loop k v