1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! [Sodium](https://github.com/jedisct1/libsodium) is a modern, easy-to-use
//! software library for encryption, decryption, signatures, password hashing
//! and more.
//!
//! It is a portable, cross-compilable, installable, packageable fork of
//! [NaCl](http://nacl.cr.yp.to), with a compatible API, and an extended API to
//! improve usability even further.
//!
//! Its goal is to provide all of the core operations needed to build
//! higher-level cryptographic tools.
//!
//! Sodium supports a variety of compilers and operating systems, including
//! Windows (with MinGW or Visual Studio, x86 and x64), iOS and Android.
//!
//! The design choices emphasize security, and "magic constants" have clear
//! rationales.
//!
//! And despite the emphasis on high security, primitives are faster
//! across-the-board than most implementations of the NIST standards.
//!
//! Version 1.0.3 was released on May 9, 2015.
//! Building feature "latest" will build with version 1.0.4.
//!
//! [sodium-sys](https://github.com/rustyhorde/sodium-sys) is rust bindings for
//! [Sodium](https://github.com/jedisct1/libsodium).
#![allow(non_upper_case_globals)]
extern crate libc;
#[cfg(test)]
extern crate regex;

use std::ffi::NulError;
use std::str;
use std::string;

/// Crypography library modules.
pub mod crypto {
    pub mod asymmetrickey;
    pub mod hash;
    pub mod symmetrickey;
    pub mod utils;
}

pub use self::SSError::*;

#[derive(Debug)]
/// A sodium-sys error.  This is used to wrap various other errors to unify
/// the Result returns from the library.
pub enum SSError {
    /// An error returned from ```CString::new``` to indicate that a nul byte
    /// was found in the vector provided.
    CSTR(NulError),
    /// An error returned from functions that decrypt ciphertext.
    DECRYPT(&'static str),
    /// An error returned from functions that encrypt messages.
    ENCRYPT(&'static str),
    /// An error returned from functions that hash messages.
    HASH(&'static str),
    /// An error returned from functions the generate keypairs.
    KEYGEN(&'static str),
    /// An error returned from functions that generate MACs.
    MAC(&'static str),
    /// An error returned from signing functions.
    SIGN(&'static str),
    /// Errors which can occur when attempting to interpret a byte slice as a
    /// str.
    STR(str::Utf8Error),
    /// A possible error value from the String::from_utf8 function.
    STRING(string::FromUtf8Error),
    /// A possible error when verfiying a signed message.
    VERIFYSIGNED(&'static str),
}

impl From<NulError> for SSError {
    fn from(err: NulError) -> SSError {
        CSTR(err)
    }
}

impl From<str::Utf8Error> for SSError {
    fn from(err: str::Utf8Error) -> SSError {
        STR(err)
    }
}

impl From<string::FromUtf8Error> for SSError {
    fn from(err: string::FromUtf8Error) -> SSError {
        STRING(err)
    }
}