1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //! Helper functions that includes data transformation for AIDL types.
18
19 use android_hardware_security_see_hwcrypto::aidl::android::hardware::security::see::hwcrypto::types::{
20 AesCipherMode::AesCipherMode, AesKey::AesKey, CipherModeParameters::CipherModeParameters,
21 ExplicitKeyMaterial::ExplicitKeyMaterial, HmacKey::HmacKey, KeyType::KeyType, KeyUse::KeyUse,
22 SymmetricCryptoParameters::SymmetricCryptoParameters, SymmetricOperation::SymmetricOperation,
23 };
24 use hwcryptohal_common::{err::HwCryptoError, hwcrypto_err};
25 use kmr_common::crypto::{
26 self, aes, hmac, KeyMaterial, OpaqueOr, SymmetricOperation as KmSymmetricOperation,
27 };
28 use kmr_wire::keymint;
29
30 use crate::opaque_key::OpaqueKey;
31
aidl_hmac_explicit_key_to_rust_key_material( hmac_key_material: &[u8], ) -> Result<KeyMaterial, HwCryptoError>32 fn aidl_hmac_explicit_key_to_rust_key_material(
33 hmac_key_material: &[u8],
34 ) -> Result<KeyMaterial, HwCryptoError> {
35 let mut ekm = Vec::new();
36 ekm.try_reserve(hmac_key_material.len())?;
37 ekm.extend_from_slice(hmac_key_material);
38 Ok(KeyMaterial::Hmac(OpaqueOr::Explicit(hmac::Key(ekm))))
39 }
40
aidl_explicit_key_to_rust_key_material( key_material: &ExplicitKeyMaterial, ) -> Result<KeyMaterial, HwCryptoError>41 pub(crate) fn aidl_explicit_key_to_rust_key_material(
42 key_material: &ExplicitKeyMaterial,
43 ) -> Result<KeyMaterial, HwCryptoError> {
44 match key_material {
45 ExplicitKeyMaterial::Aes(AesKey::Aes128(km)) => {
46 Ok(KeyMaterial::Aes(OpaqueOr::Explicit(aes::Key::Aes128(*km))))
47 }
48 ExplicitKeyMaterial::Aes(AesKey::Aes256(km)) => {
49 Ok(KeyMaterial::Aes(OpaqueOr::Explicit(aes::Key::Aes256(*km))))
50 }
51 ExplicitKeyMaterial::Hmac(HmacKey::Sha256(km)) => {
52 aidl_hmac_explicit_key_to_rust_key_material(km)
53 }
54 ExplicitKeyMaterial::Hmac(HmacKey::Sha512(km)) => {
55 aidl_hmac_explicit_key_to_rust_key_material(km)
56 }
57 }
58 }
59
symmetric_encryption_block_based( parameters: &SymmetricCryptoParameters, ) -> Result<bool, HwCryptoError>60 pub(crate) fn symmetric_encryption_block_based(
61 parameters: &SymmetricCryptoParameters,
62 ) -> Result<bool, HwCryptoError> {
63 match parameters {
64 SymmetricCryptoParameters::Aes(aes_params) => match aes_params {
65 AesCipherMode::Ctr(_) => Ok(false),
66 _ => Ok(true),
67 },
68 }
69 }
70
aidl_to_rust_digest(key_type: &KeyType) -> Result<keymint::Digest, HwCryptoError>71 pub(crate) fn aidl_to_rust_digest(key_type: &KeyType) -> Result<keymint::Digest, HwCryptoError> {
72 match *key_type {
73 KeyType::HMAC_SHA256 => Ok(keymint::Digest::Sha256),
74 KeyType::HMAC_SHA512 => Ok(keymint::Digest::Sha512),
75 _ => Err(hwcrypto_err!(UNSUPPORTED, "unsupported key type to get digest: {:?}", key_type)),
76 }
77 }
78
aidl_to_rust_aes_cipher_params( params: &SymmetricCryptoParameters, opaque_key: &OpaqueKey, ) -> Result<crypto::aes::CipherMode, HwCryptoError>79 pub(crate) fn aidl_to_rust_aes_cipher_params(
80 params: &SymmetricCryptoParameters,
81 opaque_key: &OpaqueKey,
82 ) -> Result<crypto::aes::CipherMode, HwCryptoError> {
83 let SymmetricCryptoParameters::Aes(aes_params) = params;
84 match aes_params {
85 AesCipherMode::Cbc(CipherModeParameters { nonce }) => {
86 // TODO: change clone() into something like a try_clone()
87 let nonce = nonce.clone();
88 let nonce_len = nonce.len();
89 match opaque_key.get_key_type() {
90 KeyType::AES_128_CBC_NO_PADDING | KeyType::AES_256_CBC_NO_PADDING => {
91 Ok(crypto::aes::CipherMode::CbcNoPadding {
92 nonce: nonce.try_into().map_err(|_| {
93 hwcrypto_err!(BAD_PARAMETER, "bad nonce length: {}", nonce_len)
94 })?,
95 })
96 }
97 KeyType::AES_128_CBC_PKCS7_PADDING | KeyType::AES_256_CBC_PKCS7_PADDING => {
98 Ok(crypto::aes::CipherMode::CbcPkcs7Padding {
99 nonce: nonce.try_into().map_err(|_| {
100 hwcrypto_err!(BAD_PARAMETER, "bad nonce length: {}", nonce_len)
101 })?,
102 })
103 }
104 _ => Err(hwcrypto_err!(
105 BAD_PARAMETER,
106 "unsupporte key type for CBC: {:?}",
107 opaque_key.get_key_type()
108 )),
109 }
110 }
111 AesCipherMode::Ctr(CipherModeParameters { nonce }) => {
112 let nonce_len = nonce.len();
113 // TODO: change clone() into something like a try_clone()
114 Ok(crypto::aes::CipherMode::Ctr {
115 nonce: nonce
116 .clone()
117 .try_into()
118 .map_err(|_| hwcrypto_err!(BAD_PARAMETER, "bad nonce length: {}", nonce_len))?,
119 })
120 }
121 }
122 }
123
aidl_to_rust_symmetric_direction( dir: SymmetricOperation, ) -> Result<KmSymmetricOperation, HwCryptoError>124 pub(crate) fn aidl_to_rust_symmetric_direction(
125 dir: SymmetricOperation,
126 ) -> Result<KmSymmetricOperation, HwCryptoError> {
127 match dir {
128 SymmetricOperation::ENCRYPT => Ok(KmSymmetricOperation::Encrypt),
129 SymmetricOperation::DECRYPT => Ok(KmSymmetricOperation::Decrypt),
130 _ => Err(hwcrypto_err!(UNSUPPORTED, "unsupported symmetric operation: {:?}", dir)),
131 }
132 }
133
direction_to_key_usage( operation: &SymmetricOperation, ) -> Result<KeyUse, HwCryptoError>134 pub(crate) fn direction_to_key_usage(
135 operation: &SymmetricOperation,
136 ) -> Result<KeyUse, HwCryptoError> {
137 match *operation {
138 SymmetricOperation::ENCRYPT => Ok(KeyUse::ENCRYPT),
139 SymmetricOperation::DECRYPT => Ok(KeyUse::DECRYPT),
140 _ => Err(hwcrypto_err!(BAD_PARAMETER, "invalid operation type: {:?}", operation)),
141 }
142 }
143