Skip to content

Commit 6303014

Browse files
morambrocopybara-github
authored andcommitted
Simplify legacykeymanager to require a primitive constructor instead of a config
PiperOrigin-RevId: 833197726 Change-Id: I33c7b6287a713549c091cc2e4cfdcdf20bf44b1d
1 parent aa8783c commit 6303014

File tree

25 files changed

+92
-211
lines changed

25 files changed

+92
-211
lines changed

aead/aesctrhmac/aesctrhmac.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ import (
2727
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
2828
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
2929
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
30-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
3130
aesctrhmacpb "github.com/tink-crypto/tink-go/v2/proto/aes_ctr_hmac_aead_go_proto"
3231
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3332
)
3433

3534
func newKeyManager() registry.KeyManager {
36-
return legacykeymanager.New(typeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
35+
return legacykeymanager.New(typeURL, primitiveConstructor, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
3736
protoKey := &aesctrhmacpb.AesCtrHmacAeadKey{}
3837
if err := proto.Unmarshal(b, protoKey); err != nil {
3938
return nil, err

aead/aesgcm/aesgcm.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ import (
2727
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
2828
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
2929
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
30-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
3130
aesgcmpb "github.com/tink-crypto/tink-go/v2/proto/aes_gcm_go_proto"
3231
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3332
)
3433

3534
func newKeyManager() registry.KeyManager {
36-
return legacykeymanager.New(typeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
35+
return legacykeymanager.New(typeURL, primitiveConstructor, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
3736
protoKey := &aesgcmpb.AesGcmKey{}
3837
if err := proto.Unmarshal(b, protoKey); err != nil {
3938
return nil, err

aead/aesgcmsiv/aesgcmsiv.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ import (
2929
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
3030
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
3131
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
32-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
3332
aesgcmsivpb "github.com/tink-crypto/tink-go/v2/proto/aes_gcm_siv_go_proto"
3433
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3534
)
3635

3736
func newKeyManager() registry.KeyManager {
38-
return legacykeymanager.New(typeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
37+
return legacykeymanager.New(typeURL, primitiveConstructor, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
3938
protoKey := &aesgcmsivpb.AesGcmSivKey{}
4039
if err := proto.Unmarshal(b, protoKey); err != nil {
4140
return nil, err

aead/chacha20poly1305/chacha20poly1305.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ import (
2626
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
2727
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
2828
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
29-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
3029
chacha20poly1305pb "github.com/tink-crypto/tink-go/v2/proto/chacha20_poly1305_go_proto"
3130
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3231
)
3332

3433
func newKeyManager() registry.KeyManager {
35-
return legacykeymanager.New(typeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
34+
return legacykeymanager.New(typeURL, primitiveConstructor, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
3635
protoKey := &chacha20poly1305pb.ChaCha20Poly1305Key{}
3736
if err := proto.Unmarshal(b, protoKey); err != nil {
3837
return nil, err

aead/xaesgcm/xaesgcm.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ import (
2525
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
2626
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
2727
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
28-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
2928
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3029
xaesgcmpb "github.com/tink-crypto/tink-go/v2/proto/x_aes_gcm_go_proto"
3130
)
3231

3332
func newKeyManager() registry.KeyManager {
34-
return legacykeymanager.New(typeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
33+
return legacykeymanager.New(typeURL, primitiveConstructor, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
3534
protoKey := &xaesgcmpb.XAesGcmKey{}
3635
if err := proto.Unmarshal(b, protoKey); err != nil {
3736
return nil, err

aead/xchacha20poly1305/xchacha20poly1305.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ import (
2828
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
2929
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
3030
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
31-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
3231
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3332
xchacha20poly1305pb "github.com/tink-crypto/tink-go/v2/proto/xchacha20_poly1305_go_proto"
3433
)
3534

3635
func newKeyManager() registry.KeyManager {
37-
return legacykeymanager.New(typeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
36+
return legacykeymanager.New(typeURL, primitiveConstructor, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
3837
protoKey := &xchacha20poly1305pb.XChaCha20Poly1305Key{}
3938
if err := proto.Unmarshal(b, protoKey); err != nil {
4039
return nil, err

daead/aessiv/aessiv.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
2828
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
2929
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
30-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
3130
aessivpb "github.com/tink-crypto/tink-go/v2/proto/aes_siv_go_proto"
3231
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3332
)
@@ -62,7 +61,7 @@ func init() {
6261
if err := keygenregistry.RegisterKeyCreator[*Parameters](createKey); err != nil {
6362
panic(fmt.Sprintf("aessiv.init() failed: %v", err))
6463
}
65-
if err := registry.RegisterKeyManager(legacykeymanager.New(typeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
64+
if err := registry.RegisterKeyManager(legacykeymanager.New(typeURL, primitiveConstructor, tinkpb.KeyData_SYMMETRIC, func(b []byte) (proto.Message, error) {
6665
protoKey := &aessivpb.AesSivKey{}
6766
if err := proto.Unmarshal(b, protoKey); err != nil {
6867
return nil, err

hybrid/ecies/ecies.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
4242
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
4343
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
44-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
4544
eciespb "github.com/tink-crypto/tink-go/v2/proto/ecies_aead_hkdf_go_proto"
4645
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
4746
)
@@ -74,7 +73,7 @@ func init() {
7473
if err := keygenregistry.RegisterKeyCreator[*Parameters](createPrivateKey); err != nil {
7574
panic(fmt.Sprintf("ecies.init() failed: %v", err))
7675
}
77-
if err := registry.RegisterKeyManager(legacykeymanager.NewPrivateKeyManager(privateKeyTypeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_ASYMMETRIC_PRIVATE, func(b []byte) (proto.Message, error) {
76+
if err := registry.RegisterKeyManager(legacykeymanager.NewPrivateKeyManager(privateKeyTypeURL, hybridDecryptConstructor, tinkpb.KeyData_ASYMMETRIC_PRIVATE, func(b []byte) (proto.Message, error) {
7877
protoKey := &eciespb.EciesAeadHkdfPrivateKey{}
7978
if err := proto.Unmarshal(b, protoKey); err != nil {
8079
return nil, err
@@ -83,7 +82,7 @@ func init() {
8382
})); err != nil {
8483
panic(fmt.Sprintf("ecies.init() failed: %v", err))
8584
}
86-
if err := registry.RegisterKeyManager(legacykeymanager.New(publicKeyTypeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_ASYMMETRIC_PUBLIC, func(b []byte) (proto.Message, error) {
85+
if err := registry.RegisterKeyManager(legacykeymanager.New(publicKeyTypeURL, hybridEncryptConstructor, tinkpb.KeyData_ASYMMETRIC_PUBLIC, func(b []byte) (proto.Message, error) {
8786
protoKey := &eciespb.EciesAeadHkdfPublicKey{}
8887
if err := proto.Unmarshal(b, protoKey); err != nil {
8988
return nil, err

hybrid/hpke/hpke.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/tink-crypto/tink-go/v2/internal/legacykeymanager"
2828
"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
2929
"github.com/tink-crypto/tink-go/v2/internal/protoserialization"
30-
"github.com/tink-crypto/tink-go/v2/internal/registryconfig"
3130
hpkepb "github.com/tink-crypto/tink-go/v2/proto/hpke_go_proto"
3231
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
3332
)
@@ -76,12 +75,12 @@ func init() {
7675
if err := keygenregistry.RegisterKeyCreator[*Parameters](createPrivateKey); err != nil {
7776
panic(fmt.Sprintf("hpke.init() failed: %v", err))
7877
}
79-
if err := registry.RegisterKeyManager(legacykeymanager.NewPrivateKeyManager(privateKeyTypeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_ASYMMETRIC_PRIVATE, func(b []byte) (proto.Message, error) {
78+
if err := registry.RegisterKeyManager(legacykeymanager.NewPrivateKeyManager(privateKeyTypeURL, hybridDecryptConstructor, tinkpb.KeyData_ASYMMETRIC_PRIVATE, func(b []byte) (proto.Message, error) {
8079
return unmarshalHpkePrivateKey(b)
8180
})); err != nil {
8281
panic(fmt.Sprintf("hpke.init() failed: %v", err))
8382
}
84-
if err := registry.RegisterKeyManager(legacykeymanager.New(publicKeyTypeURL, &registryconfig.RegistryConfig{}, tinkpb.KeyData_ASYMMETRIC_PUBLIC, func(b []byte) (proto.Message, error) {
83+
if err := registry.RegisterKeyManager(legacykeymanager.New(publicKeyTypeURL, hybridEncryptConstructor, tinkpb.KeyData_ASYMMETRIC_PUBLIC, func(b []byte) (proto.Message, error) {
8584
return unmarshalHpkePublicKey(b)
8685
})); err != nil {
8786
panic(fmt.Sprintf("hpke.init() failed: %v", err))

internal/legacykeymanager/legacykeymanager.go

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,32 @@ type config interface {
3434
PrimitiveFromKey(key key.Key, _ internalapi.Token) (any, error)
3535
}
3636

37-
type primitiveFunc func(serializedKey []byte) (any, error)
37+
type primitiveConstructor func(key key.Key) (any, error)
3838

3939
// KeyManager implements the [registry.KeyManager] interface.
4040
type KeyManager struct {
41-
typeURL string
42-
config config
43-
protoKeyUnmashaller func([]byte) (proto.Message, error)
44-
keyMaterialType tinkpb.KeyData_KeyMaterialType
45-
primitiveFunc primitiveFunc
41+
typeURL string
42+
protoKeyUnmashaller func([]byte) (proto.Message, error)
43+
keyMaterialType tinkpb.KeyData_KeyMaterialType
44+
primitiveConstructor primitiveConstructor
4645
}
4746

4847
var _ registry.KeyManager = (*KeyManager)(nil)
4948

5049
// New creates a new [LegacyKeyManager].
5150
//
5251
// Assumes parameters are not nil.
53-
func New(typeURL string, config config, keyMaterialType tinkpb.KeyData_KeyMaterialType, protoKeyUnmashaller func([]byte) (proto.Message, error)) *KeyManager {
54-
km := &KeyManager{
55-
typeURL: typeURL,
56-
config: config,
57-
protoKeyUnmashaller: protoKeyUnmashaller,
58-
keyMaterialType: keyMaterialType,
59-
}
60-
km.primitiveFunc = km.defaultPrimitiveFunc
61-
return km
62-
}
63-
64-
// NewWithCustomPrimitive creates a new [LegacyKeyManager] with a custom primitive function.
65-
//
66-
// Assumes parameters are not nil.
67-
func NewWithCustomPrimitive(typeURL string, config config, keyMaterialType tinkpb.KeyData_KeyMaterialType, protoKeyUnmashaller func([]byte) (proto.Message, error), pf primitiveFunc) *KeyManager {
52+
func New(typeURL string, primitiveConstructor primitiveConstructor, keyMaterialType tinkpb.KeyData_KeyMaterialType, protoKeyUnmashaller func([]byte) (proto.Message, error)) *KeyManager {
6853
return &KeyManager{
69-
typeURL: typeURL,
70-
config: config,
71-
protoKeyUnmashaller: protoKeyUnmashaller,
72-
keyMaterialType: keyMaterialType,
73-
primitiveFunc: pf,
54+
typeURL: typeURL,
55+
protoKeyUnmashaller: protoKeyUnmashaller,
56+
keyMaterialType: keyMaterialType,
57+
primitiveConstructor: primitiveConstructor,
7458
}
7559
}
7660

77-
func (m *KeyManager) defaultPrimitiveFunc(serializedKey []byte) (any, error) {
61+
// Primitive creates a primitive from the given serialized key.
62+
func (m *KeyManager) Primitive(serializedKey []byte) (any, error) {
7863
keySerialization, err := protoserialization.NewKeySerialization(&tinkpb.KeyData{
7964
TypeUrl: m.typeURL,
8065
Value: serializedKey,
@@ -87,12 +72,7 @@ func (m *KeyManager) defaultPrimitiveFunc(serializedKey []byte) (any, error) {
8772
if err != nil {
8873
return nil, err
8974
}
90-
return m.config.PrimitiveFromKey(key, internalapi.Token{})
91-
}
92-
93-
// Primitive creates a primitive from the given serialized key.
94-
func (m *KeyManager) Primitive(serializedKey []byte) (any, error) {
95-
return m.primitiveFunc(serializedKey)
75+
return m.primitiveConstructor(key)
9676
}
9777

9878
// NewKey creates a new key from the given serialized key format.
@@ -150,16 +130,9 @@ type PrivateKeyManager struct {
150130
var _ registry.PrivateKeyManager = (*PrivateKeyManager)(nil)
151131

152132
// NewPrivateKeyManager creates a new [PrivateKeyManager].
153-
func NewPrivateKeyManager(typeURL string, config config, keyMaterialType tinkpb.KeyData_KeyMaterialType, protoKeyUnmashaller func([]byte) (proto.Message, error)) *PrivateKeyManager {
154-
return &PrivateKeyManager{
155-
KeyManager: *New(typeURL, config, keyMaterialType, protoKeyUnmashaller),
156-
}
157-
}
158-
159-
// NewPrivateKeyManagerWithCustomPrimitive creates a new [PrivateKeyManager] with a custom primitive function.
160-
func NewPrivateKeyManagerWithCustomPrimitive(typeURL string, config config, keyMaterialType tinkpb.KeyData_KeyMaterialType, protoKeyUnmashaller func([]byte) (proto.Message, error), pf primitiveFunc) *PrivateKeyManager {
133+
func NewPrivateKeyManager(typeURL string, primitiveConstructor primitiveConstructor, keyMaterialType tinkpb.KeyData_KeyMaterialType, protoKeyUnmashaller func([]byte) (proto.Message, error)) *PrivateKeyManager {
161134
return &PrivateKeyManager{
162-
KeyManager: *NewWithCustomPrimitive(typeURL, config, keyMaterialType, protoKeyUnmashaller, pf),
135+
KeyManager: *New(typeURL, primitiveConstructor, keyMaterialType, protoKeyUnmashaller),
163136
}
164137
}
165138

0 commit comments

Comments
 (0)