1use crate::{
4 hazmat::{DigestPrimitive, FromDigest, VerifyPrimitive},
5 Error, Result, Signature, SignatureSize,
6};
7use core::{cmp::Ordering, convert::TryFrom, fmt::Debug, ops::Add};
8use elliptic_curve::{
9 consts::U1,
10 generic_array::ArrayLength,
11 sec1::{
12 EncodedPoint, FromEncodedPoint, ToEncodedPoint, UncompressedPointSize, UntaggedPointSize,
13 },
14 weierstrass::{Curve, PointCompression},
15 AffinePoint, FieldSize, ProjectiveArithmetic, PublicKey, Scalar,
16};
17use signature::{digest::Digest, DigestVerifier, Verifier};
18
19#[cfg(feature = "pkcs8")]
20use crate::elliptic_curve::{
21 pkcs8::{self, FromPublicKey},
22 AlgorithmParameters,
23};
24
25#[cfg(feature = "pem")]
26use core::str::FromStr;
27
28#[cfg_attr(docsrs, doc(cfg(feature = "verify")))]
33#[derive(Clone, Debug)]
34pub struct VerifyingKey<C>
35where
36 C: Curve + ProjectiveArithmetic,
37{
38 pub(crate) inner: PublicKey<C>,
39}
40
41impl<C> VerifyingKey<C>
42where
43 C: Curve + ProjectiveArithmetic,
44 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
45 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
46 UncompressedPointSize<C>: ArrayLength<u8>,
47{
48 pub fn from_sec1_bytes(bytes: &[u8]) -> Result<Self> {
50 PublicKey::from_sec1_bytes(bytes)
51 .map(|pk| Self { inner: pk })
52 .map_err(|_| Error::new())
53 }
54
55 pub fn from_encoded_point(public_key: &EncodedPoint<C>) -> Result<Self> {
57 PublicKey::<C>::from_encoded_point(public_key)
58 .map(|public_key| Self { inner: public_key })
59 .ok_or_else(Error::new)
60 }
61
62 pub fn to_encoded_point(&self, compress: bool) -> EncodedPoint<C> {
65 self.inner.to_encoded_point(compress)
66 }
67}
68
69impl<C> Copy for VerifyingKey<C> where C: Curve + ProjectiveArithmetic {}
70
71impl<C, D> DigestVerifier<D, Signature<C>> for VerifyingKey<C>
72where
73 C: Curve + ProjectiveArithmetic,
74 D: Digest<OutputSize = FieldSize<C>>,
75 AffinePoint<C>: VerifyPrimitive<C>,
76 Scalar<C>: FromDigest<C>,
77 SignatureSize<C>: ArrayLength<u8>,
78{
79 fn verify_digest(&self, digest: D, signature: &Signature<C>) -> Result<()> {
80 self.inner
81 .as_affine()
82 .verify_prehashed(&Scalar::<C>::from_digest(digest), signature)
83 }
84}
85
86impl<C> Verifier<Signature<C>> for VerifyingKey<C>
87where
88 C: Curve + ProjectiveArithmetic + DigestPrimitive,
89 C::Digest: Digest<OutputSize = FieldSize<C>>,
90 AffinePoint<C>: VerifyPrimitive<C>,
91 Scalar<C>: FromDigest<C>,
92 SignatureSize<C>: ArrayLength<u8>,
93{
94 fn verify(&self, msg: &[u8], signature: &Signature<C>) -> Result<()> {
95 self.verify_digest(C::Digest::new().chain(msg), signature)
96 }
97}
98
99impl<C> From<&VerifyingKey<C>> for EncodedPoint<C>
100where
101 C: Curve + ProjectiveArithmetic + PointCompression,
102 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
103 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
104 UncompressedPointSize<C>: ArrayLength<u8>,
105{
106 fn from(verifying_key: &VerifyingKey<C>) -> EncodedPoint<C> {
107 verifying_key.to_encoded_point(C::COMPRESS_POINTS)
108 }
109}
110
111impl<C> From<PublicKey<C>> for VerifyingKey<C>
112where
113 C: Curve + ProjectiveArithmetic,
114{
115 fn from(public_key: PublicKey<C>) -> VerifyingKey<C> {
116 VerifyingKey { inner: public_key }
117 }
118}
119
120impl<C> From<&PublicKey<C>> for VerifyingKey<C>
121where
122 C: Curve + ProjectiveArithmetic,
123{
124 fn from(public_key: &PublicKey<C>) -> VerifyingKey<C> {
125 public_key.clone().into()
126 }
127}
128
129impl<C> From<VerifyingKey<C>> for PublicKey<C>
130where
131 C: Curve + ProjectiveArithmetic,
132{
133 fn from(verifying_key: VerifyingKey<C>) -> PublicKey<C> {
134 verifying_key.inner
135 }
136}
137
138impl<C> From<&VerifyingKey<C>> for PublicKey<C>
139where
140 C: Curve + ProjectiveArithmetic,
141{
142 fn from(verifying_key: &VerifyingKey<C>) -> PublicKey<C> {
143 verifying_key.clone().into()
144 }
145}
146
147impl<C> Eq for VerifyingKey<C>
148where
149 C: Curve + ProjectiveArithmetic,
150 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
151 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
152 UncompressedPointSize<C>: ArrayLength<u8>,
153{
154}
155
156impl<C> PartialEq for VerifyingKey<C>
157where
158 C: Curve + ProjectiveArithmetic,
159 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
160 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
161 UncompressedPointSize<C>: ArrayLength<u8>,
162{
163 fn eq(&self, other: &Self) -> bool {
164 self.inner.eq(&other.inner)
165 }
166}
167
168impl<C> PartialOrd for VerifyingKey<C>
169where
170 C: Curve + ProjectiveArithmetic,
171 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
172 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
173 UncompressedPointSize<C>: ArrayLength<u8>,
174{
175 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
176 self.inner.partial_cmp(&other.inner)
177 }
178}
179
180impl<C> Ord for VerifyingKey<C>
181where
182 C: Curve + ProjectiveArithmetic,
183 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
184 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
185 UncompressedPointSize<C>: ArrayLength<u8>,
186{
187 fn cmp(&self, other: &Self) -> Ordering {
188 self.inner.cmp(&other.inner)
189 }
190}
191
192impl<C> TryFrom<&[u8]> for VerifyingKey<C>
193where
194 C: Curve + ProjectiveArithmetic,
195 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
196 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
197 UncompressedPointSize<C>: ArrayLength<u8>,
198{
199 type Error = Error;
200
201 fn try_from(bytes: &[u8]) -> Result<Self> {
202 Self::from_sec1_bytes(bytes)
203 }
204}
205
206#[cfg(feature = "pkcs8")]
207#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
208impl<C> FromPublicKey for VerifyingKey<C>
209where
210 C: Curve + AlgorithmParameters + ProjectiveArithmetic + PointCompression,
211 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
212 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
213 UncompressedPointSize<C>: ArrayLength<u8>,
214{
215 fn from_spki(spki: pkcs8::SubjectPublicKeyInfo<'_>) -> pkcs8::Result<Self> {
216 PublicKey::from_spki(spki).map(|inner| Self { inner })
217 }
218}
219
220#[cfg(feature = "pem")]
221#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
222impl<C> FromStr for VerifyingKey<C>
223where
224 C: Curve + AlgorithmParameters + ProjectiveArithmetic + PointCompression,
225 AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
226 UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
227 UncompressedPointSize<C>: ArrayLength<u8>,
228{
229 type Err = Error;
230
231 fn from_str(s: &str) -> Result<Self> {
232 Self::from_public_key_pem(s).map_err(|_| Error::new())
233 }
234}