Skip to content
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

Use recommended crypto library to create ECC seed #480

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ The OIDC token includes claims regarding the GCE VM, which is verified by Attest
}

cloudLogger = cloudLogClient.Logger(toolName)
fmt.Fprintf(debugOutput(), "cloudLogger created for project: "+projectID+"\n")
fmt.Fprint(debugOutput(), "cloudLogger created for project: "+projectID+"\n")
}

key = "gceAK"
Expand Down Expand Up @@ -175,7 +175,7 @@ The OIDC token includes claims regarding the GCE VM, which is verified by Attest
}

if output == "" {
fmt.Fprintf(messageOutput(), string(token)+"\n")
fmt.Fprint(messageOutput(), string(token)+"\n")
} else {
out := []byte(token)
if _, err := dataOutput().Write(out); err != nil {
Expand All @@ -194,7 +194,7 @@ The OIDC token includes claims regarding the GCE VM, which is verified by Attest
}
}

fmt.Fprintf(debugOutput(), string(claimsString)+"\n"+"Note: these Claims are for debugging purpose and not verified"+"\n")
fmt.Fprint(debugOutput(), string(claimsString)+"\n"+"Note: these Claims are for debugging purpose and not verified"+"\n")

return nil
},
Expand Down
37 changes: 29 additions & 8 deletions server/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
"crypto/ecdsa"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"fmt"
"hash"
"io"
"math/big"

"github.com/google/go-tpm/legacy/tpm2"
"github.com/google/go-tpm/tpmutil"
Expand Down Expand Up @@ -131,25 +132,45 @@ func createECCSeed(ek tpm2.Public) (seed, encryptedSeed []byte, err error) {
if err != nil {
return nil, nil, err
}
priv, x, y, err := elliptic.GenerateKey(curve, rand.Reader)

ecdsaPriv, err := ecdsa.GenerateKey(curve, rand.Reader)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're doing ecdh here so you should use the utilities in crypto/ecdh instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need EC points {x, y} to compute ECC seed in the following operations. Using crypto/ecdh utilities is an ideal way, however they don't provide a method to expose the EC points {x, y} in public key.

crypto/ecdsa provides a method to do that, also there are methods which convert each key type to their counterparts in the the ecdh package. See line 141 that converts an ecdsa private key to its corresponding ecdh private key.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. LGTM

if err != nil {
return nil, nil, err
}

ecdhPriv, err := ecdsaPriv.ECDH()
if err != nil {
return nil, nil, err
}

pub, err := ek.Key()
if err != nil {
return nil, nil, err
}
ekPoint := ek.ECCParameters.Point
z, _ := curve.ScalarMult(ekPoint.X(), ekPoint.Y(), priv)
xBytes := eccIntToBytes(curve, x)

ekPub, err := pub.(*ecdsa.PublicKey).ECDH()
if err != nil {
return nil, nil, err
}

zBytes, err := ecdhPriv.ECDH(ekPub)
if err != nil {
return nil, nil, err
}

xBytes := eccIntToBytes(curve, ecdsaPriv.X)

seed, err = tpm2.KDFe(
ek.NameAlg,
eccIntToBytes(curve, z),
eccIntToBytes(curve, new(big.Int).SetBytes(zBytes)),
"DUPLICATE",
xBytes,
eccIntToBytes(curve, ekPoint.X()),
eccIntToBytes(curve, ek.ECCParameters.Point.X()),
getHash(ek.NameAlg).Size()*8)
if err != nil {
return nil, nil, err
}
encryptedSeed, err = tpmutil.Pack(tpmutil.U16Bytes(xBytes), tpmutil.U16Bytes(eccIntToBytes(curve, y)))
encryptedSeed, err = tpmutil.Pack(tpmutil.U16Bytes(xBytes), tpmutil.U16Bytes(eccIntToBytes(curve, ecdsaPriv.Y)))
return seed, encryptedSeed, err
}

Expand Down
1 change: 0 additions & 1 deletion server/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestImport(t *testing.T) {
{"ECC", client.DefaultEKTemplateECC()},
{"SRK-RSA", client.SRKTemplateRSA()},
{"SRK-ECC", client.SRKTemplateECC()},
{"ECC-P224", getECCTemplate(tpm2.CurveNISTP224)},
{"ECC-P256", getECCTemplate(tpm2.CurveNISTP256)},
{"ECC-P384", getECCTemplate(tpm2.CurveNISTP384)},
{"ECC-P521", getECCTemplate(tpm2.CurveNISTP521)},
Expand Down
Loading