Skip to content

Commit

Permalink
k256: impl pkcs8 traits for k256::ecdsa::{SigningKey, VerifyingKey} (
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri authored Dec 6, 2020
1 parent fec51f2 commit 3df19f4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
27 changes: 26 additions & 1 deletion k256/src/ecdsa/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ use elliptic_curve::{
#[cfg(any(feature = "keccak256", feature = "sha256"))]
use ecdsa_core::signature::{self, digest::Digest, PrehashSignature, RandomizedSigner};

#[cfg(feature = "pkcs8")]
use crate::pkcs8::{self, FromPrivateKey};

#[cfg(feature = "pem")]
use core::str::FromStr;

/// ECDSA/secp256k1 signing key
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
pub struct SigningKey {
Expand All @@ -35,7 +41,6 @@ impl SigningKey {
}

/// Initialize [`SigningKey`] from a raw scalar value (big endian).
// TODO(tarcieri): PKCS#8 support
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
SecretKey::from_bytes(bytes)
.map(|sk| Self { inner: sk })
Expand Down Expand Up @@ -232,6 +237,26 @@ impl RecoverableSignPrimitive<Secp256k1> for Scalar {
}
}

#[cfg(feature = "pkcs8")]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl FromPrivateKey for SigningKey {
fn from_pkcs8_private_key_info(
private_key_info: pkcs8::PrivateKeyInfo<'_>,
) -> pkcs8::Result<Self> {
SecretKey::from_pkcs8_private_key_info(private_key_info).map(|inner| Self { inner })
}
}

#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl FromStr for SigningKey {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Error> {
Self::from_pkcs8_pem(s).map_err(|_| Error::new())
}
}

#[cfg(test)]
mod tests {
use crate::{test_vectors::ecdsa::ECDSA_TEST_VECTORS, Secp256k1};
Expand Down
27 changes: 27 additions & 0 deletions k256/src/ecdsa/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ use signature::{digest::Digest, DigestVerifier};
#[cfg(feature = "sha256")]
use signature::PrehashSignature;

#[cfg(feature = "pkcs8")]
use crate::{
elliptic_curve::PublicKey,
pkcs8::{self, FromPublicKey},
};

#[cfg(feature = "pem")]
use core::str::FromStr;

/// ECDSA/secp256k1 verification key (i.e. public key)
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -122,6 +131,24 @@ impl VerifyPrimitive<Secp256k1> for AffinePoint {
}
}

#[cfg(feature = "pkcs8")]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl FromPublicKey for VerifyingKey {
fn from_spki(spki: pkcs8::SubjectPublicKeyInfo<'_>) -> pkcs8::Result<Self> {
PublicKey::from_spki(spki).map(|pk| Self { inner: pk.into() })
}
}

#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl FromStr for VerifyingKey {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Error> {
Self::from_public_key_pem(s).map_err(|_| Error::new())
}
}

#[cfg(test)]
mod tests {
use crate::{test_vectors::ecdsa::ECDSA_TEST_VECTORS, Secp256k1};
Expand Down

0 comments on commit 3df19f4

Please sign in to comment.