Skip to content

Commit bd9d7be

Browse files
committed
m
1 parent 0a26def commit bd9d7be

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

DynamoDbEncryption/dafny/StructuredEncryption/src/AwsCryptographyDbEncryptionSdkStructuredEncryptionOperations.dfy

+12-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ module AwsCryptographyDbEncryptionSdkStructuredEncryptionOperations refines Abst
127127
}
128128

129129
// Return the sum of the sizes of the given fields
130-
function method {:opaque} SumValueSize(fields : CanonCryptoList)
130+
function {:opaque} SumValueSize(fields : CanonCryptoList)
131131
: nat
132132
{
133133
if |fields| == 0 then
@@ -136,6 +136,17 @@ module AwsCryptographyDbEncryptionSdkStructuredEncryptionOperations refines Abst
136136
|fields[0].data.value| + SumValueSize(fields[1..])
137137
else
138138
SumValueSize(fields[1..])
139+
} by method {
140+
reveal SumValueSize();
141+
var sum : nat := 0;
142+
for i := |fields| downto 0
143+
invariant sum == SumValueSize(fields[i..])
144+
{
145+
if fields[i].action == ENCRYPT_AND_SIGN {
146+
sum := |fields[i].data.value| + sum;
147+
}
148+
}
149+
return sum;
139150
}
140151

141152
function method {:opaque} GetAlgorithmSuiteId(alg : Option<CMP.DBEAlgorithmSuiteId>)

DynamoDbEncryption/dafny/StructuredEncryption/src/Footer.dfy

+42-8
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,46 @@ module StructuredEncryptionFooter {
234234
GetCanonicalPlaintextField(data.key, data.data)
235235
}
236236

237-
function method CanonContent (
238-
data : CanonCryptoList, // remaining fields to be canonized
239-
canonized : Bytes := [] // output
240-
) : Result<Bytes, Error>
237+
function CanonContent (data : CanonCryptoList)
238+
: Result<Bytes, Error>
241239
{
242240
if |data| == 0 then
243-
Success(canonized)
241+
Success([])
244242
else if data[0].action == DO_NOTHING then
245-
CanonContent(data[1..], canonized)
243+
CanonContent(data[1..])
246244
else
245+
var tail :- CanonContent(data[1..]);
247246
var newPart :- GetCanonicalItem(data[0]);
248-
CanonContent(data[1..], canonized + newPart)
247+
Success(newPart + tail)
248+
} by method {
249+
var i: nat := |data|;
250+
var vectors : Bytes := [];
251+
252+
while i != 0
253+
decreases i
254+
invariant Success(vectors) == CanonContent(data[i..])
255+
{
256+
i := i - 1;
257+
if data[i].action != DO_NOTHING {
258+
var test := GetCanonicalItem(data[i]);
259+
if test.Failure? {
260+
ghost var j := i;
261+
while j != 0
262+
decreases j
263+
invariant Failure(test.error) == CanonContent(data[j..])
264+
{
265+
j := j - 1;
266+
}
267+
return Failure(test.error);
268+
}
269+
vectors := test.value + vectors;
270+
}
271+
}
272+
273+
return Success(vectors);
249274
}
250275

276+
251277
function method CanonRecord (
252278
data : CanonCryptoList,
253279
header : Bytes,
@@ -377,13 +403,21 @@ module StructuredEncryptionFooter {
377403
}
378404
}
379405

380-
function method SerializeTags(tags : seq<RecipientTag>)
406+
function SerializeTags(tags : seq<RecipientTag>)
381407
: Bytes
382408
{
383409
if |tags| == 0 then
384410
[]
385411
else
386412
tags[0] + SerializeTags(tags[1..])
413+
} by method {
414+
var result : Bytes := [];
415+
for i := |tags| downto 0
416+
invariant result == SerializeTags(tags[i..])
417+
{
418+
result := tags[i] + result;
419+
}
420+
return result;
387421
}
388422

389423
function method SerializeSig(sig : Option<Signature>)

0 commit comments

Comments
 (0)