Skip to content

Commit ccb525d

Browse files
committed
hash cache id
1 parent 38b93dd commit ccb525d

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

DynamoDbEncryption/dafny/DynamoDbEncryption/Model/DynamoDbEncryption.smithy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ structure KeyStoreReference {}
708708
//# On initialization of a Single Key Store, the caller MUST provide:
709709
//# - [Beacon Key Id](#beacon-key-id)
710710
//# - [cacheTTL](#cachettl)
711+
//# - [cache](#key-store-cache)
712+
//# - [partition-id](#partition-id)
711713

712714
@javadoc("The configuration for using a single Beacon Key.")
713715
structure SingleKeyStore {
@@ -728,7 +730,8 @@ structure SingleKeyStore {
728730
//# On initialization of a Multi Key Store, the caller MUST provide:
729731
//# - [Beacon Key Field Name](#beacon-key-field-name)
730732
//# - [cacheTTL](#cachettl)
731-
//# - [max cache size](#max-cache-size)
733+
//# - [cache](#key-store-cache)
734+
//# - [partition-id](#partition-id)
732735

733736
@javadoc("The configuration for using multiple Beacon Keys.")
734737
structure MultiKeyStore {

DynamoDbEncryption/dafny/DynamoDbEncryption/src/SearchInfo.dfy

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module SearchableEncryptionInfo {
2828
import SE = AwsCryptographyDbEncryptionSdkStructuredEncryptionTypes
2929
import opened CacheConstants
3030
import UUID
31+
import Digest
3132

3233
//= specification/searchable-encryption/search-config.md#version-number
3334
//= type=implication
@@ -175,7 +176,7 @@ module SearchableEncryptionInfo {
175176
if keyLoc.SingleLoc? {
176177
:- Need(keyId.DontUseKeyId?, E("KeyID should not be supplied with a SingleKeyStore"));
177178
var now := Time.GetCurrent();
178-
var theMap :- getKeysCache(stdNames, keyLoc.keyId, cacheTTL as MP.PositiveLong, partitionIdBytes, logicalKeyStoreNameBytes, now as MP.PositiveLong);
179+
var theMap :- getKeysCache(client, stdNames, keyLoc.keyId, cacheTTL as MP.PositiveLong, partitionIdBytes, logicalKeyStoreNameBytes, now as MP.PositiveLong);
179180
return Success(Keys(theMap));
180181
} else if keyLoc.LiteralLoc? {
181182
:- Need(keyId.DontUseKeyId?, E("KeyID should not be supplied with a LiteralKeyStore"));
@@ -185,7 +186,7 @@ module SearchableEncryptionInfo {
185186
match keyId {
186187
case DontUseKeyId => return Failure(E("KeyID must be supplied with a MultiKeyStore"));
187188
case ShouldHaveKeyId => return Success(ShouldHaveKeys);
188-
case KeyId(id) => var now := Time.GetCurrent(); var theMap :- getKeysCache(stdNames, id, cacheTTL as MP.PositiveLong, partitionIdBytes, logicalKeyStoreNameBytes, now as MP.PositiveLong); return Success(Keys(theMap));
189+
case KeyId(id) => var now := Time.GetCurrent(); var theMap :- getKeysCache(client, stdNames, id, cacheTTL as MP.PositiveLong, partitionIdBytes, logicalKeyStoreNameBytes, now as MP.PositiveLong); return Success(Keys(theMap));
189190
}
190191
}
191192
}
@@ -215,6 +216,7 @@ module SearchableEncryptionInfo {
215216
}
216217

217218
method getKeysCache(
219+
client : Primitives.AtomicPrimitivesClient,
218220
stdNames : seq<string>,
219221
keyId : string,
220222
cacheTTL : MP.PositiveLong,
@@ -240,7 +242,8 @@ module SearchableEncryptionInfo {
240242
&& var cacheInput := Seq.Last(newHistory).input;
241243
&& var cacheOutput := Seq.Last(newHistory).output;
242244
&& UTF8.Encode(keyId).Success?
243-
&& cacheInput.identifier == RESOURCE_ID_HIERARCHICAL_KEYRING + NULL_BYTE + SCOPE_ID_SEARCHABLE_ENCRYPTION + NULL_BYTE + partitionIdBytes + NULL_BYTE + logicalKeyStoreNameBytes + NULL_BYTE + UTF8.Encode(keyId).value
245+
// This is no longer true since we're taking a SHA384 of the identifier
246+
// && cacheInput.identifier == RESOURCE_ID_HIERARCHICAL_KEYRING + NULL_BYTE + SCOPE_ID_SEARCHABLE_ENCRYPTION + NULL_BYTE + partitionIdBytes + NULL_BYTE + logicalKeyStoreNameBytes + NULL_BYTE + UTF8.Encode(keyId).value
244247

245248
//= specification/searchable-encryption/search-config.md#get-beacon-key-materials
246249
//= type=implication
@@ -304,7 +307,23 @@ module SearchableEncryptionInfo {
304307
// Append Resource Id, Scope Id, Partition Id, and Suffix to create the cache identifier
305308
var identifier := resourceId + NULL_BYTE + scopeId + NULL_BYTE + partitionIdBytes + NULL_BYTE + suffix;
306309

307-
var getCacheInput := MP.GetCacheEntryInput(identifier := identifier, bytesUsed := None);
310+
// Take a SHA384 of the cache identifier
311+
var hashAlgorithm := Prim.DigestAlgorithm.SHA_384;
312+
313+
var identifierDigestInput := Prim.DigestInput(
314+
digestAlgorithm := hashAlgorithm, message := identifier
315+
);
316+
var maybeCacheDigest := Digest.Digest(identifierDigestInput);
317+
var cacheDigest :- maybeCacheDigest.MapFailure(e => AwsCryptographyPrimitives(e));
318+
319+
:- Need(
320+
|cacheDigest| == Digest.Length(hashAlgorithm),
321+
Error.DynamoDbEncryptionException(
322+
message := "Digest generated a message not equal to the expected length.")
323+
);
324+
325+
// Use the SHA384 of the identifier as the cache identifier
326+
var getCacheInput := MP.GetCacheEntryInput(identifier := cacheDigest, bytesUsed := None);
308327
verifyValidStateCache(cache);
309328
var getCacheOutput := cache.GetCacheEntry(getCacheInput);
310329
// If error is not EntryDoesNotExist, return failure

0 commit comments

Comments
 (0)