blob: f4c1da923d85da99584710a974779347550ccb25 [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;
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000027use authgraph_core::{error::Error as AgError, keyexchange as ke};
David Drysdalefe418252023-11-07 09:27:56 +000028use coset::CborSerializable;
David Drysdale8898d2e2023-11-07 15:20:15 +000029use std::{cell::RefCell, rc::Rc};
David Drysdalefe418252023-11-07 09:27:56 +000030
31pub mod sink;
32pub mod source;
33
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000034/// Return an AuthGraphParticipant suitable for testing.
35pub fn test_ag_participant() -> Result<ke::AuthGraphParticipant, AgError> {
36 Ok(ke::AuthGraphParticipant::new(
37 boring::crypto_trait_impls(),
David Drysdale8898d2e2023-11-07 15:20:15 +000038 Rc::new(RefCell::new(boring::test_device::AgDevice::default())),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000039 ke::MAX_OPENED_SESSIONS,
40 )?)
David Drysdalefe418252023-11-07 09:27:56 +000041}
42
43fn build_plain_pub_key(pub_key: &Option<Vec<u8>>) -> PubKey {
44 PubKey::PlainKey(PlainPubKey {
45 plainPubKey: pub_key.clone().unwrap(),
46 })
47}
48
49fn extract_plain_pub_key(pub_key: &Option<PubKey>) -> &PlainPubKey {
50 match pub_key {
51 Some(PubKey::PlainKey(pub_key)) => pub_key,
52 Some(PubKey::SignedKey(_)) => panic!("expect unsigned public key"),
53 None => panic!("expect pubKey to be populated"),
54 }
55}
56
David Drysdalefe418252023-11-07 09:27:56 +000057fn vec_to_identity(data: &[u8]) -> Identity {
58 Identity {
59 identity: data.to_vec(),
60 }
61}
62
63fn vec_to_signature(data: &[u8]) -> SessionIdSignature {
64 SessionIdSignature {
65 signature: data.to_vec(),
66 }
67}