Skip to content

Commit 924879d

Browse files
committed
replace rand with getrandom
1 parent f82effb commit 924879d

File tree

3 files changed

+47
-63
lines changed

3 files changed

+47
-63
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ chrono = { version = "0.4.20", default-features = false, features = [
4343
crc32fast = "1.2"
4444
flate2 = { version = "1.0", default-features = false }
4545
frunk = { version = "0.4", optional = true }
46+
getrandom = "0.3"
4647
num-bigint = { version = "0.4" }
4748
num-traits = { version = "0.2", features = ["i128"] }
48-
rand = "0.8"
4949
regex = "1.5"
5050
rust_decimal = { version = "1.0", optional = true }
5151
sha1 = "0.10"

src/crypto/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// option. All files in the project carrying such notice may not be copied,
77
// modified, or distributed except according to those terms.
88

9-
use rand::rngs::OsRng;
10-
119
pub mod der;
1210
pub mod rsa;
1311

@@ -16,6 +14,6 @@ pub mod rsa;
1614
/// It will use OAEP padding, so MySql versions prior to 8.0.5 are not supported.
1715
pub fn encrypt(pass: &[u8], key: &[u8]) -> Vec<u8> {
1816
let pub_key = self::rsa::PublicKey::from_pem(key);
19-
let pad = self::rsa::Pkcs1OaepPadding::new(OsRng);
17+
let pad = self::rsa::Pkcs1OaepPadding::new(self::rsa::OsRng);
2018
pub_key.encrypt_block(pass, pad)
2119
}

src/crypto/rsa.rs

+45-59
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use super::der;
1010
use byteorder::{BigEndian, ByteOrder};
1111
use num_bigint::BigUint;
12-
use rand::Rng;
1312
use sha1::{Digest, Sha1};
1413

1514
/// Padding operation trait.
@@ -18,6 +17,22 @@ pub trait Padding {
1817
fn pub_pad(&mut self, input: impl AsRef<[u8]>, k: usize) -> Vec<u8>;
1918
}
2019

20+
pub trait FillBytes {
21+
type Error: std::fmt::Debug + std::fmt::Display;
22+
23+
fn fill(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>;
24+
}
25+
26+
pub struct OsRng;
27+
28+
impl FillBytes for OsRng {
29+
type Error = getrandom::Error;
30+
31+
fn fill(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
32+
getrandom::fill(dest)
33+
}
34+
}
35+
2136
/// Padding, as described in PKCS #1: RSA Encryption Version 1.5 (rfc2313).
2237
#[derive(Debug)]
2338
pub struct Pkcs1Padding<T> {
@@ -30,7 +45,7 @@ impl<T> Pkcs1Padding<T> {
3045
}
3146
}
3247

33-
impl<T: Rng> Padding for Pkcs1Padding<T> {
48+
impl<T: FillBytes> Padding for Pkcs1Padding<T> {
3449
fn pub_pad(&mut self, input: impl AsRef<[u8]>, k: usize) -> Vec<u8> {
3550
let input = input.as_ref();
3651
let input_len = input.len();
@@ -46,13 +61,11 @@ impl<T: Rng> Padding for Pkcs1Padding<T> {
4661
let ps_len = k - 3 - input_len;
4762

4863
for i in 0..ps_len {
49-
let x = loop {
50-
match self.rng.gen::<u8>() {
51-
0x00 => continue,
52-
x => break x,
53-
}
54-
};
55-
output[i + 2] = x;
64+
while output[i + 2] == 0 {
65+
self.rng
66+
.fill(&mut output[i + 2..=i + 2])
67+
.expect("rng ran out of entropy while padding");
68+
}
5669
}
5770

5871
output[2 + ps_len] = 0x00;
@@ -104,7 +117,7 @@ impl<T> Pkcs1OaepPadding<T> {
104117
}
105118
}
106119

107-
impl<T: Rng> Padding for Pkcs1OaepPadding<T> {
120+
impl<T: FillBytes> Padding for Pkcs1OaepPadding<T> {
108121
/// Will pad input according to PKCS #1 v2 with encoding parameters equal to `[]`.
109122
fn pub_pad(&mut self, input: impl AsRef<[u8]>, k: usize) -> Vec<u8> {
110123
let input = input.as_ref();
@@ -123,7 +136,8 @@ impl<T: Rng> Padding for Pkcs1OaepPadding<T> {
123136
// data block DB as: DB = pHash || PS || 01 || M
124137
let db = [&*p_hash, &*ps, input].concat();
125138
// 6. Generate a random octet string seed of length hLen.
126-
let seed: Vec<_> = (0..Self::HASH_LEN).map(|_| self.rng.gen()).collect();
139+
let mut seed = vec![0; Self::HASH_LEN];
140+
self.rng.fill(&mut seed[..]).expect("rng out of entropy");
127141
// 7. Let dbMask = MGF(seed, emLen-hLen).
128142
let db_mask = Self::mgf1(&seed, k - Self::HASH_LEN);
129143
// 8. Let maskedDB = DB \xor dbMask.
@@ -202,7 +216,6 @@ mod tests {
202216
use std::io::Read;
203217

204218
use super::*;
205-
use rand::RngCore;
206219

207220
const SEED: &[u8; 64] = b"\x03\x2e\x45\x32\x6f\xa8\x59\xa7\x2e\xc2\x35\xac\xff\x92\x9b\x15\xd1\
208221
\x37\x2e\x30\xb2\x07\x25\x5f\x06\x11\xb8\xf7\x85\xd7\x64\x37\x41\x52\xe0\xac\x00\x9e\x50\x9e\
@@ -216,36 +229,13 @@ mod tests {
216229
\x7a\x25\x19\x29\x85\xa8\x42\xdb\xff\x8e\x13\xef\xee\x5b\x7e\x7e\x55\xbb\xe4\xd3\x89\x64\x7c\
217230
\x68\x6a\x9a\x9a\xb3\xfb\x88\x9b\x2d\x77\x67\xd3\x83\x7e\xea\x4e\x0a\x2f\x04";
218231

219-
/// Replacement for the deprecated `rand::ReadRng`.
220232
struct Seed<'a>(&'a [u8]);
221233

222-
impl<'a> RngCore for Seed<'a> {
223-
fn next_u32(&mut self) -> u32 {
224-
let mut buf = [0; 4];
225-
self.fill_bytes(&mut buf);
226-
u32::from_le_bytes(buf)
227-
}
228-
229-
fn next_u64(&mut self) -> u64 {
230-
let mut buf = [0; 8];
231-
self.fill_bytes(&mut buf);
232-
u64::from_le_bytes(buf)
233-
}
234+
impl<'a> FillBytes for Seed<'a> {
235+
type Error = std::io::Error;
234236

235-
fn fill_bytes(&mut self, dest: &mut [u8]) {
236-
self.try_fill_bytes(dest).unwrap_or_else(|err| {
237-
panic!(
238-
"reading random bytes from Read implementation failed; error: {}",
239-
err
240-
)
241-
});
242-
}
243-
244-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
245-
if dest.is_empty() {
246-
return Ok(());
247-
}
248-
self.0.read_exact(dest).map_err(|e| rand::Error::new(e))
237+
fn fill(&mut self, dest: &mut [u8]) -> Result<(), std::io::Error> {
238+
self.0.read_exact(dest)
249239
}
250240
}
251241

@@ -257,7 +247,7 @@ mod tests {
257247

258248
#[test]
259249
fn rsa_pkcs() {
260-
let modulus = vec![
250+
let modulus: &[u8] = &[
261251
0xa8, 0xb3, 0xb2, 0x84, 0xaf, 0x8e, 0xb5, 0x0b, 0x38, 0x70, 0x34, 0xa8, 0x60, 0xf1,
262252
0x46, 0xc4, 0x91, 0x9f, 0x31, 0x87, 0x63, 0xcd, 0x6c, 0x55, 0x98, 0xc8, 0xae, 0x48,
263253
0x11, 0xa1, 0xe0, 0xab, 0xc4, 0xc7, 0xe0, 0xb0, 0x82, 0xd6, 0x93, 0xa5, 0xe7, 0xfc,
@@ -269,13 +259,13 @@ mod tests {
269259
0x76, 0x16, 0xd4, 0xf5, 0xba, 0x10, 0xd4, 0xcf, 0xd2, 0x26, 0xde, 0x88, 0xd3, 0x9f,
270260
0x16, 0xfb,
271261
];
272-
let exponent = vec![0x01, 0x00, 0x01];
262+
let exponent: &[u8] = &[0x01, 0x00, 0x01];
273263

274-
let msg1 = vec![
264+
let msg1: &[u8] = &[
275265
0x66, 0x28, 0x19, 0x4e, 0x12, 0x07, 0x3d, 0xb0, 0x3b, 0xa9, 0x4c, 0xda, 0x9e, 0xf9,
276266
0x53, 0x23, 0x97, 0xd5, 0x0d, 0xba, 0x79, 0xb9, 0x87, 0x00, 0x4a, 0xfe, 0xfe, 0x34,
277267
];
278-
let seed1 = vec![
268+
let seed1: &[u8] = &[
279269
0x01, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0xae, 0x00,
280270
0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00, 0xd5, 0x00, 0x00, 0x00,
281271
0xf8, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xf8, 0x00,
@@ -319,11 +309,11 @@ mod tests {
319309
];
320310

321311
let public_key = PublicKey::new(
322-
BigUint::from_bytes_be(&modulus),
323-
BigUint::from_bytes_be(&exponent),
312+
BigUint::from_bytes_be(modulus),
313+
BigUint::from_bytes_be(exponent),
324314
);
325315

326-
let rng = Seed(&*seed1);
316+
let rng = Seed(seed1);
327317
let pad = Pkcs1Padding::new(rng);
328318

329319
let cipher_text = public_key.encrypt_block(msg1, pad);
@@ -332,7 +322,7 @@ mod tests {
332322

333323
#[test]
334324
fn rsa_oaep() {
335-
let modulus = vec![
325+
let modulus: &[u8] = &[
336326
0xbb, 0xf8, 0x2f, 0x09, 0x06, 0x82, 0xce, 0x9c, 0x23, 0x38, 0xac, 0x2b, 0x9d, 0xa8,
337327
0x71, 0xf7, 0x36, 0x8d, 0x07, 0xee, 0xd4, 0x10, 0x43, 0xa4, 0x40, 0xd6, 0xb6, 0xf0,
338328
0x74, 0x54, 0xf5, 0x1f, 0xb8, 0xdf, 0xba, 0xaf, 0x03, 0x5c, 0x02, 0xab, 0x61, 0xea,
@@ -344,18 +334,14 @@ mod tests {
344334
0xe2, 0x53, 0x72, 0x98, 0xca, 0x2a, 0x8f, 0x59, 0x46, 0xf8, 0xe5, 0xfd, 0x09, 0x1d,
345335
0xbd, 0xcb,
346336
];
347-
let exponent = vec![0x11];
348-
let msg = vec![
337+
let exponent: &[u8] = &[0x11];
338+
let msg: &[u8] = &[
349339
0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3,
350340
0x2c, 0x49,
351341
];
352-
let seed: Vec<u8> = vec![
353-
0xaa, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0xf6, 0x00,
354-
0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0xca, 0x00, 0x00, 0x00, 0xe6, 0x00, 0x00, 0x00,
355-
0x34, 0x00, 0x00, 0x00, 0x89, 0x00, 0x00, 0x00, 0xb4, 0x00, 0x00, 0x00, 0x79, 0x00,
356-
0x00, 0x00, 0xe5, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00, 0x00,
357-
0xde, 0x00, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x6c, 0x00,
358-
0x00, 0x00, 0xb5, 0x00, 0x00, 0x00, 0x8f, 0x00, 0x00, 0x00,
342+
let seed: &[u8] = &[
343+
0xaa, 0xfd, 0x12, 0xf6, 0x59, 0xca, 0xe6, 0x34, 0x89, 0xb4, 0x79, 0xe5, 0x07, 0x6d,
344+
0xde, 0xc2, 0xf0, 0x6c, 0xb5, 0x8f,
359345
];
360346
let correct_cipher_text = vec![
361347
0x12, 0x53, 0xe0, 0x4d, 0xc0, 0xa5, 0x39, 0x7b, 0xb4, 0x4a, 0x7a, 0xb8, 0x7e, 0x9b,
@@ -371,11 +357,11 @@ mod tests {
371357
];
372358

373359
let public_key = PublicKey::new(
374-
BigUint::from_bytes_be(&modulus),
375-
BigUint::from_bytes_be(&exponent),
360+
BigUint::from_bytes_be(modulus),
361+
BigUint::from_bytes_be(exponent),
376362
);
377363

378-
let rng = Seed(&*seed);
364+
let rng = Seed(seed);
379365
let pad = Pkcs1OaepPadding::new(rng);
380366

381367
let cipher_text = public_key.encrypt_block(msg, pad);

0 commit comments

Comments
 (0)