-
Notifications
You must be signed in to change notification settings - Fork 0
/
mitbScript.sml
2686 lines (2520 loc) · 82.7 KB
/
mitbScript.sml
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**********************************************************************)
(* Formalization of a revised version of Robert's MITB state machine *)
(**********************************************************************)
(*
Description of MITB state:
|----------|-----------|-----------|
| control | permanent | volatile |
|----------|-----------|-------++++|
control: two states: Ready, Absorbing;
permanent: 1600-bit requester storing the Keccak-f permution of an
initial 1152-bit key padded with 448 zeros. In the HOL
notation defined below: f(K++(Zeros 448))
volatile: 1600-bit register storing MITB state
The initial manufacturer state:
|---------|-----------|----------|
| Ready |f(K||0...0)| 0 |
|---------|-----------|----------|
- the control state is initially Ready;
- the permanent memory contains the Keccak-f permution of an initial
manufacturer-supplied 1152-bit key padded with 448 zeros. In the
HOL notation defined below: f(K++(Zeros 448));
- the volatile memory contains 1600-bit 0 (Zeros 1600);
Commands (inputs from user/attacker):
- Skip : {Ready -> Ready} + {Absorbing -> Absorbing}
State unchanged (stutter, no-op).
- Move : {Ready->Absorbing} + {Absorbing->Ready}.
In Ready: start to hash a new message.
In Absorbing: abandon absorbing, set vmem to zeros.
- Input bits len : {Ready->Ready} + {Absorbing->{Absorbing,AbsorbEnd,Ready}}.
In Ready installs f(key++(Zeros c)) in permanent memory.
In Absorbing inputs a block and continues absorbing if the block
isn't the last one (indicated by len < r - where r is the bitrate,
1152 for SHA-3). If the block being input is the last one, then
goes into AbsorbEnd (len=r-1) or Ready (len < r-s).
State-transition diagram
|-----------| |-----------|
| | Move | |
| |<-------------->| |
+---| | | |---+
| | | | | |
Input key len | | Ready | | Absorbing | | Input blk len
| | | Input blk len | | | (len = r)
|-->| | (len < r-1) | |<--|
| |<---------------| |
| | | |
|-----+-----| |-----------|
/|\ |
| | Input blk len
| | (len = r-1)
| \|/
| |-----+-----|
| | |
| | |
| | |
| | |
|----------------------| AbsorbEnd |
| |
| |
| |
| |
|-----+-----|
The changes to Robert's original design are:
- added Skip command that does nothing
- old Setup state is now subsumed by Ready;
- added addition state AbsorbEnd for len = r-1 case;
- both key and message block are input using Input command;
- remove the transition (Setup to Ready in old version) that allows
the digest corresponding to a partially hashed key
(e.g. without the padding added) to be read;
- old commands ButtonSetup, ButtonReady roughly correspond to
new Input, Move, respectively;
- Move abandons absorbing, discards vmem memory and moved to Ready;
- explicit outputs now omitted - it is assumed that in the Ready
state the control state and digest (bottom 224 bits of volatle
memory) are displayed.
The changes to Mike's modelling of the MITB are:
- MITB operates on words now. The parameters (r,c,n) are now part of the
types.
*)
open HolKernel;
open Parse;
open boolLib;
open bossLib;
open listTheory;
open rich_listTheory;
open arithmeticTheory;
open Arith;
open numLib;
open computeLib;
open wordsTheory;
open wordsLib;
open uccomTheory;
open spongeTheory;
open lcsymtacs;
(*
check later if we need this
open numposrepTheory;
*)
(**********************************************************************)
(* Start new theory MITB *)
(**********************************************************************)
val _ = new_theory "mitb";
val _ = numLib.prefer_num();
(*
Bit sizes:
digest (n): 224
capacity (c): 448
bitrate (r): 1152 (block and key size)
width (b): 1600 (SHA-3 state size = r+c)
*)
val _ = type_abbrev("bits", ``:bool list``);
(*
Datatype of control states
*)
val _ = Datatype
`control = Ready | Absorbing | AbsorbEnd`;
(*
Datatype of input commands
*)
val _ =
Datatype
`command = Move
| Skip
| Input ('r word) num`;
(*
Type abbreviation for MITB states
*)
val _ =
(* ('c,'r) mitb_state is *)
type_abbrev
("mitb_state",
``:control # ('r+'c) word # ('r+'c) word ``);
(* permanent volatile *)
(*
Type abbreviation for MITB inputs
*)
val _ =
type_abbrev
("mitb_inp",
``:bool # bool # 'r word # num``);
(* skip move block size *)
(*
Type abbreviation for MITB outputs
*)
val _ =
type_abbrev
("mitb_out",
``:bool # 'n word``);
(*
Extract components of an MITB state
*)
val cntlOf_def =
Define
`cntlOf((cntl,pmem,vmem): ('r, 'c) mitb_state) = cntl`;
val pmemOf_def =
Define
`pmemOf((cntl,pmem,vmem): ('r, 'c) mitb_state) = pmem`;
val vmemOf_def =
Define
`vmemOf((cntl,pmem,vmem): ('r, 'c) mitb_state) = vmem`;
(*
Type abbreviation for MITB device
Given a permutation on b=r+c words, moves from one state, via a command
to another state
*)
val _ =
type_abbrev
(* ('c,'r) mitb is *)
("mitb",
``: ( ('r+'c) word -> ('r+'c) word) (* permutation *)
-> (('c,'r) mitb_state) (* prev. state *)
-> ('r command) (* command *)
-> (('c,'r) mitb_state) (* next state *)
``);
(*
Type abbreviation for MITB step-function
Given a permutation on b=r+c words, a state and an input, gives
following state and the output.
*)
val _ =
type_abbrev
(* ('c, 'n,'r) mitbstepfunction is *)
("mitbstepfunction",
``: (('r+'c) word -> ('r+'c) word) (* permutation *)
-> ('c, 'r) mitb_state # 'r mitb_inp
-> ('c, 'r) mitb_state # 'n mitb_out
``);
(*
Zero word: Alternative name for the zero word.
REMARK: Zeros is a bool list (bitstring) defined in spongeTheory
*)
val ZERO_def =
Define
`ZERO = (0w: 'a word) `;
(*
We first establish some lemmas to fascilitate relating a translation of
a padded bitstring into a word to a the translation of the same word
padded by the MITB.
*)
(*
Every element in a Zeros-bitstring is F
*)
val EL_Zeros = store_thm("EL_Zeros",
``∀n m. m < n ⇒ (EL m (Zeros n) = F)``,
Induct >> simp[Zeros_def] >> Cases >> simp[] )
(*
Make rewrites for Zeros-bitstring easier.
*)
val LENGTH_Zeros = store_thm("LENGTH_Zeros",
``∀n. LENGTH (Zeros n) = n``,
Induct >> simp[Zeros_def])
val _ = export_rewrites["LENGTH_Zeros"]
val zero_splitting_lemma = store_thm("zero_splitting_lemma",
``! n m . (m <= n) ==> ((Zeros n) = (Zeros m) ++ (Zeros (n-m)))``,
Induct_on `m`
>-
simp[Zeros_def]
>>
(
strip_tac >>
strip_tac >>
qpat_abbrev_tac `X = (Zeros n)` >>
qpat_abbrev_tac `Y = (Zeros (n - (SUC m)))` >>
PURE_REWRITE_TAC [(Once Zeros_def)] >>
qpat_assum `!n. p` ( assume_tac o (Q.SPEC `(n-1)` )) >>
rw [Abbr`Y`] >>
`(n - SUC m) = (n-1) - m` by simp [] >>
pop_assum (fn thm => rw [thm]) >>
`m <= n-1` by simp [] >>
pop_assum (fn thm => fs [thm]) >>
pop_assum (fn thm => rw [SYM thm]) >>
rw [Abbr`X`,(GSYM (CONJUNCT2 Zeros_def))] >>
`n>0` by simp [] >>
simp [ADD1]
)
);
(*
At every position, the bit in a word constructed using
word_from_bin_list concides with the value at the same position in the
original bitstring.
*)
val word_bit_word_from_bin_list = store_thm("word_bit_word_from_bin_list",
``∀ls b.
EVERY ($> 2) ls ∧ b < LENGTH ls ⇒
(word_bit b ((word_from_bin_list ls):'a word) ⇔ b < dimindex (:'a) ∧ (EL b ls = 1))``,
rw[word_from_bin_list_def,l2w_def,word_bit_n2w] >>
rw[GSYM numposrepTheory.num_from_bin_list_def] >>
rw[numposrepTheory.BIT_num_from_bin_list] >>
rw[EQ_IMP_THM] >>
assume_tac DIMINDEX_GT_0 >>
DECIDE_TAC);
val l2n_APPEND = store_thm("l2n_APPEND",
`` ! a c d.
l2n b (c ++ d) = (l2n b c) + (l2n b d) * b ** (LENGTH c) ``,
strip_tac >> Induct
>- (rw [numposrepTheory.l2n_def] >> simp [])
>>
rw [numposrepTheory.l2n_def] >>
simp [EXP]
);
(*
The previous statement holds for BITS_TO_WORD, too.
REMARK: word_from_bin_list translates from num list, where BITS_TO_WORD
translates from bool list. We have chosen the latter representation in
spongeTheory, hence the "indirection".
*)
val word_bit_BITS_TO_WORD = store_thm("word_bit_BITS_TO_WORD",
``∀ls x. x < LENGTH ls ⇒ (word_bit x ((BITS_TO_WORD ls):'a word) ⇔ x < dimindex (:'a) ∧ EL x ls)``,
rw[BITS_TO_WORD_def] >>
qmatch_abbrev_tac`word_bit x (word_from_bin_list l) ⇔ y` >>
`EVERY ($> 2) l` by (
simp[Abbr`l`,EVERY_MAP,EVERY_MEM] >> rw[] ) >>
fs[Abbr`l`] >> simp[word_bit_word_from_bin_list] >>
simp[EL_MAP,Abbr`y`] >> rw[])
val l2n_Zeros_helper = prove(
``!l. l2n 2 (MAP (λe. if e then 1 else 0) (Zeros (l))) = 0``,
Induct >>
rw [numposrepTheory.l2n_def, Zeros_def]);
(* See whether this can go .. *)
(* val num_to_bool_conversion_helper = prove( *)
(* `` (($> 2) n) ==> (((λe. if e then 1 else 0) o (λe. e = 1)) n = n)``, *)
(* simp [] ); *)
val n2l_st = prove(
``! b n. b> 0 ==> EVERY ($> b) (n2l b n)``,
recInduct(fetch "numposrep" "n2l_ind") >>
rw [] >>
rw [(Once numposrepTheory.n2l_def)] >>
assume_tac ( Q.SPECL [`n`,`b`] MOD_LESS ) >>
simp []
);
val MAP_num_to_bool_conversion = prove(
`` (EVERY ($> 2) l)
==>
(MAP ((λe. if e then 1 else 0) o (λe. e = (1:num))) l = l)``,
Induct_on `l` >>
simp [] );
val BITS_TO_WORD_WORD_TO_BITS = store_thm("BITS_TO_WORD_WORD_TO_BITS",
`` ! (k:'r word ).
dimindex (:'r) > 1 ==>
(BITS_TO_WORD (WORD_TO_BITS k) = k )``,
rw [GSYM WORD_EQ,
WORD_TO_BITS_def,
BITS_TO_WORD_def,
word_from_bin_list_def,
l2w_def] >>
rw [l2n_APPEND, l2n_Zeros_helper] >>
rw [GSYM l2w_def,
Abbr`bitstring_without_zeros`, word_to_bin_list_def,
MAP_MAP_o] >>
rw [w2l_def] >>
qspecl_then [`2`,`w2n k`] assume_tac n2l_st >>
rw [MAP_num_to_bool_conversion,
GSYM w2l_def,
l2w_w2l]
);
(*
The word we use for padding. It is Zero at each position, except for the
last-position (MSB) and the position given as a parameter.
(l >< 0) w || PAD_WORD l
produces a padded word of length l from w and l.
REMARK: For l=dimindex(:'a), PAD_WORD has only the MSB set to 1, which
is useful for the definition in case l is one short to the block length.
In this case, the block needs to be followed by a 1w block.
*)
val PAD_WORD_def =
Define
`(PAD_WORD l):'a word = FCP i. (i=dimindex(:'a)-1) \/ (i=l)`;
(* The two following simplifications are used in padding_lemma *)
val word_bit_or = prove (
`` (x < dimindex(:'a)) ==> ((a:'a word || b) ' x ⇔ a ' x \/ b ' x) ``,
rw [word_or_def] >>
simp [fcpTheory.FCP_BETA] );
val word_bit_T = prove (
`` (b < dimindex(:'a) ) ==> ((01w:'a word) ' b = (b=0))``,
rw [word_index] );
(*
This Theorem shows how to construct a correct padding (w.r.t. to
PAD_WORD from spongeTheory) for words smaller than the blocklength minus
two.
*)
val padding_lemma = prove (
``
!m.
(LENGTH(m) < dimindex(:'r)-1)
==>
( 2 < dimindex(:'r))
==>
(LENGTH(m) <> 0 )
==>
(
(BITS_TO_WORD (m ++ (T::(Zeros (dimindex(:'r)-2-LENGTH (m)))++[T]))):'r word
= (((LENGTH m)-1 -- 0 ) (BITS_TO_WORD m) || PAD_WORD (LENGTH m))
)
``,
ntac 4 strip_tac >>
qmatch_abbrev_tac`(BITS_TO_WORD ls) = word` >>
simp[GSYM WORD_EQ] >>
rw [] >>
`x < (LENGTH ls) ` by ( simp[Abbr`ls`,LengthZeros] ) >>
simp[word_bit_BITS_TO_WORD, word_bit_def,Abbr`word`,word_bit_or,
PAD_WORD_def, fcpTheory.FCP_BETA,word_bits_def ] >>
Cases_on `x< LENGTH(m)`
>-
(
simp [word_bit, word_bit_BITS_TO_WORD, Abbr`ls`,EL_APPEND1]
)
>>
`LENGTH(m)-1<=x` by (rw[] >> simp []) >>
simp[word_bit, word_bit_BITS_TO_WORD, Abbr`ls`,EL_APPEND2] >>
Cases_on`x = LENGTH(m)`>>
simp[EL_APPEND2, EL_CONS] >>
(* one case left *)
Cases_on `PRE((x-LENGTH(m))) < dimindex(:'r) - (LENGTH(m) +2)`
>-
(
`PRE((x-LENGTH(m))) < LENGTH(Zeros( dimindex(:'r) - (LENGTH m +2)))` by
simp [LengthZeros] >>
simp[EL_APPEND1, EL_APPEND2, EL_CONS, EL_Zeros]
)
>>
pop_assum (fn thm => `x >= dimindex(:'r)-1` by simp [thm]) >>
pop_assum (fn thm => `x = dimindex(:'r)-1` by simp [thm]) >>
`(LENGTH (Zeros (dimindex ((:ς) :ς itself) − (LENGTH m + (2 :num)))))
<= PRE((x-LENGTH(m)))` by simp [LengthZeros] >>
pop_assum (assume_tac
o (MATCH_MP (INST_TYPE [alpha |-> Type `:bool`] EL_APPEND2 ))) >>
simp [PRE_SUB1,LengthZeros]
);
(*
This Theorem shows how to construct a correct padding (w.r.t. to
PAD_WORD from spongeTheory) for empty words.
*)
val full_padding_lemma = prove (
``
( 2 < dimindex(:'r))
==>
(
(BITS_TO_WORD (T::((Zeros (dimindex(:'r)-2))++[T]))):'r word
= PAD_WORD (0)
)
``,
strip_tac >>
qmatch_abbrev_tac`(BITS_TO_WORD ls) = word` >>
simp[GSYM WORD_EQ] >>
rw [] >>
`x < (LENGTH ls) ` by ( simp[Abbr`ls`,LengthZeros] ) >>
simp[word_bit_BITS_TO_WORD, word_bit_def,Abbr`word`,word_bit_or,
PAD_WORD_def, fcpTheory.FCP_BETA,word_bits_def,LengthZeros] >>
Cases_on `x=0`
>- simp [Abbr`ls`]
>>
`x>0` by simp[] >>
simp [ Abbr`ls`,LengthZeros ,EL_CONS] >>
Cases_on `x< LENGTH(m)` >>
pop_assum (fn thm => `0<x` by simp [thm]) >>
Cases_on `x< dimindex(:'r)-1` >>
lrw [EL_CONS,PRE_SUB1,EL_APPEND1, EL_APPEND2,EL_Zeros,LengthZeros] >>
`x+1-dimindex(:'r)=0` by simp [] >>
rw []
);
(* The following two lemmas show how to construct a padding for a word
that is one short to blocksize. If such a word m is padded, it takes two
blocks, the first one being: m++T, the second one being F::F::..::T.
one_short_lemma
shows that using PAD_WORD as usual works for the first block, i.e.,
it adds a single T at the end of the bitstring.
int_min_lemma
shows that INT_MINw conveniently expresses the second block, which is
independent of the word being padded.
*)
val one_short_lemma = prove (
``
(LENGTH(m) = dimindex(:'r)-1)
/\
( 2 < dimindex(:'r))
==>
(
((BITS_TO_WORD (m ++ [T]):'r word) =
(((LENGTH m)-1 -- 0 ) (BITS_TO_WORD m) || PAD_WORD (LENGTH m) ))
)
``,
strip_tac >>
simp[GSYM WORD_EQ] >>
rw[] >>
`x < LENGTH (m ++ [t])` by simp[] >>
simp[word_bit_BITS_TO_WORD] >>
simp[word_bit_def, word_bit_or, PAD_WORD_def, word_bits_def] >>
Cases_on `(LENGTH m)<= x`
>-
(
`x=dimindex(:'r)-1` by simp [] >>
SRW_TAC [fcpLib.FCP_ss] [EL_APPEND2] >>
simp []
)
>>
fs [NOT_LESS_EQUAL] >>
SRW_TAC [fcpLib.FCP_ss] [EL_APPEND1] >>
first_assum (assume_tac o MATCH_MP
( INST_TYPE [alpha |-> Type `:'r`] word_bit_BITS_TO_WORD)) >>
rfs [] >>
simp [] >>
fs [word_bit_def] >>
`x <= dimindex(:'r)-1` by simp [] >>
fs []
);
val int_min_lemma = prove (
``
(dimindex(:'n) > 0)
==>
((BITS_TO_WORD ((Zeros (dimindex(:'n)-1))++[T])):'n word
= INT_MINw)
``,
strip_tac >>
simp[GSYM WORD_EQ] >>
rw[] >>
qmatch_abbrev_tac`word_bit x (BITS_TO_WORD ls) ⇔ word_bit x INT_MINw` >>
`x < LENGTH ls` by ( simp[Abbr`ls`] ) >>
simp[word_bit_BITS_TO_WORD] >>
simp[word_bit_def,word_L,Abbr`ls`] >>
rev_full_simp_tac(srw_ss()++ARITH_ss)[] >>
Cases_on`x = dimindex(:'n)-1`>>
fs[]>>
simp[EL_APPEND1,EL_APPEND2] >>
simp[EL_Zeros]);
(*
Defines one step of MITB with permutation function f
MITB_FUN f : 'b mitb_state -> 'r inputs -> 'b mitb_state
*)
val MITB_FUN_def =
Define
`(* Skip : {Ready -> Ready} + {Absorbing -> Absorbing} *)
(
(MITB_FUN: ('c,'r) mitb)
f ((cntl,pmem,vmem)) Skip
= (cntl,pmem,vmem))
/\
(* Input : Ready -> Ready *)
(MITB_FUN f (Ready,pmem,vmem) (Input key len)
= (Ready, f((ZERO:'c word) @@ key ),ZERO))
/\
(* Move: {Ready -> Absorbing} *)
(MITB_FUN f (Ready,pmem,vmem) Move
= (Absorbing,pmem,pmem))
/\
(* Move: {Absorbing -> Ready} *)
(MITB_FUN f (Absorbing,pmem,vmem) Move
= (Ready,pmem,ZERO))
/\
(* Input: Absorbing -> {Absorbing,AbsorbEnd,Ready} *)
(MITB_FUN f (Absorbing,pmem,vmem) (Input blk len)
=
let r=dimindex(:'r) in
if len <= r-2 then (* More than one bit too small *)
if len = 0 then
(Ready,pmem,f(vmem ?? ((ZERO:'c word) @@ PAD_WORD (0):'r word)))
else
(Ready,pmem,
f ( vmem ??
(
(ZERO:'c word) @@
( (len-1 -- 0 ) (blk) || PAD_WORD (len) )
)))
else
if len = r-1 then (* Exactly one-bit too small *)
(AbsorbEnd,
pmem,
(* see above. Note PAD_WORD 0x10* in this case *)
f ( vmem ??
(
(ZERO:'c word) @@
( (len-1 -- 0 ) (blk) || PAD_WORD (len) )
)))
else (* Full block *)
(Absorbing,pmem,f(vmem ?? ((ZERO: 'c word) @@ blk )))
)
/\
(* Move: AbsorbEnd -> Ready} *)
(MITB_FUN f (AbsorbEnd,pmem,vmem) Move
= (Ready, pmem, ZERO))
/\
(* Input: AbsorbEnd -> Ready} *)
(MITB_FUN f (AbsorbEnd,pmem,vmem) (Input blk len)
= (Ready, pmem,
(* see above
* f(vmem XOR (Zeros(r-1) ++ [T] ++ Zeros c)))) *)
f(vmem ?? ( (ZERO: 'c word) @@ (INT_MINw:'r word )))
))
`;
(*
Predicate to test for well-formed Keccak parameters
*)
val GoodParameters_def =
Define
`GoodParameters (r:num,c:num,n:num)
⇔ 2 < r /\ 0 < c /\ n <= r`;
(*
Functional version as in the paper
*)
val MITB_def =
Define
`MITB f ((skip,move,block,size), (cntl,pmem,vmem)) =
MITB_FUN f
(cntl, pmem, vmem)
(if skip = T
then Skip else
if move = T
then Move
else
if (size <=dimindex(:'r)) then
Input (block: 'r word) size
else Skip)`;
(*
We define a step function that behaves like MITB, but defines the
output, too.
Parametric in:
f - compression function used inside MITB
Input:
(cnt,pmem,vmem) - current state of the MITB
(skip,move,block,size) - input to the MITB
Output:
(cntl_n,pmem_n,vmem_n) - next state of the MITB
(ready:bool, digest:bits) - output of the MITB
*)
val MITB_STEP_def =
Define
`MITB_STEP f ((cntl,pmem,vmem), (skip,move,block,size)) =
let (cntl_n,pmem_n,vmem_n) = MITB f ((skip,move,block,size), (cntl, pmem, vmem))
in
((cntl_n,pmem_n,vmem_n),
(
(cntl_n = Ready),
(if cntl_n = Ready then ((dimindex(:'n)-1 >< 0) vmem_n) else (ZERO:'n word )))
)
`;
(*
Datatype of commands to the library/protocol calling the MITB
*)
val _ =
Datatype
`mac_query =
SetKey ('r word)
| Mac bits
| Corrupt
`;
(*
Datatype for
- responses from the library/protocol to the adversary (real
world)
- responses from the simulator to the environment (ideal world)
or from the S.
WasCorrupted is a notice that the environment decided to corrupt the
library/protocal or functionality
OracleResponse is the response to an Oracle Query
*)
val _ =
Hol_datatype
`mac_to_adv_msg =
WasCorrupted
| OracleResponse of 'n word
`;
(*
Datatype for
- queries from the adversary to the library/protocol (real world)
- queries from the simulator to the functionality (ideal world)
*)
val _ =
Datatype
`adv_to_mac_msg =
CorruptACK
| OracleQuery bits
`;
(*
State transition function for the functionality defining a perfect MAC
device for a given Hash function
parameters:
H -- Hash function
internal state:
current key K, corruption status
inputs:
queries of type query
output:
bitstrings
REMARK: Whoever is on the adversarial interface may request Hashes with
K prepended to the input. This interface will be accessed by SIM, to be
able to emulate a MITB
FMAC
: (bits -> 'n word) -> (* Hash function *)
'r word # bool -> (* current key, corruption status *)
('r mac_query, γ, δ, ε, ζ, adv_to_mac_msg) Message ->
(* Input from environment or adversary *)
('r word # bool) # ('n word, 'n mac_to_adv_msg) ProtoMessage
(* output to environment or adversary *)
*)
val FMAC_def =
Define
`
( FMAC (H: bits -> 'n word) (K,F)
(EnvtoP (SetKey k:'r mac_query)) =
((k,F),(Proto_toEnv (0w:'n word)))
)
/\
( FMAC H (K,F) (EnvtoP (Mac m)) =
((K,F),(Proto_toEnv (H (WORD_TO_BITS(K) ++ m)))))
/\
( FMAC H (K,F) (EnvtoP (Corrupt)) = ((K,T),Proto_toA (WasCorrupted)))
/\
( FMAC H (K,T) (AtoP (CorruptACK)) = ((K,T),Proto_toEnv 0w))
/\
( FMAC H (K,T) (AtoP (OracleQuery m)) =
((K,T),(Proto_toA (OracleResponse (H((WORD_TO_BITS K)++m))))))
/\
(* When corrupted, ignore honest queries *)
( FMAC H (K,T) (EnvtoP q) = ((K,T),Proto_toEnv 0w))
`;
(*
Run MITB mitbf s l
Executes a list of commands l on a initial state s, using the step
function mitbf. This function will make the definition of the protocol,
see below, easier in the future.
The output consists of the state after execution of list l and the final
output (preceeding outputs are discarded).
*)
val RunMITB_def =
Define
`RunMITB mitbf s (i::il) =
if (il=[]) then
(mitbf (s,i))
else
let (s', out) = (mitbf (s,i)) in
RunMITB mitbf s' il
`;
(*
PROCESS_MESSAGE_LIST: bits list -> 'r mitb_inp list
Given a list of bitstrings, PROCESS_MESSAGE_LIST produces a list of
input queries to the MITB that will leave the MITB in ready state, with
vmem set to the hash of the flattening of the input. This is used in the
protocol definition below.
*)
val PROCESS_MESSAGE_LIST_def= Define
`
(PROCESS_MESSAGE_LIST [] =
([(F,F,0w,0)]:'r mitb_inp list))
/\
(PROCESS_MESSAGE_LIST (hd::tl) =
if (LENGTH hd) = dimindex(:'r)-1 then
([(F,F,(BITS_TO_WORD hd),(LENGTH hd));
(F,F,0w,0)])
else
(if (LENGTH hd) < dimindex(:'r)-1 then
[ (F,F,(BITS_TO_WORD hd),(LENGTH hd)) ]
else
((F,F,(BITS_TO_WORD hd),(LENGTH hd))
:: (PROCESS_MESSAGE_LIST tl))))
`;
(* PROCESS_MESSAGE_LIST never outputs NIL *)
val PROCESS_MESSAGE_LIST_neq_NIL = prove (
``!a . PROCESS_MESSAGE_LIST a <> []:'r mitb_inp list``,
Cases >> rw[PROCESS_MESSAGE_LIST_def] );
(*
PROTO
stepfunction defining the protocol. When used with a "correct" MITB (described by a step function), it implements FMAC.
(In real life, this protocol corresponds to a client library that
computes hashes by splitting the message and feeding it into the MITB.
This is how honest users are supposed to use the MITB )
Parametric in:
mitbf - step function of MITB,
Internal state:
s - current MITB state
T/F - corruption status
Input:
mac_query
Output:
bitstring
*)
val PROTO_def =
Define
`
( PROTO (mitbf : ('c,'r) mitb_state # 'r mitb_inp -> ('c,'r)
mitb_state # 'n mitb_out) (s,F) (EnvtoP (SetKey k)) =
let (s1,(rdy1,dig1))=mitbf (s,(T,F,(ZERO: 'r word),0)) in
if rdy1=F then
(let (s2,(rdy2,dig2)) =mitbf(s1,(F,T,(ZERO:'r word),0)) in
let (s3,(rdy3,dig3))=
mitbf (s2,(F,F,k,(dimindex (:'r)))) in
((s3,F),(Proto_toEnv 0w)))
else
let (s2,rdy2,dig2)=mitbf(s1,(F,F,k,(dimindex (:'r)))) in
((s2,F),(Proto_toEnv 0w))
)
/\
( PROTO mitbf (s,F) (EnvtoP (Mac m)) =
(* Bring MITB into Ready state *)
let (s0,(rdy0,dig0)) = RunMITB mitbf s [(T,F,(ZERO: 'r
word),0)] in
(* make sure that MITB is in Ready state *)
let (sr,rdyr,digr) =
( if (rdy0=F) then
RunMITB mitbf (s0) [(F,T,ZERO,0)]
else
(s0,rdy0,dig0)
) in
let (ss,rdys,digest) = ( RunMITB
mitbf
(sr)
((F,T,ZERO,0)
:: (PROCESS_MESSAGE_LIST (Split (dimindex(:'r)) m))))
in
(* two consecutive moves to re-initialise vmem *)
let (sq,rdyq,digq) = RunMITB mitbf ss [(F,T,ZERO,0);
(F,T,ZERO,0)] in
((sq,F),(Proto_toEnv digest))
)
/\
( PROTO mitbf (s,F) (EnvtoP (Corrupt)) =
((s,T),(Proto_toEnv 0w)))
/\
(* Give adversary blackbox access when corrupted, but
* not complete: she is not allowed to set the key.
* TODO: would be nicer if we would check the ready state via the LED
* *)
(* Ignore Key-overwrite *)
( PROTO mitbf ((Ready,cntl,vmem),T) (AtoP (F,F,inp,len)) =
(((Ready,cntl,vmem),T), (Proto_toA (F,ZERO)))
)
/\
( PROTO mitbf (s,T) (AtoP i) =
let (s_next,rdy,dig) = mitbf (s,i) in
((s_next,T), (Proto_toA (rdy,dig))))
/\
(* Ignore honest queries when corrupted *)
( PROTO mitbf (s,T) (EnvtoP _) = ((s,T),(Proto_toEnv 0w)))
/\
(* Ignore adversarial queries when not corrupted *)
( PROTO mitbf (s,F) (AtoP _) = ((s,F),(Proto_toA ( F,0w ))) )
/\
(* Ignore the rest TODO : get rid of this and replace with individual
* cases.. *)
( PROTO mitbf (s,cor) _ = ((s,cor),(Proto_toEnv 0w)))
`;
(*
SIM - step-function defining the simulator.
The simulator can make queries to F, but only on the adversarial
interface. It should not alter or read F's state directly.
REMARK: We first define a step function for SIM, which is then used in a
wrapper function that instantiates the adversarial interface of F as an
oracle.
*)
val SIM_def =
Define `
(SIM (T,Ready,(vm:'n word) ,m) (EnvtoA (T,_,_,_)) = ((T,Ready,vm,m),(Adv_toEnv
(T,vm))))
/\
(SIM (T,Absorbing,vm,m) (EnvtoA (T,_,_,_)) =
((T,Absorbing,vm,m),(Adv_toEnv (F,ZERO))))
/\
(SIM (T,AbsorbEnd,vm,m) (EnvtoA (T,_,_,_)) =
((T,AbsorbEnd,vm,m),(Adv_toEnv (F,ZERO))))
/\
(SIM (T,Ready,vm,m) (EnvtoA (F,T,_,_)) = ((T,Absorbing,vm,[]),(Adv_toEnv
(F,ZERO ))))
/\
(SIM (T,Absorbing,vm,m) (EnvtoA (F,T,_,_)) =
((T,Ready,ZERO ,m),(Adv_toEnv (T,ZERO ))))
/\
(SIM (T,AbsorbEnd,vm,m) (EnvtoA (F,T,_,_)) =
((T,Ready,ZERO ,m),(Adv_toEnv (T,ZERO ))))
/\
(SIM (T,Absorbing,(vm: 'n word),m) (EnvtoA (F,F,(inp: 'r word),inp_size)) =
let r = dimindex(:'r) in
(* Cases:
* inp_size=r) take full block
* inp_size=r-1 take partial block, goto AbsorbEnd
* inp_size<r-1 query oracle
* *)
if (inp_size=r) then
((T,Absorbing,ZERO, (m ++ (WORD_TO_BITS inp))),(Adv_toEnv (F,ZERO)))
else
if (inp_size=r-1) then
((T,AbsorbEnd,ZERO,
(m ++ TAKE inp_size (WORD_TO_BITS ((inp_size-1 -- 0)
inp)))),Adv_toEnv (F,ZERO))
else
if inp_size<r-1 then
(* Send to Functionality for communication. Proceed when response is *)
(* received, see (MARK) *)
(
if inp_size = 0 then
((T,Absorbing,vm,[]), Adv_toP ( OracleQuery (m)))
else
((T,Absorbing,vm,[]),
(Adv_toP (
OracleQuery (m ++
TAKE inp_size (WORD_TO_BITS ((inp_size-1 -- 0)
inp))))))
)
else (*if inp_size>r behave like Skip*)
((T,Absorbing,vm,m),(Adv_toEnv (F,ZERO)))
(* ) *)
)
/\
(SIM (T,AbsorbEnd,vm,m) (EnvtoA (F,F,inp,inp_size)) =
if (inp_size <= dimindex(:'r)) then
(
((T,AbsorbEnd,vm,[]),(Adv_toP (
OracleQuery ((m)))))
)
else (* behave like Skip *)
((T,AbsorbEnd,vm,m),(Adv_toEnv (F,ZERO)))
)
/\
(* MARK *)
(SIM (T,_,vm,m) (PtoA (OracleResponse hashvalue)) =
((T,Ready,hashvalue,[]),(Adv_toEnv (T,hashvalue))))
/\
(* If FMAC was corrupted, change corruption state *)
(SIM (F,cntl,vm,m) (PtoA WasCorrupted) = ((T,cntl,vm,m),(Adv_toP
(CorruptACK))))
/\
(* Ignore other queries while not corrupted *)
(SIM (F,cntl,vm,m) (EnvtoA _) = ((F,cntl,vm,m),(Adv_toEnv (F,ZERO))))
/\
(* Ignore other queries, while corrupted, in particular:
* query to set the key. *)
(SIM (T,cntl,vm,m) (EnvtoA _) = ((T,cntl,vm,m),(Adv_toEnv (F,ZERO))))
`;
(* Type abbreviations for easier debugging *)
val _ =
type_abbrev
("real_game_state",
``: (('c,'r) mitb_state # bool) # num list ``);
(* ^ corruption status *)
val _ = type_abbrev ("fmac_state",
``: ( 'r word # bool) ``);
(* corruption status ^ *)
val _ = type_abbrev ("proto_state",
``: (('c,'r) mitb_state # bool)``);
(* ('n,'r) real_message is *)
val _ = type_abbrev ("real_message",