-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new algorithms supported in firmware 5.7.x
This commit supports the new algorithms supported on YubiKeys with a 5.7.x firmware. It adds support for RSA-3072, RSA-4096, Ed25519, and X25519. Generating or importing X25519 keys is only supported with Go 1.20+, which adds support for the crypto/ecdh package.
- Loading branch information
1 parent
7988525
commit 33d0d46
Showing
8 changed files
with
565 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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,116 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
// 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 | ||
// | ||
// https://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 piv | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdh" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type X25519PrivateKey struct { | ||
yk *YubiKey | ||
slot Slot | ||
pub *ecdh.PublicKey | ||
auth KeyAuth | ||
pp PINPolicy | ||
} | ||
|
||
func (k *X25519PrivateKey) Public() crypto.PublicKey { | ||
return k.pub | ||
} | ||
|
||
// SharedKey performs an ECDH exchange and returns the shared secret. | ||
// | ||
// Peer's public key must use the same algorithm as the key in this slot, or an | ||
// error will be returned. | ||
func (k *X25519PrivateKey) SharedKey(peer *ecdh.PublicKey) ([]byte, error) { | ||
return k.auth.do(k.yk, k.pp, func(tx *scTx) ([]byte, error) { | ||
return ykECDHX25519(tx, k.slot, k.pub, peer) | ||
}) | ||
} | ||
|
||
func (yk *YubiKey) privateKey(slot Slot, public crypto.PublicKey, auth KeyAuth, pp PINPolicy) (crypto.PrivateKey, error) { | ||
switch pub := public.(type) { | ||
case *ecdh.PublicKey: | ||
if crv := pub.Curve(); crv != ecdh.X25519() { | ||
return nil, fmt.Errorf("unsupported ecdh curve: %v", crv) | ||
} | ||
return &X25519PrivateKey{yk, slot, pub, auth, pp}, nil | ||
default: | ||
return nil, fmt.Errorf("unsupported public key type: %T", public) | ||
} | ||
} | ||
|
||
func (yk *YubiKey) setPrivateKeyInsecure(private crypto.PrivateKey) ([][]byte, byte, int, error) { | ||
switch priv := private.(type) { | ||
case *ecdh.PrivateKey: | ||
if priv.Curve() != ecdh.X25519() { | ||
return nil, 0, 0, errors.New("unsupported private key type") | ||
} | ||
// seed | ||
params := make([][]byte, 0) | ||
params = append(params, priv.Bytes()) | ||
return params, 0x08, 32, nil | ||
default: | ||
return nil, 0, 0, errors.New("unsupported private key type") | ||
} | ||
} | ||
|
||
func decodeX25519Public(b []byte) (*ecdh.PublicKey, error) { | ||
// Adaptation of | ||
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=95 | ||
p, _, err := unmarshalASN1(b, 2, 0x06) | ||
if err != nil { | ||
return nil, fmt.Errorf("unmarshal points: %v", err) | ||
} | ||
return ecdh.X25519().NewPublicKey(p) | ||
} | ||
|
||
func ykECDHX25519(tx *scTx, slot Slot, pub *ecdh.PublicKey, peer *ecdh.PublicKey) ([]byte, error) { | ||
if crv := pub.Curve(); crv != ecdh.X25519() { | ||
return nil, fmt.Errorf("unsupported ecdh curve: %v", crv) | ||
} | ||
if pub.Curve() != peer.Curve() { | ||
return nil, errMismatchingAlgorithms | ||
} | ||
cmd := apdu{ | ||
instruction: insAuthenticate, | ||
param1: algX25519, | ||
param2: byte(slot.Key), | ||
data: marshalASN1(0x7c, | ||
append([]byte{0x82, 0x00}, | ||
marshalASN1(0x85, peer.Bytes())...)), | ||
} | ||
resp, err := tx.Transmit(cmd) | ||
if err != nil { | ||
return nil, fmt.Errorf("command failed: %w", err) | ||
} | ||
|
||
sig, _, err := unmarshalASN1(resp, 1, 0x1c) // 0x7c | ||
if err != nil { | ||
return nil, fmt.Errorf("unmarshal response: %v", err) | ||
} | ||
sharedSecret, _, err := unmarshalASN1(sig, 2, 0x02) // 0x82 | ||
if err != nil { | ||
return nil, fmt.Errorf("unmarshal response signature: %v", err) | ||
} | ||
|
||
return sharedSecret, nil | ||
} |
This file contains 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,120 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
// 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 | ||
// | ||
// https://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 piv | ||
|
||
import ( | ||
"bytes" | ||
"crypto/ecdh" | ||
"crypto/rand" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestYubiKeyX25519ImportKey(t *testing.T) { | ||
importKey, err := ecdh.X25519().GenerateKey(rand.Reader) | ||
if err != nil { | ||
t.Fatalf("error geneating X25519 key: %v", err) | ||
} | ||
|
||
yk, close := newTestYubiKey(t) | ||
defer close() | ||
|
||
slot := SlotAuthentication | ||
|
||
err = yk.SetPrivateKeyInsecure(DefaultManagementKey, slot, importKey, Key{AlgorithmX25519, PINPolicyNever, TouchPolicyNever}) | ||
if err != nil { | ||
t.Fatalf("error importing key: %v", err) | ||
} | ||
want := KeyInfo{ | ||
Algorithm: AlgorithmX25519, | ||
PINPolicy: PINPolicyNever, | ||
TouchPolicy: TouchPolicyNever, | ||
Origin: OriginImported, | ||
PublicKey: importKey.Public(), | ||
} | ||
|
||
got, err := yk.KeyInfo(slot) | ||
if err != nil { | ||
t.Fatalf("KeyInfo() = _, %v", err) | ||
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("KeyInfo() = %#v, want %#v", got, want) | ||
} | ||
} | ||
|
||
func TestYubiKeyX25519SharedKey(t *testing.T) { | ||
yk, close := newTestYubiKey(t) | ||
defer close() | ||
|
||
slot := SlotAuthentication | ||
|
||
key := Key{ | ||
Algorithm: AlgorithmX25519, | ||
TouchPolicy: TouchPolicyNever, | ||
PINPolicy: PINPolicyNever, | ||
} | ||
pubKey, err := yk.GenerateKey(DefaultManagementKey, slot, key) | ||
if err != nil { | ||
t.Fatalf("generating key: %v", err) | ||
} | ||
pub, ok := pubKey.(*ecdh.PublicKey) | ||
if !ok { | ||
t.Fatalf("public key is not an ecdh key") | ||
} | ||
priv, err := yk.PrivateKey(slot, pub, KeyAuth{}) | ||
if err != nil { | ||
t.Fatalf("getting private key: %v", err) | ||
} | ||
privX25519, ok := priv.(*X25519PrivateKey) | ||
if !ok { | ||
t.Fatalf("expected private key to be X25519 private key") | ||
} | ||
|
||
t.Run("good", func(t *testing.T) { | ||
peer, err := ecdh.X25519().GenerateKey(rand.Reader) | ||
if err != nil { | ||
t.Fatalf("cannot generate key: %v", err) | ||
} | ||
|
||
secret1, err := privX25519.SharedKey(peer.PublicKey()) | ||
if err != nil { | ||
t.Fatalf("key agreement failed: %v", err) | ||
} | ||
secret2, err := peer.ECDH(pub) | ||
if err != nil { | ||
t.Fatalf("key agreement failed: %v", err) | ||
} | ||
if !bytes.Equal(secret1, secret2) { | ||
t.Errorf("key agreement didn't match") | ||
} | ||
}) | ||
|
||
t.Run("bad", func(t *testing.T) { | ||
t.Run("curve", func(t *testing.T) { | ||
peer, err := ecdh.P256().GenerateKey(rand.Reader) | ||
if err != nil { | ||
t.Fatalf("cannot generate key: %v", err) | ||
} | ||
_, err = privX25519.SharedKey(peer.PublicKey()) | ||
if !errors.Is(err, errMismatchingAlgorithms) { | ||
t.Fatalf("unexpected error value: wanted errMismatchingAlgorithms: %v", err) | ||
} | ||
}) | ||
}) | ||
} |
This file contains 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,36 @@ | ||
//go:build !go1.20 | ||
// +build !go1.20 | ||
|
||
// 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 | ||
// | ||
// https://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 piv | ||
|
||
import ( | ||
"crypto" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
func (yk *YubiKey) privateKey(slot Slot, public crypto.PublicKey, auth KeyAuth, pp PINPolicy) (crypto.PrivateKey, error) { | ||
return nil, fmt.Errorf("unsupported public key type: %T", public) | ||
} | ||
|
||
func (yk *YubiKey) setPrivateKeyInsecure(private crypto.PrivateKey) ([][]byte, byte, int, error) { | ||
return nil, 0, 0, errors.New("unsupported private key type") | ||
} | ||
|
||
func decodeX25519Public(b []byte) (crypto.PublicKey, error) { | ||
return nil, fmt.Errorf("unsupported algorithm") | ||
} |
Oops, something went wrong.