1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::{
    medium_encryptor::MediumEncryptor, small_encryptor::SmallEncryptor, utils, SelfEncryptionError,
    Storage, MAX_CHUNK_SIZE, MIN_CHUNK_SIZE,
};
use crate::data_map::{ChunkDetails, DataMap};
use std::{cmp, convert::From, mem};
use unwrap::unwrap;
pub const MIN: u64 = 3 * MAX_CHUNK_SIZE as u64 + 1;
const MAX_BUFFER_LEN: usize = (MAX_CHUNK_SIZE + MIN_CHUNK_SIZE) as usize;

// An encryptor for data which will be split into more three chunks.  Calls to `write()` will
// trigger the creation and storing of any completed chunks up to that point except for the first
// two and last two chunks.  These will always be dealt with in `close()` since they may always be
// affected subsequent `write()` calls.
pub struct LargeEncryptor<S: Storage + Send + Sync> {
    storage: S,
    chunks: Vec<ChunkDetails>,
    original_chunks: Option<Vec<ChunkDetails>>,
    chunk_0_data: Vec<u8>,
    chunk_1_data: Vec<u8>,
    buffer: Vec<u8>,
}

impl<S> LargeEncryptor<S>
where
    S: Storage + 'static + Send + Sync,
{
    // Constructor for use with pre-existing `DataMap::Chunks` where there are more than three
    // chunks.  Retrieves the first two and and last two chunks from storage and decrypts them to
    // its internal buffers.
    #[allow(clippy::new_ret_no_self)]
    pub async fn new(
        storage: S,
        chunks: Vec<ChunkDetails>,
    ) -> Result<LargeEncryptor<S>, SelfEncryptionError<S::Error>> {
        debug_assert!(chunks.len() > 3);
        debug_assert!(MIN <= chunks.iter().fold(0, |acc, chunk| acc + chunk.source_size));
        let mut partial_details = chunks.clone();
        let mut truncated_details_len = chunks.len() - 1;

        let mut chunk_0_data;
        let mut chunk_1_data;
        let mut buffer;
        let buffer_extension;

        {
            // Decrypt first two chunks
            let mut start_iter = partial_details.iter_mut().enumerate();
            let (index, chunk) = unwrap!(start_iter.next());
            let pad_key_iv = utils::get_pad_key_and_iv(index, &chunks);

            chunk_0_data = storage.get(&chunk.hash).await?;
            chunk_0_data = utils::decrypt_chunk(&chunk_0_data, pad_key_iv)?;
            chunk.hash.clear();

            let (index, chunk) = unwrap!(start_iter.next());
            let pad_key_iv = utils::get_pad_key_and_iv(index, &chunks);
            chunk_1_data = storage.get(&chunk.hash).await?;
            chunk_1_data = utils::decrypt_chunk(&chunk_1_data, pad_key_iv)?;
            chunk.hash.clear();

            // If the penultimate chunk is not at MAX_CHUNK_SIZE, decrypt it to `buffer`
            let mut end_iter = start_iter.skip(chunks.len() - 4);
            let (index, chunk) = unwrap!(end_iter.next());
            buffer = if chunk.source_size < MAX_CHUNK_SIZE as u64 {
                let pad_key_iv = utils::get_pad_key_and_iv(index, &chunks);
                truncated_details_len -= 1;
                let another_chunk_data = storage.get(&chunk.hash).await?;

                utils::decrypt_chunk(&another_chunk_data, pad_key_iv)?
            } else {
                Vec::with_capacity(MAX_BUFFER_LEN)
            };

            // Decrypt the last chunk to `buffer`
            let (index, chunk) = unwrap!(end_iter.next());
            let pad_key_iv = utils::get_pad_key_and_iv(index, &chunks);
            let data = storage.get(&chunk.hash).await?;

            buffer_extension = utils::decrypt_chunk(&data, pad_key_iv)?
        }

        // Remove the last one or two chunks' details since they're now in `buffer`
        partial_details.truncate(truncated_details_len);
        buffer.extend(buffer_extension);

        Ok(LargeEncryptor {
            storage,
            chunks: partial_details,
            original_chunks: Some(chunks),
            chunk_0_data,
            chunk_1_data,
            buffer,
        })
    }

    pub async fn from_medium(
        medium_encryptor: MediumEncryptor<S>,
    ) -> Result<Self, SelfEncryptionError<S::Error>> {
        let encryptor = LargeEncryptor {
            storage: medium_encryptor.storage,
            chunks: vec![],
            original_chunks: None,
            chunk_0_data: vec![],
            chunk_1_data: vec![],
            buffer: vec![],
        };

        encryptor.write(&medium_encryptor.buffer).await
    }

    // Stores any chunks which cannot be modified by subsequent `write()` calls and buffers the
    // remainder.  Chunks which cannot be stored yet include the first two chunks and the final
    // chunk.  If the final chunk is smaller than `MIN_CHUNK_SIZE` then the penultimate chunk
    // cannot be stored either.
    pub async fn write(mut self, mut data: &[u8]) -> Result<Self, SelfEncryptionError<S::Error>> {
        self.original_chunks = None;

        // Try filling `chunk_0_data` and `chunk_1_data` buffers first.
        data = self.fill_chunk_buffer(data, 0).await;
        data = self.fill_chunk_buffer(data, 1).await;

        let mut all_things = Vec::new();

        while !data.is_empty() {
            let amount = cmp::min(MAX_BUFFER_LEN - self.buffer.len(), data.len());
            // TODO - avoid copying _all_ of `data` to `self_buffer` where full chunks can be
            // encrypted.
            self.buffer.extend_from_slice(&data[..amount]);
            data = &data[amount..];
            // If the buffer's full, encrypt and remove the first `MAX_CHUNK_SIZE` of it.
            if self.buffer.len() == MAX_BUFFER_LEN {
                let mut data_to_encrypt = self.buffer.split_off(MAX_CHUNK_SIZE as usize);
                mem::swap(&mut self.buffer, &mut data_to_encrypt);
                let index = self.chunks.len();
                all_things.push(self.encrypt_chunk(&data_to_encrypt, index).await?);
            }
        }

        Ok(self)
    }

    // This finalises the encryptor - it should not be used again after this call.  Either three or
    // four chunks will be generated and stored by calling this; chunks 0 and 1, the last chunk, and
    // possibly the penultimate chunk if the last chunk would otherwise be too small.  The only
    // exception is where the encryptor didn't receive any `write()` calls, in which case no chunks
    // are stored.
    pub async fn close(mut self) -> Result<(DataMap, S), SelfEncryptionError<S::Error>> {
        if let Some(chunks) = self.original_chunks {
            return Ok((DataMap::Chunks(chunks), self.storage));
        }

        // Handle encrypting and storing the contents of `self.buffer`.
        debug_assert!(self.buffer.len() >= MIN_CHUNK_SIZE as usize);
        debug_assert!(self.buffer.len() <= MAX_BUFFER_LEN);
        let (first_len, need_two_chunks) = if self.buffer.len() <= MAX_CHUNK_SIZE as usize {
            (self.buffer.len(), false)
        } else {
            ((MAX_CHUNK_SIZE - MIN_CHUNK_SIZE) as usize, true)
        };
        let mut index = self.chunks.len();
        let mut swapped_buffer = vec![];

        let mut all_chunks = Vec::with_capacity(4);

        mem::swap(&mut swapped_buffer, &mut self.buffer);
        all_chunks.push(
            self.encrypt_chunk(&swapped_buffer[..first_len], index)
                .await?,
        );
        if need_two_chunks {
            index += 1;
            all_chunks.push(
                self.encrypt_chunk(&swapped_buffer[first_len..], index)
                    .await?,
            );
        }

        // Handle encrypting and storing the contents of the first two chunks' buffers.
        mem::swap(&mut swapped_buffer, &mut self.chunk_0_data);
        all_chunks.push(self.encrypt_chunk(&swapped_buffer, 0).await?);
        mem::swap(&mut swapped_buffer, &mut self.chunk_1_data);
        all_chunks.push(self.encrypt_chunk(&swapped_buffer, 1).await?);

        let mut swapped_chunks = vec![];
        mem::swap(&mut swapped_chunks, &mut self.chunks);

        Ok((DataMap::Chunks(swapped_chunks), self.storage))
    }

    pub fn len(&self) -> u64 {
        self.chunk_0_data.len() as u64
            + self.chunk_1_data.len() as u64
            + self.buffer.len() as u64
            + ((self.chunks.len().saturating_sub(2)) * MAX_CHUNK_SIZE as usize) as u64
    }

    pub fn is_empty(&self) -> bool {
        self.chunk_0_data.is_empty()
    }

    #[allow(clippy::needless_lifetimes)]
    async fn fill_chunk_buffer<'b>(&mut self, mut data: &'b [u8], index: u32) -> &'b [u8] {
        let buffer_ref = if index == 0 {
            &mut self.chunk_0_data
        } else {
            &mut self.chunk_1_data
        };
        let amount = cmp::min(MAX_CHUNK_SIZE as usize - buffer_ref.len(), data.len());
        if amount > 0 {
            buffer_ref.extend_from_slice(&data[..amount]);
            data = &data[amount..];
            // If the buffer's full, update `chunks` with the pre-encryption hash and size.
            if buffer_ref.len() == MAX_CHUNK_SIZE as usize {
                self.chunks.push(ChunkDetails {
                    chunk_num: index,
                    hash: vec![],
                    pre_hash: self.storage.generate_address(buffer_ref).await,
                    source_size: MAX_CHUNK_SIZE as u64,
                });
            }
        }
        data
    }

    async fn encrypt_chunk(
        &mut self,
        data: &[u8],
        index: usize,
    ) -> Result<(), SelfEncryptionError<S::Error>> {
        if index > 1 {
            self.chunks.push(ChunkDetails {
                chunk_num: index as u32,
                hash: vec![],
                pre_hash: self.storage.generate_address(data).await,
                source_size: data.len() as u64,
            });
        }

        let pad_key_iv = utils::get_pad_key_and_iv(index, &self.chunks);
        let encrypted_contents = utils::encrypt_chunk(data, pad_key_iv)?;

        let hash = self.storage.generate_address(&encrypted_contents).await;
        self.chunks[index].hash = hash.to_vec();

        self.storage
            .put(hash.to_vec(), encrypted_contents.to_vec())
            .await?;
        Ok(())
    }
}

