-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge35_dh_negotiated_groups.rs
559 lines (464 loc) · 17.7 KB
/
challenge35_dh_negotiated_groups.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::thread;
use num_bigint::BigUint;
use rustopals::block::{BlockCipher, BlockMode, AES128, CBC};
use rustopals::digest::{Digest, SHA1};
use rustopals::key_exchange::dh::{DHOffer, NIST_BASE, NIST_MODULUS};
enum Message {
Negotiate { modulus: BigUint, base: BigUint },
Accept,
Offer { public_key: BigUint },
Response { public_key: BigUint },
Message { message: Vec<u8>, iv: Vec<u8> },
}
fn alice(tx: SyncSender<Message>, rx: Receiver<Message>) -> Vec<u8> {
// (1) A->B -- Send "p", "g"
tx.send(Message::Negotiate {
modulus: NIST_MODULUS.clone(),
base: NIST_BASE.clone(),
})
.unwrap();
// (2) B->A -- Send ACK
match rx.recv().unwrap() {
Message::Accept => {},
_ => panic!("Expected `Message::Accept` on Alice"),
};
// (3) A->B -- Send "A"
let dh_offer = DHOffer::new_custom(NIST_MODULUS.clone(), &NIST_BASE);
tx.send(Message::Offer {
public_key: dh_offer.get_public().clone(),
})
.unwrap();
// (4) B->A -- Send "B"
let their_public = match rx.recv().unwrap() {
Message::Response { public_key } => public_key,
_ => panic!("Expected `Message::Response` on Alice"),
};
let session = dh_offer
.establish(&their_public)
.expect("Detected an error!");
let key_material = session.to_key_material::<SHA1>();
// (5) A->B -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), msg) + iv
let my_message = crate::gen_random_bytes_between(123, 456);
let my_iv = crate::gen_random_bytes(AES128::BLOCK_SIZE);
let my_encrypted_message = CBC::new(&my_iv).encrypt(&AES128, &my_message, &key_material[0..16]);
tx.send(Message::Message {
message: my_encrypted_message,
iv: my_iv,
})
.unwrap();
// (6) B->A -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), A's msg) + iv
let (their_ecrypted_message, their_iv) = match rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` on Alice"),
};
let their_message = CBC::new(&their_iv)
.decrypt(&AES128, &their_ecrypted_message, &key_material[0..16])
.unwrap();
assert_eq!(my_message, their_message); // Check that echo is OK!
// Ensure communication is dropped
drop(rx);
drop(tx);
// Return to double-check later
my_message
}
fn bob(tx: SyncSender<Message>, rx: Receiver<Message>) {
// (1) A->B -- Send "p", "g"
let (modulus, base) = match rx.recv().unwrap() {
Message::Negotiate { modulus, base } => (modulus, base),
_ => panic!("Expected `Message::Negotiate` on Bob"),
};
// (2) B->A -- Send ACK
tx.send(Message::Accept).unwrap();
// (3) A->B -- Send "A"
let their_public = match rx.recv().unwrap() {
Message::Offer { public_key } => public_key,
_ => panic!("Expected `Message::Offer` on Bob"),
};
// (4) B->A -- Send "B"
let dh_offer = DHOffer::new_custom(modulus, &base);
tx.send(Message::Response {
public_key: dh_offer.get_public().clone(),
})
.unwrap();
let session = dh_offer
.establish(&their_public)
.expect("Detected an error!");
let key_material = session.to_key_material::<SHA1>();
// (5) A->B -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), msg) + iv
let (their_ecrypted_message, their_iv) = match rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` on Bob"),
};
let their_message = CBC::new(&their_iv)
.decrypt(&AES128, &their_ecrypted_message, &key_material[0..16])
.unwrap();
// (6) B->A -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), A's msg) + iv
let my_iv = crate::gen_random_bytes(AES128::BLOCK_SIZE);
let my_encrypted_message =
CBC::new(&my_iv).encrypt(&AES128, &their_message, &key_material[0..16]);
tx.send(Message::Message {
message: my_encrypted_message,
iv: my_iv,
})
.unwrap();
// Ensure communication is dropped
drop(rx);
drop(tx);
}
fn eve_g_1(
alice_tx: SyncSender<Message>,
alice_rx: Receiver<Message>,
bob_tx: SyncSender<Message>,
bob_rx: Receiver<Message>,
) -> Vec<u8> {
// (1) A->B -- Send "p", "g"
let (modulus, _base) = match alice_rx.recv().unwrap() {
Message::Negotiate { modulus, base } => (modulus, base),
_ => panic!("Expected `Message::Negotiate` on Eve"),
};
// (1B) Send g = 1 to Bob instead
bob_tx
.send(Message::Negotiate {
modulus,
base: BigUint::from(1_usize),
})
.unwrap();
// (2) B->A -- Send ACK
match bob_rx.recv().unwrap() {
Message::Accept => {},
_ => panic!("Expected `Message::Accept` on Eve"),
};
// (2B) Relay it to Alice
alice_tx.send(Message::Accept).unwrap();
// (3) A->B -- Send "A"
// Notice this isn't a regular MITM since we're ignoring Alice's PK
let _alice_public = match alice_rx.recv().unwrap() {
Message::Offer { public_key } => public_key,
_ => panic!("Expected `Message::Offer` on Eve"),
};
// (3B) Relay instead "1" to Bob to force his secret to be "1" like Alice's
// Is this a HACK? Or intended? I can't see any other way to force both
// ends to have the same shared secret.
bob_tx
.send(Message::Offer {
public_key: BigUint::from(1_usize),
})
.unwrap();
// (4) B->A -- Send "B"
// Notice this isn't a regular MITM since we're ignoring Bob's PK
let bob_public = match bob_rx.recv().unwrap() {
Message::Response { public_key } => public_key,
_ => panic!("Expected `Message::Response` on Eve"),
};
// (4B) Relay it to Alice
alice_tx
.send(Message::Response {
public_key: bob_public,
})
.unwrap();
// (5) A->M -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), msg) + iv
let (alice_encrypted_message, alice_iv) = match alice_rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` from Alice on Eve"),
};
// (5B) Relay it to Bob
bob_tx
.send(Message::Message {
message: alice_encrypted_message.clone(),
iv: alice_iv.clone(),
})
.unwrap();
// (6) B->M -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), A's msg) + iv
let (bob_encrypted_message, bob_iv) = match bob_rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` from Bob on Eve"),
};
// (6B) Relay it to Alice
alice_tx
.send(Message::Message {
message: bob_encrypted_message.clone(),
iv: bob_iv.clone(),
})
.unwrap();
// Okay, let's see if your nefarious deeds were successful...
let expected_key_material = &SHA1::digest(&BigUint::from(1_usize).to_bytes_be())[0..16];
let alice_message = CBC::new(&alice_iv)
.decrypt(&AES128, &alice_encrypted_message, expected_key_material)
.unwrap();
let bob_message = CBC::new(&bob_iv)
.decrypt(&AES128, &bob_encrypted_message, expected_key_material)
.unwrap();
assert_eq!(alice_message, bob_message);
// Ensure communication is dropped
drop(alice_tx);
drop(alice_rx);
drop(bob_tx);
drop(bob_rx);
// Return to double-check later
alice_message
}
fn eve_g_p(
alice_tx: SyncSender<Message>,
alice_rx: Receiver<Message>,
bob_tx: SyncSender<Message>,
bob_rx: Receiver<Message>,
) -> Vec<u8> {
// (1) A->B -- Send "p", "g"
let (modulus, _base) = match alice_rx.recv().unwrap() {
Message::Negotiate { modulus, base } => (modulus, base),
_ => panic!("Expected `Message::Negotiate` on Eve"),
};
// (1B) Send g = p to Bob instead
bob_tx
.send(Message::Negotiate {
modulus: modulus.clone(),
base: modulus,
})
.unwrap();
// (2) B->A -- Send ACK
match bob_rx.recv().unwrap() {
Message::Accept => {},
_ => panic!("Expected `Message::Accept` on Eve"),
};
// (2B) Relay it to Alice
alice_tx.send(Message::Accept).unwrap();
// (3) A->B -- Send "A"
// Notice this isn't a regular MITM since we're ignoring Alice's PK
let _alice_public = match alice_rx.recv().unwrap() {
Message::Offer { public_key } => public_key,
_ => panic!("Expected `Message::Offer` on Eve"),
};
// (3B) Relay instead "0" to Bob to force his secret to be "0" like Alice's
// Is this a HACK? Or intended? I can't see any other way to force both
// ends to have the same shared secret.
bob_tx
.send(Message::Offer {
public_key: BigUint::from(0_usize),
})
.unwrap();
// (4) B->A -- Send "B"
// Notice this isn't a regular MITM since we're ignoring Bob's PK
let bob_public = match bob_rx.recv().unwrap() {
Message::Response { public_key } => public_key,
_ => panic!("Expected `Message::Response` on Eve"),
};
// (4B) Relay it to Alice
alice_tx
.send(Message::Response {
public_key: bob_public,
})
.unwrap();
// (5) A->M -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), msg) + iv
let (alice_encrypted_message, alice_iv) = match alice_rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` from Alice on Eve"),
};
// (5B) Relay it to Bob
bob_tx
.send(Message::Message {
message: alice_encrypted_message.clone(),
iv: alice_iv.clone(),
})
.unwrap();
// (6) B->M -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), A's msg) + iv
let (bob_encrypted_message, bob_iv) = match bob_rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` from Bob on Eve"),
};
// (6B) Relay it to Alice
alice_tx
.send(Message::Message {
message: bob_encrypted_message.clone(),
iv: bob_iv.clone(),
})
.unwrap();
// Okay, let's see if your nefarious deeds were successful...
let expected_key_material = &SHA1::digest(&BigUint::from(0_usize).to_bytes_be())[0..16];
let alice_message = CBC::new(&alice_iv)
.decrypt(&AES128, &alice_encrypted_message, expected_key_material)
.unwrap();
let bob_message = CBC::new(&bob_iv)
.decrypt(&AES128, &bob_encrypted_message, expected_key_material)
.unwrap();
assert_eq!(alice_message, bob_message);
// Ensure communication is dropped
drop(alice_tx);
drop(alice_rx);
drop(bob_tx);
drop(bob_rx);
// Return to double-check later
alice_message
}
// HACK? I don't get this attack :/
//
// In the way the protocol is designed, I can only change `g` for Bob (Alice
// sends `g` and never receives it, for her it's always the real deal). That
// means that the only thing we control by changing `g` is Bob's public key
// (i.e. `B`). At that point, why bother even touching `g`? Just give Alice
// a bogus `B` directly.
//
// That applies to `g = 1` and `g = p`, but it's even worse in this case.
//
// If I give Bob `g` as `(p-1)`, Bob's public key will be:
// - `1` if his private key is even
// - This means Alice's "shared" secret is `1`
// - `(p-1)` if his private key is odd
// - Alice's secret is `1` if her private key is even
// - Alice's secret is `(p-1)` if her private key is odd
//
// Now, how can we make Bob's and Alice's secret match? By feeding Bob a
// bogus private key, that's not even related to `g`. See where this is going?
// At this point this is either a full-blown MITM, or we're not using `g`
// at all and wer'e just playing with `A` and `B`. Weird.
//
// This same attack would work for ANY `g` as far as I can tell :/
fn eve_g_p_minus_1(
alice_tx: SyncSender<Message>,
alice_rx: Receiver<Message>,
bob_tx: SyncSender<Message>,
bob_rx: Receiver<Message>,
) -> Vec<u8> {
// (1) A->B -- Send "p", "g"
let (modulus, _base) = match alice_rx.recv().unwrap() {
Message::Negotiate { modulus, base } => (modulus, base),
_ => panic!("Expected `Message::Negotiate` on Eve"),
};
// (1B) Send g = (p - 1) to Bob instead
bob_tx
.send(Message::Negotiate {
modulus: modulus.clone(),
base: modulus - BigUint::from(1_usize),
})
.unwrap();
// (2) B->A -- Send ACK
match bob_rx.recv().unwrap() {
Message::Accept => {},
_ => panic!("Expected `Message::Accept` on Eve"),
};
// (2B) Relay it to Alice
alice_tx.send(Message::Accept).unwrap();
// (3) A->B -- Send "A"
let _alice_public = match alice_rx.recv().unwrap() {
Message::Offer { public_key } => public_key,
_ => panic!("Expected `Message::Offer` on Eve"),
};
// (3B) Relay "1" to Bob (see note above)
bob_tx
.send(Message::Offer {
public_key: BigUint::from(1_usize),
})
.unwrap();
// (4) B->A -- Send "B"
let _bob_public = match bob_rx.recv().unwrap() {
Message::Response { public_key } => public_key,
_ => panic!("Expected `Message::Response` on Eve"),
};
// (4B) Relay "1" to Alice (see note above)
alice_tx
.send(Message::Response {
public_key: BigUint::from(1_usize),
})
.unwrap();
// (5) A->M -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), msg) + iv
let (alice_encrypted_message, alice_iv) = match alice_rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` from Alice on Eve"),
};
// (5B) Relay it to Bob
bob_tx
.send(Message::Message {
message: alice_encrypted_message.clone(),
iv: alice_iv.clone(),
})
.unwrap();
// (6) B->M -- Send AES-CBC(SHA1(s)[0:16], iv=random(16), A's msg) + iv
let (bob_encrypted_message, bob_iv) = match bob_rx.recv().unwrap() {
Message::Message { message, iv } => (message, iv),
_ => panic!("Expected `Message::Message` from Bob on Eve"),
};
// (6B) Relay it to Alice
alice_tx
.send(Message::Message {
message: bob_encrypted_message.clone(),
iv: bob_iv.clone(),
})
.unwrap();
// Okay, let's see if your nefarious deeds were successful...
let expected_key_material = &SHA1::digest(&BigUint::from(1_usize).to_bytes_be())[0..16];
let alice_message = CBC::new(&alice_iv)
.decrypt(&AES128, &alice_encrypted_message, expected_key_material)
.unwrap();
let bob_message = CBC::new(&bob_iv)
.decrypt(&AES128, &bob_encrypted_message, expected_key_material)
.unwrap();
assert_eq!(alice_message, bob_message);
// Ensure communication is dropped
drop(alice_tx);
drop(alice_rx);
drop(bob_tx);
drop(bob_rx);
// Return to double-check later
alice_message
}
// This test passes if non-MITM'd communication works
#[test]
fn test_normal_operation() {
let (alice_tx, bob_rx) = sync_channel::<Message>(0);
let (bob_tx, alice_rx) = sync_channel::<Message>(0);
let alice_thread = thread::spawn(move || alice(alice_tx, alice_rx));
let bob_thread = thread::spawn(move || bob(bob_tx, bob_rx));
alice_thread.join().unwrap();
bob_thread.join().unwrap();
}
// This test passes if nobody noticed the MITM
#[test]
fn test_g_1() {
let (alice_tx, eve_alice_rx) = sync_channel::<Message>(0);
let (eve_alice_tx, alice_rx) = sync_channel::<Message>(0);
let (eve_bob_tx, bob_rx) = sync_channel::<Message>(0);
let (bob_tx, eve_bob_rx) = sync_channel::<Message>(0);
let alice_thread = thread::spawn(move || alice(alice_tx, alice_rx));
let bob_thread = thread::spawn(move || bob(bob_tx, bob_rx));
let eve_thread =
thread::spawn(move || eve_g_1(eve_alice_tx, eve_alice_rx, eve_bob_tx, eve_bob_rx));
let alice_message = alice_thread.join().unwrap();
bob_thread.join().unwrap();
let eve_message = eve_thread.join().unwrap();
// Double check
assert_eq!(alice_message, eve_message);
}
// This test passes if nobody noticed the MITM
#[test]
fn test_g_p() {
let (alice_tx, eve_alice_rx) = sync_channel::<Message>(0);
let (eve_alice_tx, alice_rx) = sync_channel::<Message>(0);
let (eve_bob_tx, bob_rx) = sync_channel::<Message>(0);
let (bob_tx, eve_bob_rx) = sync_channel::<Message>(0);
let alice_thread = thread::spawn(move || alice(alice_tx, alice_rx));
let bob_thread = thread::spawn(move || bob(bob_tx, bob_rx));
let eve_thread =
thread::spawn(move || eve_g_p(eve_alice_tx, eve_alice_rx, eve_bob_tx, eve_bob_rx));
let alice_message = alice_thread.join().unwrap();
bob_thread.join().unwrap();
let eve_message = eve_thread.join().unwrap();
// Double check
assert_eq!(alice_message, eve_message);
}
// This test passes if nobody noticed the MITM
#[test]
fn test_g_p_minus_1() {
let (alice_tx, eve_alice_rx) = sync_channel::<Message>(0);
let (eve_alice_tx, alice_rx) = sync_channel::<Message>(0);
let (eve_bob_tx, bob_rx) = sync_channel::<Message>(0);
let (bob_tx, eve_bob_rx) = sync_channel::<Message>(0);
let alice_thread = thread::spawn(move || alice(alice_tx, alice_rx));
let bob_thread = thread::spawn(move || bob(bob_tx, bob_rx));
let eve_thread =
thread::spawn(move || eve_g_p_minus_1(eve_alice_tx, eve_alice_rx, eve_bob_tx, eve_bob_rx));
let alice_message = alice_thread.join().unwrap();
bob_thread.join().unwrap();
let eve_message = eve_thread.join().unwrap();
// Double check
assert_eq!(alice_message, eve_message);
}