Skip to content

Commit 4d6b18e

Browse files
authored
Merge pull request #252 from jacobprudhomme/multipart-operations
Add bindings for multi-part operations
2 parents 58114a4 + 2639ed4 commit 4d6b18e

File tree

5 files changed

+786
-0
lines changed

5 files changed

+786
-0
lines changed

cryptoki/src/session/decryption.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,84 @@ impl Session {
6060

6161
Ok(data)
6262
}
63+
64+
/// Starts new multi-part decryption operation
65+
pub fn decrypt_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
66+
let mut mechanism: CK_MECHANISM = mechanism.into();
67+
68+
unsafe {
69+
Rv::from(get_pkcs11!(self.client(), C_DecryptInit)(
70+
self.handle(),
71+
&mut mechanism as CK_MECHANISM_PTR,
72+
key.handle(),
73+
))
74+
.into_result(Function::DecryptInit)?;
75+
}
76+
77+
Ok(())
78+
}
79+
80+
/// Continues an ongoing multi-part decryption operation,
81+
/// taking in the next part of the encrypted data and returning its decryption
82+
pub fn decrypt_update(&self, encrypted_data: &[u8]) -> Result<Vec<u8>> {
83+
let mut data_len = 0;
84+
85+
// Get the output buffer length
86+
unsafe {
87+
Rv::from(get_pkcs11!(self.client(), C_DecryptUpdate)(
88+
self.handle(),
89+
encrypted_data.as_ptr() as *mut u8,
90+
encrypted_data.len().try_into()?,
91+
std::ptr::null_mut(),
92+
&mut data_len,
93+
))
94+
.into_result(Function::DecryptUpdate)?;
95+
}
96+
97+
let mut data = vec![0; data_len.try_into()?];
98+
99+
unsafe {
100+
Rv::from(get_pkcs11!(self.client(), C_DecryptUpdate)(
101+
self.handle(),
102+
encrypted_data.as_ptr() as *mut u8,
103+
encrypted_data.len().try_into()?,
104+
data.as_mut_ptr(),
105+
&mut data_len,
106+
))
107+
.into_result(Function::DecryptUpdate)?;
108+
}
109+
110+
Ok(data)
111+
}
112+
113+
/// Finalizes ongoing multi-part decryption operation,
114+
/// returning any remaining bytes in the decrypted data
115+
pub fn decrypt_final(&self) -> Result<Vec<u8>> {
116+
let mut data_len = 0;
117+
118+
// Get the output buffer length
119+
unsafe {
120+
Rv::from(get_pkcs11!(self.client(), C_DecryptFinal)(
121+
self.handle(),
122+
std::ptr::null_mut(),
123+
&mut data_len,
124+
))
125+
.into_result(Function::DecryptFinal)?;
126+
}
127+
128+
let mut data = vec![0; data_len.try_into()?];
129+
130+
unsafe {
131+
Rv::from(get_pkcs11!(self.client(), C_DecryptFinal)(
132+
self.handle(),
133+
data.as_mut_ptr(),
134+
&mut data_len,
135+
))
136+
.into_result(Function::DecryptFinal)?;
137+
}
138+
139+
data.resize(data_len.try_into()?, 0);
140+
141+
Ok(data)
142+
}
63143
}

