Skip to content

Commit 53abc2c

Browse files
Add GetEncryptionContextHV1 and GetEncryptionContextHV2
1 parent 0c5cfa7 commit 53abc2c

File tree

1 file changed

+35
-3
lines changed
  • AwsCryptographicMaterialProviders/dafny/AwsCryptographyKeyStore/src

1 file changed

+35
-3
lines changed

AwsCryptographicMaterialProviders/dafny/AwsCryptographyKeyStore/src/Structure.dfy

+35-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module {:options "/functionSyntax:4" } Structure {
2525
const M_INPUT := "input" // The DDB Attribute name for the input, which is AttributeValue.B
2626
const M_UUID := "uuid" // The DDB Attribute name for the uuid, which is AttributeValue.S
2727
const M_PAGE_INDEX := "pageIndex" // The DDB Attribute name for the pageIndex, which is AttributeValue.B
28+
const HIERARCHY_VERSION_1 := "1"
29+
const HIERARCHY_VERSION_2 := "2"
2830

2931
const AWS_CRYPTO_EC := "aws-crypto-ec"
3032
const ENCRYPTION_CONTEXT_PREFIX := AWS_CRYPTO_EC + ":"
@@ -186,7 +188,20 @@ module {:options "/functionSyntax:4" } Structure {
186188
requires BranchKeyItem?(item)
187189
ensures EncryptedHierarchicalKey?(output)
188190
{
189-
var EncryptionContext := map k <- item.Keys - {BRANCH_KEY_FIELD} + {TABLE_FIELD}
191+
var EncryptionContext := if item[HIERARCHY_VERSION].N == HIERARCHY_VERSION_1
192+
then GetEncryptionContextHV1(item, logicalKeyStoreName)
193+
else GetEncryptionContextHV2(item);
194+
195+
ConstructEncryptedHierarchicalKey(EncryptionContext, item[BRANCH_KEY_FIELD].B)
196+
}
197+
198+
function GetEncryptionContextHV1(
199+
item: DDB.AttributeMap,
200+
logicalKeyStoreName: string
201+
): (output: map<string, string>)
202+
requires BranchKeyItem?(item)
203+
{
204+
map k <- item.Keys - {BRANCH_KEY_FIELD} + {TABLE_FIELD}
190205
// Working around https://github.com/dafny-lang/dafny/issues/5776
191206
// that will make the following fail to compile
192207
// match k
@@ -198,9 +213,26 @@ module {:options "/functionSyntax:4" } Structure {
198213
else if k == TABLE_FIELD then
199214
logicalKeyStoreName
200215
else
201-
item[k].S;
216+
item[k].S
217+
}
202218

203-
ConstructEncryptedHierarchicalKey(EncryptionContext, item[BRANCH_KEY_FIELD].B)
219+
function GetEncryptionContextHV2(
220+
item: DDB.AttributeMap
221+
): (output: map<string, string>)
222+
requires BranchKeyItem?(item)
223+
{
224+
var fieldsToRemove := {
225+
BRANCH_KEY_FIELD,
226+
BRANCH_KEY_IDENTIFIER_FIELD,
227+
TYPE_FIELD,
228+
KEY_CREATE_TIME,
229+
HIERARCHY_VERSION,
230+
KMS_FIELD,
231+
BRANCH_KEY_ACTIVE_VERSION_FIELD
232+
};
233+
234+
map k <- item.Keys - fieldsToRemove
235+
:: k[|ENCRYPTION_CONTEXT_PREFIX|..] := item[k].S
204236
}
205237

206238
function ConstructEncryptedHierarchicalKey(

0 commit comments

Comments
 (0)