@@ -4,10 +4,7 @@ import (
4
4
"context"
5
5
"crypto"
6
6
"crypto/elliptic"
7
- "crypto/hmac"
8
- "crypto/sha256"
9
7
"errors"
10
- "fmt"
11
8
"log/slog"
12
9
13
10
"github.com/opentdf/platform/lib/ocrypto"
@@ -17,87 +14,6 @@ import (
17
14
18
15
const inProcessSystemName = "opentdf.io/in-process"
19
16
20
- // InProcessAESKey implements the trust.ProtectedKey interface with an in-memory secret key
21
- type InProcessAESKey struct {
22
- rawKey []byte
23
- logger * slog.Logger
24
- }
25
-
26
- var _ trust.ProtectedKey = (* InProcessAESKey )(nil )
27
-
28
- // NewInProcessAESKey creates a new instance of StandardUnwrappedKey
29
- func NewInProcessAESKey (rawKey []byte ) * InProcessAESKey {
30
- return & InProcessAESKey {
31
- rawKey : rawKey ,
32
- logger : slog .Default (),
33
- }
34
- }
35
-
36
- func (k * InProcessAESKey ) DecryptAESGCM (iv []byte , body []byte , tagSize int ) ([]byte , error ) {
37
- aesGcm , err := ocrypto .NewAESGcm (k .rawKey )
38
- if err != nil {
39
- return nil , err
40
- }
41
-
42
- decryptedData , err := aesGcm .DecryptWithIVAndTagSize (iv , body , tagSize )
43
- if err != nil {
44
- return nil , err
45
- }
46
-
47
- return decryptedData , nil
48
- }
49
-
50
- // Export returns the raw key data, optionally encrypting it with the provided trust.Encapsulator
51
- func (k * InProcessAESKey ) Export (encapsulator trust.Encapsulator ) ([]byte , error ) {
52
- if encapsulator == nil {
53
- if k .logger != nil {
54
- k .logger .Warn ("exporting raw key data without encryption" )
55
- }
56
- return k .rawKey , nil
57
- }
58
-
59
- // If an encryptor is provided, encrypt the key data before returning
60
- encryptedKey , err := encapsulator .Encrypt (k .rawKey )
61
- if err != nil {
62
- if k .logger != nil {
63
- k .logger .Warn ("failed to encrypt key data for export" , slog .Any ("err" , err ))
64
- }
65
- return nil , err
66
- }
67
-
68
- return encryptedKey , nil
69
- }
70
-
71
- // VerifyBinding checks if the policy binding matches the given policy data
72
- func (k * InProcessAESKey ) VerifyBinding (ctx context.Context , policy , policyBinding []byte ) error {
73
- if len (k .rawKey ) == 0 {
74
- return errors .New ("key data is empty" )
75
- }
76
-
77
- actualHMAC , err := k .generateHMACDigest (ctx , policy )
78
- if err != nil {
79
- return fmt .Errorf ("unable to generate policy hmac: %w" , err )
80
- }
81
-
82
- if ! hmac .Equal (actualHMAC , policyBinding ) {
83
- return errors .New ("policy hmac mismatch" )
84
- }
85
-
86
- return nil
87
- }
88
-
89
- // generateHMACDigest is a helper to generate an HMAC digest from a message using the key
90
- func (k * InProcessAESKey ) generateHMACDigest (ctx context.Context , msg []byte ) ([]byte , error ) {
91
- mac := hmac .New (sha256 .New , k .rawKey )
92
- _ , err := mac .Write (msg )
93
- if err != nil {
94
- if k .logger != nil {
95
- k .logger .WarnContext (ctx , "failed to compute hmac" )
96
- }
97
- return nil , errors .New ("policy hmac" )
98
- }
99
- return mac .Sum (nil ), nil
100
- }
101
17
102
18
func convertPEMToJWK (_ string ) (string , error ) {
103
19
// Implement the conversion logic here or use an external library if available.
@@ -328,16 +244,13 @@ func (a *InProcessProvider) Decrypt(ctx context.Context, keyDetails trust.KeyDet
328
244
return nil , err
329
245
}
330
246
331
- return & InProcessAESKey {
332
- rawKey : rawKey ,
333
- logger : a .logger ,
334
- }, nil
247
+ return ocrypto .NewAESProtectedKey (rawKey ), nil
335
248
}
336
249
337
250
// DeriveKey generates a symmetric key for NanoTDF
338
251
func (a * InProcessProvider ) DeriveKey (_ context.Context , keyDetails trust.KeyDetails , ephemeralPublicKeyBytes []byte , curve elliptic.Curve ) (trust.ProtectedKey , error ) {
339
252
k , err := a .cryptoProvider .GenerateNanoTDFSymmetricKey (string (keyDetails .ID ()), ephemeralPublicKeyBytes , curve )
340
- return NewInProcessAESKey (k ), err
253
+ return ocrypto . NewAESProtectedKey (k ), err
341
254
}
342
255
343
256
// GenerateECSessionKey generates a session key for NanoTDF
0 commit comments