Browse Source

Add rust implementation and test/benchmark script

Travis Burtrum 1 year ago
parent
commit
b57378bbae
4 changed files with 308 additions and 0 deletions
  1. 130
    0
      Cargo.lock
  2. 10
    0
      Cargo.toml
  3. 149
    0
      src/main.rs
  4. 19
    0
      test.sh

+ 130
- 0
Cargo.lock View File

@@ -0,0 +1,130 @@
1
+[root]
2
+name = "image_downloader"
3
+version = "0.1.0"
4
+dependencies = [
5
+ "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
6
+ "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
7
+]
8
+
9
+[[package]]
10
+name = "bitflags"
11
+version = "0.7.0"
12
+source = "registry+https://github.com/rust-lang/crates.io-index"
13
+
14
+[[package]]
15
+name = "conv"
16
+version = "0.3.3"
17
+source = "registry+https://github.com/rust-lang/crates.io-index"
18
+dependencies = [
19
+ "custom_derive 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
20
+]
21
+
22
+[[package]]
23
+name = "custom_derive"
24
+version = "0.1.7"
25
+source = "registry+https://github.com/rust-lang/crates.io-index"
26
+
27
+[[package]]
28
+name = "gcc"
29
+version = "0.3.51"
30
+source = "registry+https://github.com/rust-lang/crates.io-index"
31
+
32
+[[package]]
33
+name = "kernel32-sys"
34
+version = "0.2.2"
35
+source = "registry+https://github.com/rust-lang/crates.io-index"
36
+dependencies = [
37
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
38
+ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
39
+]
40
+
41
+[[package]]
42
+name = "libc"
43
+version = "0.2.29"
44
+source = "registry+https://github.com/rust-lang/crates.io-index"
45
+
46
+[[package]]
47
+name = "magenta"
48
+version = "0.1.1"
49
+source = "registry+https://github.com/rust-lang/crates.io-index"
50
+dependencies = [
51
+ "conv 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
52
+ "magenta-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
53
+]
54
+
55
+[[package]]
56
+name = "magenta-sys"
57
+version = "0.1.1"
58
+source = "registry+https://github.com/rust-lang/crates.io-index"
59
+dependencies = [
60
+ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
61
+]
62
+
63
+[[package]]
64
+name = "rand"
65
+version = "0.3.16"
66
+source = "registry+https://github.com/rust-lang/crates.io-index"
67
+dependencies = [
68
+ "libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
69
+ "magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
70
+]
71
+
72
+[[package]]
73
+name = "redox_syscall"
74
+version = "0.1.29"
75
+source = "registry+https://github.com/rust-lang/crates.io-index"
76
+
77
+[[package]]
78
+name = "rust-crypto"
79
+version = "0.2.36"
80
+source = "registry+https://github.com/rust-lang/crates.io-index"
81
+dependencies = [
82
+ "gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)",
83
+ "libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
84
+ "rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
85
+ "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
86
+ "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
87
+]
88
+
89
+[[package]]
90
+name = "rustc-serialize"
91
+version = "0.3.24"
92
+source = "registry+https://github.com/rust-lang/crates.io-index"
93
+
94
+[[package]]
95
+name = "time"
96
+version = "0.1.38"
97
+source = "registry+https://github.com/rust-lang/crates.io-index"
98
+dependencies = [
99
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
100
+ "libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
101
+ "redox_syscall 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
102
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
103
+]
104
+
105
+[[package]]
106
+name = "winapi"
107
+version = "0.2.8"
108
+source = "registry+https://github.com/rust-lang/crates.io-index"
109
+
110
+[[package]]
111
+name = "winapi-build"
112
+version = "0.1.1"
113
+source = "registry+https://github.com/rust-lang/crates.io-index"
114
+
115
+[metadata]
116
+"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
117
+"checksum conv 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "78ff10625fd0ac447827aa30ea8b861fead473bb60aeb73af6c1c58caf0d1299"
118
+"checksum custom_derive 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ef8ae57c4978a2acd8b869ce6b9ca1dfe817bff704c220209fdef2c0b75a01b9"
119
+"checksum gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)" = "120d07f202dcc3f72859422563522b66fe6463a4c513df062874daad05f85f0a"
120
+"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
121
+"checksum libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "8a014d9226c2cc402676fbe9ea2e15dd5222cd1dd57f576b5b283178c944a264"
122
+"checksum magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf0336886480e671965f794bc9b6fce88503563013d1bfb7a502c81fe3ac527"
123
+"checksum magenta-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40d014c7011ac470ae28e2f76a02bfea4a8480f73e701353b49ad7a8d75f4699"
124
+"checksum rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "eb250fd207a4729c976794d03db689c9be1d634ab5a1c9da9492a13d8fecbcdf"
125
+"checksum redox_syscall 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)" = "3c9309631a35303bffb47e397198e3668cb544fe8834cd3da2a744441e70e524"
126
+"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
127
+"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
128
+"checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520"
129
+"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
130
+"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

