@@ -264,27 +264,32 @@ int musig_nonce_gen(const uint8_t *rand,
264264 uint8_t msg [] = {0x00 };
265265
266266 musig_nonce_hash (rand , rand_len , pk , aggpk , 0 , msg , 1 , NULL , 0 , secnonce -> k_1 );
267- if (CX_OK != cx_math_modm_no_throw (secnonce -> k_1 , 32 , secp256k1_n , 32 )) return -1 ;
267+ if (CX_OK != cx_math_modm_no_throw (secnonce -> k_1 , 32 , secp256k1_n , 32 )) goto nonce_gen_fail ;
268268 musig_nonce_hash (rand , rand_len , pk , aggpk , 1 , msg , 1 , NULL , 0 , secnonce -> k_2 );
269- if (CX_OK != cx_math_modm_no_throw (secnonce -> k_2 , 32 , secp256k1_n , 32 )) return -1 ;
269+ if (CX_OK != cx_math_modm_no_throw (secnonce -> k_2 , 32 , secp256k1_n , 32 )) goto nonce_gen_fail ;
270270
271271 if (is_array_all_zeros (secnonce -> k_1 , sizeof (secnonce -> k_1 )) ||
272272 is_array_all_zeros (secnonce -> k_2 , sizeof (secnonce -> k_2 ))) {
273273 // this can only happen with negligible probability
274- return -1 ;
274+ goto nonce_gen_fail ;
275275 }
276276
277277 memcpy (secnonce -> pk , pk , sizeof (secnonce -> pk ));
278278
279279 point_t R_s1 , R_s2 ;
280280
281- if (CX_OK != point_mul (G , secnonce -> k_1 , & R_s1 )) return -1 ;
282- if (CX_OK != point_mul (G , secnonce -> k_2 , & R_s2 )) return -1 ;
281+ if (CX_OK != point_mul (G , secnonce -> k_1 , & R_s1 )) goto nonce_gen_fail ;
282+ if (CX_OK != point_mul (G , secnonce -> k_2 , & R_s2 )) goto nonce_gen_fail ;
283283
284- if (0 > crypto_get_compressed_pubkey (R_s1 .raw , pubnonce -> R_s1 )) return -1 ;
285- if (0 > crypto_get_compressed_pubkey (R_s2 .raw , pubnonce -> R_s2 )) return -1 ;
284+ if (0 > crypto_get_compressed_pubkey (R_s1 .raw , pubnonce -> R_s1 )) goto nonce_gen_fail ;
285+ if (0 > crypto_get_compressed_pubkey (R_s2 .raw , pubnonce -> R_s2 )) goto nonce_gen_fail ;
286286
287287 return 0 ;
288+
289+ nonce_gen_fail :
290+ explicit_bzero (secnonce -> k_1 , sizeof (secnonce -> k_1 ));
291+ explicit_bzero (secnonce -> k_2 , sizeof (secnonce -> k_2 ));
292+ return -1 ;
288293}
289294
290295int musig_nonce_agg (const musig_pubnonce_t pubnonces [], size_t n_keys , musig_pubnonce_t * out ) {
@@ -484,40 +489,54 @@ int musig_sign(musig_secnonce_t *secnonce,
484489 explicit_bzero (secnonce -> k_1 , sizeof (secnonce -> k_1 ));
485490 explicit_bzero (secnonce -> k_2 , sizeof (secnonce -> k_2 ));
486491
492+ bool err = false;
493+ uint8_t bk_2 [32 ];
494+
495+ if (0 > musig_get_session_values (session_ctx , & Q , gacc , tacc , b , & R , e )) {
496+ err = true;
497+ goto cleanup ;
498+ }
499+
487500 if (CX_OK != cx_math_cmp_no_throw (k_1 , secp256k1_n , 32 , & diff )) {
488- return -1 ;
501+ err = true;
502+ goto cleanup ;
489503 }
490504 if (is_array_all_zeros (k_1 , sizeof (k_1 )) || diff >= 0 ) {
491505 PRINTF ("first secnonce value is out of range\n" );
492- return -1 ;
506+ err = true;
507+ goto cleanup ;
493508 }
494509 if (CX_OK != cx_math_cmp_no_throw (k_2 , secp256k1_n , 32 , & diff )) {
495- return -1 ;
510+ err = true;
511+ goto cleanup ;
496512 }
497513 if (is_array_all_zeros (k_2 , sizeof (k_2 )) || diff >= 0 ) {
498514 PRINTF ("second secnonce value is out of range\n" );
499- return -1 ;
515+ err = true;
516+ goto cleanup ;
500517 }
501518
502519 if (!has_even_y (& R )) {
503520 if (CX_OK != cx_math_sub_no_throw (k_1 , secp256k1_n , k_1 , 32 )) {
504- return -1 ;
521+ err = true;
522+ goto cleanup ;
505523 };
506524 if (CX_OK != cx_math_sub_no_throw (k_2 , secp256k1_n , k_2 , 32 )) {
507- return -1 ;
525+ err = true;
526+ goto cleanup ;
508527 };
509528 }
510529
511530 if (CX_OK != cx_math_cmp_no_throw (sk , secp256k1_n , 32 , & diff )) {
512- return -1 ;
531+ err = true;
532+ goto cleanup ;
513533 }
514534 if (is_array_all_zeros (sk , 32 ) || diff >= 0 ) {
515535 PRINTF ("secret key value is out of range\n" );
516- return -1 ;
536+ err = true;
537+ goto cleanup ;
517538 }
518539
519- bool err = false;
520-
521540 // Put together all the variables that we want to always zero out before returning.
522541 // As an excess of safety, we put here any variable that is (directly or indirectly) derived
523542 // from the secret during the computation of the signature
@@ -574,7 +593,7 @@ int musig_sign(musig_secnonce_t *secnonce,
574593 break ;
575594 }
576595
577- uint8_t bk_2 [ 32 ]; // b * k_2
596+ // bk_2 = b * k_2
578597 if (CX_OK != cx_math_multm_no_throw (bk_2 , b , k_2 , secp256k1_n , 32 )) {
579598 err = true;
580599 break ;
@@ -607,6 +626,11 @@ int musig_sign(musig_secnonce_t *secnonce,
607626 // make sure to zero out any variable derived from secrets before returning
608627 explicit_bzero (& secrets , sizeof (secrets ));
609628
629+ cleanup :
630+ explicit_bzero (k_1 , sizeof (k_1 ));
631+ explicit_bzero (k_2 , sizeof (k_2 ));
632+ explicit_bzero (bk_2 , sizeof (bk_2 ));
633+
610634 if (err ) {
611635 return -1 ;
612636 }
0 commit comments