Browse Source

Moved Hash, Signature and Key into the crypto module.

Matthew Carr 2 years ago
parent
commit
304162d309
3 changed files with 54 additions and 52 deletions
  1. 52 2
      crates/node/src/crypto.rs
  2. 1 50
      crates/node/src/main.rs
  3. 1 0
      crates/node/src/serde_tests.rs

+ 52 - 2
crates/node/src/crypto.rs

@@ -6,11 +6,61 @@ use openssl::{
 };
 
 /// Errors that can occur during cryptographic operations.
-pub(crate) enum Error {
+pub enum Error {
     NoReadCap
 }
 
-type Result<T> = std::result::Result<T, Error>;
+pub type Result<T> = std::result::Result<T, Error>;
+
+/// A cryptographic hash.
+#[allow(dead_code)]
+#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
+pub enum Hash {
+    Sha2_256([u8; 32]),
+    #[serde(with = "BigArray")]
+    Sha2_512([u8; 64]),
+}
+
+/// A cryptographic signature.
+#[allow(dead_code)]
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+pub enum Signature {
+    #[serde(with = "BigArray")]
+    Ed25519([u8; 64]),
+}
+
+/// A cryptographic key.
+#[allow(dead_code)]
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+pub enum Key {
+    Xsalsa20Poly1305([u8; 32]),
+}
+
+impl Key {
+    /// Returns the data in the key as a slice.
+    fn as_slice(&self) -> &[u8] {
+        let Key::Xsalsa20Poly1305(array) = self;
+        array
+    }
+
+    /// Returns the data in the key as a mutable slice.
+    fn as_mut_slice(&mut self) -> &mut [u8] {
+        let Key::Xsalsa20Poly1305(array) = self;
+        array
+    }
+}
+
+impl AsRef<[u8]> for Key {
+    fn as_ref(&self) -> &[u8] {
+        self.as_slice()
+    }
+}
+
+impl AsMut<[u8]> for Key {
+    fn as_mut(&mut self) -> &mut [u8] {
+        self.as_mut_slice()
+    }
+}
 
 #[allow(dead_code)]
 pub(crate) fn encrypt_block(

+ 1 - 50
crates/node/src/main.rs

@@ -11,6 +11,7 @@ use serde_big_array::BigArray;
 mod serde_tests;
 
 mod crypto;
+use crypto::{Hash, Signature, Key};
 
 /// A Block tagged with its version number.
 #[allow(dead_code)]
@@ -136,30 +137,6 @@ struct Epoch(i64);
 #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
 struct FragmentSerial(u32);
 
-/// A cryptographic hash.
-#[allow(dead_code)]
-#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
-enum Hash {
-    Sha2_256([u8; 32]),
-    #[serde(with = "BigArray")]
-    Sha2_512([u8; 64]),
-}
-
-/// A cryptographic signature.
-#[allow(dead_code)]
-#[derive(Debug, PartialEq, Serialize, Deserialize)]
-enum Signature {
-    #[serde(with = "BigArray")]
-    Ed25519([u8; 64]),
-}
-
-/// A cryptographic key.
-#[allow(dead_code)]
-#[derive(Debug, PartialEq, Serialize, Deserialize)]
-enum Key {
-    Xsalsa20Poly1305([u8; 32]),
-}
-
 fn main() {
     println!("Hello, world!");
 }
@@ -298,32 +275,6 @@ impl<'s> TryFrom<&'s str> for Path {
     }
 }
 
-impl Key {
-    /// Returns the data in the key as a slice.
-    fn as_slice(&self) -> &[u8] {
-        let Key::Xsalsa20Poly1305(array) = self;
-        array
-    }
-
-    /// Returns the data in the key as a mutable slice.
-    fn as_mut_slice(&mut self) -> &mut [u8] {
-        let Key::Xsalsa20Poly1305(array) = self;
-        array
-    }
-}
-
-impl AsRef<[u8]> for Key {
-    fn as_ref(&self) -> &[u8] {
-        self.as_slice()
-    }
-}
-
-impl AsMut<[u8]> for Key {
-    fn as_mut(&mut self) -> &mut [u8] {
-        self.as_mut_slice()
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;

+ 1 - 0
crates/node/src/serde_tests.rs

@@ -1,6 +1,7 @@
 /// Tests which ensure that the main data structures can be round-tripped faithfully.
 
 use super::*;
+use crypto::{Hash, Signature, Key};
 use serde_block_tree::{Error, Result, from_vec, to_vec};
 
 static PRINCIPAL: [u8; 32] = [