|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | +// |
| 13 | +//////////////////////////////////////////////////////////////////////////////// |
| 14 | + |
| 15 | +use prost::Message; |
| 16 | +use tink::{utils::wrap_err, TinkError}; |
| 17 | + |
| 18 | +/// Maximal version of AES-CMAC keys. |
| 19 | +pub const CMAC_KEY_VERSION: u32 = 0; |
| 20 | +/// Type URL of AES-CMAC keys that Tink supports. |
| 21 | +pub const CMAC_TYPE_URL: &str = "type.googleapis.com/google.crypto.tink.AesCmacKey"; |
| 22 | + |
| 23 | +/// Generates new AES-CMAC keys and produces new instances of AES-CMAC. |
| 24 | +pub(crate) struct AesCmacKeyManager; |
| 25 | + |
| 26 | +impl AesCmacKeyManager { |
| 27 | + pub fn new() -> Self { |
| 28 | + Self |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Default for AesCmacKeyManager { |
| 33 | + fn default() -> Self { |
| 34 | + Self::new() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl tink::registry::KeyManager for AesCmacKeyManager { |
| 39 | + /// Create an [`AesCmac`](crate::subtle::AesCmac) instance for the given serialized |
| 40 | + /// [`AesCmacKey`](tink:;proto::AesCmacKey) proto. |
| 41 | + fn primitive(&self, serialized_key: &[u8]) -> Result<tink::Primitive, TinkError> { |
| 42 | + if serialized_key.is_empty() { |
| 43 | + return Err("AesCmacKeyManager: invalid key".into()); |
| 44 | + } |
| 45 | + |
| 46 | + let key = tink::proto::AesCmacKey::decode(serialized_key) |
| 47 | + .map_err(|e| wrap_err("decode failed", e))?; |
| 48 | + validate_key(&key)?; |
| 49 | + match crate::subtle::AesCmac::new(&key.key_value, key.params.unwrap().tag_size as usize) { |
| 50 | + Ok(p) => Ok(tink::Primitive::Mac(std::sync::Arc::new(p))), |
| 51 | + Err(e) => Err(wrap_err( |
| 52 | + "AesCmacKeyManager: cannot create new primitive", |
| 53 | + e, |
| 54 | + )), |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// Generate a new serialized [`AesCmacKey`](tink::proto::AesCmacKey) according to |
| 59 | + /// specification in the given [`AesCmacKeyFormat`](tink::proto::AesCmacKeyFormat). |
| 60 | + fn new_key(&self, serialized_key_format: &[u8]) -> Result<Vec<u8>, TinkError> { |
| 61 | + if serialized_key_format.is_empty() { |
| 62 | + return Err("AesCmacKeyManager: invalid key format".into()); |
| 63 | + } |
| 64 | + let key_format = tink::proto::AesCmacKeyFormat::decode(serialized_key_format) |
| 65 | + .map_err(|_| TinkError::new("AesCmacKeyManager: invalid key format"))?; |
| 66 | + validate_key_format(&key_format) |
| 67 | + .map_err(|e| wrap_err("AesCmacKeyManager: invalid key format", e))?; |
| 68 | + let key_value = tink::subtle::random::get_random_bytes(key_format.key_size as usize); |
| 69 | + let mut sk = Vec::new(); |
| 70 | + tink::proto::AesCmacKey { |
| 71 | + version: CMAC_KEY_VERSION, |
| 72 | + params: key_format.params, |
| 73 | + key_value, |
| 74 | + } |
| 75 | + .encode(&mut sk) |
| 76 | + .map_err(|e| wrap_err("Failed to encode new key", e))?; |
| 77 | + Ok(sk) |
| 78 | + } |
| 79 | + |
| 80 | + fn does_support(&self, type_url: &str) -> bool { |
| 81 | + type_url == CMAC_TYPE_URL |
| 82 | + } |
| 83 | + |
| 84 | + fn type_url(&self) -> String { |
| 85 | + CMAC_TYPE_URL.to_string() |
| 86 | + } |
| 87 | + |
| 88 | + /// Create a new [`KeyData`](tink::proto::KeyData) according to the specification in the given |
| 89 | + /// serialized [`AesCmacKeyFormat`](tink::proto::AesCmacKeyFormat). This should be used |
| 90 | + /// solely by the key management API. |
| 91 | + fn new_key_data( |
| 92 | + &self, |
| 93 | + serialized_key_format: &[u8], |
| 94 | + ) -> Result<tink::proto::KeyData, TinkError> { |
| 95 | + let serialized_key = self.new_key(serialized_key_format)?; |
| 96 | + |
| 97 | + Ok(tink::proto::KeyData { |
| 98 | + type_url: CMAC_TYPE_URL.to_string(), |
| 99 | + value: serialized_key, |
| 100 | + key_material_type: tink::proto::key_data::KeyMaterialType::Symmetric as i32, |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// Validate the given [`AesCmacKey`](tink::proto::AesCmacKey). It only validates the version of the |
| 106 | +/// key because other parameters will be validated in primitive construction. |
| 107 | +fn validate_key(key: &tink::proto::AesCmacKey) -> Result<(), TinkError> { |
| 108 | + tink::keyset::validate_key_version(key.version, CMAC_KEY_VERSION) |
| 109 | + .map_err(|e| wrap_err("AesCmacKeyManager: invalid version", e))?; |
| 110 | + let key_size = key.key_value.len(); |
| 111 | + match &key.params { |
| 112 | + None => Err("missing AES-CMAC params".into()), |
| 113 | + Some(params) => crate::subtle::validate_cmac_params(key_size, params.tag_size as usize), |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/// Validate the given [`AesCmacKeyFormat`](tink::proto::AesCmacKeyFormat). |
| 118 | +fn validate_key_format(format: &tink::proto::AesCmacKeyFormat) -> Result<(), TinkError> { |
| 119 | + match &format.params { |
| 120 | + None => Err("missing AES-CMAC params".into()), |
| 121 | + Some(params) => { |
| 122 | + crate::subtle::validate_cmac_params(format.key_size as usize, params.tag_size as usize) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments