-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
did-simple: added ed25519 pubkey type (#104)
Includes validation logic for the pubkey. Disclaimer, I consulted with a friend who is a security researcher on how to validate the pubkey. But any additional feedback is appreciated.
- Loading branch information
Showing
5 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use curve25519_dalek::edwards::CompressedEdwardsY; | ||
use ed25519_dalek::VerifyingKey; | ||
|
||
use crate::key_algos::StaticKeyAlgo as _; | ||
|
||
/// An ed25519 public key. | ||
#[allow(dead_code)] | ||
pub struct PubKey(VerifyingKey); | ||
|
||
impl PubKey { | ||
pub const LEN: usize = Self::key_len(); | ||
|
||
/// Instantiates `PubKey` from some bytes. Performs all necessary validation | ||
/// that the key is valid and of sufficient strength. | ||
/// | ||
/// Note that we will reject any keys that are too weak (aka low order). | ||
pub fn try_from(bytes: &[u8; Self::LEN]) -> Result<Self, TryFromBytesError> { | ||
let compressed_edwards = CompressedEdwardsY(bytes.to_owned()); | ||
let Some(edwards) = compressed_edwards.decompress() else { | ||
return Err(TryFromBytesError::NotOnCurve); | ||
}; | ||
let key = VerifyingKey::from(edwards); | ||
if key.is_weak() { | ||
return Err(TryFromBytesError::WeakKey); | ||
} | ||
Ok(Self(key)) | ||
} | ||
|
||
// TODO: Turn this into inline const when that feature stabilizes | ||
const fn key_len() -> usize { | ||
let len = crate::key_algos::Ed25519::PUB_KEY_LEN; | ||
assert!(len == ed25519_dalek::PUBLIC_KEY_LENGTH); | ||
len | ||
} | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum TryFromBytesError { | ||
#[error( | ||
"the provided bytes was not the y coordinate of a valid point on the curve" | ||
)] | ||
NotOnCurve, | ||
#[error("public key has a low order and is too weak, which would allow the key to generate signatures that work for almost any message. To prevent this, we reject weak keys.")] | ||
WeakKey, | ||
} | ||
|
||
/// Errors which may occur while processing signatures and keypairs. | ||
#[derive(thiserror::Error, Debug)] | ||
#[error("invalid signature")] | ||
pub struct SignatureError(#[from] ed25519_dalek::SignatureError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//! Implementations of cryptographic operations | ||
|
||
#[cfg(feature = "ed25519")] | ||
pub mod ed25519; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
use std::str::FromStr; | ||
|
||
pub mod crypto; | ||
pub(crate) mod key_algos; | ||
pub mod methods; | ||
pub mod url; | ||
|