-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCreateKeys.dfy
667 lines (564 loc) · 36.4 KB
/
CreateKeys.dfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
include "../Model/AwsCryptographyKeyStoreTypes.dfy"
include "Structure.dfy"
include "DefaultKeyStorageInterface.dfy"
include "KMSKeystoreOperations.dfy"
include "ErrorMessages.dfy"
include "../../AwsCryptographicMaterialProviders/src/AwsArnParsing.dfy"
include "KmsArn.dfy"
module {:options "/functionSyntax:4" } CreateKeys {
import opened StandardLibrary
import opened Wrappers
import Structure
import DefaultKeyStorageInterface
import KMSKeystoreOperations
import ErrorMessages = KeyStoreErrorMessages
import opened Seq
import opened UInt = StandardLibrary.UInt
import Types = AwsCryptographyKeyStoreTypes
import DDB = ComAmazonawsDynamodbTypes
import KMS = ComAmazonawsKmsTypes
import AwsArnParsing
import KmsArn
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# To create a branch key, this operation MUST take the following:
//#
//# - `branchKeyId`: The identifier
//# - `encryptionContext`: Additional encryption context to bind to the created keys
method CreateBranchAndBeaconKeys(
branchKeyIdentifier: string,
customEncryptionContext: map<string, string>,
timestamp: string,
branchKeyVersion: string,
logicalKeyStoreName: string,
kmsConfiguration: Types.KMSConfiguration,
grantTokens: KMS.GrantTokenList,
kmsClient: KMS.IKMSClient,
storage: Types.IKeyStorageInterface
)
returns (output: Result<Types.CreateKeyOutput, Types.Error>)
requires 0 < |branchKeyIdentifier|
requires 0 < |branchKeyVersion|
requires forall k <- customEncryptionContext :: DDB.IsValid_AttributeName(Structure.ENCRYPTION_CONTEXT_PREFIX + k)
requires storage.Modifies !! kmsClient.Modifies
requires KMSKeystoreOperations.HasKeyId(kmsConfiguration) && KmsArn.ValidKmsArn?(KMSKeystoreOperations.GetKeyId(kmsConfiguration))
requires kmsClient.ValidState() && storage.ValidState()
modifies storage.Modifies, kmsClient.Modifies
ensures storage.ValidState() && kmsClient.ValidState()
//= aws-encryption-sdk-specification/framework/branch-key-store.md#createkey
//= type=implication
//# This operation MUST create a [branch key](structures.md#branch-key) and a [beacon key](structures.md#beacon-key) according to
//# the [Branch Key and Beacon Key Creation](#branch-key-and-beacon-key-creation) section.
ensures output.Success?
==>
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# The call to AWS KMS GenerateDataKeyWithoutPlaintext MUST use the configured AWS KMS client to make the call.
// The second call is for the beacon key, the first call is the decrypt only. See Seq.Last(Seq.DropLast(
&& |kmsClient.History.GenerateDataKeyWithoutPlaintext| == |old(kmsClient.History.GenerateDataKeyWithoutPlaintext)| + 2
&& |kmsClient.History.ReEncrypt| == |old(kmsClient.History.ReEncrypt)| + 1
&& old(kmsClient.History.GenerateDataKeyWithoutPlaintext) < kmsClient.History.GenerateDataKeyWithoutPlaintext
&& old(kmsClient.History.ReEncrypt) < kmsClient.History.ReEncrypt
&& var decryptOnlyEncryptionContext := Structure.DecryptOnlyBranchKeyEncryptionContext(
branchKeyIdentifier,
branchKeyVersion,
timestamp,
logicalKeyStoreName,
KMSKeystoreOperations.GetKeyId(kmsConfiguration),
customEncryptionContext
);
//= aws-encryption-sdk-specification/framework/branch-key-store.md#logical-keystore-name
//= type=implication
//# The logical keystore name MUST be bound to every created key.
&& decryptOnlyEncryptionContext[Structure.TABLE_FIELD] == logicalKeyStoreName
//= aws-encryption-sdk-specification/framework/branch-key-store.md#encryption-context
//= type=implication
//# Any additionally attributes in the EncryptionContext
//# of the [encrypted hierarchical key](./key-store/key-storage.md#encryptedhierarchicalkey)
//# MUST be added to the encryption context.
&& (forall k <- customEncryptionContext
::
&& Structure.ENCRYPTION_CONTEXT_PREFIX + k in decryptOnlyEncryptionContext
&& decryptOnlyEncryptionContext[Structure.ENCRYPTION_CONTEXT_PREFIX + k] == customEncryptionContext[k])
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# The wrapped Branch Keys, DECRYPT_ONLY and ACTIVE, MUST be created according to [Wrapped Branch Key Creation](#wrapped-branch-key-creation).
&& WrappedBranchKeyCreation?(
Seq.Last(Seq.DropLast(kmsClient.History.GenerateDataKeyWithoutPlaintext)),
Seq.Last(kmsClient.History.ReEncrypt),
kmsClient,
kmsConfiguration,
grantTokens,
decryptOnlyEncryptionContext
)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# The operation MUST call [AWS KMS API GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html).
&& var beaconKmsRequest := Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext);
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# The operation MUST call AWS KMS GenerateDataKeyWithoutPlaintext with a request constructed as follows:
&& var beaconKmsInput := beaconKmsRequest.input;
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# - `KeyId` MUST be [compatible with](#aws-key-arn-compatibility) the configured KMS Key in the [AWS KMS Configuration](#aws-kms-configuration) for this keystore.
&& KMSKeystoreOperations.Compatible?(kmsConfiguration, beaconKmsInput.KeyId)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# - `NumberOfBytes` MUST be 32.
&& beaconKmsInput.NumberOfBytes == Some(32)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# - `EncryptionContext` MUST be the [encryption context for beacon keys](#beacon-key-encryption-context).
&& beaconKmsInput.EncryptionContext == Some(Structure.BeaconKeyEncryptionContext(decryptOnlyEncryptionContext))
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# - `GrantTokens` MUST be this keystore's [grant tokens](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#grant_token).
&& beaconKmsInput.GrantTokens == Some(grantTokens)
&& beaconKmsRequest.output.Success?
&& beaconKmsRequest.output.value.CiphertextBlob.Some?
&& |storage.History.WriteNewEncryptedBranchKey| == |old(storage.History.WriteNewEncryptedBranchKey)| + 1
&& Seq.Last(storage.History.WriteNewEncryptedBranchKey).input.Active
== Structure.ConstructEncryptedHierarchicalKey(
Seq.Last(kmsClient.History.ReEncrypt).input.DestinationEncryptionContext.value,
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# If the call to AWS KMS ReEncrypt succeeds,
//# the operation MUST use the ReEncrypt result `CiphertextBlob`
//# as the wrapped ACTIVE Branch Key.
Seq.Last(kmsClient.History.ReEncrypt).output.value.CiphertextBlob.value
)
&& Seq.Last(storage.History.WriteNewEncryptedBranchKey).input.Version
== Structure.ConstructEncryptedHierarchicalKey(
Seq.Last(Seq.DropLast(kmsClient.History.GenerateDataKeyWithoutPlaintext)).input.EncryptionContext.value,
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# If the call to AWS KMS GenerateDataKeyWithoutPlaintext succeeds,
//# the operation MUST use the GenerateDataKeyWithoutPlaintext result `CiphertextBlob`
//# as the wrapped DECRYPT_ONLY Branch Key.
Seq.Last(Seq.DropLast(kmsClient.History.GenerateDataKeyWithoutPlaintext)).output.value.CiphertextBlob.value
)
&& Seq.Last(storage.History.WriteNewEncryptedBranchKey).input.Beacon
== Structure.ConstructEncryptedHierarchicalKey(
Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).input.EncryptionContext.value,
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=implication
//# If the call to AWS KMS GenerateDataKeyWithoutPlaintext succeeds,
//# the operation MUST use the `CiphertextBlob` as the wrapped Beacon Key.
Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).output.value.CiphertextBlob.value
)
&& Seq.Last(storage.History.WriteNewEncryptedBranchKey).output.Success?
//= aws-encryption-sdk-specification/framework/branch-key-store.md#createkey
//= type=implication
//# If creation of the keys are successful,
//# then the key store MUST call the configured [KeyStorage interface's](./key-store/key-storage.md#interface)
//# [WriteNewEncryptedBranchKey](./key-store/key-storage.md#writenewencryptedbranchkey) with these 3 [EncryptedHierarchicalKeys](./key-store/key-storage.md#encryptedhierarchicalkey).
ensures
&& output.Success?
&& |kmsClient.History.GenerateDataKeyWithoutPlaintext| == |old(kmsClient.History.GenerateDataKeyWithoutPlaintext)| + 2
&& |kmsClient.History.ReEncrypt| == |old(kmsClient.History.ReEncrypt)| + 1
&& Seq.Last(Seq.DropLast(kmsClient.History.GenerateDataKeyWithoutPlaintext)).output.Success?
&& Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).output.Success?
&& Seq.Last(kmsClient.History.ReEncrypt).output.Success?
==>
&& |storage.History.WriteNewEncryptedBranchKey| == |old(storage.History.WriteNewEncryptedBranchKey)| + 1
//= aws-encryption-sdk-specification/framework/branch-key-store.md#createkey
//= type=implication
//# If writing to the keystore succeeds,
//# the operation MUST return the branch-key-id that maps to both
//# the branch key and the beacon key.
ensures
&& |storage.History.WriteNewEncryptedBranchKey| == |old(storage.History.WriteNewEncryptedBranchKey)| + 1
&& Seq.Last(storage.History.WriteNewEncryptedBranchKey).output.Success?
==>
&& output.Success?
&& output.value.branchKeyIdentifier == branchKeyIdentifier
//= aws-encryption-sdk-specification/framework/branch-key-store.md#createkey
//= type=implication
//# Otherwise, this operation MUST yield an error.
ensures
|| (&& |kmsClient.History.GenerateDataKeyWithoutPlaintext| == |old(kmsClient.History.GenerateDataKeyWithoutPlaintext)| + 1
&& Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).output.Failure?
==> output.Failure?)
|| (&& |kmsClient.History.ReEncrypt| == |old(kmsClient.History.ReEncrypt)| + 1
&& Seq.Last(kmsClient.History.ReEncrypt).output.Failure?
==> output.Failure?)
|| (&& |kmsClient.History.GenerateDataKeyWithoutPlaintext| == |old(kmsClient.History.GenerateDataKeyWithoutPlaintext)| + 2
&& Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).output.Failure?
==> output.Failure?)
|| (&& |storage.History.WriteNewEncryptedBranchKey| == |old(storage.History.WriteNewEncryptedBranchKey)| + 1
&& Seq.Last(storage.History.WriteNewEncryptedBranchKey).output.Failure?
==> output.Failure?)
{
var decryptOnlyEncryptionContext := Structure.DecryptOnlyBranchKeyEncryptionContext(
branchKeyIdentifier,
branchKeyVersion,
timestamp,
logicalKeyStoreName,
KMSKeystoreOperations.GetKeyId(kmsConfiguration),
customEncryptionContext
);
var activeEncryptionContext := Structure.ActiveBranchKeyEncryptionContext(decryptOnlyEncryptionContext);
var beaconEncryptionContext := Structure.BeaconKeyEncryptionContext(decryptOnlyEncryptionContext);
assert KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, decryptOnlyEncryptionContext);
var wrappedDecryptOnlyBranchKey :- KMSKeystoreOperations.GenerateKey(
decryptOnlyEncryptionContext,
kmsConfiguration,
grantTokens,
kmsClient
);
assert KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, activeEncryptionContext);
var wrappedActiveBranchKey :- KMSKeystoreOperations.ReEncryptKey(
wrappedDecryptOnlyBranchKey.CiphertextBlob.value,
decryptOnlyEncryptionContext,
activeEncryptionContext,
kmsConfiguration,
grantTokens,
kmsClient
);
assert KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, beaconEncryptionContext);
var wrappedBeaconKey :- KMSKeystoreOperations.GenerateKey(
beaconEncryptionContext,
kmsConfiguration,
grantTokens,
kmsClient
);
var _ :- storage.WriteNewEncryptedBranchKey(
Types.WriteNewEncryptedBranchKeyInput(
Active := Structure.ConstructEncryptedHierarchicalKey(
activeEncryptionContext,
wrappedActiveBranchKey.CiphertextBlob.value
),
Version := Structure.ConstructEncryptedHierarchicalKey(
decryptOnlyEncryptionContext,
wrappedDecryptOnlyBranchKey.CiphertextBlob.value
),
Beacon := Structure.ConstructEncryptedHierarchicalKey(
beaconEncryptionContext,
wrappedBeaconKey.CiphertextBlob.value
)
)
);
output := Success(
Types.CreateKeyOutput(
branchKeyIdentifier := branchKeyIdentifier
));
}
method VersionActiveBranchKey(
input: Types.VersionKeyInput,
timestamp: string,
branchKeyVersion: string,
logicalKeyStoreName: string,
kmsConfiguration: Types.KMSConfiguration,
grantTokens: KMS.GrantTokenList,
kmsClient: KMS.IKMSClient,
storage: Types.IKeyStorageInterface
)
returns (output: Result<Types.VersionKeyOutput, Types.Error>)
requires 0 < |input.branchKeyIdentifier| && 0 < |branchKeyVersion|
requires storage.Modifies !! kmsClient.Modifies
requires KMSKeystoreOperations.HasKeyId(kmsConfiguration) && KmsArn.ValidKmsArn?(KMSKeystoreOperations.GetKeyId(kmsConfiguration))
requires storage is DefaultKeyStorageInterface.DynamoDBKeyStorageInterface
==>
logicalKeyStoreName == (storage as DefaultKeyStorageInterface.DynamoDBKeyStorageInterface).logicalKeyStoreName
requires kmsClient.ValidState() && storage.ValidState()
modifies storage.Modifies, kmsClient.Modifies
ensures storage.ValidState() && kmsClient.ValidState()
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# VersionKey MUST first get the active version for the branch key from the keystore
//# by calling the configured [KeyStorage interface's](./key-store/key-storage.md#interface)
//# [GetEncryptedActiveBranchKey](./key-store/key-storage.md#getencryptedactivebranchkey)
//# using the `branch-key-id`.
ensures
&& |storage.History.GetEncryptedActiveBranchKey| == |old(storage.History.GetEncryptedActiveBranchKey)| + 1
&& Seq.Last(storage.History.GetEncryptedActiveBranchKey).input.Identifier == input.branchKeyIdentifier
ensures output.Success?
==>
&& Seq.Last(storage.History.GetEncryptedActiveBranchKey).output.Success?
&& var oldActiveItem := Seq.Last(storage.History.GetEncryptedActiveBranchKey).output.value.Item;
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# VersionKey MUST verify that the returned EncryptedHierarchicalKey MUST have the requested `branch-key-id`.
&& oldActiveItem.Identifier == input.branchKeyIdentifier
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# VersionKey MUST verify that the returned EncryptedHierarchicalKey is an ActiveHierarchicalSymmetricVersion.
&& Structure.ActiveHierarchicalSymmetricKey?(oldActiveItem)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# VersionKey MUST verify that the returned EncryptedHierarchicalKey MUST have a logical table name equal to the configured logical table name.
&& oldActiveItem.EncryptionContext[Structure.TABLE_FIELD] == logicalKeyStoreName
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# The `KmsArn` of the [EncryptedHierarchicalKey](./key-store/key-storage.md#encryptedhierarchicalkey)
//# MUST be [compatible with](#aws-key-arn-compatibility)
//# the configured `KMS ARN` in the [AWS KMS Configuration](#aws-kms-configuration) for this keystore.
&& KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, oldActiveItem.EncryptionContext)
&& KMSKeystoreOperations.Compatible?(kmsConfiguration, oldActiveItem.KmsArn)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# The [EncryptedHierarchicalKey](./key-store/key-storage.md#encryptedhierarchicalkey)
//# MUST be authenticated according to [authenticating a keystore item](#authenticating-an-encryptedhierarchicalkey).
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# The operation MUST use the configured `KMS SDK Client` to authenticate the value of the keystore item.
&& |kmsClient.History.ReEncrypt| == |old(kmsClient.History.ReEncrypt)| + 2 // This 2 because we need to wrap the new version
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# The operation MUST call [AWS KMS API ReEncrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html)
//# with a request constructed as follows:
&& var reEncryptInput := Seq.Last(Seq.DropLast(kmsClient.History.ReEncrypt)).input;
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# - `SourceEncryptionContext` MUST be the [encryption context](#encryption-context) of the EncryptedHierarchicalKey to be authenticated
&& reEncryptInput.SourceEncryptionContext == Some(oldActiveItem.EncryptionContext)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# - `SourceKeyId` MUST be [compatible with](#aws-key-arn-compatibility) the configured KMS Key in the [AWS KMS Configuration](#aws-kms-configuration) for this keystore.
&& KMSKeystoreOperations.OptCompatible?(kmsConfiguration, reEncryptInput.SourceKeyId)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# - `CiphertextBlob` MUST be the `CiphertextBlob` attribute value on the EncryptedHierarchicalKey to be authenticated
&& reEncryptInput.CiphertextBlob == oldActiveItem.CiphertextBlob
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# - `GrantTokens` MUST be the configured [grant tokens](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#grant_token).
&& reEncryptInput.GrantTokens == Some(grantTokens)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# - `DestinationKeyId` MUST be [compatible with](#aws-key-arn-compatibility) the configured KMS Key in the [AWS KMS Configuration](#aws-kms-configuration) for this keystore.
&& KMSKeystoreOperations.Compatible?(kmsConfiguration, reEncryptInput.DestinationKeyId)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#authenticating-an-encryptedhierarchicalkey
//= type=implication
//# - `DestinationEncryptionContext` MUST be the [encryption context](#encryption-context) of the EncryptedHierarchicalKey to be authenticated
&& reEncryptInput.DestinationEncryptionContext == Some(oldActiveItem.EncryptionContext)
&& |kmsClient.History.GenerateDataKeyWithoutPlaintext| == |old(kmsClient.History.GenerateDataKeyWithoutPlaintext)| + 1
&& old(kmsClient.History.GenerateDataKeyWithoutPlaintext) < kmsClient.History.GenerateDataKeyWithoutPlaintext
&& old(kmsClient.History.ReEncrypt) < kmsClient.History.ReEncrypt
&& var decryptOnlyEncryptionContext := Structure.NewVersionFromActiveBranchKeyEncryptionContext(
oldActiveItem.EncryptionContext,
branchKeyVersion,
timestamp
);
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# The wrapped Branch Keys, DECRYPT_ONLY and ACTIVE, MUST be created according to [Wrapped Branch Key Creation](#wrapped-branch-key-creation).
&& WrappedBranchKeyCreation?(
Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext),
Seq.Last(kmsClient.History.ReEncrypt),
kmsClient,
kmsConfiguration,
grantTokens,
decryptOnlyEncryptionContext
)
&& |storage.History.WriteNewEncryptedBranchKeyVersion| == |old(storage.History.WriteNewEncryptedBranchKeyVersion)| + 1
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# If creation of the keys are successful,
//# then the key store MUST call the configured [KeyStorage interface's](./key-store/key-storage.md#interface)
//# [WriteNewEncryptedBranchKeyVersion](./key-store/key-storage.md##writenewencryptedbranchkeyversion)
//# with these 2 [EncryptedHierarchicalKeys](./key-store/key-storage.md##encryptedhierarchicalkey).
&& Seq.Last(storage.History.WriteNewEncryptedBranchKeyVersion).input.Active.Item
== Structure.ConstructEncryptedHierarchicalKey(
Seq.Last(kmsClient.History.ReEncrypt).input.DestinationEncryptionContext.value,
Seq.Last(kmsClient.History.ReEncrypt).output.value.CiphertextBlob.value
)
&& Seq.Last(storage.History.WriteNewEncryptedBranchKeyVersion).input.Version
== Structure.ConstructEncryptedHierarchicalKey(
Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).input.EncryptionContext.value,
Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).output.value.CiphertextBlob.value
)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# The `kms-arn` stored in the table MUST NOT change as a result of this operation,
//# even if the KeyStore is configured with a `KMS MRKey ARN` that does not exactly match the stored ARN.
&& Seq.Last(storage.History.WriteNewEncryptedBranchKeyVersion).input.Active.Item.KmsArn == oldActiveItem.KmsArn
&& Seq.Last(storage.History.WriteNewEncryptedBranchKeyVersion).input.Version.KmsArn == oldActiveItem.KmsArn
&& Seq.Last(storage.History.WriteNewEncryptedBranchKeyVersion).output.Success?
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# If the [WriteNewEncryptedBranchKeyVersion](./key-store/key-storage.md#writenewencryptedbranchkeyversion) is successful,
//# this operation MUST return a successful response containing no additional data.
&& output == Success(Types.VersionKeyOutput)
ensures
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# If the item fails to authenticate this operation MUST fail.
|| (&& |kmsClient.History.ReEncrypt| == |old(kmsClient.History.ReEncrypt)| + 1
&& Seq.Last(kmsClient.History.ReEncrypt).output.Failure?
==> output.Failure?)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# Otherwise, this operation MUST yield an error.
|| (&& |kmsClient.History.GenerateDataKeyWithoutPlaintext| == |old(kmsClient.History.GenerateDataKeyWithoutPlaintext)| + 1
&& Seq.Last(kmsClient.History.GenerateDataKeyWithoutPlaintext).output.Failure?
==> output.Failure?)
|| (&& |kmsClient.History.ReEncrypt| == |old(kmsClient.History.ReEncrypt)| + 2
&& Seq.Last(kmsClient.History.ReEncrypt).output.Failure?
==> output.Failure?)
|| (&& |storage.History.GetEncryptedActiveBranchKey| == |old(storage.History.GetEncryptedActiveBranchKey)| + 1
&& Seq.Last(storage.History.WriteNewEncryptedBranchKeyVersion).output.Failure?
==> output.Failure?)
|| (&& |storage.History.WriteNewEncryptedBranchKeyVersion| == |old(storage.History.WriteNewEncryptedBranchKeyVersion)| + 1
&& Seq.Last(storage.History.WriteNewEncryptedBranchKeyVersion).output.Failure?
==> output.Failure?)
{
var GetEncryptedActiveBranchKeyOutput :- storage.GetEncryptedActiveBranchKey(
Types.GetEncryptedActiveBranchKeyInput(
Identifier := input.branchKeyIdentifier
)
);
var oldActiveItem := GetEncryptedActiveBranchKeyOutput.Item;
:- Need(
|| storage is DefaultKeyStorageInterface.DynamoDBKeyStorageInterface
|| (
&& oldActiveItem.Identifier == input.branchKeyIdentifier
&& Structure.ActiveHierarchicalSymmetricKey?(oldActiveItem)
&& oldActiveItem.EncryptionContext[Structure.TABLE_FIELD] == logicalKeyStoreName
),
Types.KeyStoreException(
message := ErrorMessages.INVALID_ACTIVE_BRANCH_KEY_FROM_STORAGE)
);
// TODO-HV-2-M3: Support Version in HV-2
:- Need(
oldActiveItem.EncryptionContext[Structure.HIERARCHY_VERSION] == Structure.HIERARCHY_VERSION_VALUE_1,
Types.KeyStoreException(
message := "At this time, VersionKey ONLY supports HV-1; BK's Active Item is HV-2.")
);
:- Need(
&& KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, oldActiveItem.EncryptionContext),
Types.KeyStoreException(
message := ErrorMessages.VERSION_KEY_KMS_KEY_ARN_DISAGREEMENT)
);
var _ :- KMSKeystoreOperations.ReEncryptKey(
oldActiveItem.CiphertextBlob,
oldActiveItem.EncryptionContext,
oldActiveItem.EncryptionContext,
kmsConfiguration,
grantTokens,
kmsClient
);
var decryptOnlyEncryptionContext := Structure.NewVersionFromActiveBranchKeyEncryptionContext(
oldActiveItem.EncryptionContext,
branchKeyVersion,
timestamp
);
var activeEncryptionContext := Structure.ActiveBranchKeyEncryptionContext(decryptOnlyEncryptionContext);
assert KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, decryptOnlyEncryptionContext);
var wrappedDecryptOnlyBranchKey :- KMSKeystoreOperations.GenerateKey(
decryptOnlyEncryptionContext,
kmsConfiguration,
grantTokens,
kmsClient
);
var wrappedActiveBranchKey :- KMSKeystoreOperations.ReEncryptKey(
wrappedDecryptOnlyBranchKey.CiphertextBlob.value,
decryptOnlyEncryptionContext,
activeEncryptionContext,
kmsConfiguration,
grantTokens,
kmsClient
);
var active := Structure.ConstructEncryptedHierarchicalKey(
activeEncryptionContext,
wrappedActiveBranchKey.CiphertextBlob.value
);
var overWrite := Types.OverWriteEncryptedHierarchicalKey(
Item := active,
Old := oldActiveItem
);
var _ :- storage.WriteNewEncryptedBranchKeyVersion(
Types.WriteNewEncryptedBranchKeyVersionInput(
Active := overWrite,
Version := Structure.ConstructEncryptedHierarchicalKey(
decryptOnlyEncryptionContext,
wrappedDecryptOnlyBranchKey.CiphertextBlob.value
)
)
);
output := Success(Types.VersionKeyOutput());
}
twostate predicate WrappedBranchKeyCreation?(
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# The operation MUST call [AWS KMS API GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html).
new generateHistory: KMS.DafnyCallEvent<KMS.GenerateDataKeyWithoutPlaintextRequest, Result<KMS.GenerateDataKeyWithoutPlaintextResponse, KMS.Error>>,
new reEncryptHistory: KMS.DafnyCallEvent<KMS.ReEncryptRequest, Result<KMS.ReEncryptResponse, KMS.Error>>,
kmsClient: KMS.IKMSClient,
kmsConfiguration: Types.KMSConfiguration,
grantTokens: KMS.GrantTokenList,
decryptOnlyEncryptionContext: map<string, string>
)
reads kmsClient.History
requires KMSKeystoreOperations.HasKeyId(kmsConfiguration) && KmsArn.ValidKmsArn?(KMSKeystoreOperations.GetKeyId(kmsConfiguration))
// The history elements are "the things I added"
requires old(kmsClient.History.GenerateDataKeyWithoutPlaintext) < kmsClient.History.GenerateDataKeyWithoutPlaintext
requires old(kmsClient.History.ReEncrypt) < kmsClient.History.ReEncrypt
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# The call to AWS KMS GenerateDataKeyWithoutPlaintext MUST use the configured AWS KMS client to make the call.
requires
&& generateHistory in kmsClient.History.GenerateDataKeyWithoutPlaintext[|old(kmsClient.History.GenerateDataKeyWithoutPlaintext)|..]
&& reEncryptHistory in kmsClient.History.ReEncrypt[|old(kmsClient.History.ReEncrypt)|..]
{
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# The operation MUST call AWS KMS GenerateDataKeyWithoutPlaintext with a request constructed as follows:
&& var decryptOnlyKmsInput := generateHistory.input;
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `KeyId` MUST be [compatible with](#aws-key-arn-compatibility) the configured KMS Key in the [AWS KMS Configuration](#aws-kms-configuration) for this keystore.
&& KMSKeystoreOperations.Compatible?(kmsConfiguration, decryptOnlyKmsInput.KeyId)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `NumberOfBytes` MUST be 32.
&& decryptOnlyKmsInput.NumberOfBytes == Some(32)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `EncryptionContext` MUST be the [DECRYPT_ONLY encryption context for branch keys](#decrypt_only-encryption-context).
&& Structure.BranchKeyContext?(decryptOnlyEncryptionContext)
&& decryptOnlyKmsInput.EncryptionContext == Some(decryptOnlyEncryptionContext)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - GenerateDataKeyWithoutPlaintext `GrantTokens` MUST be this keystore's [grant tokens](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#grant_token).
&& decryptOnlyKmsInput.GrantTokens == Some(grantTokens)
&& generateHistory.output.Success?
&& generateHistory.output.value.CiphertextBlob.Some?
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# The operation MUST call [AWS KMS API ReEncrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html)
//# with a request constructed as follows:
&& var activeInput := reEncryptHistory.input;
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `SourceKeyId` MUST be [compatible with](#aws-key-arn-compatibility) the configured KMS Key in the [AWS KMS Configuration](#aws-kms-configuration) for this keystore.
&& KMSKeystoreOperations.OptCompatible?(kmsConfiguration, activeInput.SourceKeyId)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `DestinationKeyId` MUST be [compatible with](#aws-key-arn-compatibility) the configured KMS Key in the [AWS KMS Configuration](#aws-kms-configuration) for this keystore.
&& KMSKeystoreOperations.Compatible?(kmsConfiguration, activeInput.DestinationKeyId)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - ReEncrypt `GrantTokens` MUST be this keystore's [grant tokens](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#grant_token).
&& activeInput.GrantTokens == Some(grantTokens)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `CiphertextBlob` MUST be the wrapped DECRYPT_ONLY Branch Key.
&& activeInput.CiphertextBlob == generateHistory.output.value.CiphertextBlob.value
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `SourceEncryptionContext` MUST be the [DECRYPT_ONLY encryption context for branch keys](#decrypt_only-encryption-context).
&& activeInput.SourceEncryptionContext == Some(decryptOnlyEncryptionContext)
&& Structure.BRANCH_KEY_TYPE_PREFIX < decryptOnlyEncryptionContext[Structure.TYPE_FIELD]
&& Structure.BRANCH_KEY_ACTIVE_VERSION_FIELD !in decryptOnlyEncryptionContext
//= aws-encryption-sdk-specification/framework/branch-key-store.md#wrapped-branch-key-creation
//= type=implication
//# - `DestinationEncryptionContext` MUST be the [ACTIVE encryption context for branch keys](#active-encryption-context).
&& activeInput.DestinationEncryptionContext == Some(Structure.ActiveBranchKeyEncryptionContext(decryptOnlyEncryptionContext))
&& reEncryptHistory.output.Success?
&& reEncryptHistory.output.value.CiphertextBlob.Some?
}
}