Skip to content

Commit 512f13b

Browse files
Merge branch 'main' into primitives-name
2 parents 1baeefb + f12fe5b commit 512f13b

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

AwsCryptographicMaterialProviders/dafny/AwsCryptographicMaterialProviders/src/CMMs/DefaultCMM.dfy

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module DefaultCMM {
2323
import Defaults
2424
import Commitment
2525
import Seq
26+
import SortedSets
2627

2728
class DefaultCMM
2829
extends CMM.VerifiableInterface
@@ -329,7 +330,7 @@ module DefaultCMM {
329330
//# - The operations made on the encryption context on the Get Encryption Materials call
330331
//# SHOULD be inverted on the Decrypt Materials call.
331332

332-
method DecryptMaterials'(
333+
method {:vcs_split_on_every_assert} DecryptMaterials'(
333334
input: Types.DecryptMaterialsInput
334335
)
335336
returns (output: Result<Types.DecryptMaterialsOutput, Types.Error>)
@@ -478,25 +479,36 @@ module DefaultCMM {
478479
var requiredEncryptionContextKeys := [];
479480
if input.reproducedEncryptionContext.Some? {
480481
var keysSet := input.reproducedEncryptionContext.value.Keys;
481-
while keysSet != {}
482+
ghost var keysSet' := keysSet;
483+
var keysSeq := SortedSets.ComputeSetToSequence(keysSet);
484+
var i := 0;
485+
while i < |keysSeq|
486+
invariant Seq.HasNoDuplicates(keysSeq)
487+
invariant forall j | 0 <= j < i && i < |keysSeq| :: keysSeq[j] !in keysSet'
488+
invariant forall j | i <= j < |keysSeq| :: keysSeq[j] in keysSet'
489+
invariant |keysSet'| == |keysSeq| - i
482490
invariant forall key
483491
|
484492
&& key in input.reproducedEncryptionContext.value
485493
&& key in input.encryptionContext
486-
&& key !in keysSet
494+
&& key !in keysSet'
487495
:: input.reproducedEncryptionContext.value[key] == input.encryptionContext[key]
488496
invariant forall key <- requiredEncryptionContextKeys
489497
:: key !in input.encryptionContext
490498
{
491-
var key :| key in keysSet;
499+
var key := keysSeq[i];
492500
if key in input.encryptionContext {
493501
:- Need(input.reproducedEncryptionContext.value[key] == input.encryptionContext[key],
494502
Types.AwsCryptographicMaterialProvidersException(
495503
message := "Encryption context does not match reproduced encryption context."));
496504
} else {
497505
requiredEncryptionContextKeys := requiredEncryptionContextKeys + [key];
498506
}
499-
keysSet := keysSet - {key};
507+
keysSet' := keysSet' - {key};
508+
i := i + 1;
509+
assert forall j | i <= j < |keysSeq| :: keysSeq[j] in keysSet' by {
510+
reveal Seq.HasNoDuplicates();
511+
}
500512
}
501513
}
502514

AwsCryptographicMaterialProviders/dafny/AwsCryptographicMaterialProviders/src/CMMs/RequiredEncryptionContextCMM.dfy

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module RequiredEncryptionContextCMM {
1616
import UTF8
1717
import Types = AwsCryptographyMaterialProvidersTypes
1818
import Seq
19+
import SortedSets
1920

2021
class RequiredEncryptionContextCMM
2122
extends CMM.VerifiableInterface
@@ -52,17 +53,10 @@ module RequiredEncryptionContextCMM {
5253
ensures Modifies == { History } + underlyingCMM.Modifies
5354
{
5455
var keySet := inputKeys;
55-
var keySeq := [];
56-
while keySet != {}
57-
invariant |keySeq| + |keySet| == |inputKeys|
58-
invariant forall k <- keySeq
59-
:: k in inputKeys
60-
{
61-
var key :| key in keySet;
62-
keySeq := keySeq + [key];
63-
keySet := keySet - {key};
64-
}
65-
56+
var keySeq := SortedSets.ComputeSetToSequence(keySet);
57+
assert |keySeq| == |keySet| == |inputKeys|;
58+
assert forall k <- keySeq
59+
:: k in inputKeys;
6660
underlyingCMM := inputCMM;
6761
requiredEncryptionContextKeys := keySeq;
6862

AwsCryptographicMaterialProviders/dafny/AwsCryptographicMaterialProviders/src/CanonicalEncryptionContext.dfy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module CanonicalEncryptionContext {
99
import Types = AwsCryptographyMaterialProvidersTypes
1010
import opened Wrappers
1111
import Seq
12+
import SortedSets
1213

1314
//= aws-encryption-sdk-specification/framework/raw-aes-keyring.md#onencrypt
1415
//# The keyring MUST attempt to serialize the [encryption materials']
@@ -25,7 +26,7 @@ module CanonicalEncryptionContext {
2526
{
2627
:- Need(|encryptionContext| < UINT16_LIMIT,
2728
Types.AwsCryptographicMaterialProvidersException( message := "Encryption Context is too large" ));
28-
var keys := SetToOrderedSequence(encryptionContext.Keys, UInt.UInt8Less);
29+
var keys := SortedSets.ComputeSetToOrderedSequence2(encryptionContext.Keys, UInt.UInt8Less);
2930

3031
if |keys| == 0 then
3132
Success([])

AwsCryptographyPrimitives/test/TestRSA.dfy

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module {:options "-functionSyntax:4"} TestAwsCryptographyPrimitivesRSA {
99
import opened Wrappers
1010
import opened StandardLibrary.UInt
1111
import UTF8
12+
import SortedSets
1213

1314
const RSA_PUBLIC_2048 := "-----BEGIN PUBLIC KEY-----\n"
1415
+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqBvECLsPdF1J5DOkaA1n\n"
@@ -130,10 +131,11 @@ module {:options "-functionSyntax:4"} TestAwsCryptographyPrimitivesRSA {
130131
lengthBits := 2048,
131132
pem := PrivateKeyFromGenerateRSAKeyPairPemBytes
132133
));
133-
134-
while allPadding != {}
134+
var paddingSeq := SortedSets.ComputeSetToSequence(allPadding);
135+
for paddingIdx := 0 to |paddingSeq|
135136
{
136-
var padding :| padding in allPadding;
137+
var padding := paddingSeq[paddingIdx];
138+
137139
BasicRSAEncryptTest(
138140
AtomicPrimitives.Types.RSAEncryptInput(
139141
padding := padding,
@@ -144,7 +146,6 @@ module {:options "-functionSyntax:4"} TestAwsCryptographyPrimitivesRSA {
144146
KeyFromGenerateRSAKeyPair
145147
);
146148

147-
allPadding := allPadding - {padding};
148149
}
149150
}
150151

@@ -164,9 +165,10 @@ module {:options "-functionSyntax:4"} TestAwsCryptographyPrimitivesRSA {
164165
pem := PrivateKeyFromTestVectorsPemBytes
165166
));
166167

167-
while allPadding != {}
168+
var paddingSeq := SortedSets.ComputeSetToSequence(allPadding);
169+
for paddingIdx := 0 to |paddingSeq|
168170
{
169-
var padding :| padding in allPadding;
171+
var padding := paddingSeq[paddingIdx];
170172

171173
BasicRSAEncryptTest(
172174
AtomicPrimitives.Types.RSAEncryptInput(
@@ -178,7 +180,6 @@ module {:options "-functionSyntax:4"} TestAwsCryptographyPrimitivesRSA {
178180
KeyFromTestVectorsPair
179181
);
180182

181-
allPadding := allPadding - {padding};
182183
}
183184
}
184185

0 commit comments

Comments
 (0)