-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpke.curveprime.base.oneshot.coll.cv
1106 lines (798 loc) · 30.4 KB
/
hpke.curveprime.base.oneshot.coll.cv
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
(* Mechanised cryptographic proofs for HPKE using CryptoVerif.
Following as close as possible the notation of the specification at
https://cfrg.github.io/draft-irtf-cfrg-hpke/draft-irtf-cfrg-hpke.html
Generate individual model files with:
./run false
Generate and run (takes a while and writes a lot of files to disk):
./run
Run individual model with (example file):
f=hpke.curveprime.base.oneshot; mkdir -p $f; cryptoverif -oproof $f/$f.proof -o $f $f.cv
2019-2020, Benjamin Lipp, INRIA Paris, Prosecco
*)
(* BEGIN m4 variables are processed that are given as arguments. *)
(* mode_base is es:
- no skS
- no psk
- corrupting skR leads to trivial break *)
(* TODO Use m4_authqueries to put success only when needed *)
(* END *)
(* BEGIN process m4 variables created from input variables. *)
(* END *)
(* BEGIN other m4 macros. *)
(* The macro m4_out_game outputs the current game to a file.
The file name contains a number that is increased at each output. *)
(* END *)
proof {
SArename pkE_4; (* the proof is significantly slower without *)
out_game "g01.out.cv";
insert after "Marshal(pkE_5"
"find j <= N_initiators_base suchthat
defined(pkE_9[j]) && pkE_5 = pkE_9[j] then";
out_game "g02.out.cv";
simplify;
out_game "g03.out.cv";
insert after "in(ch1_1\\["
"let extract_input(psk: psk_t, G_to_bitstring(zz: G_t)) = x1 in";
out_game "g03.1.out.cv";
simplify;
out_game "g03.2.out.cv";
crypto rom(Extract_inner);
simplify;
out_game "g04.out.cv";
simplify;
out_game "g05.out.cv";
crypto gdh(exp) z_4;
simplify;
out_game "g06.out.cv";
crypto prf(Expand) **;
simplify;
crypto truncate(truncate_to_Nn) **;
crypto truncate(truncate_to_Nk) **;
simplify;
out_game "g07.out.cv";
crypto int_ctxt(Seal_inner) **;
simplify;
out_game "g08.out.cv";
crypto ind_cpa(Seal_inner) **;
success
}
(* TODO make sure the channel names are all unique *)
channel c_start, c_setup, c_config_init, c_init, c_resp.
channel c_config_init_sw, c_init_sw.
channel c_init_send, c_init_send_config.
channel c_resp_mult.
channel c_resp_send_config.
channel c_msgs_send_config, c_msgs_send, c_msgs_recv, c_msgs_recv_finish.
channel c_corrupt_skS, c_corrupt_skR, c_corrupt_psk, c_corrupt_skS_psk.
(* This session index is not part of the specification but assumed
by the model. In an implementation, a session index is likely needed
to match subsequent messages to a session and thus the appropriate
decryption context. The session index could be explicit, or implicit,
i.e. a TCP connection. *)
(*type session_index_t [large,fixed].
table sent_seqs(session_index_t, nonce_t).
table rcvd_seqs(session_index_t, nonce_t).*)
type psk_t [large,fixed].
const default_psk: psk_t.
fun psk_to_bitstring(psk_t): bitstring [data].
const default_pskID: bitstring.
const default_pskID_hash: bitstring.
(* We believe an equation like
equation Hash(default_pskID) = default_pskID_hash.
is not necessary, because the default psk is only used in modes where
the psk in not used and thus a formal link between default_psk and
default_pskID is not necessary. *)
const default_pkSm: bitstring.
type expand_t [large,fixed].
(* This covers the maximum length of HKDF-Expand's output. For
RFC 5869, this is 255*Nh. *)
type key_t [large,fixed].
type nonce_t [large,fixed].
(* Application constants *)
const app_info: bitstring.
expand Xor(
nonce_t,
xor,
nonce_zero (* also used for seq that starts at zero *)
).
(* DH-based KEM *)
(* For P-256 and P-521
- to model Unmarshal(), use a left-hand-side pattern matching of Marshal
- TODO figure out if Marshal and G_to_bitstring are actually the same:
I think they are the same, because looking at the F* specs for
P256 and Curve25519, the scalarmult function is returning an
encoded point.
*)
type G_t [bounded,large].
fun Marshal(G_t): bitstring [data].
fun G_to_bitstring(G_t): bitstring [data].
type Z_t [bounded,large].
expand DH_good_group(
G_t,
Z_t,
g,
exp,
exp_1,
mult
).
proba P_GDH.
expand GDH(
(* types *)
G_t, (* Group elements *)
Z_t, (* Exponents *)
(* variables *)
g, (* a generator of the group *)
exp, (* exponentiation function *)
exp_1, (* a symbol that replaces exp after game transformation *)
mult, (* multiplication function for exponents *)
(* probabilities *)
P_GDH (* probability of breaking the GDH assumption *)
).
const default_pkS: G_t.
equation Marshal(default_pkS) = default_pkSm.
letfun DH(exponent: Z_t, group_element: G_t) =
exp(group_element, exponent).
letfun pk(exponent: Z_t) =
exp(g, exponent).
letfun GenerateKeyPair() =
new z: Z_t;
(z, exp(g, z)).
(* Key Derivation Function *)
type hash_key_t [fixed].
type hash_output_t [large,fixed].
fun hash_output_to_bitstring(hash_output_t): bitstring [data].
proba P_hash. (* probability of breaking collision resistance *)
expand CollisionResistant_hash(
(* types *)
hash_key_t, (* key of the hash function, models the choice of *)
(* the hash function *)
bitstring, (* input that gets hashed *)
hash_output_t, (* output type of the hash function *)
(* functions *)
Hash_inner, (* name of the hash function: *)
(* hash(hashkey_t, bitstring): hash_output_t *)
(* processes *)
Hash_oracle, (* name of the oracle that will make available the *)
(* hash key to the attacker *)
(* parameters *)
P_hash (* probability of breaking collision resistance *)
).
letfun Hash(key_hash: hash_key_t, input: bitstring) =
hash_output_to_bitstring(Hash_inner(key_hash, input)).
(*
Extract(salt, IKM):
Extract a pseudorandom key of fixed length from
input keying material IKM and an optional octet string salt.
Extract(salt, IKM) is HMAC-Hash(salt, IKM)
*)
type extract_input_t.
fun extract_input(psk_t, bitstring): extract_input_t [data].
type extract_auth_input_t.
fun extract_auth_input(psk_t, bitstring): extract_auth_input_t [data].
type extract_output_t [large,fixed]. (* size: Nh bytes *)
expand ROM_hash_1(
(* types *)
hash_key_t,
extract_input_t,
extract_output_t,
(* functions *)
Extract_inner,
(* processes *)
Extract_oracle,
(* parameters *)
N_qExtract (* number of queries to the oracle by the adversary *)
).
expand ROM_hash_1(
(* types *)
hash_key_t,
extract_auth_input_t,
extract_output_t,
(* functions *)
ExtractAuth_inner,
(* processes *)
ExtractAuth_oracle,
(* parameters *)
N_qExtractAuth (* number of queries to the oracle by the adversary *)
).
letfun Extract(key_extract: hash_key_t, psk: psk_t, zz: bitstring) =
Extract_inner(key_extract, extract_input(psk, zz)).
letfun ExtractAuth(key_extract_auth: hash_key_t, psk: psk_t, zz: bitstring) =
ExtractAuth_inner(key_extract_auth, extract_auth_input(psk, zz)).
(*
Expand(PRK, info, L):
Expand a pseudorandom key PRK using optional string info into L bytes
of output keying material.
*)
proba P_PRF.
expand PRF_large(
extract_output_t,
bitstring,
expand_t,
Expand,
P_PRF
).
def truncate(input_t, output_t, truncate_f) {
param N.
fun truncate_f(input_t): output_t.
(* If we truncate a uniformly distributed random value,
we obtain a uniformly distributed random value *)
equiv(truncate(truncate_f))
foreach i<=N do h <-R input_t;
O_trunc() := return(truncate_f(h))
<=(0)=>
foreach i<=N do k <-R output_t;
O_trunc() := return(k).
}
expand truncate(
expand_t,
nonce_t,
truncate_to_Nn
).
expand truncate(
expand_t,
key_t,
truncate_to_Nk
).
(* Nh: The output size of the Hash and Extract functions *)
expand truncate(
expand_t,
extract_output_t,
truncate_to_Nh
).
letfun Expand_Nk(key: extract_output_t, input: bitstring) =
truncate_to_Nk(Expand(key, input)).
letfun Expand_Nn(key: extract_output_t, input: bitstring) =
truncate_to_Nn(Expand(key, input)).
letfun Expand_Nh(key: extract_output_t, input: bitstring) =
truncate_to_Nh(Expand(key, input)).
(* An AEAD encryption algorithm *)
proba P_cpa.
proba P_ctxt.
expand AEAD_nonce(
(* types *)
key_t,
bitstring, (* plaintext *)
bitstring, (* ciphertext *)
bitstring, (* additional data *)
nonce_t,
(* functions *)
Seal_inner,
Open_inner,
injbot, (* injection from plaintext to bitstringbot:
(* injbot(plaintext): bitstringbot *)
Zero, (* returns a plaintext of same length, consisting of zeros:
(* Zero(plaintext): plaintext *)
(* probabilities *)
P_cpa,
P_ctxt
).
letfun Seal(key: key_t, nonce: nonce_t, aad: bitstring, pt: bitstring) =
Seal_inner(pt, aad, key, nonce).
letfun Open(key: key_t, nonce: nonce_t, aad: bitstring, ct: bitstring) =
Open_inner(ct, aad, key, nonce).
(***********************************************************************
The following is part of boolean_choice.cvl
inspired by some CryptoVerif examples and Bruno Blanchet
***********************************************************************)
def boolean_choice(value_t, test) {
fun test(bool, value_t, value_t) : value_t.
equation forall x:value_t, y:value_t; test(true, x, y) = x.
equation forall x:value_t, y:value_t; test(false, x, y) = y.
(* Knowing the equations defined above, this can be deduced, but
CryptoVerif can’t do this on its own. *)
equation forall x:value_t, b:bool; test(b,x,x) = x.
}
(* Zero needs to be defined already, typically by the AEAD scheme that’s
* expanded somewhere before.
*)
def boolean_choice_for_encryption(value_t, Zero, test) {
expand boolean_choice(value_t, test).
(* Knowing the equations defined above, this can be deduced, but
CryptoVerif can’t do this on its own. *)
equation forall x:value_t, y:value_t, b:bool; Zero(test(b,x,y)) = test (b,Zero(x),Zero(y)).
}
(* Define a function for choosing from two attacker-provided plaintexts based
on a bit. Also, defines some equations on it so CryptoVerif is able
to reason about it. *)
expand boolean_choice_for_encryption(
(* types *)
bitstring, (* type of the values *)
(* functions *)
Zero, (* the Zero function provided by the encryption scheme. *)
(* Needed for some equations about the function. *)
test (* Name of the choice function: *)
(* test(bool, bitstring, bitstring): bitstring *)
).
(* 5.1. DH-Based KEM *)
type Encap_res_t.
fun Encap_success(bitstring, bitstring): Encap_res_t [data].
const Encap_fail: Encap_res_t.
equation forall zz: bitstring, enc: bitstring;
Encap_success(zz, enc) <> Encap_fail.
letfun Encap(pkR: G_t) =
let (skE: Z_t, pkE: G_t) = GenerateKeyPair() in
(
let zz: bitstring = G_to_bitstring(DH(skE, pkR)) in
let enc: bitstring = Marshal(pkE) in
Encap_success(zz, enc)
) else (
Encap_fail
).
type Decap_res_t.
fun Decap_success(bitstring): Decap_res_t [data].
const Decap_fail: Decap_res_t.
equation forall zz: bitstring; Decap_success(zz) <> Decap_fail.
letfun Decap(enc: bitstring, skR: Z_t) =
let Marshal(pkE: G_t) = enc in
(
Decap_success(G_to_bitstring(DH(skR, pkE)))
) else (
Decap_fail
).
fun concat2G(G_t, G_t): bitstring [data].
type AuthEncap_res_t.
fun AuthEncap_success(bitstring, bitstring): AuthEncap_res_t [data].
const AuthEncap_fail: AuthEncap_res_t.
equation forall zz: bitstring, enc: bitstring;
AuthEncap_success(zz, enc) <> AuthEncap_fail.
letfun AuthEncap(pkR: G_t, skS: Z_t) =
let (skE: Z_t, pkE: G_t) = GenerateKeyPair() in
(
let zz: bitstring = concat2G(DH(skE, pkR), DH(skS, pkR)) in
let enc: bitstring = Marshal(pkE) in
AuthEncap_success(zz, enc)
) else (
AuthEncap_fail
).
type AuthDecap_res_t.
fun AuthDecap_success(bitstring): AuthDecap_res_t [data].
const AuthDecap_fail: AuthDecap_res_t.
equation forall zz: bitstring; AuthDecap_success(zz) <> AuthDecap_fail.
letfun AuthDecap(enc: bitstring, skR: Z_t, pkS: G_t) =
let Marshal(pkE: G_t) = enc in
(
AuthDecap_success(concat2G(DH(skR, pkE), DH(skR, pkS)))
) else (
AuthDecap_fail
).
(* Encryption Context *)
type mode_t [fixed].
const mode_base: mode_t.
const mode_psk: mode_t.
const mode_auth: mode_t.
const mode_auth_psk: mode_t.
type two_bytes [fixed].
const kem_id: two_bytes.
const kdf_id: two_bytes.
const aead_id: two_bytes.
fun concat2bitstring(bitstring, bitstring): bitstring [data].
fun concat3(two_bytes, two_bytes, two_bytes): bitstring [data].
fun concat7(mode_t, bitstring, bitstring, bitstring,
bitstring, bitstring, bitstring): bitstring [data].
const hpke_key: bitstring.
const hpke_nonce: bitstring.
const hpke_exp: bitstring.
type context_t [large,fixed].
(* key, nonce, seq, exporter_secret *)
fun Context(key_t, nonce_t, nonce_t, extract_output_t): context_t [data].
type Context_new_seq_res_t.
fun Context_new_seq_success(context_t): Context_new_seq_res_t [data].
const Context_new_seq_fail: Context_new_seq_res_t.
equation forall ctx: context_t;
Context_new_seq_success(ctx) <> Context_new_seq_fail.
letfun Context_new_seq(ctx: context_t, seq: nonce_t) =
let Context(key: key_t, nonce: nonce_t, seq_old: nonce_t, exporter_secret_old: extract_output_t) = ctx in
(
Context_new_seq_success(Context(key, nonce, seq, exporter_secret_old))
) else (
Context_new_seq_fail
).
letfun VerifyMode(mode: mode_t, psk: psk_t, pskID: bitstring, pkSm: bitstring) =
let got_psk: bool = not(psk = default_psk) && not(pskID = default_pskID) in
let no_psk: bool = (psk = default_psk) && (pskID = default_pskID) in
let got_pkSm: bool = not(pkSm = default_pkSm) in
let no_pkSm: bool = (pkSm = default_pkSm) in
if (mode = mode_base && (got_psk || got_pkSm)) then (false) else (
if (mode = mode_psk && (no_psk || got_pkSm)) then (false) else (
if (mode = mode_auth && (got_psk || no_pkSm) ) then (false) else (
if (mode = mode_auth_psk && (no_psk || no_pkSm) ) then (false) else (
true)))).
(* We provide pskID_hash and info_hash as parameters to simplify
the model. They are either way the same for all protocol
executions in this model, and then the random oracle doesn't
blow up inside KeySchedule. *)
type KeySchedule_res_t.
fun KeySchedule_success(context_t): KeySchedule_res_t [data].
const KeySchedule_fail: KeySchedule_res_t.
equation forall ctx: context_t;
KeySchedule_success(ctx) <> KeySchedule_fail.
letfun KeySchedule(key_hash: hash_key_t, key_extract: hash_key_t,
mode: mode_t, pkR: G_t,
zz: bitstring, enc: bitstring, info_hash: bitstring,
psk: psk_t, pskID: bitstring, pskID_hash: bitstring, pkSm: bitstring) =
if VerifyMode(mode, psk, pskID, pkSm) then
(
let pkRm: bitstring = Marshal(pkR) in
let ciphersuite: bitstring = concat3(kem_id, kdf_id, aead_id) in
let context: bitstring = concat7(mode, ciphersuite, enc, pkRm, pkSm,
pskID_hash, info_hash) in
(* secret is a reserved keyword in CryptoVerif *)
let secrett: extract_output_t = Extract(key_extract, psk, zz) in
let key: key_t = Expand_Nk(secrett, concat2bitstring(hpke_key, context)) in
let nonce: nonce_t = Expand_Nn(secrett, concat2bitstring(hpke_nonce, context)) in
let exporter_secret: extract_output_t = Expand_Nh(secrett, concat2bitstring(hpke_exp, context)) in
KeySchedule_success(Context(key, nonce, nonce_zero, exporter_secret))
) else (
KeySchedule_fail
).
(* Encryption to a public key *)
type SetupBaseI_res_t.
fun SetupBaseI_success(bitstring, context_t): SetupBaseI_res_t [data].
const SetupBaseI_fail: SetupBaseI_res_t.
equation forall enc: bitstring, ctx: context_t;
SetupBaseI_success(enc, ctx) <> SetupBaseI_fail.
letfun SetupBaseI(key_hash: hash_key_t, key_extract: hash_key_t,
pkR: G_t, info_hash: bitstring) =
let Encap_success(zz: bitstring, enc: bitstring) = Encap(pkR) in
(
let KeySchedule_success(ctx: context_t) = KeySchedule(key_hash, key_extract, mode_base, pkR, zz, enc, info_hash, default_psk, default_pskID, default_pskID_hash, default_pkSm) in (
SetupBaseI_success(enc, ctx)
) else (
SetupBaseI_fail
)
) else (
SetupBaseI_fail
).
type SetupBaseR_res_t.
fun SetupBaseR_success(context_t): SetupBaseR_res_t [data].
const SetupBaseR_fail: SetupBaseR_res_t.
equation forall ctx: context_t;
SetupBaseR_success(ctx) <> SetupBaseR_fail.
letfun SetupBaseR(key_hash: hash_key_t, key_extract: hash_key_t,
enc: bitstring, skR: Z_t, info_hash: bitstring) =
let Decap_success(zz: bitstring) = Decap(enc, skR) in
(
let KeySchedule_success(ctx: context_t) = KeySchedule(key_hash, key_extract, mode_base, pk(skR), zz, enc, info_hash, default_psk, default_pskID, default_pskID_hash, default_pkSm) in
(
SetupBaseR_success(ctx)
) else (
SetupBaseR_fail
)
) else (
SetupBaseR_fail
).
(* Authentication using a Pre-Shared Key *)
type SetupPSKI_res_t.
fun SetupPSKI_success(bitstring, context_t): SetupPSKI_res_t [data].
const SetupPSKI_fail: SetupPSKI_res_t.
equation forall enc: bitstring, ctx: context_t;
SetupPSKI_success(enc, ctx) <> SetupPSKI_fail.
letfun SetupPSKI(key_hash: hash_key_t, key_extract: hash_key_t,
pkR: G_t, info_hash: bitstring,
psk: psk_t, pskID: bitstring, pskID_hash: bitstring) =
let Encap_success(zz: bitstring, enc: bitstring) = Encap(pkR) in
(
let KeySchedule_success(ctx: context_t) = KeySchedule(key_hash, key_extract, mode_psk, pkR, zz, enc, info_hash, psk, pskID, pskID_hash, default_pkSm) in
(
SetupPSKI_success(enc, ctx)
) else (
SetupPSKI_fail
)
) else (
SetupPSKI_fail
).
type SetupPSKR_res_t.
fun SetupPSKR_success(context_t): SetupPSKR_res_t [data].
const SetupPSKR_fail: SetupPSKR_res_t.
equation forall ctx: context_t;
SetupPSKR_success(ctx) <> SetupPSKR_fail.
letfun SetupPSKR(key_hash: hash_key_t, key_extract: hash_key_t,
enc: bitstring, skR: Z_t, info_hash: bitstring,
psk: psk_t, pskID: bitstring, pskID_hash: bitstring) =
let Decap_success(zz: bitstring) = Decap(enc, skR) in
(
let KeySchedule_success(ctx: context_t) = KeySchedule(key_hash, key_extract, mode_psk, pk(skR), zz, enc, info_hash, psk, pskID, pskID_hash, default_pkSm) in
(
SetupPSKR_success(ctx)
) else (
SetupPSKR_fail
)
) else (
SetupPSKR_fail
).
(* Encryption and Decryption *)
letfun Context_Nonce(nonce: nonce_t, seq: nonce_t) =
(* We suppose that seq has already the length of the nonce, by
assigning it the type nonce_t. *)
xor(nonce, seq).
type Context_Seal_res_t.
fun Context_Seal_success(bitstring): Context_Seal_res_t [data].
const Context_Seal_fail: Context_Seal_res_t.
equation forall ct: bitstring;
Context_Seal_success(ct) <> Context_Seal_fail.
letfun Context_Seal(context: context_t, aad: bitstring,
pt: bitstring) =
let Context(key: key_t, nonce: nonce_t, seq: nonce_t, exporter_secret_unused: extract_output_t) = context in
(
let ct: bitstring = Seal(key, Context_Nonce(nonce, seq), aad, pt) in
(* TODO model increment seq (probably outside of this function)
self.seq += 1 *)
Context_Seal_success(ct)
) else (
Context_Seal_fail
).
type Context_Open_res_t.
fun Context_Open_success(bitstring): Context_Open_res_t [data].
const Context_Open_fail: Context_Open_res_t.
equation forall pt: bitstring;
Context_Open_success(pt) <> Context_Open_fail.
letfun Context_Open(context: context_t, aad: bitstring,
ct: bitstring) =
let Context(key: key_t, nonce: nonce_t, seq: nonce_t, exporter_secret_unused: extract_output_t) = context in
(
let injbot(pt: bitstring) = Open(key, Context_Nonce(nonce, seq),
aad, ct) in
(
(* TODO model increment seq (probably outside of this function)
self.seq += 1 *)
Context_Open_success(pt)
) else (
Context_Open_fail
)
) else (
Context_Open_fail
).
type Context_Export_res_t.
fun Context_Export_success(expand_t): Context_Export_res_t [data].
const Context_Export_fail: Context_Export_res_t.
equation forall exported: expand_t;
Context_Export_success(exported) <> Context_Export_fail.
(* Context_Export directly outputs the maximum length (for HKDF it is
255*Hashlen) and the truncation is left to the user.
This simplifies the model, as we can re-use the same Expand function. *)
letfun Context_Export(context: context_t, exporter_context: bitstring) =
let Context(key: key_t, nonce: nonce_t, seq: nonce_t, exporter_secret_here: extract_output_t) = context in
(
let exported: expand_t = Expand(exporter_secret_here, exporter_context) in
Context_Export_success(exported)
) else (
Context_Export_fail
).
(* Single-Shot APIs *)
type SealBase_res_t.
fun SealBase_success(bitstring, bitstring): SealBase_res_t [data].
const SealBase_fail: SealBase_res_t.
equation forall enc: bitstring, ct: bitstring;
SealBase_success(enc, ct) <> SealBase_fail.
letfun SealBase(key_hash: hash_key_t, key_extract: hash_key_t,
pkR: G_t, info_hash: bitstring, aad: bitstring,
pt: bitstring) =
let SetupBaseI_success(enc: bitstring, ctx: context_t) =
SetupBaseI(key_hash, key_extract, pkR, info_hash) in
(
let Context_Seal_success(ct: bitstring) =
Context_Seal(ctx, aad, pt) in
(
SealBase_success(enc, ct)
) else (
SealBase_fail
)
) else (
SealBase_fail
).
type OpenBase_res_t.
fun OpenBase_success(Context_Open_res_t): OpenBase_res_t [data].
const OpenBase_fail: OpenBase_res_t.
equation forall ctx_open: Context_Open_res_t;
OpenBase_success(ctx_open) <> OpenBase_fail.
letfun OpenBase(key_hash: hash_key_t, key_extract: hash_key_t,
enc: bitstring, skR: Z_t, info_hash: bitstring,
aad: bitstring, ct: bitstring) =
let SetupBaseR_success(ctx: context_t) =
SetupBaseR(key_hash, key_extract, enc, skR, info_hash) in
(
OpenBase_success(Context_Open(ctx, aad, ct))
) else (
OpenBase_fail
).
type SealPSK_res_t.
fun SealPSK_success(bitstring, bitstring): SealPSK_res_t [data].
const SealPSK_fail: SealPSK_res_t.
equation forall enc: bitstring, ct: bitstring;
SealPSK_success(enc, ct) <> SealPSK_fail.
letfun SealPSK(key_hash: hash_key_t, key_extract: hash_key_t,
pkR: G_t, info_hash: bitstring, aad: bitstring, pt: bitstring,
psk: psk_t, pskID: bitstring, pskID_hash: bitstring) =
let SetupPSKI_success(enc: bitstring, ctx: context_t) =
SetupPSKI(key_hash, key_extract, pkR, info_hash, psk, pskID, pskID_hash) in
(
let Context_Seal_success(ct: bitstring) = Context_Seal(ctx, aad, pt) in
(
SealPSK_success(enc, ct)
) else (
SealPSK_fail
)
) else (
SealPSK_fail
).
type OpenPSK_res_t.
fun OpenPSK_success(Context_Open_res_t): OpenPSK_res_t [data].
const OpenPSK_fail: OpenPSK_res_t.
equation forall ctx_open: Context_Open_res_t;
OpenPSK_success(ctx_open) <> OpenPSK_fail.
letfun OpenPSK(key_hash: hash_key_t, key_extract: hash_key_t,
enc: bitstring, skR: Z_t, info_hash: bitstring,
aad: bitstring, ct: bitstring,
psk: psk_t, pskID: bitstring, pskID_hash: bitstring) =
let SetupPSKR_success(ctx: context_t) =
SetupPSKR(key_hash, key_extract, enc, skR, info_hash, psk, pskID, pskID_hash) in
(
OpenPSK_success(Context_Open(ctx, aad, ct))
) else (
OpenPSK_fail
).
(* TODO make sure the params used are all unique *)
param N_initiators_base, N_initiators_base_sw.
param N_initiators_auth, N_initiators_auth_sw.
param N_initiators_psk, N_initiators_psk_sw.
param N_initiators_auth_psk, N_initiators_auth_psk_sw.
param N_initiators_mult.
param N_initiators_mult_send.
param N_responders_base, N_responders_base_sw.
param N_responders_auth, N_responders_auth_sw.
param N_responders_psk, N_responders_psk_sw.
param N_responders_auth_psk, N_responders_auth_psk_sw.
param N_initiators_psk_adv, N_responders_psk_adv.
param N_initiators_auth_psk_adv, N_responders_auth_psk_adv.
param N_responders_mult.
param N_responders_mult_recv.
param N_responders_mult_send.
param N_msgs_send, N_msgs_recv.
(* In one-shot, the initiator does not receive the encryption
context from the API. This means it cannot send subsequent messages.
*)
letfun has_secrecy(pkX: G_t, pkR: G_t) =
pkX = pkR
.
letfun has_auth() =
false
.
const exp_ctx_1: bitstring.
const exp_ctx_2: bitstring.
let Initiator_Base(key_hash: hash_key_t, key_extract: hash_key_t, info_hash: bitstring,
b_I: bool, pkR: G_t) =
! i_N_initiators <= N_initiators_base
in(c_config_init, (pkX: G_t, pt1: bitstring, pt2: bitstring,
aad: bitstring));
if Zero(pt1) = Zero(pt2) then
let bit: bool = if has_secrecy(pkX, pkR) then b_I else false in
let pt: bitstring = test(bit, pt1, pt2) in
(* oneshot *)
let SealBase_success(enc: bitstring, ct: bitstring) =
SealBase(key_hash, key_extract, pkX, info_hash, aad, pt) in
out(c_init, (enc, ct, aad))
.
let Responder_Base(key_hash: hash_key_t, key_extract: hash_key_t, info_hash: bitstring,
skR: Z_t) =
! i_N_responders <= N_responders_base
in(c_resp, (enc: bitstring, ct: bitstring, aad: bitstring));
let OpenBase_success(Context_Open_success(pt: bitstring)) =
OpenBase(key_hash, key_extract, enc, skR, info_hash, aad, ct) in
out(c_resp, ())
.
let Initiator_Base_swap(key_hash: hash_key_t,
key_extract: hash_key_t, info_hash: bitstring, pkR: G_t) =
! i_N_initiators_sw <= N_initiators_base_sw
in(c_config_init_sw, (pkX: G_t, pt: bitstring, aad: bitstring));
(* oneshot *)
let SealBase_success(enc: bitstring, ct: bitstring) =
SealBase(key_hash, key_extract, pkX, info_hash, aad, pt) in
out(c_init_sw, (enc, ct, aad)).
let Responder_Base_swap(key_hash: hash_key_t,
key_extract: hash_key_t, info_hash: bitstring, skR: Z_t) =
! i_N_responders_sw <= N_responders_base_sw
in(c_resp, (enc: bitstring, ct: bitstring, aad: bitstring));
let OpenBase_success(Context_Open_success(pt: bitstring)) =
OpenBase(key_hash, key_extract, enc, skR, info_hash, aad, ct) in
out(c_resp, ())
.
event rcvd(
bool, (* clean_session *)
mode_t,
G_t, (* pkR *)
G_t, (* pkS *)
bitstring, (* pskID *)
bitstring, (* info *)
bitstring, (* aad *)
bitstring (* plaintext *)
).
event sent(
mode_t,
G_t, (* pkR *)
G_t, (* pkS *)
bitstring, (* pskID *)
bitstring, (* info *)
bitstring, (* aad *)
bitstring (* plaintext *)
).
let Initiator_PSK(key_hash: hash_key_t, key_extract: hash_key_t, info_hash: bitstring,
b_I: bool, pkR: G_t, psk: psk_t, pskID: bitstring, pskID_hash: bitstring) =
! i_N_initiators <= N_initiators_psk
in(c_config_init, (pkX: G_t, pt1: bitstring, pt2: bitstring,
aad: bitstring));
if pkX = pkR then
if Zero(pt1) = Zero(pt2) then
let bit: bool = if has_secrecy(pkX, pkR) then b_I else false in
let pt: bitstring = test(bit, pt1, pt2) in
let SealPSK_success(enc: bitstring, ct: bitstring) =
SealPSK(key_hash, key_extract, pkX, info_hash, aad, pt, psk, pskID, pskID_hash) in
event sent(mode_psk, pkX, default_pkS, pskID, app_info, aad, pt);
out(c_init, (enc, ct, aad, pskID))
.
let Responder_PSK(key_hash: hash_key_t, key_extract: hash_key_t, info_hash: bitstring,
skR: Z_t, pkR: G_t, psk: psk_t, pskID: bitstring, pskID_hash: bitstring) =
! i_N_responders <= N_responders_psk
in(c_resp, (enc: bitstring, ct: bitstring, aad: bitstring, pskID_recv: bitstring));
if pskID_recv = pskID then
let bool_has_auth: bool = has_auth() in
let OpenPSK_success(Context_Open_success(pt: bitstring)) =
OpenPSK(key_hash, key_extract, enc, skR, info_hash, aad, ct, psk, pskID, pskID_hash) in