| 1 |
package tpm2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdh" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
) |
| 9 |
|
| 10 |
var ( |
| 11 |
// The curve is not supported. |
| 12 |
ErrUnsupportedCurve = errors.New("unsupported curve") |
| 13 |
// There was an internal error parsing the ephemeral public key during encapsulation. |
| 14 |
ErrBadEphemeralKey = errors.New("bad ephemeral ECC key") |
| 15 |
) |
| 16 |
|
| 17 |
// An eccKey is an One-Pass-Diffie-Hellman-based Labeled Encapsulation key. |
| 18 |
type eccKey struct { |
| 19 |
// The actual public key. |
| 20 |
eccPub *ecdh.PublicKey |
| 21 |
// The name algorithm of the key. |
| 22 |
nameAlg TPMIAlgHash |
| 23 |
// The symmetric parameters of the key. |
| 24 |
symParms *TPMTSymDefObject |
| 25 |
} |
| 26 |
|
| 27 |
// importECCEncapsulationKey imports an ECC key for use in labeled encapsulation. |
| 28 |
func importECCEncapsulationKey(pub *TPMTPublic) (*eccKey, error) { |
| 29 |
eccParms, err := pub.Parameters.ECCDetail() |
| 30 |
if err != nil { |
| 31 |
return nil, err |
| 32 |
} |
| 33 |
eccPub, err := pub.Unique.ECC() |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
ecdhPub, err := ECDHPub(eccParms, eccPub) |
| 38 |
if err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
|
| 42 |
return &eccKey{ |
| 43 |
eccPub: ecdhPub, |
| 44 |
nameAlg: pub.NameAlg, |
| 45 |
symParms: &eccParms.Symmetric, |
| 46 |
}, nil |
| 47 |
} |
| 48 |
|
| 49 |
// getXY gets the big-endian X/Y coordinates as full-length buffers. |
| 50 |
func getXY(pub *ecdh.PublicKey) ([]byte, []byte, error) { |
| 51 |
// Check and strip the leading 0x04 byte, which indicates an uncompressed ECC point. |
| 52 |
rawPub := pub.Bytes() |
| 53 |
if len(rawPub) == 0 || rawPub[0] != 0x04 { |
| 54 |
return nil, nil, fmt.Errorf("%w: could not decode %x as an uncompressed point", ErrBadEphemeralKey, rawPub) |
| 55 |
} |
| 56 |
rawPub = rawPub[1:] |
| 57 |
return rawPub[:len(rawPub)/2], rawPub[len(rawPub)/2:], nil |
| 58 |
} |
| 59 |
|
| 60 |
// Encapsulate implements LabeledEncapsulationKey. |
| 61 |
func (pub *eccKey) Encapsulate(random io.Reader, label string) (secret []byte, ciphertext []byte, err error) { |
| 62 |
ephemeralPriv, err := pub.eccPub.Curve().GenerateKey(random) |
| 63 |
if err != nil { |
| 64 |
return nil, nil, err |
| 65 |
} |
| 66 |
return pub.encapsulateDerandomized(ephemeralPriv, label) |
| 67 |
} |
| 68 |
|
| 69 |
// encapsulateDerandomized is a derandomized internal version of Encapsulate for testing. |
| 70 |
func (pub *eccKey) encapsulateDerandomized(ephPrivate *ecdh.PrivateKey, label string) (secret []byte, ciphertext []byte, err error) { |
| 71 |
nameHash, err := pub.nameAlg.Hash() |
| 72 |
if err != nil { |
| 73 |
return nil, nil, err |
| 74 |
} |
| 75 |
pubX, _, err := getXY(pub.eccPub) |
| 76 |
if err != nil { |
| 77 |
return nil, nil, err |
| 78 |
} |
| 79 |
ephX, ephY, err := getXY(ephPrivate.PublicKey()) |
| 80 |
if err != nil { |
| 81 |
return nil, nil, err |
| 82 |
} |
| 83 |
z, err := ephPrivate.ECDH(pub.eccPub) |
| 84 |
if err != nil { |
| 85 |
return nil, nil, err |
| 86 |
} |
| 87 |
secret = KDFe(nameHash, z, label, ephX, pubX, nameHash.Size()*8) |
| 88 |
ciphertext = Marshal(TPMSECCPoint{ |
| 89 |
X: TPM2BECCParameter{ |
| 90 |
Buffer: ephX, |
| 91 |
}, |
| 92 |
Y: TPM2BECCParameter{ |
| 93 |
Buffer: ephY, |
| 94 |
}, |
| 95 |
}) |
| 96 |
return secret, ciphertext, nil |
| 97 |
} |
| 98 |
|
| 99 |
// NameAlg implements LabeledEncapsulationKey. |
| 100 |
func (pub *eccKey) NameAlg() TPMAlgID { |
| 101 |
return pub.nameAlg |
| 102 |
} |
| 103 |
|
| 104 |
// SymmetricParameters implements LabeledEncapsulationkey. |
| 105 |
func (pub *eccKey) SymmetricParameters() *TPMTSymDefObject { |
| 106 |
return pub.symParms |
| 107 |
} |