added support for cloning on EcKey

This commit is contained in:
mamoniot 2023-03-13 16:12:57 -04:00
parent e64fab8b9d
commit b3bd64504b
No known key found for this signature in database
GPG key ID: ADCCDBBE0E3D3B3B

View file

@ -1,3 +1,4 @@
use core::fmt;
use std::ptr;
use crate::bn::{BigNumContext, BigNumContextRef, BigNumRef};
@ -5,13 +6,11 @@ use crate::error::{cvt, cvt_n, cvt_p, ErrorStack};
use foreign_types::{foreign_type, ForeignType, ForeignTypeRef};
foreign_type! {
#[derive(Clone)]
pub unsafe type EcGroup: Send + Sync {
type CType = ffi::EC_GROUP;
fn drop = ffi::EC_GROUP_free;
}
/// Public and optional private key on the given curve.
#[derive(Clone)]
pub unsafe type EcKey {
type CType = ffi::EC_KEY;
fn drop = ffi::EC_KEY_free;
@ -125,3 +124,27 @@ impl EcPointRef {
}
}
}
impl ToOwned for EcKeyRef {
type Owned = EcKey;
fn to_owned(&self) -> EcKey {
unsafe {
let r = ffi::EC_KEY_up_ref(self.as_ptr());
assert!(r == 1);
EcKey::from_ptr(self.as_ptr())
}
}
}
impl Clone for EcKey {
fn clone(&self) -> EcKey {
(**self).to_owned()
}
}
impl fmt::Debug for EcKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "EcKey")
}
}