self_encryption 0.35.0

Self encrypting files (convergent encryption plus obfuscation)
Documentation
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright 2021 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 serde::{
    de::{self, MapAccess, Visitor},
    ser::SerializeStruct,
    Deserialize, Deserializer, Serialize, Serializer,
};
use std::fmt::{self, Debug, Formatter, Write};
use xor_name::XorName;

/// Holds the information that is required to recover the content of the encrypted file.
/// This is held as a vector of `ChunkInfo`, i.e. a list of the file's chunk hashes.
/// Only files larger than 3072 bytes (3 * MIN_CHUNK_SIZE) can be self-encrypted.
/// Smaller files will have to be batched together.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct DataMap {
    /// List of chunk hashes
    pub chunk_identifiers: Vec<ChunkInfo>,
    /// Child value, None means root data map and any other valuesignifies how
    /// many levels of data map we have shrunk
    pub child: Option<usize>,
}

impl DataMap {
    /// Serialize DataMap to bytes using bincode
    pub fn to_bytes(&self) -> Result<Vec<u8>, bincode::Error> {
        bincode::serialize(self)
    }

    /// Deserialize DataMap from bytes (v1 versioned format).
    /// Delegates to the `Deserialize` impl which handles version checking.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, bincode::Error> {
        bincode::deserialize(bytes)
    }
}

#[allow(clippy::len_without_is_empty)]
impl DataMap {
    /// A new instance from a vec of partial keys.
    ///
    /// Sorts on instantiation.
    /// The algorithm requires this to be a sorted list to allow get_pad_iv_key to obtain the
    /// correct pre-encryption hashes for decryption/encryption.
    pub fn new(mut keys: Vec<ChunkInfo>) -> Self {
        keys.sort_by(|a, b| a.index.cmp(&b.index));
        Self {
            chunk_identifiers: keys,
            child: None,
        }
    }

    /// Creates a new DataMap with a specified child value
    pub fn with_child(mut keys: Vec<ChunkInfo>, child: usize) -> Self {
        keys.sort_by(|a, b| a.index.cmp(&b.index));
        Self {
            chunk_identifiers: keys,
            child: Some(child),
        }
    }

    /// Original (pre-encryption) size of the file.
    pub fn original_file_size(&self) -> usize {
        DataMap::total_size(&self.chunk_identifiers)
    }

    /// Returns the list of chunks pre and post encryption hashes if present.
    pub fn infos(&self) -> &[ChunkInfo] {
        &self.chunk_identifiers
    }

    /// Returns the child value if set
    pub fn child(&self) -> Option<usize> {
        self.child
    }

    /// Iterates through the keys to figure out the total size of the data, i.e. the file size.
    fn total_size(keys: &[ChunkInfo]) -> usize {
        keys.iter().fold(0, |acc, chunk| acc + chunk.src_size)
    }

    /// Returns the number of chunks in the DataMap
    pub fn len(&self) -> usize {
        self.chunk_identifiers.len()
    }

    /// Returns true if this DataMap has a child value
    pub fn is_child(&self) -> bool {
        self.child.is_some()
    }
}

impl Serialize for DataMap {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if serializer.is_human_readable() {
            // For JSON and other human-readable formats, use struct format
            let mut st = serializer.serialize_struct("DataMap", 2)?;
            st.serialize_field("chunk_identifiers", &self.chunk_identifiers)?;
            st.serialize_field("child", &self.child)?;
            st.end()
        } else {
            // For binary formats, prepend a version byte
            // Version 1: New format with chunk_identifiers and child fields
            #[derive(Serialize)]
            struct VersionedDataMap<'a> {
                version: u8,
                chunk_identifiers: &'a [ChunkInfo],
                child: &'a Option<usize>,
            }

            let versioned = VersionedDataMap {
                version: 1u8,
                chunk_identifiers: &self.chunk_identifiers,
                child: &self.child,
            };

            versioned.serialize(serializer)
        }
    }
}

impl<'de> Deserialize<'de> for DataMap {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            struct DataMapVisitor;

            impl<'de> Visitor<'de> for DataMapVisitor {
                type Value = DataMap;

                fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                    write!(
                        f,
                        "a DataMap struct with chunk_identifiers and optional child"
                    )
                }

                fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
                where
                    A: MapAccess<'de>,
                {
                    let mut chunks: Option<Vec<ChunkInfo>> = None;
                    let mut child: Option<Option<usize>> = None;

                    while let Some(key) = map.next_key::<&str>()? {
                        match key {
                            "chunk_identifiers" => chunks = Some(map.next_value()?),
                            "child" => child = Some(map.next_value()?),
                            _ => {
                                let _: de::IgnoredAny = map.next_value()?;
                            }
                        }
                    }

                    let chunk_identifiers =
                        chunks.ok_or_else(|| de::Error::missing_field("chunk_identifiers"))?;
                    Ok(DataMap {
                        chunk_identifiers,
                        child: child.flatten(),
                    })
                }
            }

            deserializer.deserialize_any(DataMapVisitor)
        } else {
            #[derive(Deserialize)]
            struct VersionedDataMap {
                version: u8,
                chunk_identifiers: Vec<ChunkInfo>,
                child: Option<usize>,
            }

            let versioned = VersionedDataMap::deserialize(deserializer)?;
            if versioned.version == 1 {
                Ok(DataMap {
                    chunk_identifiers: versioned.chunk_identifiers,
                    child: versioned.child,
                })
            } else {
                Err(de::Error::custom(format!(
                    "unsupported DataMap version: {ver}",
                    ver = versioned.version
                )))
            }
        }
    }
}

impl Debug for DataMap {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(formatter, "DataMap:")?;
        if let Some(child) = self.child {
            writeln!(formatter, "    child: {child}")?;
        }
        let len = self.chunk_identifiers.len();
        for (index, chunk) in self.chunk_identifiers.iter().enumerate() {
            if index + 1 == len {
                write!(formatter, "        {chunk:?}")?
            } else {
                writeln!(formatter, "        {chunk:?}")?
            }
        }
        Ok(())
    }
}

/// This is - in effect - a partial decryption key for an encrypted chunk of data.
///
/// It holds pre- and post-encryption hashes as well as the original
/// (pre-compression) size for a given chunk.
/// This information is required for successful recovery of a chunk, as well as for the
/// encryption/decryption of it's two immediate successors, modulo the number of chunks in the
/// corresponding DataMap.
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone, Default)]
pub struct ChunkInfo {
    /// Index number (zero-based)
    pub index: usize,
    /// Post-encryption hash of chunk
    pub dst_hash: XorName,
    /// Pre-encryption hash of chunk
    pub src_hash: XorName,
    /// Size before encryption and compression (any possible padding depending
    /// on cipher used alters this)
    pub src_size: usize,
}

fn debug_bytes<V: AsRef<[u8]>>(input: V) -> String {
    let input_ref = input.as_ref();
    if input_ref.is_empty() {
        return "<empty>".to_owned();
    }
    if input_ref.len() <= 6 {
        let mut ret = String::new();
        for byte in input_ref.iter() {
            let _ = write!(ret, "{byte:02x}");
        }
        return ret;
    }
    // Safety: len > 6 is guaranteed by the early return above, so all indices are valid.
    let len = input_ref.len();
    format!(
        "{:02x}{:02x}{:02x}..{:02x}{:02x}{:02x}",
        input_ref[0],
        input_ref[1],
        input_ref[2],
        input_ref[len - 3],
        input_ref[len - 2],
        input_ref[len - 1]
    )
}

