Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Implements safe wrappers around the public API of libopen-dice. |
| 16 | //! ## Example: |
| 17 | //! ``` |
| 18 | //! use diced_open_dice_cbor as dice; |
| 19 | //! |
| 20 | //! let context = dice::dice::OpenDiceCborContext::new() |
| 21 | //! let parent_cdi_attest = [1u8, dice::CDI_SIZE]; |
| 22 | //! let parent_cdi_seal = [2u8, dice::CDI_SIZE]; |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 23 | //! let input_values = dice::InputValues::new( |
| 24 | //! [3u8, dice::HASH_SIZE], // code_hash |
| 25 | //! dice::Config::Descriptor(&"My descriptor".as_bytes().to_vec()), |
| 26 | //! [0u8, dice::HASH_SIZE], // authority_hash |
| 27 | //! dice::DiceMode::kDiceModeNormal, |
| 28 | //! [0u8, dice::HIDDEN_SIZE], // hidden |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 29 | //! }; |
| 30 | //! let (cdi_attest, cdi_seal, cert_chain) = context |
| 31 | //! .main_flow(&parent_cdi_attest, &parent_cdi_seal, &input_values)?; |
| 32 | //! ``` |
| 33 | |
Alice Wang | 856d656 | 2023-02-03 13:51:08 +0000 | [diff] [blame] | 34 | pub use diced_open_dice::{ |
Alice Wang | 7cc5956 | 2023-02-08 13:17:10 +0000 | [diff] [blame^] | 35 | check_result, derive_cdi_private_key_seed, hash, retry_bcc_format_config_descriptor, Config, |
| 36 | DiceError, Hash, Hidden, InputValues, Result, CDI_SIZE, HASH_SIZE, HIDDEN_SIZE, |
| 37 | PRIVATE_KEY_SEED_SIZE, |
Alice Wang | 856d656 | 2023-02-03 13:51:08 +0000 | [diff] [blame] | 38 | }; |
| 39 | use keystore2_crypto::ZVec; |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 40 | use open_dice_bcc_bindgen::BccMainFlow; |
Alice Wang | 97d47a7 | 2023-01-31 12:48:02 +0000 | [diff] [blame] | 41 | pub use open_dice_cbor_bindgen::DiceMode; |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 42 | use open_dice_cbor_bindgen::{ |
Alice Wang | 7cc5956 | 2023-02-08 13:17:10 +0000 | [diff] [blame^] | 43 | DiceGenerateCertificate, DiceKdf, DiceKeypairFromSeed, DiceMainFlow, DiceSign, DiceVerify, |
| 44 | DICE_PRIVATE_KEY_SIZE, DICE_PUBLIC_KEY_SIZE, DICE_SIGNATURE_SIZE, |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 45 | }; |
Alice Wang | 856d656 | 2023-02-03 13:51:08 +0000 | [diff] [blame] | 46 | use std::ffi::c_void; |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 47 | |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 48 | /// The size of a private key. |
| 49 | pub const PRIVATE_KEY_SIZE: usize = DICE_PRIVATE_KEY_SIZE as usize; |
| 50 | /// The size of a public key. |
| 51 | pub const PUBLIC_KEY_SIZE: usize = DICE_PUBLIC_KEY_SIZE as usize; |
| 52 | /// The size of a signature. |
| 53 | pub const SIGNATURE_SIZE: usize = DICE_SIGNATURE_SIZE as usize; |
| 54 | |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 55 | /// Multiple of the open dice function required preallocated output buffer |
| 56 | /// which may be too small, this function implements the retry logic to handle |
| 57 | /// too small buffer allocations. |
| 58 | /// The callback `F` must expect a mutable reference to a buffer and a size hint |
| 59 | /// field. The callback is called repeatedly as long as it returns |
| 60 | /// `Err(Error::BufferTooSmall)`. If the size hint remains 0, the buffer size is |
| 61 | /// doubled with each iteration. If the size hint is set by the callback, the buffer |
| 62 | /// will be set to accommodate at least this many bytes. |
| 63 | /// If the callback returns `Ok(())`, the buffer is truncated to the size hint |
| 64 | /// exactly. |
| 65 | /// The function panics if the callback returns `Ok(())` and the size hint is |
| 66 | /// larger than the buffer size. |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 67 | /// TODO(b/267575445): Remove this method once we migrate all its callers to |
| 68 | /// `retry_with_bigger_buffer` in `diced_open_dice`. |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 69 | fn retry_while_adjusting_output_buffer<F>(mut f: F) -> Result<Vec<u8>> |
| 70 | where |
| 71 | F: FnMut(&mut Vec<u8>, &mut usize) -> Result<()>, |
| 72 | { |
| 73 | let mut buffer = vec![0; INITIAL_OUT_BUFFER_SIZE]; |
| 74 | let mut actual_size: usize = 0; |
| 75 | loop { |
| 76 | match f(&mut buffer, &mut actual_size) { |
| 77 | // If Error::BufferTooSmall was returned, the allocated certificate |
| 78 | // buffer was to small for the output. So the buffer is resized to the actual |
| 79 | // size, and a second attempt is made with the new buffer. |
Alice Wang | 856d656 | 2023-02-03 13:51:08 +0000 | [diff] [blame] | 80 | Err(DiceError::BufferTooSmall) => { |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 81 | let new_size = if actual_size == 0 { |
| 82 | // Due to an off spec implementation of open dice cbor, actual size |
| 83 | // does not return the required size if the buffer was too small. So |
| 84 | // we have to try and approach it gradually. |
| 85 | buffer.len() * 2 |
| 86 | } else { |
| 87 | actual_size |
| 88 | }; |
| 89 | buffer.resize(new_size, 0); |
| 90 | continue; |
| 91 | } |
| 92 | Err(e) => return Err(e), |
| 93 | Ok(()) => { |
| 94 | if actual_size > buffer.len() { |
| 95 | panic!( |
| 96 | "actual_size larger than buffer size: open-dice function |
| 97 | may have written past the end of the buffer." |
| 98 | ); |
| 99 | } |
| 100 | // Truncate the certificate buffer to the actual size because it may be |
| 101 | // smaller than the original allocation. |
| 102 | buffer.truncate(actual_size); |
| 103 | return Ok(buffer); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// Some libopen-dice variants use a context. Developers that want to customize these |
| 110 | /// bindings may want to implement their own Context factory that creates a context |
| 111 | /// useable by their preferred backend. |
| 112 | pub trait Context { |
| 113 | /// # Safety |
| 114 | /// The return value of get_context is passed to any open dice function. |
| 115 | /// Implementations must explain why the context pointer returned is safe |
| 116 | /// to be used by the open dice library. |
| 117 | unsafe fn get_context(&mut self) -> *mut c_void; |
| 118 | } |
| 119 | |
| 120 | impl<T: Context + Send> ContextImpl for T {} |
| 121 | |
| 122 | /// This represents a context for the open dice library. The wrapped open dice instance, which |
| 123 | /// is based on boringssl and cbor, does not use a context, so that this type is empty. |
| 124 | #[derive(Default)] |
| 125 | pub struct OpenDiceCborContext(); |
| 126 | |
| 127 | impl OpenDiceCborContext { |
| 128 | /// Construct a new instance of OpenDiceCborContext. |
| 129 | pub fn new() -> Self { |
| 130 | Default::default() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl Context for OpenDiceCborContext { |
| 135 | unsafe fn get_context(&mut self) -> *mut c_void { |
| 136 | // # Safety |
| 137 | // The open dice cbor implementation does not use a context. It is safe |
| 138 | // to return NULL. |
| 139 | std::ptr::null_mut() |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// Type alias for ZVec indicating that it holds a CDI_ATTEST secret. |
| 144 | pub type CdiAttest = ZVec; |
| 145 | |
| 146 | /// Type alias for ZVec indicating that it holds a CDI_SEAL secret. |
| 147 | pub type CdiSeal = ZVec; |
| 148 | |
| 149 | /// Type alias for Vec<u8> indicating that it hold a DICE certificate. |
| 150 | pub type Cert = Vec<u8>; |
| 151 | |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 152 | /// Type alias for Vec<u8> indicating that it holds a BCC certificate chain. |
| 153 | pub type Bcc = Vec<u8>; |
| 154 | |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 155 | const INITIAL_OUT_BUFFER_SIZE: usize = 1024; |
| 156 | |
| 157 | /// ContextImpl is a mixin trait that implements the safe wrappers around the open dice |
| 158 | /// library calls. Implementations must implement Context::get_context(). As of |
| 159 | /// this writing, the only implementation is OpenDiceCborContext, which returns NULL. |
| 160 | pub trait ContextImpl: Context + Send { |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 161 | /// Safe wrapper around open-dice DiceMainFlow, see open dice |
| 162 | /// documentation for details. |
| 163 | /// Returns a tuple of: |
| 164 | /// * The next attestation CDI, |
| 165 | /// * the next seal CDI, and |
| 166 | /// * the next attestation certificate. |
| 167 | /// `(next_attest_cdi, next_seal_cdi, next_attestation_cert)` |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 168 | fn main_flow( |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 169 | &mut self, |
| 170 | current_cdi_attest: &[u8; CDI_SIZE], |
| 171 | current_cdi_seal: &[u8; CDI_SIZE], |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 172 | input_values: &InputValues, |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 173 | ) -> Result<(CdiAttest, CdiSeal, Cert)> { |
| 174 | let mut next_attest = CdiAttest::new(CDI_SIZE)?; |
| 175 | let mut next_seal = CdiSeal::new(CDI_SIZE)?; |
| 176 | |
| 177 | // SAFETY (DiceMainFlow): |
| 178 | // * The first context argument may be NULL and is unused by the wrapped |
| 179 | // implementation. |
| 180 | // * The second argument and the third argument are const arrays of size CDI_SIZE. |
| 181 | // This is fulfilled as per the definition of the arguments `current_cdi_attest` |
| 182 | // and `current_cdi_seal. |
| 183 | // * The fourth argument is a pointer to `DiceInputValues`. It, and its indirect |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 184 | // references must be valid for the duration of the function call. |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 185 | // * The fifth and sixth argument are the length of and the pointer to the |
| 186 | // allocated certificate buffer respectively. They are used to return |
| 187 | // the generated certificate. |
| 188 | // * The seventh argument is a pointer to a mutable usize object. It is |
| 189 | // used to return the actual size of the output certificate. |
| 190 | // * The eighth argument and the ninth argument are pointers to mutable buffers of size |
| 191 | // CDI_SIZE. This is fulfilled if the allocation above succeeded. |
| 192 | // * No pointers are expected to be valid beyond the scope of the function |
| 193 | // call. |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 194 | let cert = retry_while_adjusting_output_buffer(|cert, actual_size| { |
| 195 | check_result(unsafe { |
| 196 | DiceMainFlow( |
| 197 | self.get_context(), |
| 198 | current_cdi_attest.as_ptr(), |
| 199 | current_cdi_seal.as_ptr(), |
| 200 | input_values.as_ptr(), |
| 201 | cert.len(), |
| 202 | cert.as_mut_ptr(), |
| 203 | actual_size as *mut _, |
| 204 | next_attest.as_mut_ptr(), |
| 205 | next_seal.as_mut_ptr(), |
| 206 | ) |
| 207 | }) |
| 208 | })?; |
| 209 | Ok((next_attest, next_seal, cert)) |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 212 | /// Safe wrapper around open-dice DiceKdf, see open dice |
| 213 | /// documentation for details. |
| 214 | fn kdf(&mut self, length: usize, input_key: &[u8], salt: &[u8], info: &[u8]) -> Result<ZVec> { |
| 215 | let mut output = ZVec::new(length)?; |
| 216 | |
| 217 | // SAFETY: |
| 218 | // * The first context argument may be NULL and is unused by the wrapped |
| 219 | // implementation. |
| 220 | // * The second argument is primitive. |
| 221 | // * The third argument and the fourth argument are the pointer to and length of the given |
| 222 | // input key. |
| 223 | // * The fifth argument and the sixth argument are the pointer to and length of the given |
| 224 | // salt. |
| 225 | // * The seventh argument and the eighth argument are the pointer to and length of the |
| 226 | // given info field. |
| 227 | // * The ninth argument is a pointer to the output buffer which must have the |
| 228 | // length given by the `length` argument (see second argument). This is |
| 229 | // fulfilled if the allocation of `output` succeeds. |
| 230 | // * All pointers must be valid for the duration of the function call, but not |
| 231 | // longer. |
| 232 | check_result(unsafe { |
| 233 | DiceKdf( |
| 234 | self.get_context(), |
| 235 | length, |
| 236 | input_key.as_ptr(), |
| 237 | input_key.len(), |
| 238 | salt.as_ptr(), |
| 239 | salt.len(), |
| 240 | info.as_ptr(), |
| 241 | info.len(), |
| 242 | output.as_mut_ptr(), |
| 243 | ) |
| 244 | })?; |
| 245 | Ok(output) |
| 246 | } |
| 247 | |
| 248 | /// Safe wrapper around open-dice DiceKeyPairFromSeed, see open dice |
| 249 | /// documentation for details. |
| 250 | fn keypair_from_seed(&mut self, seed: &[u8; PRIVATE_KEY_SEED_SIZE]) -> Result<(Vec<u8>, ZVec)> { |
| 251 | let mut private_key = ZVec::new(PRIVATE_KEY_SIZE)?; |
| 252 | let mut public_key = vec![0u8; PUBLIC_KEY_SIZE]; |
| 253 | |
| 254 | // SAFETY: |
| 255 | // * The first context argument may be NULL and is unused by the wrapped |
| 256 | // implementation. |
| 257 | // * The second argument is a pointer to a const buffer of size `PRIVATE_KEY_SEED_SIZE` |
| 258 | // fulfilled by the definition of the argument. |
| 259 | // * The third argument and the fourth argument are mutable buffers of size |
| 260 | // `PRIVATE_KEY_SIZE` and `PUBLIC_KEY_SIZE` respectively. This is fulfilled by the |
| 261 | // allocations above. |
| 262 | // * All pointers must be valid for the duration of the function call but not beyond. |
| 263 | check_result(unsafe { |
| 264 | DiceKeypairFromSeed( |
| 265 | self.get_context(), |
| 266 | seed.as_ptr(), |
| 267 | public_key.as_mut_ptr(), |
| 268 | private_key.as_mut_ptr(), |
| 269 | ) |
| 270 | })?; |
| 271 | Ok((public_key, private_key)) |
| 272 | } |
| 273 | |
| 274 | /// Safe wrapper around open-dice DiceSign, see open dice |
| 275 | /// documentation for details. |
| 276 | fn sign(&mut self, message: &[u8], private_key: &[u8; PRIVATE_KEY_SIZE]) -> Result<Vec<u8>> { |
| 277 | let mut signature = vec![0u8; SIGNATURE_SIZE]; |
| 278 | |
| 279 | // SAFETY: |
| 280 | // * The first context argument may be NULL and is unused by the wrapped |
| 281 | // implementation. |
| 282 | // * The second argument and the third argument are the pointer to and length of the given |
| 283 | // message buffer. |
| 284 | // * The fourth argument is a const buffer of size `PRIVATE_KEY_SIZE`. This is fulfilled |
| 285 | // by the definition of `private key`. |
| 286 | // * The fifth argument is mutable buffer of size `SIGNATURE_SIZE`. This is fulfilled |
| 287 | // by the allocation above. |
| 288 | // * All pointers must be valid for the duration of the function call but not beyond. |
| 289 | check_result(unsafe { |
| 290 | DiceSign( |
| 291 | self.get_context(), |
| 292 | message.as_ptr(), |
| 293 | message.len(), |
| 294 | private_key.as_ptr(), |
| 295 | signature.as_mut_ptr(), |
| 296 | ) |
| 297 | })?; |
| 298 | Ok(signature) |
| 299 | } |
| 300 | |
| 301 | /// Safe wrapper around open-dice DiceVerify, see open dice |
| 302 | /// documentation for details. |
| 303 | fn verify( |
| 304 | &mut self, |
| 305 | message: &[u8], |
| 306 | signature: &[u8; SIGNATURE_SIZE], |
| 307 | public_key: &[u8; PUBLIC_KEY_SIZE], |
| 308 | ) -> Result<()> { |
| 309 | // SAFETY: |
| 310 | // * The first context argument may be NULL and is unused by the wrapped |
| 311 | // implementation. |
| 312 | // * The second argument and the third argument are the pointer to and length of the given |
| 313 | // message buffer. |
| 314 | // * The fourth argument is a const buffer of size `SIGNATURE_SIZE`. This is fulfilled |
| 315 | // by the definition of `signature`. |
| 316 | // * The fifth argument is a const buffer of size `PUBLIC_KEY_SIZE`. This is fulfilled |
| 317 | // by the definition of `public_key`. |
| 318 | // * All pointers must be valid for the duration of the function call but not beyond. |
| 319 | check_result(unsafe { |
| 320 | DiceVerify( |
| 321 | self.get_context(), |
| 322 | message.as_ptr(), |
| 323 | message.len(), |
| 324 | signature.as_ptr(), |
| 325 | public_key.as_ptr(), |
| 326 | ) |
| 327 | }) |
| 328 | } |
| 329 | |
| 330 | /// Safe wrapper around open-dice DiceGenerateCertificate, see open dice |
| 331 | /// documentation for details. |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 332 | fn generate_certificate( |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 333 | &mut self, |
| 334 | subject_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE], |
| 335 | authority_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE], |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 336 | input_values: &InputValues, |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 337 | ) -> Result<Vec<u8>> { |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 338 | // SAFETY (DiceGenerateCertificate): |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 339 | // * The first context argument may be NULL and is unused by the wrapped |
| 340 | // implementation. |
| 341 | // * The second argument and the third argument are const arrays of size |
| 342 | // `PRIVATE_KEY_SEED_SIZE`. This is fulfilled as per the definition of the arguments. |
| 343 | // * The fourth argument is a pointer to `DiceInputValues` it, and its indirect |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 344 | // references must be valid for the duration of the function call. |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 345 | // * The fifth argument and the sixth argument are the length of and the pointer to the |
| 346 | // allocated certificate buffer respectively. They are used to return |
| 347 | // the generated certificate. |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 348 | // * The seventh argument is a pointer to a mutable usize object. It is |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 349 | // used to return the actual size of the output certificate. |
| 350 | // * All pointers must be valid for the duration of the function call but not beyond. |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 351 | let cert = retry_while_adjusting_output_buffer(|cert, actual_size| { |
| 352 | check_result(unsafe { |
| 353 | DiceGenerateCertificate( |
| 354 | self.get_context(), |
| 355 | subject_private_key_seed.as_ptr(), |
| 356 | authority_private_key_seed.as_ptr(), |
| 357 | input_values.as_ptr(), |
| 358 | cert.len(), |
| 359 | cert.as_mut_ptr(), |
| 360 | actual_size as *mut _, |
| 361 | ) |
| 362 | }) |
| 363 | })?; |
| 364 | Ok(cert) |
Janis Danisevskis | 13356b2 | 2021-10-20 09:44:12 -0700 | [diff] [blame] | 365 | } |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 366 | |
| 367 | /// Safe wrapper around open-dice BccDiceMainFlow, see open dice |
| 368 | /// documentation for details. |
| 369 | /// Returns a tuple of: |
| 370 | /// * The next attestation CDI, |
| 371 | /// * the next seal CDI, and |
| 372 | /// * the next bcc adding the new certificate to the given bcc. |
| 373 | /// `(next_attest_cdi, next_seal_cdi, next_bcc)` |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 374 | fn bcc_main_flow( |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 375 | &mut self, |
| 376 | current_cdi_attest: &[u8; CDI_SIZE], |
| 377 | current_cdi_seal: &[u8; CDI_SIZE], |
| 378 | bcc: &[u8], |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 379 | input_values: &InputValues, |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 380 | ) -> Result<(CdiAttest, CdiSeal, Bcc)> { |
| 381 | let mut next_attest = CdiAttest::new(CDI_SIZE)?; |
| 382 | let mut next_seal = CdiSeal::new(CDI_SIZE)?; |
| 383 | |
| 384 | // SAFETY (BccMainFlow): |
| 385 | // * The first context argument may be NULL and is unused by the wrapped |
| 386 | // implementation. |
| 387 | // * The second argument and the third argument are const arrays of size CDI_SIZE. |
| 388 | // This is fulfilled as per the definition of the arguments `current_cdi_attest` |
| 389 | // and `current_cdi_seal`. |
| 390 | // * The fourth argument and the fifth argument are the pointer to and size of the buffer |
| 391 | // holding the current bcc. |
| 392 | // * The sixth argument is a pointer to `DiceInputValues` it, and its indirect |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 393 | // references must be valid for the duration of the function call. |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 394 | // * The seventh argument and the eighth argument are the length of and the pointer to the |
| 395 | // allocated certificate buffer respectively. They are used to return the generated |
| 396 | // certificate. |
| 397 | // * The ninth argument is a pointer to a mutable usize object. It is |
| 398 | // used to return the actual size of the output certificate. |
| 399 | // * The tenth argument and the eleventh argument are pointers to mutable buffers of |
| 400 | // size CDI_SIZE. This is fulfilled if the allocation above succeeded. |
| 401 | // * No pointers are expected to be valid beyond the scope of the function |
| 402 | // call. |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 403 | let next_bcc = retry_while_adjusting_output_buffer(|next_bcc, actual_size| { |
| 404 | check_result(unsafe { |
| 405 | BccMainFlow( |
| 406 | self.get_context(), |
| 407 | current_cdi_attest.as_ptr(), |
| 408 | current_cdi_seal.as_ptr(), |
| 409 | bcc.as_ptr(), |
| 410 | bcc.len(), |
| 411 | input_values.as_ptr(), |
| 412 | next_bcc.len(), |
| 413 | next_bcc.as_mut_ptr(), |
| 414 | actual_size as *mut _, |
| 415 | next_attest.as_mut_ptr(), |
| 416 | next_seal.as_mut_ptr(), |
| 417 | ) |
| 418 | }) |
| 419 | })?; |
| 420 | Ok((next_attest, next_seal, next_bcc)) |
Janis Danisevskis | 2cef73f | 2021-11-03 15:02:48 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Janis Danisevskis | db78b77 | 2021-11-29 17:59:50 -0800 | [diff] [blame] | 424 | #[cfg(test)] |
| 425 | mod test { |
| 426 | use super::*; |
| 427 | use diced_sample_inputs::make_sample_bcc_and_cdis; |
| 428 | use std::convert::TryInto; |
| 429 | |
| 430 | static SEED_TEST_VECTOR: &[u8] = &[ |
| 431 | 0xfa, 0x3c, 0x2f, 0x58, 0x37, 0xf5, 0x8e, 0x96, 0x16, 0x09, 0xf5, 0x22, 0xa1, 0xf1, 0xba, |
| 432 | 0xaa, 0x19, 0x95, 0x01, 0x79, 0x2e, 0x60, 0x56, 0xaf, 0xf6, 0x41, 0xe7, 0xff, 0x48, 0xf5, |
| 433 | 0x3a, 0x08, 0x84, 0x8a, 0x98, 0x85, 0x6d, 0xf5, 0x69, 0x21, 0x03, 0xcd, 0x09, 0xc3, 0x28, |
| 434 | 0xd6, 0x06, 0xa7, 0x57, 0xbd, 0x48, 0x4b, 0x0f, 0x79, 0x0f, 0xf8, 0x2f, 0xf0, 0x0a, 0x41, |
| 435 | 0x94, 0xd8, 0x8c, 0xa8, |
| 436 | ]; |
| 437 | |
| 438 | static CDI_ATTEST_TEST_VECTOR: &[u8] = &[ |
| 439 | 0xfa, 0x3c, 0x2f, 0x58, 0x37, 0xf5, 0x8e, 0x96, 0x16, 0x09, 0xf5, 0x22, 0xa1, 0xf1, 0xba, |
| 440 | 0xaa, 0x19, 0x95, 0x01, 0x79, 0x2e, 0x60, 0x56, 0xaf, 0xf6, 0x41, 0xe7, 0xff, 0x48, 0xf5, |
| 441 | 0x3a, 0x08, |
| 442 | ]; |
| 443 | static CDI_PRIVATE_KEY_SEED_TEST_VECTOR: &[u8] = &[ |
| 444 | 0x5f, 0xcc, 0x8e, 0x1a, 0xd1, 0xc2, 0xb3, 0xe9, 0xfb, 0xe1, 0x68, 0xf0, 0xf6, 0x98, 0xfe, |
| 445 | 0x0d, 0xee, 0xd4, 0xb5, 0x18, 0xcb, 0x59, 0x70, 0x2d, 0xee, 0x06, 0xe5, 0x70, 0xf1, 0x72, |
| 446 | 0x02, 0x6e, |
| 447 | ]; |
| 448 | |
| 449 | static PUB_KEY_TEST_VECTOR: &[u8] = &[ |
| 450 | 0x47, 0x42, 0x4b, 0xbd, 0xd7, 0x23, 0xb4, 0xcd, 0xca, 0xe2, 0x8e, 0xdc, 0x6b, 0xfc, 0x23, |
| 451 | 0xc9, 0x21, 0x5c, 0x48, 0x21, 0x47, 0xee, 0x5b, 0xfa, 0xaf, 0x88, 0x9a, 0x52, 0xf1, 0x61, |
| 452 | 0x06, 0x37, |
| 453 | ]; |
| 454 | static PRIV_KEY_TEST_VECTOR: &[u8] = &[ |
| 455 | 0x5f, 0xcc, 0x8e, 0x1a, 0xd1, 0xc2, 0xb3, 0xe9, 0xfb, 0xe1, 0x68, 0xf0, 0xf6, 0x98, 0xfe, |
| 456 | 0x0d, 0xee, 0xd4, 0xb5, 0x18, 0xcb, 0x59, 0x70, 0x2d, 0xee, 0x06, 0xe5, 0x70, 0xf1, 0x72, |
| 457 | 0x02, 0x6e, 0x47, 0x42, 0x4b, 0xbd, 0xd7, 0x23, 0xb4, 0xcd, 0xca, 0xe2, 0x8e, 0xdc, 0x6b, |
| 458 | 0xfc, 0x23, 0xc9, 0x21, 0x5c, 0x48, 0x21, 0x47, 0xee, 0x5b, 0xfa, 0xaf, 0x88, 0x9a, 0x52, |
| 459 | 0xf1, 0x61, 0x06, 0x37, |
| 460 | ]; |
| 461 | |
| 462 | static SIGNATURE_TEST_VECTOR: &[u8] = &[ |
| 463 | 0x44, 0xae, 0xcc, 0xe2, 0xb9, 0x96, 0x18, 0x39, 0x0e, 0x61, 0x0f, 0x53, 0x07, 0xbf, 0xf2, |
| 464 | 0x32, 0x3d, 0x44, 0xd4, 0xf2, 0x07, 0x23, 0x30, 0x85, 0x32, 0x18, 0xd2, 0x69, 0xb8, 0x29, |
| 465 | 0x3c, 0x26, 0xe6, 0x0d, 0x9c, 0xa5, 0xc2, 0x73, 0xcd, 0x8c, 0xb8, 0x3c, 0x3e, 0x5b, 0xfd, |
| 466 | 0x62, 0x8d, 0xf6, 0xc4, 0x27, 0xa6, 0xe9, 0x11, 0x06, 0x5a, 0xb2, 0x2b, 0x64, 0xf7, 0xfc, |
| 467 | 0xbb, 0xab, 0x4a, 0x0e, |
| 468 | ]; |
| 469 | |
| 470 | #[test] |
| 471 | fn hash_derive_sign_verify() { |
| 472 | let mut ctx = OpenDiceCborContext::new(); |
Alice Wang | 24954b4 | 2023-02-06 10:03:45 +0000 | [diff] [blame] | 473 | let seed = diced_open_dice::hash("MySeedString".as_bytes()).unwrap(); |
Janis Danisevskis | db78b77 | 2021-11-29 17:59:50 -0800 | [diff] [blame] | 474 | assert_eq!(seed, SEED_TEST_VECTOR); |
| 475 | let cdi_attest = &seed[..CDI_SIZE]; |
| 476 | assert_eq!(cdi_attest, CDI_ATTEST_TEST_VECTOR); |
| 477 | let cdi_private_key_seed = |
Alice Wang | 7cc5956 | 2023-02-08 13:17:10 +0000 | [diff] [blame^] | 478 | derive_cdi_private_key_seed(cdi_attest.try_into().unwrap()).unwrap(); |
Janis Danisevskis | db78b77 | 2021-11-29 17:59:50 -0800 | [diff] [blame] | 479 | assert_eq!(&cdi_private_key_seed[..], CDI_PRIVATE_KEY_SEED_TEST_VECTOR); |
| 480 | let (pub_key, priv_key) = |
| 481 | ctx.keypair_from_seed(cdi_private_key_seed[..].try_into().unwrap()).unwrap(); |
| 482 | assert_eq!(&pub_key, PUB_KEY_TEST_VECTOR); |
| 483 | assert_eq!(&priv_key[..], PRIV_KEY_TEST_VECTOR); |
| 484 | let mut signature = |
| 485 | ctx.sign("MyMessage".as_bytes(), priv_key[..].try_into().unwrap()).unwrap(); |
| 486 | assert_eq!(&signature, SIGNATURE_TEST_VECTOR); |
| 487 | assert!(ctx |
| 488 | .verify( |
| 489 | "MyMessage".as_bytes(), |
| 490 | signature[..].try_into().unwrap(), |
| 491 | pub_key[..].try_into().unwrap() |
| 492 | ) |
| 493 | .is_ok()); |
| 494 | assert!(ctx |
| 495 | .verify( |
| 496 | "MyMessage_fail".as_bytes(), |
| 497 | signature[..].try_into().unwrap(), |
| 498 | pub_key[..].try_into().unwrap() |
| 499 | ) |
| 500 | .is_err()); |
| 501 | signature[0] += 1; |
| 502 | assert!(ctx |
| 503 | .verify( |
| 504 | "MyMessage".as_bytes(), |
| 505 | signature[..].try_into().unwrap(), |
| 506 | pub_key[..].try_into().unwrap() |
| 507 | ) |
| 508 | .is_err()); |
| 509 | } |
| 510 | |
| 511 | static SAMPLE_CDI_ATTEST_TEST_VECTOR: &[u8] = &[ |
| 512 | 0x3e, 0x57, 0x65, 0x5d, 0x48, 0x02, 0xbd, 0x5c, 0x66, 0xcc, 0x1f, 0x0f, 0xbe, 0x5e, 0x32, |
| 513 | 0xb6, 0x9e, 0x3d, 0x04, 0xaf, 0x00, 0x15, 0xbc, 0xdd, 0x1f, 0xbc, 0x59, 0xe4, 0xc3, 0x87, |
| 514 | 0x95, 0x5e, |
| 515 | ]; |
| 516 | |
| 517 | static SAMPLE_CDI_SEAL_TEST_VECTOR: &[u8] = &[ |
| 518 | 0x36, 0x1b, 0xd2, 0xb3, 0xc4, 0xda, 0x77, 0xb2, 0x9c, 0xba, 0x39, 0x53, 0x82, 0x93, 0xd9, |
| 519 | 0xb8, 0x9f, 0x73, 0x2d, 0x27, 0x06, 0x15, 0xa8, 0xcb, 0x6d, 0x1d, 0xf2, 0xb1, 0x54, 0xbb, |
| 520 | 0x62, 0xf1, |
| 521 | ]; |
| 522 | |
| 523 | static SAMPLE_BCC_TEST_VECTOR: &[u8] = &[ |
Alan Stokes | 7cdcf99 | 2022-05-24 07:42:24 +0000 | [diff] [blame] | 524 | 0x84, 0xa5, 0x01, 0x01, 0x03, 0x27, 0x04, 0x81, 0x02, 0x20, 0x06, 0x21, 0x58, 0x20, 0x3e, |
| 525 | 0x85, 0xe5, 0x72, 0x75, 0x55, 0xe5, 0x1e, 0xe7, 0xf3, 0x35, 0x94, 0x8e, 0xbb, 0xbd, 0x74, |
| 526 | 0x1e, 0x1d, 0xca, 0x49, 0x9c, 0x97, 0x39, 0x77, 0x06, 0xd3, 0xc8, 0x6e, 0x8b, 0xd7, 0x33, |
| 527 | 0xf9, 0x84, 0x43, 0xa1, 0x01, 0x27, 0xa0, 0x59, 0x01, 0x8a, 0xa9, 0x01, 0x78, 0x28, 0x34, |
| 528 | 0x32, 0x64, 0x38, 0x38, 0x36, 0x34, 0x66, 0x39, 0x37, 0x62, 0x36, 0x35, 0x34, 0x37, 0x61, |
| 529 | 0x35, 0x30, 0x63, 0x31, 0x65, 0x30, 0x61, 0x37, 0x34, 0x39, 0x66, 0x38, 0x65, 0x66, 0x38, |
| 530 | 0x62, 0x38, 0x31, 0x65, 0x63, 0x36, 0x32, 0x61, 0x66, 0x02, 0x78, 0x28, 0x31, 0x66, 0x36, |
| 531 | 0x39, 0x36, 0x66, 0x30, 0x37, 0x32, 0x35, 0x32, 0x66, 0x32, 0x39, 0x65, 0x39, 0x33, 0x66, |
| 532 | 0x65, 0x34, 0x64, 0x65, 0x31, 0x39, 0x65, 0x65, 0x33, 0x32, 0x63, 0x64, 0x38, 0x31, 0x64, |
| 533 | 0x63, 0x34, 0x30, 0x34, 0x65, 0x37, 0x36, 0x3a, 0x00, 0x47, 0x44, 0x50, 0x58, 0x40, 0x16, |
| 534 | 0x48, 0xf2, 0x55, 0x53, 0x23, 0xdd, 0x15, 0x2e, 0x83, 0x38, 0xc3, 0x64, 0x38, 0x63, 0x26, |
| 535 | 0x0f, 0xcf, 0x5b, 0xd1, 0x3a, 0xd3, 0x40, 0x3e, 0x23, 0xf8, 0x34, 0x4c, 0x6d, 0xa2, 0xbe, |
| 536 | 0x25, 0x1c, 0xb0, 0x29, 0xe8, 0xc3, 0xfb, 0xb8, 0x80, 0xdc, 0xb1, 0xd2, 0xb3, 0x91, 0x4d, |
| 537 | 0xd3, 0xfb, 0x01, 0x0f, 0xe4, 0xe9, 0x46, 0xa2, 0xc0, 0x26, 0x57, 0x5a, 0xba, 0x30, 0xf7, |
| 538 | 0x15, 0x98, 0x14, 0x3a, 0x00, 0x47, 0x44, 0x53, 0x56, 0xa3, 0x3a, 0x00, 0x01, 0x11, 0x71, |
| 539 | 0x63, 0x41, 0x42, 0x4c, 0x3a, 0x00, 0x01, 0x11, 0x72, 0x01, 0x3a, 0x00, 0x01, 0x11, 0x73, |
| 540 | 0xf6, 0x3a, 0x00, 0x47, 0x44, 0x52, 0x58, 0x40, 0x47, 0xae, 0x42, 0x27, 0x4c, 0xcb, 0x65, |
| 541 | 0x4d, 0xee, 0x74, 0x2d, 0x05, 0x78, 0x2a, 0x08, 0x2a, 0xa5, 0xf0, 0xcf, 0xea, 0x3e, 0x60, |
| 542 | 0xee, 0x97, 0x11, 0x4b, 0x5b, 0xe6, 0x05, 0x0c, 0xe8, 0x90, 0xf5, 0x22, 0xc4, 0xc6, 0x67, |
| 543 | 0x7a, 0x22, 0x27, 0x17, 0xb3, 0x79, 0xcc, 0x37, 0x64, 0x5e, 0x19, 0x4f, 0x96, 0x37, 0x67, |
| 544 | 0x3c, 0xd0, 0xc5, 0xed, 0x0f, 0xdd, 0xe7, 0x2e, 0x4f, 0x70, 0x97, 0x30, 0x3a, 0x00, 0x47, |
| 545 | 0x44, 0x54, 0x58, 0x40, 0xf9, 0x00, 0x9d, 0xc2, 0x59, 0x09, 0xe0, 0xb6, 0x98, 0xbd, 0xe3, |
| 546 | 0x97, 0x4a, 0xcb, 0x3c, 0xe7, 0x6b, 0x24, 0xc3, 0xe4, 0x98, 0xdd, 0xa9, 0x6a, 0x41, 0x59, |
| 547 | 0x15, 0xb1, 0x23, 0xe6, 0xc8, 0xdf, 0xfb, 0x52, 0xb4, 0x52, 0xc1, 0xb9, 0x61, 0xdd, 0xbc, |
| 548 | 0x5b, 0x37, 0x0e, 0x12, 0x12, 0xb2, 0xfd, 0xc1, 0x09, 0xb0, 0xcf, 0x33, 0x81, 0x4c, 0xc6, |
| 549 | 0x29, 0x1b, 0x99, 0xea, 0xae, 0xfd, 0xaa, 0x0d, 0x3a, 0x00, 0x47, 0x44, 0x56, 0x41, 0x01, |
| 550 | 0x3a, 0x00, 0x47, 0x44, 0x57, 0x58, 0x2d, 0xa5, 0x01, 0x01, 0x03, 0x27, 0x04, 0x81, 0x02, |
| 551 | 0x20, 0x06, 0x21, 0x58, 0x20, 0xb1, 0x02, 0xcc, 0x2c, 0xb2, 0x6a, 0x3b, 0xe9, 0xc1, 0xd3, |
| 552 | 0x95, 0x10, 0xa0, 0xe1, 0xff, 0x51, 0xde, 0x57, 0xd5, 0x65, 0x28, 0xfd, 0x7f, 0xeb, 0xd4, |
| 553 | 0xca, 0x15, 0xf3, 0xca, 0xdf, 0x37, 0x88, 0x3a, 0x00, 0x47, 0x44, 0x58, 0x41, 0x20, 0x58, |
| 554 | 0x40, 0x58, 0xd8, 0x03, 0x24, 0x53, 0x60, 0x57, 0xa9, 0x09, 0xfa, 0xab, 0xdc, 0x57, 0x1e, |
| 555 | 0xf0, 0xe5, 0x1e, 0x51, 0x6f, 0x9e, 0xa3, 0x42, 0xe6, 0x6a, 0x8c, 0xaa, 0xad, 0x08, 0x48, |
| 556 | 0xde, 0x7f, 0x4f, 0x6e, 0x2f, 0x7f, 0x39, 0x6c, 0xa1, 0xf8, 0x42, 0x71, 0xfe, 0x17, 0x3d, |
| 557 | 0xca, 0x31, 0x83, 0x92, 0xed, 0xbb, 0x40, 0xb8, 0x10, 0xe0, 0xf2, 0x5a, 0x99, 0x53, 0x38, |
| 558 | 0x46, 0x33, 0x97, 0x78, 0x05, 0x84, 0x43, 0xa1, 0x01, 0x27, 0xa0, 0x59, 0x01, 0x8a, 0xa9, |
| 559 | 0x01, 0x78, 0x28, 0x31, 0x66, 0x36, 0x39, 0x36, 0x66, 0x30, 0x37, 0x32, 0x35, 0x32, 0x66, |
| 560 | 0x32, 0x39, 0x65, 0x39, 0x33, 0x66, 0x65, 0x34, 0x64, 0x65, 0x31, 0x39, 0x65, 0x65, 0x33, |
| 561 | 0x32, 0x63, 0x64, 0x38, 0x31, 0x64, 0x63, 0x34, 0x30, 0x34, 0x65, 0x37, 0x36, 0x02, 0x78, |
| 562 | 0x28, 0x32, 0x35, 0x39, 0x34, 0x38, 0x39, 0x65, 0x36, 0x39, 0x37, 0x34, 0x38, 0x37, 0x30, |
| 563 | 0x35, 0x64, 0x65, 0x33, 0x65, 0x32, 0x66, 0x34, 0x34, 0x32, 0x36, 0x37, 0x65, 0x61, 0x34, |
| 564 | 0x39, 0x33, 0x38, 0x66, 0x66, 0x36, 0x61, 0x35, 0x37, 0x32, 0x35, 0x3a, 0x00, 0x47, 0x44, |
| 565 | 0x50, 0x58, 0x40, 0xa4, 0x0c, 0xcb, 0xc1, 0xbf, 0xfa, 0xcc, 0xfd, 0xeb, 0xf4, 0xfc, 0x43, |
| 566 | 0x83, 0x7f, 0x46, 0x8d, 0xd8, 0xd8, 0x14, 0xc1, 0x96, 0x14, 0x1f, 0x6e, 0xb3, 0xa0, 0xd9, |
| 567 | 0x56, 0xb3, 0xbf, 0x2f, 0xfa, 0x88, 0x70, 0x11, 0x07, 0x39, 0xa4, 0xd2, 0xa9, 0x6b, 0x18, |
| 568 | 0x28, 0xe8, 0x29, 0x20, 0x49, 0x0f, 0xbb, 0x8d, 0x08, 0x8c, 0xc6, 0x54, 0xe9, 0x71, 0xd2, |
| 569 | 0x7e, 0xa4, 0xfe, 0x58, 0x7f, 0xd3, 0xc7, 0x3a, 0x00, 0x47, 0x44, 0x53, 0x56, 0xa3, 0x3a, |
| 570 | 0x00, 0x01, 0x11, 0x71, 0x63, 0x41, 0x56, 0x42, 0x3a, 0x00, 0x01, 0x11, 0x72, 0x01, 0x3a, |
| 571 | 0x00, 0x01, 0x11, 0x73, 0xf6, 0x3a, 0x00, 0x47, 0x44, 0x52, 0x58, 0x40, 0x93, 0x17, 0xe1, |
| 572 | 0x11, 0x27, 0x59, 0xd0, 0xef, 0x75, 0x0b, 0x2b, 0x1c, 0x0f, 0x5f, 0x52, 0xc3, 0x29, 0x23, |
| 573 | 0xb5, 0x2a, 0xe6, 0x12, 0x72, 0x6f, 0x39, 0x86, 0x65, 0x2d, 0xf2, 0xe4, 0xe7, 0xd0, 0xaf, |
| 574 | 0x0e, 0xa7, 0x99, 0x16, 0x89, 0x97, 0x21, 0xf7, 0xdc, 0x89, 0xdc, 0xde, 0xbb, 0x94, 0x88, |
| 575 | 0x1f, 0xda, 0xe2, 0xf3, 0xe0, 0x54, 0xf9, 0x0e, 0x29, 0xb1, 0xbd, 0xe1, 0x0c, 0x0b, 0xd7, |
| 576 | 0xf6, 0x3a, 0x00, 0x47, 0x44, 0x54, 0x58, 0x40, 0xb2, 0x69, 0x05, 0x48, 0x56, 0xb5, 0xfa, |
| 577 | 0x55, 0x6f, 0xac, 0x56, 0xd9, 0x02, 0x35, 0x2b, 0xaa, 0x4c, 0xba, 0x28, 0xdd, 0x82, 0x3a, |
| 578 | 0x86, 0xf5, 0xd4, 0xc2, 0xf1, 0xf9, 0x35, 0x7d, 0xe4, 0x43, 0x13, 0xbf, 0xfe, 0xd3, 0x36, |
| 579 | 0xd8, 0x1c, 0x12, 0x78, 0x5c, 0x9c, 0x3e, 0xf6, 0x66, 0xef, 0xab, 0x3d, 0x0f, 0x89, 0xa4, |
| 580 | 0x6f, 0xc9, 0x72, 0xee, 0x73, 0x43, 0x02, 0x8a, 0xef, 0xbc, 0x05, 0x98, 0x3a, 0x00, 0x47, |
| 581 | 0x44, 0x56, 0x41, 0x01, 0x3a, 0x00, 0x47, 0x44, 0x57, 0x58, 0x2d, 0xa5, 0x01, 0x01, 0x03, |
| 582 | 0x27, 0x04, 0x81, 0x02, 0x20, 0x06, 0x21, 0x58, 0x20, 0x96, 0x6d, 0x96, 0x42, 0xda, 0x64, |
| 583 | 0x51, 0xad, 0xfa, 0x00, 0xbc, 0xbc, 0x95, 0x8a, 0xb0, 0xb9, 0x76, 0x01, 0xe6, 0xbd, 0xc0, |
| 584 | 0x26, 0x79, 0x26, 0xfc, 0x0f, 0x1d, 0x87, 0x65, 0xf1, 0xf3, 0x99, 0x3a, 0x00, 0x47, 0x44, |
| 585 | 0x58, 0x41, 0x20, 0x58, 0x40, 0x10, 0x7f, 0x77, 0xad, 0x70, 0xbd, 0x52, 0x81, 0x28, 0x8d, |
| 586 | 0x24, 0x81, 0xb4, 0x3f, 0x21, 0x68, 0x9f, 0xc3, 0x80, 0x68, 0x86, 0x55, 0xfb, 0x2e, 0x6d, |
| 587 | 0x96, 0xe1, 0xe1, 0xb7, 0x28, 0x8d, 0x63, 0x85, 0xba, 0x2a, 0x01, 0x33, 0x87, 0x60, 0x63, |
| 588 | 0xbb, 0x16, 0x3f, 0x2f, 0x3d, 0xf4, 0x2d, 0x48, 0x5b, 0x87, 0xed, 0xda, 0x34, 0xeb, 0x9c, |
| 589 | 0x4d, 0x14, 0xac, 0x65, 0xf4, 0xfa, 0xef, 0x45, 0x0b, 0x84, 0x43, 0xa1, 0x01, 0x27, 0xa0, |
| 590 | 0x59, 0x01, 0x8f, 0xa9, 0x01, 0x78, 0x28, 0x32, 0x35, 0x39, 0x34, 0x38, 0x39, 0x65, 0x36, |
| 591 | 0x39, 0x37, 0x34, 0x38, 0x37, 0x30, 0x35, 0x64, 0x65, 0x33, 0x65, 0x32, 0x66, 0x34, 0x34, |
| 592 | 0x32, 0x36, 0x37, 0x65, 0x61, 0x34, 0x39, 0x33, 0x38, 0x66, 0x66, 0x36, 0x61, 0x35, 0x37, |
| 593 | 0x32, 0x35, 0x02, 0x78, 0x28, 0x35, 0x64, 0x34, 0x65, 0x64, 0x37, 0x66, 0x34, 0x31, 0x37, |
| 594 | 0x61, 0x39, 0x35, 0x34, 0x61, 0x31, 0x38, 0x31, 0x34, 0x30, 0x37, 0x62, 0x35, 0x38, 0x38, |
| 595 | 0x35, 0x61, 0x66, 0x64, 0x37, 0x32, 0x61, 0x35, 0x62, 0x66, 0x34, 0x30, 0x64, 0x61, 0x36, |
| 596 | 0x3a, 0x00, 0x47, 0x44, 0x50, 0x58, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Janis Danisevskis | db78b77 | 2021-11-29 17:59:50 -0800 | [diff] [blame] | 597 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 598 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 599 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Alan Stokes | 7cdcf99 | 2022-05-24 07:42:24 +0000 | [diff] [blame] | 600 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x47, 0x44, |
| 601 | 0x53, 0x58, 0x1a, 0xa3, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x67, 0x41, 0x6e, 0x64, 0x72, 0x6f, |
| 602 | 0x69, 0x64, 0x3a, 0x00, 0x01, 0x11, 0x72, 0x0c, 0x3a, 0x00, 0x01, 0x11, 0x73, 0xf6, 0x3a, |
| 603 | 0x00, 0x47, 0x44, 0x52, 0x58, 0x40, 0x26, 0x1a, 0xbd, 0x26, 0xd8, 0x37, 0x8f, 0x4a, 0xf2, |
| 604 | 0x9e, 0x49, 0x4d, 0x93, 0x23, 0xc4, 0x6e, 0x02, 0xda, 0xe0, 0x00, 0x02, 0xe7, 0xed, 0x29, |
| 605 | 0xdf, 0x2b, 0xb3, 0x69, 0xf3, 0x55, 0x0e, 0x4c, 0x22, 0xdc, 0xcf, 0xf5, 0x92, 0xc9, 0xfa, |
| 606 | 0x78, 0x98, 0xf1, 0x0e, 0x55, 0x5f, 0xf4, 0x45, 0xed, 0xc0, 0x0a, 0x72, 0x2a, 0x7a, 0x3a, |
| 607 | 0xd2, 0xb1, 0xf7, 0x76, 0xfe, 0x2a, 0x6b, 0x7b, 0x2a, 0x53, 0x3a, 0x00, 0x47, 0x44, 0x54, |
| 608 | 0x58, 0x40, 0x04, 0x25, 0x5d, 0x60, 0x5f, 0x5c, 0x45, 0x0d, 0xf2, 0x9a, 0x6e, 0x99, 0x30, |
| 609 | 0x03, 0xb8, 0xd6, 0xe1, 0x99, 0x71, 0x1b, 0xf8, 0x44, 0xfa, 0xb5, 0x31, 0x79, 0x1c, 0x37, |
| 610 | 0x68, 0x4e, 0x1d, 0xc0, 0x24, 0x74, 0x68, 0xf8, 0x80, 0x20, 0x3e, 0x44, 0xb1, 0x43, 0xd2, |
| 611 | 0x9c, 0xfc, 0x12, 0x9e, 0x77, 0x0a, 0xde, 0x29, 0x24, 0xff, 0x2e, 0xfa, 0xc7, 0x10, 0xd5, |
| 612 | 0x73, 0xd4, 0xc6, 0xdf, 0x62, 0x9f, 0x3a, 0x00, 0x47, 0x44, 0x56, 0x41, 0x01, 0x3a, 0x00, |
| 613 | 0x47, 0x44, 0x57, 0x58, 0x2d, 0xa5, 0x01, 0x01, 0x03, 0x27, 0x04, 0x81, 0x02, 0x20, 0x06, |
| 614 | 0x21, 0x58, 0x20, 0xdb, 0xe7, 0x5b, 0x3f, 0xa3, 0x42, 0xb0, 0x9c, 0xf8, 0x40, 0x8c, 0xb0, |
| 615 | 0x9c, 0xf0, 0x0a, 0xaf, 0xdf, 0x6f, 0xe5, 0x09, 0x21, 0x11, 0x92, 0xe1, 0xf8, 0xc5, 0x09, |
| 616 | 0x02, 0x3d, 0x1f, 0xb7, 0xc5, 0x3a, 0x00, 0x47, 0x44, 0x58, 0x41, 0x20, 0x58, 0x40, 0xc4, |
| 617 | 0xc1, 0xd7, 0x1c, 0x2d, 0x26, 0x89, 0x22, 0xcf, 0xa6, 0x99, 0x77, 0x30, 0x84, 0x86, 0x27, |
| 618 | 0x59, 0x8f, 0xd8, 0x08, 0x75, 0xe0, 0xb2, 0xef, 0xf9, 0xfa, 0xa5, 0x40, 0x8c, 0xd3, 0xeb, |
| 619 | 0xbb, 0xda, 0xf2, 0xc8, 0xae, 0x41, 0x22, 0x50, 0x9c, 0xe8, 0xb2, 0x9c, 0x9b, 0x3f, 0x8a, |
| 620 | 0x78, 0x76, 0xab, 0xd0, 0xbe, 0xfc, 0xe4, 0x79, 0xcb, 0x1b, 0x2b, 0xaa, 0x4d, 0xdd, 0x15, |
| 621 | 0x61, 0x42, 0x06, |
Janis Danisevskis | db78b77 | 2021-11-29 17:59:50 -0800 | [diff] [blame] | 622 | ]; |
| 623 | |
| 624 | // This test invokes make_sample_bcc_and_cdis and compares the result bitwise to the target |
| 625 | // vectors. The function uses main_flow, bcc_main_flow, format_config_descriptor, |
| 626 | // derive_cdi_private_key_seed, and keypair_from_seed. This test is sensitive to errors |
| 627 | // and changes in any of those functions. |
| 628 | #[test] |
| 629 | fn main_flow_and_bcc_main_flow() { |
| 630 | let (cdi_attest, cdi_seal, bcc) = make_sample_bcc_and_cdis().unwrap(); |
| 631 | assert_eq!(&cdi_attest[..], SAMPLE_CDI_ATTEST_TEST_VECTOR); |
| 632 | assert_eq!(&cdi_seal[..], SAMPLE_CDI_SEAL_TEST_VECTOR); |
| 633 | assert_eq!(&bcc[..], SAMPLE_BCC_TEST_VECTOR); |
| 634 | } |
| 635 | |
| 636 | static DERIVED_KEY_TEST_VECTOR: &[u8] = &[ |
| 637 | 0x0e, 0xd6, 0x07, 0x0e, 0x1c, 0x38, 0x2c, 0x76, 0x13, 0xc6, 0x76, 0x25, 0x7e, 0x07, 0x6f, |
| 638 | 0xdb, 0x1d, 0xb1, 0x0f, 0x3f, 0xed, 0xc5, 0x2b, 0x95, 0xd1, 0x32, 0xf1, 0x63, 0x2f, 0x2a, |
| 639 | 0x01, 0x5e, |
| 640 | ]; |
| 641 | |
| 642 | #[test] |
| 643 | fn kdf() { |
| 644 | let mut ctx = OpenDiceCborContext::new(); |
| 645 | let derived_key = ctx |
| 646 | .kdf( |
| 647 | PRIVATE_KEY_SEED_SIZE, |
| 648 | "myKey".as_bytes(), |
| 649 | "mySalt".as_bytes(), |
| 650 | "myInfo".as_bytes(), |
| 651 | ) |
| 652 | .unwrap(); |
| 653 | assert_eq!(&derived_key[..], DERIVED_KEY_TEST_VECTOR); |
| 654 | } |
Janis Danisevskis | db78b77 | 2021-11-29 17:59:50 -0800 | [diff] [blame] | 655 | } |