-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jose-jwk: add support for
p521
(#62)
Adds support for the NIST P-521 elliptic curve using the `p521` crate, analogous to the existing support for `p256` and `p384`
- Loading branch information
Showing
8 changed files
with
218 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target/ | ||
**/*.rs.bk | ||
.idea/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ mod keyinfo; | |
mod kind; | ||
mod p256; | ||
mod p384; | ||
mod p521; | ||
mod rsa; | ||
|
||
pub use key::Key; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// SPDX-FileCopyrightText: 2022 Profian Inc. <[email protected]> | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
#![cfg(feature = "p521")] | ||
|
||
use p521::elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint}; | ||
use p521::{EncodedPoint, FieldBytes, PublicKey, SecretKey}; | ||
|
||
use jose_jwa::{Algorithm, Algorithm::Signing, Signing::*}; | ||
|
||
use super::Error; | ||
use super::KeyInfo; | ||
use crate::{Ec, EcCurves}; | ||
|
||
impl KeyInfo for PublicKey { | ||
fn strength(&self) -> usize { | ||
32 | ||
} | ||
|
||
fn is_supported(&self, algo: &Algorithm) -> bool { | ||
matches!(algo, Signing(Es512)) | ||
} | ||
} | ||
|
||
impl KeyInfo for SecretKey { | ||
fn strength(&self) -> usize { | ||
32 | ||
} | ||
|
||
fn is_supported(&self, algo: &Algorithm) -> bool { | ||
matches!(algo, Signing(Es512)) | ||
} | ||
} | ||
|
||
impl From<&PublicKey> for Ec { | ||
fn from(pk: &PublicKey) -> Self { | ||
let ep = pk.to_encoded_point(false); | ||
|
||
Self { | ||
crv: EcCurves::P521, | ||
x: ep.x().expect("unreachable").to_vec().into(), | ||
y: ep.y().expect("unreachable").to_vec().into(), | ||
d: None, | ||
} | ||
} | ||
} | ||
|
||
impl From<PublicKey> for Ec { | ||
fn from(sk: PublicKey) -> Self { | ||
(&sk).into() | ||
} | ||
} | ||
|
||
impl TryFrom<&Ec> for PublicKey { | ||
type Error = Error; | ||
|
||
fn try_from(value: &Ec) -> Result<Self, Self::Error> { | ||
if value.crv != EcCurves::P521 { | ||
return Err(Error::AlgMismatch); | ||
} | ||
|
||
let mut x = FieldBytes::default(); | ||
if value.x.len() != x.len() { | ||
return Err(Error::Invalid); | ||
} | ||
|
||
let mut y = FieldBytes::default(); | ||
if value.y.len() != y.len() { | ||
return Err(Error::Invalid); | ||
} | ||
|
||
x.copy_from_slice(&value.x); | ||
y.copy_from_slice(&value.y); | ||
|
||
let ep = EncodedPoint::from_affine_coordinates(&x, &y, false); | ||
Option::from(Self::from_encoded_point(&ep)).ok_or(Error::Invalid) | ||
} | ||
} | ||
|
||
impl TryFrom<Ec> for PublicKey { | ||
type Error = Error; | ||
|
||
fn try_from(value: Ec) -> Result<Self, Self::Error> { | ||
(&value).try_into() | ||
} | ||
} | ||
|
||
impl From<&SecretKey> for Ec { | ||
fn from(sk: &SecretKey) -> Self { | ||
let mut key: Self = sk.public_key().into(); | ||
key.d = Some(sk.to_bytes().to_vec().into()); | ||
key | ||
} | ||
} | ||
|
||
impl From<SecretKey> for Ec { | ||
fn from(sk: SecretKey) -> Self { | ||
(&sk).into() | ||
} | ||
} | ||
|
||
impl TryFrom<&Ec> for SecretKey { | ||
type Error = Error; | ||
|
||
fn try_from(value: &Ec) -> Result<Self, Self::Error> { | ||
if value.crv != EcCurves::P521 { | ||
return Err(Error::AlgMismatch); | ||
} | ||
|
||
if let Some(d) = value.d.as_ref() { | ||
return Self::from_slice(d).map_err(|_| Error::Invalid); | ||
} | ||
|
||
Err(Error::NotPrivate) | ||
} | ||
} | ||
|
||
impl TryFrom<Ec> for SecretKey { | ||
type Error = Error; | ||
|
||
fn try_from(value: Ec) -> Result<Self, Self::Error> { | ||
(&value).try_into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters