Require sha.sha.
Local Open Scope logic.
(*TODO: eliminate*)
Ltac canon_load_result ::= idtac.
(* Lemma eval_cast_tuchar_of_isbyteZ q: isbyteZ q -> eval_cast tuchar tuchar (Vint (Int.repr q)) = Vint (Int.repr q).  Lemma Znth_map_Vint_is_int_I8: forall l (i : Z) (F: Forall isbyteZ l), 0 <= i < Zlength l -> is_int I8 Unsigned (Znth i (map Vint (map Int.repr l))).  Lemma Znth_map_Vint_is_int_I8': forall l (i : Z) , 0 <= i < Zlength l -> is_int I8 Unsigned (Znth i (map Vint (map Int.repr (map Byte.unsigned l)))). *)
(*Definition postResetHMS (iS oS: s256state): hmacstate := (emptySha, (iS, oS)).*)
Definition postResetHMS (iS oS: s256state): hmacstate :=
(default_val t_struct_SHA256state_st, (iS, oS)).
Definition initPostResetConditional r (c:val) (k: val) h wsh sh key iS oS: mpred:=
match k with
Vint z => if Int.eq z Int.zero
then if zeq r Z0 then hmacstate_PreInitNull wsh key h c else FF
else FF
| Vptr b ofs => if zeq r 0 then FF
else ((data_at wsh t_struct_hmac_ctx_st (postResetHMS iS oS) c) *
(data_at sh (tarray tuchar (Zlength key)) (map Vubyte key) (Vptr b ofs)))
| _ => FF
end.

Lemma ipad_loop Espec pb pofs cb cofs ckb ckoff kb kofs l key gv (FR:mpred): forall
(IPADcont : list val)
(HeqIPADcont : IPADcont =
map Vubyte
(HMAC_SHA256.mkArg (HMAC_SHA256.mkKey key) Ipad))
(ZLI : Zlength
(HMAC_SHA256.mkArg (HMAC_SHA256.mkKey key) Ipad) =
64),
@semax CompSpecs Espec
(func_tycontext f_HMAC_Init HmacVarSpecs HmacFunSpecs nil)
(PROP  ()
LOCAL  (temp _reset (Vint (Int.repr 1));
lvar _ctx_key (Tarray tuchar 64 noattr) (Vptr ckb ckoff);
lvar _pad (Tarray tuchar 64 noattr) (Vptr pb pofs);
temp _ctx (Vptr cb cofs); temp _key (Vptr kb kofs);
temp _len (Vint (Int.repr l)); gvars gv)
SEP  (FR;
data_at Tsh (Tarray tuchar 64 noattr)
(default_val (Tarray tuchar 64 noattr)) (Vptr pb pofs);
data_at Tsh (tarray tuchar 64)
(map Vubyte (HMAC_SHA256.mkKey key)) (Vptr ckb ckoff)))
(Sfor (Sset _i (Econst_int (Int.repr 0) tint))
(Ebinop Olt (Etempvar _i tint) (Econst_int (Int.repr 64) tint) tint)
(Ssequence
(Sset _aux
(Ecast
(Ederef
(Ebinop Oadd (Evar _ctx_key (Tarray tuchar 64 noattr))
(Etempvar _i tint) (tptr tuchar)) tuchar) tuchar))
(Ssequence
(Sset _aux
(Ecast
(Ebinop Oxor (Econst_int (Int.repr 54) tint)
(Etempvar _aux tuchar) tint) tuchar))
(Sassign
(Ederef
(Ebinop Oadd (Evar _pad (Tarray tuchar 64 noattr))
(Etempvar _i tint) (tptr tuchar)) tuchar)
(Etempvar _aux tuchar))))
(Sset _i
(Ebinop Oadd (Etempvar _i tint) (Econst_int (Int.repr 1) tint) tint)))
(normal_ret_assert
(PROP  ()
LOCAL  (temp _reset (Vint (Int.repr 1));
lvar _ctx_key (Tarray tuchar 64 noattr) (Vptr ckb ckoff);
lvar _pad (Tarray tuchar 64 noattr) (Vptr pb pofs);
temp _ctx (Vptr cb cofs); temp _key (Vptr kb kofs);
temp _len (Vint (Int.repr l)); gvars gv)
SEP  (FR;
data_at Tsh (Tarray tuchar 64 noattr) IPADcont (Vptr pb pofs);
data_at Tsh (tarray tuchar 64)
(map Vubyte (HMAC_SHA256.mkKey key)) (Vptr ckb ckoff)))).