|
| 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 crate::subtle; |
| 16 | +use prost::Message; |
| 17 | +use tink::{ |
| 18 | + utils::{hash_name, wrap_err}, |
| 19 | + TinkError, |
| 20 | +}; |
| 21 | + |
| 22 | +/// Maximal version of HKDF PRF keys. |
| 23 | +pub const HKDF_PRF_KEY_VERSION: u32 = 0; |
| 24 | +/// Type URL of HKDF PRF keys that Tink supports. |
| 25 | +pub const HKDF_PRF_TYPE_URL: &str = "type.googleapis.com/google.crypto.tink.HkdfPrfKey"; |
| 26 | + |
| 27 | +/// Generates new HKDF PRF keys and produces new instances of HKDF. |
| 28 | +pub(crate) struct HkdfPrfKeyManager; |
| 29 | + |
| 30 | +impl HkdfPrfKeyManager { |
| 31 | + pub fn new() -> Self { |
| 32 | + Self |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl Default for HkdfPrfKeyManager { |
| 37 | + fn default() -> Self { |
| 38 | + Self::new() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl tink::registry::KeyManager for HkdfPrfKeyManager { |
| 43 | + /// Construct an HKDF instance for the given serialized [`HkdfPrfKey`](tink::proto::HkdfPrfKey). |
| 44 | + fn primitive(&self, serialized_key: &[u8]) -> Result<tink::Primitive, TinkError> { |
| 45 | + if serialized_key.is_empty() { |
| 46 | + return Err("HkdfPrfKeyManager: invalid key".into()); |
| 47 | + } |
| 48 | + let key = tink::proto::HkdfPrfKey::decode(serialized_key) |
| 49 | + .map_err(|_| TinkError::new("HkdfPrfKeyManager: invalid key"))?; |
| 50 | + validate_key(&key)?; |
| 51 | + let params = key |
| 52 | + .params |
| 53 | + .ok_or_else(|| TinkError::new("no key parameters"))?; |
| 54 | + let hash = hash_name(params.hash); |
| 55 | + |
| 56 | + match subtle::HkdfPrf::new(hash, &key.key_value, ¶ms.salt) { |
| 57 | + Ok(p) => Ok(tink::Primitive::Prf(std::sync::Arc::new(p))), |
| 58 | + Err(e) => Err(wrap_err("HkdfPrfManager: cannot create new primitive", e)), |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Generate a new [`HkdfPrfKey`](tink::proto::HkdfPrfKey) according to specification in the |
| 63 | + /// given [`HkdfPrfKeyFormat`](tink::proto::HkdfPrfKeyFormat). |
| 64 | + fn new_key(&self, serialized_key_format: &[u8]) -> Result<Vec<u8>, TinkError> { |
| 65 | + if serialized_key_format.is_empty() { |
| 66 | + return Err("HkdfPrfKeyManager: invalid key format".into()); |
| 67 | + } |
| 68 | + |
| 69 | + let key_format = tink::proto::HkdfPrfKeyFormat::decode(serialized_key_format) |
| 70 | + .map_err(|_| TinkError::new("HkdfPrfKeyManager: invalid key format"))?; |
| 71 | + validate_key_format(&key_format) |
| 72 | + .map_err(|e| wrap_err("HkdfPrfKeyManager: invalid key format", e))?; |
| 73 | + |
| 74 | + let key_value = tink::subtle::random::get_random_bytes(key_format.key_size as usize); |
| 75 | + let mut sk = Vec::new(); |
| 76 | + |
| 77 | + tink::proto::HkdfPrfKey { |
| 78 | + version: HKDF_PRF_KEY_VERSION, |
| 79 | + params: key_format.params, |
| 80 | + key_value, |
| 81 | + } |
| 82 | + .encode(&mut sk) |
| 83 | + .map_err(|e| wrap_err("Failed to encode new key", e))?; |
| 84 | + Ok(sk) |
| 85 | + } |
| 86 | + |
| 87 | + fn does_support(&self, type_url: &str) -> bool { |
| 88 | + type_url == HKDF_PRF_TYPE_URL |
| 89 | + } |
| 90 | + |
| 91 | + fn type_url(&self) -> String { |
| 92 | + HKDF_PRF_TYPE_URL.to_string() |
| 93 | + } |
| 94 | + |
| 95 | + /// Generate a new [`KeyData`](tink::proto::KeyData) according to specification in the given |
| 96 | + /// serialized [`HkdfPrfKeyFormat`](tink::proto::HkdfPrfKeyFormat). This should be used solely |
| 97 | + /// by the key management API. |
| 98 | + fn new_key_data( |
| 99 | + &self, |
| 100 | + serialized_key_format: &[u8], |
| 101 | + ) -> Result<tink::proto::KeyData, TinkError> { |
| 102 | + let serialized_key = self.new_key(serialized_key_format)?; |
| 103 | + |
| 104 | + Ok(tink::proto::KeyData { |
| 105 | + type_url: HKDF_PRF_TYPE_URL.to_string(), |
| 106 | + value: serialized_key, |
| 107 | + key_material_type: tink::proto::key_data::KeyMaterialType::Symmetric as i32, |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/// Validate the given [`HkdfPrfKey`](tink::proto::HkdfPrfKey). It only validates the version of the |
| 113 | +/// key because other parameters will be validated in primitive construction. |
| 114 | +fn validate_key(key: &tink::proto::HkdfPrfKey) -> Result<(), TinkError> { |
| 115 | + tink::keyset::validate_key_version(key.version, HKDF_PRF_KEY_VERSION) |
| 116 | + .map_err(|e| wrap_err("HkdfPrfKeyManager: invalid version", e))?; |
| 117 | + let key_size = key.key_value.len(); |
| 118 | + let hash = hash_name(key.params.as_ref().unwrap().hash); |
| 119 | + subtle::validate_hkdf_prf_params(hash, key_size, &key.params.as_ref().unwrap().salt) |
| 120 | +} |
| 121 | + |
| 122 | +/// Validate the given [`HkdfPrfKeyFormat`](tink::proto::HkdfPrfKeyFormat). |
| 123 | +fn validate_key_format(format: &tink::proto::HkdfPrfKeyFormat) -> Result<(), TinkError> { |
| 124 | + if format.params.is_none() { |
| 125 | + return Err("null HKDF params".into()); |
| 126 | + } |
| 127 | + let hash = hash_name(format.params.as_ref().unwrap().hash); |
| 128 | + subtle::validate_hkdf_prf_params( |
| 129 | + hash, |
| 130 | + format.key_size as usize, |
| 131 | + &format.params.as_ref().unwrap().salt, |
| 132 | + ) |
| 133 | +} |
0 commit comments