@@ -13,11 +13,11 @@ import (
13
13
)
14
14
15
15
type nistCurve struct {
16
- name string
17
- generate func (io.Reader ) (privateKey , publicKey [] byte , err error )
18
- importKey func ([]byte ) (publicKey [] byte , err error )
19
- checkPubkey func (publicKey []byte ) error
20
- sharedSecret func (privateKey , publicKey [] byte ) (sharedSecret []byte , err error )
16
+ name string
17
+ generate func (io.Reader ) (* ecdh. PrivateKey , error )
18
+ newPrivateKey func ([]byte ) (* ecdh. PrivateKey , error )
19
+ newPublicKey func (publicKey []byte ) ( * ecdh. PublicKey , error )
20
+ sharedSecret func (* ecdh. PrivateKey , * ecdh. PublicKey ) (sharedSecret []byte , err error )
21
21
}
22
22
23
23
func (c * nistCurve ) String () string {
@@ -43,15 +43,20 @@ func (c *nistCurve) GenerateKey(rand io.Reader) (*PrivateKey, error) {
43
43
return k , nil
44
44
}
45
45
46
- privateKey , publicKey , err := c .generate (rand )
46
+ privateKey , err := c .generate (rand )
47
47
if err != nil {
48
48
return nil , err
49
49
}
50
50
51
51
k := & PrivateKey {
52
52
curve : c ,
53
- privateKey : privateKey ,
54
- publicKey : & PublicKey {curve : c , publicKey : publicKey },
53
+ privateKey : privateKey .Bytes (),
54
+ fips : privateKey ,
55
+ publicKey : & PublicKey {
56
+ curve : c ,
57
+ publicKey : privateKey .PublicKey ().Bytes (),
58
+ fips : privateKey .PublicKey (),
59
+ },
55
60
}
56
61
if boring .Enabled {
57
62
bk , err := boring .NewPrivateKeyECDH (c .name , k .privateKey )
@@ -87,15 +92,19 @@ func (c *nistCurve) NewPrivateKey(key []byte) (*PrivateKey, error) {
87
92
return k , nil
88
93
}
89
94
90
- publicKey , err := c .importKey (key )
95
+ fk , err := c .newPrivateKey (key )
91
96
if err != nil {
92
97
return nil , err
93
98
}
94
-
95
99
k := & PrivateKey {
96
100
curve : c ,
97
101
privateKey : bytes .Clone (key ),
98
- publicKey : & PublicKey {curve : c , publicKey : publicKey },
102
+ fips : fk ,
103
+ publicKey : & PublicKey {
104
+ curve : c ,
105
+ publicKey : fk .PublicKey ().Bytes (),
106
+ fips : fk .PublicKey (),
107
+ },
99
108
}
100
109
return k , nil
101
110
}
@@ -117,9 +126,11 @@ func (c *nistCurve) NewPublicKey(key []byte) (*PublicKey, error) {
117
126
}
118
127
k .boring = bk
119
128
} else {
120
- if err := c .checkPubkey (k .publicKey ); err != nil {
129
+ fk , err := c .newPublicKey (key )
130
+ if err != nil {
121
131
return nil , err
122
132
}
133
+ k .fips = fk
123
134
}
124
135
return k , nil
125
136
}
@@ -135,7 +146,7 @@ func (c *nistCurve) ecdh(local *PrivateKey, remote *PublicKey) ([]byte, error) {
135
146
if boring .Enabled {
136
147
return boring .ECDH (local .boring , remote .boring )
137
148
}
138
- return c .sharedSecret (local .privateKey , remote .publicKey )
149
+ return c .sharedSecret (local .fips , remote .fips )
139
150
}
140
151
141
152
// P256 returns a [Curve] which implements NIST P-256 (FIPS 186-3, section D.2.3),
@@ -146,11 +157,19 @@ func (c *nistCurve) ecdh(local *PrivateKey, remote *PublicKey) ([]byte, error) {
146
157
func P256 () Curve { return p256 }
147
158
148
159
var p256 = & nistCurve {
149
- name : "P-256" ,
150
- generate : ecdh .GenerateKeyP256 ,
151
- importKey : ecdh .ImportKeyP256 ,
152
- checkPubkey : ecdh .CheckPublicKeyP256 ,
153
- sharedSecret : ecdh .ECDHP256 ,
160
+ name : "P-256" ,
161
+ generate : func (r io.Reader ) (* ecdh.PrivateKey , error ) {
162
+ return ecdh .GenerateKey (ecdh .P256 (), r )
163
+ },
164
+ newPrivateKey : func (b []byte ) (* ecdh.PrivateKey , error ) {
165
+ return ecdh .NewPrivateKey (ecdh .P256 (), b )
166
+ },
167
+ newPublicKey : func (publicKey []byte ) (* ecdh.PublicKey , error ) {
168
+ return ecdh .NewPublicKey (ecdh .P256 (), publicKey )
169
+ },
170
+ sharedSecret : func (priv * ecdh.PrivateKey , pub * ecdh.PublicKey ) (sharedSecret []byte , err error ) {
171
+ return ecdh .ECDH (ecdh .P256 (), priv , pub )
172
+ },
154
173
}
155
174
156
175
// P384 returns a [Curve] which implements NIST P-384 (FIPS 186-3, section D.2.4),
@@ -161,11 +180,19 @@ var p256 = &nistCurve{
161
180
func P384 () Curve { return p384 }
162
181
163
182
var p384 = & nistCurve {
164
- name : "P-384" ,
165
- generate : ecdh .GenerateKeyP384 ,
166
- importKey : ecdh .ImportKeyP384 ,
167
- checkPubkey : ecdh .CheckPublicKeyP384 ,
168
- sharedSecret : ecdh .ECDHP384 ,
183
+ name : "P-384" ,
184
+ generate : func (r io.Reader ) (* ecdh.PrivateKey , error ) {
185
+ return ecdh .GenerateKey (ecdh .P384 (), r )
186
+ },
187
+ newPrivateKey : func (b []byte ) (* ecdh.PrivateKey , error ) {
188
+ return ecdh .NewPrivateKey (ecdh .P384 (), b )
189
+ },
190
+ newPublicKey : func (publicKey []byte ) (* ecdh.PublicKey , error ) {
191
+ return ecdh .NewPublicKey (ecdh .P384 (), publicKey )
192
+ },
193
+ sharedSecret : func (priv * ecdh.PrivateKey , pub * ecdh.PublicKey ) (sharedSecret []byte , err error ) {
194
+ return ecdh .ECDH (ecdh .P384 (), priv , pub )
195
+ },
169
196
}
170
197
171
198
// P521 returns a [Curve] which implements NIST P-521 (FIPS 186-3, section D.2.5),
@@ -176,9 +203,17 @@ var p384 = &nistCurve{
176
203
func P521 () Curve { return p521 }
177
204
178
205
var p521 = & nistCurve {
179
- name : "P-521" ,
180
- generate : ecdh .GenerateKeyP521 ,
181
- importKey : ecdh .ImportKeyP521 ,
182
- checkPubkey : ecdh .CheckPublicKeyP521 ,
183
- sharedSecret : ecdh .ECDHP521 ,
206
+ name : "P-521" ,
207
+ generate : func (r io.Reader ) (* ecdh.PrivateKey , error ) {
208
+ return ecdh .GenerateKey (ecdh .P521 (), r )
209
+ },
210
+ newPrivateKey : func (b []byte ) (* ecdh.PrivateKey , error ) {
211
+ return ecdh .NewPrivateKey (ecdh .P521 (), b )
212
+ },
213
+ newPublicKey : func (publicKey []byte ) (* ecdh.PublicKey , error ) {
214
+ return ecdh .NewPublicKey (ecdh .P521 (), publicKey )
215
+ },
216
+ sharedSecret : func (priv * ecdh.PrivateKey , pub * ecdh.PublicKey ) (sharedSecret []byte , err error ) {
217
+ return ecdh .ECDH (ecdh .P521 (), priv , pub )
218
+ },
184
219
}
0 commit comments