| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/elliptic" |
| 5 |
"fmt" |
| 6 |
"math/big" |
| 7 |
|
| 8 |
"github.com/google/go-tpm/legacy/tpm2" |
| 9 |
) |
| 10 |
|
| 11 |
// ECC coordinates need to maintain a specific size based on the curve, so we pad the front with zeros. |
| 12 |
// This is particularly an issue for NIST-P521 coordinates, as they are frequently missing their first byte. |
| 13 |
func eccIntToBytes(curve elliptic.Curve, i *big.Int) []byte { |
| 14 |
bytes := i.Bytes() |
| 15 |
curveBytes := (curve.Params().BitSize + 7) / 8 |
| 16 |
return append(make([]byte, curveBytes-len(bytes)), bytes...) |
| 17 |
} |
| 18 |
|
| 19 |
func curveIDToGoCurve(curve tpm2.EllipticCurve) (elliptic.Curve, error) { |
| 20 |
switch curve { |
| 21 |
case tpm2.CurveNISTP224: |
| 22 |
return elliptic.P224(), nil |
| 23 |
case tpm2.CurveNISTP256: |
| 24 |
return elliptic.P256(), nil |
| 25 |
case tpm2.CurveNISTP384: |
| 26 |
return elliptic.P384(), nil |
| 27 |
case tpm2.CurveNISTP521: |
| 28 |
return elliptic.P521(), nil |
| 29 |
default: |
| 30 |
return nil, fmt.Errorf("unsupported TPM2 curve: %v", curve) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func goCurveToCurveID(curve elliptic.Curve) (tpm2.EllipticCurve, error) { |
| 35 |
switch curve.Params().Name { |
| 36 |
case elliptic.P224().Params().Name: |
| 37 |
return tpm2.CurveNISTP224, nil |
| 38 |
case elliptic.P256().Params().Name: |
| 39 |
return tpm2.CurveNISTP256, nil |
| 40 |
case elliptic.P384().Params().Name: |
| 41 |
return tpm2.CurveNISTP384, nil |
| 42 |
case elliptic.P521().Params().Name: |
| 43 |
return tpm2.CurveNISTP521, nil |
| 44 |
default: |
| 45 |
return 0, fmt.Errorf("unsupported Go curve: %v", curve.Params().Name) |
| 46 |
} |
| 47 |
} |