lakers_shared/
crypto.rs

1//! Cryptography trait back-end for the lakers-crypto crate
2
3use super::*;
4
5/// Returns the SUITES_I array, or an error if selected_suite is not supported.
6///
7/// The SUITES_I list will contain:
8/// - the selected suite at the last position
9/// - an ordered list of preferred suites in the first positions
10pub fn prepare_suites_i(
11    supported_suites: &EdhocBuffer<MAX_SUITES_LEN>,
12    selected_suite: u8,
13) -> Result<EdhocBuffer<MAX_SUITES_LEN>, EDHOCError> {
14    // TODO: implement a re-positioning algorithm, considering preferred and selected suites (see Section 5.2.2 of RFC 9528)
15    //       for now, we only support a single suite so we just return it
16    // NOTE: should we assume that supported_suites == preferred_suites?
17    if supported_suites.contains(&(selected_suite)) {
18        EdhocBuffer::<MAX_SUITES_LEN>::new_from_slice(&[selected_suite.into()])
19            .map_err(|_| EDHOCError::UnsupportedCipherSuite)
20    } else {
21        Err(EDHOCError::UnsupportedCipherSuite)
22    }
23}
24
25/// Interface between the lakers crate and any implementations of the required crypto primitives.
26///
27/// Sending cryptographic operations through a trait gives the library the flexibility to use
28/// hardware acceleration on microcontrollers, implementations that facilitate hacspec/hax
29/// verification, or software implementations.
30///
31/// The crypto trait itself operates on an exclusive reference, which is useful for the hardware
32/// implementations that can only perform a single operation at a time.
33///
34/// Many implementations will have a Default constructor or will be Clone (even Copy); either
35/// facilitates storing multiple EDHOC exchanges at a time. When neither is an option, the
36/// remaining options are to wrap a Crypto implementation into interior mutability using the
37/// platform's mutex, or to refactor the main initiator and responder objects into a form where the
38/// cryptography implementation can be taken out and stored separately.
39pub trait Crypto: core::fmt::Debug {
40    /// Returns the list of cryptographic suites supported by the backend implementation.
41    fn supported_suites(&self) -> EdhocBuffer<MAX_SUITES_LEN> {
42        EdhocBuffer::<MAX_SUITES_LEN>::new_from_slice(&[EDHOCSuite::CipherSuite2 as u8])
43            .expect("This should never fail, as the slice is of the correct length")
44    }
45    fn sha256_digest(&mut self, message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen;
46    fn hkdf_expand(
47        &mut self,
48        prk: &BytesHashLen,
49        info: &BytesMaxInfoBuffer,
50        info_len: usize,
51        length: usize,
52    ) -> BytesMaxBuffer;
53    fn hkdf_extract(&mut self, salt: &BytesHashLen, ikm: &BytesP256ElemLen) -> BytesHashLen;
54    fn aes_ccm_encrypt_tag_8(
55        &mut self,
56        key: &BytesCcmKeyLen,
57        iv: &BytesCcmIvLen,
58        ad: &[u8],
59        plaintext: &BufferPlaintext3,
60    ) -> BufferCiphertext3;
61    fn aes_ccm_decrypt_tag_8(
62        &mut self,
63        key: &BytesCcmKeyLen,
64        iv: &BytesCcmIvLen,
65        ad: &[u8],
66        ciphertext: &BufferCiphertext3,
67    ) -> Result<BufferPlaintext3, EDHOCError>;
68    fn p256_ecdh(
69        &mut self,
70        private_key: &BytesP256ElemLen,
71        public_key: &BytesP256ElemLen,
72    ) -> BytesP256ElemLen;
73    fn get_random_byte(&mut self) -> u8;
74    fn p256_generate_key_pair(&mut self) -> (BytesP256ElemLen, BytesP256ElemLen);
75}