Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 79fffa7

Browse files
Merge pull request #17 from franziskuskiefer/0.0.5
0.0.5
2 parents 9b3497b + 79a732a commit 79fffa7

File tree

14 files changed

+151
-11
lines changed

14 files changed

+151
-11
lines changed

.github/workflows/evercrypt-rs.yml

+29
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,32 @@ jobs:
5555
run: |
5656
cd evercrypt-rs
5757
cargo bench --verbose --features rust-crypto-aes
58+
fuzz:
59+
strategy:
60+
fail-fast: false
61+
matrix:
62+
os:
63+
- macos-latest
64+
- ubuntu-latest
65+
runs-on: ${{ matrix.os }}
66+
steps:
67+
- uses: actions/checkout@v2
68+
with:
69+
submodules: true
70+
- name: Install latest nightly
71+
uses: actions-rs/toolchain@v1
72+
with:
73+
toolchain: nightly
74+
default: true
75+
- uses: actions-rs/[email protected]
76+
with:
77+
crate: cargo-fuzz
78+
version: latest
79+
- name: Fuzz AEAD
80+
run: |
81+
cd evercrypt-rs
82+
cargo fuzz run aead -- -runs=1000000
83+
- name: Fuzz ECDH
84+
run: |
85+
cd evercrypt-rs
86+
cargo fuzz run ecdh -- -runs=1000000

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[workspace]
22
members = [
33
"evercrypt-rs",
4-
"evercrypt-sys"
4+
"evercrypt-sys",
55
]
66

77
[patch.crates-io]

evercrypt-rs/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "evercrypt"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
authors = ["Franziskus Kiefer <[email protected]>"]
55
edition = "2018"
66
license = "MPL-2.0"
@@ -22,7 +22,7 @@ random = ["rand", "rand_core"]
2222
serialization = ["serde", "serde_json"]
2323

2424
[dependencies]
25-
evercrypt-sys = { version = "0.0.4" }
25+
evercrypt-sys = { version = "0.0.5" }
2626
aes-gcm = { version = "0.8", optional = true }
2727
rand = { version = "0.7", optional = true }
2828
rand_core = { version = "0.5", optional = true }

evercrypt-rs/fuzz/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
target
3+
corpus
4+
artifacts

evercrypt-rs/fuzz/Cargo.toml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
[package]
3+
name = "evercrypt-fuzz"
4+
version = "0.0.0"
5+
authors = ["Automatically generated"]
6+
publish = false
7+
edition = "2018"
8+
9+
[package.metadata]
10+
cargo-fuzz = true
11+
12+
[dependencies]
13+
libfuzzer-sys = "0.3"
14+
15+
[dependencies.evercrypt]
16+
path = ".."
17+
18+
[patch.crates-io]
19+
evercrypt-sys = { path = "../../evercrypt-sys" }
20+
21+
# Prevent this from interfering with workspaces
22+
[workspace]
23+
members = ["."]
24+
25+
[[bin]]
26+
name = "ecdh"
27+
path = "fuzz_targets/ecdh.rs"
28+
test = false
29+
doc = false
30+
31+
[[bin]]
32+
name = "aead"
33+
path = "fuzz_targets/aead.rs"
34+
test = false
35+
doc = false
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
use evercrypt::prelude::*;
5+
6+
fuzz_target!(|data: &[u8]| {
7+
let modes = [
8+
AeadMode::Aes128Gcm,
9+
AeadMode::Aes256Gcm,
10+
AeadMode::Chacha20Poly1305,
11+
];
12+
for &mode in modes.iter() {
13+
let nonce = aead_nonce_gen(mode);
14+
let enc_result = aead_encrypt(mode, data, data, &nonce, &[]);
15+
let (c, t) = if let Ok((c, t)) = enc_result {
16+
(c, t)
17+
} else {
18+
if data.len() != 16 {
19+
return;
20+
}
21+
let mut tag = [0u8; 16];
22+
tag.clone_from_slice(data);
23+
(data.to_vec(), tag)
24+
};
25+
let dec_result = aead_decrypt(mode, data, &c, &t, &nonce, &[]);
26+
if let Ok(ptxt) = dec_result {
27+
assert_eq!(ptxt, data);
28+
}
29+
}
30+
});
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
use evercrypt::prelude::*;
5+
6+
fuzz_target!(|data: &[u8]| {
7+
let _ = ecdh_derive(EcdhMode::X25519, data, data);
8+
let _ = ecdh_derive_base(EcdhMode::X25519, data);
9+
});

evercrypt-rs/src/aead.rs

+18
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub enum Error {
124124
UnsupportedConfig = 4,
125125
Encrypting = 5,
126126
Decrypting = 6,
127+
InvalidKeySize = 7,
127128
}
128129

