11#![ no_std]
22
3+ use lakers_shared:: CcmTagLen ;
34use lakers_shared:: {
45 BytesCcmIvLen , BytesCcmKeyLen , BytesHashLen , BytesP256ElemLen , Crypto as CryptoTrait ,
5- EDHOCError , EDHOCSuite , EdhocBuffer , AES_CCM_TAG_LEN , MAX_SUITES_LEN ,
6+ EDHOCError , EDHOCSuite , EdhocBuffer , MAX_SUITES_LEN ,
67} ;
78
89use ccm:: AeadInPlace ;
@@ -12,6 +13,7 @@ use p256::elliptic_curve::point::DecompressPoint;
1213use sha2:: Digest ;
1314
1415type AesCcm16_64_128 = ccm:: Ccm < aes:: Aes128 , ccm:: consts:: U8 , ccm:: consts:: U13 > ;
16+ type AesCcm16_128_128 = ccm:: Ccm < aes:: Aes128 , ccm:: consts:: U16 , ccm:: consts:: U13 > ;
1517
1618/// A type representing cryptographic operations through various RustCrypto crates (eg. [aes],
1719/// [ccm], [p256]).
@@ -72,51 +74,81 @@ impl<Rng: rand_core::RngCore + rand_core::CryptoRng> CryptoTrait for Crypto<Rng>
7274 extracted. finalize ( ) . 0 . into ( )
7375 }
7476
75- fn aes_ccm_encrypt_tag_8 < const N : usize > (
77+ fn aes_ccm_encrypt < const N : usize , Tag : CcmTagLen > (
7678 & mut self ,
7779 key : & BytesCcmKeyLen ,
7880 iv : & BytesCcmIvLen ,
7981 ad : & [ u8 ] ,
8082 plaintext : & [ u8 ] ,
8183 ) -> EdhocBuffer < N > {
82- let key = AesCcm16_64_128 :: new ( key. into ( ) ) ;
8384 let mut outbuffer = EdhocBuffer :: new_from_slice ( plaintext) . unwrap ( ) ;
8485 #[ allow(
8586 deprecated,
8687 reason = "hax won't allow creating a .as_mut_slice() method"
8788 ) ]
88- if let Ok ( tag) =
89- key. encrypt_in_place_detached ( iv. into ( ) , ad, & mut outbuffer. content [ ..plaintext. len ( ) ] )
90- {
91- outbuffer. extend_from_slice ( & tag) . unwrap ( ) ;
92- } else {
93- panic ! ( "Preconfigured sizes should not allow encryption to fail" )
94- }
89+ match Tag :: LEN {
90+ 8 => {
91+ let enc = AesCcm16_64_128 :: new ( key. into ( ) )
92+ . encrypt_in_place_detached (
93+ iv. into ( ) ,
94+ ad,
95+ & mut outbuffer. content [ ..plaintext. len ( ) ] ,
96+ )
97+ . expect ( "Preconfigured sizes should not allow encryption to fail" ) ;
98+
99+ outbuffer. extend_from_slice ( & enc) . unwrap ( )
100+ }
101+
102+ 16 => {
103+ let enc = AesCcm16_128_128 :: new ( key. into ( ) )
104+ . encrypt_in_place_detached (
105+ iv. into ( ) ,
106+ ad,
107+ & mut outbuffer. content [ ..plaintext. len ( ) ] ,
108+ )
109+ . expect ( "Preconfigured sizes should not allow encryption to fail" ) ;
110+
111+ outbuffer. extend_from_slice ( & enc) . unwrap ( )
112+ }
113+
114+ _ => unreachable ! ( ) , // CcmTagLen bound guarantees this
115+ } ;
95116 outbuffer
96117 }
97118
98- fn aes_ccm_decrypt_tag_8 < const N : usize > (
119+ fn aes_ccm_decrypt < const N : usize , Tag : CcmTagLen > (
99120 & mut self ,
100121 key : & BytesCcmKeyLen ,
101122 iv : & BytesCcmIvLen ,
102123 ad : & [ u8 ] ,
103124 ciphertext : & [ u8 ] ,
104125 ) -> Result < EdhocBuffer < N > , EDHOCError > {
105- let key = AesCcm16_64_128 :: new ( key. into ( ) ) ;
106- let plaintext_len = ciphertext. len ( ) - AES_CCM_TAG_LEN ;
126+ let plaintext_len = ciphertext. len ( ) - Tag :: LEN ;
107127 let mut buffer = EdhocBuffer :: new_from_slice ( & ciphertext[ ..plaintext_len] ) . unwrap ( ) ;
108128 let tag = & ciphertext[ plaintext_len..] ;
109129 #[ allow(
110130 deprecated,
111131 reason = "hax won't allow creating a .as_mut_slice() method"
112132 ) ]
113- key. decrypt_in_place_detached (
114- iv. into ( ) ,
115- ad,
116- & mut buffer. content [ ..plaintext_len] ,
117- tag. into ( ) ,
118- )
119- . map_err ( |_| EDHOCError :: MacVerificationFailed ) ?;
133+ match Tag :: LEN {
134+ 8 => AesCcm16_64_128 :: new ( key. into ( ) )
135+ . decrypt_in_place_detached (
136+ iv. into ( ) ,
137+ ad,
138+ & mut buffer. content [ ..plaintext_len] ,
139+ tag. into ( ) ,
140+ )
141+ . map_err ( |_| EDHOCError :: MacVerificationFailed ) ?,
142+ 16 => AesCcm16_128_128 :: new ( key. into ( ) )
143+ . decrypt_in_place_detached (
144+ iv. into ( ) ,
145+ ad,
146+ & mut buffer. content [ ..plaintext_len] ,
147+ tag. into ( ) ,
148+ )
149+ . map_err ( |_| EDHOCError :: MacVerificationFailed ) ?,
150+ _ => unreachable ! ( ) , // CcmTagLen bound guarantees this
151+ } ;
120152 Ok ( buffer)
121153 }
122154
@@ -153,3 +185,23 @@ impl<Rng: rand_core::RngCore + rand_core::CryptoRng> CryptoTrait for Crypto<Rng>
153185 ( private_key. into ( ) , public_key. into ( ) )
154186 }
155187}
188+
189+ #[ cfg( test) ]
190+ mod tests {
191+ use lakers_shared:: test_helper:: {
192+ test_aes_ccm_roundtrip, test_aes_ccm_tag_16, test_aes_ccm_tag_8,
193+ } ;
194+ use lakers_shared:: { CcmTagLen16 , CcmTagLen8 } ;
195+
196+ use super :: * ;
197+
198+ #[ test]
199+ fn test_rustcrypto_aes_ccm ( ) {
200+ let mut crypto = Crypto :: new ( rand_core:: OsRng ) ;
201+ test_aes_ccm_roundtrip :: < Crypto < rand_core:: OsRng > , CcmTagLen8 > ( & mut crypto) ;
202+ test_aes_ccm_roundtrip :: < Crypto < rand_core:: OsRng > , CcmTagLen16 > ( & mut crypto) ;
203+
204+ test_aes_ccm_tag_8 :: < Crypto < rand_core:: OsRng > > ( & mut crypto) ;
205+ test_aes_ccm_tag_16 :: < Crypto < rand_core:: OsRng > > ( & mut crypto) ;
206+ }
207+ }
0 commit comments