blob: da176aefcaf310e056da2e217e0f4f1f181d3f9f [file] [log] [blame]
Alice Wang000595b2023-10-02 13:46:45 +00001// Copyright 2023, 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
15use bssl_avf::{EcKey, Result};
Alice Wang9bd98092023-11-10 14:08:12 +000016use coset::CborSerializable;
Alice Wang000595b2023-10-02 13:46:45 +000017
18#[test]
19fn ec_private_key_serialization() -> Result<()> {
Alice Wang9bd98092023-11-10 14:08:12 +000020 let mut ec_key = EcKey::new_p256()?;
21 ec_key.generate_key()?;
Alice Wang000595b2023-10-02 13:46:45 +000022 let der_encoded_ec_private_key = ec_key.ec_private_key()?;
23 let deserialized_ec_key = EcKey::from_ec_private_key(der_encoded_ec_private_key.as_slice())?;
24
25 assert_eq!(ec_key.cose_public_key()?, deserialized_ec_key.cose_public_key()?);
26 Ok(())
27}
Alice Wang9bd98092023-11-10 14:08:12 +000028
29#[test]
30fn cose_public_key_serialization() -> Result<()> {
31 let mut ec_key = EcKey::new_p256()?;
32 ec_key.generate_key()?;
33 let cose_key = ec_key.cose_public_key()?;
34 let cose_key_data = cose_key.clone().to_vec().unwrap();
35 let deserialized_ec_key = EcKey::from_cose_public_key(&cose_key_data)?;
36
37 assert_eq!(cose_key, deserialized_ec_key.cose_public_key()?);
38 Ok(())
39}