Skip to content
Permalink
Browse files

split AsymmetricKey requirement for static secret key size

  • Loading branch information...
vincenthz committed May 13, 2019
1 parent a7cade2 commit 25baf22179d4b3a481bbceb3d09069eed6574052
@@ -1,4 +1,4 @@
use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError};
use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError, SecretKeySizeStatic};
use crate::sign::{SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm};
use cryptoxide::ed25519;
use rand::{CryptoRng, RngCore};
@@ -50,7 +50,6 @@ impl AsymmetricKey for Ed25519 {
const SECRET_BECH32_HRP: &'static str = "ed25519_sk";
const PUBLIC_BECH32_HRP: &'static str = "ed25519_pk";

const SECRET_KEY_SIZE: usize = ed25519::SEED_LENGTH;
const PUBLIC_KEY_SIZE: usize = ed25519::PUBLIC_KEY_LENGTH;

fn generate<T: RngCore + CryptoRng>(mut rng: T) -> Self::Secret {
@@ -82,6 +81,10 @@ impl AsymmetricKey for Ed25519 {
}
}

impl SecretKeySizeStatic for Ed25519 {
const SECRET_KEY_SIZE: usize = ed25519::SEED_LENGTH;
}

impl VerificationAlgorithm for Ed25519 {
type Signature = Sig;

@@ -121,12 +124,12 @@ mod test {
use crate::sign::test::{keypair_signing_ko, keypair_signing_ok};

#[quickcheck]
fn sign_ok(input: (KeyPair<Ed25519>, Vec<u8>)) -> bool {
keypair_signing_ok(input)
}
fn sign_ok(input: (KeyPair<Ed25519>, Vec<u8>)) -> bool {
keypair_signing_ok(input)
}

#[quickcheck]
fn sign_ko(input: (KeyPair<Ed25519>, PublicKey<Ed25519>, Vec<u8>)) -> bool {
keypair_signing_ko(input)
}
fn sign_ko(input: (KeyPair<Ed25519>, PublicKey<Ed25519>, Vec<u8>)) -> bool {
keypair_signing_ko(input)
}
}
@@ -1,4 +1,4 @@
use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError};
use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError, SecretKeySizeStatic};
use crate::sign::{SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm};

