Skip to content

Commit 58eec0a

Browse files
Added multi-part decryption/encryption bindings
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent 334bc5e commit 58eec0a

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

cryptoki/src/session/decryption.rs

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

6161
Ok(data)
6262
}
63+
64+
/// Starts new multi-part decryption operation
65+
pub fn decrypt_initialize(&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+
pub fn decrypt_update(&self, encrypted_data: &[u8]) -> Result<Vec<u8>> {
82+
let mut data_len = 0;
83+
84+
// Get the output buffer length
85+
unsafe {
86+
Rv::from(get_pkcs11!(self.client(), C_DecryptUpdate)(
87+
self.handle(),
88+
encrypted_data.as_ptr() as *mut u8,
89+
encrypted_data.len().try_into()?,
90+
std::ptr::null_mut(),
91+
&mut data_len,
92+
))
93+
.into_result(Function::DecryptUpdate)?;
94+
}
95+
96+
let mut data = vec![0; data_len.try_into()?];
97+
98+
unsafe {
99+
Rv::from(get_pkcs11!(self.client(), C_DecryptUpdate)(
100+
self.handle(),
101+
encrypted_data.as_ptr() as *mut u8,
102+
encrypted_data.len().try_into()?,
103+
data.as_mut_ptr(),
104+
&mut data_len,
105+
))
106+
.into_result(Function::DecryptUpdate)?;
107+
}
108+
109+
Ok(data)
110+
}
111+
112+
/// Finalizes ongoing multi-part decryption operation
113+
pub fn decrypt_finalize(&self) -> Result<Vec<u8>> {
114+
let mut data_len = 0;
115+
116+
// Get the output buffer length
117+
unsafe {
118+
Rv::from(get_pkcs11!(self.client(), C_DecryptFinal)(
119+
self.handle(),
120+
std::ptr::null_mut(),
121+
&mut data_len,
122+
))
123+
.into_result(Function::DecryptFinal)?;
124+
}
125+
126+
let mut data = vec![0; data_len.try_into()?];
127+
128+
unsafe {
129+
Rv::from(get_pkcs11!(self.client(), C_DecryptFinal)(
130+
self.handle(),
131+
data.as_mut_ptr(),
132+
&mut data_len,
133+
))
134+
.into_result(Function::DecryptFinal)?;
135+
}
136+
137+
data.resize(data_len.try_into()?, 0);
138+
139+
Ok(data)
140+
}
63141
}

cryptoki/src/session/encryption.rs

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

6060
Ok(encrypted_data)
6161
}
62+
63+
/// Starts new multi-part encryption operation
64+
pub fn encrypt_initialize(&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+
pub fn encrypt_update(&self, data: &[u8]) -> Result<Vec<u8>> {
81+
let mut encrypted_data_len = 0;
82+
83+
// Get the output buffer length
84+
unsafe {
85+
Rv::from(get_pkcs11!(self.client(), C_EncryptUpdate)(
86+
self.handle(),
87+
data.as_ptr() as *mut u8,
88+
data.len().try_into()?,
89+
std::ptr::null_mut(),
90+
&mut encrypted_data_len,
91+
))
92+
.into_result(Function::EncryptUpdate)?;
93+
}
94+
95+
let mut encrypted_data = vec![0; encrypted_data_len.try_into()?];
96+
97+
unsafe {
98+
Rv::from(get_pkcs11!(self.client(), C_EncryptUpdate)(
99+
self.handle(),
100+
data.as_ptr() as *mut u8,
101+
data.len().try_into()?,
102+
encrypted_data.as_mut_ptr(),
103+
&mut encrypted_data_len,
104+
))
105+
.into_result(Function::EncryptUpdate)?;
106+
}
107+
108+
Ok(encrypted_data)
109+
}
110+
111+
/// Finalizes ongoing multi-part encryption operation
112+
pub fn encrypt_finalize(&self) -> Result<Vec<u8>> {
113+
let mut encrypted_data_len = 0;
114+
115+
// Get the output buffer length
116+
unsafe {
117+
Rv::from(get_pkcs11!(self.client(), C_EncryptFinal)(
118+
self.handle(),
119+
std::ptr::null_mut(),
120+
&mut encrypted_data_len,
121+
))
122+
.into_result(Function::EncryptFinal)?;
123+
}
124+
125+
let mut encrypted_data = vec![0; encrypted_data_len.try_into()?];
126+
127+
unsafe {
128+
Rv::from(get_pkcs11!(self.client(), C_EncryptFinal)(
129+
self.handle(),
130+
encrypted_data.as_mut_ptr(),
131+
&mut encrypted_data_len,
132+
))
133+
.into_result(Function::EncryptFinal)?;
134+
}
135+
136+
encrypted_data.resize(encrypted_data_len.try_into()?, 0);
137+
138+
Ok(encrypted_data)
139+
}
62140
}

0 commit comments

Comments
 (0)