cryptoki/src/session/digesting.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::context::Function;
66
use crate::error::{Result, Rv};
77
use crate::mechanism::Mechanism;
8+
use crate::object::ObjectHandle;
89
use crate::session::Session;
910
use cryptoki_sys::*;
1011
use std::convert::TryInto;
@@ -52,4 +53,79 @@ impl Session {
5253

5354
Ok(digest)
5455
}
56+
57+
/// Starts new multi-part digesting operation
58+
pub fn digest_init(&self, m: &Mechanism) -> Result<()> {
59+
let mut mechanism: CK_MECHANISM = m.into();
60+
61+
unsafe {
62+
Rv::from(get_pkcs11!(self.client(), C_DigestInit)(
63+
self.handle(),
64+
&mut mechanism as CK_MECHANISM_PTR,
65+
))
66+
.into_result(Function::DigestInit)?;
67+
}
68+
69+
Ok(())
70+
}
71+
72+
/// Continues an ongoing multi-part digesting operation,
73+
/// taking in the next part of the data to digest
74+
pub fn digest_update(&self, data: &[u8]) -> Result<()> {
75+
unsafe {
76+
Rv::from(get_pkcs11!(self.client(), C_DigestUpdate)(
77+
self.handle(),
78+
data.as_ptr() as *mut u8,
79+
data.len().try_into()?,
80+
))
81+
.into_result(Function::DigestUpdate)?;
82+
}
83+
84+
Ok(())
85+
}
86+
87+
/// Continues an ongoing multi-part digesting operation,
88+
/// using the value of a secret key as input
89+
pub fn digest_key(&self, key: ObjectHandle) -> Result<()> {
90+
unsafe {
91+
Rv::from(get_pkcs11!(self.client(), C_DigestKey)(
92+
self.handle(),
93+
key.handle(),
94+
))
95+
.into_result(Function::DigestKey)?;
96+
}
97+
98+
Ok(())
99+
}
100+
101+
/// Finalizes ongoing multi-part digest operation,
102+
/// returning the digest
103+
pub fn digest_final(&self) -> Result<Vec<u8>> {
104+
let mut digest_len = 0;
105+
106+
// Get the output buffer length
107+
unsafe {
108+
Rv::from(get_pkcs11!(self.client(), C_DigestFinal)(
109+
self.handle(),
110+
std::ptr::null_mut(),
111+
&mut digest_len,
112+
))
113+
.into_result(Function::DigestFinal)?;
114+
}
115+
116+
let mut digest = vec![0; digest_len.try_into()?];
117+
118+
unsafe {
119+
Rv::from(get_pkcs11!(self.client(), C_DigestFinal)(
120+
self.handle(),
121+
digest.as_mut_ptr(),
122+
&mut digest_len,
123+
))
124+
.into_result(Function::DigestFinal)?;
125+
}
126+
127+
digest.resize(digest_len.try_into()?, 0);
128+
129+
Ok(digest)
130+
}
55131
}

cryptoki/src/session/encryption.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,84 @@ impl Session {
5959

6060
Ok(encrypted_data)
6161
}
62+
63+
/// Starts new multi-part encryption operation
64+
pub fn encrypt_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
65+
let mut mechanism: CK_MECHANISM = mechanism.into();
66+
67+
unsafe {
68+
Rv::from(get_pkcs11!(self.client(), C_EncryptInit)(
69+
self.handle(),
70+
&mut mechanism as CK_MECHANISM_PTR,
71+
key.handle(),
72+
))
73+
.into_result(Function::EncryptInit)?;
74+
}
75+
76+
Ok(())
77+
}
78+
79+
/// Continues an ongoing multi-part encryption operation,
80+
/// taking in the next part of the data and returning its encryption
81+
pub fn encrypt_update(&self, data: &[u8]) -> Result<Vec<u8>> {
82+
let mut encrypted_data_len = 0;
83+
84+
// Get the output buffer length
85+
unsafe {
86+
Rv::from(get_pkcs11!(self.client(), C_EncryptUpdate)(
87+
self.handle(),
88+
data.as_ptr() as *mut u8,
89+
data.len().try_into()?,
90+
std::ptr::null_mut(),
91+
&mut encrypted_data_len,
92+
))
93+
.into_result(Function::EncryptUpdate)?;
94+
}
95+
96+
let mut encrypted_data = vec![0; encrypted_data_len.try_into()?];
97+
98+
unsafe {
99+
Rv::from(get_pkcs11!(self.client(), C_EncryptUpdate)(
100+
self.handle(),
101+
data.as_ptr() as *mut u8,
102+
data.len().try_into()?,
103+
encrypted_data.as_mut_ptr(),
104+
&mut encrypted_data_len,
105+
))
106+
.into_result(Function::EncryptUpdate)?;
107+
}
108+
109+
Ok(encrypted_data)
110+
}
111+
112+
/// Finalizes ongoing multi-part encryption operation,
113+
/// returning any remaining bytes in the encrypted data
114+
pub fn encrypt_final(&self) -> Result<Vec<u8>> {
115+
let mut encrypted_data_len = 0;
116+
117+
// Get the output buffer length
118+
unsafe {
119+
Rv::from(get_pkcs11!(self.client(), C_EncryptFinal)(
120+
self.handle(),
121+
std::ptr::null_mut(),
122+
&mut encrypted_data_len,
123+
))
124+
.into_result(Function::EncryptFinal)?;
125+
}
126+
127+
let mut encrypted_data = vec![0; encrypted_data_len.try_into()?];
128+
129+
unsafe {
130+
Rv::from(get_pkcs11!(self.client(), C_EncryptFinal)(
131+
self.handle(),
132+
encrypted_data.as_mut_ptr(),
133+
&mut encrypted_data_len,
134+
))
135+
.into_result(Function::EncryptFinal)?;
136+
}
137+
138+
encrypted_data.resize(encrypted_data_len.try_into()?, 0);
139+
140+
Ok(encrypted_data)
141+
}
62142
}

