1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889(*
* Copyright (c) 2017 Christiano F. Haesbaert <haesbaert@haesbaert.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)openMirage_crypto.Hashtypet=|Plaintext|Md5|Md5_96|Sha1|Sha1_96|Sha2_256|Sha2_512typekey={hmac:t;(* Hmac algorithm *)key:Cstruct.t;(* The actual hmac key *)}letto_string=function|Plaintext->"none"|Md5->"hmac-md5"|Md5_96->"hmac-md5-96"|Sha1->"hmac-sha1"|Sha1_96->"hmac-sha1-96"|Sha2_256->"hmac-sha2-256"|Sha2_512->"hmac-sha2-512"letof_string=function|"none"->OkPlaintext|"hmac-md5"->OkMd5|"hmac-md5-96"->OkMd5_96|"hmac-sha1"->OkSha1|"hmac-sha1-96"->OkSha1_96|"hmac-sha2-256"->OkSha2_256|"hmac-sha2-512"->OkSha2_512|s->Error("Unknown mac "^s)letdigest_len=function|Plaintext->0|Md5->MD5.digest_size|Md5_96->12|Sha1->SHA1.digest_size|Sha1_96->12|Sha2_256->SHA256.digest_size|Sha2_512->SHA512.digest_sizeletkey_len=function|Plaintext->0|Md5->MD5.digest_size|Md5_96->MD5.digest_size|Sha1->SHA1.digest_size|Sha1_96->SHA1.digest_size|Sha2_256->SHA256.digest_size|Sha2_512->SHA512.digest_sizeletknowns=Result.is_ok(of_strings)letpreferred=[Md5;Sha1;Sha2_256;Sha2_512;Sha1_96;Md5_96]lethmacvhmac~keydata=lettake_96buf=ifCstruct.lengthbuf<12thenfailwith"digest is too short."elseCstruct.subbuf012inmatchhmacwith|Plaintext->Cstruct.create0|Md5->MD5.hmaci~key(funf->List.iterfdata)|Md5_96->MD5.hmaci~key(funf->List.iterfdata)|>take_96|Sha1->SHA1.hmaci~key(funf->List.iterfdata)|Sha1_96->SHA1.hmaci~key(funf->List.iterfdata)|>take_96|Sha2_256->SHA256.hmaci~key(funf->List.iterfdata)|Sha2_512->SHA512.hmaci~key(funf->List.iterfdata)