xref: /aosp_15_r20/system/keymint/hal/src/sharedsecret.rs (revision 9860b7637a5f185913c70aa0caabe3ecb78441e4)
1 // Copyright 2022, 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 //! SharedSecret HAL device implementation.
16 
17 use crate::binder;
18 use crate::hal::{
19     sharedsecret::{ISharedSecret, SharedSecretParameters::SharedSecretParameters},
20     Innto,
21 };
22 use crate::{ChannelHalService, SerializedChannel};
23 use kmr_wire::*;
24 use std::sync::{Arc, Mutex, MutexGuard};
25 
26 /// `ISharedSecret` implementation which converts all method invocations to serialized requests that
27 /// are sent down the associated channel.
28 pub struct Device<T: SerializedChannel + 'static> {
29     channel: Arc<Mutex<T>>,
30 }
31 
32 impl<T: SerializedChannel + Send> binder::Interface for Device<T> {}
33 
34 impl<T: SerializedChannel + 'static> Device<T> {
35     /// Construct a new instance that uses the provided channel.
new(channel: Arc<Mutex<T>>) -> Self36     pub fn new(channel: Arc<Mutex<T>>) -> Self {
37         Self { channel }
38     }
39     /// Create a new instance wrapped in a proxy object.
new_as_binder( channel: Arc<Mutex<T>>, ) -> binder::Strong<dyn ISharedSecret::ISharedSecret>40     pub fn new_as_binder(
41         channel: Arc<Mutex<T>>,
42     ) -> binder::Strong<dyn ISharedSecret::ISharedSecret> {
43         ISharedSecret::BnSharedSecret::new_binder(
44             Self::new(channel),
45             binder::BinderFeatures::default(),
46         )
47     }
48 }
49 
50 impl<T: SerializedChannel> ChannelHalService<T> for Device<T> {
channel(&self) -> MutexGuard<T>51     fn channel(&self) -> MutexGuard<T> {
52         self.channel.lock().unwrap()
53     }
54 }
55 
56 impl<T: SerializedChannel> ISharedSecret::ISharedSecret for Device<T> {
getSharedSecretParameters(&self) -> binder::Result<SharedSecretParameters>57     fn getSharedSecretParameters(&self) -> binder::Result<SharedSecretParameters> {
58         let rsp: GetSharedSecretParametersResponse =
59             self.execute(GetSharedSecretParametersRequest {})?;
60         Ok(rsp.ret.innto())
61     }
computeSharedSecret(&self, params: &[SharedSecretParameters]) -> binder::Result<Vec<u8>>62     fn computeSharedSecret(&self, params: &[SharedSecretParameters]) -> binder::Result<Vec<u8>> {
63         let rsp: ComputeSharedSecretResponse =
64             self.execute(ComputeSharedSecretRequest { params: params.to_vec().innto() })?;
65         Ok(rsp.ret)
66     }
67 }
68