1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! Fixed-size, compact ECDSA signatures (as used in e.g. PKCS#11)

use core::fmt::{self, Debug};
use core::marker::PhantomData;
use generic_array::{typenum::Unsigned, GenericArray};
#[cfg(feature = "encoding")]
use subtle_encoding::Encoding;

use curve::WeierstrassCurve;
use ecdsa;
#[cfg(feature = "encoding")]
use encoding::Decode;
#[cfg(all(feature = "alloc", feature = "encoding"))]
use encoding::Encode;
use error::Error;
#[allow(unused_imports)]
use prelude::*;
use util::fmt_colon_delimited_hex;

/// ECDSA signatures serialized in a compact, fixed-sized form
#[derive(Clone, PartialEq, Eq)]
pub struct FixedSignature<C: WeierstrassCurve> {
    /// Signature data as bytes
    bytes: GenericArray<u8, C::FixedSignatureSize>,

    /// Placeholder for elliptic curve type
    curve: PhantomData<C>,
}

impl<C> ::Signature for FixedSignature<C>
where
    C: WeierstrassCurve,
{
    /// Create an ECDSA signature from its serialized byte representation
    fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Result<Self, Error> {
        ensure!(
            bytes.as_ref().len() == C::FixedSignatureSize::to_usize(),
            SignatureInvalid,
            "expected {}-byte signature (got {})",
            C::FixedSignatureSize::to_usize(),
            bytes.as_ref().len()
        );

        Ok(Self::from(GenericArray::clone_from_slice(bytes.as_ref())))
    }
}

impl<C: WeierstrassCurve> ecdsa::Signature for FixedSignature<C> {}

impl<C> FixedSignature<C>
where
    C: WeierstrassCurve,
{
    /// Convert signature into owned byte array
    #[inline]
    pub fn into_bytes(self) -> GenericArray<u8, C::FixedSignatureSize> {
        self.bytes
    }
}

impl<C> AsRef<[u8]> for FixedSignature<C>
where
    C: WeierstrassCurve,
{
    fn as_ref(&self) -> &[u8] {
        self.bytes.as_slice()
    }
}

impl<C> Debug for FixedSignature<C>
where
    C: WeierstrassCurve,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "signatory::ecdsa::FixedSignature<{:?}>(", C::default())?;
        fmt_colon_delimited_hex(f, self.as_ref())?;
        write!(f, ")")
    }
}

#[cfg(feature = "encoding")]
impl<C> Decode for FixedSignature<C>
where
    C: WeierstrassCurve,
{
    /// Decode an ASN.1 encoded ECDSA signature from a byte slice with the
    /// given encoding (e.g. hex, Base64)
    fn decode<E: Encoding>(encoded_signature: &[u8], encoding: &E) -> Result<Self, Error> {
        let mut array = GenericArray::default();
        let decoded_len = encoding.decode_to_slice(encoded_signature, array.as_mut_slice())?;

        ensure!(
            decoded_len == C::FixedSignatureSize::to_usize(),
            SignatureInvalid,
            "expected {}-byte signature (got {})",
            C::FixedSignatureSize::to_usize(),
            decoded_len
        );

        Ok(Self::from(array))
    }
}

#[cfg(all(feature = "encoding", feature = "alloc"))]
impl<C> Encode for FixedSignature<C>
where
    C: WeierstrassCurve,
{
    /// Encode an ASN.1 encoded ECDSA signature with the given encoding
    /// (e.g. hex, Base64)
    fn encode<E: Encoding>(&self, encoding: &E) -> Vec<u8> {
        encoding.encode(self.as_ref())
    }
}

impl<C> From<GenericArray<u8, C::FixedSignatureSize>> for FixedSignature<C>
where
    C: WeierstrassCurve,
{
    fn from(bytes: GenericArray<u8, C::FixedSignatureSize>) -> Self {
        Self {
            bytes,
            curve: PhantomData,
        }
    }
}