@@ -226,6 +226,61 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
226
226
227
227
// Derive key
228
228
switch k .(type ) {
229
+ case * ecdsaPublicKey :
230
+ // Validate opts
231
+ if opts == nil {
232
+ return nil , errors .New ("Invalid Opts parameter. It must not be nil." )
233
+ }
234
+
235
+ ecdsaK := k .(* ecdsaPublicKey )
236
+
237
+ switch opts .(type ) {
238
+
239
+ // Re-randomized an ECDSA private key
240
+ case * bccsp.ECDSAReRandKeyOpts :
241
+ reRandOpts := opts .(* bccsp.ECDSAReRandKeyOpts )
242
+ tempSK := & ecdsa.PublicKey {
243
+ Curve : ecdsaK .pubKey .Curve ,
244
+ X : new (big.Int ),
245
+ Y : new (big.Int ),
246
+ }
247
+
248
+ var k = new (big.Int ).SetBytes (reRandOpts .ExpansionValue ())
249
+ var one = new (big.Int ).SetInt64 (1 )
250
+ n := new (big.Int ).Sub (ecdsaK .pubKey .Params ().N , one )
251
+ k .Mod (k , n )
252
+ k .Add (k , one )
253
+
254
+ // Compute temporary public key
255
+ tempX , tempY := ecdsaK .pubKey .ScalarBaseMult (k .Bytes ())
256
+ tempSK .X , tempSK .Y = tempSK .Add (
257
+ ecdsaK .pubKey .X , ecdsaK .pubKey .Y ,
258
+ tempX , tempY ,
259
+ )
260
+
261
+ // Verify temporary public key is a valid point on the reference curve
262
+ isOn := tempSK .Curve .IsOnCurve (tempSK .X , tempSK .Y )
263
+ if ! isOn {
264
+ return nil , errors .New ("Failed temporary public key IsOnCurve check." )
265
+ }
266
+
267
+ reRandomizedKey := & ecdsaPublicKey {tempSK }
268
+
269
+ // If the key is not Ephemeral, store it.
270
+ if ! opts .Ephemeral () {
271
+ // Store the key
272
+ err = csp .ks .StoreKey (reRandomizedKey )
273
+ if err != nil {
274
+ return nil , fmt .Errorf ("Failed storing ECDSA key [%s]" , err )
275
+ }
276
+ }
277
+
278
+ return reRandomizedKey , nil
279
+
280
+ default :
281
+ return nil , fmt .Errorf ("Unrecognized KeyDerivOpts provided [%s]" , opts .Algorithm ())
282
+
283
+ }
229
284
case * ecdsaPrivateKey :
230
285
// Validate opts
231
286
if opts == nil {
@@ -268,7 +323,7 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
268
323
// Verify temporary public key is a valid point on the reference curve
269
324
isOn := tempSK .Curve .IsOnCurve (tempSK .PublicKey .X , tempSK .PublicKey .Y )
270
325
if ! isOn {
271
- return nil , errors .New ("Failed temporary public key IsOnCurve check. This is an foreign key. " )
326
+ return nil , errors .New ("Failed temporary public key IsOnCurve check." )
272
327
}
273
328
274
329
reRandomizedKey := & ecdsaPrivateKey {tempSK }
0 commit comments