1use crate::aws_lc::{BN_bin2bn, BN_bn2bin, BN_new, BN_num_bytes, BN_set_u64, BIGNUM};
5use crate::ptr::{ConstPointer, DetachableLcPtr, LcPtr};
6use core::ptr::null_mut;
7
8impl TryFrom<&[u8]> for LcPtr<BIGNUM> {
9 type Error = ();
10
11 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
12 unsafe { LcPtr::new(BN_bin2bn(bytes.as_ptr(), bytes.len(), null_mut())) }
13 }
14}
15
16impl TryFrom<&[u8]> for DetachableLcPtr<BIGNUM> {
17 type Error = ();
18
19 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
20 unsafe { DetachableLcPtr::new(BN_bin2bn(bytes.as_ptr(), bytes.len(), null_mut())) }
21 }
22}
23
24impl TryFrom<u64> for DetachableLcPtr<BIGNUM> {
25 type Error = ();
26
27 fn try_from(value: u64) -> Result<Self, Self::Error> {
28 unsafe {
29 let bn = DetachableLcPtr::new(BN_new())?;
30 if 1 != BN_set_u64(*bn, value) {
31 return Err(());
32 }
33 Ok(bn)
34 }
35 }
36}
37
38impl ConstPointer<'_, BIGNUM> {
39 pub(crate) fn to_be_bytes(&self) -> Vec<u8> {
40 unsafe {
41 let bn_bytes = BN_num_bytes(**self);
42 let mut byte_vec = Vec::with_capacity(bn_bytes as usize);
43 let out_bytes = BN_bn2bin(**self, byte_vec.as_mut_ptr());
44 debug_assert_eq!(out_bytes, bn_bytes as usize);
45 byte_vec.set_len(out_bytes);
46 byte_vec
47 }
48 }
49}