2022-10-25 23:17:25 -05:00
|
|
|
use std::io::{self, Read};
|
2022-06-24 19:47:19 -04:00
|
|
|
|
|
|
|
|
use rand_core::{RngCore, CryptoRng};
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
use zeroize::Zeroize;
|
2022-10-13 00:38:36 -04:00
|
|
|
use subtle::ConstantTimeEq;
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
|
2022-10-29 03:54:42 -05:00
|
|
|
use digest::Digest;
|
2022-06-24 19:47:19 -04:00
|
|
|
|
2022-10-29 03:54:42 -05:00
|
|
|
use group::{
|
|
|
|
|
ff::{Field, PrimeField},
|
|
|
|
|
Group,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub use ciphersuite::Ciphersuite;
|
|
|
|
|
|
|
|
|
|
#[cfg(any(feature = "ristretto", feature = "ed25519"))]
|
2022-06-24 19:47:19 -04:00
|
|
|
mod dalek;
|
2022-10-29 03:54:42 -05:00
|
|
|
#[cfg(feature = "ristretto")]
|
2022-06-24 19:47:19 -04:00
|
|
|
pub use dalek::{Ristretto, IetfRistrettoHram};
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
pub use dalek::{Ed25519, IetfEd25519Hram};
|
|
|
|
|
|
2022-10-29 03:54:42 -05:00
|
|
|
#[cfg(any(feature = "secp256k1", feature = "p256"))]
|
2022-06-24 19:47:19 -04:00
|
|
|
mod kp256;
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
2022-08-26 05:59:43 -04:00
|
|
|
pub use kp256::{Secp256k1, IetfSecp256k1Hram};
|
2022-06-24 19:47:19 -04:00
|
|
|
#[cfg(feature = "p256")]
|
|
|
|
|
pub use kp256::{P256, IetfP256Hram};
|
|
|
|
|
|
2022-08-29 02:32:59 -05:00
|
|
|
#[cfg(feature = "ed448")]
|
|
|
|
|
mod ed448;
|
|
|
|
|
#[cfg(feature = "ed448")]
|
2022-10-13 00:38:36 -04:00
|
|
|
pub use ed448::{Ed448, Ietf8032Ed448Hram, IetfEd448Hram};
|
2022-08-29 02:32:59 -05:00
|
|
|
|
2022-10-29 03:54:42 -05:00
|
|
|
/// FROST Ciphersuite, except for the signing algorithm specific H2, making this solely the curve,
|
|
|
|
|
/// its associated hash function, and the functions derived from it.
|
|
|
|
|
pub trait Curve: Ciphersuite {
|
|
|
|
|
/// Context string for this curve.
|
|
|
|
|
const CONTEXT: &'static [u8];
|
2022-06-24 19:47:19 -04:00
|
|
|
|
2022-08-26 05:59:43 -04:00
|
|
|
/// Hash the given dst and data to a byte vector. Used to instantiate H4 and H5.
|
2022-10-29 03:54:42 -05:00
|
|
|
fn hash_to_vec(dst: &[u8], data: &[u8]) -> Vec<u8> {
|
2022-11-01 00:03:36 -05:00
|
|
|
Self::H::digest([Self::CONTEXT, dst, data].concat()).as_ref().to_vec()
|
2022-10-29 03:54:42 -05:00
|
|
|
}
|
2022-08-26 05:59:43 -04:00
|
|
|
|
|
|
|
|
/// Field element from hash. Used during key gen and by other crates under Serai as a general
|
|
|
|
|
/// utility. Used to instantiate H1 and H3.
|
|
|
|
|
#[allow(non_snake_case)]
|
2022-10-29 03:54:42 -05:00
|
|
|
fn hash_to_F(dst: &[u8], msg: &[u8]) -> Self::F {
|
|
|
|
|
<Self as Ciphersuite>::hash_to_F(&[Self::CONTEXT, dst].concat(), msg)
|
|
|
|
|
}
|
2022-08-26 05:59:43 -04:00
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Hash the message for the binding factor. H4 from the IETF draft.
|
2022-08-26 05:59:43 -04:00
|
|
|
fn hash_msg(msg: &[u8]) -> Vec<u8> {
|
|
|
|
|
Self::hash_to_vec(b"msg", msg)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Hash the commitments for the binding factor. H5 from the IETF draft.
|
2022-08-26 05:59:43 -04:00
|
|
|
fn hash_commitments(commitments: &[u8]) -> Vec<u8> {
|
|
|
|
|
Self::hash_to_vec(b"com", commitments)
|
|
|
|
|
}
|
2022-06-24 19:47:19 -04:00
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Hash the commitments and message to calculate the binding factor. H1 from the IETF draft.
|
2022-08-26 05:59:43 -04:00
|
|
|
fn hash_binding_factor(binding: &[u8]) -> Self::F {
|
2022-10-29 03:54:42 -05:00
|
|
|
<Self as Curve>::hash_to_F(b"rho", binding)
|
2022-10-13 00:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Securely generate a random nonce. H3 from the IETF draft.
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
fn random_nonce<R: RngCore + CryptoRng>(mut secret: Self::F, rng: &mut R) -> Self::F {
|
|
|
|
|
let mut seed = vec![0; 32];
|
|
|
|
|
rng.fill_bytes(&mut seed);
|
|
|
|
|
|
|
|
|
|
let mut repr = secret.to_repr();
|
|
|
|
|
secret.zeroize();
|
|
|
|
|
|
2022-10-13 00:38:36 -04:00
|
|
|
let mut res;
|
|
|
|
|
while {
|
|
|
|
|
seed.extend(repr.as_ref());
|
2022-10-29 03:54:42 -05:00
|
|
|
res = <Self as Curve>::hash_to_F(b"nonce", &seed);
|
2022-10-13 00:38:36 -04:00
|
|
|
res.ct_eq(&Self::F::zero()).into()
|
|
|
|
|
} {
|
|
|
|
|
rng.fill_bytes(&mut seed);
|
|
|
|
|
}
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
for i in repr.as_mut() {
|
2022-08-12 01:14:13 -04:00
|
|
|
i.zeroize();
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
}
|
|
|
|
|
seed.zeroize();
|
|
|
|
|
res
|
|
|
|
|
}
|
2022-06-24 19:47:19 -04:00
|
|
|
|
2022-07-13 02:38:29 -04:00
|
|
|
#[allow(non_snake_case)]
|
2022-10-29 03:54:42 -05:00
|
|
|
fn read_G<R: Read>(reader: &mut R) -> io::Result<Self::G> {
|
|
|
|
|
let res = <Self as Ciphersuite>::read_G(reader)?;
|
|
|
|
|
if res.is_identity().into() {
|
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other, "identity point"))?;
|
2022-07-13 02:38:29 -04:00
|
|
|
}
|
2022-10-29 03:54:42 -05:00
|
|
|
Ok(res)
|
2022-06-28 01:25:26 -04:00
|
|
|
}
|
2022-06-24 19:47:19 -04:00
|
|
|
}
|