-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAwsCryptographyDbEncryptionSdkStructuredEncryptionOperations.dfy
1224 lines (1080 loc) · 59.3 KB
/
AwsCryptographyDbEncryptionSdkStructuredEncryptionOperations.dfy
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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
include "../Model/AwsCryptographyDbEncryptionSdkStructuredEncryptionTypes.dfy"
include "../../../../submodules/MaterialProviders/libraries/src/Collections/Maps/Maps.dfy"
include "Header.dfy"
include "Footer.dfy"
include "Paths.dfy"
include "Crypt.dfy"
include "Util.dfy"
include "Canonize.dfy"
module AwsCryptographyDbEncryptionSdkStructuredEncryptionOperations refines AbstractAwsCryptographyDbEncryptionSdkStructuredEncryptionOperations {
import opened StructuredEncryptionUtil
import opened AwsCryptographyDbEncryptionSdkStructuredEncryptionTypes
import Base64
import CMP = AwsCryptographyMaterialProvidersTypes
import Prim = AwsCryptographyPrimitivesTypes
import StructuredEncryptionHeader
import Random
import Primitives = AtomicPrimitives
import Header = StructuredEncryptionHeader
import Footer = StructuredEncryptionFooter
import MaterialProviders
import Materials
import Crypt = StructuredEncryptionCrypt
import Paths = StructuredEncryptionPaths
import SortedSets
import Seq
import Digest
import Defaults
import HKDF
import AlgorithmSuites
import Maps
import opened Canonize
datatype Config = Config(
primitives : Primitives.AtomicPrimitivesClient,
materialProviders : MaterialProviders.MaterialProvidersClient
)
type InternalConfig = Config
predicate ValidInternalConfig?(config: InternalConfig)
{
&& config.primitives.ValidState()
&& config.materialProviders.ValidState()
}
function ModifiesInternalConfig(config: InternalConfig) : set<object>
{config.primitives.Modifies + config.materialProviders.Modifies}
predicate EncryptStructureEnsuresPublicly(
input: EncryptStructureInput,
output: Result<EncryptStructureOutput, Error>) {
// Ensure the CryptoSchema in the ParsedHeader matches the input crypto Schema, minus any DO_NOTHING terminals
output.Success? ==>
// For now we only support encrypting flat maps
&& var headerSchema := output.value.cryptoSchema;
&& var inputSchema := input.cryptoSchema;
// && (forall k :: k in headerSchema ==> k in inputSchema && inputSchema[k] == headerSchema[k])
&& (forall v :: v in headerSchema.Values ==> IsAuthAttr(v))
}
predicate DecryptStructureEnsuresPublicly(
input: DecryptStructureInput,
output: Result<DecryptStructureOutput, Error>) {
output.Success? ==>
// For now we only support encrypting flat maps
&& var headerSchema := output.value.cryptoSchema;
// && var inputSchema := input.cryptoSchema;
// && (forall k :: k in headerSchema ==> k in inputSchema && inputSchema[k] == headerSchema[k])
&& (forall v :: v in headerSchema.Values ==> IsAuthAttr(v))
}
predicate DecryptPathStructureEnsuresPublicly(
input: DecryptPathStructureInput,
output: Result<DecryptPathStructureOutput, Error>)
{
output.Success? ==> DecryptPathFinal(input.encryptedStructure, output.value.plaintextStructure)
}
predicate EncryptPathStructureEnsuresPublicly(
input: EncryptPathStructureInput,
output: Result<EncryptPathStructureOutput, Error>)
{
output.Success? ==> EncryptPathFinal(input.plaintextStructure, output.value.encryptedStructure)
}
predicate ResolveAuthActionsEnsuresPublicly(
input: ResolveAuthActionsInput,
output: Result<ResolveAuthActionsOutput, Error>) {
true
}
method ResolveAuthActions (config: InternalConfig, input: ResolveAuthActionsInput)
returns (output: Result<ResolveAuthActionsOutput, Error>)
{
:- Need(AuthListHasNoDuplicatesFromSet(input.authActions), E("Duplicate Paths"));
SetSizeImpliesAuthListHasNoDuplicates(input.authActions);
assert AuthListHasNoDuplicates(input.authActions);
var head :- Header.PartialDeserialize(input.headerBytes);
:- Need(ValidString(input.tableName), E("Bad Table Name"));
:- Need(exists x :: x in input.authActions && x.key == HeaderPath, E("Header Required"));
:- Need(exists x :: x in input.authActions && x.key == FooterPath, E("Footer Required"));
var canonData :- ForDecrypt(input.tableName, input.authActions, head.legend);
reveal CanonCryptoMatchesAuthList();
return Success(ResolveAuthActionsOutput(cryptoActions := UnCanon(canonData)));
}
const DBE_COMMITMENT_POLICY := CMP.CommitmentPolicy.DBE(CMP.DBECommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT)
// Fail unless the field exists, and is a binary terminal
function method {:opaque} GetBinary(data : AuthList, path : Path): (result: Result<StructuredDataTerminal, Error>)
ensures result.Success? ==> exists x :: x in data && x.key == path
{
var data := FindAuth(data, path);
if data.None? then
Failure(E("The field name " + Paths.PathToString(path) + " is required."))
else if data.value.data.typeId != BYTES_TYPE_ID then
Failure(E(Paths.PathToString(path) + " must be a binary Terminal."))
else if data.value.action != DO_NOT_SIGN then
Failure(E(Paths.PathToString(path) + " must be DO_NOT_SIGN."))
else
Success(data.value.data)
}
// Return the sum of the sizes of the given fields
function method {:opaque} SumValueSize(fields : CanonCryptoList)
: nat
{
if |fields| == 0 then
0
else if fields[0].action == ENCRYPT_AND_SIGN then
|fields[0].data.value| + SumValueSize(fields[1..])
else
SumValueSize(fields[1..])
}
function method {:opaque} GetAlgorithmSuiteId(alg : Option<CMP.DBEAlgorithmSuiteId>)
: (ret : CMP.AlgorithmSuiteId)
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//= type=implication
//# - Algorithm Suite: If provided, this is the [input algorithm suite](#algorithm-suite);
//# otherwise, this field MUST be the algorithm suite corresponding to the enum
//# [DBE.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384](../../submodules/MaterialProviders/aws-encryption-sdk-specification/framework/algorithm-suites.md#supported-algorithm-suites-enum).
ensures
&& (alg.Some? ==> ret == CMP.AlgorithmSuiteId.DBE(alg.value))
&& (alg.None? ==> ret == CMP.AlgorithmSuiteId.DBE(CMP.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384))
{
if alg.Some? then
CMP.AlgorithmSuiteId.DBE(alg.value)
else
CMP.AlgorithmSuiteId.DBE(CMP.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384)
}
// return the appropriate EncryptionMaterials
method {:opaque} GetStructuredEncryptionMaterials(
cmm : CMP.ICryptographicMaterialsManager,
encryptionContext : Option<CMP.EncryptionContext>,
algorithmSuiteId: Option<CMP.DBEAlgorithmSuiteId>,
encryptedTerminalDataNum : nat,
totalEncryptedTerminalValuesSize : nat
)
returns (ret : Result<CMP.EncryptionMaterials, Error>)
ensures ret.Success? ==>
&& var mat := ret.value;
&& Materials.EncryptionMaterialsHasPlaintextDataKey(mat)
&& ValidSuite(mat.algorithmSuite)
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//= type=implication
//# This operation MUST obtain a set of encryption materials by calling
//# [Get Encryption Materials](../../submodules/MaterialProviders/aws-encryption-sdk-specification/framework/cmm-interface.md#get-encryption-materials)
//# on the [CMM](#cmm) calculated above.
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//= type=implication
//# This operation MUST call Get Encryption Materials on the CMM as follows.
&& (|cmm.History.GetEncryptionMaterials| == |old(cmm.History.GetEncryptionMaterials)| + 1)
&& Seq.Last(cmm.History.GetEncryptionMaterials).output.Success?
&& var getEncIn := Seq.Last(cmm.History.GetEncryptionMaterials).input;
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//= type=implication
//# - Encryption Context: This MUST be the encryption context calculated above.
&& (|| (encryptionContext.None? && getEncIn.encryptionContext == map[])
|| (encryptionContext.Some? && getEncIn.encryptionContext == encryptionContext.value))
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//= type=implication
//# - Commitment Policy: This MUST be
//# [REQUIRE_ENCRYPT_REQUIRE_DECRYPT](../../submodules/MaterialProviders/aws-encryption-sdk-specification/framework/commitment-policy.md#esdkrequire_encrypt_require_decrypt).
&& getEncIn.commitmentPolicy == DBE_COMMITMENT_POLICY
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//= type=implication
//# - Max Plaintext Length: This field MUST be the result of the calculation `encryptedTerminalDataNum * 2 + totalEncryptedTerminalValuesSize`
// - `encryptedTerminalDataNum` is the number of [Terminal Data](./structures.md#terminal-data)
// in the [input Structured Data](#structured-data) being encrypted,
// as defined by the [input Crypto Schema](#crypto-schema).
// - `totalEncryptedTerminalValuesSize` is the sum of the length of all [Terminal Values](./structures.md#terminal-value)
// in the [input Structured Data](#structured-data) being encrypted,
// as defined by the [input Crypto Schema](#crypto-schema).
&& var maxLength := encryptedTerminalDataNum * 2 + totalEncryptedTerminalValuesSize;
&& maxLength < INT64_MAX_LIMIT
&& (getEncIn.maxPlaintextLength == Some(maxLength as int64))
modifies cmm.Modifies
requires cmm.ValidState()
ensures cmm.ValidState()
{
var maxLength := encryptedTerminalDataNum * 2 + totalEncryptedTerminalValuesSize;
:- Need(maxLength < INT64_MAX_LIMIT, E("Encrypted Size too long."));
var algId := GetAlgorithmSuiteId(algorithmSuiteId);
var matR := cmm.GetEncryptionMaterials(
CMP.GetEncryptionMaterialsInput(
encryptionContext := encryptionContext.UnwrapOr(map[]),
commitmentPolicy := DBE_COMMITMENT_POLICY,
algorithmSuiteId := Some(algId),
maxPlaintextLength := Some(maxLength as int64),
requiredEncryptionContextKeys := None
)
);
var matOutput :- matR.MapFailure(e => AwsCryptographyMaterialProviders(e));
var mat := matOutput.encryptionMaterials;
:- Need(Materials.EncryptionMaterialsHasPlaintextDataKey(mat), E("Encryption material has no key"));
var alg := mat.algorithmSuite;
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//# If this algorithm suite is not a
//# [supported suite for Database Encryption (DBE)](../../submodules/MaterialProviders/aws-encryption-sdk-specification/framework/algorithm-suites.md#supported-algorithm-suites-enum),
//# this operation MUST yield an error.
:- Need(ValidSuite(alg), E("Invalid Algorithm Suite"));
var key : Key := mat.plaintextDataKey.value;
return Success(mat);
}
method GetV2EncryptionContextCanon(schema : CanonCryptoList)
returns (output : Result<CMP.EncryptionContext, Error>)
{
var canonAttrs : CanonCryptoList := Seq.Filter((s : CanonCryptoItem) => s.action == SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT, schema);
var contextAttrs : CryptoList := Seq.Map((s : CanonCryptoItem) => CryptoItem(key := s.origKey, data := s.data, action := s.action), canonAttrs);
output := GetV2EncryptionContext2(contextAttrs);
}
method GetV2EncryptionContext(schema : CryptoList)
returns (output : Result<CMP.EncryptionContext, Error>)
{
var contextAttrs : CryptoList := Seq.Filter((s : CryptoItem) => s.action == SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT, schema);
//= specification/structured-encryption/encrypt-path-structure.md#create-new-encryption-context-and-cmm
//# Otherwise, this operation MUST add an [entry](../dynamodb-encryption-client/encrypt-item.md#base-context-value-version-2) to the encryption context for every
//# [SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT Crypto Action](./structures.md#sign_and_include_in_encryption_context)
//# [Terminal Data](./structures.md#terminal-data)
//# in the input record, plus the Legend.
output := GetV2EncryptionContext2(contextAttrs);
}
function method {:opaque} Find(haystack : CryptoList, needle : Path) : Result<CryptoItem, Error>
{
if |haystack| == 0 then
Failure(E("Not Found"))
else if haystack[0].key == needle
then Success(haystack[0])
else
Find(haystack[1..], needle)
}
function method {:opaque} FindAuth(haystack : AuthList, needle : Path) : (result : Option<AuthItem>)
ensures result.Some? ==> exists x :: x in haystack && x.key == needle
{
if |haystack| == 0 then
None
else if haystack[0].key == needle
then Some(haystack[0])
else
FindAuth(haystack[1..], needle)
}
function method {:opaque} CountEncrypted(list : CanonCryptoList) : nat
{
if |list| == 0 then
0
else if list[0].action == ENCRYPT_AND_SIGN then
1 + CountEncrypted(list[1..])
else
CountEncrypted(list[1..])
}
method {:vcs_split_on_every_assert} GetV2EncryptionContext2(fields : CryptoList)
returns (output : Result<CMP.EncryptionContext, Error>)
{
var fieldMap : map<ValidUTF8Bytes, Path> := map[];
for i := 0 to |fields|
{
//= specification/structured-encryption/encrypt-path-structure.md#encryption-context-naming
//# When a key-value pair is added to the encryption context,
//# the key MUST be the concatenation of the literal
//# "aws-crypto-attr." and the member strings of the
//# path joined by the '.' character.
var keyVal := ATTR_PREFIX + Paths.PathToString(fields[i].key);
var utf8Value :- UTF8.Encode(keyVal).MapFailure(e =>E(e));
//= specification/structured-encryption/encrypt-path-structure.md#encryption-context-naming
//# An error MUST be returned if an attempt is made to add two
//# different attributes that produce the same encryption context key.
if utf8Value in fieldMap {
return Failure(E(keyVal + " appears twice in encryption context."));
}
fieldMap := fieldMap[utf8Value := fields[i].key];
}
var keys : seq<UTF8.ValidUTF8Bytes> := SortedSets.ComputeSetToOrderedSequence2(fieldMap.Keys, ByteLess);
var newContext : CMP.EncryptionContext := map[];
var legend : string := "";
//= specification/dynamodb-encryption-client/encrypt-item.md#base-context-value-version-2
//# The value MUST be :
//# - If the type is Number or String, the unaltered (already utf8) bytes of the value
//# - If the type if Null, the string "null"
//# - If the type is Boolean, then the string "true" for true and the string "false" for false.
//# - Else, the value as defined in [Base Context Value Version 1](#base-context-value-version-1)
//= specification/structured-encryption/encrypt-path-structure.md#create-new-encryption-context-and-cmm
//# The Legend MUST be named "aws-crypto-legend" and be a string with one character per attribute added above,
//# with a one-to-one correspondence with the attributes sorted by their UTF8 encoding,
//# each character designating the original type of the attribute,
//# to allow reversing of the [encoding](../dynamodb-encryption-client/encrypt-item.md#base-context-value-version-2).
//# - 'S' if the attribute was of type String
//# - 'N' if the attribute was of type Number
//# - 'L' if the attribute was of type Null or Boolean
//# - 'B' otherwise
for i := 0 to |keys|
invariant forall j | 0 <= j < i :: keys[j] in newContext
invariant forall k <- newContext :: k in keys[..i]
invariant |legend| == |newContext| == i
{
assert keys[i] !in newContext by {
reveal Seq.HasNoDuplicates();
}
var fieldUtf8 := keys[i];
var fieldStr := fieldMap[fieldUtf8];
var item :- Find(fields, fieldMap[fieldUtf8]);
var attr : StructuredDataTerminal := item.data;
var attrStr : ValidUTF8Bytes;
var legendChar : char;
if attr.typeId == NULL {
legendChar := LEGEND_LITERAL;
attrStr := NULL_UTF8;
} else if attr.typeId == STRING {
legendChar := LEGEND_STRING;
:- Need(ValidUTF8Seq(attr.value), E("Internal Error : string was not UTF8."));
attrStr := attr.value;
var yy :- expect UTF8.Decode(attrStr);
} else if attr.typeId == NUMBER {
legendChar := LEGEND_NUMBER;
:- Need(ValidUTF8Seq(attr.value), E("Internal Error : number was not UTF8."));
attrStr := attr.value;
} else if attr.typeId == BOOLEAN {
legendChar := LEGEND_LITERAL;
:- Need(|attr.value| == 1, E("Internal Error : boolean was not of length 1."));
attrStr := if attr.value[0] == 0 then FALSE_UTF8 else TRUE_UTF8;
} else {
legendChar := LEGEND_BINARY;
attrStr := EncodeTerminal(attr);
}
assert fieldUtf8 !in newContext;
newContext := newContext[fieldUtf8 := attrStr];
legend := legend + [legendChar];
assert forall j | 0 <= j < i+1 :: keys[j] in newContext by {
assert keys[i] in newContext;
}
}
var utf8Legend :- UTF8.Encode(legend).MapFailure(e =>E(e));
newContext := newContext[LEGEND_UTF8 := utf8Legend];
return Success(newContext);
}
function method {:tailrecursion} BuildCryptoMap2(
keys : seq<string>,
plaintextStructure: StructuredDataMap,
cryptoSchema: CryptoSchemaMap,
ghost origKeys : seq<string> := keys,
acc : CryptoList := []
)
: (ret : Result<CryptoList, Error>)
requires forall k <- keys :: k in plaintextStructure
requires forall k <- keys :: k in cryptoSchema
requires forall k <- acc :: |k.key| == 1
requires CryptoListHasNoDuplicates(acc)
requires |acc| + |keys| == |origKeys|
requires keys == origKeys[|acc|..]
requires forall i | 0 <= i < |acc| :: acc[i].key == Paths.StringToUniPath(origKeys[i])
requires Seq.HasNoDuplicates(keys)
requires Seq.HasNoDuplicates(origKeys)
ensures ret.Success? ==>
&& (forall k <- ret.value :: |k.key| == 1)
&& CryptoListHasNoDuplicates(ret.value)
{
reveal Seq.HasNoDuplicates();
Paths.StringToUniPathUnique();
if |keys| == 0 then
Success(acc)
else
var key := keys[0];
var path := Paths.StringToUniPath(key);
var item := CryptoItem(key := path, data := plaintextStructure[key], action := cryptoSchema[key]);
var newAcc := acc + [item];
BuildCryptoMap2(keys[1..], plaintextStructure, cryptoSchema, origKeys, newAcc)
}
function method BuildCryptoMap(plaintextStructure: StructuredDataMap, cryptoSchema: CryptoSchemaMap) :
(ret : Result<CryptoList, Error>)
requires plaintextStructure.Keys == cryptoSchema.Keys
ensures ret.Success? ==>
&& (forall k <- ret.value :: |k.key| == 1)
&& CryptoListHasNoDuplicates(ret.value)
{
var keys := SortedSets.ComputeSetToOrderedSequence2(plaintextStructure.Keys, CharLess);
BuildCryptoMap2(keys, plaintextStructure, cryptoSchema)
}
function method {:tailrecursion} BuildAuthMap2(
keys : seq<string>,
plaintextStructure: StructuredDataMap,
authSchema: AuthenticateSchemaMap,
ghost origKeys : seq<string> := keys,
acc : AuthList := []
)
: (ret : Result<AuthList, Error>)
requires forall k <- keys :: k in plaintextStructure
requires forall k <- keys :: k in authSchema
requires forall k <- acc :: |k.key| == 1
requires AuthListHasNoDuplicates(acc)
requires |acc| + |keys| == |origKeys|
requires keys == origKeys[|acc|..]
requires forall i | 0 <= i < |acc| :: acc[i].key == Paths.StringToUniPath(origKeys[i])
requires Seq.HasNoDuplicates(keys)
requires Seq.HasNoDuplicates(origKeys)
ensures ret.Success? ==>
&& (forall k <- ret.value :: |k.key| == 1)
&& AuthListHasNoDuplicates(ret.value)
{
reveal Seq.HasNoDuplicates();
if |keys| == 0 then
Success(acc)
else
var key := keys[0];
var path := Paths.StringToUniPath(key);
var item := AuthItem(key := path, data := plaintextStructure[key], action := authSchema[key]);
var newAcc := acc + [item];
BuildAuthMap2(keys[1..], plaintextStructure, authSchema, origKeys, newAcc)
}
function method BuildAuthMap(plaintextStructure: StructuredDataMap, authSchema: AuthenticateSchemaMap)
: (ret : Result<AuthList, Error>)
requires plaintextStructure.Keys == authSchema.Keys
ensures ret.Success? ==>
&& (forall k <- ret.value :: |k.key| == 1)
&& AuthListHasNoDuplicates(ret.value)
{
var keys := SortedSets.ComputeSetToOrderedSequence2(plaintextStructure.Keys, CharLess);
BuildAuthMap2(keys, plaintextStructure, authSchema)
}
function method UnBuildCryptoMap(list : CryptoList, dataSoFar : StructuredDataMap := map[], actionsSoFar : CryptoSchemaMap := map[]) :
(res : Result<(StructuredDataMap, CryptoSchemaMap), Error>)
requires forall k <- actionsSoFar :: k in dataSoFar
requires (forall v :: v in actionsSoFar.Values ==> IsAuthAttr(v))
requires forall k <- list :: |k.key| == 1
ensures res.Success? ==>
&& (forall k <- res.value.1 :: k in res.value.0)
&& (forall v :: v in res.value.1.Values ==> IsAuthAttr(v))
{
if |list| == 0 then
Success((dataSoFar, actionsSoFar))
else
var key :- Paths.UniPathToString(list[0].key);
:- Need(key !in dataSoFar, E("Duplicate Key " + key));
if IsAuthAttr(list[0].action) then
UnBuildCryptoMap(list[1..], dataSoFar[key := list[0].data], actionsSoFar[key := list[0].action])
else
UnBuildCryptoMap(list[1..], dataSoFar[key := list[0].data], actionsSoFar)
}
lemma EncryptStructureOutputHasSinglePaths(origData : CryptoList, finalData : CryptoList)
requires EncryptPathFinal(origData, finalData)
requires forall k <- origData :: |k.key| == 1
ensures forall k <- finalData :: |k.key| == 1
{
reveal EncryptPathFinal();
reveal CryptoUpdatedCryptoListHeader();
reveal NewCryptoUpdatedCrypto();
}
lemma DecryptStructureOutputHasSinglePaths(origData : AuthList, finalData : CryptoList)
requires DecryptPathFinal(origData, finalData)
requires forall k <- origData :: |k.key| == 1
ensures forall k <- finalData :: |k.key| == 1
{
reveal DecryptPathFinal();
reveal AuthUpdatedCrypto();
reveal CryptoUpdatedAuth();
}
method {:vcs_split_on_every_assert} EncryptStructure(config: InternalConfig, input: EncryptStructureInput)
returns (output: Result<EncryptStructureOutput, Error>)
ensures output.Success? ==>
&& var headerSchema := output.value.cryptoSchema;
&& var inputSchema := input.cryptoSchema;
&& (forall v :: v in headerSchema.Values ==> IsAuthAttr(v))
{
//= specification/structured-encryption/encrypt-structure.md#behavior
//= type=implication
//# The input [Structured Data](encrypt-path-structure.md#structured-data) and [Crypto Schema](encrypt-path-structure.md#crypto-schema)
//# MUST refer to the same set of locations.
:- Need(input.plaintextStructure.Keys == input.cryptoSchema.Keys, E("Crypto Keys don't match."));
//= specification/structured-encryption/encrypt-structure.md#behavior
//= type=implication
//# The input [Structured Data](encrypt-path-structure.md#structured-data) and [Crypto Schema](encrypt-path-structure.md#crypto-schema)
//# MUST be combined into a single [Crypto List](encrypt-path-structure.md#crypto-list).
var cryptoMap :- BuildCryptoMap(input.plaintextStructure, input.cryptoSchema);
var pathInput := EncryptPathStructureInput(
tableName := input.tableName,
plaintextStructure := cryptoMap,
cmm := input.cmm,
algorithmSuiteId := input.algorithmSuiteId,
encryptionContext := input.encryptionContext
);
//= specification/structured-encryption/encrypt-structure.md#behavior
//= type=implication
//# Encrypt Structure MUST then behave as [Encrypt Path Structure](encrypt-path-structure.md)
var pathOutput :- EncryptPathStructure(config, pathInput);
assert forall k <- pathOutput.encryptedStructure :: |k.key| == 1 by {
EncryptStructureOutputHasSinglePaths(cryptoMap, pathOutput.encryptedStructure);
}
// This should be provable, but I'm not smart enough
:- Need(forall k <- pathOutput.encryptedStructure :: |k.key| == 1, E("Internal Error"));
//= specification/structured-encryption/encrypt-structure.md#behavior
//= type=implication
//# The output [Crypto List](encrypt-path-structure.md#crypto-list) produced by [Encrypt Path Structure](encrypt-path-structure.md)
//# MUST be split into [Structured Data](encrypt-path-structure.md#structured-data) and [Crypto Schema](encrypt-path-structure.md#crypto-schema)
//# maps.
var parts :- UnBuildCryptoMap(pathOutput.encryptedStructure);
var plainOutput := EncryptStructureOutput(
encryptedStructure := parts.0,
cryptoSchema := parts.1,
parsedHeader := pathOutput.parsedHeader
);
return Success(plainOutput);
}
//= specification/structured-encryption/encrypt-path-structure.md#encrypted-structured-data
//= type=implication
//# - for every entry in the input [Crypto List](#crypto-list)
//# an entry MUST exist with the same [path](./structures.md#path) in the final Encrypted Structured Data.
// && (forall k <- input.plaintextStructure :: (exists x :: x in output.value.encryptedStructure && x.key == k.key))
lemma AllEncryptPathInputInOutput(origData : CryptoList, finalData : CryptoList)
requires EncryptPathFinal(origData, finalData)
ensures forall k <- origData :: (exists x :: x in finalData && x.key == k.key)
{
reveal EncryptPathFinal();
reveal CryptoUpdatedCryptoListHeader();
reveal CryptoUpdatedNewCrypto();
}
//= specification/structured-encryption/encrypt-path-structure.md#encrypted-structured-data
//= type=implication
//# If the [Crypto Schema](#crypto-list)
//# indicates a [Crypto Action](./structures.md#crypto-action)
//# of [ENCRYPT_AND_SIGN](./structures.md#encryptandsign),
//# the Terminal Data MUST have [Terminal Type ID](./structures.md#terminal-type-id)
//# equal to 0xffff and the value MUST be
//# the [encryption](#terminal-data-encryption)
//# of the input's Terminal Data.
//= specification/structured-encryption/encrypt-path-structure.md#encrypted-structured-data
//= type=implication
//# Otherwise, this Terminal Data MUST have [Terminal Type ID](./structures.md#terminal-type-id)
//# and [Terminal Value](./structures.md#terminal-value) equal to the input Terminal Data's.
lemma AllEncryptPathInputUpdatedInOutput(origData : CryptoList, finalData : CryptoList)
requires EncryptPathFinal(origData, finalData)
ensures forall k <- origData :: (exists x :: x in finalData && Updated4(k, x, DoEncrypt))
{
reveal EncryptPathFinal();
reveal CryptoUpdatedCryptoListHeader();
reveal CryptoUpdatedNewCrypto();
}
//= specification/structured-encryption/encrypt-path-structure.md#encrypted-structured-data
//= type=implication
//# - For every entry in the final Encrypted Structured Data, other than the header and footer,
//# an entry MUST exist with the same [path](./structures.md#path) in the input [Crypto List](#crypto-list).
lemma AllEncryptPathOutputInInput(origData : CryptoList, finalData : CryptoList)
requires EncryptPathFinal(origData, finalData)
ensures |finalData| == |origData| + 2
ensures forall k <- finalData[..(|finalData|-2)] :: (exists x :: x in origData && x.key == k.key)
{
reveal EncryptPathFinal();
reveal CryptoUpdatedCryptoListHeader();
reveal NewCryptoUpdatedCrypto();
}
//= specification/structured-encryption/encrypt-path-structure.md#encrypted-structured-data
//= type=implication
//# - The [Header Field](#header-field) MUST exist in the final Encrypted Structured Data
lemma EncryptPathOutputHasHeader(origData : CryptoList, finalData : CryptoList)
requires EncryptPathFinal(origData, finalData)
ensures |finalData| == |origData| + 2
ensures finalData[|finalData|-2].key == HeaderPath
{
reveal EncryptPathFinal();
}
//= specification/structured-encryption/encrypt-path-structure.md#encrypted-structured-data
//= type=implication
//# - The [Footer Field](#footer-field) MUST exist in the final Encrypted Structured Data
lemma EncryptPathOutputHasFooter(origData : CryptoList, finalData : CryptoList)
requires EncryptPathFinal(origData, finalData)
ensures |finalData| == |origData| + 2
ensures finalData[|finalData|-1].key == FooterPath
{
reveal EncryptPathFinal();
}
method {:vcs_split_on_every_assert} EncryptPathStructure(config: InternalConfig, input: EncryptPathStructureInput)
returns (output: Result<EncryptPathStructureOutput, Error>)
ensures
output.Success? ==>
&& EncryptPathFinal(input.plaintextStructure, output.value.encryptedStructure)
//= specification/structured-encryption/encrypt-path-structure.md#crypto-list
//= type=implication
//# The Crypto List MUST include at least one [Crypto Action](./structures.md#crypto-action)
//# that is not [DO_NOTHING](./structures.md#do_nothing).
&& (exists k <- input.plaintextStructure :: IsAuthAttr(k.action))
//= specification/structured-encryption/encrypt-path-structure.md#crypto-list
//= type=implication
//# This Crypto List MUST NOT already contain data located at the [header index](./header.md#header-index)
//# or the [footer index](./footer.md#footer-index).
&& (!exists x | x in input.plaintextStructure :: x.key in HeaderPaths)
//= specification/structured-encryption/encrypt-path-structure.md#crypto-list
//= type=implication
//# The [paths](./structures.md#path) in the input [Crypto List](./structures.md#crypto-list) MUST be unique.
&& CryptoListHasNoDuplicatesFromSet(input.plaintextStructure)
//= specification/structured-encryption/encrypt-path-structure.md#encryption-context
//= type=implication
//# The operation MUST fail if an encryption context is provided which contains a key with the prefix `aws-crypto-`.
&& (
|| input.encryptionContext.None?
|| !exists k <- input.encryptionContext.value :: ReservedCryptoContextPrefixUTF8 <= input.encryptionContext.value[k])
{
:- Need(
|| input.encryptionContext.None?
|| !exists k <- input.encryptionContext.value :: ReservedCryptoContextPrefixUTF8 <= input.encryptionContext.value[k],
E("Encryption Context must not contain members beginning with " + ReservedCryptoContextPrefixString));
:- Need(exists k <- input.plaintextStructure :: IsAuthAttr(k.action),
E("At least one field in the Crypto Schema must be ENCRYPT_AND_SIGN, SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT or SIGN_ONLY."));
:- Need(!exists x | x in input.plaintextStructure :: x.key in HeaderPaths,
E("The paths " + HeaderField + " and " + FooterField + " are reserved."));
:- Need(CryptoListHasNoDuplicatesFromSet(input.plaintextStructure), E("Duplicate Paths"));
SetSizeImpliesCryptoListHasNoDuplicates(input.plaintextStructure);
:- Need(ValidString(input.tableName), E("Bad Table Name"));
var canonData :- ForEncrypt(input.tableName, input.plaintextStructure);
//= specification/structured-encryption/encrypt-path-structure.md#calculate-intermediate-encrypted-structured-data
//= type=implication
//# For every entry
//# in the input [Crypto List](#crypto-list)
//# there MUST be an entry with the same [canonical path](./header.md#canonical-path)
//# in Intermediate Encrypted Structured Data.
assert forall k <- input.plaintextStructure :: (exists x :: x in canonData && x.origKey == k.key && x.data == k.data) by {
reveal CanonCryptoMatchesCryptoList();
reveal CryptoExistsInCanonCrypto();
}
//= specification/structured-encryption/encrypt-path-structure.md#calculate-intermediate-encrypted-structured-data
//= type=implication
//# There MUST be no other entries in the Intermediate Encrypted Structured Data.
assert |input.plaintextStructure| == |canonData| by {
reveal CanonCryptoMatchesCryptoList();
}
//= specification/structured-encryption/encrypt-path-structure.md#retrieve-encryption-materials
//# This operation MUST [calculate the appropriate CMM and encryption context](#create-new-encryption-context-and-cmm).
var encryptionContext := input.encryptionContext.UnwrapOr(map[]);
var cmm := input.cmm;
//= specification/structured-encryption/encrypt-path-structure.md#create-new-encryption-context-and-cmm
//# If no [Crypto Action](./structures.md#crypto-action) is configured to be
//# [SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT Crypto Action](./structures.md#sign_and_include_in_encryption_context)
//# then the input cmm and encryption context MUST be used unchanged.
if exists x <- input.plaintextStructure :: x.action == SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT {
assume {:axiom} input.cmm.Modifies !! {config.materialProviders.History};
var newEncryptionContext :- GetV2EncryptionContext(input.plaintextStructure);
if |newEncryptionContext| != 0 {
//= specification/structured-encryption/encrypt-path-structure.md#create-new-encryption-context-and-cmm
//# An error MUST be returned if any of the entries added to the encryption context in this step
//# have the same key as any entry already in the encryption context.
:- Need(encryptionContext.Keys !! newEncryptionContext.Keys,
E("Internal Error - Structured Encryption encryption context overlaps with Item Encryptor encryption context."));
encryptionContext := encryptionContext + newEncryptionContext;
assert cmm.Modifies !! {config.materialProviders.History};
//= specification/structured-encryption/encrypt-path-structure.md#create-new-encryption-context-and-cmm
//# Then, this operation MUST create a [Required Encryption Context CMM](https://github.com/awslabs/private-aws-encryption-sdk-specification-staging/blob/dafny-verified/framework/required-encryption-context-cmm.md)
//# with the following inputs:
//# - This input [CMM](./ddb-table-encryption-config.md#cmm) as the underlying CMM.
//# - The name of every entry added above.
var contextKeysX := SortedSets.ComputeSetToOrderedSequence2(newEncryptionContext.Keys, ByteLess);
assert forall k <- contextKeysX :: ValidUTF8Seq(k) by {
assert forall k <- newEncryptionContext.Keys :: ValidUTF8Seq(k);
assert forall k <- contextKeysX :: k in newEncryptionContext.Keys;
}
var contextKeys : seq<UTF8.ValidUTF8Bytes> := contextKeysX;
var cmmR := config.materialProviders.CreateRequiredEncryptionContextCMM(
CMP.CreateRequiredEncryptionContextCMMInput(
underlyingCMM := Some(input.cmm),
keyring := None,
requiredEncryptionContextKeys := contextKeys
)
);
cmm :- cmmR.MapFailure(e => AwsCryptographyMaterialProviders(e));
}
}
var mat :- GetStructuredEncryptionMaterials(
cmm,
Some(encryptionContext),
input.algorithmSuiteId,
CountEncrypted(canonData),
SumValueSize(canonData));
var key : Key := mat.plaintextDataKey.value;
var alg := mat.algorithmSuite;
:- Need(Header.ValidEncryptionContext(mat.encryptionContext), E("Bad encryption context"));
//= specification/structured-encryption/header.md#message-id
//# Implementations MUST generate a fresh 256-bit random MessageID, from a cryptographically secure source, for each record encrypted.
//= specification/structured-encryption/encrypt-path-structure.md#calculate-intermediate-encrypted-structured-data
//# The process used to generate this identifier MUST use a good source of randomness
//# to make the chance of duplicate identifiers negligible.
var randBytes := Random.GenerateBytes(MSGID_LEN as int32);
var msgID :- randBytes.MapFailure(e => Error.AwsCryptographyPrimitives(e));
var head :- Header.Create(input.tableName, canonData, msgID, mat);
//= specification/structured-encryption/header.md#commit-key
//# The commit key calculation described above MUST be performed with the record's plaintext data key
//# and the header's message id.
var commitKey :- Crypt.GetCommitKey(config.primitives, alg, key, head.msgID);
var headerSerialized :- Header.Serialize(config.primitives, alg, commitKey, head);
//= specification/structured-encryption/encrypt-path-structure.md#header-field
//# The Header Field TypeID MUST be 0xFFFF
//= specification/structured-encryption/encrypt-path-structure.md#header-field
//# The Header Field Value MUST be the full serialized [header](header.md) with commitment.
var headerAttribute := ValueToData(headerSerialized, BYTES_TYPE_ID);
:- Need(|canonData| < (UINT32_LIMIT / 3), E("Too many encrypted fields"));
// input canonData has all input fields, none encrypted
// output canonData has all input fields, some encrypted
var encryptedItems : CanonCryptoList :- Crypt.Encrypt(config.primitives, alg, key, head, canonData, input.tableName, input.plaintextStructure);
var smallResult : CryptoList := UnCanonEncrypt(encryptedItems, input.tableName, input.plaintextStructure);
var footer :- Footer.CreateFooter(config.primitives, mat, encryptedItems, headerSerialized);
var footerAttribute := footer.makeTerminal();
var largeResult := AddHeaders(smallResult, headerAttribute, footerAttribute, input.plaintextStructure);
var headerAlgorithmSuite :- head.GetAlgorithmSuite(config.materialProviders);
var parsedHeader := ParsedHeader (
algorithmSuiteId := headerAlgorithmSuite.id.DBE,
encryptedDataKeys := head.dataKeys,
storedEncryptionContext := head.encContext,
encryptionContext := mat.encryptionContext
);
var encryptOutput := EncryptPathStructureOutput (
encryptedStructure := largeResult,
parsedHeader := parsedHeader
);
assert EncryptPathFinal(input.plaintextStructure, encryptOutput.encryptedStructure);
return Success(encryptOutput);
}
function method SafeDecode(data : CMP.Utf8Bytes) : string
{
var x := UTF8.Decode(data);
if x.Success? then
x.value
else
"[corrupt value]"
}
function method {:tailrecursion} DescribeMismatch(inputFields : seq<Bytes>, inputContext : CMP.EncryptionContext, headContext : Header.CMP.EncryptionContext)
: string
requires forall k <- inputFields :: k in inputContext
{
if |inputFields| == 0 then
""
else
var key := inputFields[0];
if key in headContext && headContext[key] != inputContext[key] then
var keyStr := SafeDecode(key);
var headStr := SafeDecode(headContext[key]);
var inputStr := SafeDecode(inputContext[key]);
var msg := "input context for " + keyStr + " was " + inputStr + " but stored context had " + headStr + "\n";
msg + DescribeMismatch(inputFields[1..], inputContext, headContext)
else
DescribeMismatch(inputFields[1..], inputContext, headContext)
}
function method DetectMismatch(inputContext : CMP.EncryptionContext, headContext : CMP.EncryptionContext)
: Outcome<Error>
{
var inputFields := SortedSets.ComputeSetToOrderedSequence2(inputContext.Keys, ByteLess);
var str := DescribeMismatch(inputFields, inputContext, headContext);
if |str| == 0 then
Pass
else
Fail(E("Encryption Context Mismatch\n" + str))
}
method NewCmm(config: InternalConfig, cmm : CMP.ICryptographicMaterialsManager, context : CMP.EncryptionContext)
returns (ret : Result<CMP.ICryptographicMaterialsManager, Error>)
requires |context| != 0
requires ValidInternalConfig?(config)
requires cmm.ValidState()
requires cmm.Modifies !! {config.materialProviders.History}
modifies ModifiesInternalConfig(config)
modifies cmm.Modifies
ensures ValidInternalConfig?(config)
ensures cmm.ValidState()
ensures ret.Success? ==>
&& ret.value.ValidState()
&& fresh(ret.value)
{
var contextKeysX := SortedSets.ComputeSetToOrderedSequence2(context.Keys, ByteLess);
assert forall k <- contextKeysX :: ValidUTF8Seq(k) by {
assert forall k <- context.Keys :: ValidUTF8Seq(k);
assert forall k <- contextKeysX :: k in context.Keys;
}
var contextKeys : seq<UTF8.ValidUTF8Bytes> := contextKeysX;
var cmmR := config.materialProviders.CreateRequiredEncryptionContextCMM(
CMP.CreateRequiredEncryptionContextCMMInput(
underlyingCMM := Some(cmm),
keyring := None,
requiredEncryptionContextKeys := contextKeys
)
);
var newCmm :- cmmR.MapFailure(e => AwsCryptographyMaterialProviders(e));
return Success(newCmm);
}
method {:vcs_split_on_every_assert} DecryptStructure (config: InternalConfig, input: DecryptStructureInput)
returns (output: Result<DecryptStructureOutput, Error>)
{
//= specification/structured-encryption/decrypt-structure.md#behavior
//= type=implication
//# The input [Structured Data](decrypt-path-structure.md#structured-data) and [Authenticate Schema](decrypt-path-structure.md#authenticate-schema)
//# MUST refer to the same set of locations.
:- Need(input.encryptedStructure.Keys == input.authenticateSchema.Keys, E("DecryptStructure requires encryptedStructure and authenticateSchema have the same keys."));
//= specification/structured-encryption/decrypt-structure.md#behavior
//= type=implication
//# The input [Structured Data](decrypt-path-structure.md#structured-data) and [Authenticate Schema](decrypt-path-structure.md#authenticate-schema)
//# MUST be combined into a single [Auth List](decrypt-path-structure.md#auth-list).
var cryptoMap :- BuildAuthMap(input.encryptedStructure, input.authenticateSchema);
var pathInput := DecryptPathStructureInput(
tableName := input.tableName,
encryptedStructure := cryptoMap,
cmm := input.cmm,
encryptionContext := input.encryptionContext
);
//= specification/structured-encryption/decrypt-structure.md#behavior
//= type=implication
//# Decrypt Structure MUST then behave as [Decrypt Path Structure](decrypt-path-structure.md)
var pathOutput :- DecryptPathStructure(config, pathInput);
assert forall k <- pathOutput.plaintextStructure :: |k.key| == 1 by {
DecryptStructureOutputHasSinglePaths(pathInput.encryptedStructure, pathOutput.plaintextStructure);
}
//= specification/structured-encryption/decrypt-structure.md#behavior
//= type=implication
//# The output [Crypto List](decrypt-path-structure.md#crypto-list) produced by [Decrypt Path Structure](decrypt-path-structure.md)
//# MUST be split into [Structured Data](decrypt-path-structure.md#structured-data) and [Crypto Schema](decrypt-path-structure.md#crypto-schema)
//# maps.
var parts :- UnBuildCryptoMap(pathOutput.plaintextStructure);
var plainOutput := DecryptStructureOutput(
plaintextStructure := parts.0,
cryptoSchema := parts.1,
parsedHeader := pathOutput.parsedHeader
);
return Success(plainOutput);
}
//= specification/structured-encryption/decrypt-path-structure.md#construct-decrypted-structured-data
//= type=implication
//# - For every entry in the [input Auth List](#auth-list), other than the header and footer,
//# an entry MUST exist with the same key in the output Crypto List.
lemma AllDecryptPathInputInOutput(origData : AuthList, finalData : CryptoList)
requires DecryptPathFinal(origData, finalData)
ensures forall k <- origData :: (k.key in [HeaderPath, FooterPath]) || exists x :: x in finalData
{
reveal DecryptPathFinal();
reveal AuthUpdatedCrypto();
}
//= specification/structured-encryption/decrypt-path-structure.md#construct-decrypted-structured-data
//= type=implication
//# - For every entry in the output Crypto List
//# an entry MUST exist with the same key in the [input Auth List](#auth-list).
lemma AllDecryptPathOutputInInput(origData : AuthList, finalData : CryptoList)
requires DecryptPathFinal(origData, finalData)
ensures forall k <- finalData :: exists x :: x in origData
{
reveal DecryptPathFinal();
reveal CryptoUpdatedAuth();
}
//= specification/structured-encryption/decrypt-path-structure.md#construct-decrypted-structured-data
//= type=implication
//# If the action is [ENCRYPT_AND_SIGN](./structures.md#encryptandsign)
//# this Terminal Data MUST have [Terminal Type ID](./structures.md#terminal-type-id)
//# equal to the first two bytes of the input Terminal Data's value,
//# and a value equal to the [decryption](#terminal-data-decryption) of the input Terminal Data's value.
//= specification/structured-encryption/decrypt-path-structure.md#construct-decrypted-structured-data
//= type=implication
//# Otherwise, this Terminal Data MUST have [Terminal Type ID](./structures.md#terminal-type-id) and
//# [Terminal Value](./structures.md#terminal-value) equal to the input Terminal Data.
lemma AllDecryptPathOutputUpdatesInput(origData : AuthList, finalData : CryptoList)
requires DecryptPathFinal(origData, finalData)
ensures forall k <- finalData :: exists x ::
&& x in origData
&& (k.action != ENCRYPT_AND_SIGN <==> x.data == k.data)
&& (k.action == ENCRYPT_AND_SIGN <==> x.data != k.data)
&& (k.action == ENCRYPT_AND_SIGN ==> |x.data.value| >= 2 && k.data.typeId == x.data.value[..2])
{
reveal DecryptPathFinal();
reveal CryptoUpdatedAuth();
}
//= specification/structured-encryption/decrypt-path-structure.md#construct-decrypted-structured-data
//= type=implication
//# - An entry MUST NOT exist with the key "aws_dbe_head" or "aws_dbe_foot".
lemma DecryptPathRemovesHeaders(origData : AuthList, finalData : CryptoList)
requires DecryptPathFinal(origData, finalData)
ensures !exists x :: x in finalData && x.key == HeaderPath
ensures !exists x :: x in finalData && x.key == FooterPath
{
reveal DecryptPathFinal();
}
method {:vcs_split_on_every_assert} DecryptPathStructure (config: InternalConfig, input: DecryptPathStructureInput)
returns (output: Result<DecryptPathStructureOutput, Error>)
ensures output.Success? ==>
&& DecryptPathFinal(input.encryptedStructure, output.value.plaintextStructure)
&& var encRecord : AuthList := input.encryptedStructure;
//= specification/structured-encryption/decrypt-path-structure.md#parse-the-header
//= type=implication
//# Given the [input data](#auth-list),
//# this operation MUST access the [Terminal Data](./structures.md#terminal-data)
//# at "aws_dbe_head".
//= specification/structured-encryption/decrypt-path-structure.md#auth-list
//= type=implication
//# This Auth List MUST contain data located at the [header index](./header.md#header-index)