abcrypt 0.5.0

An implementation of the abcrypt encrypted data format
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
// SPDX-FileCopyrightText: 2022 Shun Sakai
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Specifications of the abcrypt encrypted data format.

use core::mem;

use argon2::Algorithm;
use blake2::{
    Blake2bMac512,
    digest::{self, Mac, Output, OutputSizeUser, typenum::Unsigned},
};
use chacha20poly1305::{
    AeadCore, Key as XChaCha20Poly1305Key, KeySizeUser, XChaCha20Poly1305, XNonce,
};
use rand::{Rng, SeedableRng, rngs::StdRng};

use crate::{Error, Params, Result, argon2_context};

/// A type alias for magic number of the abcrypt encrypted data format.
type MagicNumber = [u8; 7];

/// A type alias for salt of Argon2.
type Salt = [u8; 32];

/// A type alias for output of BLAKE2b-512-MAC.
type Blake2bMac512Output = Output<Blake2bMac512>;

/// A type alias for key of BLAKE2b-512-MAC.
type Blake2bMac512Key = digest::Key<Blake2bMac512>;

/// The number of bytes of the header.
///
/// # Examples
///
/// ```
/// assert_eq!(abcrypt::HEADER_SIZE, 148);
///
/// let ciphertext = include_bytes!("../tests/data/v1/argon2id/v0x13/data.txt.abcrypt");
/// let plaintext = include_bytes!("../tests/data/data.txt");
/// assert_eq!(
///     abcrypt::HEADER_SIZE,
///     ciphertext.len() - (plaintext.len() + abcrypt::TAG_SIZE)
/// );
/// ```
pub const HEADER_SIZE: usize = Header::SIZE;

/// The number of bytes of the MAC (authentication tag) of the ciphertext.
///
/// # Examples
///
/// ```
/// assert_eq!(abcrypt::TAG_SIZE, 16);
///
/// let ciphertext = include_bytes!("../tests/data/v1/argon2id/v0x13/data.txt.abcrypt");
/// let plaintext = include_bytes!("../tests/data/data.txt");
/// assert_eq!(
///     abcrypt::TAG_SIZE,
///     ciphertext.len() - (abcrypt::HEADER_SIZE + plaintext.len())
/// );
/// ```
pub const TAG_SIZE: usize = <XChaCha20Poly1305 as AeadCore>::TagSize::USIZE;

/// Version of the abcrypt encrypted data format.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
enum Version {
    /// Version 0.
    V0,

    /// Version 1.
    #[default]
    V1,
}

impl From<Version> for u8 {
    #[inline]
    fn from(version: Version) -> Self {
        version as Self
    }
}

impl TryFrom<u8> for Version {
    type Error = Error;

    #[inline]
    fn try_from(version: u8) -> Result<Self> {
        match version {
            0 => Ok(Self::V0),
            1 => Ok(Self::V1),
            v => Err(Error::UnknownVersion(v)),
        }
    }
}

/// Header of the abcrypt encrypted data format.
#[derive(Clone, Debug)]
pub struct Header {
    magic_number: MagicNumber,
    version: Version,
    argon2_type: argon2_context::Variant,
    argon2_version: argon2_context::Version,
    params: Params,
    salt: Salt,
    nonce: XNonce,
    mac: Blake2bMac512Output,
}

impl Header {
    /// Magic number of the abcrypt encrypted data format.
    ///
    /// This is the ASCII code for "abcrypt".
    const MAGIC_NUMBER: MagicNumber = *b"abcrypt";

    /// The number of bytes of the header.
    const SIZE: usize = mem::size_of::<MagicNumber>()
        + mem::size_of::<Version>()
        + mem::size_of::<argon2_context::Variant>()
        + mem::size_of::<argon2_context::Version>()
        + mem::size_of::<Params>()
        + mem::size_of::<Salt>()
        + <XChaCha20Poly1305 as AeadCore>::NonceSize::USIZE
        + <Blake2bMac512 as OutputSizeUser>::OutputSize::USIZE;

