Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to secp256k1 signature support #964

Merged
merged 3 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tendermint/src/block/commit_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl TryFrom<RawCommitSig> for CommitSig {
return Ok(CommitSig::BlockIdFlagCommit {
validator_address: value.validator_address.try_into()?,
timestamp,
signature: Signature::new(value.signature),
signature: Signature::new(value.signature)?,
});
}
if value.block_id_flag == BlockIdFlag::Nil.to_i32().unwrap() {
Expand All @@ -120,7 +120,7 @@ impl TryFrom<RawCommitSig> for CommitSig {
return Ok(CommitSig::BlockIdFlagNil {
validator_address: value.validator_address.try_into()?,
timestamp: value.timestamp.ok_or_else(Error::missing_timestamp)?.into(),
signature: Signature::new(value.signature),
signature: Signature::new(value.signature)?,
});
}
Err(Error::block_id_flag())
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl TryFrom<RawProposal> for Proposal {
pol_round,
block_id: value.block_id.map(TryInto::try_into).transpose()?,
timestamp: value.timestamp.map(|t| t.into()),
signature: Signature::new(value.signature),
signature: Signature::new(value.signature)?,
})
}
}
Expand Down
258 changes: 257 additions & 1 deletion tendermint/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ where

#[cfg(test)]
mod tests {
use super::{PublicKey, TendermintKey};
use std::convert::TryFrom;

use super::{PublicKey, Signature, TendermintKey};
use crate::public_key::PubKeyResponse;
use subtle_encoding::hex;
use tendermint_proto::Protobuf;
Expand Down Expand Up @@ -489,4 +491,258 @@ mod tests {
assert_eq!(got, encoded);
assert_eq!(PubKeyResponse::decode_vec(&encoded).unwrap(), msg);
}

// From https://datatracker.ietf.org/doc/html/rfc8032#section-7.1
// Each test vector consists of: [public_key, message, signature].
const ED25519_TEST_VECTORS: &[&[&[u8]]] = &[
// Test 1
&[
&[
0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64,
0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68,
0xf7, 0x07, 0x51, 0x1a,
],
&[],
&[
0xe5, 0x56, 0x43, 0x00, 0xc3, 0x60, 0xac, 0x72, 0x90, 0x86, 0xe2, 0xcc, 0x80, 0x6e,
0x82, 0x8a, 0x84, 0x87, 0x7f, 0x1e, 0xb8, 0xe5, 0xd9, 0x74, 0xd8, 0x73, 0xe0, 0x65,
0x22, 0x49, 0x01, 0x55, 0x5f, 0xb8, 0x82, 0x15, 0x90, 0xa3, 0x3b, 0xac, 0xc6, 0x1e,
0x39, 0x70, 0x1c, 0xf9, 0xb4, 0x6b, 0xd2, 0x5b, 0xf5, 0xf0, 0x59, 0x5b, 0xbe, 0x24,
0x65, 0x51, 0x41, 0x43, 0x8e, 0x7a, 0x10, 0x0b,
],
],
// Test 2
&[
&[
0x3d, 0x40, 0x17, 0xc3, 0xe8, 0x43, 0x89, 0x5a, 0x92, 0xb7, 0x0a, 0xa7, 0x4d, 0x1b,
0x7e, 0xbc, 0x9c, 0x98, 0x2c, 0xcf, 0x2e, 0xc4, 0x96, 0x8c, 0xc0, 0xcd, 0x55, 0xf1,
0x2a, 0xf4, 0x66, 0x0c,
],
&[0x72],
&[
0x92, 0xa0, 0x09, 0xa9, 0xf0, 0xd4, 0xca, 0xb8, 0x72, 0x0e, 0x82, 0x0b, 0x5f, 0x64,
0x25, 0x40, 0xa2, 0xb2, 0x7b, 0x54, 0x16, 0x50, 0x3f, 0x8f, 0xb3, 0x76, 0x22, 0x23,
0xeb, 0xdb, 0x69, 0xda, 0x08, 0x5a, 0xc1, 0xe4, 0x3e, 0x15, 0x99, 0x6e, 0x45, 0x8f,
0x36, 0x13, 0xd0, 0xf1, 0x1d, 0x8c, 0x38, 0x7b, 0x2e, 0xae, 0xb4, 0x30, 0x2a, 0xee,
0xb0, 0x0d, 0x29, 0x16, 0x12, 0xbb, 0x0c, 0x00,
],
],
// Test 3
&[
&[
0xfc, 0x51, 0xcd, 0x8e, 0x62, 0x18, 0xa1, 0xa3, 0x8d, 0xa4, 0x7e, 0xd0, 0x02, 0x30,
0xf0, 0x58, 0x08, 0x16, 0xed, 0x13, 0xba, 0x33, 0x03, 0xac, 0x5d, 0xeb, 0x91, 0x15,
0x48, 0x90, 0x80, 0x25,
],
&[0xaf, 0x82],
&[
0x62, 0x91, 0xd6, 0x57, 0xde, 0xec, 0x24, 0x02, 0x48, 0x27, 0xe6, 0x9c, 0x3a, 0xbe,
0x01, 0xa3, 0x0c, 0xe5, 0x48, 0xa2, 0x84, 0x74, 0x3a, 0x44, 0x5e, 0x36, 0x80, 0xd7,
0xdb, 0x5a, 0xc3, 0xac, 0x18, 0xff, 0x9b, 0x53, 0x8d, 0x16, 0xf2, 0x90, 0xae, 0x67,
0xf7, 0x60, 0x98, 0x4d, 0xc6, 0x59, 0x4a, 0x7c, 0x15, 0xe9, 0x71, 0x6e, 0xd2, 0x8d,
0xc0, 0x27, 0xbe, 0xce, 0xea, 0x1e, 0xc4, 0x0a,
],
],
// Test 1024
&[
&[
0x27, 0x81, 0x17, 0xfc, 0x14, 0x4c, 0x72, 0x34, 0x0f, 0x67, 0xd0, 0xf2, 0x31, 0x6e,
0x83, 0x86, 0xce, 0xff, 0xbf, 0x2b, 0x24, 0x28, 0xc9, 0xc5, 0x1f, 0xef, 0x7c, 0x59,
0x7f, 0x1d, 0x42, 0x6e,
],
&[
0x08, 0xb8, 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 0x11, 0xfa, 0xa9,
0x33, 0xa1, 0x5e, 0xf1, 0x36, 0x95, 0x46, 0x86, 0x8a, 0x7f, 0x3a, 0x45, 0xa9, 0x67,
0x68, 0xd4, 0x0f, 0xd9, 0xd0, 0x34, 0x12, 0xc0, 0x91, 0xc6, 0x31, 0x5c, 0xf4, 0xfd,
0xe7, 0xcb, 0x68, 0x60, 0x69, 0x37, 0x38, 0x0d, 0xb2, 0xea, 0xaa, 0x70, 0x7b, 0x4c,
0x41, 0x85, 0xc3, 0x2e, 0xdd, 0xcd, 0xd3, 0x06, 0x70, 0x5e, 0x4d, 0xc1, 0xff, 0xc8,
0x72, 0xee, 0xee, 0x47, 0x5a, 0x64, 0xdf, 0xac, 0x86, 0xab, 0xa4, 0x1c, 0x06, 0x18,
0x98, 0x3f, 0x87, 0x41, 0xc5, 0xef, 0x68, 0xd3, 0xa1, 0x01, 0xe8, 0xa3, 0xb8, 0xca,
0xc6, 0x0c, 0x90, 0x5c, 0x15, 0xfc, 0x91, 0x08, 0x40, 0xb9, 0x4c, 0x00, 0xa0, 0xb9,
0xd0,
],
&[
0x0a, 0xab, 0x4c, 0x90, 0x05, 0x01, 0xb3, 0xe2, 0x4d, 0x7c, 0xdf, 0x46, 0x63, 0x32,
0x6a, 0x3a, 0x87, 0xdf, 0x5e, 0x48, 0x43, 0xb2, 0xcb, 0xdb, 0x67, 0xcb, 0xf6, 0xe4,
0x60, 0xfe, 0xc3, 0x50, 0xaa, 0x53, 0x71, 0xb1, 0x50, 0x8f, 0x9f, 0x45, 0x28, 0xec,
0xea, 0x23, 0xc4, 0x36, 0xd9, 0x4b, 0x5e, 0x8f, 0xcd, 0x4f, 0x68, 0x1e, 0x30, 0xa6,
0xac, 0x00, 0xa9, 0x70, 0x4a, 0x18, 0x8a, 0x03,
],
],
];

#[test]
fn ed25519_test_vectors() {
for (i, v) in ED25519_TEST_VECTORS.iter().enumerate() {
let public_key = v[0];
let msg = v[1];
let sig = v[2];

let public_key = PublicKey::from_raw_ed25519(public_key).unwrap();
match public_key {
PublicKey::Ed25519(_) => {}
#[cfg(feature = "secp256k1")]
_ => panic!("expected public key to be Ed25519: {:?}", public_key),
}
let sig = Signature::try_from(sig).unwrap();
public_key
.verify(msg, &sig)
.unwrap_or_else(|_| panic!("signature should be valid for test vector {}", i));
}
}

// Arbitrary "valid" tests taken from
// https://github.com/google/wycheproof/blob/2196000605e45d91097147c9c71f26b72af58003/testvectors/ecdsa_secp256k1_sha256_test.json
//
// Each test vector consists of: [public_key, message, signature].
//
// NB: It appears as though all signatures in this test suite are
// DER-encoded.
#[cfg(feature = "secp256k1")]
const SECP256K1_TEST_VECTORS: &[&[&[u8]]] = &[
// tcId 3
&[
&[
0x04, 0xb8, 0x38, 0xff, 0x44, 0xe5, 0xbc, 0x17, 0x7b, 0xf2, 0x11, 0x89, 0xd0, 0x76,
0x60, 0x82, 0xfc, 0x9d, 0x84, 0x32, 0x26, 0x88, 0x7f, 0xc9, 0x76, 0x03, 0x71, 0x10,
0x0b, 0x7e, 0xe2, 0x0a, 0x6f, 0xf0, 0xc9, 0xd7, 0x5b, 0xfb, 0xa7, 0xb3, 0x1a, 0x6b,
0xca, 0x19, 0x74, 0x49, 0x6e, 0xeb, 0x56, 0xde, 0x35, 0x70, 0x71, 0x95, 0x5d, 0x83,
0xc4, 0xb1, 0xba, 0xda, 0xa0, 0xb2, 0x18, 0x32, 0xe9,
],
&[0x31, 0x32, 0x33, 0x34, 0x30, 0x30],
&[
0x30, 0x45, 0x02, 0x21, 0x00, 0x81, 0x3e, 0xf7, 0x9c, 0xce, 0xfa, 0x9a, 0x56, 0xf7,
0xba, 0x80, 0x5f, 0x0e, 0x47, 0x85, 0x84, 0xfe, 0x5f, 0x0d, 0xd5, 0xf5, 0x67, 0xbc,
0x09, 0xb5, 0x12, 0x3c, 0xcb, 0xc9, 0x83, 0x23, 0x65, 0x02, 0x20, 0x6f, 0xf1, 0x8a,
0x52, 0xdc, 0xc0, 0x33, 0x6f, 0x7a, 0xf6, 0x24, 0x00, 0xa6, 0xdd, 0x9b, 0x81, 0x07,
0x32, 0xba, 0xf1, 0xff, 0x75, 0x80, 0x00, 0xd6, 0xf6, 0x13, 0xa5, 0x56, 0xeb, 0x31,
0xba,
],
],
// tcId 230
&[
&[
0x04, 0xb8, 0x38, 0xff, 0x44, 0xe5, 0xbc, 0x17, 0x7b, 0xf2, 0x11, 0x89, 0xd0, 0x76,
0x60, 0x82, 0xfc, 0x9d, 0x84, 0x32, 0x26, 0x88, 0x7f, 0xc9, 0x76, 0x03, 0x71, 0x10,
0x0b, 0x7e, 0xe2, 0x0a, 0x6f, 0xf0, 0xc9, 0xd7, 0x5b, 0xfb, 0xa7, 0xb3, 0x1a, 0x6b,
0xca, 0x19, 0x74, 0x49, 0x6e, 0xeb, 0x56, 0xde, 0x35, 0x70, 0x71, 0x95, 0x5d, 0x83,
0xc4, 0xb1, 0xba, 0xda, 0xa0, 0xb2, 0x18, 0x32, 0xe9,
],
&[0x32, 0x35, 0x35, 0x38, 0x35],
&[
0x30, 0x45, 0x02, 0x21, 0x00, 0xdd, 0x1b, 0x7d, 0x09, 0xa7, 0xbd, 0x82, 0x18, 0x96,
0x10, 0x34, 0xa3, 0x9a, 0x87, 0xfe, 0xcf, 0x53, 0x14, 0xf0, 0x0c, 0x4d, 0x25, 0xeb,
0x58, 0xa0, 0x7a, 0xc8, 0x5e, 0x85, 0xea, 0xb5, 0x16, 0x02, 0x20, 0x35, 0x13, 0x8c,
0x40, 0x1e, 0xf8, 0xd3, 0x49, 0x3d, 0x65, 0xc9, 0x00, 0x2f, 0xe6, 0x2b, 0x43, 0xae,
0xe5, 0x68, 0x73, 0x1b, 0x74, 0x45, 0x48, 0x35, 0x89, 0x96, 0xd9, 0xcc, 0x42, 0x7e,
0x06,
],
],
// tcId 231
&[
&[
0x04, 0xb8, 0x38, 0xff, 0x44, 0xe5, 0xbc, 0x17, 0x7b, 0xf2, 0x11, 0x89, 0xd0, 0x76,
0x60, 0x82, 0xfc, 0x9d, 0x84, 0x32, 0x26, 0x88, 0x7f, 0xc9, 0x76, 0x03, 0x71, 0x10,
0x0b, 0x7e, 0xe2, 0x0a, 0x6f, 0xf0, 0xc9, 0xd7, 0x5b, 0xfb, 0xa7, 0xb3, 0x1a, 0x6b,
0xca, 0x19, 0x74, 0x49, 0x6e, 0xeb, 0x56, 0xde, 0x35, 0x70, 0x71, 0x95, 0x5d, 0x83,
0xc4, 0xb1, 0xba, 0xda, 0xa0, 0xb2, 0x18, 0x32, 0xe9,
],
&[0x34, 0x32, 0x36, 0x34, 0x37, 0x39, 0x37, 0x32, 0x34],
&[
0x30, 0x45, 0x02, 0x21, 0x00, 0x95, 0xc2, 0x92, 0x67, 0xd9, 0x72, 0xa0, 0x43, 0xd9,
0x55, 0x22, 0x45, 0x46, 0x22, 0x2b, 0xba, 0x34, 0x3f, 0xc1, 0xd4, 0xdb, 0x0f, 0xec,
0x26, 0x2a, 0x33, 0xac, 0x61, 0x30, 0x56, 0x96, 0xae, 0x02, 0x20, 0x6e, 0xdf, 0xe9,
0x67, 0x13, 0xae, 0xd5, 0x6f, 0x8a, 0x28, 0xa6, 0x65, 0x3f, 0x57, 0xe0, 0xb8, 0x29,
0x71, 0x2e, 0x5e, 0xdd, 0xc6, 0x7f, 0x34, 0x68, 0x2b, 0x24, 0xf0, 0x67, 0x6b, 0x26,
0x40,
],
],
];

#[cfg(feature = "secp256k1")]
#[test]
fn secp256k1_test_vectors() {
for (i, v) in SECP256K1_TEST_VECTORS.iter().enumerate() {
let public_key = v[0];
let msg = v[1];
let sig = v[2];

let public_key = PublicKey::from_raw_secp256k1(public_key).unwrap();
match public_key {
PublicKey::Secp256k1(_) => {}
_ => panic!("expected public key to be secp256k1: {:?}", public_key),
}
let der_sig = k256::ecdsa::Signature::from_der(sig).unwrap();
let sig = der_sig.as_ref();
let sig = Signature::try_from(sig).unwrap();
public_key
.verify(msg, &sig)
.unwrap_or_else(|_| panic!("signature should be valid for test vector {}", i));
}
}
}
37 changes: 29 additions & 8 deletions tendermint/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use tendermint_proto::Protobuf;

