-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
284 lines (257 loc) · 13.5 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#[cfg(test)]
pub mod tests {
use crate::shared::conversion::{base64_to_bytes, hex_to_bytes};
use crate::shared::sha1::SHA1;
use crate::shared::{bleichenbacher, dsa, mod_inv, mod_sub, rsa};
use num_bigint::BigUint;
use num_integer::Integer;
use num_traits::{Num, One, Zero};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::ops::Shr;
use std::str::FromStr;
#[test]
fn test_challenge_41() {
let message = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit";
let m = BigUint::from_bytes_be(message);
// Apparently there's no real pure Rust libraries to generate random primes??...
let p = &BigUint::from_str("9902478688314345424239631829098064031372511021415073888934444987805904619070767824954564980642642554558422713147827332946886953946202126417051242267443733").unwrap();
let q = &BigUint::from_str("9023289800571256384296979170278503137808766752150078076803904588875045578444674044397684797154640374473290798963775917093544857834628721547751219278749279").unwrap();
let (public_key, private_key) = rsa::generate_keypair(p, q);
let c = rsa::encrypt(&public_key, &m);
let (n, e) = public_key;
let _2 = &BigUint::from(2u8);
let c_ = (_2.modpow(&e, &n) * c).mod_floor(&n);
let m_ = rsa::decrypt(&private_key, &c_);
let m = (m_ * mod_inv(_2, &n).unwrap()).mod_floor(&n);
assert_eq!(m.to_bytes_be(), message);
}
#[test]
fn test_challenge_42() {
// We need a message with an odd SHA-1 hash.
let message = b"hi mom!!!";
// Apparently there's no real pure Rust libraries to generate random primes??...
let p = &BigUint::from_str("9902478688314345424239631829098064031372511021415073888934444987805904619070767824954564980642642554558422713147827332946886953946202126417051242267443733").unwrap();
let q = &BigUint::from_str("9023289800571256384296979170278503137808766752150078076803904588875045578444674044397684797154640374473290798963775917093544857834628721547751219278749279").unwrap();
let (public_key, private_key) = rsa::generate_keypair(p, q);
let sig = rsa::sign(&private_key, message);
assert!(rsa::verify(&public_key, message, &sig));
let mut hash = [0; 20];
SHA1::default().hash(message, &mut hash);
let mut data = [0; 35];
data[0..15].copy_from_slice(rsa::SHA1_ASN1_ID);
data[15..35].copy_from_slice(&hash);
let suffix = BigUint::from_bytes_be(&data);
assert!(!suffix.is_even());
let mut s = BigUint::one();
for i in 0..288 {
if s.pow(3).bit(i) != suffix.bit(i) {
s.set_bit(i, true);
}
}
// Most of c is randomly generated...
let c = hex_to_bytes("0001ff4a5cf727f546010e5a7d86356c1139f7dd9b5817939c30edb390c713785ce88268b10473ca2eca46fe22f6b3117364997f0f6dc3ce4c1b8efb65bfba72b692d2de5a3bfeeae1ac3b37a8866f423ed0ccbbe9ccc2b79c66e8ac95fe818a8833819602d5cdd4fbb882c1b0be3c4fc06fa4ceffe731a9998d773d28dfc286").unwrap();
let suffix = s.to_bytes_be();
let mut sig = BigUint::from_bytes_be(&c).nth_root(3).to_bytes_be();
let sig_len = sig.len();
sig[sig_len - suffix.len()..sig_len].copy_from_slice(&suffix);
assert!(rsa::verify(&public_key, message, &sig));
}
#[test]
fn test_challenge_43() {
let p_hex = b"\
800000000000000089e1855218a0e7dac38136ffafa72eda7\
859f2171e25e65eac698c1702578b07dc2a1076da241c76c6\
2d374d8389ea5aeffd3226a0530cc565f3bf6b50929139ebe\
ac04f48c3c84afb796d61e5a4f9a8fda812ab59494232c7d2\
b4deb50aa18ee9e132bfa85ac4374d7f9091abc3d015efc87\
1a584471bb1";
let q_hex = b"f4f47f05794b256174bba6e9b396a7707e563c5b";
let g_hex = b"\
5958c9d3898b224b12672c0b98e06c60df923cb8bc999d119\
458fef538b8fa4046c8db53039db620c094c9fa077ef389b5\
322a559946a71903f990f1f7e0e025e2d7f7cf494aff1a047\
0f5b64c36b625a097f1651fe775323556fe00b3608c887892\
878480e99041be601a62166ca6894bdd41a7054ec89f756ba\
9fc95302291";
let y_hex = b"\
84ad4719d044495496a3201c8ff484feb45b962e7302e56a392aee4\
abab3e4bdebf2955b4736012f21a08084056b19bcd7fee56048e004\
e44984e2f411788efdc837a0d2e5abb7b555039fd243ac01f0fb2ed\
1dec568280ce678e931868d23eb095fde9d3779191b8c0299d6e07b\
bb283e6633451e535c45513b2d33c99ea17";
let m_hex = b"d2d0714f014a9784047eaeccf956520045c45265";
let _p = &BigUint::parse_bytes(p_hex, 16).unwrap();
let q = &BigUint::parse_bytes(q_hex, 16).unwrap();
let _g = &BigUint::parse_bytes(g_hex, 16).unwrap();
let _y = &BigUint::parse_bytes(y_hex, 16).unwrap();
let m = &BigUint::parse_bytes(m_hex, 16).unwrap();
let r = &BigUint::from_str("548099063082341131477253921760299949438196259240").unwrap();
let s = &BigUint::from_str("857042759984254168557880549501802188789837994940").unwrap();
let target_hash = hex_to_bytes("0954edd5e0afe5542a4adf012611a91912a3ec16").unwrap();
assert!((0..=65535).any(|k: u16| {
let x = dsa::find_x(q, m, &BigUint::from(k), r, s);
let mut hash = [0; 20];
SHA1::default().hash(x.to_str_radix(16).as_bytes(), &mut hash);
target_hash == hash
}));
}
#[test]
fn test_challenge_44() {
let p_hex = b"\
800000000000000089e1855218a0e7dac38136ffafa72eda7\
859f2171e25e65eac698c1702578b07dc2a1076da241c76c6\
2d374d8389ea5aeffd3226a0530cc565f3bf6b50929139ebe\
ac04f48c3c84afb796d61e5a4f9a8fda812ab59494232c7d2\
b4deb50aa18ee9e132bfa85ac4374d7f9091abc3d015efc87\
1a584471bb1";
let q_hex = b"f4f47f05794b256174bba6e9b396a7707e563c5b";
let g_hex = b"\
5958c9d3898b224b12672c0b98e06c60df923cb8bc999d119\
458fef538b8fa4046c8db53039db620c094c9fa077ef389b5\
322a559946a71903f990f1f7e0e025e2d7f7cf494aff1a047\
0f5b64c36b625a097f1651fe775323556fe00b3608c887892\
878480e99041be601a62166ca6894bdd41a7054ec89f756ba\
9fc95302291";
let y_hex = b"\
2d026f4bf30195ede3a088da85e398ef869611d0f68f07\
13d51c9c1a3a26c95105d915e2d8cdf26d056b86b8a7b8\
5519b1c23cc3ecdc6062650462e3063bd179c2a6581519\
f674a61f1d89a1fff27171ebc1b93d4dc57bceb7ae2430\
f98a6a4d83d8279ee65d71c1203d2c96d65ebbf7cce9d3\
2971c3de5084cce04a2e147821";
let _p = &BigUint::parse_bytes(p_hex, 16).unwrap();
let q = &BigUint::parse_bytes(q_hex, 16).unwrap();
let _g = &BigUint::parse_bytes(g_hex, 16).unwrap();
let _y = &BigUint::parse_bytes(y_hex, 16).unwrap();
let mut m = Vec::new();
let mut r = Vec::new();
let mut s = Vec::new();
BufReader::new(File::open("src/set6/challenge44.txt").unwrap())
.lines()
.filter_map(|line| line.ok())
.for_each(|line| {
let value: String = line.chars().skip(3).collect();
if line.starts_with("m: ") {
m.push(BigUint::from_str_radix(&value, 16).unwrap());
}
if line.starts_with("r: ") {
r.push(BigUint::from_str(&value).unwrap());
}
if line.starts_with("s: ") {
s.push(BigUint::from_str(&value).unwrap());
}
});
let target_hash = hex_to_bytes("ca8f6f7c66fa362d40760d135b763eb8527d3d52").unwrap();
assert!((0..m.len()).any(|i| {
(i + 1..m.len()).filter(|&j| m[i] != m[j]).any(|j| {
let k = (mod_inv(&mod_sub(&s[i], &s[j], q), q).unwrap() * mod_sub(&m[i], &m[j], q))
.mod_floor(q);
let x = dsa::find_x(q, &m[i], &BigUint::from(k), &r[i], &s[i]);
let mut hash = [0; 20];
SHA1::default().hash(x.to_str_radix(16).as_bytes(), &mut hash);
println!("{x}");
return target_hash == hash;
})
}));
}
#[test]
fn test_challenge_45() {
let p_hex = b"\
800000000000000089e1855218a0e7dac38136ffafa72eda7\
859f2171e25e65eac698c1702578b07dc2a1076da241c76c6\
2d374d8389ea5aeffd3226a0530cc565f3bf6b50929139ebe\
ac04f48c3c84afb796d61e5a4f9a8fda812ab59494232c7d2\
b4deb50aa18ee9e132bfa85ac4374d7f9091abc3d015efc87\
1a584471bb1";
let q_hex = b"f4f47f05794b256174bba6e9b396a7707e563c5b";
let x_hex = b"f1b733db159c66bce071d21e044a48b0e4c1665a";
let y_hex = b"\
2d026f4bf30195ede3a088da85e398ef869611d0f68f07\
13d51c9c1a3a26c95105d915e2d8cdf26d056b86b8a7b8\
5519b1c23cc3ecdc6062650462e3063bd179c2a6581519\
f674a61f1d89a1fff27171ebc1b93d4dc57bceb7ae2430\
f98a6a4d83d8279ee65d71c1203d2c96d65ebbf7cce9d3\
2971c3de5084cce04a2e147821";
let m_hex = b"d2d0714f014a9784047eaeccf956520045c45265";
let p = &BigUint::parse_bytes(p_hex, 16).unwrap();
let q = &BigUint::parse_bytes(q_hex, 16).unwrap();
let x = &BigUint::parse_bytes(x_hex, 16).unwrap();
let y = &BigUint::parse_bytes(y_hex, 16).unwrap();
let m = &BigUint::parse_bytes(m_hex, 16).unwrap();
let g = &BigUint::zero();
let (ref r, ref s) = dsa::sign(p, q, g, x, m);
assert!(dsa::verify(p, q, g, y, m, r, s));
assert!(dsa::verify(p, q, g, y, &(m + BigUint::one()), r, s));
let g = &BigUint::one();
let r = &y.mod_floor(q);
let s = r;
assert!(dsa::verify(p, q, g, y, m, r, s));
assert!(dsa::verify(p, q, g, y, &(m + BigUint::one()), r, s));
}
#[test]
fn test_challenge_46() {
let message = base64_to_bytes("VGhhdCdzIHdoeSBJIGZvdW5kIHlvdSBkb24ndCBwbGF5IGFyb3VuZCB3aXRoIHRoZSBGdW5reSBDb2xkIE1lZGluYQ==").unwrap();
let m = BigUint::from_bytes_be(&message);
// Apparently there's no real pure Rust libraries to generate random primes??...
let p = &BigUint::from_str("9902478688314345424239631829098064031372511021415073888934444987805904619070767824954564980642642554558422713147827332946886953946202126417051242267443733").unwrap();
let q = &BigUint::from_str("9023289800571256384296979170278503137808766752150078076803904588875045578444674044397684797154640374473290798963775917093544857834628721547751219278749279").unwrap();
let (public_key, private_key) = rsa::generate_keypair(p, q);
let mut c = rsa::encrypt(&public_key, &m);
let (ref n, ref e) = public_key;
let parity_oracle = |c: &BigUint| rsa::decrypt(&private_key, c).is_even();
let mul = &BigUint::from(2u8).modpow(e, n);
let mut left = BigUint::zero();
let mut right = n.clone();
// This will take a long time...
while left != right {
c = (c * mul).mod_floor(n);
if parity_oracle(&c) {
right = (&right + &left).shr(1);
} else {
left = (&right + &left).shr(1);
}
}
let m = right.to_bytes_be();
let message = String::from_utf8_lossy(&m);
assert!(message.starts_with("That's why I found you don't play around with the Funky Cold"));
}
#[allow(non_snake_case)]
#[test]
fn test_challenge_47() {
let msg = b"kick it, CC";
// Apparently there's no real pure Rust libraries to generate random primes??...
let p = &BigUint::from_str("226147947679998672588246739628235237089").unwrap();
let q = &BigUint::from_str("172775480584146578358417323216618068661").unwrap();
let (public_key, private_key) = rsa::generate_keypair(p, q);
let ct = hex_to_bytes("10cd173291c54bd29c5c3fa496368b093661b16e340c51bc4e2aa9768377f504")
.unwrap();
assert_eq!(rsa::decrypt_padded(&private_key, &ct).unwrap(), msg);
let c = &BigUint::from_bytes_be(&ct);
let m = rsa::decrypt(&private_key, c);
let (ref n, ref e) = public_key;
let padding_oracle =
|c: &BigUint| rsa::decrypt_padded(&private_key, &c.to_bytes_be()).is_some();
let m_ = bleichenbacher::attack(padding_oracle, n, e, c);
assert_eq!(m_, m);
}
#[allow(non_snake_case)]
#[test]
fn test_challenge_48() {
let msg = b"a much longer plaintext message";
// Apparently there's no real pure Rust libraries to generate random primes??...
let p = &BigUint::from_str("6735141684357686395037910431080036071732546238601212352069774099870323265055480397226207750793893471356146570963896469919488976152492583032174713330061959").unwrap();
let q = &BigUint::from_str("4031388370185090417350155343960777126114936942397927759073182276734271076803441996486242455696330540663229242967183783542330024599134161134568952089530167").unwrap();
let (public_key, private_key) = rsa::generate_keypair(p, q);
let ct = hex_to_bytes("26331a7f0ea4b9917d043c8ac7495b3ec3e4d5599aef1f0b1e0b84bb47a9056394f6c802c093658b49dcc368189afe0aff821c34e23d3d1ed033009c77bdb8df80118a538a75feae2ee614cbbed80092c3bf666fc5674c16f84714231727efb568da2260fc950dd0d60ed2c935f50446e261536fa766e4afbb26e2d60c8efdf1")
.unwrap();
assert_eq!(rsa::decrypt_padded(&private_key, &ct).unwrap(), msg);
let c = &BigUint::from_bytes_be(&ct);
let m = rsa::decrypt(&private_key, c);
let (ref n, ref e) = public_key;
let padding_oracle =
|c: &BigUint| rsa::decrypt_padded(&private_key, &c.to_bytes_be()).is_some();
let m_ = bleichenbacher::attack(padding_oracle, n, e, c);
assert_eq!(m_, m);
}
}