blob: 3823bb7b7fb4ade29d7bfc2682c86fab03ffd6f3 [file] [log] [blame]
Alice Wangceb66422023-02-07 13:55:33 +00001/*
2 * Copyright (C) 2023 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
Alice Wang6ef44bc2023-02-08 12:08:32 +000017use diced_open_dice::{
18 derive_cdi_certificate_id, hash, kdf, HASH_SIZE, ID_SIZE, PRIVATE_KEY_SEED_SIZE,
19};
Alice Wangceb66422023-02-07 13:55:33 +000020
21#[test]
22fn hash_succeeds() {
Alice Wang6ef44bc2023-02-08 12:08:32 +000023 const EXPECTED_HASH: [u8; HASH_SIZE] = [
24 0x30, 0x9e, 0xcc, 0x48, 0x9c, 0x12, 0xd6, 0xeb, 0x4c, 0xc4, 0x0f, 0x50, 0xc9, 0x02, 0xf2,
25 0xb4, 0xd0, 0xed, 0x77, 0xee, 0x51, 0x1a, 0x7c, 0x7a, 0x9b, 0xcd, 0x3c, 0xa8, 0x6d, 0x4c,
26 0xd8, 0x6f, 0x98, 0x9d, 0xd3, 0x5b, 0xc5, 0xff, 0x49, 0x96, 0x70, 0xda, 0x34, 0x25, 0x5b,
27 0x45, 0xb0, 0xcf, 0xd8, 0x30, 0xe8, 0x1f, 0x60, 0x5d, 0xcf, 0x7d, 0xc5, 0x54, 0x2e, 0x93,
28 0xae, 0x9c, 0xd7, 0x6f,
29 ];
30 assert_eq!(EXPECTED_HASH, hash(b"hello world").expect("hash failed"));
31}
32
33#[test]
34fn kdf_succeeds() {
35 let mut derived_key = [0u8; PRIVATE_KEY_SEED_SIZE];
36 kdf(b"myInitialKeyMaterial", b"mySalt", b"myInfo", &mut derived_key).unwrap();
37 const EXPECTED_DERIVED_KEY: [u8; PRIVATE_KEY_SEED_SIZE] = [
38 0x91, 0x9b, 0x8d, 0x29, 0xc4, 0x1b, 0x93, 0xd7, 0xeb, 0x09, 0xfa, 0xd7, 0xc9, 0x87, 0xb0,
39 0xd1, 0xcc, 0x26, 0xef, 0x07, 0x83, 0x42, 0xcf, 0xa3, 0x45, 0x0a, 0x57, 0xe9, 0x19, 0x86,
40 0xef, 0x48,
41 ];
42 assert_eq!(EXPECTED_DERIVED_KEY, derived_key);
Alice Wangceb66422023-02-07 13:55:33 +000043}
Alice Wang7cc59562023-02-08 13:17:10 +000044
45#[test]
46fn derive_cdi_certificate_id_succeeds() {
47 const EXPECTED_ID: [u8; ID_SIZE] = [
48 0x7a, 0x36, 0x45, 0x2c, 0x02, 0xf6, 0x2b, 0xec, 0xf9, 0x80, 0x06, 0x75, 0x87, 0xa5, 0xc1,
49 0x44, 0x0c, 0xd3, 0xc0, 0x6d,
50 ];
51 assert_eq!(EXPECTED_ID, derive_cdi_certificate_id(b"MyPubKey").unwrap());
52}