forked from PrincetonUniversity/VST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverif_hmac_init_part1.v
568 lines (524 loc) · 24 KB
/
verif_hmac_init_part1.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
Require Import floyd.proofauto.
Import ListNotations.
Require sha.sha.
Require Import sha.SHA256.
Local Open Scope logic.
Require Import sha.spec_sha.
Require Import sha.sha_lemmas.
Require Import sha.HMAC_functional_prog.
Require Import sha.HMAC256_functional_prog.
Require Import sha.hmac091c.
Require Import sha.vst_lemmas.
Require Import sha.hmac_pure_lemmas.
Require Import sha.hmac_common_lemmas.
Require Import sha.spec_hmac.
Definition initPostKeyNullConditional r (c:val) (k: val) h key : mpred:=
match k with
Vint z => if Int.eq z Int.zero
then if zeq r Z0 then hmacstate_PreInitNull key h c else FF
else FF
| Vptr b ofs => if zeq r 0 then FF
else !!(Forall isbyteZ key) &&
((data_at Tsh t_struct_hmac_ctx_st (keyedHMS key) c) *
(data_at Tsh (tarray tuchar (Zlength key)) (map Vint (map Int.repr key))
(Vptr b ofs)))
| _ => FF
end.
Lemma mkKey_left' {d d'}: forall l (L: Zlength l <= 64)
i (I: 0<= i < Zlength l),
nth (Z.to_nat i) (HMAC_SHA256.mkKey l) d = nth (Z.to_nat i) l d'.
Proof.
unfold HMAC_SHA256.mkKey. intros. simpl.
rewrite Z.gtb_ltb. rewrite Fcore_Zaux.Zlt_bool_false by omega.
apply nth_zeropad_left; trivial.
Qed.
Lemma mkKey_right' {d}: forall l (L: Zlength l <= 64)
i (I: Zlength l <= i < 64),
nth (Z.to_nat i) (HMAC_SHA256.mkKey l) d = Z0.
Proof.
unfold HMAC_SHA256.mkKey. intros. simpl.
rewrite Z.gtb_ltb. rewrite Fcore_Zaux.Zlt_bool_false by omega.
apply nth_zeropad_right; trivial.
Qed.
Lemma hmac_init_part1: forall
(Espec : OracleKind)
(c : val)
(k : val)
(l : Z)
(key : list Z)
(kv : val)
(h1:hmacabs)
(KL1 : l = Zlength key)
(KL2 : 0 <= l <= Int.max_signed)
(KL3 : l * 8 < two_p 64)
(ctx' : name _ctx)
(key' : name _key)
(len' : name _len)
(pad : val)
(Delta := (initialized _reset
(func_tycontext f_HMAC_Init HmacVarSpecs HmacFunSpecs)))
(PostKeyNull : environ -> mpred)
(HeqPostKeyNull : PostKeyNull =
EX cb : block,
(EX cofs : int,
(EX r : Z,
PROP (c = Vptr cb cofs /\ (r=0 \/ r=1))
LOCAL (temp _reset (Vint (Int.repr r));
lvar _pad (tarray tuchar 64) pad; temp _ctx c; temp _key k;
temp _len (Vint (Int.repr l));
gvar sha._K256 kv)
SEP (`(data_at_ Tsh (tarray tuchar 64) pad);
`(initPostKeyNullConditional r c k h1 key);
`(K_vector kv))))),
@semax Espec Delta
(PROP ()
LOCAL (temp _reset (Vint (Int.repr 0)); lvar _pad (tarray tuchar 64) pad;
temp _ctx c; temp _key k; temp _len (Vint (Int.repr l));
gvar sha._K256 kv)
SEP
(`(data_at_ Tsh (tarray tuchar 64) pad);
`(K_vector kv); `(initPre c k h1 key)))
(Sifthenelse
(Ebinop One (Etempvar _key (tptr tuchar))
(Ecast (Econst_int (Int.repr 0) tint) (tptr tvoid)) tint)
(Ssequence (Sset _reset (Econst_int (Int.repr 1) tint))
(Ssequence (Sset _j (Econst_int (Int.repr 64) tint))
(Sifthenelse
(Ebinop Olt (Etempvar _j tint) (Etempvar _len tint) tint)
(Ssequence
(Scall None
(Evar _SHA256_Init
(Tfunction
(Tcons (tptr t_struct_SHA256state_st) Tnil)
tvoid cc_default))
[Eaddrof
(Efield
(Ederef
(Etempvar _ctx (tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _md_ctx
t_struct_SHA256state_st)
(tptr t_struct_SHA256state_st)])
(Ssequence
(Scall None
(Evar _SHA256_Update
(Tfunction
(Tcons (tptr t_struct_SHA256state_st)
(Tcons (tptr tvoid) (Tcons tuint Tnil)))
tvoid cc_default))
[Eaddrof
(Efield
(Ederef
(Etempvar _ctx (tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _md_ctx
t_struct_SHA256state_st)
(tptr t_struct_SHA256state_st);
Etempvar _key (tptr tuchar); Etempvar _len tint])
(Ssequence
(Scall None
(Evar _SHA256_Final
(Tfunction
(Tcons (tptr tuchar)
(Tcons (tptr t_struct_SHA256state_st)
Tnil)) tvoid cc_default))
[Efield
(Ederef
(Etempvar _ctx (tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _key
(tarray tuchar 64);
Eaddrof
(Efield
(Ederef
(Etempvar _ctx
(tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _md_ctx
t_struct_SHA256state_st)
(tptr t_struct_SHA256state_st)])
(Ssequence
(Scall None
(Evar _memset
(Tfunction
(Tcons (tptr tvoid)
(Tcons tint (Tcons tuint Tnil)))
(tptr tvoid) cc_default))
[Eaddrof
(Ederef
(Ebinop Oadd
(Efield
(Ederef
(Etempvar _ctx
(tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _key
(tarray tuchar 64))
(Econst_int (Int.repr 32) tint)
(tptr tuchar)) tuchar) (tptr tuchar);
Econst_int (Int.repr 0) tint;
Econst_int (Int.repr 32) tint])
(Sassign
(Efield
(Ederef
(Etempvar _ctx
(tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _key_length tuint)
(Econst_int (Int.repr 32) tint))))))
(Ssequence
(Scall None
(Evar _memcpy
(Tfunction
(Tcons (tptr tvoid)
(Tcons (tptr tvoid) (Tcons tuint Tnil)))
(tptr tvoid) cc_default))
[Efield
(Ederef (Etempvar _ctx (tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _key (tarray tuchar 64);
Etempvar _key (tptr tuchar); Etempvar _len tint])
(Ssequence
(Scall None
(Evar _memset
(Tfunction
(Tcons (tptr tvoid)
(Tcons tint (Tcons tuint Tnil)))
(tptr tvoid) cc_default))
[Eaddrof
(Ederef
(Ebinop Oadd
(Efield
(Ederef
(Etempvar _ctx
(tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _key
(tarray tuchar 64))
(Etempvar _len tint) (tptr tuchar)) tuchar)
(tptr tuchar); Econst_int (Int.repr 0) tint;
Ebinop Osub (Econst_int (Int.repr 64) tuint)
(Etempvar _len tint) tuint])
(Sassign
(Efield
(Ederef
(Etempvar _ctx (tptr t_struct_hmac_ctx_st))
t_struct_hmac_ctx_st) _key_length tuint)
(Etempvar _len tint))))))) Sskip)
(normal_ret_assert PostKeyNull).
Proof. intros.
subst PostKeyNull.
abbreviate_semax.
forward_if.
{ (* THEN*)
simpl.
unfold initPre.
assert_PROP (isptr k). {
entailer!. hnf in TC0. destruct key'; try contradiction.
subst i; contradiction H; reflexivity. auto.
}
make_Vptr k. clear H H0. rename b into kb; rename i into kofs.
normalize.
assert_PROP (Forall isbyteZ key).
{ unfold data_block. entailer. }
rename H into isbyte_key.
unfold data_block; rewrite prop_true_andp by auto.
forward v. subst v.
forward. (*j=HMAC_MAX_MD_CBLOCK*)
simpl.
forward_if.
{ (* j < len*)
rename H into lt_64_l.
(*call to SHA256_init*)
unfold data_at_ at 1. simpl default_val.
unfold_data_at 1%nat.
rewrite (field_at_data_at Tsh t_struct_hmac_ctx_st [StructField _md_ctx]); try reflexivity.
assert_PROP (field_compatible t_struct_hmac_ctx_st [StructField _md_ctx] c).
entailer!. rename H into FC.
make_Vptr c. rename b into cb; rename i into cofs.
unfold field_address; rewrite if_true by auto.
normalize.
forward_call' (Vptr cb cofs).
(*call to SHA256_Update*)
rewrite (split_offset_array_at (Z.to_nat l))
by (try reflexivity; subst l; rewrite ?Zlength_map, Z2Nat.id; omega).
rewrite firstn_same
by (subst l; rewrite !map_length, Zlength_correct, Nat2Z.id; omega).
normalize. simpl in H, H0. rewrite Zplus_0_r in H.
rename H into OIR_kofs. rename H0 into OIR_kofs_key.
replace_SEP 0 (`(data_block Tsh key (Vptr kb kofs))).
unfold data_block. rewrite prop_true_andp by auto. rewrite Z2Nat.id by omega. entailer!.
forward_call' (init_s256abs, key, Vptr cb cofs,
Vptr kb kofs, Tsh, l, kv) ctxSha.
{ subst l. split. omega.
split. specialize Int.max_signed_unsigned. omega.
apply KL3.
}
simpl.
replace_SEP 3 (`emp).
{ entailer.
rewrite <- Zlength_correct. rewrite Z.sub_diag.
eapply derives_trans; try eapply data_at_data_at_.
rewrite data_at__memory_block; [ | reflexivity | simpl; omega].
rewrite memory_block_zero_Vptr.
entailer.
}
rename H into updAbs.
(*call Final*)
rewrite (field_at_data_at Tsh t_struct_hmac_ctx_st [StructField _key]) by reflexivity.
simpl.
(*now:*) unfold tarray. rewrite (data_at_Tarray_split3 Tsh tuchar 64 noattr 32) by omega.
change (force_lengthn (nat_of_Z 32) [] (default_val tuchar) ++
Znth 32 [] (default_val tuchar) :: skipn (nat_of_Z (32 + 1)) [])
with (list_repeat 33 Vundef).
fold (tarray tuchar 64).
rewrite (split_offset_array_at 32) by (try reflexivity; rewrite ?Zlength_correct; simpl; omega).
change (firstn 32 (list_repeat 33 Vundef)) with (list_repeat 32 Vundef).
change (skipn 32 (list_repeat 33 Vundef)) with [Vundef].
normalize.
rename H into BND1; rename H0 into BND2.
(*Call to _SHA256_Final*)
forward_call' (ctxSha, (field_address t_struct_hmac_ctx_st [StructField _key] (Vptr cb cofs)),
Vptr cb cofs, Tsh, kv).
entailer!. unfold field_address; rewrite if_true by auto. reflexivity.
normalize.
(*call memset*)
forward_call' (Tsh, (offset_val (Int.repr (Z.of_nat 32))
(field_address t_struct_hmac_ctx_st [StructField _key]
(Vptr cb cofs))), 32, Int.zero) v.
entailer!. unfold field_address; rewrite if_true by auto.
normalize; reflexivity.
normalize. subst v.
forward. (*ctx->key_length=32*)
apply exp_right with cb; apply exp_right with cofs; apply exp_right with 1.
unfold initPostKeyNullConditional.
rewrite if_false by omega.
unfold data_block.
set (z32 := (list_repeat (Z.to_nat 32) (Vint Int.zero))).
change (Z.of_nat 32) with 32%Z.
entailer.
fold (tarray tuchar 64).
unfold_data_at 4%nat.
rewrite if_true by omega.
rewrite (field_at_data_at Tsh t_struct_hmac_ctx_st [StructField _key]); try reflexivity.
cancel.
simpl.
unfold tarray. rewrite (data_at_Tarray_split3 Tsh tuchar 64 noattr 32) by omega.
fold (tarray tuchar 64).
rewrite (split_offset_array_at 32); try reflexivity.
Focus 2. rewrite Zlength_correct, app_length, force_lengthn_length_n, Nat2Z.inj_add, nat_of_Z_eq.
assert (TT: Z.of_nat 32=32) by reflexivity. rewrite TT; clear TT. omega.
omega.
2: simpl; omega.
entailer.
rewrite firstn_app1
by(rewrite force_lengthn_length_n; simpl; omega).
rewrite firstn_same
by(rewrite force_lengthn_length_n; simpl; omega).
assert (LengthShaFinish: Zlength (sha_finish ctxSha) = 32).
unfold sha_finish. destruct ctxSha.
rewrite <- functional_prog.SHA_256'_eq, Zlength_correct, length_SHA256'. reflexivity.
rewrite LengthShaFinish.
change (nat_of_Z 32) with 32%nat.
change (64 - Z.of_nat 32) with 32.
rewrite skipn_force_lengthn_app.
rewrite (field_at_data_at Tsh t_struct_hmac_ctx_st [StructField _md_ctx]); try reflexivity.
fold (tarray tuchar 32).
unfold HMAC_SHA256.mkKey.
assert (Z6: Zlength key > 64) by omega.
apply Zgt_is_gt_bool in Z6.
change (Z.of_nat SHA256.BlockSize) with 64. rewrite Z6.
match goal with |- _ |-- _ * data_at Tsh (tarray tuchar 32) ?A _ =>
replace A with z32
end.
Focus 2. {
unfold HMAC_SHA256.zeroPad.
unfold SHA256.Hash. rewrite length_SHA256'.
change (SHA256.BlockSize - SHA256.DigestLength)%nat with 32%nat.
rewrite !skipn_map.
rewrite skipn_app2
by (rewrite length_SHA256'; compute; omega).
unfold Znth.
rewrite if_false by omega.
rewrite !map_app.
rewrite app_nth2 by (rewrite !map_length, length_SHA256'; compute; omega).
rewrite !map_length, length_SHA256'.
reflexivity.
} Unfocus.
change (Z.of_nat 32) with 32.
fold (tarray tuchar 32).
assert (sha_finish ctxSha ++ list_repeat 32 0 = HMAC_SHA256.zeroPad (SHA256.Hash key)). {
clear - lt_64_l H2 updAbs.
inv updAbs.
unfold sha_finish. simpl. rewrite <- H8; simpl.
rewrite firstn_same by (rewrite Zlength_correct, Nat2Z.id; auto).
unfold HMAC_SHA256.zeroPad.
unfold SHA256.Hash. rewrite length_SHA256'.
f_equal. rewrite functional_prog.SHA_256'_eq; auto.
}
rewrite <- H8.
assert (length (sha_finish ctxSha) = 32)%nat
by (apply Nat2Z.inj; rewrite <- Zlength_correct, LengthShaFinish; auto).
rewrite force_lengthn_long
by (rewrite !map_length, app_length, length_list_repeat; omega).
rewrite !firstn_map.
rewrite firstn_app1 by omega.
rewrite firstn_same by omega.
cancel.
unfold field_address; rewrite if_true by auto.
simpl offset_val; normalize.
}
{ (* j >= len*)
rename H into ge_64_l.
(*call to memcpy*)
focus_SEP 0 2.
unfold data_at_.
unfold_data_at 1%nat.
rewrite (field_at_data_at Tsh t_struct_hmac_ctx_st [StructField _key]); try reflexivity.
(* assert_PROP (field_compatible t_struct_hmac_ctx_st [StructField _key] c).
entailer!.
rename H into FCKey.
make_Vptr c. rename b into cb; rename i into cofs.*)
(* focus_SEP 1 5.*)
simpl.
unfold tarray.
remember (`(data_at Tsh (Tarray tuchar 64 noattr) [] pad)) as ZZ.
rewrite (data_at_Tarray_split3a Tsh tuchar 64 noattr l). 2: omega.
simpl.
fold (tarray tuchar 64). rewrite (split_offset_array_at (Z.to_nat l)); try reflexivity.
Focus 2. rewrite Zlength_correct, app_length, force_lengthn_length_n. simpl.
rewrite Nat2Z.inj_add. repeat rewrite nat_of_Z_eq; omega.
2:rewrite Z2Nat.id; omega.
subst ZZ.
normalize.
rename H into OIR0_328. rename H0 into OIR64_328.
rewrite firstn_app1. 2: rewrite force_lengthn_length_n; trivial.
rewrite firstn_precise. 2: rewrite force_lengthn_length_n; trivial.
rewrite skipn_app2. 2: rewrite force_lengthn_length_n; unfold nat_of_Z; omega.
rewrite force_lengthn_length_n.
assert (X: (Z.to_nat l - nat_of_Z l = 0)%nat).
unfold nat_of_Z. omega.
rewrite X, skipn_0, Z2Nat.id; clear X. 2: omega.
assert (NL: nat_of_Z l = length key).
{ subst l; unfold nat_of_Z. rewrite Zlength_correct, Nat2Z.id. trivial. }
rewrite NL.
remember (64 - l) as l64.
assert (HH: match Z.max 0 (Zlength key) with
| 0 => 0
| Z.pos y' => Z.pos y'
| Z.neg y' => Z.neg y'
end = Zlength key).
rewrite Z.max_r by omega. destruct (Zlength key); auto; omega.
rewrite KL1 in *.
(*Call to memcpy*)
forward_call' ((Tsh, Tsh), (field_address t_struct_hmac_ctx_st [StructField _key] c),
Vptr kb kofs, mkTrep (Tarray tuchar (Zlength key) noattr)
(map Vint (map Int.repr key))) v.
{ rewrite HH. entailer!.
unfold field_address; rewrite if_true by auto; reflexivity. }
{ simpl; split; auto. rewrite HH. repable_signed. }
subst v; normalize. simpl. clear HH.
assert (l64 <= Int.max_unsigned) by MyOmega.
(*call memset*)
remember (map Vint (map Int.repr key)) as CONT.
forward_call' (Tsh, offset_val (Int.repr (Zlength key)) (field_address t_struct_hmac_ctx_st [StructField _key] c), l64, Int.zero) v.
{ entailer!. unfold field_address; rewrite if_true by auto.
f_equal. rewrite Zplus_comm. unfold nested_field_offset2. simpl.
rewrite offset_offset_val. rewrite add_repr. rewrite Zplus_comm. reflexivity.
(*f_equal. rewrite add_repr. reflexivity.*) }
{ replace (Int.repr l64) with (Int.repr (sizeof (tarray tuchar l64)))
by (rewrite sizeof_tarray_tuchar; auto; omega).
assert (match Z.max 0 l64 with
| 0 => 0
| Z.pos y' => Z.pos y'
| Z.neg y' => Z.neg y'
end = l64).
rewrite Z.max_r by omega. destruct l64; reflexivity.
cancel. }
{ simpl; split; auto. omega. }
simpl map.
clear H.
forward. (*ctx->key_length=len*)
assert_PROP (isptr c); [entailer! |].
make_Vptr c. rename b into cb; rename i into cofs; clear H.
apply exp_right with cb; apply exp_right with cofs; apply exp_right with 1.
unfold initPostKeyNullConditional. rewrite zeq_false by omega.
entailer. cancel.
unfold_data_at 3%nat. rewrite if_false by omega.
cancel.
(*clear H0 TC0 h1.*)
rewrite (field_at_data_at Tsh t_struct_hmac_ctx_st [StructField _key]); try reflexivity.
remember (64 - Zlength key) as ZK64.
simpl.
unfold tarray.
rewrite (data_at_Tarray_split3a Tsh tuchar 64 noattr (Zlength key)) by omega.
simpl. unfold nat_of_Z. rewrite Zlength_correct, Nat2Z.id.
fold (tarray tuchar 64).
rewrite (split_offset_array_at (length key) Tsh tuchar 64); try reflexivity.
2: rewrite Zlength_correct, app_length, force_lengthn_length_n, Nat2Z.inj_add; omega.
2: rewrite Zlength_correct in ge_64_l; omega.
entailer.
rewrite firstn_app1 by (rewrite force_lengthn_length_n; trivial).
rewrite firstn_precise by (rewrite force_lengthn_length_n; trivial).
rewrite sepcon_comm.
apply sepcon_derives.
{ apply data_at_Tarray_ext_derives. intros i I.
apply data_at_triv. unfold Znth. rewrite if_false by omega.
rewrite nth_force_lengthn.
Focus 2. split. omega. destruct I as [Ipos I]. apply Z2Nat.inj_lt in I; trivial.
rewrite Nat2Z.id in I. trivial. omega.
assert (Z32: (Z.to_nat i < length key)%nat).
clear - I; destruct I as [XX YY]. rewrite Z2Nat.inj_lt in YY.
rewrite Nat2Z.id in YY; trivial. trivial. omega.
apply eq_sym.
rewrite if_false by omega.
assert (L1: (Z.to_nat i < length (HMAC_SHA256.mkKey key))%nat).
rewrite mkKey_length; unfold SHA256.BlockSize.
assert (Zlength key <= 64) by omega. apply Z2Nat.inj_le in H10. simpl in H10.
rewrite Zlength_correct, Nat2Z.id in H10. omega.
omega. omega.
rewrite nth_map' with (d':=Int.zero) by (rewrite map_length; trivial).
rewrite nth_map' with (d':=Int.zero) by (rewrite map_length; trivial).
rewrite nth_map' with (d':=Z0) by trivial.
rewrite nth_map' with (d':=Z0) by trivial.
f_equal. f_equal.
eapply mkKey_left'; auto; try omega. rewrite Zlength_correct. trivial.
}
{ rewrite skipn_force_lengthn_app.
rewrite Zlength_correct.
apply data_at_Tarray_ext_derives. intros i I.
apply data_at_triv. unfold Znth. rewrite if_false by omega.
rewrite if_false by omega.
rewrite nth_indep with (d:=(default_val tuchar))(d':=Vint (Int.repr 0))
by (rewrite length_list_repeat; apply Z2Nat.inj_lt; omega).
rewrite nth_list_repeat.
rewrite if_false by omega.
rewrite nth_map' with (d':=Int.zero)
by (
rewrite map_length, mkKey_length, Nat2Z.id; unfold SHA256.BlockSize; omega).
rewrite nth_map' with (d':=Z0)
by (rewrite mkKey_length, Nat2Z.id; unfold SHA256.BlockSize; omega).
remember (Z.to_nat i) as K; destruct K; simpl.
rewrite mkKey_right'; try omega. auto. rewrite Zlength_correct; omega.
rewrite nth_skipn.
assert (K + Z.to_nat (Z.of_nat (length key) + 1) = Z.to_nat (Z.of_nat (length key) + i))%nat.
rewrite Z2Nat.inj_add. rewrite Z2Nat.inj_add. rewrite <- HeqK.
remember (Z.to_nat (Z.of_nat (length key))) as u. simpl. rewrite <- plus_n_Sm. rewrite <- (plus_Snm_nSm u). omega.
rewrite <- Zlength_correct. apply Zlength_nonneg.
omega.
rewrite <- Zlength_correct. apply Zlength_nonneg.
omega.
rewrite H10; clear H10. clear K HeqK.
rewrite nth_map' with (d':=Int.zero).
rewrite nth_map' with (d':=Z0); trivial. f_equal. f_equal.
rewrite mkKey_right'; try omega. rewrite Zlength_correct. omega.
rewrite mkKey_length. unfold SHA256.BlockSize. apply (Z2Nat.inj_lt _ 64). omega. omega. omega.
rewrite map_length. rewrite mkKey_length. unfold SHA256.BlockSize. apply (Z2Nat.inj_lt _ 64). omega. omega. omega.
}
}
}
{ (*key == NULL*)
revert POSTCONDITION; subst k; intro.
forward.
unfold initPre, initPostKeyNullConditional. simpl SEPx.
unfold hmacstate_PreInitNull.
assert_PROP (isptr c). entailer.
make_Vptr c. rename b into cb; rename i into cofs; clear H.
apply (exp_right cb). apply (exp_right cofs). apply (exp_right 0).
rewrite if_true by auto.
entailer.
apply (exp_right r). cancel. apply (exp_right v). entailer!.
}
Qed.