Debian Codegraph @

tpm2/crypto.go

raw
1 package tpm2
2
3 import (
4 "crypto"
5 "crypto/ecdh"
6 "crypto/ecdsa"
7 "crypto/elliptic"
8 "crypto/rsa"
9 "fmt"
10 "math/big"
11 )
12
13 // Priv converts a TPM private key into one recognized by the crypto package.
14 func Priv(public TPMTPublic, sensitive TPMTSensitive) (crypto.PrivateKey, error) {
15
16 var privateKey crypto.PrivateKey
17
18 publicKey, err := Pub(public)
19 if err != nil {
20 return nil, err
21 }
22
23 switch public.Type {
24 case TPMAlgRSA:
25 publicKey := publicKey.(*rsa.PublicKey)
26
27 if sensitive.SensitiveType != TPMAlgRSA {
28 return nil, fmt.Errorf("sensitive type is not equal to public type")
29 }
30
31 prime, err := sensitive.Sensitive.RSA()
32 if err != nil {
33 return nil, fmt.Errorf("failed to retrieve the RSA prime number")
34 }
35
36 P := new(big.Int).SetBytes(prime.Buffer)
37 Q := new(big.Int).Div(publicKey.N, P)
38 phiN := new(big.Int).Mul(new(big.Int).Sub(P, big.NewInt(1)), new(big.Int).Sub(Q, big.NewInt(1)))
39 D := new(big.Int).ModInverse(big.NewInt(int64(publicKey.E)), phiN)
40
41 rsaKey := &rsa.PrivateKey{
42 PublicKey: *publicKey,
43 D: D,
44 Primes: []*big.Int{P, Q},
45 }
46 rsaKey.Precompute()
47
48 privateKey = rsaKey
49 case TPMAlgECC:
50 publicKey := publicKey.(*ecdsa.PublicKey)
51
52 if sensitive.SensitiveType != TPMAlgECC {
53 return nil, fmt.Errorf("sensitive type is not equal to public type")
54 }
55
56 d, err := sensitive.Sensitive.ECC()
57 if err != nil {
58 return nil, fmt.Errorf("failed to retrieve the ECC")
59 }
60
61 D := new(big.Int).SetBytes(d.Buffer)
62
63 ecdsaKey := &ecdsa.PrivateKey{
64 PublicKey: *publicKey,
65 D: D,
66 }
67
68 privateKey = ecdsaKey
69 default:
70 return nil, fmt.Errorf("unsupported public key type: %v", public.Type)
71 }
72
73 return privateKey, nil
74 }
75
76 // Pub converts a TPM public key into one recognized by the crypto package.
77 func Pub(public TPMTPublic) (crypto.PublicKey, error) {
78 var publicKey crypto.PublicKey
79
80 switch public.Type {
81 case TPMAlgRSA:
82 parameters, err := public.Parameters.RSADetail()
83 if err != nil {
84 return nil, fmt.Errorf("failed to retrieve the RSA parameters")
85 }
86
87 n, err := public.Unique.RSA()
88 if err != nil {
89 return nil, fmt.Errorf("failed to parse and retrieve the RSA modulus")
90 }
91
92 publicKey, err = RSAPub(parameters, n)
93 if err != nil {
94 return nil, fmt.Errorf("failed to retrieve the RSA public key")
95 }
96 case TPMAlgECC:
97 parameters, err := public.Parameters.ECCDetail()
98 if err != nil {
99 return nil, fmt.Errorf("failed to retrieve the ECC parameters")
100 }
101
102 pub, err := public.Unique.ECC()
103 if err != nil {
104 return nil, fmt.Errorf("failed to parse and retrieve the ECC point")
105 }
106
107 publicKey, err = ECDSAPub(parameters, pub)
108 if err != nil {
109 return nil, fmt.Errorf("failed to retrieve the ECC public key")
110 }
111 default:
112 return nil, fmt.Errorf("unsupported public key type: %v", public.Type)
113 }
114
115 return publicKey, nil
116 }
117
118 // RSAPub converts a TPM RSA public key into one recognized by the rsa package.
119 func RSAPub(parms *TPMSRSAParms, pub *TPM2BPublicKeyRSA) (*rsa.PublicKey, error) {
120 result := rsa.PublicKey{
121 N: big.NewInt(0).SetBytes(pub.Buffer),
122 E: int(parms.Exponent),
123 }
124 // TPM considers 65537 to be the default RSA public exponent, and 0 in
125 // the parms
126 // indicates so.
127 if result.E == 0 {
128 result.E = 65537
129 }
130 return &result, nil
131 }
132
133 // ECDSAPub converts a TPM ECC public key into one recognized by the ecdh package
134 func ECDSAPub(parms *TPMSECCParms, pub *TPMSECCPoint) (*ecdsa.PublicKey, error) {
135
136 var c elliptic.Curve
137 switch parms.CurveID {
138 case TPMECCNistP256:
139 c = elliptic.P256()
140 case TPMECCNistP384:
141 c = elliptic.P384()
142 case TPMECCNistP521:
143 c = elliptic.P521()
144 default:
145 return nil, fmt.Errorf("unknown curve: %v", parms.CurveID)
146 }
147
148 pubKey := ecdsa.PublicKey{
149 Curve: c,
150 X: big.NewInt(0).SetBytes(pub.X.Buffer),
151 Y: big.NewInt(0).SetBytes(pub.Y.Buffer),
152 }
153
154 return &pubKey, nil
155 }
156
157 // ECDHPub converts a TPM ECC public key into one recognized by the ecdh package
158 func ECDHPub(parms *TPMSECCParms, pub *TPMSECCPoint) (*ecdh.PublicKey, error) {
159
160 pubKey, err := ECDSAPub(parms, pub)
161 if err != nil {
162 return nil, err
163 }
164
165 return pubKey.ECDH()
166 }
167
168 // ECCPoint returns an uncompressed ECC Point
169 func ECCPoint(pubKey *ecdh.PublicKey) (*big.Int, *big.Int, error) {
170 b := pubKey.Bytes()
171 size, err := elementLength(pubKey.Curve())
172 if err != nil {
173 return nil, nil, fmt.Errorf("ECCPoint: %w", err)
174 }
175 return big.NewInt(0).SetBytes(b[1 : size+1]),
176 big.NewInt(0).SetBytes(b[size+1:]), nil
177 }
178
179 func elementLength(c ecdh.Curve) (int, error) {
180 switch c {
181 case ecdh.P256():
182 // crypto/internal/nistec/fiat.p256ElementLen
183 return 32, nil
184 case ecdh.P384():
185 // crypto/internal/nistec/fiat.p384ElementLen
186 return 48, nil
187 case ecdh.P521():
188 // crypto/internal/nistec/fiat.p521ElementLen
189 return 66, nil
190 default:
191 return 0, fmt.Errorf("unknown element length for curve: %v", c)
192 }
193 }