summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bin/server.rs7
-rw-r--r--src/hex.rs119
-rw-r--r--src/sign.rs33
3 files changed, 16 insertions, 143 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs
index ce72aaf..277e57e 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -52,6 +52,7 @@ extern crate yaml_rust;
extern crate log;
extern crate simple_logger;
extern crate mio;
+extern crate hex;
use std::env;
use std::process;
@@ -107,8 +108,8 @@ fn make_key_and_cert(seed: &[u8]) -> (Signer, Vec<u8>) {
let mut long_term_key = Signer::new(seed);
let ephemeral_key = create_ephemeral_key();
- info!("Long-term public key: {}", long_term_key.public_key_bytes().to_hex());
- info!("Ephemeral public key: {}", ephemeral_key.public_key_bytes().to_hex());
+ info!("Long-term public key: {}", hex::encode(long_term_key.public_key_bytes()));
+ info!("Ephemeral public key: {}", hex::encode(ephemeral_key.public_key_bytes()));
// Make DELE and sign it with long-term key
let dele_bytes = make_dele_bytes(&ephemeral_key).unwrap();
@@ -245,7 +246,7 @@ fn load_config(config_file: &str) -> (SocketAddr, Vec<u8>) {
let sock_addr: SocketAddr = addr.parse()
.expect(&format!("could not create socket address from {}", addr));
- let binseed = seed.from_hex()
+ let binseed = hex::decode(seed)
.expect("seed value invalid; 'seed' should be 32 byte hex value");
(sock_addr, binseed)
diff --git a/src/hex.rs b/src/hex.rs
deleted file mode 100644
index cbe6056..0000000
--- a/src/hex.rs
+++ /dev/null
@@ -1,119 +0,0 @@
-// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! Hex binary-to-text encoding
-
-pub use self::FromHexError::*;
-
-use std::fmt;
-use std::error;
-
-/// A trait for converting a value to hexadecimal encoding
-pub trait ToHex {
- /// Converts the value of `self` to a hex value, returning the owned
- /// string.
- fn to_hex(&self) -> String;
-}
-
-const CHARS: &[u8] = b"0123456789abcdef";
-
-impl ToHex for [u8] {
- /// Turn a vector of `u8` bytes into a hexadecimal string.
- fn to_hex(&self) -> String {
- let mut v = Vec::with_capacity(self.len() * 2);
- for &byte in self {
- v.push(CHARS[(byte >> 4) as usize]);
- v.push(CHARS[(byte & 0xf) as usize]);
- }
-
- unsafe { String::from_utf8_unchecked(v) }
- }
-}
-
-/// A trait for converting hexadecimal encoded values
-pub trait FromHex {
- /// Converts the value of `self`, interpreted as hexadecimal encoded data,
- /// into an owned vector of bytes, returning the vector.
- fn from_hex(&self) -> Result<Vec<u8>, FromHexError>;
-}
-
-/// Errors that can occur when decoding a hex encoded string
-#[derive(Copy, Clone, Debug)]
-pub enum FromHexError {
- /// The input contained a character not part of the hex format
- InvalidHexCharacter(char, usize),
- /// The input had an invalid length
- InvalidHexLength,
-}
-
-impl fmt::Display for FromHexError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match *self {
- InvalidHexCharacter(ch, idx) => {
- write!(f, "Invalid character '{}' at position {}", ch, idx)
- }
- InvalidHexLength => write!(f, "Invalid input length"),
- }
- }
-}
-
-impl error::Error for FromHexError {
- fn description(&self) -> &str {
- match *self {
- InvalidHexCharacter(..) => "invalid character",
- InvalidHexLength => "invalid length",
- }
- }
-}
-
-
-impl FromHex for str {
- /// Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
- /// to the byte values it encodes.
- ///
- /// You can use the `String::from_utf8` function to turn a
- /// `Vec<u8>` into a string with characters corresponding to those values.
- ///
- fn from_hex(&self) -> Result<Vec<u8>, FromHexError> {
- // This may be an overestimate if there is any whitespace
- let mut b = Vec::with_capacity(self.len() / 2);
- let mut modulus = 0;
- let mut buf = 0;
-
- for (idx, byte) in self.bytes().enumerate() {
- buf <<= 4;
-
- match byte {
- b'A'...b'F' => buf |= byte - b'A' + 10,
- b'a'...b'f' => buf |= byte - b'a' + 10,
- b'0'...b'9' => buf |= byte - b'0',
- b' ' | b'\r' | b'\n' | b'\t' => {
- buf >>= 4;
- continue;
- }
- _ => {
- let ch = self[idx..].chars().next().unwrap();
- return Err(InvalidHexCharacter(ch, idx));
- }
- }
-
- modulus += 1;
- if modulus == 2 {
- modulus = 0;
- b.push(buf);
- }
- }
-
- match modulus {
- 0 => Ok(b.into_iter().collect()),
- _ => Err(InvalidHexLength),
- }
- }
-}
diff --git a/src/sign.rs b/src/sign.rs
index c81ad38..d7ef156 100644
--- a/src/sign.rs
+++ b/src/sign.rs
@@ -20,6 +20,7 @@
extern crate ring;
extern crate untrusted;
+extern crate hex;
use self::ring::signature;
use self::ring::signature::Ed25519KeyPair;
@@ -92,17 +93,14 @@ impl Signer {
#[cfg(test)]
mod test {
- use hex::*;
use super::*;
#[test]
fn verify_ed25519_sig_on_empty_message() {
- let pubkey = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"
- .from_hex()
+ let pubkey = hex::decode("d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a")
.unwrap();
- let signature = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"
- .from_hex()
+ let signature = hex::decode("e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b")
.unwrap();
let v = Verifier::new(&pubkey);
@@ -112,14 +110,12 @@ mod test {
#[test]
fn verify_ed25519_sig() {
- let pubkey = "c0dac102c4533186e25dc43128472353eaabdb878b152aeb8e001f92d90233a7"
- .from_hex()
+ let pubkey = hex::decode("c0dac102c4533186e25dc43128472353eaabdb878b152aeb8e001f92d90233a7")
.unwrap();
- let message = "5f4c8989".from_hex().unwrap();
+ let message = hex::decode("5f4c8989").unwrap();
- let signature = "124f6fc6b0d100842769e71bd530664d888df8507df6c56dedfdb509aeb93416e26b918d38aa06305df3095697c18b2aa832eaa52edc0ae49fbae5a85e150c07"
- .from_hex()
+ let signature = hex::decode("124f6fc6b0d100842769e71bd530664d888df8507df6c56dedfdb509aeb93416e26b918d38aa06305df3095697c18b2aa832eaa52edc0ae49fbae5a85e150c07")
.unwrap();
let mut v = Verifier::new(&pubkey);
@@ -130,12 +126,10 @@ mod test {
#[test]
fn sign_ed25519_empty_message() {
- let seed = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"
- .from_hex()
+ let seed = hex::decode("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60")
.unwrap();
- let expected_sig = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"
- .from_hex()
+ let expected_sig = hex::decode("e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b")
.unwrap();
let mut s = Signer::new(&seed);
@@ -145,14 +139,12 @@ mod test {
#[test]
fn sign_ed25519_message() {
- let seed = "0d4a05b07352a5436e180356da0ae6efa0345ff7fb1572575772e8005ed978e9"
- .from_hex()
+ let seed = hex::decode("0d4a05b07352a5436e180356da0ae6efa0345ff7fb1572575772e8005ed978e9")
.unwrap();
- let message = "cbc77b".from_hex().unwrap();
+ let message = hex::decode("cbc77b").unwrap();
- let expected_sig = "d9868d52c2bebce5f3fa5a79891970f309cb6591e3e1702a70276fa97c24b3a8e58606c38c9758529da50ee31b8219cba45271c689afa60b0ea26c99db19b00c"
- .from_hex()
+ let expected_sig = hex::decode("d9868d52c2bebce5f3fa5a79891970f309cb6591e3e1702a70276fa97c24b3a8e58606c38c9758529da50ee31b8219cba45271c689afa60b0ea26c99db19b00c")
.unwrap();
let mut s = Signer::new(&seed);
@@ -163,8 +155,7 @@ mod test {
#[test]
fn sign_verify_round_trip() {
- let seed = "334a05b07352a5436e180356da0ae6efa0345ff7fb1572575772e8005ed978e9"
- .from_hex()
+ let seed = hex::decode("334a05b07352a5436e180356da0ae6efa0345ff7fb1572575772e8005ed978e9")
.unwrap();
let message = "Hello world".as_bytes();