/* * * Copyright (c) 2020-2022 Project CHIP Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use crate::{ error::{Error, ErrorCode}, tlv::{FromTLV, TLVWriter, TagType, ToTLV}, }; pub const SYMM_KEY_LEN_BITS: usize = 128; pub const SYMM_KEY_LEN_BYTES: usize = SYMM_KEY_LEN_BITS / 8; pub const AEAD_MIC_LEN_BITS: usize = 128; pub const AEAD_MIC_LEN_BYTES: usize = AEAD_MIC_LEN_BITS / 8; pub const AEAD_NONCE_LEN_BYTES: usize = 13; pub const AEAD_AAD_LEN_BYTES: usize = 8; pub const SHA256_HASH_LEN_BYTES: usize = 256 / 8; pub const BIGNUM_LEN_BYTES: usize = 32; pub const EC_POINT_LEN_BYTES: usize = 65; pub const ECDH_SHARED_SECRET_LEN_BYTES: usize = 32; pub const EC_SIGNATURE_LEN_BYTES: usize = 64; #[cfg(all(feature = "mbedtls", target_os = "espidf"))] mod crypto_esp_mbedtls; #[cfg(all(feature = "mbedtls", target_os = "espidf"))] pub use self::crypto_esp_mbedtls::*; #[cfg(all(feature = "mbedtls", not(target_os = "espidf")))] mod crypto_mbedtls; #[cfg(all(feature = "mbedtls", not(target_os = "espidf")))] pub use self::crypto_mbedtls::*; #[cfg(feature = "openssl")] mod crypto_openssl; #[cfg(feature = "openssl")] pub use self::crypto_openssl::*; #[cfg(feature = "rustcrypto")] mod crypto_rustcrypto; #[cfg(feature = "rustcrypto")] pub use self::crypto_rustcrypto::*; #[cfg(not(any(feature = "openssl", feature = "mbedtls", feature = "rustcrypto")))] pub mod crypto_dummy; #[cfg(not(any(feature = "openssl", feature = "mbedtls", feature = "rustcrypto")))] pub use self::crypto_dummy::*; impl<'a> FromTLV<'a> for KeyPair { fn from_tlv(t: &crate::tlv::TLVElement<'a>) -> Result where Self: Sized, { t.confirm_array()?.enter(); if let Some(mut array) = t.enter() { let pub_key = array.next().ok_or(ErrorCode::Invalid)?.slice()?; let priv_key = array.next().ok_or(ErrorCode::Invalid)?.slice()?; KeyPair::new_from_components(pub_key, priv_key) } else { Err(ErrorCode::Invalid.into()) } } } impl ToTLV for KeyPair { fn to_tlv(&self, tw: &mut TLVWriter, tag: TagType) -> Result<(), Error> { let mut buf = [0; 1024]; // TODO tw.start_array(tag)?; let size = self.get_public_key(&mut buf)?; tw.str16(TagType::Anonymous, &buf[..size])?; let size = self.get_private_key(&mut buf)?; tw.str16(TagType::Anonymous, &buf[..size])?; tw.end_container() } } #[cfg(test)] mod tests { use crate::error::ErrorCode; use super::KeyPair; #[test] fn test_verify_msg_success() { let key = KeyPair::new_from_public(&test_vectors::PUB_KEY1).unwrap(); key.verify_msg(&test_vectors::MSG1_SUCCESS, &test_vectors::SIGNATURE1) .unwrap(); } #[test] fn test_verify_msg_fail() { let key = KeyPair::new_from_public(&test_vectors::PUB_KEY1).unwrap(); assert_eq!( key.verify_msg(&test_vectors::MSG1_FAIL, &test_vectors::SIGNATURE1) .map_err(|e| e.code()), Err(ErrorCode::InvalidSignature) ); } mod test_vectors { pub const PUB_KEY1: [u8; 65] = [ 0x4, 0x56, 0x19, 0x77, 0x18, 0x3f, 0xd4, 0xff, 0x2b, 0x58, 0x3d, 0xe9, 0x79, 0x34, 0x66, 0xdf, 0xe9, 0x0, 0xfb, 0x6d, 0xa1, 0xef, 0xe0, 0xcc, 0xdc, 0x77, 0x30, 0xc0, 0x6f, 0xb6, 0x2d, 0xff, 0xbe, 0x54, 0xa0, 0x95, 0x75, 0xb, 0x8b, 0x7, 0xbc, 0x55, 0xdb, 0x9c, 0xb6, 0x55, 0x13, 0x8, 0xb8, 0xdf, 0x2, 0xe3, 0x40, 0x6b, 0xae, 0x34, 0xf5, 0xc, 0xba, 0xc9, 0xf2, 0xbf, 0xf1, 0xe7, 0x50, ]; pub const MSG1_SUCCESS: [u8; 421] = [ 0x30, 0x82, 0x1, 0xa1, 0xa0, 0x3, 0x2, 0x1, 0x2, 0x2, 0x1, 0x1, 0x30, 0xa, 0x6, 0x8, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x4, 0x3, 0x2, 0x30, 0x44, 0x31, 0x20, 0x30, 0x1e, 0x6, 0xa, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xa2, 0x7c, 0x1, 0x3, 0xc, 0x10, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x20, 0x30, 0x1e, 0x6, 0xa, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xa2, 0x7c, 0x1, 0x5, 0xc, 0x10, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x1e, 0x17, 0xd, 0x32, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0xd, 0x33, 0x30, 0x31, 0x32, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x44, 0x31, 0x20, 0x30, 0x1e, 0x6, 0xa, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xa2, 0x7c, 0x1, 0x1, 0xc, 0x10, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x42, 0x43, 0x35, 0x43, 0x30, 0x32, 0x31, 0x20, 0x30, 0x1e, 0x6, 0xa, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xa2, 0x7c, 0x1, 0x5, 0xc, 0x10, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,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0x56, 0xeb, 0xc2, 0xdc, 0x30, 0x1f, 0x6, 0x3, 0x55, 0x1d, 0x23, 0x4, 0x18, 0x30, 0x16, 0x80, 0x14, 0xce, 0x60, 0xb4, 0x28, 0x96, 0x72, 0x27, 0x64, 0x81, 0xbc, 0x4f, 0x0, 0x78, 0xa3, 0x30, 0x48, 0xfe, 0x6e, 0x65, 0x86, ]; pub const MSG1_FAIL: [u8; 421] = [ 0x30, 0x82, 0x1, 0xa1, 0xa0, 0x3, 0x2, 0x1, 0x2, 0x2, 0x1, 0x1, 0x30, 0xa, 0x6, 0x8, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x4, 0x3, 0x2, 0x30, 0x44, 0x31, 0x20, 0x30, 0x1e, 0x6, 0xa, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xa2, 0x7c, 0x1, 0x3, 0xc, 0x10, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x20, 0x30, 0x1e, 0x6, 0xa, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xa2, 0x7c, 0x1, 0x5, 0xc, 0x10, 0x30, 0x30, 0x30, 0x31, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x1e, 0x17, 0xd, 0x32, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0xd, 0x33, 0x30, 0x31, 0x32, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x44, 0x31, 0x20, 0x30, 0x1e, 0x6, 0xa, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xa2, 0x7c, 0x1, 0x1, 0xc, 0x10, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x42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