Skip to main content

pg_core/
consts.rs

1//! Constants used in the PostGuard protocol.
2
3// Container format versions. The name of each constant is the wire value it
4// holds, so `VERSION_2` is the version identifier `2` that appears in the
5// preamble. The older `VERSION_V1`/`VERSION_V2`/`VERSION_V3` spelling counted
6// from one and is kept below as deprecated aliases.
7
8/// Version 0 (legacy).
9///
10/// This version used the Kiltz-Vahlis-1 scheme.
11/// The header format was defined by Postcard, but is no longer supported.
12pub const VERSION_0: u16 = 0;
13
14/// Version 1 (legacy).
15///
16/// This version uses the CGW anonymous IBE scheme to construct a KEM variant. This scheme can
17/// encapsulate the same shared secret for multiple recipients. This version also supports
18/// conjunctions. For this version we required the header to be dynamic.
19/// The header format is defined by MessagePack.
20pub const VERSION_1: u16 = 1;
21
22/// Version 2.
23///
24/// This version uses the CGW anonymous IBE scheme to construct a KEM variant.
25/// The scheme supports a Sign-then-Encrypt composition using the GG-IBS scheme.
26/// The binary header format is defined by Bincode.
27pub const VERSION_2: u16 = 2;
28
29/// Deprecated alias for [`VERSION_0`].
30#[deprecated(note = "the name is one higher than the wire value it holds; use VERSION_0")]
31pub const VERSION_V1: u16 = VERSION_0;
32
33/// Deprecated alias for [`VERSION_1`].
34#[deprecated(note = "the name is one higher than the wire value it holds; use VERSION_1")]
35pub const VERSION_V2: u16 = VERSION_1;
36
37/// Deprecated alias for [`VERSION_2`].
38#[deprecated(note = "the name is one higher than the wire value it holds; use VERSION_2")]
39pub const VERSION_V3: u16 = VERSION_2;
40
41/// The size of the tag with which all PostGuard bytestreams begin.
42pub const PRELUDE_SIZE: usize = 4;
43
44/// The tag bytes with which all PostGuard bytestreams begin.
45pub const PRELUDE: [u8; PRELUDE_SIZE] = [0x14, 0x8A, 0x8E, 0xA7];
46
47/// The size of the version identifier.
48pub const VERSION_SIZE: usize = core::mem::size_of::<u16>();
49
50/// The size of the header size.
51pub const HEADER_SIZE_SIZE: usize = core::mem::size_of::<u32>();
52
53/// The size of the signature size.
54pub const SIG_SIZE_SIZE: usize = core::mem::size_of::<u32>();
55
56/// The size of the policy size.
57pub const POL_SIZE_SIZE: usize = core::mem::size_of::<u32>();
58
59/// The maximum size of the header (1 MiB).
60pub const MAX_HEADER_SIZE: usize = 1024 * 1024;
61
62/// The maximum size of the serialized header signature (8 KiB).
63///
64/// A serialized header signature holds a single signature plus its policy,
65/// which comfortably fits in a few KiB. Bounding the serialized
66/// header-signature length to a sane maximum before allocation mirrors the
67/// `MAX_HEADER_SIZE` check applied to the preamble.
68pub const MAX_SIG_SIZE: usize = 8 * 1024;
69
70/// The maximum size of symmetric segments (4 MiB).
71pub const MAX_SYMMETRIC_CHUNK_SIZE: u32 = 1024 * 1024 * 4;
72
73/// The preamble contains the following bytes:
74/// * Prelude: 4 bytes,
75/// * Version identifier: 2 bytes,
76/// * Size of header: 4 bytes,
77/// * Totalling: 4 + 2 + 4 = 10 bytes.
78pub const PREAMBLE_SIZE: usize = PRELUDE_SIZE + VERSION_SIZE + HEADER_SIZE_SIZE;
79
80/// Default size of symmetric encryption segments, if in streaming mode.
81///
82/// A reasonable default is 256 KiB.
83pub const SYMMETRIC_CRYPTO_DEFAULT_CHUNK: u32 = 256 * 1024;
84
85// Symmetric crypto constants.
86// This library uses AES128 because BLS12-381 is only secure up to around 120 bits.
87
88/// Size of the symmetric key.
89pub const KEY_SIZE: usize = 16;
90
91/// Size of the initialization vector.
92pub const IV_SIZE: usize = 12;
93
94// The STREAM construction needs only 12 bytes:
95// A 7-byte nonce, a 4-byte counter (u32) and an all-zero or all-one byte,
96// depending on if the segment is the final segment.
97
98/// Size of the nonce in the "STREAM" encryption construction.
99pub const STREAM_NONCE_SIZE: usize = 7;
100
101/// Size of the authentication tag.
102/// The authentication tag is appended to each segment.
103pub const TAG_SIZE: usize = 16;