Alice Wang | 000595b | 2023-10-02 13:46:45 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | use bssl_avf::{EcKey, Result}; |
Alice Wang | 9bd9809 | 2023-11-10 14:08:12 +0000 | [diff] [blame^] | 16 | use coset::CborSerializable; |
Alice Wang | 000595b | 2023-10-02 13:46:45 +0000 | [diff] [blame] | 17 | |
| 18 | #[test] |
| 19 | fn ec_private_key_serialization() -> Result<()> { |
Alice Wang | 9bd9809 | 2023-11-10 14:08:12 +0000 | [diff] [blame^] | 20 | let mut ec_key = EcKey::new_p256()?; |
| 21 | ec_key.generate_key()?; |
Alice Wang | 000595b | 2023-10-02 13:46:45 +0000 | [diff] [blame] | 22 | 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 Wang | 9bd9809 | 2023-11-10 14:08:12 +0000 | [diff] [blame^] | 28 | |
| 29 | #[test] |
| 30 | fn 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 | } |