-
Notifications
You must be signed in to change notification settings - Fork 14
Add support for Tink keyset signer #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hayden-IO
wants to merge
1
commit into
transparency-dev:main
Choose a base branch
from
Hayden-IO:add-tink-signer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright 2025 Google LLC. All Rights Reserved. | ||
| // | ||
| // 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 main | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto" | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/tink-crypto/tink-go-gcpkms/v2/integration/gcpkms" | ||
| "github.com/tink-crypto/tink-go/v2/core/registry" | ||
| "github.com/tink-crypto/tink-go/v2/keyset" | ||
| "github.com/tink-crypto/tink-go/v2/tink" | ||
| tinkUtils "github.com/transparency-dev/tesseract/internal/tink" | ||
| ) | ||
|
|
||
| const TinkScheme = "tink" | ||
|
|
||
| // NewTinkSignerVerifier returns a crypto.Signer. Only ECDSA P-256 is supported. | ||
| // Provide a path to the encrypted keyset and GCP KMS key URI for decryption. | ||
| func NewTinkSignerVerifier(ctx context.Context, kekURI, keysetPath string) (crypto.Signer, error) { | ||
| if kekURI == "" || keysetPath == "" { | ||
| return nil, fmt.Errorf("key encryption key URI or keyset path unset") | ||
| } | ||
| kek, err := getKeyEncryptionKey(ctx, kekURI) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| f, err := os.Open(filepath.Clean(keysetPath)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer f.Close() //nolint: errcheck | ||
|
|
||
| kh, err := keyset.Read(keyset.NewJSONReader(f), kek) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| signer, err := tinkUtils.KeyHandleToSigner(kh) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // validate that key is ECDSA P-256 | ||
| pub, ok := signer.Public().(*ecdsa.PublicKey) | ||
| if !ok { | ||
| return nil, fmt.Errorf("key must be ECDSA") | ||
| } | ||
| if pub.Curve != elliptic.P256() { | ||
| return nil, fmt.Errorf("elliptic curve must be P-256, was %s", pub.Curve.Params().Name) | ||
| } | ||
|
|
||
| return signer, err | ||
| } | ||
|
|
||
| // getKeyEncryptionKey returns a Tink AEAD encryption key from KMS | ||
| func getKeyEncryptionKey(ctx context.Context, kmsKey string) (tink.AEAD, error) { | ||
| switch { | ||
| case strings.HasPrefix(kmsKey, "gcp-kms://"): | ||
| gcpClient, err := gcpkms.NewClientWithOptions(ctx, kmsKey) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| registry.RegisterKMSClient(gcpClient) | ||
| return gcpClient.GetAEAD(kmsKey) | ||
| default: | ||
| return nil, fmt.Errorf("unsupported KMS key type for key %s", kmsKey) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright 2025 Google LLC. All Rights Reserved. | ||
| // | ||
| // 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 tink | ||
|
|
||
| import ( | ||
| "crypto" | ||
| "crypto/ecdsa" | ||
| "crypto/ed25519" | ||
| "crypto/elliptic" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/tink-crypto/tink-go/v2/insecuresecretdataaccess" | ||
| "github.com/tink-crypto/tink-go/v2/keyset" | ||
| tinkecdsa "github.com/tink-crypto/tink-go/v2/signature/ecdsa" | ||
| tinked25519 "github.com/tink-crypto/tink-go/v2/signature/ed25519" | ||
| ) | ||
|
|
||
| func curveFromTinkECDSACurveType(curveType tinkecdsa.CurveType) (elliptic.Curve, error) { | ||
| switch curveType { | ||
| case tinkecdsa.NistP256: | ||
| return elliptic.P256(), nil | ||
| case tinkecdsa.NistP384: | ||
| return elliptic.P384(), nil | ||
| case tinkecdsa.NistP521: | ||
| return elliptic.P521(), nil | ||
| default: | ||
| // Should never happen. | ||
| return nil, fmt.Errorf("unsupported curve: %v", curveType) | ||
| } | ||
| } | ||
|
|
||
| // KeyHandleToSigner constructs a [crypto.Signer] from a Tink [keyset.Handle]'s | ||
| // primary key. | ||
| // | ||
| // NOTE: Tink validates keys on [keyset.Handle] creation. | ||
| func KeyHandleToSigner(kh *keyset.Handle) (crypto.Signer, error) { | ||
| primary, err := kh.Primary() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| switch privateKey := primary.Key().(type) { | ||
| case *tinkecdsa.PrivateKey: | ||
| publicKey, err := privateKey.PublicKey() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| ecdsaPublicKey, ok := publicKey.(*tinkecdsa.PublicKey) | ||
| if !ok { | ||
| return nil, fmt.Errorf("error asserting ecdsa public key") | ||
| } | ||
|
|
||
| curveParams, ok := ecdsaPublicKey.Parameters().(*tinkecdsa.Parameters) | ||
| if !ok { | ||
| return nil, fmt.Errorf("error asserting ecdsa parameters") | ||
| } | ||
| curve, err := curveFromTinkECDSACurveType(curveParams.CurveType()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Encoded as: 0x04 || X || Y. | ||
| // See https://github.com/tink-crypto/tink-go/blob/v2.3.0/signature/ecdsa/key.go#L335 | ||
| publicPoint := ecdsaPublicKey.PublicPoint() | ||
| xy := publicPoint[1:] | ||
| pk := new(ecdsa.PrivateKey) | ||
| pk.Curve = curve | ||
| pk.X = new(big.Int).SetBytes(xy[:len(xy)/2]) | ||
| pk.Y = new(big.Int).SetBytes(xy[len(xy)/2:]) | ||
| pk.D = new(big.Int).SetBytes(privateKey.PrivateKeyValue().Data(insecuresecretdataaccess.Token{})) | ||
| return pk, err | ||
| case *tinked25519.PrivateKey: | ||
| return ed25519.NewKeyFromSeed(privateKey.PrivateKeyBytes().Data(insecuresecretdataaccess.Token{})), err | ||
| default: | ||
| return nil, fmt.Errorf("unsupported key type: %T", primary.Key()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid opening the objets and doing curve operations, do you think that it would be possible to wrap an object around the
keyset.Handle, which itself would implementcrypto.Signer, and calls Tink's methods directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mentioned this in #282 (comment), the issue is that
keyset.Handletakes in the pre-image whilecrypto.Signertakes in the digest. I'd like to simplify this, but we'd need Tink to implement a prehashed signer.