cryptoki/src/session/signing_macing.rs

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

59+
/// Starts new multi-part signing operation
60+
pub fn sign_init(&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+
/// taking in the next part of the data to sign
77+
pub fn sign_update(&self, data: &[u8]) -> Result<()> {
78+
unsafe {
79+
Rv::from(get_pkcs11!(self.client(), C_SignUpdate)(
80+
self.handle(),
81+
data.as_ptr() as *mut u8,
82+
data.len().try_into()?,
83+
))
84+
.into_result(Function::SignUpdate)?;
85+
}
86+
87+
Ok(())
88+
}
89+
90+
/// Finalizes ongoing multi-part signing operation,
91+
/// returning the signature
92+
pub fn sign_final(&self) -> Result<Vec<u8>> {
93+
let mut signature_len = 0;
94+
95+
// Get the output buffer length
96+
unsafe {
97+
Rv::from(get_pkcs11!(self.client(), C_SignFinal)(
98+
self.handle(),
99+
std::ptr::null_mut(),
100+
&mut signature_len,
101+
))
102+
.into_result(Function::SignFinal)?;
103+
}
104+
105+
let mut signature = vec![0; signature_len.try_into()?];
106+
107+
unsafe {
108+
Rv::from(get_pkcs11!(self.client(), C_SignFinal)(
109+
self.handle(),
110+
signature.as_mut_ptr(),
111+
&mut signature_len,
112+
))
113+
.into_result(Function::SignFinal)?;
114+
}
115+
116+
signature.resize(signature_len.try_into()?, 0);
117+
118+
Ok(signature)
119+
}
120+
59121
/// Verify data in single-part
60122
pub fn verify(
61123
&self,
@@ -86,4 +148,50 @@ impl Session {
86148
.into_result(Function::Verify)
87149
}
88150
}
151+
152+
/// Starts new multi-part verifying operation
153+
pub fn verify_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
154+
let mut mechanism: CK_MECHANISM = mechanism.into();
155+
156+
unsafe {
157+
Rv::from(get_pkcs11!(self.client(), C_VerifyInit)(
158+
self.handle(),
159+
&mut mechanism as CK_MECHANISM_PTR,
160+
key.handle(),
161+
))
162+
.into_result(Function::VerifyInit)?;
163+
}
164+
165+
Ok(())
166+
}
167+
168+
/// Continues an ongoing multi-part verifying operation,
169+
/// taking in the next part of the data to verify
170+
pub fn verify_update(&self, data: &[u8]) -> Result<()> {
171+
unsafe {
172+
Rv::from(get_pkcs11!(self.client(), C_VerifyUpdate)(
173+
self.handle(),
174+
data.as_ptr() as *mut u8,
175+
data.len().try_into()?,
176+
))
177+
.into_result(Function::VerifyUpdate)?;
178+
}
179+
180+
Ok(())
181+
}
182+
183+
/// Finalizes ongoing multi-part verifying operation,
184+
/// returning Ok only if the signature verifies
185+
pub fn verify_final(&self, signature: &[u8]) -> Result<()> {
186+
unsafe {
187+
Rv::from(get_pkcs11!(self.client(), C_VerifyFinal)(
188+
self.handle(),
189+
signature.as_ptr() as *mut u8,
190+
signature.len().try_into()?,
191+
))
192+
.into_result(Function::VerifyFinal)?;
193+
}
194+
195+
Ok(())
196+
}
89197
}

0 commit comments

Comments
 (0)