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
//! Pure Rust implementations of static Diffie-Hellman key-exchange. 
//! It includes impls for both plain vanilla DH and elliptic-curve DH.

#![no_std]
#![deny(unsafe_code)]
#![deny(missing_docs)]

/// ECDH implementation 
pub mod ecdh;
/// DH implementation
pub mod dh;
/// A module to import Hash Types from RustCrypto
pub mod digest;
/// ECDSA implementation
pub mod signatures;
pub mod constants;


use core::fmt;

/// The CryptoError type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CryptoError {

    /// Error while performing an EC Crypto operation
    ECCError,
    /// Invalid encoding
    InvalidEncoding,
    /// Signature Error
    SignatureError,

    #[doc(hidden)]
    __Nonexhaustive,
}

/// The result type for Crypto operations
pub type Result<T> = core::result::Result<T, CryptoError>;

impl fmt::Display for CryptoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &CryptoError::ECCError              => write!(f, "EC Crypto operation failed"),
            &CryptoError::InvalidEncoding       => write!(f, "Invalid encoding"),
            &CryptoError::SignatureError        => write!(f, "Signature Error"),
            &CryptoError::__Nonexhaustive       => unreachable!(),
        }
    }   
}

impl From<p256::elliptic_curve::Error> for CryptoError {
    fn from(_error: p256::elliptic_curve::Error) -> Self {
        CryptoError::ECCError
    }
}