Update the report.
[crypto_lab1.git] / lab1_rust / src / main.rs
1 #![feature(macro_rules)]
2
3 extern crate openssl;
4 extern crate serialize;
5
6 use std::io;
7 use std::os;
8
9 use end_point::{ Client, Server };
10
11 mod crypto;
12 mod packet;
13 mod end_point;
14 mod oracle_machine;
15
16 const PORT: u16 = 4221;
17
18 fn do_oracle_attack(address: &str, variant: packet::Variant) {
19 // 16 bytes encrypted data from 'Packet::random_packet_data([4])'.
20 let cipher_block: [u8, ..16] = [254, 9, 228, 149, 60, 42, 165, 34, 233, 75, 112, 57, 37, 9, 116, 103]; // Known by the attacker.
21 let xor_operand: [u8, ..16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3]; // This is the IV or the previous 16 bytes cipherblock. In our case we took the IV.
22
23 let expected_clear_block: [u8, ..16] = [44, 92, 31, 98, 220, 84, 226, 53, 58, 94, 45, 25, 242, 6, 199, 1]; // To be found by the attacker.
24
25 match oracle_machine::decipher(address, PORT, &xor_operand, &cipher_block, variant) {
26 Some(ref deciphered) if deciphered.as_slice() == expected_clear_block => {
27 println!("The oracle machine has found the clear block!:");
28 println!(" Expected block: {}", expected_clear_block.to_vec());
29 println!(" Decrypted block: {}", deciphered)
30 }
31 Some(ref other) =>
32 println!("The oracle machine hasn't found the clear block: {}", other),
33 _ =>
34 println!("The oracle machine hasn't found the clear block"),
35 }
36 }
37
38 enum Mode {
39 Help,
40 ServerAlone,
41 GenKey,
42 Tests,
43 OracleWeak,
44 OracleFixed,
45 }
46
47 fn mode() -> Mode {
48 let args = os::args();
49
50 if args.iter().any(|a| a.as_slice() == "--help" || a.as_slice() == "-h") {
51 return Help
52 }
53
54 if args.len() <= 1 {
55 ServerAlone
56 } else {
57 match args[1].as_slice() {
58 "genkey" => GenKey,
59 "tests" => Tests,
60 "oracle-weak" => OracleWeak,
61 "oracle-fixed" => OracleFixed,
62 _ => ServerAlone,
63 }
64 }
65 }
66
67 fn print_usage() {
68 println!(
69 r"{} [genkey | tests | oracle-weak | oracle-fixed]
70 genkey: Generate a 256 bits key
71 tests: launch some tests between a client and a weak server
72 oracle-weak: launch a padding oracle attack against a weak server
73 oracle-fixed: launch a padding oracle attack against a fixed server",
74 os::args()[0]
75 );
76 }
77
78 fn main() {
79 let mode = mode();
80
81 match mode {
82 Help => print_usage(),
83 GenKey =>
84 match crypto::generate_key(256 / 8) {
85 Ok(key) => println!("key: {}", key),
86 Err(e) => println!("Unable to generate a key. Error: {}", e)
87 },
88 _ => {
89 let address = "::1";
90 println!("Starting server on [{}]:{}...", address, PORT);
91
92 match Server::new(address, PORT, match mode { OracleFixed => packet::Fixed, _ => packet::Weak }) {
93 Ok(mut server) => {
94 println!("Server started");
95
96 match mode {
97 Tests => Client::start_tests(address, PORT, packet::Weak),
98 OracleWeak => do_oracle_attack(address, packet::Weak),
99 OracleFixed => do_oracle_attack(address, packet::Fixed),
100 _ => {
101 println!("Press any key to quit");
102 io::stdin().read_line().ok().expect("Failed to read line");
103 }
104 }
105
106 server.close().ok().expect("Failed to close the server");
107 },
108 Err(e) =>
109 println!("Unable to create a new server. Error: {}", e)
110 }
111 }
112 }
113 }