9
9
use super :: der;
10
10
use byteorder:: { BigEndian , ByteOrder } ;
11
11
use num_bigint:: BigUint ;
12
- use rand:: Rng ;
13
12
use sha1:: { Digest , Sha1 } ;
14
13
15
14
/// Padding operation trait.
@@ -18,6 +17,22 @@ pub trait Padding {
18
17
fn pub_pad ( & mut self , input : impl AsRef < [ u8 ] > , k : usize ) -> Vec < u8 > ;
19
18
}
20
19
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
+
21
36
/// Padding, as described in PKCS #1: RSA Encryption Version 1.5 (rfc2313).
22
37
#[ derive( Debug ) ]
23
38
pub struct Pkcs1Padding < T > {
@@ -30,7 +45,7 @@ impl<T> Pkcs1Padding<T> {
30
45
}
31
46
}
32
47
33
- impl < T : Rng > Padding for Pkcs1Padding < T > {
48
+ impl < T : FillBytes > Padding for Pkcs1Padding < T > {
34
49
fn pub_pad ( & mut self , input : impl AsRef < [ u8 ] > , k : usize ) -> Vec < u8 > {
35
50
let input = input. as_ref ( ) ;
36
51
let input_len = input. len ( ) ;
@@ -46,13 +61,11 @@ impl<T: Rng> Padding for Pkcs1Padding<T> {
46
61
let ps_len = k - 3 - input_len;
47
62
48
63
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
+ }
56
69
}
57
70
58
71
output[ 2 + ps_len] = 0x00 ;
@@ -104,7 +117,7 @@ impl<T> Pkcs1OaepPadding<T> {
104
117
}
105
118
}
106
119
107
- impl < T : Rng > Padding for Pkcs1OaepPadding < T > {
120
+ impl < T : FillBytes > Padding for Pkcs1OaepPadding < T > {
108
121
/// Will pad input according to PKCS #1 v2 with encoding parameters equal to `[]`.
109
122
fn pub_pad ( & mut self , input : impl AsRef < [ u8 ] > , k : usize ) -> Vec < u8 > {
110
123
let input = input. as_ref ( ) ;
@@ -123,7 +136,8 @@ impl<T: Rng> Padding for Pkcs1OaepPadding<T> {
123
136
// data block DB as: DB = pHash || PS || 01 || M
124
137
let db = [ & * p_hash, & * ps, input] . concat ( ) ;
125
138
// 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" ) ;
127
141
// 7. Let dbMask = MGF(seed, emLen-hLen).
128
142
let db_mask = Self :: mgf1 ( & seed, k - Self :: HASH_LEN ) ;
129
143
// 8. Let maskedDB = DB \xor dbMask.
@@ -202,7 +216,6 @@ mod tests {
202
216
use std:: io:: Read ;
203
217
204
218
use super :: * ;
205
- use rand:: RngCore ;
206
219
207
220
const SEED : & [ u8 ; 64 ] = b"\x03 \x2e \x45 \x32 \x6f \xa8 \x59 \xa7 \x2e \xc2 \x35 \xac \xff \x92 \x9b \x15 \xd1 \
208
221
\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 {
216
229
\x7a \x25 \x19 \x29 \x85 \xa8 \x42 \xdb \xff \x8e \x13 \xef \xee \x5b \x7e \x7e \x55 \xbb \xe4 \xd3 \x89 \x64 \x7c \
217
230
\x68 \x6a \x9a \x9a \xb3 \xfb \x88 \x9b \x2d \x77 \x67 \xd3 \x83 \x7e \xea \x4e \x0a \x2f \x04 ";
218
231
219
- /// Replacement for the deprecated `rand::ReadRng`.
220
232
struct Seed < ' a > ( & ' a [ u8 ] ) ;
221
233
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 ;
234
236
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)
249
239
}
250
240
}
251
241
@@ -257,7 +247,7 @@ mod tests {
257
247
258
248
#[ test]
259
249
fn rsa_pkcs ( ) {
260
- let modulus = vec ! [
250
+ let modulus: & [ u8 ] = & [
261
251
0xa8 , 0xb3 , 0xb2 , 0x84 , 0xaf , 0x8e , 0xb5 , 0x0b , 0x38 , 0x70 , 0x34 , 0xa8 , 0x60 , 0xf1 ,
262
252
0x46 , 0xc4 , 0x91 , 0x9f , 0x31 , 0x87 , 0x63 , 0xcd , 0x6c , 0x55 , 0x98 , 0xc8 , 0xae , 0x48 ,
263
253
0x11 , 0xa1 , 0xe0 , 0xab , 0xc4 , 0xc7 , 0xe0 , 0xb0 , 0x82 , 0xd6 , 0x93 , 0xa5 , 0xe7 , 0xfc ,
@@ -269,13 +259,13 @@ mod tests {
269
259
0x76 , 0x16 , 0xd4 , 0xf5 , 0xba , 0x10 , 0xd4 , 0xcf , 0xd2 , 0x26 , 0xde , 0x88 , 0xd3 , 0x9f ,
270
260
0x16 , 0xfb ,
271
261
] ;
272
- let exponent = vec ! [ 0x01 , 0x00 , 0x01 ] ;
262
+ let exponent: & [ u8 ] = & [ 0x01 , 0x00 , 0x01 ] ;
273
263
274
- let msg1 = vec ! [
264
+ let msg1: & [ u8 ] = & [
275
265
0x66 , 0x28 , 0x19 , 0x4e , 0x12 , 0x07 , 0x3d , 0xb0 , 0x3b , 0xa9 , 0x4c , 0xda , 0x9e , 0xf9 ,
276
266
0x53 , 0x23 , 0x97 , 0xd5 , 0x0d , 0xba , 0x79 , 0xb9 , 0x87 , 0x00 , 0x4a , 0xfe , 0xfe , 0x34 ,
277
267
] ;
278
- let seed1 = vec ! [
268
+ let seed1: & [ u8 ] = & [
279
269
0x01 , 0x00 , 0x00 , 0x00 , 0x73 , 0x00 , 0x00 , 0x00 , 0x41 , 0x00 , 0x00 , 0x00 , 0xae , 0x00 ,
280
270
0x00 , 0x00 , 0x38 , 0x00 , 0x00 , 0x00 , 0x75 , 0x00 , 0x00 , 0x00 , 0xd5 , 0x00 , 0x00 , 0x00 ,
281
271
0xf8 , 0x00 , 0x00 , 0x00 , 0x71 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0xf8 , 0x00 ,
@@ -319,11 +309,11 @@ mod tests {
319
309
] ;
320
310
321
311
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) ,
324
314
) ;
325
315
326
- let rng = Seed ( & * seed1) ;
316
+ let rng = Seed ( seed1) ;
327
317
let pad = Pkcs1Padding :: new ( rng) ;
328
318
329
319
let cipher_text = public_key. encrypt_block ( msg1, pad) ;
@@ -332,7 +322,7 @@ mod tests {
332
322
333
323
#[ test]
334
324
fn rsa_oaep ( ) {
335
- let modulus = vec ! [
325
+ let modulus: & [ u8 ] = & [
336
326
0xbb , 0xf8 , 0x2f , 0x09 , 0x06 , 0x82 , 0xce , 0x9c , 0x23 , 0x38 , 0xac , 0x2b , 0x9d , 0xa8 ,
337
327
0x71 , 0xf7 , 0x36 , 0x8d , 0x07 , 0xee , 0xd4 , 0x10 , 0x43 , 0xa4 , 0x40 , 0xd6 , 0xb6 , 0xf0 ,
338
328
0x74 , 0x54 , 0xf5 , 0x1f , 0xb8 , 0xdf , 0xba , 0xaf , 0x03 , 0x5c , 0x02 , 0xab , 0x61 , 0xea ,
@@ -344,18 +334,14 @@ mod tests {
344
334
0xe2 , 0x53 , 0x72 , 0x98 , 0xca , 0x2a , 0x8f , 0x59 , 0x46 , 0xf8 , 0xe5 , 0xfd , 0x09 , 0x1d ,
345
335
0xbd , 0xcb ,
346
336
] ;
347
- let exponent = vec ! [ 0x11 ] ;
348
- let msg = vec ! [
337
+ let exponent: & [ u8 ] = & [ 0x11 ] ;
338
+ let msg: & [ u8 ] = & [
349
339
0xd4 , 0x36 , 0xe9 , 0x95 , 0x69 , 0xfd , 0x32 , 0xa7 , 0xc8 , 0xa0 , 0x5b , 0xbc , 0x90 , 0xd3 ,
350
340
0x2c , 0x49 ,
351
341
] ;
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 ,
359
345
] ;
360
346
let correct_cipher_text = vec ! [
361
347
0x12 , 0x53 , 0xe0 , 0x4d , 0xc0 , 0xa5 , 0x39 , 0x7b , 0xb4 , 0x4a , 0x7a , 0xb8 , 0x7e , 0x9b ,
@@ -371,11 +357,11 @@ mod tests {
371
357
] ;
372
358
373
359
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) ,
376
362
) ;
377
363
378
- let rng = Seed ( & * seed) ;
364
+ let rng = Seed ( seed) ;
379
365
let pad = Pkcs1OaepPadding :: new ( rng) ;
380
366
381
367
let cipher_text = public_key. encrypt_block ( msg, pad) ;
0 commit comments