Skip to content

Commit b55df81

Browse files
committed
Cargo fmt
1 parent bf4b07b commit b55df81

File tree

14 files changed

+127
-264
lines changed

14 files changed

+127
-264
lines changed

src/errors.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Errors returned will be converted to one of the structs in this module.
2-
use std::{error, fmt, io, str, string};
32
use data_encoding;
43
use ring;
54
use serde_json;
5+
use std::{error, fmt, io, str, string};
66
use url::ParseError;
77

88
#[derive(Debug)]
@@ -71,11 +71,13 @@ pub enum ValidationError {
7171
}
7272

7373
macro_rules! impl_from_error {
74-
($f: ty, $e: expr) => {
74+
($f:ty, $e:expr) => {
7575
impl From<$f> for Error {
76-
fn from(f: $f) -> Error { $e(f) }
76+
fn from(f: $f) -> Error {
77+
$e(f)
78+
}
7779
}
78-
}
80+
};
7981
}
8082

8183
impl_from_error!(String, Error::GenericError);
@@ -189,8 +191,8 @@ impl error::Error for ValidationError {
189191

190192
impl fmt::Display for ValidationError {
191193
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
192-
use ValidationError::*;
193194
use std::error::Error;
195+
use ValidationError::*;
194196

195197
match *self {
196198
MissingRequired(ref field) => write!(f, "{} is required but is missing", field),

src/jwa.rs

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
//! Typically, you will not use these directly, but as part of a JWS or JWE.
66
use std::fmt;
77

8-
use ring::{aead, digest, hmac, rand, signature};
98
use ring::constant_time::verify_slices_are_equal;
109
use ring::rand::SystemRandom;
11-
use serde::Serialize;
10+
use ring::{aead, digest, hmac, rand, signature};
1211
use serde::de::DeserializeOwned;
12+
use serde::Serialize;
1313
use untrusted;
1414

1515
use errors::Error;
@@ -25,8 +25,9 @@ const AES_GCM_NONCE_LENGTH: usize = 96 / 8;
2525

2626
/// A zeroed AES GCM Nonce EncryptionOptions
2727
lazy_static! {
28-
static ref AES_GCM_ZEROED_NONCE: EncryptionOptions =
29-
EncryptionOptions::AES_GCM { nonce: vec![0; AES_GCM_NONCE_LENGTH] };
28+
static ref AES_GCM_ZEROED_NONCE: EncryptionOptions = EncryptionOptions::AES_GCM {
29+
nonce: vec![0; AES_GCM_NONCE_LENGTH],
30+
};
3031
}
3132

3233
/// A default `None` `EncryptionOptions`
@@ -472,10 +473,7 @@ impl KeyManagementAlgorithm {
472473
A128GCMKW | A192GCMKW | A256GCMKW => self.aes_gcm_encrypt(payload, key, options),
473474
DirectSymmetricKey => match *options {
474475
EncryptionOptions::None => Ok(Default::default()),
475-
ref other => Err(unexpected_encryption_options_error!(
476-
EncryptionOptions::None,
477-
other
478-
)),
476+
ref other => Err(unexpected_encryption_options_error!(EncryptionOptions::None, other)),
479477
},
480478
_ => Err(Error::UnsupportedOperation),
481479
}
@@ -513,10 +511,7 @@ impl KeyManagementAlgorithm {
513511

514512
let nonce = match *options {
515513
EncryptionOptions::AES_GCM { ref nonce } => Ok(nonce),
516-
ref others => Err(unexpected_encryption_options_error!(
517-
AES_GCM_ZEROED_NONCE,
518-
others
519-
)),
514+
ref others => Err(unexpected_encryption_options_error!(AES_GCM_ZEROED_NONCE, others)),
520515
}?;
521516
// FIXME: Should we check the nonce length here or leave it to ring?
522517

@@ -627,10 +622,7 @@ impl ContentEncryptionAlgorithm {
627622

628623
let nonce = match *options {
629624
EncryptionOptions::AES_GCM { ref nonce } => Ok(nonce),
630-
ref others => Err(unexpected_encryption_options_error!(
631-
AES_GCM_ZEROED_NONCE,
632-
others
633-
)),
625+
ref others => Err(unexpected_encryption_options_error!(AES_GCM_ZEROED_NONCE, others)),
634626
}?;
635627
// FIXME: Should we check the nonce length here or leave it to ring?
636628

@@ -730,8 +722,8 @@ mod tests {
730722
use ring::constant_time::verify_slices_are_equal;
731723

732724
use super::*;
733-
use CompactPart;
734725
use jwa;
726+
use CompactPart;
735727

736728
#[test]
737729
fn sign_and_verify_none() {
@@ -740,22 +732,17 @@ mod tests {
740732
not_err!(SignatureAlgorithm::None.sign("payload".to_string().as_bytes(), &Secret::None,));
741733
assert_eq!(expected_signature, actual_signature);
742734

743-
not_err!(SignatureAlgorithm::None.verify(
744-
vec![].as_slice(),
745-
"payload".to_string().as_bytes(),
746-
&Secret::None,
747-
));
735+
not_err!(SignatureAlgorithm::None.verify(vec![].as_slice(), "payload".to_string().as_bytes(), &Secret::None));
748736
}
749737

750738
#[test]
751739
fn sign_and_verify_hs256() {
752740
let expected_base64 = "uC_LeRrOxXhZuYm0MKgmSIzi5Hn9-SMmvQoug3WkK6Q";
753741
let expected_bytes: Vec<u8> = not_err!(CompactPart::from_base64(&expected_base64));
754742

755-
let actual_signature = not_err!(SignatureAlgorithm::HS256.sign(
756-
"payload".to_string().as_bytes(),
757-
&Secret::bytes_from_str("secret"),
758-
));
743+
let actual_signature = not_err!(
744+
SignatureAlgorithm::HS256.sign("payload".to_string().as_bytes(), &Secret::bytes_from_str("secret"),)
745+
);
759746
assert_eq!(&*not_err!(actual_signature.to_base64()), expected_base64);
760747

761748
not_err!(SignatureAlgorithm::HS256.verify(
@@ -789,11 +776,7 @@ mod tests {
789776
assert_eq!(&*not_err!(actual_signature.to_base64()), expected_signature);
790777

791778
let public_key = Secret::public_key_from_file("test/fixtures/rsa_public_key.der").unwrap();
792-
not_err!(SignatureAlgorithm::RS256.verify(
793-
expected_signature_bytes.as_slice(),
794-
payload_bytes,
795-
&public_key,
796-
));
779+
not_err!(SignatureAlgorithm::RS256.verify(expected_signature_bytes.as_slice(), payload_bytes, &public_key,));
797780
}
798781

799782
/// This signature is non-deterministic.
@@ -836,18 +819,15 @@ mod tests {
836819
/// This signature is non-deterministic.
837820
#[test]
838821
fn sign_and_verify_es256_round_trip() {
839-
let private_key = Secret::ecdsa_keypair_from_file(SignatureAlgorithm::ES256, "test/fixtures/ecdsa_private_key.p8").unwrap();
822+
let private_key =
823+
Secret::ecdsa_keypair_from_file(SignatureAlgorithm::ES256, "test/fixtures/ecdsa_private_key.p8").unwrap();
840824
let payload = "payload".to_string();
841825
let payload_bytes = payload.as_bytes();
842826

843827
let actual_signature = not_err!(SignatureAlgorithm::ES256.sign(payload_bytes, &private_key));
844828

845829
let public_key = Secret::public_key_from_file("test/fixtures/ecdsa_public_key.der").unwrap();
846-
not_err!(SignatureAlgorithm::ES256.verify(
847-
actual_signature.as_slice(),
848-
payload_bytes,
849-
&public_key,
850-
));
830+
not_err!(SignatureAlgorithm::ES256.verify(actual_signature.as_slice(), payload_bytes, &public_key,));
851831
}
852832

853833
/// Test case from https://github.com/briansmith/ring/blob/a13b8e2/src/ec/suite_b/ecdsa_verify_fixed_tests.txt
@@ -899,11 +879,7 @@ mod tests {
899879
let invalid_signature = "broken".to_string();
900880
let signature_bytes = invalid_signature.as_bytes();
901881
let _ = SignatureAlgorithm::None
902-
.verify(
903-
signature_bytes,
904-
"payload".to_string().as_bytes(),
905-
&Secret::None,
906-
)
882+
.verify(signature_bytes, "payload".to_string().as_bytes(), &Secret::None)
907883
.unwrap();
908884
}
909885

@@ -928,11 +904,7 @@ mod tests {
928904
let invalid_signature = "broken".to_string();
929905
let signature_bytes = invalid_signature.as_bytes();
930906
let _ = SignatureAlgorithm::RS256
931-
.verify(
932-
signature_bytes,
933-
"payload".to_string().as_bytes(),
934-
&public_key,
935-
)
907+
.verify(signature_bytes, "payload".to_string().as_bytes(), &public_key)
936908
.unwrap();
937909
}
938910

@@ -943,11 +915,7 @@ mod tests {
943915
let invalid_signature = "broken".to_string();
944916
let signature_bytes = invalid_signature.as_bytes();
945917
let _ = SignatureAlgorithm::PS256
946-
.verify(
947-
signature_bytes,
948-
"payload".to_string().as_bytes(),
949-
&public_key,
950-
)
918+
.verify(signature_bytes, "payload".to_string().as_bytes(), &public_key)
951919
.unwrap();
952920
}
953921

@@ -958,11 +926,7 @@ mod tests {
958926
let invalid_signature = "broken".to_string();
959927
let signature_bytes = invalid_signature.as_bytes();
960928
let _ = SignatureAlgorithm::ES256
961-
.verify(
962-
signature_bytes,
963-
"payload".to_string().as_bytes(),
964-
&public_key,
965-
)
929+
.verify(signature_bytes, "payload".to_string().as_bytes(), &public_key)
966930
.unwrap();
967931
}
968932

@@ -1125,12 +1089,7 @@ mod tests {
11251089
let encrypted_cek = not_err!(cek_alg.wrap_key(cek.octect_key().unwrap(), &key, &options));
11261090
let decrypted_cek = not_err!(cek_alg.unwrap_key(&encrypted_cek, enc_alg, &key));
11271091

1128-
assert!(
1129-
verify_slices_are_equal(
1130-
cek.octect_key().unwrap(),
1131-
decrypted_cek.octect_key().unwrap(),
1132-
).is_ok()
1133-
);
1092+
assert!(verify_slices_are_equal(cek.octect_key().unwrap(), decrypted_cek.octect_key().unwrap(),).is_ok());
11341093
}
11351094

11361095
#[test]
@@ -1158,12 +1117,7 @@ mod tests {
11581117
let encrypted_cek = not_err!(cek_alg.wrap_key(cek.octect_key().unwrap(), &key, &options));
11591118
let decrypted_cek = not_err!(cek_alg.unwrap_key(&encrypted_cek, enc_alg, &key));
11601119

1161-
assert!(
1162-
verify_slices_are_equal(
1163-
cek.octect_key().unwrap(),
1164-
decrypted_cek.octect_key().unwrap(),
1165-
).is_ok()
1166-
);
1120+
assert!(verify_slices_are_equal(cek.octect_key().unwrap(), decrypted_cek.octect_key().unwrap(),).is_ok());
11671121
}
11681122

11691123
/// `ContentEncryptionAlgorithm::A128GCM` generates CEK of the right length

src/jwe.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
//! you will want to look at the [`Compact`](enum.Compact.html) enum.
66
use std::fmt;
77

8-
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
98
use serde::de::{self, DeserializeOwned};
9+
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
1010
use serde_json;
1111

12-
use {CompactJson, CompactPart, Empty};
1312
use errors::{Error, ValidationError};
1413
use jwa::{self, ContentEncryptionAlgorithm, EncryptionOptions, EncryptionResult, KeyManagementAlgorithm};
1514
use jwk;
1615
use serde_custom;
16+
use {CompactJson, CompactPart, Empty};
1717

1818
#[derive(Debug, Eq, PartialEq, Clone)]
1919
/// Compression algorithm applied to plaintext before encryption.
@@ -479,9 +479,7 @@ where
479479

480480
// Verify that the algorithms are expected
481481
if header.registered.cek_algorithm != cek_alg || header.registered.enc_algorithm != enc_alg {
482-
Err(Error::ValidationError(
483-
ValidationError::WrongAlgorithmHeader,
484-
))?;
482+
Err(Error::ValidationError(ValidationError::WrongAlgorithmHeader))?;
485483
}
486484

487485
// TODO: Steps 4-5 not implemented at the moment.
@@ -547,9 +545,7 @@ where
547545
/// Convenience method to get a mutable reference to the payload from an Decrypted JWE
548546
pub fn payload_mut(&mut self) -> Result<&mut T, Error> {
549547
match *self {
550-
Compact::Decrypted {
551-
ref mut payload, ..
552-
} => Ok(payload),
548+
Compact::Decrypted { ref mut payload, .. } => Ok(payload),
553549
Compact::Encrypted(_) => Err(Error::UnsupportedOperation),
554550
}
555551
}
@@ -617,11 +613,11 @@ mod tests {
617613
use ring::rand::SecureRandom;
618614
use serde_test::{assert_tokens, Token};
619615

620-
use JWE;
621616
use super::*;
622617
use jwa::{self, random_aes_gcm_nonce, rng};
623618
use jws;
624619
use test::assert_serde_json;
620+
use JWE;
625621

626622
fn cek_oct_key(len: usize) -> jwk::JWK<::Empty> {
627623
// Construct the encryption key
@@ -650,10 +646,7 @@ mod tests {
650646
assert_tokens(
651647
&test_value,
652648
&[
653-
Token::Struct {
654-
name: "Test",
655-
len: 1,
656-
},
649+
Token::Struct { name: "Test", len: 1 },
657650
Token::Str("test"),
658651
Token::Str("DEF"),
659652
Token::StructEnd,
@@ -666,10 +659,7 @@ mod tests {
666659
assert_tokens(
667660
&test_value,
668661
&[
669-
Token::Struct {
670-
name: "Test",
671-
len: 1,
672-
},
662+
Token::Struct { name: "Test", len: 1 },
673663
Token::Str("test"),
674664
Token::Str("xxx"),
675665
Token::StructEnd,

0 commit comments

Comments
 (0)