impl Debug for ChunkInfo {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            formatter,
            "ChunkInfo {{ index: {}, dst_hash: {}, src_hash: {}, src_size: {} }}",
            self.index,
            debug_bytes(self.dst_hash),
            debug_bytes(self.src_hash),
            self.src_size
        )
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    fn create_test_chunk_info(index: usize) -> ChunkInfo {
        ChunkInfo {
            index,
            dst_hash: crate::hash::content_hash(format!("dst_{index}").as_bytes()),
            src_hash: crate::hash::content_hash(format!("src_{index}").as_bytes()),
            src_size: 1024 * (index + 1),
        }
    }

    #[test]
    fn test_deserialize_new_format_json() {
        // Create a DataMap with the new format
        let chunks = vec![create_test_chunk_info(0), create_test_chunk_info(1)];
        let data_map = DataMap::with_child(chunks.clone(), 5);

        // Serialize to JSON
        let json = serde_json::to_string(&data_map).unwrap();

        // Verify the JSON contains the expected structure
        assert!(json.contains("\"chunk_identifiers\""));
        assert!(json.contains("\"child\":5"));

        // Deserialize back
        let deserialized: DataMap = serde_json::from_str(&json).unwrap();

        // Verify
        assert_eq!(deserialized.chunk_identifiers.len(), 2);
        assert_eq!(deserialized.child, Some(5));
        assert_eq!(deserialized.chunk_identifiers[0].index, 0);
        assert_eq!(deserialized.chunk_identifiers[1].index, 1);
    }

    #[test]
    fn test_new_format_without_child_json() {
        // Create a DataMap without child
        let chunks = vec![create_test_chunk_info(0)];
        let data_map = DataMap::new(chunks.clone());

        // Serialize and deserialize
        let json = serde_json::to_string(&data_map).unwrap();
        let deserialized: DataMap = serde_json::from_str(&json).unwrap();

        // Verify
        assert_eq!(deserialized.chunk_identifiers.len(), 1);
        assert_eq!(deserialized.child, None);
    }

    #[test]
    fn test_bincode_new_format() {
        // Create and serialize with new format
        let chunks = vec![create_test_chunk_info(0)];
        let data_map = DataMap::with_child(chunks, 3);

        let bytes = data_map.to_bytes().unwrap();
        let deserialized = DataMap::from_bytes(&bytes).unwrap();

        assert_eq!(deserialized.chunk_identifiers.len(), 1);
        assert_eq!(deserialized.child, Some(3));
    }

    #[test]
    fn test_bincode_version_byte() {
        // Verify that new format includes version byte
        let chunks = vec![create_test_chunk_info(0)];
        let data_map = DataMap::new(chunks);

        let bytes = data_map.to_bytes().unwrap();

        // First byte should be the version (1)
        assert!(!bytes.is_empty());
        assert_eq!(bytes[0], 1u8);
    }

    #[test]
    fn test_preserve_chunk_order() {
        // Ensure that chunk ordering is preserved through serialization
        let chunks = vec![
            create_test_chunk_info(2),
            create_test_chunk_info(0),
            create_test_chunk_info(1),
        ];

        // DataMap::new should sort them
        let data_map = DataMap::new(chunks);
        assert_eq!(data_map.chunk_identifiers[0].index, 0);
        assert_eq!(data_map.chunk_identifiers[1].index, 1);
        assert_eq!(data_map.chunk_identifiers[2].index, 2);

        // Serialize and deserialize
        let json = serde_json::to_string(&data_map).unwrap();
        let deserialized: DataMap = serde_json::from_str(&json).unwrap();

        // Order should be preserved
        assert_eq!(deserialized.chunk_identifiers[0].index, 0);
        assert_eq!(deserialized.chunk_identifiers[1].index, 1);
        assert_eq!(deserialized.chunk_identifiers[2].index, 2);
    }

    #[test]
    fn test_error_handling_for_corrupted_data() {
        // Test with completely invalid data
        let invalid_data = b"this is not valid bincode data";
        assert!(DataMap::from_bytes(invalid_data).is_err());

        // Test with partial data (truncated)
        let chunks = vec![create_test_chunk_info(0)];
        let data_map = DataMap::new(chunks);
        let valid_bytes = data_map.to_bytes().unwrap();
        let truncated_bytes = &valid_bytes[..valid_bytes.len() - 5];
        assert!(DataMap::from_bytes(truncated_bytes).is_err());

        // Test with wrong version number
        let mut wrong_version_data = valid_bytes.clone();
        if let Some(first) = wrong_version_data.first_mut() {
            *first = 99u8;
        }
        assert!(DataMap::from_bytes(&wrong_version_data).is_err());
    }

    #[test]
    fn test_bincode_version_byte_consistency() {
        let chunks = vec![create_test_chunk_info(0)];
        let data_map = DataMap::new(chunks.clone());
        let bytes = data_map.to_bytes().unwrap();

        assert!(!bytes.is_empty());
        assert_eq!(bytes[0], 1u8);

        let data_map_with_child = DataMap::with_child(chunks, 42);
        let bytes_with_child = data_map_with_child.to_bytes().unwrap();
        assert_eq!(bytes_with_child[0], 1u8);
    }

    #[test]
    fn test_round_trip_serialization_consistency() {
        let chunks = vec![create_test_chunk_info(0), create_test_chunk_info(1)];

        let data_map = DataMap::with_child(chunks.clone(), 3);
        let bytes = data_map.to_bytes().unwrap();
        let deserialized = DataMap::from_bytes(&bytes).unwrap();
        assert_eq!(data_map, deserialized);

        let data_map_no_child = DataMap::new(chunks);
        let bytes_no_child = data_map_no_child.to_bytes().unwrap();
        let deserialized_no_child = DataMap::from_bytes(&bytes_no_child).unwrap();
        assert_eq!(data_map_no_child, deserialized_no_child);
    }
}