@@ -155,7 +155,7 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
155
155
DDBEncode (SE.ATTR_PREFIX + k)
156
156
}
157
157
158
- function method MakeEncryptionContext (
158
+ function method MakeEncryptionContextForEncrypt (
159
159
config : InternalConfig ,
160
160
item : DynamoToStruct .TerminalDataMap)
161
161
: (ret : Result< CMP. EncryptionContext, Error> )
@@ -174,6 +174,36 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
174
174
MakeEncryptionContextV1 (config, item)
175
175
}
176
176
177
+ function method MakeEncryptionContextForDecrypt (
178
+ config : InternalConfig ,
179
+ header : seq <uint8 >,
180
+ item : DynamoToStruct .TerminalDataMap)
181
+ : (ret : Result< CMP. EncryptionContext, Error> )
182
+ requires 0 < |header|
183
+ ensures ret. Success? ==>
184
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
185
+ // = type=implication
186
+ // # 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.
187
+ && (header[0] == 2 ==> ret == MakeEncryptionContextV2 (config, item))
188
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
189
+ // = type=implication
190
+ // # If the Version Number is 1, the base context MUST be the [version 1](./encrypt-item.md#dynamodb-item-base-context-version-1) context.
191
+ && (header[0] == 1 ==> ret == MakeEncryptionContextV1 (config, item))
192
+ && ((header[0] == 1) || (header[0] == 2))
193
+
194
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
195
+ // = type=implication
196
+ // # If the Version Number is not 1 or 2, the operation MUST return an error.
197
+ ensures ((header[0] != 1) && (header[0] != 2)) ==> ret. Failure?
198
+ {
199
+ if header[0] == 2 then
200
+ MakeEncryptionContextV2 (config, item)
201
+ else if header[0] == 1 then
202
+ MakeEncryptionContextV1 (config, item)
203
+ else
204
+ Failure (E("Header attribute has unexpected version number"))
205
+ }
206
+
177
207
function method {:opaque} {:vcs_split_on_every_assert} MakeEncryptionContextV1 (
178
208
config : InternalConfig ,
179
209
item : DynamoToStruct .TerminalDataMap)
@@ -751,9 +781,9 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
751
781
// = specification/dynamodb-encryption-client/encrypt-item.md#behavior
752
782
// = type=implication
753
783
// # - Encryption Context MUST be this input Item's [DynamoDB Item Base Context](#dynamodb-item-base-context).
754
- && MakeEncryptionContext (config, plaintextStructure). Success?
784
+ && MakeEncryptionContextForEncrypt (config, plaintextStructure). Success?
755
785
&& Seq. Last (config.structuredEncryption.History.EncryptStructure). input. encryptionContext
756
- == Some (MakeEncryptionContext (config, plaintextStructure). value)
786
+ == Some (MakeEncryptionContextForEncrypt (config, plaintextStructure). value)
757
787
758
788
&& output. value. parsedHeader. Some?
759
789
&& var structuredEncOut := Seq. Last (config.structuredEncryption.History.EncryptStructure). output. value;
@@ -826,7 +856,7 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
826
856
827
857
var plaintextStructure :- DynamoToStruct. ItemToStructured (input.plaintextItem)
828
858
.MapFailure (e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
829
- var context :- MakeEncryptionContext (config, plaintextStructure);
859
+ var context :- MakeEncryptionContextForEncrypt (config, plaintextStructure);
830
860
var cryptoSchema :- ConfigToCryptoSchema (config, input.plaintextItem)
831
861
.MapFailure (e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
832
862
@@ -957,12 +987,25 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
957
987
&& Seq. Last (config.structuredEncryption.History.DecryptStructure). input. encryptedStructure
958
988
== plaintextStructure
959
989
990
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
991
+ // = type=implication
992
+ // # The item to be encrypted MUST have an attribute named `aws_dbe_head`.
993
+ && SE. HeaderField in input. encryptedItem
994
+ && var header := input. encryptedItem[SE. HeaderField];
995
+
996
+ // = specification/dynamodb-encryption-client/decrypt-item.md#dynamodb-item-base-context
997
+ // = type=implication
998
+ // # The attribute named `aws_dbe_head` MUST be of type `B` Binary.
999
+ && header. B?
1000
+ && 0 < |header. B|
1001
+
960
1002
// = specification/dynamodb-encryption-client/decrypt-item.md#behavior
961
1003
// = type=implication
962
1004
// # - Encryption Context MUST be the input Item's [DynamoDB Item Base Context](./encrypt-item.md#dynamodb-item-base-context).
963
- && MakeEncryptionContext (config, plaintextStructure). Success?
1005
+ && MakeEncryptionContextForDecrypt (config, header.B, plaintextStructure). Success?
1006
+
964
1007
&& Seq. Last (config.structuredEncryption.History.DecryptStructure). input. encryptionContext
965
- == Some (MakeEncryptionContext (config, plaintextStructure). value)
1008
+ == Some (MakeEncryptionContextForDecrypt (config, header.B , plaintextStructure). value)
966
1009
967
1010
// = specification/dynamodb-encryption-client/decrypt-item.md#output
968
1011
// = type=implication
@@ -1055,15 +1098,20 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
1055
1098
1056
1099
var encryptedStructure :- DynamoToStruct. ItemToStructured (input.encryptedItem)
1057
1100
.MapFailure (e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
1058
- var context :- MakeEncryptionContext (config, encryptedStructure);
1101
+ :- Need (SE.HeaderField in input.encryptedItem, E("Header field, \"aws_dbe_head\", not in item."));
1102
+ var header := input. encryptedItem[SE. HeaderField];
1103
+ :- Need (header.B?, E("Header field, \"aws_dbe_head\", not binary"));
1104
+ assert header. B?;
1105
+ :- Need (0 < |header.B|, E("Unexpected empty header field."));
1106
+ var context :- MakeEncryptionContextForDecrypt (config, header.B, encryptedStructure);
1059
1107
var authenticateSchema := ConfigToAuthenticateSchema (config, input.encryptedItem);
1060
1108
1061
1109
// = specification/dynamodb-encryption-client/decrypt-item.md#behavior
1062
1110
// # This operation MUST create a
1063
1111
// # [Required Encryption Context CMM](https://github.com/awslabs/private-aws-encryption-sdk-specification-staging/blob/dafny-verified/framework/required-encryption-context-cmm.md)
1064
1112
// # with the following inputs:
1065
1113
// # - This item encryptor's [CMM](./ddb-table-encryption-config.md#cmm) as the underlying CMM.
1066
- // # - The keys from the [DynamoDB Item Base Context](./encrypt-item.md #dynamodb-item-base-context).
1114
+ // # - The keys from the [DynamoDB Item Base Context](#dynamodb-item-base-context).
1067
1115
1068
1116
var reqCMMR := config. cmpClient. CreateRequiredEncryptionContextCMM (
1069
1117
CMP.CreateRequiredEncryptionContextCMMInput(
0 commit comments