Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jose-jwt Add A key kind:Secp256k1 #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 27 additions & 7 deletions jose-jwk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,47 @@ RFC7517
"""
documentation = "https://docs.rs/jose-jwk"
repository = "https://github.com/RustCrypto/JOSE/tree/master/jose-jwk"
categories = ["cryptography", "data-structures", "encoding", "parser-implementations"]
categories = [
"cryptography",
"data-structures",
"encoding",
"parser-implementations",
]
keywords = ["json", "jose"]
readme = "README.md"
edition = "2021"
rust-version = "1.65"

[features]
default = ["crypto"]
crypto = ["p256", "p384", "rsa"]
crypto = ["p256", "p384", "rsa", "k256"]

[dependencies]
jose-b64 = { version = "0.1", default-features = false, features = ["secret"], path = "../jose-b64" }
jose-b64 = { version = "0.1", default-features = false, features = [
"secret",
], path = "../jose-b64" }
jose-jwa = { version = "0.1", path = "../jose-jwa" }
serde = { version = "1.0.185", default-features = false, features = ["alloc", "derive"] }
serde = { version = "1.0.185", default-features = false, features = [
"alloc",
"derive",
] }
zeroize = { version = "1.6.0", default-features = false, features = ["alloc"] }

# optional dependencies
p256 = { version = "0.13.2", default-features = false, optional = true, features = ["arithmetic"] }
p384 = { version = "0.13.0", default-features = false, optional = true, features = ["arithmetic"] }
p256 = { version = "0.13.2", default-features = false, optional = true, features = [
"arithmetic",
] }
p384 = { version = "0.13.0", default-features = false, optional = true, features = [
"arithmetic",
] }

k256 = { version = "0.13.1", default-features = false, optional = true, features = [
"default",
] }
rsa = { version = "0.9", default-features = false, optional = true }
url = { version = "2.4.1", default-features = false, optional = true, features = ["serde"] }
url = { version = "2.4.1", default-features = false, optional = true, features = [
"serde",
] }
Comment on lines +40 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the linewrapping changes in this file seem superfluous


[dev-dependencies]
serde_json = "1.0.96"
Expand Down
16 changes: 16 additions & 0 deletions jose-jwk/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub enum Key {
/// A P-384 key.
#[cfg(feature = "p384")]
P384(super::Kind<p384::PublicKey, p384::SecretKey>),

/// A Secp256k1 key.
#[cfg(feature = "k256")]
P256K(super::Kind<k256::PublicKey, k256::SecretKey>),
}

impl KeyInfo for Key {
Expand All @@ -49,6 +53,9 @@ impl KeyInfo for Key {

#[cfg(feature = "p384")]
Self::P384(k) => k.strength(),

#[cfg(feature = "k256")]
Self::P256K(k) => k.strength(),
}
}

Expand All @@ -64,6 +71,9 @@ impl KeyInfo for Key {

#[cfg(feature = "p384")]
Self::P384(k) => k.is_supported(algo),

#[cfg(feature = "k256")]
Self::P256K(k) => k.is_supported(algo),
}
}
}
Expand Down Expand Up @@ -211,6 +221,12 @@ impl From<&Key> for crate::Key {
super::Kind::Public(public) => Self::Ec(public.into()),
super::Kind::Secret(secret) => Self::Ec(secret.into()),
},

#[cfg(feature = "k256")]
Key::P256K(kind) => match kind {
super::Kind::Public(public) => Self::Ec(public.into()),
super::Kind::Secret(secret) => Self::Ec(secret.into()),
},
}
}
}
24 changes: 24 additions & 0 deletions jose-jwk/src/crypto/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,27 @@ impl TryFrom<&crate::Ec> for Kind<p384::PublicKey, p384::SecretKey> {
}
}
}


#[cfg(feature = "k256")]
impl From<&Kind<k256::PublicKey, k256::SecretKey>> for crate::Ec {
fn from(value: &Kind<k256::PublicKey, k256::SecretKey>) -> Self {
match value {
Kind::Public(key) => key.into(),
Kind::Secret(key) => key.into(),
}
}
}

#[cfg(feature = "k256")]
impl TryFrom<&crate::Ec> for Kind<k256::PublicKey, k256::SecretKey> {
type Error = super::Error;

fn try_from(value: &crate::Ec) -> Result<Self, Self::Error> {
if value.d.is_none() {
Ok(Kind::Public(value.try_into()?))
} else {
Ok(Kind::Secret(value.try_into()?))
}
}
}
1 change: 1 addition & 0 deletions jose-jwk/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod key;
mod keyinfo;
mod kind;
mod p256;
mod p256k;
mod p384;
mod rsa;

Expand Down
Loading