Skip to content

Commit 6b2397c

Browse files
authored
Bump go to 1.22 and golangci-lint to 1.56.2 (#353)
* Bump go to 1.22 and golangci-lint to 1.56.2 * Use single assignment of ECC curve in test
1 parent 08987ce commit 6b2397c

File tree

10 files changed

+136
-56
lines changed

10 files changed

+136
-56
lines changed

.cirrus.yml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,4 @@ lint_task:
1818
- GOOS: linux
1919
- GOOS: windows
2020
lint_script:
21-
golangci-lint run
22-
-D errcheck
23-
-E stylecheck
24-
-E goimports
25-
-E misspell
26-
-E revive
27-
--exclude-use-default=false
28-
--exclude stutters
29-
--exclude underscores
30-
--exclude unexported-return
31-
--max-same-issues=0
32-
--max-issues-per-linter=0
33-
./tpmutil/...
34-
./tpm2/...
21+
golangci-lint run ./tpmutil/... ./tpm2/...

.golangci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
linters:
2+
disable:
3+
- errcheck
4+
enable:
5+
- stylecheck
6+
- goimports
7+
- misspell
8+
- revive
9+
linters-settings:
10+
revive:
11+
rules:
12+
- name: dot-imports
13+
disabled: true
14+
issues:
15+
exclude-use-default: false
16+
exclude:
17+
- stutters
18+
- underscores
19+
- unexported-return
20+
max-issues-per-linter: 0
21+
max-same-issues: 0

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
FROM golang:1.21
1+
FROM golang:1.22
22
# We need OpenSSL headers to build the simulator
33
RUN apt-get update && apt-get install -y \
44
libssl-dev \
55
&& rm -rf /var/lib/apt/lists/*
66
# We need golangci-lint for linting
7-
ARG VERSION=1.52.2
7+
ARG VERSION=1.56.2
88
RUN curl -SL \
99
https://github.com/golangci/golangci-lint/releases/download/v${VERSION}/golangci-lint-${VERSION}-linux-amd64.tar.gz \
1010
--output golangci.tar.gz \

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/google/go-tpm
22

3-
go 1.20
3+
go 1.22
44

55
require (
66
github.com/google/go-cmp v0.5.9

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
22
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
33
github.com/google/go-sev-guest v0.6.1 h1:NajHkAaLqN9/aW7bCFSUplUMtDgk2+HcN7jC2btFtk0=
4+
github.com/google/go-sev-guest v0.6.1/go.mod h1:UEi9uwoPbLdKGl1QHaq1G8pfCbQ4QP0swWX4J0k6r+Q=
45
github.com/google/go-tpm-tools v0.3.13-0.20230620182252-4639ecce2aba h1:qJEJcuLzH5KDR0gKc0zcktin6KSAwL7+jWKBYceddTc=
56
github.com/google/go-tpm-tools v0.3.13-0.20230620182252-4639ecce2aba/go.mod h1:EFYHy8/1y2KfgTAsx7Luu7NGhoxtuVHnNo8jE7FikKc=
67
github.com/google/logger v1.1.1 h1:+6Z2geNxc9G+4D4oDO9njjjn2d0wN5d7uOo0vOIW1NQ=
8+
github.com/google/logger v1.1.1/go.mod h1:BkeJZ+1FhQ+/d087r4dzojEg1u2ZX+ZqG1jTUrLM+zQ=
79
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
10+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
811
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
12+
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
913
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
14+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1015
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
16+
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
1117
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
1218
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1319
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
20+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

tpm2/crypto.go

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package tpm2
22

33
import (
4+
"crypto/ecdh"
5+
"crypto/ecdsa"
46
"crypto/elliptic"
57
"crypto/rsa"
8+
"fmt"
69
"math/big"
710
)
811

@@ -21,23 +24,53 @@ func RSAPub(parms *TPMSRSAParms, pub *TPM2BPublicKeyRSA) (*rsa.PublicKey, error)
2124
return &result, nil
2225
}
2326

24-
// ECDHPub is a convenience wrapper around the necessary info to perform point
25-
// multiplication with the elliptic package.
26-
type ECDHPub struct {
27-
Curve elliptic.Curve
28-
X, Y *big.Int
29-
}
27+
// ECDHPubKey converts a TPM ECC public key into one recognized by the ecdh package
28+
func ECDHPubKey(curve ecdh.Curve, pub *TPMSECCPoint) (*ecdh.PublicKey, error) {
3029

31-
// ECCPub converts a TPM ECC public key into one recognized by the elliptic
32-
// package's point-multiplication functions, for use in ECDH.
33-
func ECCPub(parms *TPMSECCParms, pub *TPMSECCPoint) (*ECDHPub, error) {
34-
curve, err := parms.CurveID.Curve()
35-
if err != nil {
36-
return nil, err
30+
var c elliptic.Curve
31+
switch curve {
32+
case ecdh.P256():
33+
c = elliptic.P256()
34+
case ecdh.P384():
35+
c = elliptic.P384()
36+
case ecdh.P521():
37+
c = elliptic.P521()
38+
default:
39+
return nil, fmt.Errorf("unknown curve: %v", curve)
3740
}
38-
return &ECDHPub{
39-
Curve: curve,
41+
42+
pubKey := ecdsa.PublicKey{
43+
Curve: c,
4044
X: big.NewInt(0).SetBytes(pub.X.Buffer),
4145
Y: big.NewInt(0).SetBytes(pub.Y.Buffer),
42-
}, nil
46+
}
47+
48+
return pubKey.ECDH()
49+
}
50+
51+
// ECCPoint returns an uncompressed ECC Point
52+
func ECCPoint(pubKey *ecdh.PublicKey) (*big.Int, *big.Int, error) {
53+
b := pubKey.Bytes()
54+
size, err := elementLength(pubKey.Curve())
55+
if err != nil {
56+
return nil, nil, fmt.Errorf("ECCPoint: %w", err)
57+
}
58+
return big.NewInt(0).SetBytes(b[1 : size+1]),
59+
big.NewInt(0).SetBytes(b[size+1:]), nil
60+
}
61+
62+
func elementLength(c ecdh.Curve) (int, error) {
63+
switch c {
64+
case ecdh.P256():
65+
// crypto/internal/nistec/fiat.p256ElementLen
66+
return 32, nil
67+
case ecdh.P384():
68+
// crypto/internal/nistec/fiat.p384ElementLen
69+
return 48, nil
70+
case ecdh.P521():
71+
// crypto/internal/nistec/fiat.p521ElementLen
72+
return 66, nil
73+
default:
74+
return 0, fmt.Errorf("unknown element length for curve: %v", c)
75+
}
4376
}

tpm2/reflect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,9 @@ func marshalParameter[R any](buf *bytes.Buffer, cmd Command[R, *R], i int) error
839839
return marshal(buf, reflect.ValueOf(TPMRHNull))
840840
} else if parm.IsZero() && parm.Kind() == reflect.Uint16 && hasTag(field, "nullable") {
841841
return marshal(buf, reflect.ValueOf(TPMAlgNull))
842-
} else {
843-
return marshal(buf, parm)
844842
}
843+
844+
return marshal(buf, parm)
845845
}
846846

847847
// cmdParameters returns the parameters area of the command.

tpm2/sessions.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"crypto/aes"
66
"crypto/cipher"
7-
"crypto/elliptic"
87
"crypto/hmac"
98
"crypto/rand"
109
"crypto/rsa"
@@ -425,24 +424,31 @@ func getEncryptedSaltRSA(nameAlg TPMIAlgHash, parms *TPMSRSAParms, pub *TPM2BPub
425424

426425
// Part 1, 19.6.13
427426
func getEncryptedSaltECC(nameAlg TPMIAlgHash, parms *TPMSECCParms, pub *TPMSECCPoint) (*TPM2BEncryptedSecret, []byte, error) {
428-
curve, err := parms.CurveID.Curve()
427+
curve, err := parms.CurveID.ECDHCurve()
429428
if err != nil {
430-
return nil, nil, fmt.Errorf("could not encrypt salt to ECC key: %w", err)
429+
return nil, nil, fmt.Errorf("ecc salt: param curve: %w", err)
431430
}
432-
eccPub, err := ECCPub(parms, pub)
431+
eccPub, err := ECDHPubKey(curve, pub)
433432
if err != nil {
434-
return nil, nil, fmt.Errorf("could not encrypt salt to ECC key: %w", err)
433+
return nil, nil, fmt.Errorf("ecc salt: unmarshalling tpm ecc key: %w", err)
435434
}
436-
ephPriv, ephPubX, ephPubY, err := elliptic.GenerateKey(curve, rand.Reader)
435+
436+
// Generate new ECDH key
437+
ephPriv, err := curve.GenerateKey(rand.Reader)
438+
if err != nil {
439+
return nil, nil, fmt.Errorf("ecc salt: generating ecc private key: %w", err)
440+
}
441+
ephPubX, ephPubY, err := ECCPoint(ephPriv.PublicKey())
442+
if err != nil {
443+
return nil, nil, fmt.Errorf("ecc salt: ecc pubkey: %w", err)
444+
}
445+
446+
// Calculate Z (ECDH key * TPM pub)
447+
z, err := ephPriv.ECDH(eccPub)
437448
if err != nil {
438-
return nil, nil, fmt.Errorf("could not encrypt salt to ECC key: %w", err)
439-
}
440-
zx, _ := curve.Params().ScalarMult(eccPub.X, eccPub.Y, ephPriv)
441-
// ScalarMult returns a big.Int, whose Bytes() function may return the
442-
// compacted form. In our case, we want to left-pad zx to the size of
443-
// the curve.
444-
z := make([]byte, (curve.Params().BitSize+7)/8)
445-
zx.FillBytes(z)
449+
return nil, nil, fmt.Errorf("ecc salt: z calc: %w", err)
450+
}
451+
446452
ha, err := nameAlg.Hash()
447453
if err != nil {
448454
return nil, nil, err

tpm2/structures.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package tpm2
44
import (
55
"bytes"
66
"crypto"
7+
"crypto/ecdh"
78
"crypto/elliptic"
89
"encoding/binary"
910
"reflect"
@@ -96,6 +97,20 @@ func (c TPMECCCurve) Curve() (elliptic.Curve, error) {
9697
}
9798
}
9899

100+
// ECDHCurve returns the ecdh.Curve associated with a TPMECCCurve.
101+
func (c TPMECCCurve) ECDHCurve() (ecdh.Curve, error) {
102+
switch c {
103+
case TPMECCNistP256:
104+
return ecdh.P256(), nil
105+
case TPMECCNistP384:
106+
return ecdh.P384(), nil
107+
case TPMECCNistP521:
108+
return ecdh.P521(), nil
109+
default:
110+
return nil, fmt.Errorf("unsupported ECC curve: %v", c)
111+
}
112+
}
113+
99114
// HandleValue returns the handle value. This behavior is intended to satisfy
100115
// an interface that can be implemented by other, more complex types as well.
101116
func (h TPMHandle) HandleValue() uint32 {

tpm2/test/ecdh_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package tpm2test
22

33
import (
4-
"crypto/elliptic"
4+
"crypto/ecdh"
55
"crypto/rand"
6-
"math/big"
76
"testing"
87

98
"github.com/google/go-cmp/cmp"
@@ -57,6 +56,9 @@ func TestECDH(t *testing.T) {
5756
}),
5857
}
5958

59+
// Use NIST P-256
60+
curve := ecdh.P256()
61+
6062
tpmCreateRsp, err := tpmCreate.Execute(thetpm)
6163
if err != nil {
6264
t.Fatalf("could not create the TPM key: %v", err)
@@ -69,24 +71,33 @@ func TestECDH(t *testing.T) {
6971
if err != nil {
7072
t.Fatalf("%v", err)
7173
}
72-
tpmX := big.NewInt(0).SetBytes(tpmPub.X.Buffer)
73-
tpmY := big.NewInt(0).SetBytes(tpmPub.Y.Buffer)
74+
tpmPubKey, err := ECDHPubKey(curve, tpmPub)
75+
if err != nil {
76+
t.Fatalf("could not unmarshall pubkey: %v", err)
77+
}
7478

7579
// Create a SW ECDH key
76-
priv, x, y, err := elliptic.GenerateKey(elliptic.P256(), rand.Reader)
80+
swPriv, err := curve.GenerateKey(rand.Reader)
7781
if err != nil {
7882
t.Fatalf("could not create the SW key: %v", err)
7983
}
84+
x, y, err := ECCPoint(swPriv.PublicKey())
85+
if err != nil {
86+
t.Fatalf("could not get SW key point: %v", err)
87+
}
8088
swPub := TPMSECCPoint{
8189
X: TPM2BECCParameter{Buffer: x.FillBytes(make([]byte, 32))},
8290
Y: TPM2BECCParameter{Buffer: y.FillBytes(make([]byte, 32))},
8391
}
8492

8593
// Calculate Z based on the SW priv * TPM pub
86-
zx, zy := elliptic.P256().ScalarMult(tpmX, tpmY, priv)
94+
zx, err := swPriv.ECDH(tpmPubKey)
95+
if err != nil {
96+
t.Fatalf("ecdh exchange: %v", err)
97+
}
98+
8799
z := TPMSECCPoint{
88-
X: TPM2BECCParameter{Buffer: zx.FillBytes(make([]byte, 32))},
89-
Y: TPM2BECCParameter{Buffer: zy.FillBytes(make([]byte, 32))},
100+
X: TPM2BECCParameter{Buffer: zx},
90101
}
91102

92103
// Calculate Z based on TPM priv * SW pub

0 commit comments

Comments
 (0)