@@ -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,49 @@ 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+
487495 if (CX_OK != cx_math_cmp_no_throw (k_1 , secp256k1_n , 32 , & diff )) {
488- return -1 ;
496+ err = true;
497+ goto cleanup ;
489498 }
490499 if (is_array_all_zeros (k_1 , sizeof (k_1 )) || diff >= 0 ) {
491500 PRINTF ("first secnonce value is out of range\n" );
492- return -1 ;
501+ err = true;
502+ goto cleanup ;
493503 }
494504 if (CX_OK != cx_math_cmp_no_throw (k_2 , secp256k1_n , 32 , & diff )) {
495- return -1 ;
505+ err = true;
506+ goto cleanup ;
496507 }
497508 if (is_array_all_zeros (k_2 , sizeof (k_2 )) || diff >= 0 ) {
498509 PRINTF ("second secnonce value is out of range\n" );
499- return -1 ;
510+ err = true;
511+ goto cleanup ;
500512 }
501513
502514 if (!has_even_y (& R )) {
503515 if (CX_OK != cx_math_sub_no_throw (k_1 , secp256k1_n , k_1 , 32 )) {
504- return -1 ;
516+ err = true;
517+ goto cleanup ;
505518 };
506519 if (CX_OK != cx_math_sub_no_throw (k_2 , secp256k1_n , k_2 , 32 )) {
507- return -1 ;
520+ err = true;
521+ goto cleanup ;
508522 };
509523 }
510524
511525 if (CX_OK != cx_math_cmp_no_throw (sk , secp256k1_n , 32 , & diff )) {
512- return -1 ;
526+ err = true;
527+ goto cleanup ;
513528 }
514529 if (is_array_all_zeros (sk , 32 ) || diff >= 0 ) {
515530 PRINTF ("secret key value is out of range\n" );
516- return -1 ;
531+ err = true;
532+ goto cleanup ;
517533 }
518534
519- bool err = false;
520-
521535 // Put together all the variables that we want to always zero out before returning.
522536 // As an excess of safety, we put here any variable that is (directly or indirectly) derived
523537 // from the secret during the computation of the signature
@@ -574,7 +588,7 @@ int musig_sign(musig_secnonce_t *secnonce,
574588 break ;
575589 }
576590
577- uint8_t bk_2 [ 32 ]; // b * k_2
591+ // bk_2 = b * k_2
578592 if (CX_OK != cx_math_multm_no_throw (bk_2 , b , k_2 , secp256k1_n , 32 )) {
579593 err = true;
580594 break ;
@@ -607,6 +621,11 @@ int musig_sign(musig_secnonce_t *secnonce,
607621 // make sure to zero out any variable derived from secrets before returning
608622 explicit_bzero (& secrets , sizeof (secrets ));
609623
624+ cleanup :
625+ explicit_bzero (k_1 , sizeof (k_1 ));
626+ explicit_bzero (k_2 , sizeof (k_2 ));
627+ explicit_bzero (bk_2 , sizeof (bk_2 ));
628+
610629 if (err ) {
611630 return -1 ;
612631 }
0 commit comments