libsignal_protocol/keys/
private.rs1use crate::{
2 errors::FromInternalErrorCode, keys::PublicKey, raw_ptr::Raw, Context,
3};
4use failure::Error;
5use std::{
6 cmp::{Ord, Ordering},
7 ptr,
8};
9
10#[derive(Clone, Debug)]
12pub struct PrivateKey {
13 pub(crate) raw: Raw<sys::ec_private_key>,
14}
15
16impl PrivateKey {
17 pub fn decode_point(
19 ctx: &Context,
20 key: &[u8],
21 ) -> Result<PrivateKey, Error> {
22 unsafe {
23 let mut raw = ptr::null_mut();
24 sys::curve_decode_private_point(
25 &mut raw,
26 key.as_ptr(),
27 key.len(),
28 ctx.raw(),
29 )
30 .into_result()?;
31
32 Ok(PrivateKey {
33 raw: Raw::from_ptr(raw),
34 })
35 }
36 }
37
38 pub fn generate_public_key(&self) -> Result<PublicKey, Error> {
40 unsafe {
41 let mut raw = ptr::null_mut();
42 sys::curve_generate_public_key(&mut raw, self.raw.as_const_ptr())
43 .into_result()?;
44
45 Ok(PublicKey {
46 raw: Raw::from_ptr(raw),
47 })
48 }
49 }
50}
51
52impl Ord for PrivateKey {
53 fn cmp(&self, other: &PrivateKey) -> Ordering {
54 let cmp = unsafe {
55 sys::ec_private_key_compare(
56 self.raw.as_const_ptr(),
57 other.raw.as_const_ptr(),
58 )
59 };
60
61 if cmp < 0 {
62 Ordering::Less
63 } else if cmp > 0 {
64 Ordering::Greater
65 } else {
66 Ordering::Equal
67 }
68 }
69}
70
71impl PartialEq for PrivateKey {
72 fn eq(&self, other: &PrivateKey) -> bool {
73 self.cmp(other) == Ordering::Equal
74 }
75}
76
77impl Eq for PrivateKey {}
78
79impl PartialOrd for PrivateKey {
80 fn partial_cmp(&self, other: &PrivateKey) -> Option<Ordering> {
81 Some(self.cmp(other))
82 }
83}
84
85impl_serializable!(PrivateKey, ec_private_key_serialize, asd);