Skip to content
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
38 changes: 38 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,44 @@ getrandom = ["crypto-bigint/getrandom", "crypto-common"]
serde = ["encoding", "dep:serde", "dep:serdect", "crypto-bigint/serde"]
pkcs5 = ["pkcs8/encryption"]

[lints.clippy]
borrow_as_ptr = "warn"
cast_lossless = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_sign_loss = "warn"
checked_conversions = "warn"
implicit_saturating_sub = "warn"
manual_assert = "warn"
map_unwrap_or = "warn"
missing_errors_doc = "warn"
missing_panics_doc = "warn"
mod_module_files = "warn"
must_use_candidate = "warn"
needless_range_loop = "allow"
ptr_as_ptr = "warn"
redundant_closure_for_method_calls = "warn"
ref_as_ptr = "warn"
return_self_not_must_use = "warn"
semicolon_if_nothing_returned = "warn"
trivially_copy_pass_by_ref = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
undocumented_unsafe_blocks = "warn"
unnecessary_safety_comment = "warn"
unwrap_in_result = "warn"
unwrap_used = "warn"

[lints.rust]
missing_copy_implementations = "warn"
missing_debug_implementations = "warn"
missing_docs = "warn"
trivial_casts = "warn"
trivial_numeric_casts = "warn"
unused_lifetimes = "warn"
unused_qualifications = "warn"

[package.metadata.docs.rs]
features = ["std", "serde", "hazmat", "sha2"]

Expand Down
2 changes: 2 additions & 0 deletions benches/key.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Key generation benchmark.

#![feature(test)]

extern crate test;
Expand Down
2 changes: 2 additions & 0 deletions src/algorithms/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub(crate) fn generate_multi_prime_key_with_exp<R: CryptoRng + ?Sized>(
return Err(Error::NprimesTooSmall);
}

