@@ -5,6 +5,7 @@ mod store;
55use store:: OpenSeekFrom ;
66
77use chacha20poly1305:: {
8+ aead,
89 aead:: stream:: { DecryptorLE31 , EncryptorLE31 , Nonce as StreamNonce , StreamLE31 } ,
910 ChaCha8Poly1305 , KeyInit ,
1011} ;
@@ -27,6 +28,38 @@ use crate::StagingContext;
2728const POLY1305_TAG_LEN : usize = 16 ;
2829const CHACHA8_KEY_LEN : usize = 32 ;
2930
31+ struct HeaplessBuffer < ' a , LenT : heapless:: LenType > ( & ' a mut heapless_bytes:: BytesView < LenT > ) ;
32+
33+ impl < ' a , LenT : heapless:: LenType , S : heapless_bytes:: BytesStorage + ?Sized >
34+ From < & ' a mut heapless_bytes:: BytesInner < LenT , S > > for HeaplessBuffer < ' a , LenT >
35+ {
36+ fn from ( value : & ' a mut heapless_bytes:: BytesInner < LenT , S > ) -> Self {
37+ Self ( value. as_mut_view ( ) )
38+ }
39+ }
40+
41+ impl < ' a , LenT : heapless:: LenType > AsMut < [ u8 ] > for HeaplessBuffer < ' a , LenT > {
42+ fn as_mut ( & mut self ) -> & mut [ u8 ] {
43+ & mut self . 0
44+ }
45+ }
46+
47+ impl < ' a , LenT : heapless:: LenType > AsRef < [ u8 ] > for HeaplessBuffer < ' a , LenT > {
48+ fn as_ref ( & self ) -> & [ u8 ] {
49+ & self . 0
50+ }
51+ }
52+
53+ impl < ' a , LenT : heapless:: LenType > aead:: Buffer for HeaplessBuffer < ' a , LenT > {
54+ fn extend_from_slice ( & mut self , other : & [ u8 ] ) -> aead:: Result < ( ) > {
55+ self . 0 . extend_from_slice ( other) . map_err ( |_| aead:: Error )
56+ }
57+
58+ fn truncate ( & mut self , len : usize ) {
59+ self . 0 . truncate ( len) ;
60+ }
61+ }
62+
3063#[ derive( Debug ) ]
3164pub struct ChunkedReadState {
3265 pub path : PathBuf ,
@@ -209,7 +242,7 @@ impl ExtensionImpl<ChunkedExtension> for super::StagingBackend {
209242 let nonce: Bytes < CHACHA8_STREAM_NONCE_LEN > =
210243 filestore. read ( & request. path , request. location ) ?;
211244 let nonce: & StreamNonce < ChaCha8Poly1305 , StreamLE31 < ChaCha8Poly1305 > > =
212- ( & * * nonce) . into ( ) ;
245+ ( & * nonce) . into ( ) ;
213246 let aead = ChaCha8Poly1305 :: new ( ( & * key. material ) . into ( ) ) ;
214247 let decryptor = DecryptorLE31 :: < ChaCha8Poly1305 > :: from_aead ( aead, nonce) ;
215248 backend_ctx. chunked_io_state =
@@ -263,10 +296,13 @@ fn write_chunk(
263296 }
264297 Some ( ChunkedIoState :: EncryptedWrite ( ref mut write_state) ) => {
265298 let mut data =
266- Bytes :: < { MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN } > :: from_slice ( data) . unwrap ( ) ;
299+ Bytes :: < { MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN } > :: try_from ( & * * data) . unwrap ( ) ;
267300 write_state
268301 . encryptor
269- . encrypt_next_in_place ( write_state. path . as_ref ( ) . as_bytes ( ) , & mut * data)
302+ . encrypt_next_in_place (
303+ write_state. path . as_ref ( ) . as_bytes ( ) ,
304+ & mut HeaplessBuffer :: from ( & mut data) ,
305+ )
270306 . map_err ( |_err| {
271307 error ! ( "Failed to encrypt {:?}" , _err) ;
272308 Error :: AeadError
@@ -303,10 +339,13 @@ fn write_last_chunk(
303339 }
304340 Some ( ChunkedIoState :: EncryptedWrite ( write_state) ) => {
305341 let mut data =
306- Bytes :: < { MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN } > :: from_slice ( data) . unwrap ( ) ;
342+ Bytes :: < { MAX_MESSAGE_LENGTH + POLY1305_TAG_LEN } > :: try_from ( & * * data) . unwrap ( ) ;
307343 write_state
308344 . encryptor
309- . encrypt_last_in_place ( & [ write_state. location as u8 ] , & mut * data)
345+ . encrypt_last_in_place (
346+ & [ write_state. location as u8 ] ,
347+ & mut HeaplessBuffer :: from ( & mut data) ,
348+ )
310349 . map_err ( |_err| {
311350 error ! ( "Failed to encrypt {:?}" , _err) ;
312351 Error :: AeadError
@@ -354,12 +393,15 @@ fn read_encrypted_chunk(
354393
355394 read_state
356395 . decryptor
357- . decrypt_last_in_place ( & [ read_state. location as u8 ] , & mut * data)
396+ . decrypt_last_in_place (
397+ & [ read_state. location as u8 ] ,
398+ & mut HeaplessBuffer :: from ( & mut data) ,
399+ )
358400 . map_err ( |_err| {
359401 error ! ( "Failed to decrypt {:?}" , _err) ;
360402 Error :: AeadError
361403 } ) ?;
362- let data = Bytes :: from_slice ( & data) . expect ( "decryptor removes the tag" ) ;
404+ let data = Bytes :: try_from ( & * data) . expect ( "decryptor removes the tag" ) ;
363405 Ok ( reply:: ReadChunk {
364406 data,
365407 len : chunked_decrypted_len ( len) ?,
@@ -368,12 +410,15 @@ fn read_encrypted_chunk(
368410 } else {
369411 read_state
370412 . decryptor
371- . decrypt_next_in_place ( read_state. path . as_ref ( ) . as_bytes ( ) , & mut * data)
413+ . decrypt_next_in_place (
414+ read_state. path . as_ref ( ) . as_bytes ( ) ,
415+ & mut HeaplessBuffer :: from ( & mut data) ,
416+ )
372417 . map_err ( |_err| {
373418 error ! ( "Failed to decrypt {:?}" , _err) ;
374419 Error :: AeadError
375420 } ) ?;
376- let data = Bytes :: from_slice ( & data) . expect ( "decryptor removes the tag" ) ;
421+ let data = Bytes :: try_from ( & * data) . expect ( "decryptor removes the tag" ) ;
377422 Ok ( reply:: ReadChunk {
378423 data,
379424 len : chunked_decrypted_len ( len) ?,
0 commit comments