Skip to content

Commit

Permalink
Move SignatureAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulstrackx authored and petreeftime committed Feb 2, 2022
1 parent 0bcb7e2 commit f2fac4b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 79 deletions.
76 changes: 75 additions & 1 deletion src/crypto/mod.rs
@@ -1,9 +1,12 @@
//! (Signing) cryptography abstraction

use crate::encrypt::COSEAlgorithm;
use crate::{error::CoseError, sign::SignatureAlgorithm};
use crate::error::CoseError;
use crate::header_map::HeaderMap;
#[cfg(feature = "openssl")]
use ::openssl::symm::Cipher;
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::str::FromStr;

#[cfg(feature = "openssl")]
mod openssl;
Expand Down Expand Up @@ -149,3 +152,74 @@ pub trait SigningPrivateKey: SigningPublicKey {
/// Given a digest, returns a signature
fn sign(&self, digest: &[u8]) -> Result<Vec<u8>, CoseError>;
}

/// Values from https://tools.ietf.org/html/rfc8152#section-8.1
#[derive(Debug, Copy, Clone, Serialize_repr, Deserialize_repr)]
#[repr(i8)]
pub enum SignatureAlgorithm {
/// ECDSA w/ SHA-256
ES256 = -7,
/// ECDSA w/ SHA-384
ES384 = -35,
/// ECDSA w/ SHA-512
ES512 = -36,
}

impl SignatureAlgorithm {
/// Key length of the given signature algorithm
pub fn key_length(&self) -> usize {
match self {
SignatureAlgorithm::ES256 => 32,
SignatureAlgorithm::ES384 => 48,
// Not a typo
SignatureAlgorithm::ES512 => 66,
}
}

/// Suggested cryptographic hash function given a signature algorithm
pub fn suggested_message_digest(&self) -> MessageDigest {
match self {
SignatureAlgorithm::ES256 => MessageDigest::Sha256,
SignatureAlgorithm::ES384 => MessageDigest::Sha384,
SignatureAlgorithm::ES512 => MessageDigest::Sha512,
}
}
}

impl FromStr for SignatureAlgorithm {
type Err = CoseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ES256" => Ok(SignatureAlgorithm::ES256),
"ES384" => Ok(SignatureAlgorithm::ES384),
"ES512" => Ok(SignatureAlgorithm::ES512),
name => Err(CoseError::UnsupportedError(format!(
"Algorithm '{}' is not supported",
name
))),
}
}
}

impl ToString for SignatureAlgorithm {
fn to_string(&self) -> String {
match self {
SignatureAlgorithm::ES256 => "ES256",
SignatureAlgorithm::ES384 => "ES384",
SignatureAlgorithm::ES512 => "ES512",
}
.to_string()
}
}

impl From<SignatureAlgorithm> for HeaderMap {
fn from(sig_alg: SignatureAlgorithm) -> Self {
// Convenience method for creating the map that would go into the signature structures
// Can be appended into a larger HeaderMap
// `1` is the index defined in the spec for Algorithm
let mut map = HeaderMap::new();
map.insert(1.into(), (sig_alg as i8).into());
map
}
}
3 changes: 1 addition & 2 deletions src/crypto/openssl_pkey.rs
Expand Up @@ -8,9 +8,8 @@ use openssl::{
};

use crate::{
crypto::{MessageDigest, SigningPrivateKey, SigningPublicKey},
crypto::{MessageDigest, SignatureAlgorithm, SigningPrivateKey, SigningPublicKey},
error::CoseError,
sign::SignatureAlgorithm,
};

/// Follows the recommandations put in place by the RFC and doesn't deal with potential
Expand Down
79 changes: 3 additions & 76 deletions src/sign.rs
@@ -1,90 +1,14 @@
//! COSE Signing

use std::str::FromStr;

use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
use serde_bytes::ByteBuf;
use serde_cbor::Error as CborError;
use serde_cbor::Value as CborValue;
use serde_repr::{Deserialize_repr, Serialize_repr};

#[cfg(feature = "openssl")]
use crate::crypto::MessageDigest;
use crate::crypto::{Hash, SigningPrivateKey, SigningPublicKey};
use crate::error::CoseError;
use crate::header_map::{map_to_empty_or_serialized, HeaderMap};

/// Values from https://tools.ietf.org/html/rfc8152#section-8.1
#[derive(Debug, Copy, Clone, Serialize_repr, Deserialize_repr)]
#[repr(i8)]
pub enum SignatureAlgorithm {
/// ECDSA w/ SHA-256
ES256 = -7,
/// ECDSA w/ SHA-384
ES384 = -35,
/// ECDSA w/ SHA-512
ES512 = -36,
}

impl SignatureAlgorithm {
#[cfg(feature = "openssl")]
pub(crate) fn key_length(&self) -> usize {
match self {
SignatureAlgorithm::ES256 => 32,
SignatureAlgorithm::ES384 => 48,
// Not a typo
SignatureAlgorithm::ES512 => 66,
}
}

#[cfg(feature = "openssl")]
pub(crate) fn suggested_message_digest(&self) -> MessageDigest {
match self {
SignatureAlgorithm::ES256 => MessageDigest::Sha256,
SignatureAlgorithm::ES384 => MessageDigest::Sha384,
SignatureAlgorithm::ES512 => MessageDigest::Sha512,
}
}
}

impl FromStr for SignatureAlgorithm {
type Err = CoseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ES256" => Ok(SignatureAlgorithm::ES256),
"ES384" => Ok(SignatureAlgorithm::ES384),
"ES512" => Ok(SignatureAlgorithm::ES512),
name => Err(CoseError::UnsupportedError(format!(
"Algorithm '{}' is not supported",
name
))),
}
}
}

impl ToString for SignatureAlgorithm {
fn to_string(&self) -> String {
match self {
SignatureAlgorithm::ES256 => "ES256",
SignatureAlgorithm::ES384 => "ES384",
SignatureAlgorithm::ES512 => "ES512",
}
.to_string()
}
}

impl From<SignatureAlgorithm> for HeaderMap {
fn from(sig_alg: SignatureAlgorithm) -> Self {
// Convenience method for creating the map that would go into the signature structures
// Can be appended into a larger HeaderMap
// `1` is the index defined in the spec for Algorithm
let mut map = HeaderMap::new();
map.insert(1.into(), (sig_alg as i8).into());
map
}
}

/// Implementation of the Sig_structure as defined in
/// [RFC8152](https://tools.ietf.org/html/rfc8152#section-4.4).
///
Expand Down Expand Up @@ -522,10 +446,12 @@ impl CoseSign1 {

#[cfg(test)]
mod tests {

// Public domain work: Pride and Prejudice by Jane Austen, taken from https://www.gutenberg.org/files/1342/1342.txt
const TEXT: &[u8] = b"It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.";

mod generic {
use crate::crypto::SignatureAlgorithm;
use crate::sign::*;

use super::TEXT;
Expand Down Expand Up @@ -639,6 +565,7 @@ mod tests {
#[cfg(feature = "key_openssl_pkey")]
mod openssl {
use crate::crypto::OpenSSL;
use crate::crypto::SignatureAlgorithm;
use crate::sign::*;
use openssl::pkey::{PKey, Private, Public};

Expand Down

0 comments on commit f2fac4b

Please sign in to comment.