Skip to content

Commit 71783b9

Browse files
authored
elliptic-curve: PublicKey <-> SEC1 conversions (#1272)
Adds the following conversion impls to `PublicKey` - `TryFrom`: `sec1::{CompressedPoint`, `EncodedPoint}` - `Into` (via `From`): `sec1::{CompressedPoint`, `EncodedPoint}`
1 parent 947a4db commit 71783b9

File tree

1 file changed

+96
-3
lines changed

1 file changed

+96
-3
lines changed

elliptic-curve/src/public_key.rs

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::str::FromStr;
1919
use {
2020
crate::{
2121
point::PointCompression,
22-
sec1::{EncodedPoint, FromEncodedPoint, ModulusSize, ToEncodedPoint},
22+
sec1::{CompressedPoint, EncodedPoint, FromEncodedPoint, ModulusSize, ToEncodedPoint},
2323
Curve, FieldBytesSize,
2424
},
2525
core::cmp::Ordering,
@@ -143,8 +143,7 @@ where
143143
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
144144
FieldBytesSize<C>: ModulusSize,
145145
{
146-
let point = EncodedPoint::<C>::from(self);
147-
point.to_bytes()
146+
EncodedPoint::<C>::from(self).to_bytes()
148147
}
149148

150149
/// Borrow the inner [`AffinePoint`] from this [`PublicKey`].
@@ -250,6 +249,30 @@ where
250249
}
251250
}
252251

252+
#[cfg(feature = "sec1")]
253+
impl<C> From<PublicKey<C>> for CompressedPoint<C>
254+
where
255+
C: CurveArithmetic + PointCompression,
256+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
257+
FieldBytesSize<C>: ModulusSize,
258+
{
259+
fn from(public_key: PublicKey<C>) -> CompressedPoint<C> {
260+
CompressedPoint::<C>::from(&public_key)
261+
}
262+
}
263+
264+
#[cfg(feature = "sec1")]
265+
impl<C> From<&PublicKey<C>> for CompressedPoint<C>
266+
where
267+
C: CurveArithmetic + PointCompression,
268+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
269+
FieldBytesSize<C>: ModulusSize,
270+
{
271+
fn from(public_key: &PublicKey<C>) -> CompressedPoint<C> {
272+
CompressedPoint::<C>::clone_from_slice(public_key.to_encoded_point(true).as_bytes())
273+
}
274+
}
275+
253276
#[cfg(feature = "sec1")]
254277
impl<C> From<PublicKey<C>> for EncodedPoint<C>
255278
where
@@ -341,6 +364,62 @@ where
341364
}
342365
}
343366

367+
#[cfg(feature = "sec1")]
368+
impl<C> TryFrom<CompressedPoint<C>> for PublicKey<C>
369+
where
370+
C: CurveArithmetic,
371+
FieldBytesSize<C>: ModulusSize,
372+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
373+
{
374+
type Error = Error;
375+
376+
fn try_from(point: CompressedPoint<C>) -> Result<Self> {
377+
Self::from_sec1_bytes(&point)
378+
}
379+
}
380+
381+
#[cfg(feature = "sec1")]
382+
impl<C> TryFrom<&CompressedPoint<C>> for PublicKey<C>
383+
where
384+
C: CurveArithmetic,
385+
FieldBytesSize<C>: ModulusSize,
386+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
387+
{
388+
type Error = Error;
389+
390+
fn try_from(point: &CompressedPoint<C>) -> Result<Self> {
391+
Self::from_sec1_bytes(point)
392+
}
393+
}
394+
395+
#[cfg(feature = "sec1")]
396+
impl<C> TryFrom<EncodedPoint<C>> for PublicKey<C>
397+
where
398+
C: CurveArithmetic,
399+
FieldBytesSize<C>: ModulusSize,
400+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
401+
{
402+
type Error = Error;
403+
404+
fn try_from(point: EncodedPoint<C>) -> Result<Self> {
405+
Self::from_sec1_bytes(point.as_bytes())
406+
}
407+
}
408+
409+
#[cfg(feature = "sec1")]
410+
impl<C> TryFrom<&EncodedPoint<C>> for PublicKey<C>
411+
where
412+
C: CurveArithmetic,
413+
FieldBytesSize<C>: ModulusSize,
414+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
415+
{
416+
type Error = Error;
417+
418+
fn try_from(point: &EncodedPoint<C>) -> Result<Self> {
419+
Self::from_sec1_bytes(point.as_bytes())
420+
}
421+
}
422+
344423
#[cfg(all(feature = "pkcs8", feature = "sec1"))]
345424
impl<C> TryFrom<pkcs8::SubjectPublicKeyInfoRef<'_>> for PublicKey<C>
346425
where
@@ -351,6 +430,20 @@ where
351430
type Error = pkcs8::spki::Error;
352431

353432
fn try_from(spki: pkcs8::SubjectPublicKeyInfoRef<'_>) -> pkcs8::spki::Result<Self> {
433+
Self::try_from(&spki)
434+
}
435+
}
436+
437+
#[cfg(all(feature = "pkcs8", feature = "sec1"))]
438+
impl<C> TryFrom<&pkcs8::SubjectPublicKeyInfoRef<'_>> for PublicKey<C>
439+
where
440+
C: AssociatedOid + CurveArithmetic,
441+
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
442+
FieldBytesSize<C>: ModulusSize,
443+
{
444+
type Error = pkcs8::spki::Error;
445+
446+
fn try_from(spki: &pkcs8::SubjectPublicKeyInfoRef<'_>) -> pkcs8::spki::Result<Self> {
354447
spki.algorithm.assert_oids(ALGORITHM_OID, C::OID)?;
355448

356449
let public_key_bytes = spki

0 commit comments

Comments
 (0)