carbonado 0.6.0

An apocalypse-resistant data storage format for the truly paranoid.
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
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::{
    convert::TryFrom,
    fs::File,
    io::{Read, Seek},
};

use bao::Hash;
use bytes::Bytes;
use nom::{
    bytes::complete::take,
    number::complete::{le_u32, le_u8},
    IResult,
};
use secp256k1::{schnorr::Signature, Keypair, Message, PublicKey, Secp256k1, SecretKey};

use crate::{
    constants::{Format, MAGICNO},
    decoding, encoding,
    error::CarbonadoError,
    structs::{EncodeInfo, Encoded},
    utils::{decode_bao_hash, encode_bao_hash},
};

/// Contains deserialized copies of the data kept in the Carbonado header.
#[derive(Clone, Debug)]
pub struct Header {
    /// A secp256k1 compressed public key is 33 bytes.
    pub pubkey: PublicKey,
    /// A Bao hash is 32 bytes.
    pub hash: Hash,
    /// A Schnorr signature is 64 bytes.
    pub signature: Signature,
    /// A Carbonado format code is 1 byte.
    pub format: Format,
    /// A chunk index is provided so each chunk can be stored in separate files.
    pub chunk_index: u8,
    /// Number of verifiable bytes.
    pub encoded_len: u32,
    /// Number of bytes added to pad to align zfec chunks and bao slices.
    pub padding_len: u32,
    /// Bytes that are normally zero but can contain extra data if needed
    pub metadata: Option<[u8; 8]>,
}

impl TryFrom<&File> for Header {
    type Error = CarbonadoError;

    /// Attempts to decode a header from a file.
    fn try_from(mut file: &File) -> Result<Self, CarbonadoError> {
        let mut magic_no = [0_u8; 12];
        let mut pubkey = [0_u8; 33];
        let mut hash = [0_u8; 32];
        let mut signature = [0_u8; 64];
        let mut format = [0_u8; 1];
        let mut chunk_index = [0_u8; 1];
        let mut encoded_len = [0_u8; 4];
        let mut padding_len = [0_u8; 4];
        let mut metadata = [0_u8; 8];

        file.rewind()?;

        let mut handle = file.take(Header::len() as u64);
        handle.read_exact(&mut magic_no)?;
        handle.read_exact(&mut pubkey)?;
        handle.read_exact(&mut hash)?;
        handle.read_exact(&mut signature)?;
        handle.read_exact(&mut format)?;
        handle.read_exact(&mut chunk_index)?;
        handle.read_exact(&mut encoded_len)?;
        handle.read_exact(&mut padding_len)?;
        handle.read_exact(&mut metadata)?;

        if &magic_no != MAGICNO {
            return Err(CarbonadoError::InvalidMagicNumber(format!("{magic_no:#?}")));
        }

        let pubkey = PublicKey::from_slice(&pubkey)?;
        let signature = Signature::from_slice(&signature)?;

        // Verify hash against signature
        let (x_only_pubkey, _) = pubkey.x_only_public_key();
        signature.verify(&Message::from_digest_slice(&hash)?, &x_only_pubkey)?;

        let hash = bao::Hash::from(hash);

        let format = Format::from(format[0]);
        let chunk_index = u8::from_le_bytes(chunk_index);
        let encoded_len = u32::from_le_bytes(encoded_len);
        let padding_len = u32::from_le_bytes(padding_len);

        Ok(Header {
            pubkey,
            hash,
            signature,
            format,
            chunk_index,
            encoded_len,
            padding_len,
            metadata: if metadata.iter().any(|b| b != &0) {
                Some(metadata)
            } else {
                None
            },
        })
    }
}

impl TryFrom<&[u8]> for Header {
    type Error = CarbonadoError;

    /// Attempts to decode a header from a file.
    fn try_from(bytes: &[u8]) -> Result<Self, CarbonadoError> {
        let (
            _,
            (
                magic_no,
                pubkey,
                hash,
                signature,
                format,
                chunk_index,
                encoded_len,
                padding_len,
                metadata,
            ),
        ) = Header::parse_bytes(bytes).unwrap();

        if magic_no != MAGICNO {
            return Err(CarbonadoError::InvalidMagicNumber(format!("{magic_no:#?}")));
        }

        let pubkey = PublicKey::from_slice(pubkey)?;
        let signature = Signature::from_slice(signature)?;

        // Verify hash against signature
        let (x_only_pubkey, _) = pubkey.x_only_public_key();
        signature.verify(&Message::from_digest_slice(hash)?, &x_only_pubkey)?;

        let hash: [u8; 32] = hash[0..32].try_into()?;
        let hash = bao::Hash::from(hash);

        let format = Format::from(format);

        Ok(Header {
            pubkey,
            hash,
            signature,
            format,
            chunk_index,
            encoded_len,
            padding_len,
            metadata,
        })
    }
}

