Skip to main content

arx_kw/
gx.rs

1use crate::ConstantTimeEq;
2use crate::{generate, util, ArxKW, ArxKwError, AuthTag};
3
4/// The ARX-8-2-4-GX variant. Has a key length of 32 bytes and a maximum input length of one MiB.
5/// See the [`ArxKW`] trait for usage.
6pub struct GX;
7impl GX {
8    /// The length in bytes of the secret key used by this variant of ARX-KW
9    #[must_use]
10    pub const fn key_length() -> usize {
11        32
12    }
13}
14
15impl ArxKW for GX {
16    type Key = [u8; Self::key_length()];
17    const VARIANT_ID: u8 = 4;
18    fn encrypt(key: &Self::Key, plaintext: &[u8]) -> Result<(Vec<u8>, AuthTag), ArxKwError> {
19        crate::memory::check_length(plaintext.len(), Self::MAX_INPUT_LENGTH)?;
20        let (k1, k2) = generate::subkeys(key)?;
21        let authentication_tag = util::sip_array_keyed(&k1, plaintext);
22        let nonce = construct_nonce(&authentication_tag);
23        let mut stream = util::xchacha8::new(&k2, &nonce);
24        let mut ciphertext = crate::memory::copy_secret(plaintext)?;
25        stream.xor_read(&mut ciphertext)?;
26        Ok((std::mem::take(&mut *ciphertext), authentication_tag))
27    }
28    fn decrypt(
29        key: &Self::Key,
30        ciphertext: &[u8],
31        authentication_tag: &AuthTag,
32    ) -> Result<Vec<u8>, ArxKwError> {
33        crate::memory::check_length(ciphertext.len(), Self::MAX_INPUT_LENGTH)?;
34        let (k1, k2) = generate::subkeys(key)?;
35        let nonce = construct_nonce(authentication_tag);
36        let mut stream = util::xchacha8::new(&k2, &nonce);
37        let mut p_prime = crate::memory::copy_secret(ciphertext)?;
38        stream.xor_read(&mut p_prime)?;
39        let t_prime = zeroize::Zeroizing::new(util::sip_array_keyed(&k1, &p_prime));
40        if bool::from(t_prime.ct_eq(authentication_tag)) {
41            // Equality check is done in constant time
42            return Ok(std::mem::take(&mut *p_prime));
43        }
44        Err(ArxKwError::BadTags)
45    }
46}
47/// The prefix specified for the GX variant in the ARX-KW paper (61 72 62 69 74 72 47 58) followed by 16 zeros
48const NONCE_INIT_GX: [u8; 24] = [
49    0x61, 0x72, 0x62, 0x69, 0x74, 0x72, 0x47, 0x58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50];
51
52/// Builds the 24-byte XChaCha8 nonce for ARX-KW-8-2-4-GX.
53///
54/// Concatenates the eight-byte ASCII prefix `arbitrGX` (`0x6172626974724758`)
55/// with the 16-byte authentication tag. Encryption and decryption call this
56/// internally; applications normally only need the [`ArxKW`] methods.
57///
58/// ```
59/// # use arx_kw::gx::construct_nonce;
60/// # use arx_kw::AuthTag;
61/// let mut t = AuthTag([0u8; 16]);
62/// for i in (0u8..16u8) {
63///     t.0[i as usize] = i;
64/// }
65/// // t contains [0x0, 0x1, 0x2 .. 0xf]
66///
67/// let nonce = construct_nonce(&t);
68/// assert_eq!(nonce,
69/// [0x61,0x72,0x62,0x69,0x74,0x72,0x47,0x58,0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf]);
70/// ```
71/// Using the authentication tag (T) and resulting nonce (N) for GX from the test vectors included in the ARX-KW paper:
72/// ```
73///  extern crate hex;
74/// # extern crate anyhow;
75///  use hex::FromHex;
76/// # use arx_kw::{
77///     AuthTag,
78///     gx::construct_nonce,
79/// };
80///
81/// # fn main() -> anyhow::Result<()> {
82/// let authentication_tag = AuthTag(<[u8;16]>::from_hex("016325cf6a3c4b2e3b039675e1ccbc65")?);
83/// let nonce_expected = <[u8;24]>::from_hex("6172626974724758016325cf6a3c4b2e3b039675e1ccbc65")?;
84/// let nonce = construct_nonce(&authentication_tag);
85/// assert_eq!(nonce,nonce_expected);
86/// # Ok(())
87/// # }
88/// ```
89#[must_use]
90#[inline]
91pub fn construct_nonce(authentication_tag: &AuthTag) -> [u8; 24] {
92    // Initialize nonce with the defined prefix followed by 16 zeros.
93    let mut nonce = NONCE_INIT_GX;
94    // Copy the contents of t into bytes 8 to 23 of nonce
95    nonce[8..24].clone_from_slice(authentication_tag.as_ref());
96    nonce
97}
98
99#[cfg(test)]
100mod tests {
101    extern crate hex;
102    use super::*;
103    use crate::assert_ct_eq;
104    use anyhow::Result;
105    use hex::FromHex;
106
107    #[test]
108    fn test_encrypt() -> Result<()> {
109        let k = <[u8; 32]>::from_hex(
110            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
111        )?;
112        let p = <[u8; 32]>::from_hex(
113            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
114        )?;
115        let t_expected = AuthTag(<[u8; 16]>::from_hex("016325cf6a3c4b2e3b039675e1ccbc65")?);
116        let c_expected = <[u8; 32]>::from_hex(
117            "2f83f391c97f3606ccd5709c6ee15d66cd7e65a2aeb7dc3066636e8f6b0d39c3",
118        )?;
119        let (c, t) = GX::encrypt(&k, &p)?;
120        assert_eq!(&c, &c_expected);
121        assert_ct_eq!(t, &t_expected);
122        Ok(())
123    }
124
125    #[test]
126    fn test_encrypt_blob() -> Result<()> {
127        let k = <[u8; 32]>::from_hex(
128            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
129        )?;
130        let p = <[u8; 32]>::from_hex(
131            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
132        )?;
133        let blob_expected = <[u8; 48]>::from_hex("016325cf6a3c4b2e3b039675e1ccbc652f83f391c97f3606ccd5709c6ee15d66cd7e65a2aeb7dc3066636e8f6b0d39c3")?;
134        let blob = GX::encrypt_blob(&k, &p)?;
135        assert_ct_eq!(blob, &blob_expected);
136        Ok(())
137    }
138
139    #[test]
140    fn test_decrypt_blob() -> Result<()> {
141        let k = <[u8; 32]>::from_hex(
142            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
143        )?;
144        let p_expected = <[u8; 32]>::from_hex(
145            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
146        )?;
147        let blob= <[u8; 48]>::from_hex("016325cf6a3c4b2e3b039675e1ccbc652f83f391c97f3606ccd5709c6ee15d66cd7e65a2aeb7dc3066636e8f6b0d39c3")?;
148        let p = GX::decrypt_blob(&k, &blob)?;
149        assert_ct_eq!(&p, &p_expected);
150        Ok(())
151    }
152
153    #[test]
154    fn test_decrypt() -> Result<()> {
155        let k = <[u8; 32]>::from_hex(
156            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
157        )?;
158        let t = AuthTag(<[u8; 16]>::from_hex("016325cf6a3c4b2e3b039675e1ccbc65")?);
159        let c = <[u8; 32]>::from_hex(
160            "2f83f391c97f3606ccd5709c6ee15d66cd7e65a2aeb7dc3066636e8f6b0d39c3",
161        )?;
162        let p_expected = <[u8; 32]>::from_hex(
163            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
164        )?;
165        let p = GX::decrypt(&k, &c, &t)?;
166        assert_eq!(p, p_expected);
167        Ok(())
168    }
169
170    /// Make sure that a bad authentication tag yields an error when decrypting
171    #[test]
172    fn test_bad_decrypt() -> Result<()> {
173        let k = <[u8; 32]>::from_hex(
174            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
175        )?;
176        let c = <[u8; 32]>::from_hex(
177            "2283f391c97f3606ccd5709c6ee15d66cd7e65a2aeb7dc3066636e8f6b0d39c3",
178        )?; // First two hex digits should be 2f instead of 22
179        let t_bad = AuthTag(<[u8; 16]>::from_hex("016325cf6a3c4b2e3b039675e1ccbc65")?);
180        let res = GX::decrypt(&k, &c, &t_bad);
181        assert!(res.is_err());
182        Ok(())
183    }
184}