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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! ECDSA public keys: compressed or uncompressed Weierstrass elliptic
//! curve points.

#[cfg(feature = "encoding")]
use crate::encoding::Decode;
use crate::{
    ecdsa::curve::{
        point::{CompressedCurvePoint, UncompressedCurvePoint},
        WeierstrassCurve,
    },
    util::fmt_colon_delimited_hex,
    Error,
};
#[cfg(all(feature = "alloc", feature = "encoding"))]
use crate::{encoding::Encode, prelude::*};
use core::fmt::{self, Debug};
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
#[cfg(feature = "encoding")]
use subtle_encoding::Encoding;

/// ECDSA public keys
#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum PublicKey<C: WeierstrassCurve> {
    /// Compressed Weierstrass elliptic curve point
    Compressed(CompressedCurvePoint<C>),

    /// Uncompressed Weierstrass elliptic curve point
    Uncompressed(UncompressedCurvePoint<C>),
}

impl<C> PublicKey<C>
where
    C: WeierstrassCurve,
{
    /// Create an ECDSA public key from an elliptic curve point
    /// (compressed or uncompressed) encoded using the
    /// `Elliptic-Curve-Point-to-Octet-String` algorithm described in
    /// SEC 1: Elliptic Curve Cryptography (Version 2.0) section
    /// 2.3.3 (page 10).
    ///
    /// <http://www.secg.org/sec1-v2.pdf>
    pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Option<Self> {
        let slice = bytes.as_ref();
        let length = slice.len();

        if length == C::CompressedPointSize::to_usize() {
            let array = GenericArray::clone_from_slice(slice);
            let point = CompressedCurvePoint::from_bytes(array)?;
            Some(PublicKey::Compressed(point))
        } else if length == C::UncompressedPointSize::to_usize() {
            let array = GenericArray::clone_from_slice(slice);
            let point = UncompressedCurvePoint::from_bytes(array)?;
            Some(PublicKey::Uncompressed(point))
        } else {
            None
        }
    }

    /// Create an ECDSA public key from an compressed elliptic curve point
    /// encoded using the `Elliptic-Curve-Point-to-Octet-String` algorithm
    /// described in SEC 1: Elliptic Curve Cryptography (Version 2.0) section
    /// 2.3.3 (page 10).
    ///
    /// <http://www.secg.org/sec1-v2.pdf>
    pub fn from_compressed_point<B>(into_bytes: B) -> Option<Self>
    where
        B: Into<GenericArray<u8, C::CompressedPointSize>>,
    {
        CompressedCurvePoint::from_bytes(into_bytes).map(PublicKey::Compressed)
    }

    /// Create an ECDSA public key from a raw uncompressed point serialized
    /// as a bytestring, without a `0x04`-byte tag.
    ///
    /// This will be twice the modulus size, or 1-byte smaller than the
    /// `Elliptic-Curve-Point-to-Octet-String` encoding i.e
    /// with the leading `0x04` byte in that encoding removed.
    pub fn from_untagged_point(bytes: &GenericArray<u8, C::UntaggedPointSize>) -> Self {
        let mut tagged_bytes = GenericArray::default();
        tagged_bytes.as_mut_slice()[0] = 0x04;
        tagged_bytes.as_mut_slice()[1..].copy_from_slice(bytes.as_ref());

        PublicKey::Uncompressed(UncompressedCurvePoint::from_bytes(tagged_bytes).unwrap())
    }

    /// Obtain public key as a byte array reference
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            PublicKey::Compressed(ref point) => point.as_bytes(),
            PublicKey::Uncompressed(ref point) => point.as_bytes(),
        }
    }
}

impl<C> AsRef<[u8]> for PublicKey<C>
where
    C: WeierstrassCurve,
{
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl<C> Copy for PublicKey<C>
where
    C: WeierstrassCurve,
    <<C as WeierstrassCurve>::CompressedPointSize as ArrayLength<u8>>::ArrayType: Copy,
    <<C as WeierstrassCurve>::UncompressedPointSize as ArrayLength<u8>>::ArrayType: Copy,
{
}

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

#[cfg(feature = "encoding")]
impl<C> Decode for PublicKey<C>
where
    C: WeierstrassCurve,
{
    /// Decode an ECDSA public key from an elliptic curve point
    /// (compressed or uncompressed) encoded using given `Encoding`
    /// with the underlying bytes serialized using the
    /// `Elliptic-Curve-Point-to-Octet-String` algorithm described in
    /// SEC 1: Elliptic Curve Cryptography (Version 2.0) section
    /// 2.3.3 (page 10).
    ///
    /// <http://www.secg.org/sec1-v2.pdf>
    fn decode<E: Encoding>(encoded_signature: &[u8], encoding: &E) -> Result<Self, Error> {
        let mut array: GenericArray<u8, C::UncompressedPointSize> = GenericArray::default();

        let decoded_len = encoding
            .decode_to_slice(encoded_signature, array.as_mut_slice())
            .map_err(|_| Error::new())?;

        Self::from_bytes(&array.as_ref()[..decoded_len]).ok_or_else(Error::new)
    }
}

#[cfg(all(feature = "encoding", feature = "alloc"))]
impl<C> Encode for PublicKey<C>
where
    C: WeierstrassCurve,
{
    /// Encode this ECDSA public key (compressed or uncompressed) encoded
    /// using given `Encoding` with the underlying bytes serialized using the
    /// `Elliptic-Curve-Point-to-Octet-String` algorithm described in
    /// SEC 1: Elliptic Curve Cryptography (Version 2.0) section
    /// 2.3.3 (page 10).
    ///
    /// <http://www.secg.org/sec1-v2.pdf>
    fn encode<E: Encoding>(&self, encoding: &E) -> Vec<u8> {
        encoding.encode(self.as_ref())
    }
}

impl<C: WeierstrassCurve> crate::PublicKey for PublicKey<C> {}