forked from parallaxsecond/rust-cryptoki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binding for single-part digest function.
Signed-off-by: Helmut Eller <[email protected]>
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2021 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
//! Digesting functions | ||
use crate::error::{Result, Rv}; | ||
use crate::mechanism::Mechanism; | ||
use crate::session::Session; | ||
use cryptoki_sys::*; | ||
use std::convert::TryInto; | ||
|
||
impl Session { | ||
/// Single-part digesting operation | ||
pub fn digest(&self, m: &Mechanism, data: &[u8]) -> Result<Vec<u8>> { | ||
let mut mechanism: CK_MECHANISM = m.into(); | ||
let mut digest_len = 0; | ||
|
||
unsafe { | ||
Rv::from(get_pkcs11!(self.client(), C_DigestInit)( | ||
self.handle(), | ||
&mut mechanism as CK_MECHANISM_PTR, | ||
)) | ||
.into_result()?; | ||
} | ||
|
||
// Get the output buffer length | ||
unsafe { | ||
Rv::from(get_pkcs11!(self.client(), C_Digest)( | ||
self.handle(), | ||
data.as_ptr() as *mut u8, | ||
data.len().try_into()?, | ||
std::ptr::null_mut(), | ||
&mut digest_len, | ||
)) | ||
.into_result()?; | ||
} | ||
|
||
let mut digest = vec![0; digest_len.try_into()?]; | ||
|
||
unsafe { | ||
Rv::from(get_pkcs11!(self.client(), C_Digest)( | ||
self.handle(), | ||
data.as_ptr() as *mut u8, | ||
data.len().try_into()?, | ||
digest.as_mut_ptr(), | ||
&mut digest_len, | ||
)) | ||
.into_result()?; | ||
} | ||
|
||
digest.resize(digest_len.try_into()?, 0); | ||
|
||
Ok(digest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters