| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![allow(non_camel_case_types)] |
| 8 |
|
| 9 |
use crate::err::IntoResult; |
| 10 |
use crate::hash; |
| 11 |
use crate::hash::HashAlgorithm; |
| 12 |
use crate::p11; |
| 13 |
use crate::p11::PK11Origin; |
| 14 |
use crate::p11::PK11_CreateContextBySymKey; |
| 15 |
use crate::p11::PK11_DigestFinal; |
| 16 |
use crate::p11::PK11_DigestOp; |
| 17 |
use crate::p11::PK11_ImportSymKey; |
| 18 |
use crate::p11::Slot; |
| 19 |
use crate::Error; |
| 20 |
use crate::SECItemBorrowed; |
| 21 |
use pkcs11_bindings::CKA_SIGN; |
| 22 |
use std::convert::TryFrom; |
| 23 |
use std::ptr; |
| 24 |
|
| 25 |
// |
| 26 |
// Constants |
| 27 |
// |
| 28 |
|
| 29 |
pub enum HmacAlgorithm { |
| 30 |
HMAC_SHA2_256, |
| 31 |
HMAC_SHA2_384, |
| 32 |
HMAC_SHA2_512, |
| 33 |
} |
| 34 |
|
| 35 |
fn hmac_alg_to_ckm(alg: &HmacAlgorithm) -> p11::CK_MECHANISM_TYPE { |
| 36 |
match alg { |
| 37 |
HmacAlgorithm::HMAC_SHA2_256 => p11::CKM_SHA256_HMAC.into(), |
| 38 |
HmacAlgorithm::HMAC_SHA2_384 => p11::CKM_SHA384_HMAC.into(), |
| 39 |
HmacAlgorithm::HMAC_SHA2_512 => p11::CKM_SHA512_HMAC.into(), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
pub fn hmac_alg_to_hash_alg(alg: &HmacAlgorithm) -> Result<HashAlgorithm, Error> { |
| 44 |
match alg { |
| 45 |
HmacAlgorithm::HMAC_SHA2_256 => Ok(HashAlgorithm::SHA2_256), |
| 46 |
HmacAlgorithm::HMAC_SHA2_384 => Ok(HashAlgorithm::SHA2_384), |
| 47 |
HmacAlgorithm::HMAC_SHA2_512 => Ok(HashAlgorithm::SHA2_512), |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
pub fn hmac_alg_to_hmac_len(alg: &HmacAlgorithm) -> Result<usize, Error> { |
| 52 |
let hash_alg = hmac_alg_to_hash_alg(&alg)?; |
| 53 |
hash::hash_alg_to_hash_len(&hash_alg) |
| 54 |
} |
| 55 |
|
| 56 |
pub fn hmac(alg: &HmacAlgorithm, key: &[u8], data: &[u8]) -> Result<Vec<u8>, Error> { |
| 57 |
crate::init(); |
| 58 |
|
| 59 |
let data_len = match u32::try_from(data.len()) { |
| 60 |
Ok(data_len) => data_len, |
| 61 |
_ => return Err(Error::InternalError), |
| 62 |
}; |
| 63 |
|
| 64 |
let slot = Slot::internal()?; |
| 65 |
let sym_key = unsafe { |
| 66 |
PK11_ImportSymKey( |
| 67 |
*slot, |
| 68 |
hmac_alg_to_ckm(&alg), |
| 69 |
PK11Origin::PK11_OriginUnwrap, |
| 70 |
CKA_SIGN, |
| 71 |
SECItemBorrowed::wrap(key).as_mut(), |
| 72 |
ptr::null_mut(), |
| 73 |
) |
| 74 |
.into_result()? |
| 75 |
}; |
| 76 |
let param = SECItemBorrowed::make_empty(); |
| 77 |
let context = unsafe { |
| 78 |
PK11_CreateContextBySymKey(hmac_alg_to_ckm(&alg), CKA_SIGN, *sym_key, param.as_ref()) |
| 79 |
.into_result()? |
| 80 |
}; |
| 81 |
unsafe { PK11_DigestOp(*context, data.as_ptr(), data_len).into_result()? }; |
| 82 |
let expected_len = hmac_alg_to_hmac_len(alg)?; |
| 83 |
let mut digest = vec![0u8; expected_len]; |
| 84 |
let mut digest_len = 0u32; |
| 85 |
unsafe { |
| 86 |
PK11_DigestFinal( |
| 87 |
*context, |
| 88 |
digest.as_mut_ptr(), |
| 89 |
&mut digest_len, |
| 90 |
digest.len() as u32, |
| 91 |
) |
| 92 |
.into_result()? |
| 93 |
} |
| 94 |
assert_eq!(digest_len as usize, expected_len); |
| 95 |
Ok(digest) |
| 96 |
} |