Skip to content

Commit 92347c3

Browse files
committed
支持wg
1 parent 57b3a61 commit 92347c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+10076
-525
lines changed

Cargo.lock

Lines changed: 231 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ log4rs = "1.3"
1313
dirs = "5"
1414
crossbeam = "0.8"
1515
parking_lot = "0.12"
16-
dashmap = "5.5"
16+
dashmap = "6.0.1"
1717

18-
rsa = { version = "0.7.2", features = [] }
19-
spki = { version = "0.6.0", features = ["fingerprint", "alloc"] }
18+
rsa = { version = "0.9.6", features = [] }
19+
spki = { version = "0.7.3", features = ["fingerprint", "alloc", "base64"] }
2020
aes-gcm = { version = "0.10.2", optional = true }
2121
ring = { version = "0.17", optional = true }
2222
rand = "0.8"
@@ -39,6 +39,10 @@ actix-files = { version = "0.6", optional = true }
3939
actix-web-static-files = { version = "4.0.1", optional = true }
4040
tokio-tungstenite = "0.23.1"
4141

42+
boringtun = { path = "lib/boringtun", features = [] }
43+
ipnetwork = "0.20.0"
44+
base64 = "0.22.1"
45+
4246
serde = { version = "1", features = ["derive"] }
4347
crossbeam-utils = "0.8"
4448
futures-util = "0.3"

