Debian Codegraph @

keymanager/key_protection_service/service.go

raw
1 // Package keyprotectionservice implements the Key Orchestration Layer (KOL)
2 // for the Key Protection Service. It wraps the KPS Key Custody Core (KCC) FFI
3 // to provide a Go-native interface for cryptographic operations and key management.
4 package keyprotectionservice
5
6 import (
7 kpskcc "github.com/google/go-tpm-tools/keymanager/key_protection_service/key_custody_core"
8 "github.com/google/uuid"
9
10 keymanager "github.com/google/go-tpm-tools/keymanager/km_common/proto"
11 )
12
13 // KeyProtectionService provides the core key custody operations.
14 type KeyProtectionService interface {
15 // GenerateKEMKeypair generates a new Key Encapsulation Mechanism (KEM) keypair.
16 // The generated KEM key is linked to the provided binding public key, ensuring that
17 // only the holder of the corresponding binding private key can ultimately access
18 // the shared secrets.
19 //
20 // Parameters:
21 // - algo: The HPKE algorithm suite to use for the KEM keypair.
22 // - bindingPubKey: The public key of the workload/client that this KEM key is bound to.
23 // - lifespanSecs: The duration (in seconds) for which the generated keypair remains valid.
24 //
25 // Returns:
26 // - uuid.UUID: A unique identifier representing the stored KEM keypair.
27 // - []byte: The public KEM key bytes to be shared with the sender.
28 // - error: An error if generation or storage fails.
29 GenerateKEMKeypair(algo *keymanager.HpkeAlgorithm, bindingPubKey []byte, lifespanSecs uint64) (uuid.UUID, []byte, error)
30
31 // DecapAndSeal decapsulates an encapsulated shared secret and reseals it.
32 // This operation uses the stored private KEM key to recover the shared secret,
33 // and immediately reseals it using the binding public key associated with the KEM key.
34 //
35 // Parameters:
36 // - kemUUID: The unique identifier of the stored KEM keypair to use for decapsulation.
37 // - encapsulatedKey: The encapsulated shared secret received from a sender.
38 // - aad: Additional Authenticated Data to include in the resealing process.
39 //
40 // Returns:
41 // - []byte: The encapsulated key for the resealed shared secret (seal_enc).
42 // - []byte: The authenticated ciphertext of the resealed shared secret (sealed_ct).
43 // - error: An error if the KEM key is not found, expired, or if decapsulation/sealing fails.
44 DecapAndSeal(kemUUID uuid.UUID, encapsulatedKey, aad []byte) ([]byte, []byte, error)
45
46 // EnumerateKEMKeys retrieves a list of active KEM keys, up to a specified limit
47 // and starting from a given offset.
48 //
49 // Parameters:
50 // - limit: The maximum number of keys to return.
51 // - offset: The index of the first key to return.
52 //
53 // Returns:
54 // - []kpskcc.KEMKeyInfo: A slice of KEM key information structs.
55 // - bool: True if there are more keys available.
56 // - error: An error if the enumeration fails.
57 EnumerateKEMKeys(limit, offset int) ([]kpskcc.KEMKeyInfo, bool, error)
58
59 // DestroyKEMKey removes the specified KEM keypair from the active key registry.
60 // This prevents any future decapsulation operations using this key.
61 //
62 // Parameters:
63 // - kemUUID: The unique identifier of the stored KEM keypair to destroy.
64 //
65 // Returns:
66 // - error: An error if the key is not found or deletion fails.
67 DestroyKEMKey(kemUUID uuid.UUID) error
68
69 // GetKEMKey retrieves metadata and public keys associated with a stored KEM keypair.
70 //
71 // Parameters:
72 // - id: The unique identifier of the stored KEM keypair.
73 //
74 // Returns:
75 // - []byte: The public KEM key bytes.
76 // - []byte: The associated binding public key bytes.
77 // - *keymanager.HpkeAlgorithm: The HPKE algorithm suite of the KEM key.
78 // - uint64: The remaining lifespan of the keypair in seconds.
79 // - error: An error if the key is not found or has expired.
80 GetKEMKey(id uuid.UUID) ([]byte, []byte, *keymanager.HpkeAlgorithm, uint64, error)
81 }
82
83 // defaultKPS implements KeyProtectionService by delegating to the KPS KCC FFI.
84 type defaultKPS struct{}
85
86 func (d *defaultKPS) GenerateKEMKeypair(algo *keymanager.HpkeAlgorithm, bindingPubKey []byte, lifespanSecs uint64) (uuid.UUID, []byte, error) {
87 return kpskcc.GenerateKEMKeypair(algo, bindingPubKey, lifespanSecs)
88 }
89
90 func (d *defaultKPS) DecapAndSeal(kemUUID uuid.UUID, encapsulatedKey, aad []byte) ([]byte, []byte, error) {
91 return kpskcc.DecapAndSeal(kemUUID, encapsulatedKey, aad)
92 }
93
94 func (d *defaultKPS) EnumerateKEMKeys(limit, offset int) ([]kpskcc.KEMKeyInfo, bool, error) {
95 return kpskcc.EnumerateKEMKeys(limit, offset)
96 }
97
98 func (d *defaultKPS) DestroyKEMKey(kemUUID uuid.UUID) error {
99 return kpskcc.DestroyKEMKey(kemUUID)
100 }
101
102 func (d *defaultKPS) GetKEMKey(id uuid.UUID) ([]byte, []byte, *keymanager.HpkeAlgorithm, uint64, error) {
103 return kpskcc.GetKEMKey(id)
104 }
105
106 // Service implements KeyProtectionService by delegating to an underlying KeyProtectionService.
107 type Service struct {
108 kps KeyProtectionService
109 }
110
111 // NewService creates a new KPS KOL service using the default KPS.
112 func NewService() *Service {
113 return &Service{
114 kps: &defaultKPS{},
115 }
116 }
117
118 // newServiceWithKPS creates a new KPS KOL service using the provided KPS for testing.
119 func newServiceWithKPS(kps KeyProtectionService) *Service {
120 return &Service{
121 kps: kps,
122 }
123 }
124
125 // GenerateKEMKeypair generates a new KEM keypair linked to the provided binding
126 // public key by delegating to the underlying KeyProtectionService backend.
127 // It ensures that only the intended workload (holding the binding private key)
128 // can access the shared secrets sent to the generated KEM public key.
129 func (s *Service) GenerateKEMKeypair(algo *keymanager.HpkeAlgorithm, bindingPubKey []byte, lifespanSecs uint64) (uuid.UUID, []byte, error) {
130 return s.kps.GenerateKEMKeypair(algo, bindingPubKey, lifespanSecs)
131 }
132
133 // DecapAndSeal securely decapsulates an encapsulated shared secret using the
134 // stored KEM private key and immediately reseals it for the workload using its
135 // associated binding public key. It delegates cryptographic operations to the
136 // underlying KeyProtectionService backend.
137 func (s *Service) DecapAndSeal(kemUUID uuid.UUID, encapsulatedKey, aad []byte) ([]byte, []byte, error) {
138 return s.kps.DecapAndSeal(kemUUID, encapsulatedKey, aad)
139 }
140
141 // EnumerateKEMKeys enumerates active KEM keys up to limit by offset calling the underlying KPS.
142 func (s *Service) EnumerateKEMKeys(limit, offset int) ([]kpskcc.KEMKeyInfo, bool, error) {
143 return s.kps.EnumerateKEMKeys(limit, offset)
144 }
145
146 // DestroyKEMKey destroys the KEM key identified by kemUUID by calling the KPS KCC FFI.
147 // DestroyKEMKey removes the specified KEM keypair from the active key registry
148 // by delegating to the underlying KeyProtectionService backend.
149 func (s *Service) DestroyKEMKey(kemUUID uuid.UUID) error {
150 return s.kps.DestroyKEMKey(kemUUID)
151 }
152
153 // GetKEMKey retrieves the public KEM key, binding public key, HPKE algorithm,
154 // and remaining lifespan in seconds of a stored KEM keypair by delegating
155 // to the underlying KeyProtectionService backend.
156 func (s *Service) GetKEMKey(id uuid.UUID) ([]byte, []byte, *keymanager.HpkeAlgorithm, uint64, error) {
157 return s.kps.GetKEMKey(id)
158 }