129130
/// The Aead struct allows to re-use a key without having to initialize it
@@ -191,6 +192,11 @@ impl Aead {
191192
/// If the algorithm is not supported or the state generation fails, this
192193
/// function returns an `Error`.
193194
pub fn new(alg: Mode, k: &[u8]) -> Result<Self, Error> {
195+
// Check key lengths. Evercrypt is not doing this.
196+
if k.len() != key_size(&alg) {
197+
return Err(Error::InvalidKeySize);
198+
}
199+
194200
unsafe {
195201
// Make sure this happened.
196202
EverCrypt_AutoConfig2_init();
@@ -388,6 +394,18 @@ impl Aead {
388394
}
389395
}
390396

397+
impl Drop for Aead {
398+
fn drop(&mut self) {
399+
if let Some(c_state) = self.c_state {
400+
unsafe { EverCrypt_AEAD_free(c_state) }
401+
}
402+
// This will probably be optimised out. But it's only best effort for
403+
// now.
404+
let zero_key: Vec<u8> = (0u8..self.key.len() as u8).collect();
405+
let _ = std::mem::replace(&mut self.key, zero_key);
406+
}
407+
}
408+
391409
// Single-shot APIs
392410

393411
/// Single-shot API for AEAD encryption.

evercrypt-rs/src/ecdh.rs

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use crate::x25519;
4848
#[derive(Debug, PartialEq)]
4949
pub enum Error {
5050
InvalidPoint,
51+
InvalidScalar,
5152
UnkownAlgorithm,
5253
}
5354

@@ -64,6 +65,12 @@ pub enum Mode {
6465
pub fn derive(mode: Mode, p: &[u8], s: &[u8]) -> Result<Vec<u8>, Error> {
6566
match mode {
6667
Mode::X25519 => {
68+
if p.len() != 32 {
69+
return Err(Error::InvalidPoint);
70+
}
71+
if s.len() != 32 {
72+
return Err(Error::InvalidScalar);
73+
}
6774
let mut point = [0u8; 32];
6875
point.clone_from_slice(p);
6976
let mut scalar = [0u8; 32];
@@ -85,6 +92,9 @@ pub fn derive(mode: Mode, p: &[u8], s: &[u8]) -> Result<Vec<u8>, Error> {
8592
pub fn derive_base(mode: Mode, s: &[u8]) -> Result<Vec<u8>, Error> {
8693
match mode {
8794
Mode::X25519 => {
95+
if s.len() != 32 {
96+
return Err(Error::InvalidScalar);
97+
}
8898
let mut scalar = [0u8; 32];
8999
scalar.clone_from_slice(s);
90100

evercrypt-rs/src/p256.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ pub fn validate_pk(pk: &[u8]) -> Result<[u8; 64], Error> {
1919

2020
// Parse the public key.
2121
let mut public = [0u8; 64];
22-
let uncompressed_point = unsafe {
23-
Hacl_P256_decompression_not_compressed_form(pk.as_ptr() as _, public.as_mut_ptr())
22+
let uncompressed_point = if pk.len() < 65 {
23+
false
24+
} else {
25+
unsafe {
26+
Hacl_P256_decompression_not_compressed_form(pk.as_ptr() as _, public.as_mut_ptr())
27+
}
2428
};
25-
let compressed_point = if !uncompressed_point {
29+
let compressed_point = if !uncompressed_point && pk.len() >= 33 {
2630
unsafe { Hacl_P256_decompression_compressed_form(pk.as_ptr() as _, public.as_mut_ptr()) }
2731
} else {
2832
false

evercrypt-sys/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "evercrypt-sys"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
authors = ["Franziskus Kiefer <[email protected]>"]
55
edition = "2018"
66
build = "build.rs"

evercrypt-sys/build.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,15 @@ fn main() {
300300
let hacl_src_path_str = hacl_src_path.to_str().unwrap();
301301

302302
// Build hacl/evercrypt
303-
if rebuild(home_dir, &out_path) {
303+
// Always rebuild on windows for now. TODO: fix rebuild check on Windows.
304+
if build_config.windows || rebuild(home_dir, &out_path) {
304305
// Only rebuild if the hacl revision changed.
305306
copy_hacl_to_out(&out_path, &hacl_src_path);
306307
build_hacl(&hacl_src_path, &build_config);
307308
}
308309

309310
// Generate new bindings if not on Windows.
310-
if !cfg.windows {
311+
if !build_config.windows {
311312
create_bindings(&hacl_dir, hacl_src_path_str, home_dir);
312313
}
313314

evercrypt-sys/hacl-build.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
echo off
22
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat" -host_arch=amd64 -arch=amd64
3-
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat" -test
3+
@REM call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat" -test
44
cd %~dp0
55
cl *.c /I ../kremlin/include /I . /I ../kremlin/kremlib/dist/minimal /c || goto :error
66
for /F %%i in ('dir /b *-x86_64-msvc.asm') do (

evercrypt-sys/hacl-star

0 commit comments

Comments
 (0)