impl<S: Storage + Send + Sync> From<SmallEncryptor<S>> for LargeEncryptor<S> {
    fn from(small_encryptor: SmallEncryptor<S>) -> LargeEncryptor<S> {
        LargeEncryptor {
            storage: small_encryptor.storage,
            chunks: vec![],
            original_chunks: None,
            chunk_0_data: small_encryptor.buffer,
            chunk_1_data: vec![],
            buffer: vec![],
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        super::{
            medium_encryptor::{self, MediumEncryptor},
            small_encryptor::SmallEncryptor,
            utils, MAX_CHUNK_SIZE,
        },
        *,
    };
    use crate::{
        data_map::DataMap,
        self_encryptor::SelfEncryptor,
        test_helpers::{new_test_rng, random_bytes, Blob, SimpleStorage},
    };
    use rand::Rng;

    #[test]
    fn consts() {
        assert_eq!(MIN, medium_encryptor::MAX + 1);
    }

    // Writes all of `data` to a new encryptor in a single call, then closes and reads back via
    // a `SelfEncryptor`.
    async fn basic_write_and_close(data: &[u8]) {
        let (data_map, storage) = {
            let storage = SimpleStorage::new();
            let mut encryptor = unwrap!(SmallEncryptor::new(storage, vec![])
                .await
                .map(LargeEncryptor::from));
            assert_eq!(encryptor.len(), 0);
            assert!(encryptor.is_empty());
            encryptor = unwrap!(encryptor.write(data).await);
            assert_eq!(encryptor.len(), data.len() as u64);
            assert!(!encryptor.is_empty());
            unwrap!(encryptor.close().await)
        };

        match data_map {
            DataMap::Chunks(ref chunks) => assert!(chunks.len() > 3),
            _ => panic!("Wrong DataMap type returned."),
        }

        let self_encryptor = unwrap!(SelfEncryptor::new(storage, data_map));
        let fetched = unwrap!(self_encryptor.read(0, data.len() as u64).await);
        assert_eq!(Blob(&fetched), Blob(data));
    }

    // Splits `data` into several pieces, then for each piece:
    //  * constructs a new encryptor from existing chunk details (except for the first piece)
    //  * writes the piece
    //  * closes and reads back the full data via a `SelfEncryptor`.
    async fn multiple_writes_then_close<T: Rng>(rng: &mut T, data: &[u8]) {
        let mut storage = SimpleStorage::new();
        let mut existing_data = vec![];
        let data_pieces = utils::make_random_pieces(rng, data, MIN as usize);
        let mut current_chunks = vec![];
        for data in data_pieces {
            let data_map = {
                let mut encryptor = if current_chunks.is_empty() {
                    unwrap!(SmallEncryptor::new(storage, vec![])
                        .await
                        .map(LargeEncryptor::from))
                } else {
                    unwrap!(LargeEncryptor::new(storage, current_chunks).await)
                };
                encryptor = unwrap!(encryptor.write(data).await);
                existing_data.extend_from_slice(data);
                assert_eq!(encryptor.len(), existing_data.len() as u64);

                let (data_map, storage2) = unwrap!(encryptor.close().await);
                storage = storage2;
                data_map
            };

            match data_map {
                DataMap::Chunks(ref chunks) => {
                    assert!(chunks.len() > 3);
                    current_chunks = chunks.clone()
                }
                _ => panic!("Wrong DataMap type returned."),
            }

            let self_encryptor = unwrap!(SelfEncryptor::new(storage, data_map));
            assert_eq!(self_encryptor.len().await, existing_data.len() as u64);
            let fetched = unwrap!(self_encryptor.read(0, existing_data.len() as u64).await);
            assert_eq!(Blob(&fetched), Blob(&existing_data));

            storage = self_encryptor.into_storage().await;
        }
        assert_eq!(Blob(&existing_data[..]), Blob(data));
    }

    #[tokio::test]
    async fn all_unit() {
        let mut rng = new_test_rng();
        let data = random_bytes(&mut rng, 5 * MAX_CHUNK_SIZE as usize);

        basic_write_and_close(&data[..MIN as usize]).await;
        basic_write_and_close(&data[..(MAX_CHUNK_SIZE as usize * 4)]).await;
        basic_write_and_close(&data[..=(MAX_CHUNK_SIZE as usize * 4)]).await;
        basic_write_and_close(&data).await;

        multiple_writes_then_close(&mut rng, &data[..(MIN as usize + 100)]).await;
        multiple_writes_then_close(&mut rng, &data).await;

        // Test converting from `MediumEncryptor`.
        let (data_map, storage) = {
            let storage = SimpleStorage::new();
            let mut medium_encryptor = unwrap!(SmallEncryptor::new(storage, vec![])
                .await
                .map(MediumEncryptor::from));

            medium_encryptor = unwrap!(medium_encryptor.write(&data[..(MIN as usize - 1)]).await);
            let mut large_encryptor = unwrap!(LargeEncryptor::from_medium(medium_encryptor).await);
            assert_eq!(large_encryptor.len(), MIN - 1);
            assert!(!large_encryptor.is_empty());
            large_encryptor = unwrap!(large_encryptor.write(&data[(MIN as usize - 1)..]).await);
            assert_eq!(large_encryptor.len(), data.len() as u64);
            assert!(!large_encryptor.is_empty());
            unwrap!(large_encryptor.close().await)
        };

        match data_map {
            DataMap::Chunks(ref chunks) => assert_eq!(chunks.len(), 5),
            _ => panic!("Wrong DataMap type returned."),
        }

        let self_encryptor = unwrap!(SelfEncryptor::new(storage, data_map));
        let fetched = unwrap!(self_encryptor.read(0, data.len() as u64).await);
        assert_eq!(Blob(&fetched), Blob(&data));
    }
}