use crate::error::Error;

/// The expected length of all currently supported signatures, in bytes.
pub const SIGNATURE_LENGTH: usize = 64;

/// Signatures
#[derive(Clone, Debug, PartialEq)]
pub struct Signature(Vec<u8>);
Expand All @@ -21,11 +24,15 @@ impl TryFrom<Vec<u8>> for Signature {
type Error = Error;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
if bytes.is_empty() {
return Err(Error::empty_signature());
}
Self::new_non_empty(bytes)
}
}

Ok(Self(bytes))
impl TryFrom<&[u8]> for Signature {
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
Self::new_non_empty(bytes)
}
}

Expand All @@ -37,12 +44,26 @@ impl From<Signature> for Vec<u8> {

impl Signature {
/// Create a new signature from the given byte array, if non-empty.
pub fn new(bytes: Vec<u8>) -> Option<Self> {
///
/// If the given byte array is empty, returns `Ok(None)`.
pub fn new<B: AsRef<[u8]>>(bytes: B) -> Result<Option<Self>, Error> {
let bytes = bytes.as_ref();
if bytes.is_empty() {
None
} else {
Some(Self(bytes))
return Ok(None);
}
if bytes.len() != SIGNATURE_LENGTH {
return Err(Error::signature_invalid(format!(
"expected signature to be {} bytes long, but was {} bytes",
SIGNATURE_LENGTH,
bytes.len()
)));
}

Ok(Some(Self(bytes.to_vec())))
}

fn new_non_empty<B: AsRef<[u8]>>(bytes: B) -> Result<Self, Error> {
Self::new(bytes)?.ok_or_else(Error::empty_signature)
}

/// Return a reference to the underlying byte array
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl TryFrom<RawVote> for Vote {
timestamp: value.timestamp.map(|t| t.into()),
validator_address: value.validator_address.try_into()?,
validator_index: value.validator_index.try_into()?,
signature: Signature::new(value.signature),
signature: Signature::new(value.signature)?,
})
}
}
Expand Down
Loading