| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/rand" |
| 7 |
"crypto/rsa" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
|
| 11 |
"github.com/google/go-tpm-tools/client" |
| 12 |
"github.com/google/go-tpm/legacy/tpm2" |
| 13 |
) |
| 14 |
|
| 15 |
var defaultNameAlg = client.DefaultEKTemplateRSA().NameAlg |
| 16 |
|
| 17 |
// CreateEKPublicAreaFromKey creates a public area from a go interface PublicKey. |
| 18 |
// Supports RSA and ECC keys. |
| 19 |
func CreateEKPublicAreaFromKey(k crypto.PublicKey) (tpm2.Public, error) { |
| 20 |
switch key := k.(type) { |
| 21 |
case *rsa.PublicKey: |
| 22 |
return createEKPublicRSA(key) |
| 23 |
case *ecdsa.PublicKey: |
| 24 |
return createEKPublicECC(key) |
| 25 |
default: |
| 26 |
return tpm2.Public{}, fmt.Errorf("unsupported public key type: %T", k) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func createEKPublicRSA(rsaKey *rsa.PublicKey) (tpm2.Public, error) { |
| 31 |
public := client.DefaultEKTemplateRSA() |
| 32 |
if rsaKey.N.BitLen() != int(public.RSAParameters.KeyBits) { |
| 33 |
return tpm2.Public{}, fmt.Errorf("unexpected RSA modulus size: %d bits", rsaKey.N.BitLen()) |
| 34 |
} |
| 35 |
if rsaKey.E != int(public.RSAParameters.Exponent()) { |
| 36 |
return tpm2.Public{}, fmt.Errorf("unexpected RSA exponent: %d", rsaKey.E) |
| 37 |
} |
| 38 |
public.RSAParameters.ModulusRaw = rsaKey.N.Bytes() |
| 39 |
return public, nil |
| 40 |
} |
| 41 |
|
| 42 |
func createEKPublicECC(eccKey *ecdsa.PublicKey) (public tpm2.Public, err error) { |
| 43 |
public = client.DefaultEKTemplateECC() |
| 44 |
public.ECCParameters.Point = tpm2.ECPoint{ |
| 45 |
XRaw: eccIntToBytes(eccKey.Curve, eccKey.X), |
| 46 |
YRaw: eccIntToBytes(eccKey.Curve, eccKey.Y), |
| 47 |
} |
| 48 |
public.ECCParameters.CurveID, err = goCurveToCurveID(eccKey.Curve) |
| 49 |
return public, err |
| 50 |
} |
| 51 |
|
| 52 |
func createPublic(private tpm2.Private) tpm2.Public { |
| 53 |
publicHash := getHash(defaultNameAlg) |
| 54 |
publicHash.Write(private.SeedValue) |
| 55 |
publicHash.Write(private.Sensitive) |
| 56 |
return tpm2.Public{ |
| 57 |
Type: tpm2.AlgKeyedHash, |
| 58 |
NameAlg: defaultNameAlg, |
| 59 |
KeyedHashParameters: &tpm2.KeyedHashParams{ |
| 60 |
Alg: tpm2.AlgNull, |
| 61 |
Unique: publicHash.Sum(nil), |
| 62 |
}, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func createPrivate(sensitive []byte) tpm2.Private { |
| 67 |
private := tpm2.Private{ |
| 68 |
Type: tpm2.AlgKeyedHash, |
| 69 |
AuthValue: nil, |
| 70 |
SeedValue: make([]byte, getHash(defaultNameAlg).Size()), |
| 71 |
Sensitive: sensitive, |
| 72 |
} |
| 73 |
if _, err := io.ReadFull(rand.Reader, private.SeedValue); err != nil { |
| 74 |
panic(err) |
| 75 |
} |
| 76 |
return private |
| 77 |
} |
| 78 |
|
| 79 |
func createPublicPrivateSign(signingKey crypto.PrivateKey) (tpm2.Public, tpm2.Private, error) { |
| 80 |
rsaPriv, ok := signingKey.(*rsa.PrivateKey) |
| 81 |
if !ok { |
| 82 |
return tpm2.Public{}, tpm2.Private{}, fmt.Errorf("unsupported signing key type: %T", signingKey) |
| 83 |
} |
| 84 |
|
| 85 |
rsaPub := rsaPriv.PublicKey |
| 86 |
public := tpm2.Public{ |
| 87 |
Type: tpm2.AlgRSA, |
| 88 |
NameAlg: defaultNameAlg, |
| 89 |
Attributes: tpm2.FlagSign, |
| 90 |
RSAParameters: &tpm2.RSAParams{ |
| 91 |
KeyBits: uint16(rsaPub.N.BitLen()), |
| 92 |
ExponentRaw: uint32(rsaPub.E), |
| 93 |
ModulusRaw: rsaPub.N.Bytes(), |
| 94 |
Sign: &tpm2.SigScheme{ |
| 95 |
Alg: tpm2.AlgRSASSA, |
| 96 |
Hash: tpm2.AlgSHA256, |
| 97 |
}, |
| 98 |
}, |
| 99 |
} |
| 100 |
private := tpm2.Private{ |
| 101 |
Type: tpm2.AlgRSA, |
| 102 |
AuthValue: nil, |
| 103 |
SeedValue: nil, // Only Storage Keys need a seed value. See part 3 TPM2_CREATE b.3. |
| 104 |
Sensitive: rsaPriv.Primes[0].Bytes(), |
| 105 |
} |
| 106 |
|
| 107 |
return public, private, nil |
| 108 |
} |