zerotier_api/
secret_key.rs

1use crate::InternalError;
2
3use arrayref::array_ref;
4use failure::Error;
5use std::convert::{TryFrom, TryInto};
6
7/// [`SecretKey`](struct.SecretKey.html) length in bytes.
8pub const SECRET_KEY_LENGTH: usize = 64;
9
10/// Concatenation of X25519 static secret (first 32 bytes) and Ed25519 secret key (last 32 bytes).
11pub struct SecretKey {
12    pub ed: ed25519_dalek::SecretKey,
13    pub dh: x25519_dalek::StaticSecret,
14}
15
16impl From<[u8; SECRET_KEY_LENGTH]> for SecretKey {
17    fn from(bytes: [u8; SECRET_KEY_LENGTH]) -> Self {
18        Self {
19            ed: bytes[32..].try_into().expect("slice with incorrect length"),
20            dh: x25519_dalek::StaticSecret::from(array_ref!(bytes, 0, 32).clone()),
21        }
22    }
23}
24
25impl TryFrom<&[u8]> for SecretKey {
26    type Error = Error;
27
28    fn try_from(bytes: &[u8]) -> Result<Self, Error> {
29        if bytes.len() != SECRET_KEY_LENGTH {
30            Err(InternalError::BytesLengthError.into())
31        } else {
32            Ok(Self {
33                ed: bytes[32..].try_into()?,
34                dh: x25519_dalek::StaticSecret::from(*array_ref!(bytes, 0, 32)),
35            })
36        }
37    }
38}