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
use std::fmt;

use crate::{ed25519::private::Ed25519PrivateKey, key_utils};

pub struct X25519PrivateKey {
    pub raw: x25519_dalek::StaticSecret,
}

impl Clone for X25519PrivateKey {
    fn clone(&self) -> Self {
        X25519PrivateKey {
            raw: self.raw.clone(),
        }
    }
}

impl fmt::Debug for X25519PrivateKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("X25519PrivateKey").finish()
    }
}

impl From<&Ed25519PrivateKey> for X25519PrivateKey {
    fn from(private_key: &Ed25519PrivateKey) -> X25519PrivateKey {
        X25519PrivateKey {
            raw: key_utils::ed25519_private_key_to_x25519(&private_key.raw),
        }
    }
}

impl Drop for X25519PrivateKey {
    fn drop(&mut self) {
        if cfg!(feature = "drop-log-enable") {
            println!("The X25519PrivateKey is being deallocated");
        }
    }
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub extern "C" fn c_x25519_private_key_destroy(private_key: &mut X25519PrivateKey) {
    let _ = unsafe { Box::from_raw(private_key) };
}