Skip to content

Commit

Permalink
Introduce a method for registering AES-GCM KeyManager in a Config.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 694475027
Change-Id: Ie8cde87c125c89d730d4aabae6177d2a7b9011cc
  • Loading branch information
LizaTretyakova authored and copybara-github committed Nov 8, 2024
1 parent bcf1817 commit bc0c9a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions aead/aesgcm/key_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
30 changes: 30 additions & 0 deletions aead/aesgcm/key_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit bc0c9a1

Please sign in to comment.