    /// Creates a new `Header`.
    pub fn new(
        argon2_type: Algorithm,
        argon2_version: argon2::Version,
        params: argon2::Params,
    ) -> Self {
        let magic_number = Self::MAGIC_NUMBER;
        let version = Version::default();
        let argon2_type = argon2_type.into();
        let argon2_version = argon2_version.into();
        let params = params.into();
        let salt = StdRng::from_entropy().r#gen();
        let nonce = XChaCha20Poly1305::generate_nonce(StdRng::from_entropy());
        let mac = Blake2bMac512Output::default();
        Self {
            magic_number,
            version,
            argon2_type,
            argon2_version,
            params,
            salt,
            nonce,
            mac,
        }
    }

    /// Parses `data` into the header.
    pub fn parse(data: &[u8]) -> Result<Self> {
        if data.len() < Self::SIZE + TAG_SIZE {
            return Err(Error::InvalidLength);
        }

        let Some(magic_number) = Some(Self::MAGIC_NUMBER).filter(|mn| &data[..7] == mn) else {
            return Err(Error::InvalidMagicNumber);
        };
        let version = Version::try_from(data[7])?;
        if version != Version::V1 {
            return Err(Error::UnsupportedVersion(version.into()));
        }
        let argon2_type = u32::from_le_bytes(
            data[8..12]
                .try_into()
                .expect("size of the Argon2 type should be 4 bytes"),
        )
        .try_into()?;
        let argon2_version = u32::from_le_bytes(
            data[12..16]
                .try_into()
                .expect("size of the Argon2 version should be 4 bytes"),
        )
        .try_into()?;
        let memory_cost = u32::from_le_bytes(
            data[16..20]
                .try_into()
                .expect("size of `memoryCost` should be 4 bytes"),
        );
        let time_cost = u32::from_le_bytes(
            data[20..24]
                .try_into()
                .expect("size of `timeCost` should be 4 bytes"),
        );
        let parallelism = u32::from_le_bytes(
            data[24..28]
                .try_into()
                .expect("size of `parallelism` should be 4 bytes"),
        );
        let params = argon2::Params::new(memory_cost, time_cost, parallelism, None)
            .map(Params::from)
            .map_err(Error::InvalidArgon2Params)?;
        let salt = data[28..60]
            .try_into()
            .expect("size of salt should be 32 bytes");
        let nonce = *XNonce::from_slice(&data[60..84]);
        let mac = Blake2bMac512Output::default();
        Ok(Self {
            magic_number,
            version,
            argon2_type,
            argon2_version,
            params,
            salt,
            nonce,
            mac,
        })
    }

    /// Gets a BLAKE2b-512-MAC of this header.
    #[inline]
    pub fn compute_mac(&mut self, key: &Blake2bMac512Key) {
        let mut mac = Blake2bMac512::new(key);
        mac.update(&self.as_bytes()[..84]);
        self.mac.copy_from_slice(&mac.finalize().into_bytes());
    }

    /// Verifies a BLAKE2b-512-MAC stored in this header.
    pub fn verify_mac(&mut self, key: &Blake2bMac512Key, tag: &Blake2bMac512Output) -> Result<()> {
        let mut mac = Blake2bMac512::new(key);
        mac.update(&self.as_bytes()[..84]);
        mac.verify(tag)?;
        self.mac.copy_from_slice(tag);
        Ok(())
    }

    /// Converts this header to a byte array.
    pub fn as_bytes(&self) -> [u8; Self::SIZE] {
        let mut header = [u8::default(); Self::SIZE];
        header[..7].copy_from_slice(&self.magic_number);
        header[7] = self.version.into();
        header[8..12].copy_from_slice(&u32::from(self.argon2_type).to_le_bytes());
        header[12..16].copy_from_slice(&u32::from(self.argon2_version).to_le_bytes());
        header[16..20].copy_from_slice(&self.params.memory_cost().to_le_bytes());
        header[20..24].copy_from_slice(&self.params.time_cost().to_le_bytes());
        header[24..28].copy_from_slice(&self.params.parallelism().to_le_bytes());
        header[28..60].copy_from_slice(&self.salt);
        header[60..84].copy_from_slice(&self.nonce);
        header[84..].copy_from_slice(&self.mac);
        header
    }

    /// Returns the Argon2 type stored in this header.
    #[inline]
    pub const fn argon2_type(&self) -> argon2_context::Variant {
        self.argon2_type
    }

    /// Returns the Argon2 version stored in this header.
    #[inline]
    pub const fn argon2_version(&self) -> argon2_context::Version {
        self.argon2_version
    }

