upstream: https://github.com/mirage/mirage-crypto
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add Hash.hmac_feed analogous to Hash.feed (#130)

authored by

Reynir Björnsson and committed by
GitHub
60b644fc cb2c2646

+54 -1
+20
src/hash.ml
··· 14 14 val feed : t -> Cstruct.t -> t 15 15 val get : t -> Cstruct.t 16 16 17 + type hmac 18 + 19 + val hmac_empty : key:Cstruct.t -> hmac 20 + val hmac_feed : hmac -> Cstruct.t -> hmac 21 + val hmac_get : hmac -> digest 22 + 17 23 val digest : Cstruct.t -> digest 18 24 val hmac : key:Cstruct.t -> Cstruct.t -> digest 19 25 ··· 72 78 73 79 include Core (F) (D) 74 80 81 + type hmac = t * t 82 + 75 83 let opad = 76 84 let buf = Cstruct.create block_size in 77 85 Cstruct.memset buf 0x5c; ··· 86 94 | 1 -> norm (digest key) 87 95 | -1 -> Cs.rpad key block_size 0 88 96 | _ -> key 97 + 98 + let hmac_empty ~key = 99 + let key = norm key in 100 + let outer = Cs.xor key opad 101 + and inner = Cs.xor key ipad in 102 + feed empty inner, feed empty outer 103 + 104 + let hmac_feed (t, outer) cs = 105 + feed t cs, outer 106 + 107 + let hmac_get (t, outer) = 108 + get (feed outer (get t)) 89 109 90 110 let hmaci ~key iter = 91 111 let key = norm key in
+15
src/mirage_crypto.mli
··· 114 114 val get : t -> digest 115 115 (** [get t] is the digest corresponding to [t]. *) 116 116 117 + (** {1 HMAC operations} *) 118 + 119 + type hmac 120 + (** Represents a running hmac computation in a way suitable for appending 121 + inputs. *) 122 + 123 + val hmac_empty : key:Cstruct.t -> hmac 124 + (** [hmac ~key] is the hmac of the empty string using key [key]. *) 125 + 126 + val hmac_feed : hmac -> Cstruct.t -> hmac 127 + (** [feed hmac msg] is analogous to [feed]. *) 128 + 129 + val hmac_get : hmac -> digest 130 + (** [hmac_get hmac] is the hmac corresponding to [hmac]. *) 131 + 117 132 (** {1 All-in-one} 118 133 119 134 Functions that operate on data stored in a single chunk. *)
+19 -1
tests/test_hmac.ml
··· 221 221 let test_hmac name id = 222 222 List.mapi (fun i args -> "HMAC " ^ name ^ " " ^ string_of_int i >:: test id i args) 223 223 224 + let test_feed hash i ((key, data), result) _ = 225 + let (module H) = Hash.module_of hash in 226 + let empty = H.hmac_empty ~key in 227 + let computed = H.hmac_get (H.hmac_feed empty data) in 228 + if i == 4 (* truncated thingy *) then 229 + assert_cs_equal result Cstruct.(sub computed 0 (len result)) 230 + else 231 + assert_cs_equal result computed 232 + 233 + let test_feed_hmac name id = 234 + List.mapi (fun i args -> "HMAC feed " ^ name ^ " " ^ string_of_int i >:: test_feed id i args) 235 + 224 236 let suite = 225 237 test_hmac "MD5" `MD5 (List.combine md5_inputs md5_results) @ 226 238 test_hmac "SHA1" `SHA1 (List.combine sha1_inputs sha1_results) @ 227 239 test_hmac "SHA224" `SHA224 (List.combine sha2_inputs sha224_results) @ 228 240 test_hmac "SHA256" `SHA256 (List.combine sha2_inputs sha256_results) @ 229 241 test_hmac "SHA384" `SHA384 (List.combine sha2_inputs sha384_results) @ 230 - test_hmac "SHA512" `SHA512 (List.combine sha2_inputs sha512_results) 242 + test_hmac "SHA512" `SHA512 (List.combine sha2_inputs sha512_results) @ 243 + test_feed_hmac "MD5" `MD5 (List.combine md5_inputs md5_results) @ 244 + test_feed_hmac "SHA1" `SHA1 (List.combine sha1_inputs sha1_results) @ 245 + test_feed_hmac "SHA224" `SHA224 (List.combine sha2_inputs sha224_results) @ 246 + test_feed_hmac "SHA256" `SHA256 (List.combine sha2_inputs sha256_results) @ 247 + test_feed_hmac "SHA384" `SHA384 (List.combine sha2_inputs sha384_results) @ 248 + test_feed_hmac "SHA512" `SHA512 (List.combine sha2_inputs sha512_results)