Skip to content

Commit 7e2b18f

Browse files
Added multi-part signing/verification bindings
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent 189f88e commit 7e2b18f

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

cryptoki/src/session/signing_macing.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,66 @@ impl Session {
5656
Ok(signature)
5757
}
5858

59+
/// Starts new multi-part signing operation
60+
pub fn sign_initialize(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
61+
let mut mechanism: CK_MECHANISM = mechanism.into();
62+
63+
unsafe {
64+
Rv::from(get_pkcs11!(self.client(), C_SignInit)(
65+
self.handle(),
66+
&mut mechanism as CK_MECHANISM_PTR,
67+
key.handle(),
68+
))
69+
.into_result(Function::SignInit)?;
70+
}
71+
72+
Ok(())
73+
}
74+
75+
/// Continues an ongoing multi-part signing operation
76+
pub fn sign_update(&self, data: &[u8]) -> Result<()> {
77+
unsafe {
78+
Rv::from(get_pkcs11!(self.client(), C_SignUpdate)(
79+
self.handle(),
80+
data.as_ptr() as *mut u8,
81+
data.len().try_into()?,
82+
))
83+
.into_result(Function::SignUpdate)?;
84+
}
85+
86+
Ok(())
87+
}
88+
89+
/// Finalizes ongoing multi-part signing operation
90+
pub fn sign_finalize(&self) -> Result<Vec<u8>> {
91+
let mut signature_len = 0;
92+
93+
// Get the output buffer length
94+
unsafe {
95+
Rv::from(get_pkcs11!(self.client(), C_SignFinal)(
96+
self.handle(),
97+
std::ptr::null_mut(),
98+
&mut signature_len,
99+
))
100+
.into_result(Function::SignFinal)?;
101+
}
102+
103+
let mut signature = vec![0; signature_len.try_into()?];
104+
105+
unsafe {
106+
Rv::from(get_pkcs11!(self.client(), C_SignFinal)(
107+
self.handle(),
108+
signature.as_mut_ptr(),
109+
&mut signature_len,
110+
))
111+
.into_result(Function::SignFinal)?;
112+
}
113+
114+
signature.resize(signature_len.try_into()?, 0);
115+
116+
Ok(signature)
117+
}
118+
59119
/// Verify data in single-part
60120
pub fn verify(
61121
&self,
@@ -86,4 +146,48 @@ impl Session {
86146
.into_result(Function::Verify)
87147
}
88148
}
149+
150+
/// Starts new multi-part verifying operation
151+
pub fn verify_initialize(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
152+
let mut mechanism: CK_MECHANISM = mechanism.into();
153+
154+
unsafe {
155+
Rv::from(get_pkcs11!(self.client(), C_VerifyInit)(
156+
self.handle(),
157+
&mut mechanism as CK_MECHANISM_PTR,
158+
key.handle(),
159+
))
160+
.into_result(Function::VerifyInit)?;
161+
}
162+
163+
Ok(())
164+
}
165+
166+
/// Continues an ongoing multi-part verifying operation
167+
pub fn verify_update(&self, data: &[u8]) -> Result<()> {
168+
unsafe {
169+
Rv::from(get_pkcs11!(self.client(), C_VerifyUpdate)(
170+
self.handle(),
171+
data.as_ptr() as *mut u8,
172+
data.len().try_into()?,
173+
))
174+
.into_result(Function::VerifyUpdate)?;
175+
}
176+
177+
Ok(())
178+
}
179+
180+
/// Finalizes ongoing multi-part verifying operation
181+
pub fn verify_finalize(&self, signature: &[u8]) -> Result<()> {
182+
unsafe {
183+
Rv::from(get_pkcs11!(self.client(), C_VerifyFinal)(
184+
self.handle(),
185+
signature.as_ptr() as *mut u8,
186+
signature.len().try_into()?,
187+
))
188+
.into_result(Function::VerifyFinal)?;
189+
}
190+
191+
Ok(())
192+
}
89193
}

0 commit comments

Comments
 (0)