    /// Returns the Argon2 parameters stored in this header.
    #[inline]
    pub const fn params(&self) -> Params {
        self.params
    }

    /// Returns a salt stored in this header.
    #[inline]
    pub const fn salt(&self) -> Salt {
        self.salt
    }

    /// Returns a nonce stored in this header.
    #[inline]
    pub const fn nonce(&self) -> XNonce {
        self.nonce
    }
}

/// Derived key.
#[derive(Clone, Debug)]
pub struct DerivedKey {
    encrypt: XChaCha20Poly1305Key,
    mac: Blake2bMac512Key,
}

impl DerivedKey {
    /// The number of bytes of the derived key.
    pub const SIZE: usize = <XChaCha20Poly1305 as KeySizeUser>::KeySize::USIZE
        + <Blake2bMac512 as KeySizeUser>::KeySize::USIZE;

    /// Creates a new `DerivedKey`.
    #[inline]
    pub fn new(dk: [u8; Self::SIZE]) -> Self {
        let encrypt = *XChaCha20Poly1305Key::from_slice(&dk[..32]);
        let mac = *Blake2bMac512Key::from_slice(&dk[32..]);
        Self { encrypt, mac }
    }

    /// Returns the key for encrypted.
    #[inline]
    pub const fn encrypt(&self) -> XChaCha20Poly1305Key {
        self.encrypt
    }

    /// Returns the key for a MAC.
    #[inline]
    pub const fn mac(&self) -> Blake2bMac512Key {
        self.mac
    }
}

#[cfg(test)]
mod tests {
    use core::str;

    use super::*;

    #[test]
    fn header_size() {
        assert_eq!(HEADER_SIZE, 148);
        assert_eq!(HEADER_SIZE, Header::SIZE);
    }

    #[test]
    fn tag_size() {
        assert_eq!(TAG_SIZE, 16);
        assert_eq!(TAG_SIZE, <XChaCha20Poly1305 as AeadCore>::TagSize::USIZE);
    }

    #[test]
    fn version() {
        assert_eq!(Version::V0 as u8, 0);
        assert_eq!(Version::V1 as u8, 1);
    }

    #[test]
    fn size_of_version() {
        assert_eq!(mem::size_of::<Version>(), mem::size_of::<u8>());
    }

    #[test]
    fn clone_version() {
        assert_eq!(Version::V0.clone(), Version::V0);
        assert_eq!(Version::V1.clone(), Version::V1);
    }

    #[test]
    fn copy_version() {
        {
            let a = Version::V0;
            let b = a;
            assert_eq!(a, b);
        }

        {
            let a = Version::V1;
            let b = a;
            assert_eq!(a, b);
        }
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn debug_version() {
        assert_eq!(format!("{:?}", Version::V0), "V0");
        assert_eq!(format!("{:?}", Version::V1), "V1");
    }

    #[test]
    fn default_version() {
        assert_eq!(Version::default(), Version::V1);
    }

    #[test]
    fn version_equality() {
        assert_eq!(Version::V0, Version::V0);
        assert_ne!(Version::V0, Version::V1);
        assert_ne!(Version::V1, Version::V0);
        assert_eq!(Version::V1, Version::V1);
    }

    #[test]
    fn from_version_to_u8() {
        assert_eq!(u8::from(Version::V0), 0);
        assert_eq!(u8::from(Version::V1), 1);
    }

    #[test]
    fn try_from_u8_to_version() {
        assert_eq!(Version::try_from(0).unwrap(), Version::V0);
        assert_eq!(Version::try_from(1).unwrap(), Version::V1);
    }

    #[test]
    fn try_from_u8_to_version_with_invalid_version() {
        assert_eq!(Version::try_from(2).unwrap_err(), Error::UnknownVersion(2));
        assert_eq!(
            Version::try_from(u8::MAX).unwrap_err(),
            Error::UnknownVersion(u8::MAX)
        );
    }

    #[test]
    fn magic_number() {
        assert_eq!(str::from_utf8(&Header::MAGIC_NUMBER).unwrap(), "abcrypt");
    }

    #[test]
    fn derived_key_size() {
        assert_eq!(DerivedKey::SIZE, 96);
    }
}