impl TryFrom<Bytes> for Header {
    type Error = CarbonadoError;

    /// Attempts to decode a header from a file.
    fn try_from(bytes: Bytes) -> Result<Self, CarbonadoError> {
        let (
            _,
            (
                magic_no,
                pubkey,
                hash,
                signature,
                format,
                chunk_index,
                encoded_len,
                padding_len,
                metadata,
            ),
        ) = Header::parse_bytes(&bytes).unwrap();

        if magic_no != MAGICNO {
            return Err(CarbonadoError::InvalidMagicNumber(format!("{magic_no:#?}")));
        }

        let pubkey = PublicKey::from_slice(pubkey)?;
        let signature = Signature::from_slice(signature)?;

        // Verify hash against signature
        let (x_only_pubkey, _) = pubkey.x_only_public_key();
        signature.verify(&Message::from_digest_slice(hash)?, &x_only_pubkey)?;

        let hash: [u8; 32] = hash[0..32].try_into()?;
        let hash = bao::Hash::from(hash);

        let format = Format::from(format);

        Ok(Header {
            pubkey,
            hash,
            signature,
            format,
            chunk_index,
            encoded_len,
            padding_len,
            metadata,
        })
    }
}

impl TryFrom<&Bytes> for Header {
    type Error = CarbonadoError;

    /// Attempts to decode a header from a file.
    fn try_from(bytes: &Bytes) -> Result<Self, CarbonadoError> {
        let (
            _,
            (
                magic_no,
                pubkey,
                hash,
                signature,
                format,
                chunk_index,
                encoded_len,
                padding_len,
                metadata,
            ),
        ) = Header::parse_bytes(bytes).unwrap();

        if magic_no != MAGICNO {
            return Err(CarbonadoError::InvalidMagicNumber(format!("{magic_no:#?}")));
        }

        let pubkey = PublicKey::from_slice(pubkey)?;
        let signature = Signature::from_slice(signature)?;

        // Verify hash against signature
        let (x_only_pubkey, _) = pubkey.x_only_public_key();
        signature.verify(&Message::from_digest_slice(hash)?, &x_only_pubkey)?;

        let hash: [u8; 32] = hash[0..32].try_into()?;
        let hash = bao::Hash::from(hash);

        let format = Format::from(format);

        Ok(Header {
            pubkey,
            hash,
            signature,
            format,
            chunk_index,
            encoded_len,
            padding_len,
            metadata,
        })
    }
}

impl Header {
    /// 160 bytes should be added for Carbonado headers.
    pub fn len() -> usize {
        12 + 33 + 32 + 64 + 1 + 1 + 4 + 4 + 8 + 1
    }

