@@ -176,8 +176,9 @@ func (p *Pool) Encrypt(ctx context.Context, att *enclave.Attestation, plaintext
176176
177177// Decrypt decrypts the ciphertext using the latest cipher key from the Pool referenced by the keyRef.
178178//
179- // The key is verified against the attestation and migrated to the current generation if needed.
180- // If a DEK cache is configured, cached keys bypass DynamoDB and KMS on hit.
179+ // The key is verified against the attestation and migrated to the current
180+ // generation if needed. A cached DEK skips all of that: it was verified when it
181+ // was loaded, and its key ref never maps to different key material.
181182func (p * Pool ) Decrypt (ctx context.Context , att * enclave.Attestation , keyRef string , ciphertext []byte , additionalData []byte ) (plaintext []byte , err error ) {
182183 ctx , span := tracing .Trace (ctx , "encryption.Pool.Decrypt" , tracing .WithAnnotation ("key_ref" , keyRef ))
183184 defer func () {
@@ -215,7 +216,8 @@ func (p *Pool) Decrypt(ctx context.Context, att *enclave.Attestation, keyRef str
215216// are deduplicated so only one of them performs the KMS round-trips.
216217func (p * Pool ) fetchDEK (ctx context.Context , att * enclave.Attestation , keyRef string ) ([]byte , error ) {
217218 if p .cache == nil {
218- return p .loadDEK (ctx , att , keyRef )
219+ dek , _ , err := p .loadDEK (ctx , att , keyRef )
220+ return dek , err
219221 }
220222
221223 // The caller's span, not loadDEK's child: the annotation belongs on the
@@ -228,11 +230,13 @@ func (p *Pool) fetchDEK(ctx context.Context, att *enclave.Attestation, keyRef st
228230 }
229231
230232 loaded := p .cache .group .DoChan (keyRef , func () (any , error ) {
231- dek , err := p .loadDEK (ctx , att , keyRef )
233+ dek , cacheable , err := p .loadDEK (ctx , att , keyRef )
232234 if err != nil {
233235 return nil , err
234236 }
235- p .cache .put (keyRef , dek )
237+ if cacheable {
238+ p .cache .put (keyRef , dek )
239+ }
236240 return dek , nil
237241 })
238242
@@ -254,8 +258,10 @@ func (p *Pool) fetchDEK(ctx context.Context, att *enclave.Attestation, keyRef st
254258}
255259
256260// loadDEK recovers a DEK through the full path: DynamoDB lookup, attestation
257- // verification, KMS share decryption, and Shamir combine.
258- func (p * Pool ) loadDEK (ctx context.Context , att * enclave.Attestation , keyRef string ) (privateKey []byte , err error ) {
261+ // verification, KMS share decryption, and Shamir combine. It reports whether the
262+ // result may be cached; a key whose migration failed must not be, or the retry
263+ // is suppressed until the entry expires.
264+ func (p * Pool ) loadDEK (ctx context.Context , att * enclave.Attestation , keyRef string ) (privateKey []byte , cacheable bool , err error ) {
259265 ctx , span := tracing .Trace (ctx , "encryption.Pool.loadDEK" , tracing .WithAnnotation ("key_ref" , keyRef ))
260266 defer func () {
261267 span .RecordError (err )
@@ -264,13 +270,13 @@ func (p *Pool) loadDEK(ctx context.Context, att *enclave.Attestation, keyRef str
264270
265271 key , found , err := p .keysTable .GetLatestByKeyRef (ctx , keyRef , false )
266272 if err != nil {
267- return nil , fmt .Errorf ("get latest key: %w" , err )
273+ return nil , false , fmt .Errorf ("get latest key: %w" , err )
268274 }
269275 if ! found {
270- return nil , fmt .Errorf ("key not found" )
276+ return nil , false , fmt .Errorf ("key not found" )
271277 }
272278 if err := p .VerifyKey (ctx , att , key ); err != nil {
273- return nil , fmt .Errorf ("verify key: %w" , err )
279+ return nil , false , fmt .Errorf ("verify key: %w" , err )
274280 }
275281
276282 span .SetAnnotation ("generation" , strconv .Itoa (key .Generation ))
@@ -280,26 +286,27 @@ func (p *Pool) loadDEK(ctx context.Context, att *enclave.Attestation, keyRef str
280286
281287 config , err := p .getConfig (key .Generation )
282288 if err != nil {
283- return nil , fmt .Errorf ("get config: %w" , err )
289+ return nil , false , fmt .Errorf ("get config: %w" , err )
284290 }
285291 if ! config .areSharesValid (key .EncryptedShares ) {
286- return nil , fmt .Errorf ("shares are invalid" )
292+ return nil , false , fmt .Errorf ("shares are invalid" )
287293 }
288294
289295 privateKey , err = p .combineShares (ctx , att , config , key .EncryptedShares )
290296 if err != nil {
291- return nil , fmt .Errorf ("combine shares: %w" , err )
297+ return nil , false , fmt .Errorf ("combine shares: %w" , err )
292298 }
293299
294- // Migration is synchronous but non-fatal: failure is logged, the DEK is
295- // still returned, and the next load past the cache TTL retries it.
300+ // Migration is non-fatal: the DEK is returned either way, but a failed
301+ // migration leaves the key uncacheable so the next decrypt retries it.
296302 if p .keyNeedsMigration (key ) {
297303 if err := p .migrateKey (ctx , att , key , privateKey ); err != nil {
298304 p .logger .ErrorContext (ctx , "migrating key failed" , "error" , err , "key_ref" , key .KeyRef , "generation" , key .Generation , "key_index" , key .KeyIndex )
305+ return privateKey , false , nil
299306 }
300307 }
301308
302- return privateKey , nil
309+ return privateKey , true , nil
303310}
304311
305312// RotateKey marks a key as inactive by setting its KeyIndex to a negative value. It won't be used for encrypting
0 commit comments