Skip to main content

crypto_profile/
sealbox_wire.rs

1//! The sealed box's version-1 framing: version, ephemeral P-384 point, nonce, ciphertext and tag.
2use crate::error::CryptoError;
3
4/// The sealed-blob format version byte.
5pub const VERSION: u8 = 1;
6/// The length of the uncompressed ephemeral P-384 point.
7pub const EPHEMERAL_KEY_LEN: usize = 97;
8/// The AES-GCM nonce length in a sealed blob.
9pub const NONCE_LEN: usize = 12;
10/// The AES-GCM tag length in a sealed blob.
11pub const TAG_LEN: usize = 16;
12/// The length of the version, ephemeral point and nonce that precede the ciphertext.
13pub const HEADER_LEN: usize = 1 + EPHEMERAL_KEY_LEN + NONCE_LEN;
14/// The shortest valid blob: a header, one ciphertext byte and a tag.
15pub const MIN_BLOB_LEN: usize = HEADER_LEN + 1 + TAG_LEN;
16const UNCOMPRESSED_POINT: u8 = 0x04;
17
18/// The fields of a parsed sealed blob.
19#[derive(Debug)]
20pub struct BlobParts<'a> {
21    /// The uncompressed ephemeral P-384 point.
22    pub ephemeral: &'a [u8],
23    /// The AES-GCM nonce.
24    pub nonce: [u8; NONCE_LEN],
25    /// The ciphertext followed by the tag.
26    pub sealed: &'a [u8],
27}
28
29/// The blob length for a plaintext of `plaintext_len` bytes.
30pub fn sealed_len(plaintext_len: usize) -> usize {
31    HEADER_LEN + plaintext_len + TAG_LEN
32}
33
34/// Splits a blob into its parts, refusing a short blob, another version or a non-uncompressed point.
35pub fn parse_blob(blob: &[u8]) -> Result<BlobParts<'_>, CryptoError> {
36    if blob.len() < MIN_BLOB_LEN {
37        return Err(CryptoError::SealBlob);
38    }
39    if blob[0] != VERSION {
40        return Err(CryptoError::SealVersion);
41    }
42    if blob[1] != UNCOMPRESSED_POINT {
43        return Err(CryptoError::SealEphemeralKey);
44    }
45    let mut nonce = [0u8; NONCE_LEN];
46    nonce.copy_from_slice(&blob[1 + EPHEMERAL_KEY_LEN..HEADER_LEN]);
47    Ok(BlobParts {
48        ephemeral: &blob[1..1 + EPHEMERAL_KEY_LEN],
49        nonce,
50        sealed: &blob[HEADER_LEN..],
51    })
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    fn blob(len: usize) -> Vec<u8> {
59        let mut b = vec![0u8; len];
60        b[0] = VERSION;
61        b[1] = 0x04;
62        b
63    }
64
65    #[test]
66    fn the_v1_layout_is_pinned() {
67        assert_eq!(
68            (VERSION, EPHEMERAL_KEY_LEN, NONCE_LEN, TAG_LEN, HEADER_LEN),
69            (1, 97, 12, 16, 110)
70        );
71        assert_eq!(MIN_BLOB_LEN, 127);
72        assert_eq!(sealed_len(95), 110 + 95 + 16);
73    }
74
75    #[test]
76    fn a_blob_splits_into_ephemeral_nonce_and_sealed_bytes() {
77        let mut b = blob(MIN_BLOB_LEN);
78        b[EPHEMERAL_KEY_LEN] = 0xEE;
79        b[1 + EPHEMERAL_KEY_LEN] = 0xAA;
80        b[HEADER_LEN - 1] = 0xAB;
81        b[HEADER_LEN] = 0xBB;
82        let p = parse_blob(&b).unwrap();
83        assert_eq!(
84            (
85                p.ephemeral.len(),
86                p.ephemeral[0],
87                p.ephemeral[EPHEMERAL_KEY_LEN - 1]
88            ),
89            (EPHEMERAL_KEY_LEN, 0x04, 0xEE)
90        );
91        assert_eq!((p.nonce[0], p.nonce[NONCE_LEN - 1]), (0xAA, 0xAB));
92        assert_eq!((p.sealed.len(), p.sealed[0]), (1 + TAG_LEN, 0xBB));
93        assert_eq!(
94            parse_blob(&blob(4096)).unwrap().sealed.len(),
95            4096 - HEADER_LEN
96        );
97    }
98
99    #[test]
100    fn a_short_blob_or_another_version_or_point_form_is_refused_in_that_order() {
101        assert!(matches!(
102            parse_blob(&blob(MIN_BLOB_LEN - 1)),
103            Err(CryptoError::SealBlob)
104        ));
105        assert!(matches!(parse_blob(&[]), Err(CryptoError::SealBlob)));
106        let mut short_v2 = blob(MIN_BLOB_LEN - 1);
107        short_v2[0] = 2;
108        assert!(matches!(parse_blob(&short_v2), Err(CryptoError::SealBlob)));
109        let mut v2 = blob(MIN_BLOB_LEN);
110        v2[0] = 2;
111        v2[1] = 0x02;
112        assert!(matches!(parse_blob(&v2), Err(CryptoError::SealVersion)));
113        let mut compressed = blob(MIN_BLOB_LEN);
114        compressed[1] = 0x02;
115        assert!(matches!(
116            parse_blob(&compressed),
117            Err(CryptoError::SealEphemeralKey)
118        ));
119    }
120}