+ 10
- 0
Cargo.toml View File

@@ -0,0 +1,10 @@
1
+[package]
2
+name = "image_downloader"
3
+version = "0.1.0"
4
+authors = ["moparisthebest <admin@moparisthebest.com>"]
5
+
6
+[dependencies]
7
+rust-crypto = "0.2.36"
8
+rustc-serialize = "0.3.24"
9
+#rand = "^0.3"
10
+#ring = "0.11.0"

+ 149
- 0
src/main.rs View File

@@ -0,0 +1,149 @@
1
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4
+// option. This file may not be copied, modified, or distributed
5
+// except according to those terms.
6
+
7
+extern crate crypto;
8
+extern crate rustc_serialize;
9
+
10
+use std::env;
11
+use std::io::{self, Write, Read, stdin};
12
+
13
+use crypto::aes;
14
+
15
+use rustc_serialize::hex::*;
16
+use std::iter::repeat;
17
+
18
+use crypto::aes_gcm::*;
19
+use crypto::aead::{AeadEncryptor,AeadDecryptor};
20
+
21
+// Encrypt a buffer with the given key and iv using
22
+// AES-256/CBC/Pkcs encryption.
23
+fn encrypt(data: &[u8], key: &[u8], iv: &[u8]) -> Vec<u8> {
24
+
25
+    // Create an encryptor instance of the best performing
26
+    // type available for the platform.
27
+    let aad: [u8; 0] = [];
28
+    let mut encryptor = AesGcm::new(
29
+        aes::KeySize::KeySize256,
30
+        key,
31
+        iv,
32
+        &aad
33
+    );
34
+
35
+    let mut out: Vec<u8> = repeat(0).take(data.len()).collect();
36
+    let mut out_tag: Vec<u8> = repeat(0).take(16).collect();
37
+
38
+    encryptor.encrypt(&data[..], &mut out[..],&mut out_tag[..]);
39
+
40
+    out.extend_from_slice(&out_tag);
41
+    return out
42
+}
43
+
44
+// Decrypts a buffer with the given key and iv using
45
+// AES-256/CBC/Pkcs encryption.
46
+//
47
+// This function is very similar to encrypt(), so, please reference
48
+// comments in that function. In non-example code, if desired, it is possible to
49
+// share much of the implementation using closures to hide the operation
50
+// being performed. However, such code would make this example less clear.
51
+fn decrypt(encrypted_data: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>, &'static str> {
52
+    // Create an decryptor instance of the best performing
53
+    // type available for the platform.
54
+
55
+    let aad: [u8; 0] = [];
56
+    let mut decryptor = AesGcm::new(
57
+        aes::KeySize::KeySize256,
58
+        key,
59
+        iv,
60
+        &aad
61
+    );
62
+
63
+    let out_len = encrypted_data.len() - 16;
64
+    let mut out: Vec<u8> = repeat(0).take(out_len).collect();
65
+
66
+    let result = decryptor.decrypt(&encrypted_data[0..out_len], &mut out[..], &encrypted_data[out_len..]);
67
+    if result {
68
+        Ok(out)
69
+    } else {
70
+        Err("decryption failed")
71
+    }
72
+}
73
+
74
+/*
75
+
76
+extern crate ring;
77
+use ring::aead::*;
78
+
79
+fn encrypt_ring(data: &[u8], key: &[u8], iv: &[u8]) -> Vec<u8> {
80
+
81
+    let sealing_key = SealingKey::new(&AES_256_GCM, key).unwrap();
82
+    let additional_data: [u8; 0] = [];
83
+
84
+    let mut out: Vec<u8> = repeat(0).take(data.len() + 16).collect();
85
+
86
+    let output_size = seal_in_place(&sealing_key,
87
+                                    &iv,
88
+                                    &additional_data,
89
+                                    &mut out,
90
+                                    16)
91
+        .unwrap();
92
+
93
+    return out
94
+}
95
+
96
+*/
97
+
98
+fn main() {
99
+    let args: Vec<String> = env::args().collect();
100
+
101
+    let mut enc= false;
102
+
103
+    if args.len() < 2 || args.len() > 3 {
104
+        panic!("Usage: {} <key> [enc]", args[0]);
105
+    } else if args.len() == 3 {
106
+        match &args[2][..] {
107
+            "enc" => { enc = true; }
108
+            _ => panic!("Usage: {} <key> [enc]", args[0])
109
+        }
110
+    }
111
+
112
+    //let message = "boba";
113
+    //println!("message: '{}' hex: '{}'", message, message.as_bytes().to_hex());
114
+    //let key_iv = "e910dd93e4bbf608957fcb25e08dd8e049e41b7b9d453b2b019bffe5726b1669bca9f40b5fdc0917cd84426df78f8236";
115
+    //let key_iv = "e910dd93e4bbf608957fcb2549e41b7b9d453b2b019bffe5726b1669bca9f40b5fdc0917cd84426df78f8236";
116
+    let key_iv = &args[1];
117
+
118
+    let mut key_iv = key_iv.from_hex().expect("Invalid hex for key");
119
+
120
+    //let iv_len = if key_iv.len() == 48 { 16 } else { 12 };
121
+    let iv_len = match key_iv.len() {
122
+        // todo: rust-crypto does not support non-12-byte nonce/iv 48 => 16,
123
+        44 => 12,
124
+        _ => panic!("only key lengths of 44 bytes are supported")
125
+    };
126
+    let (iv, key) = key_iv.split_at_mut(iv_len);
127
+    //let (iv, key) = key_iv.split_at_mut(if key_iv.len() == 48 { 16 } else { 12 }); // borrow checker explodes
128
+
129
+    //println!("key: '{:?}' iv: '{:?}'", key, iv);
130
+    //println!("key: '{}' iv: '{}'", key.to_hex(), iv.to_hex());
131
+    //let data = message.as_bytes();
132
+    let mut data : Vec<u8> = Vec::new();
133
+    stdin().read_to_end(& mut data).expect("error reading from stdin");
134
+
135
+    let data : Vec<u8> = if enc {
136
+        encrypt(&data, &key, &iv)
137
+    } else {
138
+        decrypt(&data, &key, &iv).expect("error during decryption")
139
+    };
140
+
141
+    // aesgcm bd92a7b359f5d9a359c6bbda242b1f4cb2f4c8d9
142
+    // rust   bd92a7b359f5d9a359c6bbda242b1f4cb2f4c8d9
143
+    // ring   dffdc5d2460bc181e2a34d139e72b32c0985018a
144
+
145
+    //println!("message: '{}' encrypted: '{}'", message, encrypted_data.to_hex());
146
+
147
+    //println!("decrypted: {:?}", String::from_utf8(decrypted_data).unwrap());
148
+    io::stdout().write(&data).expect("error writing to stdout");
149
+}

+ 19
- 0
test.sh View File

@@ -0,0 +1,19 @@
1
+#!/bin/bash
2
+
3
+# try different size files to encrypt/decrypt
4
+#dd if=/dev/urandom bs=1M count=100 of=/dev/shm/randombytes
5
+
6
+# compile C and rust code this way
7
+#gcc aesgcm.c -lcrypto -O3 -o aesgcm
8
+#cargo build --release
9
+
10
+export key=e910dd93e4bbf608957fcb2549e41b7b9d453b2b019bffe5726b1669bca9f40b5fdc0917cd84426df78f8236
11
+
12
+test () {
13
+    bin=$1
14
+    #tee >(md5sum - 1>&2) - < /dev/shm/randombytes | $bin $key enc | $bin $key | md5sum -
15
+    $bin $key enc < /dev/shm/randombytes | $bin $key &>/dev/null
16
+}
17
+
18
+time test ./aesgcm
19
+time test ./target/release/image_downloader

Loading…
Cancel
Save