#[allow(clippy::cast_precision_loss, reason = "TODO")]
if bit_size < 64 {
let prime_limit = (1u64 << (bit_size / nprimes) as u64) as f64;

Expand Down Expand Up @@ -81,6 +82,7 @@ pub(crate) fn generate_multi_prime_key_with_exp<R: CryptoRng + ?Sized>(
todo += (nprimes - 2) / 5;
}

#[allow(clippy::cast_possible_truncation, reason = "TODO")]
for (i, prime) in primes.iter_mut().enumerate() {
let bits = (todo / (nprimes - i)) as u32;
*prime = generate_prime_with_rng(rng, bits);
Expand Down
2 changes: 2 additions & 0 deletions src/algorithms/oaep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ fn decrypt_inner<MGF: FnMut(&mut [u8], &mut [u8])>(
let mut index = 0u32;
let mut nonzero_before_one = Choice::FALSE;

#[allow(clippy::cast_possible_truncation, reason = "TODO")]
for (i, el) in db.iter().skip(h_size).enumerate() {
let equals0 = el.ct_eq(&0u8);
let equals1 = el.ct_eq(&1u8);
Expand All @@ -256,5 +257,6 @@ fn decrypt_inner<MGF: FnMut(&mut [u8], &mut [u8])>(

let valid = first_byte_is_zero & hash_are_equal & !nonzero_before_one & !looking_for_index;

#[allow(clippy::cast_possible_truncation, reason = "TODO")]
Ok(CtOption::new(index + 2 + (h_size * 2) as u32, valid))
}
9 changes: 8 additions & 1 deletion src/algorithms/pkcs1v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ pub(crate) fn pkcs1v15_encrypt_unpad(em: &[u8], k: usize) -> Result<Vec<u8>> {
/// in order to maintain constant memory access patterns. If the plaintext was
/// valid then index contains the index of the original message in em.
#[inline]
#[allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
reason = "TODO"
)]
fn decrypt_inner(em: &[u8], k: usize) -> Result<(u8, u32)> {
if k < 11 || em.len() != k {
return Err(Error::Decryption);
Expand Down Expand Up @@ -147,7 +153,7 @@ pub(crate) fn pkcs1v15_sign_unpad(prefix: &[u8], hashed: &[u8], em: &[u8], k: us
ok &= em[k - t_len - 1].ct_eq(&0u8);

for el in em.iter().skip(2).take(k - t_len - 3) {
ok &= el.ct_eq(&0xff)
ok &= el.ct_eq(&0xff);
}

// TODO(tarcieri): avoid branching here by e.g. using a pseudorandom rejection symbol
Expand All @@ -160,6 +166,7 @@ pub(crate) fn pkcs1v15_sign_unpad(prefix: &[u8], hashed: &[u8], em: &[u8], k: us

/// prefix = 0x30 <oid_len + 8 + digest_len> 0x30 <oid_len + 4> 0x06 <oid_len> oid 0x05 0x00 0x04 <digest_len>
#[inline]
#[allow(clippy::cast_possible_truncation, reason = "TODO")]
pub(crate) fn pkcs1v15_generate_prefix<D>() -> Vec<u8>
where
D: Digest + AssociatedOid,
Expand Down
2 changes: 2 additions & 0 deletions src/algorithms/pss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ fn emsa_pss_verify_pre<'a>(
// 6. If the leftmost 8 * em_len - em_bits bits of the leftmost octet in
// maskedDB are not all equal to zero, output "inconsistent" and
// stop.
#[allow(clippy::cast_possible_truncation, reason = "TODO")]
if db[0]
& (0xFF_u8
.checked_shl(8 - (8 * em_len - em_bits) as u32)
Expand All @@ -232,6 +233,7 @@ fn emsa_pss_verify_salt(db: &[u8], em_len: usize, s_len: usize, h_len: usize) ->

/// Detect salt length by scanning DB for the 0x01 separator byte.
/// Returns (s_len, valid) where s_len is 0 on failure.
#[allow(clippy::cast_possible_truncation, reason = "TODO")]
fn emsa_pss_get_salt_len(db: &[u8], em_len: usize, h_len: usize) -> (usize, Choice) {
let em_len = em_len as u32;
let h_len = h_len as u32;
Expand Down
15 changes: 12 additions & 3 deletions src/algorithms/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ use crate::traits::keys::{PrivateKeyParts, PublicKeyParts};
/// The caller must check that the representative is less than `n` to implement either primitive;
/// this function reduces out-of-range inputs modulo `n`.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
/// <div class="warning">
/// <b> ☢️️ WARNING: HAZARDOUS API ☢️</b>
///
/// Use this function with great care! Raw RSA should never be used without an appropriate padding
/// or signature scheme. See the [module-level documentation][crate::hazmat] for more information.
/// </div>
#[inline]
#[allow(clippy::missing_errors_doc, reason = "TODO")]
pub fn rsa_encrypt<K: PublicKeyParts>(key: &K, m: &BoxedUint) -> Result<BoxedUint> {
let e = key.e();
let res = pow_mod_params_vartime_exp_bits(m, e, e.bits(), key.n_params());
Expand All @@ -40,11 +43,14 @@ pub fn rsa_encrypt<K: PublicKeyParts>(key: &K, m: &BoxedUint) -> Result<BoxedUin
///
/// Returns a plaintext `BoxedUint`. Performs RSA blinding if an `Rng` is passed.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
/// <div class="warning">
/// <b> ☢️️ WARNING: HAZARDOUS API ☢️</b>
///
/// Use this function with great care! Raw RSA should never be used without an appropriate padding
/// or signature scheme. See the [module-level documentation][crate::hazmat] for more information.
/// </div>
#[inline]
#[allow(clippy::missing_errors_doc, reason = "TODO")]
pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
rng: Option<&mut R>,
priv_key: &impl PrivateKeyParts,
Expand Down Expand Up @@ -160,11 +166,14 @@ pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
///
/// `c` must have the same `bits_precision` as the RSA key modulus.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
/// <div class="warning">
/// <b> ☢️️ WARNING: HAZARDOUS API ☢️</b>
///
/// Use this function with great care! Raw RSA should never be used without an appropriate padding
/// or signature scheme. See the [module-level documentation][crate::hazmat] for more information.
/// </div>
#[inline]
#[allow(clippy::missing_errors_doc, reason = "TODO")]
pub fn rsa_decrypt_and_check<R: TryCryptoRng + ?Sized>(
priv_key: &impl PrivateKeyParts,
rng: Option<&mut R>,
Expand Down
2 changes: 1 addition & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<U: AsUintRef> TryFrom<pkcs1::RsaPrivateKey<U>> for RsaPrivateKey {

let n = uint_from_slice(pkcs1_key.modulus.as_uint_ref().as_bytes(), bits)?;
let bits_e = u32::try_from(pkcs1_key.public_exponent.as_uint_ref().as_bytes().len())
.map_err(|_| pkcs1::Error::KeyMalformed)?
.map_err(|_| KeyMalformed)?
* 8;
let e = uint_from_slice(pkcs1_key.public_exponent.as_uint_ref().as_bytes(), bits_e)?;
let e = Option::from(e).ok_or(KeyMalformed)?;
Expand Down
4 changes: 3 additions & 1 deletion src/hazmat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! ⚠️ Low-level "hazmat" RSA functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//! <div class="warning">
//! <b> ☢️️ WARNING: HAZARDOUS API ☢️</b>
//!
//! This module holds functions that apply RSA's core encryption and decryption
//! primitives to raw data without adding or removing appropriate padding. A
Expand All @@ -10,5 +11,6 @@
//!
//! We do NOT recommend using it to implement any algorithm which has not
//! received extensive peer review by cryptographers.
//! </div>

pub use crate::algorithms::rsa::{rsa_decrypt, rsa_decrypt_and_check, rsa_encrypt};
Loading
Loading