    /// Creates a new Carbonado Header struct using the provided parameters, using provided serialized primitives.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        sk: &[u8],
        pk: &[u8],
        hash: &[u8],
        format: Format,
        chunk_index: u8,
        encoded_len: u32,
        padding_len: u32,
        metadata: Option<[u8; 8]>,
    ) -> Result<Self, CarbonadoError> {
        let msg = Message::from_digest_slice(hash)?;
        let pubkey = PublicKey::from_slice(pk)?;
        let secp = Secp256k1::new();
        let signature = Keypair::from_seckey_slice(&secp, sk)?.sign_schnorr(msg);
        let hash = decode_bao_hash(hash)?;

        Ok(Header {
            pubkey,
            signature,
            hash,
            format,
            chunk_index,
            encoded_len,
            padding_len,
            metadata,
        })
    }

    /// Creates a header to be prepended to files.
    pub fn try_to_vec(&self) -> Result<Vec<u8>, CarbonadoError> {
        let mut pubkey_bytes = self.pubkey.serialize().to_vec(); // 33 bytes
        if pubkey_bytes.len() != 33 {
            return Err(CarbonadoError::PubkeySerializationError);
        }
        let mut hash_bytes = self.hash.as_bytes().to_vec(); // 32 bytes
        if hash_bytes.len() != 32 {
            return Err(CarbonadoError::HashBytesLengthError);
        }
        let mut signature_bytes = self.signature.serialize().to_vec(); // 64 bytes
        if signature_bytes.len() != 64 {
            return Err(CarbonadoError::UnexpectedSignatureBytesLength(
                signature_bytes.len(),
            ));
        }
        let mut format_bytes = self.format.bits().to_le_bytes().to_vec(); // 1 byte
        let mut chunk_index = self.chunk_index.to_le_bytes().to_vec(); // 1 byte
        let mut encoded_len_bytes = self.encoded_len.to_le_bytes().to_vec(); // 8 bytes
        let mut padding_bytes = self.padding_len.to_le_bytes().to_vec(); // 2 bytes
        let mut header_padding = if let Some(metadata) = self.metadata {
            metadata.to_vec()
        } else {
            vec![0_u8; 8]
        };
        header_padding.push(0);

        let mut header = Vec::new();

        header.append(&mut MAGICNO.to_vec()); // 12 bytes
        header.append(&mut pubkey_bytes);
        header.append(&mut hash_bytes);
        header.append(&mut signature_bytes);
        header.append(&mut format_bytes);
        header.append(&mut chunk_index);
        header.append(&mut encoded_len_bytes);
        header.append(&mut padding_bytes);
        header.append(&mut header_padding);

        if header.len() != Header::len() {
            return Err(CarbonadoError::InvalidHeaderLength);
        }

        Ok(header)
    }

    /// Helper function for naming a Carbonado archive file.
    pub fn file_name(&self) -> String {
        let hash = encode_bao_hash(&self.hash);
        let fmt = self.format.bits();
        format!("{hash}.c{fmt}")
    }

    #[allow(clippy::type_complexity)]
    fn parse_bytes(
        b: &[u8],
    ) -> IResult<
        &[u8],
        (
            &[u8],
            &[u8],
            &[u8],
            &[u8],
            u8,
            u8,
            u32,
            u32,
            Option<[u8; 8]>,
        ),
    > {
        let (b, magic_no) = take(12u8)(b)?;
        let (b, pubkey) = take(33u8)(b)?;
        let (b, hash) = take(32u8)(b)?;
        let (b, signature) = take(64u8)(b)?;
        let (b, format) = le_u8(b)?;
        let (b, chunk_index) = le_u8(b)?;
        let (b, encoded_len) = le_u32(b)?;
        let (b, padding_len) = le_u32(b)?;
        let (b, metadata) = take(8u8)(b)?;

        let metadata: [u8; 8] = metadata.try_into().expect("8 bytes = 8 bytes");
        let metadata = if metadata.iter().any(|b| b != &0) {
            Some(metadata)
        } else {
            None
        };

        Ok((
            b,
            (
                magic_no,
                pubkey,
                hash,
                signature,
                format,
                chunk_index,
                encoded_len,
                padding_len,
                metadata,
            ),
        ))
    }
}

pub fn decode(secret_key: &[u8], encoded: &[u8]) -> Result<(Header, Vec<u8>), CarbonadoError> {
    let (header, body) = encoded.split_at(Header::len());
    let header = Header::try_from(header)?;
    let decoded = decoding::decode(
        secret_key,
        header.hash.as_bytes(),
        body,
        header.padding_len,
        header.format.into(),
    )?;

    Ok((header, decoded))
}

pub fn encode(
    sk: &[u8],
    pk: Option<&[u8]>,
    input: &[u8],
    level: u8,
    metadata: Option<[u8; 8]>,
) -> Result<(Vec<u8>, EncodeInfo), CarbonadoError> {
    let pubkey = match pk {
        Some(pubkey) => PublicKey::from_slice(pubkey)?,
        None => PublicKey::from_secret_key_global(&SecretKey::from_slice(sk)?),
    }
    .serialize();

    let Encoded(mut encoded, hash, encode_info) = encoding::encode(&pubkey, input, level)?;

    let format = Format::from(level);
    let header = Header::new(
        sk,
        &pubkey,
        hash.as_bytes(),
        format,
        0,
        encode_info.output_len,
        encode_info.padding_len,
        metadata,
    )?;

    let mut body = header.try_to_vec()?;
    body.append(&mut encoded);

    Ok((body, encode_info))
}