@@ -160,7 +160,7 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
160
160
DDBEncode (SE.ATTR_PREFIX + k)
161
161
}
162
162
163
- function method MakeEncryptionContext (
163
+ function method MakeEncryptionContextForEncrypt (
164
164
config : InternalConfig ,
165
165
item : DynamoToStruct .TerminalDataMap)
166
166
: (ret : Result< CMP. EncryptionContext, Error> )
@@ -179,6 +179,36 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
179
179
MakeEncryptionContextV1 (config, item)
180
180
}
181
181
182
+ function method MakeEncryptionContextForDecrypt (
183
+ config : InternalConfig ,
184
+ header : seq <uint8 >,
185
+ item : DynamoToStruct .TerminalDataMap)
186
+ : (ret : Result< CMP. EncryptionContext, Error> )
187
+ requires 0 < |header|
188
+ ensures ret. Success? ==>
189
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
190
+ // = type=implication
191
+ // # If the Version Number is 2, then the base context MUST be the [version 2](./encrypt-item.md#dynamodb-item-base-context-version-2) context.
192
+ && (header[0] == 2 ==> ret == MakeEncryptionContextV2 (config, item))
193
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
194
+ // = type=implication
195
+ // # If the Version Number is 1, the base context MUST be the [version 1](./encrypt-item.md#dynamodb-item-base-context-version-1) context.
196
+ && (header[0] == 1 ==> ret == MakeEncryptionContextV1 (config, item))
197
+ && ((header[0] == 1) || (header[0] == 2))
198
+
199
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
200
+ // = type=implication
201
+ // # If the Version Number is not 1 or 2, the operation MUST return an error.
202
+ ensures ((header[0] != 1) && (header[0] != 2)) ==> ret. Failure?
203
+ {
204
+ if header[0] == 2 then
205
+ MakeEncryptionContextV2 (config, item)
206
+ else if header[0] == 1 then
207
+ MakeEncryptionContextV1 (config, item)
208
+ else
209
+ Failure (E("Header attribute has unexpected version number"))
210
+ }
211
+
182
212
function method {:opaque} {:vcs_split_on_every_assert} MakeEncryptionContextV1 (
183
213
config : InternalConfig ,
184
214
item : DynamoToStruct .TerminalDataMap)
@@ -770,9 +800,9 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
770
800
// = specification/dynamodb-encryption-client/encrypt-item.md#behavior
771
801
// = type=implication
772
802
// # - Encryption Context MUST be this input Item's [DynamoDB Item Base Context](#dynamodb-item-base-context).
773
- && MakeEncryptionContext (config, plaintextStructure). Success?
803
+ && MakeEncryptionContextForEncrypt (config, plaintextStructure). Success?
774
804
&& Seq. Last (config.structuredEncryption.History.EncryptStructure). input. encryptionContext
775
- == Some (MakeEncryptionContext (config, plaintextStructure). value)
805
+ == Some (MakeEncryptionContextForEncrypt (config, plaintextStructure). value)
776
806
777
807
&& output. value. parsedHeader. Some?
778
808
&& var structuredEncParsed := Seq. Last (config.structuredEncryption.History.EncryptStructure). output. value. parsedHeader;
@@ -847,7 +877,7 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
847
877
848
878
var plaintextStructure :- DynamoToStruct. ItemToStructured (input.plaintextItem)
849
879
.MapFailure (e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
850
- var context :- MakeEncryptionContext (config, plaintextStructure);
880
+ var context :- MakeEncryptionContextForEncrypt (config, plaintextStructure);
851
881
var cryptoSchema :- ConfigToCryptoSchema (config, input.plaintextItem)
852
882
.MapFailure (e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
853
883
var wrappedStruct := CSE. StructuredData (
@@ -977,12 +1007,25 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
977
1007
content := CSE.StructuredDataContent.DataMap(plaintextStructure),
978
1008
attributes := None)
979
1009
1010
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
1011
+ // = type=implication
1012
+ // # The item to be encrypted MUST have an attribute named `aws_dbe_head`.
1013
+ && SE. HeaderField in input. encryptedItem
1014
+ && var header := input. encryptedItem[SE. HeaderField];
1015
+
1016
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
1017
+ // = type=implication
1018
+ // # The attribute named `aws_dbe_head` MUST be of type `B` Binary.
1019
+ && header. B?
1020
+ && 0 < |header. B|
1021
+
980
1022
// = specification/dynamodb-encryption-client/decrypt-item.md#behavior
981
1023
// = type=implication
982
1024
// # - Encryption Context MUST be the input Item's [DynamoDB Item Base Context](./encrypt-item.md#dynamodb-item-base-context).
983
- && MakeEncryptionContext (config, plaintextStructure). Success?
1025
+ && MakeEncryptionContextForDecrypt (config, header.B, plaintextStructure). Success?
1026
+
984
1027
&& Seq. Last (config.structuredEncryption.History.DecryptStructure). input. encryptionContext
985
- == Some (MakeEncryptionContext (config, plaintextStructure). value)
1028
+ == Some (MakeEncryptionContextForDecrypt (config, header.B , plaintextStructure). value)
986
1029
987
1030
// = specification/dynamodb-encryption-client/decrypt-item.md#output
988
1031
// = type=implication
@@ -1077,7 +1120,12 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
1077
1120
1078
1121
var encryptedStructure :- DynamoToStruct. ItemToStructured (input.encryptedItem)
1079
1122
.MapFailure (e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
1080
- var context :- MakeEncryptionContext (config, encryptedStructure);
1123
+ :- Need (SE.HeaderField in input.encryptedItem, E("Header field, \"aws_dbe_head\", not in item."));
1124
+ var header := input. encryptedItem[SE. HeaderField];
1125
+ :- Need (header.B?, E("Header field, \"aws_dbe_head\", not binary"));
1126
+ assert header. B?;
1127
+ :- Need (0 < |header.B|, E("Unexpected empty header field."));
1128
+ var context :- MakeEncryptionContextForDecrypt (config, header.B, encryptedStructure);
1081
1129
var authenticateSchema := ConfigToAuthenticateSchema (config, input.encryptedItem);
1082
1130
var wrappedStruct := CSE. StructuredData (
1083
1131
content := CSE.StructuredDataContent.DataMap(encryptedStructure),
@@ -1088,7 +1136,7 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
1088
1136
// # [Required Encryption Context CMM](https://github.com/awslabs/private-aws-encryption-sdk-specification-staging/blob/dafny-verified/framework/required-encryption-context-cmm.md)
1089
1137
// # with the following inputs:
1090
1138
// # - This item encryptor's [CMM](./ddb-table-encryption-config.md#cmm) as the underlying CMM.
1091
- // # - The keys from the [DynamoDB Item Base Context](./encrypt-item.md #dynamodb-item-base-context).
1139
+ // # - The keys from the [DynamoDB Item Base Context](#dynamodb-item-base-context).
1092
1140
1093
1141
var reqCMMR := config. cmpClient. CreateRequiredEncryptionContextCMM (
1094
1142
CMP.CreateRequiredEncryptionContextCMMInput(
0 commit comments