summaryrefslogtreecommitdiff
path: root/src/rust/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'src/rust/crypto')
-rw-r--r--src/rust/crypto/Cargo.toml6
-rw-r--r--src/rust/crypto/digests/sha2.rs47
-rw-r--r--src/rust/crypto/lib.rs26
-rw-r--r--src/rust/crypto/rand/mod.rs10
-rw-r--r--src/rust/crypto/rand/rng.rs4
5 files changed, 50 insertions, 43 deletions
diff --git a/src/rust/crypto/Cargo.toml b/src/rust/crypto/Cargo.toml
index 3d343344ae..869e0d6256 100644
--- a/src/rust/crypto/Cargo.toml
+++ b/src/rust/crypto/Cargo.toml
@@ -4,6 +4,7 @@ authors = ["The Tor Project",
name = "crypto"
version = "0.0.1"
publish = false
+build = "../build.rs"
[lib]
name = "crypto"
@@ -13,7 +14,7 @@ crate_type = ["rlib", "staticlib"]
[dependencies]
libc = "=0.2.39"
digest = "=0.7.2"
-rand_core = "=0.2.0-pre.0"
+rand_core = { version = "=0.2.0-pre.0", default-features = false }
external = { path = "../external" }
smartlist = { path = "../smartlist" }
@@ -22,7 +23,6 @@ tor_log = { path = "../tor_log" }
[dev-dependencies]
rand = { version = "=0.5.0-pre.2", default-features = false }
+rand_core = { version = "=0.2.0-pre.0", default-features = false }
[features]
-testing = ["tor_log/testing"]
-
diff --git a/src/rust/crypto/digests/sha2.rs b/src/rust/crypto/digests/sha2.rs
index 1cbb6c581e..03e0843dc0 100644
--- a/src/rust/crypto/digests/sha2.rs
+++ b/src/rust/crypto/digests/sha2.rs
@@ -43,10 +43,10 @@ pub struct Sha256 {
///
/// # Examples
///
-/// ```
-/// use crypto::digest::Sha256;
+/// ```rust,no_run
+/// use crypto::digests::sha2::{Sha256, Digest};
///
-/// let hasher: Sha256 = Sha256::default();
+/// let mut hasher: Sha256 = Sha256::default();
/// ```
///
/// # Returns
@@ -66,13 +66,13 @@ impl BlockInput for Sha256 {
///
/// # Examples
///
-/// ```
-/// use crypto::digest::Sha256;
+/// ```rust,no_run
+/// use crypto::digests::sha2::{Sha256, Digest};
///
-/// let hasher: Sha256 = Sha256::default();
+/// let mut hasher: Sha256 = Sha256::default();
///
-/// hasher.process(b"foo");
-/// hasher.process(b"bar");
+/// hasher.input(b"foo");
+/// hasher.input(b"bar");
/// ```
impl Input for Sha256 {
fn process(&mut self, msg: &[u8]) {
@@ -110,10 +110,10 @@ pub struct Sha512 {
///
/// # Examples
///
-/// ```
-/// use crypto::digest::Sha512;
+/// ```rust,no_run
+/// use crypto::digests::sha2::{Sha512, Digest};
///
-/// let hasher: Sha256 = Sha512::default();
+/// let mut hasher: Sha512 = Sha512::default();
/// ```
///
/// # Returns
@@ -133,13 +133,13 @@ impl BlockInput for Sha512 {
///
/// # Examples
///
-/// ```
-/// use crypto::digest::Sha512;
+/// ```rust,no_run
+/// use crypto::digests::sha2::{Sha512, Digest};
///
-/// let hasher: Sha512 = Sha512::default();
+/// let mut hasher: Sha512 = Sha512::default();
///
-/// hasher.process(b"foo");
-/// hasher.process(b"bar");
+/// hasher.input(b"foo");
+/// hasher.input(b"bar");
/// ```
impl Input for Sha512 {
fn process(&mut self, msg: &[u8]) {
@@ -154,7 +154,7 @@ impl Input for Sha512 {
// FIXME: Once const generics land in Rust, we should genericise calling
// crypto_digest_get_digest in external::crypto_digest.
impl FixedOutput for Sha512 {
- type OutputSize = U32;
+ type OutputSize = U64;
fn fixed_result(self) -> GenericArray<u8, Self::OutputSize> {
let buffer: [u8; DIGEST512_LEN] = get_512_bit_digest(self.engine);
@@ -178,6 +178,9 @@ mod test {
fn sha256_digest() {
let mut h: Sha256 = Sha256::new();
let mut result: [u8; DIGEST256_LEN] = [0u8; DIGEST256_LEN];
+ let expected = [151, 223, 53, 136, 181, 163, 242, 75, 171, 195,
+ 133, 27, 55, 47, 11, 167, 26, 157, 205, 222, 212,
+ 59, 20, 185, 208, 105, 97, 191, 193, 112, 125, 157];
h.input(b"foo");
h.input(b"bar");
@@ -187,7 +190,7 @@ mod test {
println!("{:?}", &result[..]);
- assert_eq!(&result[..], &b"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"[..]);
+ assert_eq!(result, expected);
}
#[test]
@@ -200,6 +203,12 @@ mod test {
let mut h: Sha512 = Sha512::new();
let mut result: [u8; DIGEST512_LEN] = [0u8; DIGEST512_LEN];
+ let expected = [203, 55, 124, 16, 176, 245, 166, 44, 128, 54, 37, 167,
+ 153, 217, 233, 8, 190, 69, 231, 103, 245, 209, 71, 212, 116,
+ 73, 7, 203, 5, 89, 122, 164, 237, 211, 41, 160, 175, 20, 122,
+ 221, 12, 244, 24, 30, 211, 40, 250, 30, 121, 148, 38, 88, 38,
+ 179, 237, 61, 126, 246, 240, 103, 202, 153, 24, 90];
+
h.input(b"foo");
h.input(b"bar");
h.input(b"baz");
@@ -208,6 +217,6 @@ mod test {
println!("{:?}", &result[..]);
- assert_eq!(&result[..], &b"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"[..]);
+ assert_eq!(&result[..], &expected[..]);
}
}
diff --git a/src/rust/crypto/lib.rs b/src/rust/crypto/lib.rs
index e7e3b22e03..f72a859dd7 100644
--- a/src/rust/crypto/lib.rs
+++ b/src/rust/crypto/lib.rs
@@ -9,19 +9,19 @@
//! The `digests` module contains submodules for specific hash digests
//! and extendable output functions.
//!
-//! ```
-//! use crypto::digests::sha256::Sha256;
+//! ```rust,no_run
+//! use crypto::digests::sha2::*;
//!
-//! let hasher: Sha256 = Sha256::default();
+//! let mut hasher: Sha256 = Sha256::default();
//! let mut result: [u8; 32] = [0u8; 32];
//!
-//! hasher.input("foo");
-//! hasher.input("bar");
-//! hasher.input("baz");
+//! hasher.input(b"foo");
+//! hasher.input(b"bar");
+//! hasher.input(b"baz");
//!
-//! result.copy_from_slice(hasher.result().as_bytes());
+//! result.copy_from_slice(hasher.result().as_slice());
//!
-//! assert!(result == "XXX");
+//! assert!(result == [b'X'; DIGEST256_LEN]);
//! ```
#[deny(missing_docs)]
@@ -29,9 +29,17 @@
// External crates from cargo or TOR_RUST_DEPENDENCIES.
extern crate digest;
extern crate libc;
+extern crate rand_core;
+
+// External dependencies for tests.
+#[cfg(test)]
+extern crate rand as rand_crate;
// Our local crates.
extern crate external;
+#[cfg(not(test))]
+#[macro_use]
+extern crate tor_log;
pub mod digests; // Unfortunately named "digests" plural to avoid name conflict with the digest crate
-
+pub mod rand;
diff --git a/src/rust/crypto/rand/mod.rs b/src/rust/crypto/rand/mod.rs
index 6b3058ad58..82d02a70bb 100644
--- a/src/rust/crypto/rand/mod.rs
+++ b/src/rust/crypto/rand/mod.rs
@@ -2,15 +2,5 @@
// Copyright (c) 2018, isis agora lovecruft
// See LICENSE for licensing information
-// External dependencies
-#[cfg(test)]
-extern crate rand;
-extern crate rand_core;
-
// Internal dependencies
-extern crate external;
-#[cfg(not(test))]
-#[macro_use]
-extern crate tor_log;
-
pub mod rng;
diff --git a/src/rust/crypto/rand/rng.rs b/src/rust/crypto/rand/rng.rs
index d5fae8a32e..07a0a7bdc7 100644
--- a/src/rust/crypto/rand/rng.rs
+++ b/src/rust/crypto/rand/rng.rs
@@ -130,8 +130,8 @@ mod internal {
#[cfg(test)]
mod internal {
// It doesn't matter if we pretend ChaCha is a CSPRNG in tests.
- pub use rand::ChaChaRng as TorRng;
- pub use rand::ChaChaRng as TorStrongestRng;
+ pub use rand_crate::ChaChaRng as TorRng;
+ pub use rand_crate::ChaChaRng as TorStrongestRng;
}
// Finally, expose the public functionality of whichever appropriate internal