Skip to content
Snippets Groups Projects
Commit 1a6bc8fe authored by Éloïs's avatar Éloïs
Browse files

[fix] ed25519:pubkey don't have min size. empty pubkey must be supported

parent b1292d6b
No related branches found
No related tags found
1 merge request!6[fix] ed25519:pubkey don't have min size. empty pubkey must be supported
Pipeline #8236 passed
# Changelog # Changelog
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
...@@ -8,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -8,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] - ReleaseDate ## [Unreleased] - ReleaseDate
### Fixed
- ed25519: public key don't have min size. empty public key must be supported.
## [0.11.0] - 2020-02-29 ## [0.11.0] - 2020-02-29
### Changed ### Changed
......
...@@ -25,7 +25,12 @@ pub trait ToBase58 { ...@@ -25,7 +25,12 @@ pub trait ToBase58 {
/// Create an array of 32 bytes from a Base58 string. /// Create an array of 32 bytes from a Base58 string.
pub fn str_base58_to_32bytes(base58_data: &str) -> Result<([u8; 32], usize), BaseConvertionError> { pub fn str_base58_to_32bytes(base58_data: &str) -> Result<([u8; 32], usize), BaseConvertionError> {
match bs58::decode(base58_data).into_vec() { let mut source = base58_data;
while !source.is_empty() && &source[0..1] == "1" {
source = &source[1..];
}
match bs58::decode(source).into_vec() {
Ok(result) => { Ok(result) => {
let len = result.len(); let len = result.len();
if len <= 32 { if len <= 32 {
......
...@@ -45,8 +45,6 @@ use zeroize::Zeroize; ...@@ -45,8 +45,6 @@ use zeroize::Zeroize;
/// Maximal size of a public key in bytes /// Maximal size of a public key in bytes
pub const PUBKEY_SIZE_IN_BYTES: usize = 32; pub const PUBKEY_SIZE_IN_BYTES: usize = 32;
/// constl size of a public key in bytes
pub const PUBKEY_MIN_SIZE_IN_BYTES: usize = 31;
/// constf a signature in bytes /// constf a signature in bytes
pub const SIG_SIZE_IN_BYTES: usize = 64; pub const SIG_SIZE_IN_BYTES: usize = 64;
...@@ -168,7 +166,7 @@ impl Default for PublicKey { ...@@ -168,7 +166,7 @@ impl Default for PublicKey {
fn default() -> Self { fn default() -> Self {
PublicKey { PublicKey {
datas: [0u8; 32], datas: [0u8; 32],
len: 32, len: 0,
} }
} }
} }
...@@ -183,7 +181,7 @@ impl TryFrom<&[u8]> for PublicKey { ...@@ -183,7 +181,7 @@ impl TryFrom<&[u8]> for PublicKey {
type Error = PubkeyFromBytesError; type Error = PubkeyFromBytesError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> { fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if bytes.len() > PUBKEY_SIZE_IN_BYTES || bytes.len() < PUBKEY_MIN_SIZE_IN_BYTES { if bytes.len() > PUBKEY_SIZE_IN_BYTES {
Err(PubkeyFromBytesError::InvalidBytesLen { Err(PubkeyFromBytesError::InvalidBytesLen {
expected: PUBKEY_SIZE_IN_BYTES, expected: PUBKEY_SIZE_IN_BYTES,
found: bytes.len(), found: bytes.len(),
...@@ -224,14 +222,7 @@ impl super::PublicKey for PublicKey { ...@@ -224,14 +222,7 @@ impl super::PublicKey for PublicKey {
#[inline] #[inline]
fn from_base58(base58_data: &str) -> Result<Self, BaseConvertionError> { fn from_base58(base58_data: &str) -> Result<Self, BaseConvertionError> {
let (datas, len) = b58::str_base58_to_32bytes(base58_data)?; let (datas, len) = b58::str_base58_to_32bytes(base58_data)?;
if len < PUBKEY_MIN_SIZE_IN_BYTES { Ok(PublicKey { datas, len })
Err(BaseConvertionError::InvalidLength {
expected: PUBKEY_SIZE_IN_BYTES,
found: len,
})
} else {
Ok(PublicKey { datas, len })
}
} }
fn to_bytes_vector(&self) -> Vec<u8> { fn to_bytes_vector(&self) -> Vec<u8> {
...@@ -495,6 +486,12 @@ mod tests { ...@@ -495,6 +486,12 @@ mod tests {
); );
} }
#[test]
fn test_pubkey_111_from_base58() {
let public58 = "11111111111111111111111111111111111111111111";
let _ = unwrap!(super::PublicKey::from_base58(public58));
}
#[test] #[test]
fn base58_public_key() { fn base58_public_key() {
let public58 = "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"; let public58 = "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV";
......
...@@ -508,10 +508,7 @@ mod tests { ...@@ -508,10 +508,7 @@ mod tests {
#[test] #[test]
fn pubkey() { fn pubkey() {
let pubkey_default = PubKey::default(); let pubkey_default = PubKey::default();
let pubkey_bytes = [ let pubkey_bytes = [];
0u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
];
let pubkey = PubKey::Ed25519(unwrap!(ed25519::PublicKey::try_from(&pubkey_bytes[..]))); let pubkey = PubKey::Ed25519(unwrap!(ed25519::PublicKey::try_from(&pubkey_bytes[..])));
assert_eq!(pubkey_default, pubkey); assert_eq!(pubkey_default, pubkey);
...@@ -522,14 +519,14 @@ mod tests { ...@@ -522,14 +519,14 @@ mod tests {
); );
assert_eq!(pubkey.size_in_bytes(), ed25519::PUBKEY_SIZE_IN_BYTES + 3); assert_eq!(pubkey.size_in_bytes(), ed25519::PUBKEY_SIZE_IN_BYTES + 3);
assert_eq!(pubkey_str_b58, format!("{}", pubkey)); assert_eq!("", &format!("{}", pubkey));
assert_eq!(KeysAlgo::Ed25519, pubkey.algo()); assert_eq!(KeysAlgo::Ed25519, pubkey.algo());
assert_eq!(KeysAlgo::Schnorr, PubKey::Schnorr().algo()); assert_eq!(KeysAlgo::Schnorr, PubKey::Schnorr().algo());
assert_eq!(pubkey_bytes.to_vec(), pubkey.to_bytes_vector()); assert_eq!(pubkey_bytes.to_vec(), pubkey.to_bytes_vector());
assert_eq!(pubkey_str_b58, pubkey.to_base58()); assert_eq!("", &pubkey.to_base58());
assert_eq!( assert_eq!(
Err(SigError::InvalidSig), Err(SigError::InvalidSig),
...@@ -718,13 +715,6 @@ mod tests { ...@@ -718,13 +715,6 @@ mod tests {
#[test] #[test]
fn pubkey_from_bytes() { fn pubkey_from_bytes() {
assert_eq!(
Err(PubkeyFromBytesError::InvalidBytesLen {
expected: ed25519::PUBKEY_SIZE_IN_BYTES,
found: 2,
}),
PubKey::from_bytes(&[0u8, 1]),
);
assert_eq!( assert_eq!(
Err(PubkeyFromBytesError::InvalidBytesLen { Err(PubkeyFromBytesError::InvalidBytesLen {
expected: ed25519::PUBKEY_SIZE_IN_BYTES, expected: ed25519::PUBKEY_SIZE_IN_BYTES,
...@@ -737,7 +727,7 @@ mod tests { ...@@ -737,7 +727,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
Ok(PubKey::Ed25519(ed25519::PublicKey::default())), Ok(PubKey::Ed25519(ed25519::PublicKey::default())),
PubKey::from_bytes(&[0u8; 32]), PubKey::from_bytes(&[]),
); );
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment