diff --git a/aead/aesgcm/key_manager.go b/aead/aesgcm/key_manager.go index 87c23e3..16a6d42 100644 --- a/aead/aesgcm/key_manager.go +++ b/aead/aesgcm/key_manager.go @@ -21,6 +21,7 @@ import ( "google.golang.org/protobuf/proto" "github.com/tink-crypto/tink-go/v2/aead/subtle" "github.com/tink-crypto/tink-go/v2/core/registry" + "github.com/tink-crypto/tink-go/v2/internal/internalapi" "github.com/tink-crypto/tink-go/v2/keyset" "github.com/tink-crypto/tink-go/v2/subtle/random" gcmpb "github.com/tink-crypto/tink-go/v2/proto/aes_gcm_go_proto" @@ -157,3 +158,15 @@ func (km *keyManager) validateKeyFormat(format *gcmpb.AesGcmKeyFormat) error { } return nil } + +type config interface { + RegisterKeyManager(keyTypeURL string, km registry.KeyManager, t internalapi.Token) error +} + +// RegisterKeyManager accepts a config object and registers an +// instance of an AES-GCM AEAD KeyManager to the provided config. +// +// It is *NOT* part of the public API. +func RegisterKeyManager(c config, t internalapi.Token) error { + return c.RegisterKeyManager(typeURL, new(keyManager), t) +} diff --git a/aead/aesgcm/key_manager_test.go b/aead/aesgcm/key_manager_test.go index 4a064c3..ced5c89 100644 --- a/aead/aesgcm/key_manager_test.go +++ b/aead/aesgcm/key_manager_test.go @@ -21,8 +21,10 @@ import ( "github.com/google/go-cmp/cmp" "google.golang.org/protobuf/proto" + "github.com/tink-crypto/tink-go/v2/aead/aesgcm" "github.com/tink-crypto/tink-go/v2/aead/subtle" "github.com/tink-crypto/tink-go/v2/core/registry" + "github.com/tink-crypto/tink-go/v2/internal/internalapi" "github.com/tink-crypto/tink-go/v2/internal/internalregistry" "github.com/tink-crypto/tink-go/v2/subtle/random" "github.com/tink-crypto/tink-go/v2/testutil" @@ -459,3 +461,31 @@ func validateAESGCMPrimitive(p any, key *gcmpb.AesGcmKey) error { } return nil } + +type stubConfig struct { + keyManagers map[string]registry.KeyManager +} + +func (sc *stubConfig) RegisterKeyManager(keyTypeURL string, km registry.KeyManager, _ internalapi.Token) error { + sc.keyManagers[keyTypeURL] = km + return nil +} + +func TestRegisterKeyManager(t *testing.T) { + sc := &stubConfig{make(map[string]registry.KeyManager)} + if len(sc.keyManagers) != 0 { + t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.keyManagers)) + } + + err := aesgcm.RegisterKeyManager(sc, internalapi.Token{}) + if err != nil { + t.Fatalf("RegisterKeyManager() err = %v, want nil", err) + } + + if len(sc.keyManagers) != 1 { + t.Errorf("Number of registered key types = %d, want 1", len(sc.keyManagers)) + } + if _, ok := sc.keyManagers[testutil.AESGCMTypeURL]; !ok { + t.Errorf("RegisterKeyManager() registered wrong type URL, want %q", testutil.AESGCMTypeURL) + } +}