Merge pull request #2205 from wpaulino/sign-ecdsa-with-noncedata
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Thu, 20 Apr 2023 21:53:13 +0000 (21:53 +0000)
committerGitHub <noreply@github.com>
Thu, 20 Apr 2023 21:53:13 +0000 (21:53 +0000)
Generate local signatures with additional randomness

1  2 
lightning/src/util/crypto.rs

index 39dfd39b785b048535163756ddc417ff80009ed4,ac159519c59048ba762384db5bdfa418ce8cb9ec..7352542605070c7812201d0a10f35f65eaa3b3e4
@@@ -3,6 -3,10 +3,10 @@@ use bitcoin::hashes::hmac::{Hmac, HmacE
  use bitcoin::hashes::sha256::Hash as Sha256;
  use bitcoin::secp256k1::{Message, Secp256k1, SecretKey, ecdsa::Signature, Signing};
  
+ use crate::chain::keysinterface::EntropySource;
+ use core::ops::Deref;
  macro_rules! hkdf_extract_expand {
        ($salt: expr, $ikm: expr) => {{
                let mut hmac = HmacEngine::<Sha256>::new($salt);
                let (k1, k2, _) = hkdf_extract_expand!($salt, $ikm);
                (k1, k2)
        }};
 -      ($salt: expr, $ikm: expr, 3) => {{
 +      ($salt: expr, $ikm: expr, 4) => {{
                let (k1, k2, prk) = hkdf_extract_expand!($salt, $ikm);
  
                let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
                hmac.input(&k2);
                hmac.input(&[3; 1]);
 -              (k1, k2, Hmac::from_engine(hmac).into_inner())
 +              let k3 = Hmac::from_engine(hmac).into_inner();
 +
 +              let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
 +              hmac.input(&k3);
 +              hmac.input(&[4; 1]);
 +              (k1, k2, k3, Hmac::from_engine(hmac).into_inner())
        }}
  }
  
@@@ -39,8 -38,8 +43,8 @@@ pub fn hkdf_extract_expand_twice(salt: 
        hkdf_extract_expand!(salt, ikm, 2)
  }
  
 -pub fn hkdf_extract_expand_thrice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32]) {
 -      hkdf_extract_expand!(salt, ikm, 3)
 +pub fn hkdf_extract_expand_4x(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32], [u8; 32]) {
 +      hkdf_extract_expand!(salt, ikm, 4)
  }
  
  #[inline]
@@@ -51,3 -50,21 +55,21 @@@ pub fn sign<C: Signing>(ctx: &Secp256k1
        let sig = ctx.sign_ecdsa(msg, sk);
        sig
  }
+ #[inline]
+ pub fn sign_with_aux_rand<C: Signing, ES: Deref>(
+       ctx: &Secp256k1<C>, msg: &Message, sk: &SecretKey, entropy_source: &ES
+ ) -> Signature where ES::Target: EntropySource {
+       #[cfg(feature = "grind_signatures")]
+       let sig = loop {
+               let sig = ctx.sign_ecdsa_with_noncedata(msg, sk, &entropy_source.get_secure_random_bytes());
+               if sig.serialize_compact()[0] < 0x80 {
+                       break sig;
+               }
+       };
+       #[cfg(all(not(feature = "grind_signatures"), not(feature = "_test_vectors")))]
+       let sig = ctx.sign_ecdsa_with_noncedata(msg, sk, &entropy_source.get_secure_random_bytes());
+       #[cfg(all(not(feature = "grind_signatures"), feature = "_test_vectors"))]
+       let sig = sign(ctx, msg, sk);
+       sig
+ }