Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 97d853e

Browse files
committedApr 24, 2025··
m
1 parent 392339a commit 97d853e

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed
 

‎DynamoDbEncryption/dafny/DynamoDbEncryption/src/DynamoToStruct.dfy

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ module DynamoToStruct {
3131
// var m := new DafnyLibraries.MutableMap<AttributeName, StructuredDataTerminal>();
3232
// return MakeError("Not valid attribute names : ");
3333

34-
var structuredMap := map k <- item | (k in actions && actions[k] != DO_NOTHING) || (ReservedPrefix <= k) :: k := AttrToStructured(item[k]);
34+
var structuredMap := map k <- item | k !in actions || actions[k] != DO_NOTHING || ReservedPrefix <= k :: k := AttrToStructured(item[k]);
3535
MapKeysMatchItems(item);
3636
MapError(SimplifyMapValue(structuredMap))
3737
}
3838

39-
function method StructuredToItem2(s : TerminalDataMap, orig : AttributeMap, actions : CryptoSchemaMap) : (ret : Result<AttributeMap, Error>)
39+
function method StructuredToItemEncrypt(s : TerminalDataMap, orig : AttributeMap, actions : CryptoSchemaMap) : (ret : Result<AttributeMap, Error>)
4040
{
4141
var ddbMap := map k <- orig :: k := if (k in s && k in actions && actions[k] == ENCRYPT_AND_SIGN) then StructuredToAttr(s[k]) else Success(orig[k]);
4242
MapKeysMatchItems(orig);
@@ -49,6 +49,13 @@ module DynamoToStruct {
4949
Success(oldMap + newMap)
5050
}
5151

52+
function method StructuredToItemDecrypt(s : TerminalDataMap, orig : AttributeMap, actions : CryptoSchemaMap) : (ret : Result<AttributeMap, Error>)
53+
{
54+
var ddbMap := map k <- orig | !(ReservedPrefix <= k) :: k := if (k in s && k in actions && actions[k] == ENCRYPT_AND_SIGN) then StructuredToAttr(s[k]) else Success(orig[k]);
55+
MapKeysMatchItems(orig);
56+
MapError(SimplifyMapValue(ddbMap))
57+
}
58+
5259
// This file exists for these two functions : ItemToStructured and StructuredToItem
5360
// which provide conversion between an AttributeMap and a StructuredDataMap
5461

@@ -405,7 +412,7 @@ module DynamoToStruct {
405412
&& ListAttrToBytes(a.L, depth).Success?
406413
&& ret.value[PREFIX_LEN..] == ListAttrToBytes(a.L, depth).value
407414
&& ListAttrToBytes(a.L, depth).value[..LENGTH_LEN] == U32ToBigEndian(|a.L|).value
408-
// && ret.value[PREFIX_LEN..PREFIX_LEN+LENGTH_LEN] == U32ToBigEndian(|a.L|).value
415+
// && ret.value[PREFIX_LEN..PREFIX_LEN+LENGTH_LEN] == U32ToBigEndian(|a.L|).value
409416
&& (|a.L| == 0 ==> |ret.value| == PREFIX_LEN + LENGTH_LEN)
410417

411418
//= specification/dynamodb-encryption-client/ddb-attribute-serialization.md#map-attribute

‎DynamoDbEncryption/dafny/DynamoDbItemEncryptor/src/AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations.dfy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
889889
e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(DDBE.AwsCryptographyDbEncryptionSdkStructuredEncryption(e)));
890890
var encryptedData := encryptVal.encryptedStructure;
891891
:- Need(forall k <- encryptedData :: DDB.IsValid_AttributeName(k), E(""));
892-
var ddbKey :- DynamoToStruct.StructuredToItem2(encryptedData, input.plaintextItem, config.attributeActionsOnEncrypt)
892+
var ddbKey :- DynamoToStruct.StructuredToItemEncrypt(encryptedData, input.plaintextItem, config.attributeActionsOnEncrypt)
893893
.MapFailure(e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
894894

895895
var parsedActions :- ConvertCryptoSchemaToAttributeActions(config, encryptVal.cryptoSchema);
@@ -1112,7 +1112,7 @@ module AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations refines Abs
11121112
e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(DDBE.AwsCryptographyDbEncryptionSdkStructuredEncryption(e)));
11131113
var decryptedData := decryptVal.plaintextStructure;
11141114
:- Need(forall k <- decryptedData :: DDB.IsValid_AttributeName(k), E(""));
1115-
var ddbItem :- DynamoToStruct.StructuredToItem2(decryptedData, input.encryptedItem, decryptVal.cryptoSchema)
1115+
var ddbItem :- DynamoToStruct.StructuredToItemDecrypt(decryptedData, input.encryptedItem, decryptVal.cryptoSchema)
11161116
.MapFailure(e => Error.AwsCryptographyDbEncryptionSdkDynamoDb(e));
11171117

11181118
var schemaToConvert := decryptVal.cryptoSchema;

‎DynamoDbEncryption/dafny/DynamoDbItemEncryptor/test/DynamoDBItemEncryptorTest.dfy

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ module DynamoDbItemEncryptorTest {
109109
"sign4" := DDB.AttributeValue.NULL(true),
110110
"nothing" := DDBS("baz")
111111
];
112-
var expectedOutputItem := inputItem["bar" := DDB.AttributeValue.N("1234")];
113112

114113
var encryptRes := encryptor.EncryptItem(
115114
Types.EncryptItemInput(
@@ -122,7 +121,7 @@ module DynamoDbItemEncryptorTest {
122121
}
123122
expect encryptRes.Success?;
124123
expect encryptRes.value.encryptedItem.Keys == inputItem.Keys + {SE.HeaderField, SE.FooterField};
125-
expect encryptRes.value.encryptedItem["bar"] == expectedOutputItem["bar"]; // because normalized
124+
expect encryptRes.value.encryptedItem["bar"] == inputItem["bar"];
126125
expect encryptRes.value.encryptedItem["encrypt"] != inputItem["encrypt"];
127126
expect encryptRes.value.encryptedItem["sign"] == inputItem["sign"];
128127
expect encryptRes.value.encryptedItem["sign3"] == inputItem["sign3"];
@@ -142,11 +141,11 @@ module DynamoDbItemEncryptorTest {
142141
print "\n", decryptRes.error, "\n";
143142
}
144143
expect decryptRes.Success?;
145-
if decryptRes.value.plaintextItem != expectedOutputItem {
146-
print "\nexpectedOutputItem :\n", expectedOutputItem, "\n";
144+
if decryptRes.value.plaintextItem != inputItem {
145+
print "\ninputItem :\n", inputItem, "\n";
147146
print "\nOutput Item :\n", decryptRes.value.plaintextItem, "\n";
148147
}
149-
expect decryptRes.value.plaintextItem == expectedOutputItem;
148+
expect decryptRes.value.plaintextItem == inputItem;
150149

151150
var parsedHeader := decryptRes.value.parsedHeader;
152151
expect parsedHeader.Some?;

0 commit comments

Comments
 (0)
Please sign in to comment.