Skip to content
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
64 changes: 1 addition & 63 deletions biscuit-auth/src/crypto/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! The implementation is based on [ed25519_dalek](https://github.com/dalek-cryptography/ed25519-dalek).
#![allow(non_snake_case)]
use crate::{error::Format, format::schema};
use crate::error::Format;

use super::error;
use super::Signature;
Expand All @@ -30,10 +30,6 @@ pub struct KeyPair {
}

impl KeyPair {
pub fn new() -> Self {
Self::new_with_rng(&mut rand::rngs::OsRng)
}

pub fn new_with_rng<T: RngCore + CryptoRng>(rng: &mut T) -> Self {
let kp = ed25519_dalek::SigningKey::generate(rng);
KeyPair { kp }
Expand Down Expand Up @@ -76,10 +72,6 @@ impl KeyPair {
PublicKey(self.kp.verifying_key())
}

pub fn algorithm(&self) -> crate::format::schema::public_key::Algorithm {
crate::format::schema::public_key::Algorithm::Ed25519
}

#[cfg(feature = "pem")]
pub fn from_private_key_der(bytes: &[u8]) -> Result<Self, error::Format> {
let kp = SigningKey::from_pkcs8_der(bytes)
Expand Down Expand Up @@ -116,12 +108,6 @@ impl KeyPair {
}
}

impl std::default::Default for KeyPair {
fn default() -> Self {
Self::new()
}
}

/// the private part of a [KeyPair]
#[derive(Debug, PartialEq)]
pub struct PrivateKey(pub(crate) ed25519_dalek::SecretKey);
Expand All @@ -132,11 +118,6 @@ impl PrivateKey {
self.0.to_vec()
}

/// serializes to an hex-encoded string
pub fn to_bytes_hex(&self) -> String {
hex::encode(self.0)
}

/// deserializes from a byte array
pub fn from_bytes(bytes: &[u8]) -> Result<Self, error::Format> {
let bytes: [u8; 32] = bytes
Expand All @@ -145,12 +126,6 @@ impl PrivateKey {
Ok(PrivateKey(bytes))
}

/// deserializes from an hex-encoded string
pub fn from_bytes_hex(str: &str) -> Result<Self, error::Format> {
let bytes = hex::decode(str).map_err(|e| error::Format::InvalidKey(e.to_string()))?;
Self::from_bytes(&bytes)
}

#[cfg(feature = "pem")]
pub fn from_der(bytes: &[u8]) -> Result<Self, error::Format> {
let kp = SigningKey::from_pkcs8_der(bytes)
Expand Down Expand Up @@ -188,10 +163,6 @@ impl PrivateKey {
pub fn public(&self) -> PublicKey {
PublicKey(SigningKey::from_bytes(&self.0).verifying_key())
}

pub fn algorithm(&self) -> crate::format::schema::public_key::Algorithm {
crate::format::schema::public_key::Algorithm::Ed25519
}
}

impl std::clone::Clone for PrivateKey {
Expand All @@ -216,11 +187,6 @@ impl PublicKey {
self.0.to_bytes()
}

/// serializes to an hex-encoded string
pub fn to_bytes_hex(&self) -> String {
hex::encode(self.to_bytes())
}

/// deserializes from a byte array
pub fn from_bytes(bytes: &[u8]) -> Result<Self, error::Format> {
let bytes: [u8; 32] = bytes
Expand All @@ -233,30 +199,6 @@ impl PublicKey {
.map_err(Format::InvalidKey)
}

/// deserializes from an hex-encoded string
pub fn from_bytes_hex(str: &str) -> Result<Self, error::Format> {
let bytes = hex::decode(str).map_err(|e| error::Format::InvalidKey(e.to_string()))?;
Self::from_bytes(&bytes)
}

pub fn from_proto(key: &schema::PublicKey) -> Result<Self, error::Format> {
if key.algorithm != schema::public_key::Algorithm::Ed25519 as i32 {
return Err(error::Format::DeserializationError(format!(
"deserialization error: unexpected key algorithm {}",
key.algorithm
)));
}

PublicKey::from_bytes(&key.key)
}

pub fn to_proto(&self) -> schema::PublicKey {
schema::PublicKey {
algorithm: schema::public_key::Algorithm::Ed25519 as i32,
key: self.to_bytes().to_vec(),
}
}

pub fn verify_signature(
&self,
data: &[u8],
Expand All @@ -277,10 +219,6 @@ impl PublicKey {
.map_err(error::Format::Signature)
}

pub fn algorithm(&self) -> crate::format::schema::public_key::Algorithm {
crate::format::schema::public_key::Algorithm::Ed25519
}

#[cfg(feature = "pem")]
pub fn from_der(bytes: &[u8]) -> Result<Self, error::Format> {
use ed25519_dalek::pkcs8::DecodePublicKey;
Expand Down
50 changes: 6 additions & 44 deletions biscuit-auth/src/crypto/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/
#![allow(non_snake_case)]
use crate::{error::Format, format::schema};
use crate::error::Format;

use super::error;
use super::Signature;

use p256::ecdsa::{signature::Signer, signature::Verifier, SigningKey, VerifyingKey};
use p256::elliptic_curve::rand_core::{CryptoRng, OsRng, RngCore};
use p256::elliptic_curve::rand_core::{CryptoRng, RngCore};
use p256::NistP256;
use std::hash::Hash;

Expand All @@ -20,10 +20,6 @@ pub struct KeyPair {
}

impl KeyPair {
pub fn new() -> Self {
Self::new_with_rng(&mut OsRng)
}

pub fn new_with_rng<T: RngCore + CryptoRng>(rng: &mut T) -> Self {
let kp = SigningKey::random(rng);

Expand Down Expand Up @@ -66,10 +62,6 @@ impl KeyPair {
PublicKey(*self.kp.verifying_key())
}

pub fn algorithm(&self) -> crate::format::schema::public_key::Algorithm {
crate::format::schema::public_key::Algorithm::Secp256r1
}

#[cfg(feature = "pem")]
pub fn from_private_key_der(bytes: &[u8]) -> Result<Self, error::Format> {
use p256::pkcs8::DecodePrivateKey;
Expand Down Expand Up @@ -110,12 +102,6 @@ impl KeyPair {
}
}

impl std::default::Default for KeyPair {
fn default() -> Self {
Self::new()
}
}

/// the private part of a [KeyPair]
#[derive(Debug, PartialEq)]
pub struct PrivateKey(SigningKey);
Expand Down Expand Up @@ -194,10 +180,6 @@ impl PrivateKey {
pub fn public(&self) -> PublicKey {
PublicKey(*(&self.0).verifying_key())
}

pub fn algorithm(&self) -> crate::format::schema::public_key::Algorithm {
crate::format::schema::public_key::Algorithm::Ed25519
}
}

impl std::clone::Clone for PrivateKey {
Expand Down Expand Up @@ -275,24 +257,6 @@ impl PublicKey {
Ok(kp)
}

pub fn from_proto(key: &schema::PublicKey) -> Result<Self, error::Format> {
if key.algorithm != schema::public_key::Algorithm::Ed25519 as i32 {
return Err(error::Format::DeserializationError(format!(
"deserialization error: unexpected key algorithm {}",
key.algorithm
)));
}

PublicKey::from_bytes(&key.key)
}

pub fn to_proto(&self) -> schema::PublicKey {
schema::PublicKey {
algorithm: schema::public_key::Algorithm::Ed25519 as i32,
key: self.to_bytes().to_vec(),
}
}

pub fn verify_signature(
&self,
data: &[u8],
Expand All @@ -312,10 +276,6 @@ impl PublicKey {
.map_err(error::Format::Signature)
}

pub fn algorithm(&self) -> crate::format::schema::public_key::Algorithm {
crate::format::schema::public_key::Algorithm::Ed25519
}

pub(crate) fn write(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "secp256r1/{}", hex::encode(&self.to_bytes()))
}
Expand All @@ -326,18 +286,20 @@ impl PublicKey {

impl Hash for PublicKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
(crate::format::schema::public_key::Algorithm::Ed25519 as i32).hash(state);
(crate::format::schema::public_key::Algorithm::Secp256r1 as i32).hash(state);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

highlighting this as the actual bug fix

self.to_bytes().hash(state);
}
}

#[cfg(test)]
mod tests {
use p256::elliptic_curve::rand_core::OsRng;

use super::*;

#[test]
fn serialization() {
let kp = KeyPair::new();
let kp = KeyPair::new_with_rng(&mut OsRng);
let private = kp.private();
let public = kp.public();
let private_hex = private.to_bytes_hex();
Expand Down
Loading