blob: 7b9b2b98be7f12dfb8ce70d6da9b682bad8f6a2f [file] [log] [blame]
David Drysdalefe418252023-11-07 09:27:56 +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
17//! VTS test library for AuthGraph functionality.
18//!
19//! This test code is bundled as a library, not as `[cfg(test)]`, to allow it to be
20//! re-used inside the (Rust) VTS tests of components that use AuthGraph.
21
22use android_hardware_security_authgraph::aidl::android::hardware::security::authgraph::{
23 Error::Error, IAuthGraphKeyExchange::IAuthGraphKeyExchange, Identity::Identity,
24 PlainPubKey::PlainPubKey, PubKey::PubKey, SessionIdSignature::SessionIdSignature,
25};
26use authgraph_boringssl as boring;
27use authgraph_core::keyexchange as ke;
28use authgraph_core::{arc, key, traits};
29use authgraph_nonsecure::StdClock;
30use coset::CborSerializable;
31
32pub mod sink;
33pub mod source;
34
35/// Return a collection of AuthGraph trait implementations suitable for testing.
36pub fn test_impls() -> traits::TraitImpl {
37 // Note that the local implementation is using a clock with a potentially different epoch than
38 // the implementation under test.
39 boring::trait_impls(
40 Box::<boring::test_device::AgDevice>::default(),
41 Some(Box::new(StdClock::default())),
42 )
43}
44
45fn build_plain_pub_key(pub_key: &Option<Vec<u8>>) -> PubKey {
46 PubKey::PlainKey(PlainPubKey {
47 plainPubKey: pub_key.clone().unwrap(),
48 })
49}
50
51fn extract_plain_pub_key(pub_key: &Option<PubKey>) -> &PlainPubKey {
52 match pub_key {
53 Some(PubKey::PlainKey(pub_key)) => pub_key,
54 Some(PubKey::SignedKey(_)) => panic!("expect unsigned public key"),
55 None => panic!("expect pubKey to be populated"),
56 }
57}
58
59fn verification_key_from_identity(impls: &traits::TraitImpl, identity: &[u8]) -> key::EcVerifyKey {
60 let identity = key::Identity::from_slice(identity).expect("invalid identity CBOR");
61 impls
62 .device
63 .process_peer_cert_chain(&identity.cert_chain, &*impls.ecdsa)
64 .expect("failed to extract signing key")
65}
66
67fn vec_to_identity(data: &[u8]) -> Identity {
68 Identity {
69 identity: data.to_vec(),
70 }
71}
72
73fn vec_to_signature(data: &[u8]) -> SessionIdSignature {
74 SessionIdSignature {
75 signature: data.to_vec(),
76 }
77}
78
79/// Decrypt a pair of AES-256 keys encrypted with the AuthGraph PBK.
80pub fn decipher_aes_keys(imp: &traits::TraitImpl, arc: &[Vec<u8>; 2]) -> [key::AesKey; 2] {
81 [
82 decipher_aes_key(imp, &arc[0]),
83 decipher_aes_key(imp, &arc[1]),
84 ]
85}
86
87/// Decrypt an AES-256 key encrypted with the AuthGraph PBK.
88pub fn decipher_aes_key(imp: &traits::TraitImpl, arc: &[u8]) -> key::AesKey {
89 let pbk = imp.device.get_per_boot_key().expect("no PBK available");
90 let arc::ArcContent {
91 payload,
92 protected_headers: _,
93 unprotected_headers: _,
94 } = arc::decipher_arc(&pbk, arc, &*imp.aes_gcm).expect("failed to decrypt arc");
95 assert_eq!(payload.0.len(), 32);
96 let mut key = key::AesKey([0; 32]);
97 key.0.copy_from_slice(&payload.0);
98 assert_ne!(key.0, [0; 32], "agreed AES-256 key should be non-zero");
99 key
100}