use ed25519_bip32 as i;
@@ -33,7 +33,6 @@ impl AsymmetricKey for Ed25519Bip32 {
const SECRET_BECH32_HRP: &'static str = "xprv";
const PUBLIC_BECH32_HRP: &'static str = "xpub";

const SECRET_KEY_SIZE: usize = XPRV_SIZE;
const PUBLIC_KEY_SIZE: usize = XPUB_SIZE;

fn generate<T: RngCore + CryptoRng>(mut rng: T) -> Self::Secret {
@@ -56,6 +55,10 @@ impl AsymmetricKey for Ed25519Bip32 {
}
}

impl SecretKeySizeStatic for Ed25519Bip32 {
const SECRET_KEY_SIZE: usize = XPRV_SIZE;
}

impl From<i::SignatureError> for SignatureError {
fn from(v: i::SignatureError) -> Self {
match v {
@@ -100,11 +103,11 @@ mod test {
use crate::sign::test::{keypair_signing_ko, keypair_signing_ok};

#[quickcheck]
fn sign_ok(input: (KeyPair<Ed25519Bip32>, Vec<u8>)) -> bool {
keypair_signing_ok(input)
}
fn sign_ok(input: (KeyPair<Ed25519Bip32>, Vec<u8>)) -> bool {
keypair_signing_ok(input)
}
#[quickcheck]
fn sign_ko(input: (KeyPair<Ed25519Bip32>, PublicKey<Ed25519Bip32>, Vec<u8>)) -> bool {
keypair_signing_ko(input)
}
fn sign_ko(input: (KeyPair<Ed25519Bip32>, PublicKey<Ed25519Bip32>, Vec<u8>)) -> bool {
keypair_signing_ko(input)
}
}
@@ -1,4 +1,4 @@
use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError};
use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError, SecretKeySizeStatic};
use crate::sign::{SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm};

use super::ed25519 as ei;
@@ -35,7 +35,6 @@ impl AsymmetricKey for Ed25519Extended {
const SECRET_BECH32_HRP: &'static str = "ed25519e_sk";
const PUBLIC_BECH32_HRP: &'static str = "ed25519e_pk";

const SECRET_KEY_SIZE: usize = ed25519::PRIVATE_KEY_LENGTH;
const PUBLIC_KEY_SIZE: usize = ed25519::PUBLIC_KEY_LENGTH;

fn generate<T: RngCore + CryptoRng>(mut rng: T) -> Self::Secret {
@@ -72,6 +71,10 @@ impl AsymmetricKey for Ed25519Extended {
}
}

impl SecretKeySizeStatic for Ed25519Extended {
const SECRET_KEY_SIZE: usize = ed25519::PRIVATE_KEY_LENGTH;
}

impl VerificationAlgorithm for Ed25519Extended {
type Signature = ei::Sig;

@@ -110,9 +113,9 @@ mod test {
use crate::sign::test::{keypair_signing_ko, keypair_signing_ok};

#[quickcheck]
fn sign_ok(input: (KeyPair<Ed25519Extended>, Vec<u8>)) -> bool {
keypair_signing_ok(input)
}
fn sign_ok(input: (KeyPair<Ed25519Extended>, Vec<u8>)) -> bool {
keypair_signing_ok(input)
}

#[quickcheck]
fn sign_ko(
@@ -122,6 +125,6 @@ mod test {
Vec<u8>,
),
) -> bool {
keypair_signing_ko(input)
}
keypair_signing_ko(input)
}
}
@@ -41,7 +41,6 @@ impl AsymmetricKey for FakeMMM {
const SECRET_BECH32_HRP: &'static str = "fakemmm_sk";
const PUBLIC_BECH32_HRP: &'static str = "fakemmm_pk";

const SECRET_KEY_SIZE: usize = ed25519::SEED_LENGTH;
const PUBLIC_KEY_SIZE: usize = ed25519::PUBLIC_KEY_LENGTH;

fn generate<T: RngCore + CryptoRng>(mut rng: T) -> Priv {
@@ -1,7 +1,7 @@
mod dleq;
pub mod vrf;

use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError};
use crate::key::{AsymmetricKey, PublicKeyError, SecretKeyError, SecretKeySizeStatic};
use crate::vrf::{VRFVerification, VerifiableRandomFunction};
use rand::{CryptoRng, RngCore};

@@ -15,7 +15,6 @@ impl AsymmetricKey for Curve25519_2HashDH {
const SECRET_BECH32_HRP: &'static str = "vrf_sk";
const PUBLIC_BECH32_HRP: &'static str = "vrf_pk";

const SECRET_KEY_SIZE: usize = vrf::SECRET_SIZE;
const PUBLIC_KEY_SIZE: usize = vrf::PUBLIC_SIZE;

fn generate<T: RngCore + CryptoRng>(rng: T) -> Self::Secret {
@@ -42,6 +41,10 @@ impl AsymmetricKey for Curve25519_2HashDH {
}
}

impl SecretKeySizeStatic for Curve25519_2HashDH {
const SECRET_KEY_SIZE: usize = vrf::SECRET_SIZE;
}

impl VerifiableRandomFunction for Curve25519_2HashDH {
type VerifiedRandomOutput = vrf::ProvenOutputSeed;
type RandomOutput = vrf::OutputSeed;
@@ -23,7 +23,6 @@ pub trait AsymmetricKey {
const SECRET_BECH32_HRP: &'static str;
const PUBLIC_BECH32_HRP: &'static str;

const SECRET_KEY_SIZE: usize;
const PUBLIC_KEY_SIZE: usize;

fn generate<T: RngCore + CryptoRng>(rng: T) -> Self::Secret;
@@ -34,6 +33,10 @@ pub trait AsymmetricKey {
fn public_from_binary(data: &[u8]) -> Result<Self::Public, PublicKeyError>;
}

pub trait SecretKeySizeStatic: AsymmetricKey {
const SECRET_KEY_SIZE: usize;
}

pub struct SecretKey<A: AsymmetricKey>(pub(crate) A::Secret);

pub struct PublicKey<A: AsymmetricKey>(pub(crate) A::Public);

0 comments on commit 25baf22

Please sign in to comment.
You can’t perform that action at this time.