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 ac02188

Browse files
committedApr 29, 2025··
m
1 parent d9f2d27 commit ac02188

File tree

5 files changed

+207
-171
lines changed

5 files changed

+207
-171
lines changed
 

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

Lines changed: 165 additions & 165 deletions
Large diffs are not rendered by default.

‎DynamoDbEncryption/dafny/DynamoDbEncryption/src/Index.dfy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ include "DDBSupport.dfy"
99
include "DynamoDbEncryptionBranchKeyIdSupplier.dfy"
1010
include "DynamoToStruct.dfy"
1111
include "FilterExpr.dfy"
12+
include "MemoryMath.dfy"
1213
include "NormalizeNumber.dfy"
1314
include "SearchInfo.dfy"
1415
include "TermLoc.dfy"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// When dealing with actual data in actual memory, we can be confident that
2+
// none of the numbers will exceed an exabyte, so we can use uint64, rather than nat.
3+
// To convince Dafny that this is true, we have the following functions
4+
// with assumptions as needed.
5+
6+
7+
include "../Model/AwsCryptographyDbEncryptionSdkDynamoDbTypes.dfy"
8+
9+
module {:options "--function-syntax:4"} MemoryMath {
10+
11+
import opened BoundedInts
12+
13+
14+
// This is safe because it is being held in memory
15+
lemma {:axiom} ValueIsSafeBecauseItIsInMemory(value : nat)
16+
ensures value < UINT64_MAX as nat
17+
18+
function {:opaque} Add(x : uint64, y : uint64) : (ret : uint64)
19+
ensures ret < UINT64_MAX as uint64
20+
ensures ret as nat == x as nat + y as nat
21+
{
22+
ValueIsSafeBecauseItIsInMemory(x as nat + y as nat);
23+
x + y
24+
}
25+
26+
function {:opaque} Add3(x : uint64, y : uint64, z : uint64) : (ret : uint64)
27+
ensures ret < UINT64_MAX as uint64
28+
ensures ret as nat == x as nat + y as nat + z as nat
29+
{
30+
ValueIsSafeBecauseItIsInMemory(x as nat + y as nat + z as nat);
31+
x + y + z
32+
}
33+
34+
35+
}

‎DynamoDbEncryption/dafny/DynamoDbEncryption/src/Util.dfy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module DynamoDbEncryptionUtil {
1414
const BeaconPrefix := "aws_dbe_b_"
1515
const VersionPrefix := "aws_dbe_v_"
1616

17-
const MAX_STRUCTURE_DEPTH : uint32 := 32
17+
const MAX_STRUCTURE_DEPTH : uint64 := 32
1818
const MAX_STRUCTURE_DEPTH_STR := "32"
1919

2020
type HmacKeyMap = map<string, Bytes>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ module DynamoDbItemEncryptorUtil {
1414
import SortedSets
1515
import SE = StructuredEncryptionUtil
1616
import DynamoToStruct
17+
import MemoryMath
1718

1819
const ReservedPrefix := "aws_dbe_"
1920
const BeaconPrefix := ReservedPrefix + "b_"
2021
const VersionPrefix := ReservedPrefix + "v_"
21-
const UINT32_MAX : uint32 := 0xFFFF_FFFF
2222

2323
function method E(msg : string) : Error
2424
{
@@ -181,8 +181,8 @@ module DynamoDbItemEncryptorUtil {
181181
Success(value)
182182
else if legend == SE.LEGEND_BINARY then
183183
var terminal :- SE.DecodeTerminal(ecValue);
184-
:- Need(|terminal.value| < UINT32_MAX as int, "LEGEND_BINARY too big");
185-
var ddbAttrValue :- DynamoToStruct.BytesToAttr(terminal.value, terminal.typeId, Some(|terminal.value| as uint32));
184+
MemoryMath.ValueIsSafeBecauseItIsInMemory(|terminal.value|);
185+
var ddbAttrValue :- DynamoToStruct.BytesToAttr(terminal.value, terminal.typeId, Some(|terminal.value| as uint64));
186186
Success(ddbAttrValue.val)
187187
else
188188
Failure("Encryption Context Legend has unexpected character : '" + [legend] + "'.")
@@ -237,8 +237,8 @@ module DynamoDbItemEncryptorUtil {
237237

238238
// Obtain attribute value from EC kvPair value
239239
var terminal :- SE.DecodeTerminal(encodedAttrValue);
240-
:- Need(|terminal.value| < UINT32_MAX as int, "Attribute Value too big");
241-
var ddbAttrValue :- DynamoToStruct.BytesToAttr(terminal.value, terminal.typeId, Some(|terminal.value| as uint32));
240+
MemoryMath.ValueIsSafeBecauseItIsInMemory(|terminal.value|);
241+
var ddbAttrValue :- DynamoToStruct.BytesToAttr(terminal.value, terminal.typeId, Some(|terminal.value| as uint64));
242242

243243
// Add to our AttributeMap
244244
Success(attrMap[ddbAttrName := ddbAttrValue.val])

0 commit comments

Comments
 (0)
Please sign in to comment.