Skip to main content

arx_kw/
e.rs

1use crate::{lqb, util, ArxKW, ArxKwError, AuthTag, ConstantTimeEq, InvalidLengthError};
2
3/// The ARX-8-2-4-E variant. Has a key length of 48 bytes and a maximum input length of 64 bytes.
4/// See the [`ArxKW`] trait for usage.
5pub struct E;
6impl E {
7    #[must_use]
8    /// The length in bytes of the secret key used by this variant of ARX-KW
9    pub const fn key_length() -> usize {
10        48
11    }
12
13    #[must_use]
14    /// Returns the maximum length in bytes for the input to `E::encrypt` and `E::decrypt`
15    ///
16    /// ---
17    /// This is the same for the plaintext input when encrypting and ciphertext input when
18    /// decrypting, but is **not** the same for all variants of ARX-KW. Specifically, the *E* and *G*
19    /// variants are defined only for plaintext/ciphertext inputs of no more than 512 bits, the length
20    /// of a ChaCha Block, whereas *EX* and *GX* do not have this limitation.
21    pub const fn max_input_length() -> usize {
22        64
23    }
24}
25
26impl ArxKW for E {
27    type Key = [u8; Self::key_length()];
28    const VARIANT_ID: u8 = 1;
29    const MAX_INPUT_LENGTH: usize = 64;
30    fn encrypt(key: &Self::Key, plaintext: &[u8]) -> Result<(Vec<u8>, AuthTag), ArxKwError> {
31        if plaintext.len() > Self::max_input_length() {
32            Err(ArxKwError::InvalidLength(InvalidLengthError::UpTo(
33                plaintext.len(),
34                Self::max_input_length(),
35            )))
36        } else {
37            let (k1, k2) = array_refs![key, 16, 32];
38            let authentication_tag = util::sip_array_keyed(k1, plaintext);
39            let ciphertext = lqb::chacha8_encrypt(k2, authentication_tag.as_ref(), plaintext)?;
40            Ok((ciphertext, authentication_tag))
41        }
42    }
43
44    fn decrypt(
45        key: &Self::Key,
46        ciphertext: &[u8],
47        authentication_tag: &AuthTag,
48    ) -> Result<Vec<u8>, ArxKwError> {
49        if ciphertext.len() > Self::max_input_length() {
50            Err(ArxKwError::InvalidLength(InvalidLengthError::UpTo(
51                ciphertext.len(),
52                Self::max_input_length(),
53            )))
54        } else {
55            let (k1, k2) = array_refs![key, 16, 32];
56            let mut p_prime = zeroize::Zeroizing::new(lqb::chacha8_encrypt(
57                k2,
58                authentication_tag.as_ref(),
59                ciphertext,
60            )?);
61            let t_prime = zeroize::Zeroizing::new(util::sip_array_keyed(k1, &p_prime));
62            if bool::from(t_prime.ct_eq(authentication_tag)) {
63                // Compare AuthTags in constant time
64                return Ok(std::mem::take(&mut *p_prime));
65            }
66            Err(ArxKwError::BadTags)
67        }
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    extern crate hex;
74    use super::{ArxKW, E};
75    use crate::{assert_ct_eq, AuthTag};
76    use anyhow::Result;
77    use hex::FromHex;
78
79    /*
80     * Tests using the Test Vectors provided in the ARX-KW paper 2020-059 by Sato Shinichi
81     */
82
83    #[test]
84    fn test_encrypt() -> Result<()> {
85        let k = <[u8; 48]>::from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f")?;
86        let p = <[u8; 32]>::from_hex(
87            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
88        )?;
89        let p_bad = [0xaf; 69];
90        let res = E::encrypt(&k, &p_bad);
91        assert!(res.is_err());
92        let t_expected = AuthTag(<[u8; 16]>::from_hex("c4f21d3b4dbcc566c3a73bbc59790f2f")?);
93        let c_expected = <[u8; 32]>::from_hex(
94            "e6457d24abaf7c2ebdb91416a18366d31a66db61a4e45c9f42a119c353bb1eb1",
95        )?;
96        let (c, t) = E::encrypt(&k, &p)?;
97        assert_eq!(&c, &c_expected);
98        assert_ct_eq!(t, &t_expected);
99        Ok(())
100    }
101
102    #[test]
103    fn test_encrypt_blob() -> Result<()> {
104        let k = <[u8; 48]>::from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f")?;
105        let p = <[u8; 32]>::from_hex(
106            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
107        )?;
108        let blob_expected = <[u8; 48]>::from_hex("c4f21d3b4dbcc566c3a73bbc59790f2fe6457d24abaf7c2ebdb91416a18366d31a66db61a4e45c9f42a119c353bb1eb1")?;
109        let blob = E::encrypt_blob(&k, &p)?;
110        assert_ct_eq!(blob, &blob_expected);
111        Ok(())
112    }
113
114    #[test]
115    fn test_decrypt() -> Result<()> {
116        let c = <[u8; 32]>::from_hex(
117            "e6457d24abaf7c2ebdb91416a18366d31a66db61a4e45c9f42a119c353bb1eb1",
118        )?;
119        let k = <[u8; 48]>::from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f")?;
120        let t = AuthTag(<[u8; 16]>::from_hex("c4f21d3b4dbcc566c3a73bbc59790f2f")?);
121        let p_expected = <[u8; 32]>::from_hex(
122            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
123        )?;
124        let p = E::decrypt(&k, &c, &t)?;
125        assert_ct_eq!(p, &p_expected);
126        Ok(())
127    }
128
129    #[test]
130    fn test_decrypt_blob() -> Result<()> {
131        let k = <[u8; 48]>::from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f")?;
132        let blob = <[u8; 48]>::from_hex("c4f21d3b4dbcc566c3a73bbc59790f2fe6457d24abaf7c2ebdb91416a18366d31a66db61a4e45c9f42a119c353bb1eb1")?;
133        let p_expected = <[u8; 32]>::from_hex(
134            "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
135        )?;
136        let p = E::decrypt_blob(&k, &blob)?;
137        assert_ct_eq!(p, &p_expected);
138        Ok(())
139    }
140
141    #[test]
142    fn test_decrypt_bad() -> Result<()> {
143        let k = <[u8; 48]>::from_hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f")?;
144        let c = <[u8; 32]>::from_hex(
145            "e6457d24abaf7c2ebdb91416a18366d31a66db61a4e45c9f42a119c353bb1eb1",
146        )?;
147        let c_bad = [0xbb; 69];
148        let t = AuthTag(<[u8; 16]>::from_hex("c4f21d3b4dbcc566c3a73bbc59790f2f")?); // good tag
149        let res = E::decrypt(&k, &c_bad, &t);
150        assert!(res.is_err());
151        let t_bad = AuthTag(<[u8; 16]>::from_hex("c4f21d3b4dbcc566c3a73bbc59790fff")?); // last 2 hex digits should be 2f
152        let res = E::decrypt(&k, &c, &t_bad);
153        assert!(res.is_err());
154        Ok(())
155    }
156}