lib/boringtun/Cargo.toml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[package]
2+
name = "boringtun"
3+
description = "an implementation of the WireGuard® protocol designed for portability and speed"
4+
version = "0.6.0"
5+
authors = [
6+
"Noah Kennedy <[email protected]>",
7+
"Andy Grover <[email protected]>",
8+
"Jeff Hiner <[email protected]>",
9+
]
10+
license = "BSD-3-Clause"
11+
repository = "https://github.com/cloudflare/boringtun"
12+
documentation = "https://docs.rs/boringtun/0.5.2/boringtun/"
13+
edition = "2018"
14+
15+
[features]
16+
default = []
17+
device = ["socket2", "thiserror"]
18+
jni-bindings = ["ffi-bindings", "jni"]
19+
ffi-bindings = ["tracing-subscriber"]
20+
# mocks std::time::Instant with mock_instant
21+
mock-instant = ["mock_instant"]
22+
23+
[dependencies]
24+
base64 = "0.13"
25+
hex = "0.4"
26+
untrusted = "0.9.0"
27+
libc = "0.2"
28+
parking_lot = "0.12"
29+
tracing = "0.1.40"
30+
tracing-subscriber = { version = "0.3", features = ["fmt"], optional = true }
31+
ip_network = "0.4.1"
32+
ip_network_table = "0.2.0"
33+
ring = "0.17"
34+
x25519-dalek = { version = "2.0.0", features = [
35+
"reusable_secrets",
36+
"static_secrets",
37+
] }
38+
rand_core = { version = "0.6.4", features = ["getrandom"] }
39+
chacha20poly1305 = "0.10.0-pre.1"
40+
aead = "0.5.0-pre.2"
41+
blake2 = "0.10"
42+
hmac = "0.12"
43+
jni = { version = "0.19.0", optional = true }
44+
mock_instant = { version = "0.3", optional = true }
45+
socket2 = { version = "0.4.7", features = ["all"], optional = true }
46+
thiserror = { version = "1", optional = true }
47+
48+
[target.'cfg(unix)'.dependencies]
49+
nix = { version = "0.25", default-features = false, features = [
50+
"time",
51+
"user",
52+
] }
53+
54+
[dev-dependencies]
55+
etherparse = "0.13"
56+
tracing-subscriber = "0.3"
57+
criterion = { version = "0.3.5", features = ["html_reports"] }
58+
59+
[lib]
60+
crate-type = ["staticlib", "cdylib", "rlib"]
61+
62+
[[bench]]
63+
name = "crypto_benches"
64+
harness = false
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use blake2::digest::{FixedOutput, KeyInit};
2+
use blake2::{Blake2s256, Blake2sMac, Digest};
3+
use criterion::{BenchmarkId, Criterion, Throughput};
4+
use ring::rand::{SecureRandom, SystemRandom};
5+
6+
pub fn bench_blake2s_hash(c: &mut Criterion) {
7+
let mut group = c.benchmark_group("blake2s_hash");
8+
9+
group.sample_size(1000);
10+
11+
for size in [32, 64, 128] {
12+
group.throughput(Throughput::Bytes(size as u64));
13+
14+
group.bench_with_input(BenchmarkId::new("blake2s_crate", size), &size, |b, _| {
15+
let buf_in = vec![0u8; size];
16+
17+
b.iter(|| {
18+
let mut hasher = Blake2s256::new();
19+
hasher.update(&buf_in);
20+
hasher.finalize();
21+
});
22+
});
23+
}
24+
25+
group.finish();
26+
}
27+
28+
pub fn bench_blake2s_hmac(c: &mut Criterion) {
29+
let mut group = c.benchmark_group("blake2s_hmac");
30+
31+
group.sample_size(1000);
32+
33+
for size in [16, 32] {
34+
group.throughput(Throughput::Bytes(size as u64));
35+
36+
group.bench_with_input(BenchmarkId::new("blake2s_crate", size), &size, |b, _| {
37+
let buf_in = vec![0u8; size];
38+
let rng = SystemRandom::new();
39+
40+
b.iter_batched(
41+
|| {
42+
let mut key = [0u8; 32];
43+
rng.fill(&mut key).unwrap();
44+
key
45+
},
46+
|key| {
47+
use blake2::digest::Update;
48+
type HmacBlake2s = hmac::SimpleHmac<blake2::Blake2s256>;
49+
let mut hmac = HmacBlake2s::new_from_slice(&key).unwrap();
50+
hmac.update(&buf_in);
51+
hmac.finalize_fixed();
52+
},
53+
criterion::BatchSize::SmallInput,
54+
);
55+
});
56+
}
57+
58+
group.finish();
59+
}
60+
61+
pub fn bench_blake2s_keyed(c: &mut Criterion) {
62+
let mut group = c.benchmark_group("blake2s_keyed_mac");
63+
64+
group.sample_size(1000);
65+
66+
for size in [128, 1024] {
67+
group.throughput(Throughput::Bytes(size as u64));
68+
69+
group.bench_with_input(BenchmarkId::new("blake2s_crate", size), &size, |b, _| {
70+
let buf_in = vec![0u8; size];
71+
let rng = SystemRandom::new();
72+
73+
b.iter_batched(
74+
|| {
75+
let mut key = [0u8; 16];
76+
rng.fill(&mut key).unwrap();
77+
key
78+
},
79+
|key| -> [u8; 16] {
80+
let mut hmac = Blake2sMac::new_from_slice(&key).unwrap();
81+
blake2::digest::Update::update(&mut hmac, &buf_in);
82+
hmac.finalize_fixed().into()
83+
},
84+
criterion::BatchSize::SmallInput,
85+
);
86+
});
87+
}
88+
89+
group.finish();
90+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use aead::{AeadInPlace, KeyInit};
2+
use criterion::{BenchmarkId, Criterion, Throughput};
3+
use rand_core::{OsRng, RngCore};
4+
use ring::aead::{Aad, LessSafeKey, Nonce, UnboundKey, CHACHA20_POLY1305};
5+
6+
fn chacha20poly1305_ring(key_bytes: &[u8], buf: &mut [u8]) {
7+
let len = buf.len();
8+
let n = len - 16;
9+
10+
let key = LessSafeKey::new(UnboundKey::new(&CHACHA20_POLY1305, key_bytes).unwrap());
11+
12+
let tag = key
13+
.seal_in_place_separate_tag(
14+
Nonce::assume_unique_for_key([0u8; 12]),
15+
Aad::from(&[]),
16+
&mut buf[..n],
17+
)
18+
.unwrap();
19+
20+
buf[n..].copy_from_slice(tag.as_ref())
21+
}
22+
23+
fn chacha20poly1305_non_ring(key_bytes: &[u8], buf: &mut [u8]) {
24+
let len = buf.len();
25+
let n = len - 16;
26+
27+
let aead = chacha20poly1305::ChaCha20Poly1305::new_from_slice(key_bytes).unwrap();
28+
let nonce = chacha20poly1305::Nonce::default();
29+
30+
let tag = aead
31+
.encrypt_in_place_detached(&nonce, &[], &mut buf[..n])
32+
.unwrap();
33+
34+
buf[n..].copy_from_slice(tag.as_ref());
35+
}
36+
37+
pub fn bench_chacha20poly1305(c: &mut Criterion) {
38+
let mut group = c.benchmark_group("chacha20poly1305");
39+
40+
group.sample_size(1000);
41+
42+
for size in [128, 192, 1400, 8192] {
43+
group.throughput(Throughput::Bytes(size as u64));
44+
45+
group.bench_with_input(
46+
BenchmarkId::new("chacha20poly1305_ring", size),
47+
&size,
48+
|b, i| {
49+
let mut key = [0; 32];
50+
let mut buf = vec![0; i + 16];
51+
52+
let mut rng = OsRng::default();
53+
54+
rng.fill_bytes(&mut key);
55+
rng.fill_bytes(&mut buf);
56+
57+
b.iter(|| chacha20poly1305_ring(&key, &mut buf));
58+
},
59+
);
60+
61+
group.bench_with_input(
62+
BenchmarkId::new("chacha20poly1305_non_ring", size),
63+
&size,
64+
|b, i| {
65+
let mut key = [0; 32];
66+
let mut buf = vec![0; i + 16];
67+
68+
let mut rng = OsRng::default();
69+
70+
rng.fill_bytes(&mut key);
71+
rng.fill_bytes(&mut buf);
72+
73+
b.iter(|| chacha20poly1305_non_ring(&key, &mut buf));
74+
},
75+
);
76+
}
77+
78+
group.finish();
79+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use blake2s_benching::{bench_blake2s_hash, bench_blake2s_hmac, bench_blake2s_keyed};
2+
use chacha20poly1305_benching::bench_chacha20poly1305;
3+
use x25519_public_key_benching::bench_x25519_public_key;
4+
use x25519_shared_key_benching::bench_x25519_shared_key;
5+
6+
mod blake2s_benching;
7+
mod chacha20poly1305_benching;
8+
mod x25519_public_key_benching;
9+
mod x25519_shared_key_benching;
10+
11+
criterion::criterion_group!(
12+
crypto_benches,
13+
bench_chacha20poly1305,
14+
bench_blake2s_hash,
15+
bench_blake2s_hmac,
16+
bench_blake2s_keyed,
17+
bench_x25519_shared_key,
18+
bench_x25519_public_key
19+
);
20+
criterion::criterion_main!(crypto_benches);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use criterion::Criterion;
2+
use rand_core::OsRng;
3+
4+
pub fn bench_x25519_public_key(c: &mut Criterion) {
5+
let mut group = c.benchmark_group("x25519_public_key");
6+
7+
group.sample_size(1000);
8+
9+
group.bench_function("x25519_public_key_dalek", |b| {
10+
b.iter(|| {
11+
let secret_key = x25519_dalek::StaticSecret::random_from_rng(OsRng);
12+
let public_key = x25519_dalek::PublicKey::from(&secret_key);
13+
14+
(secret_key, public_key)
15+
});
16+
});
17+
18+
group.bench_function("x25519_public_key_ring", |b| {
19+
let rng = ring::rand::SystemRandom::new();
20+
21+
b.iter(|| {
22+
let my_private_key =
23+
ring::agreement::EphemeralPrivateKey::generate(&ring::agreement::X25519, &rng)
24+
.unwrap();
25+
my_private_key.compute_public_key().unwrap()
26+
});
27+
});
28+
29+
group.finish();
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use criterion::{BatchSize, Criterion};
2+
use rand_core::OsRng;
3+
4+
pub fn bench_x25519_shared_key(c: &mut Criterion) {
5+
let mut group = c.benchmark_group("x25519_shared_key");
6+
7+
group.sample_size(1000);
8+
9+
group.bench_function("x25519_shared_key_dalek", |b| {
10+
let public_key =
11+
x25519_dalek::PublicKey::from(&x25519_dalek::StaticSecret::random_from_rng(OsRng));
12+
13+
b.iter_batched(
14+
|| x25519_dalek::StaticSecret::random_from_rng(OsRng),
15+
|secret_key| secret_key.diffie_hellman(&public_key),
16+
BatchSize::SmallInput,
17+
);
18+
});
19+
20+
group.bench_function("x25519_shared_key_ring", |b| {
21+
let rng = ring::rand::SystemRandom::new();
22+
23+
let peer_public_key = {
24+
let peer_private_key =
25+
ring::agreement::EphemeralPrivateKey::generate(&ring::agreement::X25519, &rng)
26+
.unwrap();
27+
peer_private_key.compute_public_key().unwrap()
28+
};
29+
let peer_public_key_alg = &ring::agreement::X25519;
30+
31+
let my_public_key =
32+
ring::agreement::UnparsedPublicKey::new(peer_public_key_alg, &peer_public_key);
33+
34+
b.iter_batched(
35+
|| {
36+
ring::agreement::EphemeralPrivateKey::generate(&ring::agreement::X25519, &rng)
37+
.unwrap()
38+
},
39+
|my_private_key| {
40+
ring::agreement::agree_ephemeral(my_private_key, &my_public_key, |_key_material| ())
41+
.unwrap()
42+
},
43+
BatchSize::SmallInput,
44+
);
45+
});
46+
47+
group.finish();
48+
}

0 commit comments

Comments
 (0)