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

Bump the minor-changes group with 1 update #191

Merged
merged 3 commits into from
Dec 31, 2023
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
# Public dependencies (present in the public API).
anyhow = { version = "1.0.76", default-features = false }
anyhow = { version = "1.0.77", default-features = false }
base64ct = { version = "1.5.2", features = ["alloc"] }
ciborium = { version = "0.2.1", default-features = false, optional = true }
chrono = { version = "0.4.22", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/no-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https://github.com/slowli/jwt-compact"
publish = false

[dependencies]
anyhow = { version = "1.0.76", default-features = false }
anyhow = { version = "1.0.77", default-features = false }
chrono = { version = "0.4.22", default-features = false }
const-decoder = "0.3.0"
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }
Expand Down
3 changes: 2 additions & 1 deletion src/alg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub use self::p256::Es256;
#[cfg(feature = "rsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "rsa")))]
pub use self::rsa::{
ModulusBits, ModulusBitsError, Rsa, RsaParseError, RsaPrivateKey, RsaPublicKey, RsaSignature,
ModulusBits, ModulusBitsError, Rsa, RsaError, RsaParseError, RsaPrivateKey, RsaPublicKey,
RsaSignature,
};

/// Wrapper around keys allowing to enforce key strength requirements.
Expand Down
53 changes: 30 additions & 23 deletions src/alg/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::{fmt, str::FromStr};

use crate::{
alg::{SecretBytes, StrongKey, WeakKeyError},
alloc::{Box, Cow, String, ToOwned, Vec},
alloc::{Cow, String, ToOwned, Vec},
jwk::{JsonWebKey, JwkError, KeyType, RsaPrimeFactor, RsaPrivateParts},
Algorithm, AlgorithmSignature,
};
Expand Down Expand Up @@ -42,22 +42,29 @@ enum HashAlg {
}

impl HashAlg {
fn digest(self, message: &[u8]) -> Box<[u8]> {
fn digest(self, message: &[u8]) -> HashDigest {
match self {
Self::Sha256 => {
let digest: [u8; 32] = *(Sha256::digest(message).as_ref());
Box::new(digest)
}
Self::Sha384 => {
let mut digest = [0_u8; 48];
digest.copy_from_slice(Sha384::digest(message).as_ref());
Box::new(digest)
}
Self::Sha512 => {
let mut digest = [0_u8; 64];
digest.copy_from_slice(Sha512::digest(message).as_ref());
Box::new(digest)
}
Self::Sha256 => HashDigest::Sha256(Sha256::digest(message).into()),
Self::Sha384 => HashDigest::Sha384(Sha384::digest(message).into()),
Self::Sha512 => HashDigest::Sha512(Sha512::digest(message).into()),
}
}
}

/// Output of a [`HashAlg`].
#[derive(Debug)]
enum HashDigest {
Sha256([u8; 32]),
Sha384([u8; 48]),
Sha512([u8; 64]),
}

impl AsRef<[u8]> for HashDigest {
fn as_ref(&self) -> &[u8] {
match self {
Self::Sha256(bytes) => bytes,
Self::Sha384(bytes) => bytes,
Self::Sha512(bytes) => bytes,
}
}
}
Expand Down Expand Up @@ -173,12 +180,13 @@ impl Algorithm for Rsa {

fn sign(&self, signing_key: &Self::SigningKey, message: &[u8]) -> Self::Signature {
let digest = self.hash_alg.digest(message);
let digest = digest.as_ref();
let signing_result = match self.padding_scheme() {
PaddingScheme::Pkcs1v15(padding) => {
signing_key.sign_with_rng(&mut rand_core::OsRng, padding, &digest)
signing_key.sign_with_rng(&mut rand_core::OsRng, padding, digest)
}
PaddingScheme::Pss(padding) => {
signing_key.sign_with_rng(&mut rand_core::OsRng, padding, &digest)
signing_key.sign_with_rng(&mut rand_core::OsRng, padding, digest)
}
};
RsaSignature(signing_result.expect("Unexpected RSA signature failure"))
Expand All @@ -191,11 +199,10 @@ impl Algorithm for Rsa {
message: &[u8],
) -> bool {
let digest = self.hash_alg.digest(message);
let digest = digest.as_ref();
let verify_result = match self.padding_scheme() {
PaddingScheme::Pkcs1v15(padding) => {
verifying_key.verify(padding, &digest, &signature.0)
}
PaddingScheme::Pss(padding) => verifying_key.verify(padding, &digest, &signature.0),
PaddingScheme::Pkcs1v15(padding) => verifying_key.verify(padding, digest, &signature.0),
PaddingScheme::Pss(padding) => verifying_key.verify(padding, digest, &signature.0),
};
verify_result.is_ok()
}
Expand Down Expand Up @@ -389,7 +396,7 @@ impl<'a> From<&'a RsaPrivateKey> for JsonWebKey<'a> {
fn from(key: &'a RsaPrivateKey) -> JsonWebKey<'a> {
const MSG: &str = "RsaPrivateKey must have at least 2 prime factors";

let p = key.primes().get(0).expect(MSG);
let p = key.primes().first().expect(MSG);
let q = key.primes().get(1).expect(MSG);

let private_parts = RsaPrivateParts {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ mod alloc {

pub use std::{
borrow::{Cow, ToOwned},
boxed::Box,
format,
string::{String, ToString},
vec::Vec,
Expand Down