Skip to content

Commit

Permalink
Add Config for key generation
Browse files Browse the repository at this point in the history
The type allows to register key creator functions that can create `key.Key` values from `key.Parameters` objects.

PiperOrigin-RevId: 716201999
Change-Id: Iebc24ae17f77d2af0e3dd36c2d557b3e80bb7f1a
  • Loading branch information
morambro authored and copybara-github committed Jan 16, 2025
1 parent 4460d31 commit dc92a17
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
66 changes: 66 additions & 0 deletions internal/keygenconfig/keygenconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package keygenconfig provides internal implementation of keygen configs.
package keygenconfig

import (
"fmt"
"reflect"

"github.com/tink-crypto/tink-go/v2/key"
)

// Config keeps a collection of functions that create keys from
// [key.Primitive].
type Config struct {
keyCreators map[reflect.Type]func(p key.Parameters, idRequirement uint32) (key.Key, error)
}

// New creates an empty [Config].
func New() *Config {
return &Config{
keyCreators: map[reflect.Type]func(p key.Parameters, idRequirement uint32) (key.Key, error){},
}
}

// RegisterKeyCreator registers a function that creates a key from
// the given [key.Parameters].
// Not thread-safe.
//
// Returns an error if a creator for parametersType is already registered (no
// matter whether it's the same object or different, since constructors are of
// type [Func] and they are never considered equal in Go unless they are nil).
//
// This is an internal API.
func (c *Config) RegisterKeyCreator(parametersType reflect.Type, creator func(p key.Parameters, idRequirement uint32) (key.Key, error)) error {
if _, found := c.keyCreators[parametersType]; found {
return fmt.Errorf("a different key creator already registered for %v", parametersType)
}
c.keyCreators[parametersType] = creator
return nil
}

// CreateKey creates a key from the given [key.Parameters] using the registry.
//
// Not thread-safe.
//
// This is an internal API.
func (c *Config) CreateKey(p key.Parameters, idRequirement uint32) (key.Key, error) {
creator, found := c.keyCreators[reflect.TypeOf(p)]
if !found {
return nil, fmt.Errorf("no creator found for parameters %T", p)
}
return creator(p, idRequirement)
}
110 changes: 110 additions & 0 deletions internal/keygenconfig/keygenconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package keygenconfig_test

import (
"errors"
"reflect"
"testing"

"github.com/tink-crypto/tink-go/v2/internal/keygenconfig"
"github.com/tink-crypto/tink-go/v2/key"
)

type stubParams1 struct{}

func (p *stubParams1) HasIDRequirement() bool { return true }
func (p *stubParams1) Equal(other key.Parameters) bool { return true }

type stubKey1 struct{}

func (k *stubKey1) Parameters() key.Parameters { return &stubParams1{} }
func (k *stubKey1) Equal(other key.Key) bool { return true }
func (k *stubKey1) IDRequirement() (uint32, bool) { return 123, true }

func stubKeyCreator1(p key.Parameters, idRequirement uint32) (key.Key, error) {
return &stubKey1{}, nil
}

type stubParams2 struct{}

func (p *stubParams2) HasIDRequirement() bool { return false }
func (p *stubParams2) Equal(other key.Parameters) bool { return true }

type stubKey2 struct{}

func (k *stubKey2) Parameters() key.Parameters { return nil }
func (k *stubKey2) Equal(other key.Key) bool { return true }
func (k *stubKey2) IDRequirement() (uint32, bool) { return 123, true }

func stubKeyCreator2(p key.Parameters, idRequirement uint32) (key.Key, error) {
return &stubKey2{}, nil
}

func TestRegisterKeyCreatorWorks(t *testing.T) {
config := keygenconfig.New()
if err := config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreator1); err != nil {
t.Fatalf("config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreator1) err = %v, want nil", err)
}
if err := config.RegisterKeyCreator(reflect.TypeOf((*stubParams2)(nil)), stubKeyCreator2); err != nil {
t.Fatalf("config.RegisterKeyCreator(reflect.TypeOf((*stubParams2)(nil)), stubKeyCreator2) err = %v, want nil", err)
}
key1, err := config.CreateKey(&stubParams1{}, 123)
if err != nil {
t.Fatalf("config.CreateKey(&stubParams1{}, 123) err = %v, want nil", err)
}
if _, ok := key1.(*stubKey1); !ok {
t.Errorf("key is of type %T; want *stubKey1", key1)
}
key2, err := config.CreateKey(&stubParams2{}, 123)
if err != nil {
t.Fatalf("config.CreateKey(&stubParams2{}, 123) err = %v, want nil", err)
}
if _, ok := key2.(*stubKey2); !ok {
t.Errorf("key is of type %T; want *stubKey2", key2)
}
}

func TestRegisterKeyCreatorFailsIfRegisteredTwice(t *testing.T) {
config := keygenconfig.New()
if err := config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreator1); err != nil {
t.Errorf("config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreator1) err = %v, want nil", err)
}
// Another creator function for the same type fails.
if err := config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreator2); err == nil {
t.Errorf("config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreator1) err = nil, want error")
}
}

func TestCreateKeyFailsIfKeyCreatorIsNotRegistered(t *testing.T) {
config := keygenconfig.New()
if _, err := config.CreateKey(&stubParams1{}, 123); err == nil {
t.Errorf("config.CreateKey(&stubParams1{}, 123) err = nil, want error")
}
}

func stubKeyCreatorFails(p key.Parameters, idRequirement uint32) (key.Key, error) {
return nil, errors.New("oh no :(")
}

func TestCreateKeyFailsIfKeyCreatorFails(t *testing.T) {
config := keygenconfig.New()
if err := config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreatorFails); err != nil {
t.Errorf("config.RegisterKeyCreator(reflect.TypeOf((*stubParams1)(nil)), stubKeyCreatorFails) err = %v, want nil", err)
}
if _, err := config.CreateKey(&stubParams1{}, 123); err == nil {
t.Errorf("config.CreateKey(&stubParams1{}, 123) err = nil, want error")
}
}

0 comments on commit dc92a17

Please sign in to comment.