Skip to main content

security/
key_agreement.rs

1use serde_json::Value;
2
3use crate::bridge;
4use crate::error::Result;
5
6#[derive(Debug)]
7/// Wraps a key-agreement public `SecKeyRef`.
8pub struct AgreementPublicKey {
9    handle: bridge::Handle,
10}
11
12impl AgreementPublicKey {
13    /// Wraps the corresponding public `SecKeyRef` agreement operation.
14    pub fn type_id() -> usize {
15        crate::key::key_type_id()
16    }
17
18    fn from_handle(handle: bridge::Handle) -> Self {
19        Self { handle }
20    }
21
22    /// Wraps the corresponding public `SecKeyRef` agreement operation.
23    pub fn attributes(&self) -> Result<Value> {
24        let mut status = 0;
25        let mut error = std::ptr::null_mut();
26        let raw = unsafe {
27            bridge::security_key_copy_attributes(self.handle.as_ptr(), &mut status, &mut error)
28        };
29        bridge::required_json("security_key_copy_attributes", raw, status, error)
30    }
31}
32
33#[derive(Debug)]
34/// Wraps a key-agreement private `SecKeyRef`.
35pub struct AgreementPrivateKey {
36    handle: bridge::Handle,
37}
38
39impl AgreementPrivateKey {
40    /// Wraps the corresponding private `SecKeyRef` agreement operation.
41    pub fn type_id() -> usize {
42        crate::key::key_type_id()
43    }
44
45    /// Wraps the corresponding private `SecKeyRef` agreement operation.
46    pub fn generate_p256() -> Result<Self> {
47        let mut status = 0;
48        let mut error = std::ptr::null_mut();
49        let raw = unsafe {
50            bridge::security_key_agreement_generate_p256_private_key(&mut status, &mut error)
51        };
52        bridge::required_handle(
53            "security_key_agreement_generate_p256_private_key",
54            raw,
55            status,
56            error,
57        )
58        .map(|handle| Self { handle })
59    }
60
61    /// Wraps the corresponding private `SecKeyRef` agreement operation.
62    pub fn public_key(&self) -> Result<AgreementPublicKey> {
63        let mut status = 0;
64        let mut error = std::ptr::null_mut();
65        let raw = unsafe {
66            bridge::security_key_copy_public_key(self.handle.as_ptr(), &mut status, &mut error)
67        };
68        bridge::required_handle("security_key_copy_public_key", raw, status, error)
69            .map(AgreementPublicKey::from_handle)
70    }
71
72    /// Wraps the corresponding private `SecKeyRef` agreement operation.
73    pub fn is_supported(&self) -> bool {
74        unsafe { bridge::security_key_agreement_is_supported(self.handle.as_ptr()) }
75    }
76
77    /// Wraps the corresponding private `SecKeyRef` agreement operation.
78    pub fn shared_secret(
79        &self,
80        peer_public_key: &AgreementPublicKey,
81        requested_size: usize,
82        shared_info: &[u8],
83    ) -> Result<Vec<u8>> {
84        let mut status = 0;
85        let mut error = std::ptr::null_mut();
86        let raw = unsafe {
87            bridge::security_key_agreement_compute_shared_secret(
88                self.handle.as_ptr(),
89                peer_public_key.handle.as_ptr(),
90                bridge::len_to_isize(requested_size)?,
91                shared_info.as_ptr().cast(),
92                bridge::len_to_isize(shared_info.len())?,
93                &mut status,
94                &mut error,
95            )
96        };
97        bridge::required_data(
98            "security_key_agreement_compute_shared_secret",
99            raw,
100            status,
101            error,
102        )
103    }
104}