Module HMAC_SHA256 := HP.HMAC_FUN SHA256.
Definition Ipad := Byte.repr 54. (*0x36*)
Definition Opad := Byte.repr 92. (*0x5c*)
Definition HMAC256 := HMAC_SHA256.HMAC Ipad Opad.
Definition HMACString (txt passwd:string): list byte :=
HMAC256 (str_to_bytes txt) (str_to_bytes passwd).
Definition HMACHex (text password:string): list byte :=
HMAC256 (hexstring_to_bytelist text) (hexstring_to_bytelist password).
Definition check password text digest :=
bytelist_eq (HMACString text password) (hexstring_to_bytelist digest) = true.
(*a random example, solution obtained via http://www.freeformatter.com/hmac-generator.html#ad-output*)
Goal check "bb" "aa"
"c1201d3dccfb84c069771d07b3eda4dc26e5b34a4d8634b2bba84fb54d11e265".
vm_compute. reflexivity. Qed.
vm_compute. reflexivity. Qed.
Definition checkHex password text digest :=
bytelist_eq (HMACHex text password) (hexstring_to_bytelist digest) = true.
vm_compute. reflexivity. Qed.
vm_compute. reflexivity. Qed.
vm_compute. reflexivity. Qed.
vm_compute. reflexivity. Qed.
(*Test with a truncation of output to 128 bits, ie the first 16 bytes*)
Definition checkHexTrunc password text digest :=
bytelist_eq (firstn 16 (HMACHex text password)) (hexstring_to_bytelist digest) = true.
vm_compute. reflexivity. Qed.
vm_compute. reflexivity. Qed.
vm_compute. reflexivity. Qed.

Lemma RFC6868_example4_2hex:
checkHex "4a656665"
"7768617420646f2079612077616e7420666f72206e6f7468696e673f"
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843".