-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTLS.Compressions.fst
2261 lines (2118 loc) · 89.6 KB
/
CTLS.Compressions.fst
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
module CTLS.Compressions
open FStar.List.Tot
open Comparse
open TLS13.MessageFormats
open TLS13.OldExtensions
open CTLS.Profile
open CTLS.MessageFormats
#set-options "--fuel 0 --ifuel 0"
(*** Compression types ***)
type equivalence (a:Type0) = eq:(a -> a -> prop){forall x. eq x x}
val equality_equivalence: #a:Type -> equivalence a
let equality_equivalence #a x y = x == y
noeq
type compression (bytes:Type0) {|bytes_like bytes|} (tls_type:Type0) (ctls_type: ctls_template bytes -> Type0) (tls_eq:equivalence tls_type) = {
compress: t:ctls_template bytes -> x:tls_type -> option (ctls_type t);
decompress: t:ctls_template bytes -> ctls_type t -> option tls_type;
decompress_compress:
t:ctls_template bytes -> x:tls_type ->
Lemma (
match compress t x with
| None -> True
| Some x_compressed -> (
match decompress t x_compressed with
| None -> False
| Some x_roundtrip -> x_roundtrip `tls_eq` x
)
);
compress_decompress:
t:ctls_template bytes -> x_compressed:ctls_type t ->
Lemma (
match decompress t x_compressed with
| None -> True
| Some x_decompressed -> compress t x_decompressed == Some x_compressed
);
}
unfold val (let?): #a:Type -> #b:Type -> x:option a -> (y:a -> Pure (option b) (requires Some y == x) (ensures fun _ -> True)) -> option b
unfold let (let?) #a #b x f =
match x with
| None -> None
| Some y -> f y
val no_compression: #bytes:Type0 -> {|bytes_like bytes|} -> a:Type0 -> compression bytes a (no_template_dep a) equality_equivalence
let no_compression #bytes #bl a = {
compress = (fun t x -> Some x);
decompress = (fun t x -> Some x);
decompress_compress = (fun t x -> ());
compress_decompress = (fun t x -> ());
}
val compression_to_bytes:
bytes:Type0 -> {|bytes_like bytes|} ->
tls_type:Type0 -> parser_serializer_whole bytes tls_type ->
ctls_type:(ctls_template bytes -> Type0) ->
compression bytes tls_type ctls_type equality_equivalence ->
compression bytes bytes ctls_type equality_equivalence
let compression_to_bytes bytes #bl tls_type ps_tls_type ctls_type comp =
{
compress = (fun t b ->
let? x = ps_tls_type.parse b in
comp.compress t x
);
decompress = (fun t x ->
let? x_decompressed = comp.decompress t x in
Some (ps_tls_type.serialize x_decompressed)
);
decompress_compress = (fun t b ->
match ps_tls_type.parse b with
| None -> ()
| Some x -> (
comp.decompress_compress t x;
ps_tls_type.serialize_parse_inv b
)
);
compress_decompress = (fun t x ->
match comp.decompress t x with
| None -> ()
| Some x_decompressed -> (
ps_tls_type.parse_serialize_inv x_decompressed;
comp.compress_decompress t x
)
);
}
val mk_compression:
#a:Type0 -> #bytes:Type0 -> {|bytes_like bytes|} ->
#get_template_elem:(ctls_template bytes -> option a) ->
#default_ty:Type0 -> #parametric_ty:(a -> Type0) ->
#eq:equivalence default_ty ->
compress:(t:a -> default_ty -> option (parametric_ty t)) ->
decompress:(t:a -> parametric_ty t -> option default_ty) ->
decompress_compress:(t:a -> x:default_ty -> Lemma (
match compress t x with
| None -> True
| Some x_compressed -> (
match decompress t x_compressed with
| None -> False
| Some x_roundtrip -> x_roundtrip `eq` x
)
)) ->
compress_decompress:(t:a -> x_compressed:parametric_ty t -> Lemma (
match decompress t x_compressed with
| None -> True
| Some x_decompressed -> compress t x_decompressed == Some x_compressed
)) ->
compression bytes default_ty (mk_templated_type bytes get_template_elem default_ty parametric_ty) eq
let mk_compression #a #bytes #bl #get_template_elem #default_ty #parametric_ty #eq compress decompress decompress_compress compress_decompress =
let compress (template:ctls_template bytes) (x:default_ty): option (mk_templated_type bytes get_template_elem default_ty parametric_ty template) =
match get_template_elem template with
| None -> Some x
| Some param -> compress param x
in
let decompress (template:ctls_template bytes) (y:mk_templated_type bytes get_template_elem default_ty parametric_ty template): option default_ty =
match get_template_elem template with
| None -> Some y
| Some param -> decompress param y
in
{
compress;
decompress;
decompress_compress = (fun template x ->
match get_template_elem template with
| None -> ()
| Some param -> decompress_compress param x
);
compress_decompress = (fun template (y:mk_templated_type bytes get_template_elem default_ty parametric_ty template) ->
match get_template_elem template with
| None -> ()
| Some param -> compress_decompress param y
);
}
(*** Helper: list compression ***)
val list_ctls_type:
#bytes:Type0 -> {|bytes_like bytes|} ->
(ctls_template bytes -> Type0) ->
(ctls_template bytes -> Type0)
let list_ctls_type #bytes #bl c = fun t ->
list (c t)
val list_equivalent:
#a:Type ->
equivalence a ->
equivalence (list a)
let list_equivalent #a eq = fun l1 l2 ->
List.Tot.length l1 = List.Tot.length l2 /\
(forall i. (List.Tot.index l1 i) `eq` (List.Tot.index l2 i))
val list_equivalent_symmetric:
#a:Type ->
eq:equivalence a ->
eq_sym:(
x1:a -> x2:a ->
Lemma
(requires x1 `eq` x2)
(ensures x2 `eq` x1)
) ->
l1:list a -> l2:list a ->
Lemma
(requires l1 `list_equivalent eq` l2)
(ensures l2 `list_equivalent eq` l1)
let list_equivalent_symmetric #a eq eq_sym l1 l2 =
introduce forall i. (List.Tot.index l2 i) `eq` (List.Tot.index l1 i) with (
eq_sym (List.Tot.index l1 i) (List.Tot.index l2 i)
)
val list_equivalent_transitive:
#a:Type ->
eq:equivalence a ->
eq_trans:(
x1:a -> x2:a -> x3:a ->
Lemma
(requires x1 `eq` x2 /\ x2 `eq` x3)
(ensures x1 `eq` x3)
) ->
l1:list a -> l2:list a -> l3:list a ->
Lemma
(requires l1 `list_equivalent eq` l2 /\ l2 `list_equivalent eq` l3)
(ensures l1 `list_equivalent eq` l3)
let list_equivalent_transitive #a eq eq_trans l1 l2 l3 =
introduce forall i. (List.Tot.index l1 i) `eq` (List.Tot.index l3 i) with (
eq_trans (List.Tot.index l1 i) (List.Tot.index l2 i) (List.Tot.index l3 i)
)
#push-options "--ifuel 1"
val mapM:
#a:Type -> #b:Type ->
(a -> option b) -> list a ->
option (list b)
let rec mapM #a #b f l =
match l with
| [] -> Some []
| h::t ->
let? fh = f h in
let? ft = mapM f t in
Some (fh::ft)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val list_equivalent_cons:
#a:Type ->
eq:equivalence a ->
h1:a -> t1:list a ->
h2:a -> t2:list a ->
Lemma
(requires (h1 `eq` h2) /\ (t1 `list_equivalent eq` t2))
(ensures ((h1::t1) `list_equivalent eq` (h2::t2)))
let list_equivalent_cons #a eq h1 t1 h2 t2 =
introduce forall i. (List.Tot.index (h1::t1) i) `eq` (List.Tot.index (h2::t2) i) with (
if i = 0 then ()
else (
// Trigger the forall with (i-1) with the correct type
let _ = List.Tot.index t1 (i-1) in
()
)
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val mapM_roundtrip:
#a:Type -> #b:Type ->
f:(a -> option b) -> g:(b -> option a) ->
l:list a ->
eq:equivalence a ->
f_g_eq:(x:a -> Lemma (
match f x with
| None -> True
| Some fx ->
match g fx with
| None -> False
| Some gfx ->
gfx `eq` x
)) ->
Lemma (
match mapM f l with
| None -> True
| Some fl ->
match mapM g fl with
| None -> False
| Some gfl ->
gfl `list_equivalent eq` l
)
let rec mapM_roundtrip #a #b f g l eq f_g_eq =
match l with
| [] -> ()
| h::t -> (
f_g_eq h;
mapM_roundtrip f g t eq f_g_eq;
match f h, mapM f t with
| Some fh, Some ft -> (
let Some gfh = g fh in
let Some gft = mapM g ft in
list_equivalent_cons eq gfh gft h t
)
| _, _ -> ()
)
#pop-options
#push-options "--ifuel 1 --fuel 0"
val list_compression:
#bytes:Type0 -> {|bytes_like bytes|} -> #tls_type:Type0 -> #ctls_type:(ctls_template bytes -> Type0) -> #tls_eq:equivalence tls_type ->
compression bytes tls_type ctls_type tls_eq ->
compression bytes (list tls_type) (list_ctls_type ctls_type) (list_equivalent tls_eq)
let list_compression #bytes #bl #tls_type #ctls_type #tls_eq comp = {
compress = (fun (t:ctls_template bytes) (x:list tls_type) ->
mapM (comp.compress t) x
);
decompress = (fun (t:ctls_template bytes) (x:list_ctls_type ctls_type t) ->
mapM (comp.decompress t) x
);
decompress_compress = (fun (t:ctls_template bytes) (x:list tls_type) ->
mapM_roundtrip (comp.compress t) (comp.decompress t) x tls_eq (comp.decompress_compress t)
);
compress_decompress = (fun (t:ctls_template bytes) (x:list_ctls_type ctls_type t) ->
mapM_roundtrip (comp.decompress t) (comp.compress t) x equality_equivalence (comp.compress_decompress t);
FStar.Classical.forall_intro (FStar.Classical.move_requires (List.Tot.index_extensionality x))
);
}
#pop-options
(*** Ciphersuite ***)
#push-options "--fuel 2 --ifuel 2"
val cipher_suites_compression:
#bytes:Type0 -> {|bytes_like bytes|} ->
compression bytes (tls_list bytes (ps_cipher_suite) ({min=2; max=(pow2 16)-2})) (ctls_cipher_suites bytes) equality_equivalence
let cipher_suites_compression #bytes #bl =
let compress (param:ctls_cipher_suite_template bytes) (x:tls_list bytes (ps_cipher_suite) ({min=2; max=(pow2 16)-2})): option (pre_ctls_cipher_suites bytes param) =
if x = [param] then
Some ()
else
None
in
let decompress (param:ctls_cipher_suite_template bytes) (y:pre_ctls_cipher_suites bytes param): option (tls_list bytes (ps_cipher_suite) ({min=2; max=(pow2 16)-2})) =
Some ([param])
in
mk_compression compress decompress
(fun param (x:tls_list bytes (ps_cipher_suite) ({min=2; max=(pow2 16)-2})) -> ())
(fun param (y:pre_ctls_cipher_suites bytes param) -> ())
#pop-options
val cipher_suite_compression:
#bytes:Type0 -> {|bytes_like bytes|} ->
compression bytes (cipher_suite) (ctls_cipher_suite bytes) equality_equivalence
let cipher_suite_compression #bytes #bl =
let compress (param:ctls_cipher_suite_template bytes) (x:cipher_suite): option (pre_ctls_cipher_suite bytes param) =
if x = param then
Some ()
else
None
in
let decompress (param:ctls_cipher_suite_template bytes) (y:pre_ctls_cipher_suite bytes param): option (cipher_suite) =
Some (param)
in
mk_compression compress decompress
(fun param (x:cipher_suite) -> ())
(fun param (y:pre_ctls_cipher_suite bytes param) -> ())
(*** DH group ***)
#push-options "--fuel 2 --ifuel 2"
val key_share_client_hello_compression: #bytes:Type0 -> {|bytes_like bytes|} -> compression bytes (key_share_client_hello bytes) (ctls_key_share_client_hello bytes) equality_equivalence
let key_share_client_hello_compression #bytes #bl =
let compress (param:ctls_dh_group_template bytes) (x:key_share_client_hello bytes): option (pre_ctls_key_share_client_hello bytes param) =
match x.client_shares with
| [key_share] -> (
if (
key_share.group = param.group_name &&
(param.key_share_length = 0 || length #bytes key_share.key_exchange = param.key_share_length)
) then (
Some ({ client_shares = key_share.key_exchange; })
) else None
)
| _ -> None
in
let decompress (param:ctls_dh_group_template bytes) (y:pre_ctls_key_share_client_hello bytes param): option (key_share_client_hello bytes) =
let client_share = {
group = param.group_name;
key_exchange = y.client_shares;
} in
if length #bytes y.client_shares < pow2 16 - 4 then (
Some ({
client_shares = [client_share];
} <: key_share_client_hello bytes)
) else None
in
mk_compression compress decompress
(fun param (x:key_share_client_hello bytes) -> ())
(fun param (y:pre_ctls_key_share_client_hello bytes param) -> ())
#pop-options
val key_share_server_hello_compression: #bytes:Type0 -> {|bytes_like bytes|} -> compression bytes (key_share_server_hello bytes) (ctls_key_share_server_hello bytes) equality_equivalence
let key_share_server_hello_compression #bytes #bl =
let compress (param:ctls_dh_group_template bytes) (x:key_share_server_hello bytes): option (pre_ctls_key_share_server_hello bytes param) =
let key_share = x.server_share in
if (
key_share.group = param.group_name &&
(param.key_share_length = 0 || length #bytes key_share.key_exchange = param.key_share_length)
) then (
Some { server_share = x.server_share.key_exchange; }
) else None
in
let decompress (param:ctls_dh_group_template bytes) (y:pre_ctls_key_share_server_hello bytes param): option (key_share_server_hello bytes) =
Some ({
server_share = {
group = param.group_name;
key_exchange = y.server_share;
}
} <: key_share_server_hello bytes)
in
mk_compression compress decompress
(fun param (x:key_share_server_hello bytes) -> ())
(fun param (y:pre_ctls_key_share_server_hello bytes param) -> ())
(*** Signature algorithm ***)
val certificate_verify_algorithm_compression: #bytes:Type0 -> {|bytes_like bytes|} -> compression bytes signature_scheme (ctls_certificate_verify_algorithm bytes) equality_equivalence
let certificate_verify_algorithm_compression #bytes #bl =
let compress (param:ctls_signature_algorithm_template bytes) (x:signature_scheme): option (pre_ctls_certificate_verify_algorithm bytes param) =
if x = param.signature_scheme then
Some ()
else
None
in
let decompress (param:ctls_signature_algorithm_template bytes) (y:pre_ctls_certificate_verify_algorithm bytes param): option signature_scheme =
Some param.signature_scheme
in
mk_compression compress decompress
(fun param (x:signature_scheme) -> ())
(fun param (y:pre_ctls_certificate_verify_algorithm bytes param) -> ())
val certificate_verify_signature_compression: #bytes:Type0 -> {|bytes_like bytes|} -> compression bytes (tls_bytes bytes (({min=0; max=(pow2 16)-1}))) (ctls_certificate_verify_signature bytes) equality_equivalence
let certificate_verify_signature_compression #bytes #bl =
let compress (param:ctls_signature_algorithm_template bytes) (x:tls_bytes bytes (({min=0; max=(pow2 16)-1}))): option (pre_ctls_certificate_verify_signature bytes param) =
if param.signature_length = 0 || length (x <: bytes) = param.signature_length then
Some x
else
None
in
let decompress (param:ctls_signature_algorithm_template bytes) (y:pre_ctls_certificate_verify_signature bytes param): option (tls_bytes bytes (({min=0; max=(pow2 16)-1}))) =
Some y
in
mk_compression compress decompress
(fun param (x:tls_bytes bytes (({min=0; max=(pow2 16)-1}))) -> ())
(fun param (y:pre_ctls_certificate_verify_signature bytes param) -> ())
(*** Random ***)
val random_compression: #bytes:Type0 -> {|bytes_like bytes|} -> compression bytes (random bytes) (ctls_random bytes) equality_equivalence
let random_compression #bytes #bl =
let compress (sz:ctls_random_template bytes) (x:random bytes): option (pre_ctls_random bytes sz) =
let? (lhs, rhs) = split #bytes x sz in
if to_nat rhs = Some 0 then
Some (lhs <: pre_ctls_random bytes sz)
else None
in
let decompress (sz:ctls_random_template bytes) (x:pre_ctls_random bytes sz): option (random bytes) =
Some (concat #bytes x (from_nat (32 - sz) 0))
in
mk_compression compress decompress
(fun (sz:ctls_random_template bytes) (x:random bytes) ->
concat_split #bytes x sz;
match split #bytes x sz with
| Some (lhs, rhs) -> to_from_nat #bytes rhs
| None -> ()
)
(fun (sz:ctls_random_template bytes) (y:pre_ctls_random bytes sz) ->
from_to_nat #bytes (32 - sz) 0;
split_concat #bytes y (from_nat (32 - sz) 0)
)
(*** Extension: framework ***)
val extensions_eq: #bytes:eqtype -> {|bytes_like bytes|} -> equivalence (list (extension bytes))
let extensions_eq #bytes #bl = fun l1 l2 ->
forall (x:extension bytes). List.Tot.count x l1 == List.Tot.count x l2
// Stage 1 : predefined extensions
// Stage 1.1: one predefined extensions
#push-options "--ifuel 1 --fuel 1"
val extensions_one_predefined_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
extension bytes -> list (extension bytes) ->
option (list (extension bytes))
let rec extensions_one_predefined_compress #bytes #bl predefined_ext exts =
match exts with
| [] -> None
| h::t ->
if h = predefined_ext then
Some t
else (
let? compressed_t = extensions_one_predefined_compress predefined_ext t in
Some (h::compressed_t)
)
#pop-options
val extensions_one_predefined_decompress:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_ext:extension bytes ->
compressed_exts:list (extension bytes) ->
list (extension bytes)
let extensions_one_predefined_decompress #bytes #bl predefined_ext compressed_exts =
predefined_ext::compressed_exts
#push-options "--ifuel 2 --fuel 2"
val extensions_one_predefined_decompress_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_ext:extension bytes -> exts:list (extension bytes) ->
Lemma
(requires Some? (extensions_one_predefined_compress predefined_ext exts))
(ensures extensions_one_predefined_decompress predefined_ext (Some?.v (extensions_one_predefined_compress predefined_ext exts)) `extensions_eq` exts)
let rec extensions_one_predefined_decompress_compress #bytes #bl predefined_ext exts =
match exts with
| h::t ->
if h = predefined_ext then ()
else extensions_one_predefined_decompress_compress predefined_ext t
#pop-options
#push-options "--ifuel 1 --fuel 1"
val count_extensions_one_predefined_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_ext:extension bytes -> exts:list (extension bytes) ->
ext:extension bytes ->
Lemma
(requires Some? (extensions_one_predefined_compress predefined_ext exts))
(ensures (List.Tot.count ext exts == ((if ext = predefined_ext then 1 else 0) + List.Tot.count ext (Some?.v (extensions_one_predefined_compress predefined_ext exts)))))
let rec count_extensions_one_predefined_compress #bytes #bl predefined_ext exts ext =
match exts with
| h::t ->
if h = predefined_ext then ()
else (
count_extensions_one_predefined_compress predefined_ext t ext
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_one_predefined_decompress_compress_eq:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_ext:extension bytes -> exts:list (extension bytes) -> compressed_exts:list (extension bytes) ->
Lemma
(requires
Some? (extensions_one_predefined_compress predefined_ext exts) /\
(Some?.v (extensions_one_predefined_compress predefined_ext exts)) `extensions_eq` compressed_exts
)
(ensures extensions_one_predefined_decompress predefined_ext compressed_exts `extensions_eq` exts)
let extensions_one_predefined_decompress_compress_eq #bytes #bl predefined_ext exts compressed_exts =
FStar.Classical.forall_intro (FStar.Classical.move_requires (count_extensions_one_predefined_compress predefined_ext exts))
#pop-options
#push-options "--ifuel 0 --fuel 1"
val extensions_one_predefined_compress_decompress:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_ext:extension bytes -> compressed_exts:list (extension bytes) ->
Lemma (
Some? (extensions_one_predefined_compress predefined_ext (extensions_one_predefined_decompress predefined_ext compressed_exts)) /\
Some?.v (extensions_one_predefined_compress predefined_ext (extensions_one_predefined_decompress predefined_ext compressed_exts)) == compressed_exts
)
let extensions_one_predefined_compress_decompress #bytes #bl predefined_ext compressed_exts = ()
#pop-options
// Stage 1.2: several predefined extensions
#push-options "--ifuel 1 --fuel 1"
val extensions_predefined_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
list (extension bytes) -> list (extension bytes) ->
option (list (extension bytes))
let rec extensions_predefined_compress #bytes #bl predefined_exts exts =
match predefined_exts with
| [] -> Some exts
| h_pe::t_pe ->
let? exts_compressed = (extensions_one_predefined_compress h_pe exts) in
extensions_predefined_compress t_pe exts_compressed
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_predefined_decompress:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_exts:list (extension bytes) ->
compressed_exts:list (extension bytes) ->
list (extension bytes)
let rec extensions_predefined_decompress #bytes #bl predefined_exts compressed_exts =
match predefined_exts with
| [] -> compressed_exts
| h_pe::t_pe ->
extensions_one_predefined_decompress h_pe (extensions_predefined_decompress t_pe compressed_exts)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_predefined_decompress_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_exts:list (extension bytes) -> exts:list (extension bytes) ->
Lemma
(requires Some? (extensions_predefined_compress predefined_exts exts))
(ensures extensions_predefined_decompress predefined_exts (Some?.v (extensions_predefined_compress predefined_exts exts)) `extensions_eq` exts)
let rec extensions_predefined_decompress_compress #bytes #bl predefined_exts exts =
match predefined_exts with
| [] -> ()
| h_pe::t_pe ->
extensions_one_predefined_decompress_compress h_pe exts;
match extensions_one_predefined_compress h_pe exts with
| None -> ()
| Some exts_compressed -> (
extensions_predefined_decompress_compress t_pe exts_compressed
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_predefined_decompress_compress_eq:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_exts:list (extension bytes) -> exts:list (extension bytes) -> compressed_exts:list (extension bytes) ->
Lemma
(requires
Some? (extensions_predefined_compress predefined_exts exts) /\
(Some?.v (extensions_predefined_compress predefined_exts exts)) `extensions_eq` compressed_exts
)
(ensures extensions_predefined_decompress predefined_exts compressed_exts `extensions_eq` exts)
let rec extensions_predefined_decompress_compress_eq #bytes #bl predefined_exts exts compressed_exts =
match predefined_exts with
| [] -> ()
| h_pe::t_pe ->
let Some exts_compressed = extensions_one_predefined_compress h_pe exts in
extensions_predefined_decompress_compress_eq t_pe exts_compressed compressed_exts;
extensions_one_predefined_decompress_compress_eq h_pe exts (extensions_predefined_decompress t_pe compressed_exts)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_predefined_compress_decompress:
#bytes:eqtype -> {|bytes_like bytes|} ->
predefined_exts:list (extension bytes) -> compressed_exts:list (extension bytes) ->
Lemma (
Some? (extensions_predefined_compress predefined_exts (extensions_predefined_decompress predefined_exts compressed_exts)) /\
Some?.v (extensions_predefined_compress predefined_exts (extensions_predefined_decompress predefined_exts compressed_exts)) == compressed_exts
)
let rec extensions_predefined_compress_decompress #bytes #bl predefined_exts compressed_exts =
match predefined_exts with
| [] -> ()
| h_pe::t_pe ->
extensions_one_predefined_compress_decompress h_pe (extensions_predefined_decompress t_pe compressed_exts);
extensions_predefined_compress_decompress t_pe compressed_exts
#pop-options
// Stage 2 : expected extensions
// Stage 2.1 : one expected extensions
#push-options "--ifuel 1 --fuel 1"
val extensions_one_expected_compress:
#bytes:Type0 -> {|bytes_like bytes|} ->
extension_type -> list (extension bytes) ->
option (extension bytes & list (extension bytes))
let rec extensions_one_expected_compress #bytes #bl expected_ty exts =
match exts with
| [] -> None
| h_ext::t_ext ->
if h_ext.extension_type = expected_ty then
Some (h_ext, t_ext)
else (
let? (res_ext, res_exts) = extensions_one_expected_compress expected_ty t_ext in
Some (res_ext, h_ext::res_exts)
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_one_expected_decompress:
#bytes:Type0 -> {|bytes_like bytes|} ->
expected_ext:extension bytes -> compressed_exts:list (extension bytes) ->
list (extension bytes)
let extensions_one_expected_decompress expected_ext compressed_exts =
expected_ext::compressed_exts
#pop-options
#push-options "--ifuel 2 --fuel 1"
val count_extensions_one_expected_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
expected_ty:extension_type -> exts:list (extension bytes) ->
ext:extension bytes ->
Lemma
(requires Some? (extensions_one_expected_compress expected_ty exts))
(ensures (
let Some (expected_ext, compressed_exts) = extensions_one_expected_compress expected_ty exts in
List.Tot.count ext exts == (if ext = expected_ext then 1 else 0) + List.Tot.count ext compressed_exts
))
let rec count_extensions_one_expected_compress #bytes #bl expected_ty exts ext =
match exts with
| [] -> assert(False)
| h_ext::t_ext ->
if h_ext.extension_type = expected_ty then ()
else (
count_extensions_one_expected_compress expected_ty t_ext ext
)
#pop-options
#push-options "--ifuel 2 --fuel 1"
val extensions_one_expected_decompress_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
expected_ty:extension_type -> exts:list (extension bytes) ->
Lemma
(requires Some? (extensions_one_expected_compress expected_ty exts))
(ensures (
let Some (expected_ext, compressed_exts) = extensions_one_expected_compress expected_ty exts in
extensions_one_expected_decompress expected_ext compressed_exts `extensions_eq` exts
))
let extensions_one_expected_decompress_compress #bytes #bl expected_ty exts =
FStar.Classical.forall_intro (FStar.Classical.move_requires (count_extensions_one_expected_compress expected_ty exts))
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_one_expected_compress_correct:
#bytes:Type0 -> {|bytes_like bytes|} ->
expected_ty:extension_type -> exts:list (extension bytes) ->
Lemma
(requires Some? (extensions_one_expected_compress expected_ty exts))
(ensures (
let Some (expected_ext, _) = extensions_one_expected_compress expected_ty exts in
expected_ext.extension_type == expected_ty
))
let rec extensions_one_expected_compress_correct #bytes #bl expected_ty exts =
match exts with
| [] -> assert(False)
| h_ext::t_ext ->
if h_ext.extension_type = expected_ty then ()
else (
extensions_one_expected_compress_correct expected_ty t_ext
)
#pop-options
// Stage 2.2 : several expected extensions
val get_extensions_types: #bytes:Type0 -> {|bytes_like bytes|} -> list (extension bytes) -> list extension_type
let get_extensions_types #bytes #bl exts = (List.Tot.map Mkextension?.extension_type exts)
#push-options "--ifuel 1 --fuel 1"
val extensions_expected_compress:
#bytes:Type0 -> {|bytes_like bytes|} ->
list extension_type -> list (extension bytes) ->
option (list (extension bytes) & list (extension bytes))
let rec extensions_expected_compress #bytes #bl expected_tys exts =
match expected_tys with
| [] -> Some ([], exts)
| h_expected_ty::t_expected_ty -> (
let? (expected_h_ext, other_exts) = extensions_one_expected_compress h_expected_ty exts in
let? (expected_t_ext, other_exts) = extensions_expected_compress t_expected_ty other_exts in
Some (expected_h_ext::expected_t_ext, other_exts)
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_expected_decompress:
#bytes:Type0 -> {|bytes_like bytes|} ->
expected_exts:list (extension bytes) -> other_exts:list (extension bytes) ->
list (extension bytes)
let rec extensions_expected_decompress #bytes #bl expected_exts other_exts =
match expected_exts with
| [] -> other_exts
| h_expected_exts::t_expected_exts ->
extensions_one_expected_decompress h_expected_exts (extensions_expected_decompress t_expected_exts other_exts)
#pop-options
#push-options "--ifuel 2 --fuel 1"
val extensions_expected_decompress_compress:
#bytes:eqtype -> {|bytes_like bytes|} ->
expected_tys:list extension_type -> exts:list (extension bytes) ->
Lemma
(requires Some? (extensions_expected_compress expected_tys exts))
(ensures (
let Some (expected_exts, other_exts) = (extensions_expected_compress expected_tys exts) in
extensions_expected_decompress expected_exts other_exts `extensions_eq` exts
))
let rec extensions_expected_decompress_compress #bytes #bl expected_tys exts =
match expected_tys with
| [] -> ()
| h_expected_ty::t_expected_ty -> (
let Some (expected_h_ext, other_exts) = extensions_one_expected_compress h_expected_ty exts in
extensions_one_expected_decompress_compress h_expected_ty exts;
extensions_expected_decompress_compress t_expected_ty other_exts
)
#pop-options
#push-options "--ifuel 2 --fuel 1"
val extensions_expected_compress_correct:
#bytes:Type0 -> {|bytes_like bytes|} ->
expected_tys:list extension_type -> exts:list (extension bytes) ->
Lemma
(requires Some? (extensions_expected_compress expected_tys exts))
(ensures (
let Some (expected_exts, _) = extensions_expected_compress expected_tys exts in
List.Tot.length expected_exts == List.Tot.length expected_tys /\
expected_tys == get_extensions_types expected_exts
))
let rec extensions_expected_compress_correct #bytes #bl expected_tys exts =
match expected_tys with
| [] -> ()
| h_expected_ty::t_expected_ty -> (
let Some (expected_h_ext, other_exts) = extensions_one_expected_compress h_expected_ty exts in
extensions_one_expected_compress_correct h_expected_ty exts;
extensions_expected_compress_correct t_expected_ty other_exts
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val extensions_expected_compress_decompress:
#bytes:Type0 -> {|bytes_like bytes|} ->
expected_exts:list (extension bytes) -> compressed_exts:list (extension bytes) ->
Lemma (extensions_expected_compress (get_extensions_types expected_exts) (extensions_expected_decompress expected_exts compressed_exts) == Some (expected_exts, compressed_exts))
let rec extensions_expected_compress_decompress #bytes #bl expected_exts compressed_exts =
match expected_exts with
| [] -> ()
| h_expected_exts::t_expected_exts -> (
extensions_expected_compress_decompress t_expected_exts compressed_exts
)
#pop-options
// Stage 3: finalize everything
noeq type ctls_extensions_comp (bytes:Type0) {|bytes_like bytes|} = {
tys: ctls_extensions_types bytes;
expected_comp: ext_ty:extension_type -> compression bytes (tls_extension_inner bytes) (tys.expected_ty ext_ty).typ equality_equivalence;
additional_comp: ext_ty:extension_type -> compression bytes (tls_extension_inner bytes) (tys.additional_ty ext_ty).typ equality_equivalence;
}
#push-options "--ifuel 1 --fuel 1"
val compress_expected_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
exts:list (extension bytes) ->
option (ctls_expected_extensions ext_comp.tys temp (get_extensions_types exts))
let rec compress_expected_extensions #bytes #bl ext_comp temp exts =
match exts with
| [] -> Some ()
| h::t -> (
let? h_compressed = (ext_comp.expected_comp h.extension_type).compress temp {extension_data = h.extension_data} in
let? t_compressed = compress_expected_extensions ext_comp temp t in
Some (h_compressed, t_compressed)
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val decompress_expected_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
ext_tys:list extension_type ->
ctls_expected_extensions ext_comp.tys temp ext_tys ->
option (exts:list (extension bytes){get_extensions_types exts == ext_tys})
let rec decompress_expected_extensions #bytes #bl ext_comp temp ext_tys compressed_exts =
match ext_tys with
| [] -> Some []
| ext_ty_h::ext_ty_t -> (
let (compressed_ext_h, compressed_ext_t): ctls_expected_extensions ext_comp.tys temp (ext_ty_h::ext_ty_t) = compressed_exts in
let? h_data_decompressed = (ext_comp.expected_comp ext_ty_h).decompress temp compressed_ext_h in
let h_decompressed: extension bytes = {
extension_type = ext_ty_h;
extension_data = h_data_decompressed.extension_data;
} in
let? t_decompressed = decompress_expected_extensions ext_comp temp ext_ty_t compressed_ext_t in
Some (h_decompressed::t_decompressed <: exts:list (extension bytes){get_extensions_types exts == ext_tys})
)
#pop-options
#push-options "--ifuel 3 --fuel 3"
val decompress_compress_expected_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
exts:list (extension bytes) ->
Lemma
(requires Some? (compress_expected_extensions ext_comp temp exts))
(ensures decompress_expected_extensions ext_comp temp (get_extensions_types exts) (Some?.v (compress_expected_extensions ext_comp temp exts)) == Some exts)
let rec decompress_compress_expected_extensions #bytes #bl ext_comp temp exts =
match exts with
| [] -> (
assert(decompress_expected_extensions ext_comp temp (get_extensions_types exts) (Some?.v (compress_expected_extensions ext_comp temp exts)) == Some [])
)
| ext_h::ext_t -> (
(ext_comp.expected_comp ext_h.extension_type).decompress_compress temp {extension_data = ext_h.extension_data};
decompress_compress_expected_extensions ext_comp temp ext_t;
assert(decompress_expected_extensions ext_comp temp (get_extensions_types exts) (Some?.v (compress_expected_extensions ext_comp temp exts)) == Some exts)
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val compress_decompress_expected_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
ext_tys:list extension_type ->
compressed_exts:ctls_expected_extensions ext_comp.tys temp ext_tys ->
Lemma
(requires Some? (decompress_expected_extensions ext_comp temp ext_tys compressed_exts))
(ensures compress_expected_extensions ext_comp temp (Some?.v (decompress_expected_extensions ext_comp temp ext_tys compressed_exts)) == Some compressed_exts)
let rec compress_decompress_expected_extensions #bytes #bl ext_comp temp ext_tys compressed_exts =
match ext_tys with
| [] -> ()
| h_ety::t_ety -> (
let (h_ce, t_ce): ctls_expected_extensions ext_comp.tys temp (h_ety::t_ety) = compressed_exts in
(ext_comp.expected_comp h_ety).compress_decompress temp h_ce;
compress_decompress_expected_extensions ext_comp temp t_ety t_ce
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val compress_additional_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
exts:list (extension bytes) ->
option (list (compressed_extension bytes ext_comp.tys temp))
let rec compress_additional_extensions #bytes #bl ext_comp temp exts =
match exts with
| [] -> Some []
| h::t -> (
let? h_compressed = (ext_comp.additional_comp h.extension_type).compress temp {extension_data = h.extension_data} in
let? t_compressed = compress_additional_extensions ext_comp temp t in
Some (({
extension_type = h.extension_type;
extension_data = h_compressed;
} <: compressed_extension bytes ext_comp.tys temp)::t_compressed)
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val decompress_additional_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
list (compressed_extension bytes ext_comp.tys temp) ->
option (list (extension bytes))
let rec decompress_additional_extensions #bytes #bl ext_comp temp compressed_exts =
match compressed_exts with
| [] -> Some []
| h::t -> (
let? h_decompressed = (ext_comp.additional_comp h.extension_type).decompress temp h.extension_data in
let? t_decompressed = decompress_additional_extensions ext_comp temp t in
Some (({
extension_type = h.extension_type;
extension_data = h_decompressed.extension_data;
} <: extension bytes)::t_decompressed)
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val decompress_compress_additional_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
exts:list (extension bytes) ->
Lemma
(requires Some? (compress_additional_extensions ext_comp temp exts))
(ensures decompress_additional_extensions ext_comp temp (Some?.v (compress_additional_extensions ext_comp temp exts)) == Some exts)
let rec decompress_compress_additional_extensions #bytes #bl ext_comp temp exts =
match exts with
| [] -> ()
| ext_h::ext_t -> (
(ext_comp.additional_comp ext_h.extension_type).decompress_compress temp {extension_data = ext_h.extension_data};
decompress_compress_additional_extensions ext_comp temp ext_t
)
#pop-options
#push-options "--ifuel 1 --fuel 1"
val compress_decompress_additional_extensions:
#bytes:Type0 -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> temp:ctls_template bytes ->
compressed_exts:list (compressed_extension bytes ext_comp.tys temp) ->
Lemma
(requires Some? (decompress_additional_extensions ext_comp temp compressed_exts))
(ensures compress_additional_extensions ext_comp temp (Some?.v (decompress_additional_extensions ext_comp temp compressed_exts)) == Some compressed_exts)
let rec compress_decompress_additional_extensions #bytes #bl ext_comp temp compressed_exts =
match compressed_exts with
| [] -> ()
| h::t -> (
(ext_comp.additional_comp h.extension_type).compress_decompress temp h.extension_data;
compress_decompress_additional_extensions ext_comp temp t
)
#pop-options
#push-options "--ifuel 2 --fuel 1"
val extensions_compression:
#bytes:eqtype -> {|bytes_like bytes|} ->
ext_comp:ctls_extensions_comp bytes -> get_ext_temp:(ctls_template bytes -> ctls_extensions_template bytes) ->
compression bytes (list (extension bytes)) (extensions_ctls_type ext_comp.tys get_ext_temp) extensions_eq
let extensions_compression #bytes #bl ext_comp get_ext_temp =
let compress (temp:ctls_template bytes) (exts:list (extension bytes)): option (extensions_ctls_type ext_comp.tys get_ext_temp temp) =
let ext_temp = get_ext_temp temp in
let? exts_to_define = extensions_predefined_compress ext_temp.predefined_extensions exts in
let? (exts_expected, exts_additional) = extensions_expected_compress ext_temp.expected_extensions exts_to_define in
let? exts_expected_compressed = compress_expected_extensions ext_comp temp exts_expected in
let? exts_additional_compressed = compress_additional_extensions ext_comp temp exts_additional in
extensions_expected_compress_correct ext_temp.expected_extensions exts_to_define;
if ext_temp.allow_additional then (
let content: ctls_extensions_allow_additional_internal _ _ _ _ = ({
expected_extensions = exts_expected_compressed;
additional_extensions = exts_additional_compressed;
}) in
let content_length = length ((ps_whole_ctls_extensions_allow_additional_internal ext_comp.tys ext_temp.expected_extensions temp).serialize content) in
if content_length < pow2 16 then (
Some ({
length = length ((ps_whole_ctls_extensions_allow_additional_internal ext_comp.tys ext_temp.expected_extensions temp).serialize content);
content;
} <: ctls_extensions_allow_additional bytes ext_comp.tys _ temp)
) else None
) else (
match exts_additional with
| [] -> Some (exts_expected_compressed <: (extensions_ctls_type ext_comp.tys get_ext_temp temp))
| _ -> None
)
in
let decompress (temp:ctls_template bytes) (ctls_exts:extensions_ctls_type ext_comp.tys get_ext_temp temp): option (list (extension bytes)) =
let ext_temp = get_ext_temp temp in
let compressed_expected_exts =
if ext_temp.allow_additional then
ctls_exts.content.expected_extensions
else
ctls_exts
in
let compressed_additional_exts: list (compressed_extension bytes ext_comp.tys temp) =
if ext_temp.allow_additional then
(ctls_exts <: ctls_extensions_allow_additional bytes ext_comp.tys ext_temp.expected_extensions temp).content.additional_extensions
else
[]
in
let? expected_exts = decompress_expected_extensions ext_comp temp ext_temp.expected_extensions compressed_expected_exts in
let? additional_exts = decompress_additional_extensions ext_comp temp compressed_additional_exts in
let exts_expected_and_additional = extensions_expected_decompress expected_exts additional_exts in
Some (extensions_predefined_decompress ext_temp.predefined_extensions exts_expected_and_additional)
in
{
compress;
decompress;
decompress_compress = (fun temp exts ->
match compress temp exts with
| None -> ()
| Some _ -> (
let ext_temp = get_ext_temp temp in