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
pub use crypto2::aeadcipher::{
    Aes128Ccm, Aes128GcmSiv, Aes128OcbTag128, Aes192OcbTag128, Aes256Ccm, Aes256GcmSiv,
    Aes256OcbTag128, AesSivCmac256, AesSivCmac384, AesSivCmac512,
};
#[cfg(not(all(
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "arm",
        target_arch = "aarch64"
    ),
    feature = "ring"
)))]
pub use crypto2::aeadcipher::{Aes128Gcm, Aes256Gcm, Chacha20Poly1305};

#[cfg(all(
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "arm",
        target_arch = "aarch64"
    ),
    feature = "ring"
))]
pub use super::ring::{Aes128Gcm, Aes256Gcm, Chacha20Poly1305};
use super::CipherKind;

trait AeadCipherInner {
    fn ac_kind(&self) -> CipherKind;
    fn ac_key_len(&self) -> usize;
    fn ac_block_len(&self) -> usize;
    fn ac_n_min(&self) -> usize;
    fn ac_n_max(&self) -> usize;
    fn ac_tag_len(&self) -> usize;

    fn ac_encrypt_slice(&self, nonce: &[u8], plaintext_in_ciphertext_out: &mut [u8]);
    fn ac_decrypt_slice(&self, nonce: &[u8], ciphertext_in_plaintext_out: &mut [u8]) -> bool;
}

macro_rules! impl_aead_cipher {
    ($name:tt, $kind:tt) => {
        impl AeadCipherInner for $name {
            fn ac_kind(&self) -> CipherKind {
                CipherKind::$kind
            }
            fn ac_key_len(&self) -> usize {
                $name::KEY_LEN
            }
            fn ac_block_len(&self) -> usize {
                $name::BLOCK_LEN
            }
            fn ac_n_min(&self) -> usize {
                $name::N_MIN
            }
            fn ac_n_max(&self) -> usize {
                $name::N_MAX
            }
            fn ac_tag_len(&self) -> usize {
                $name::TAG_LEN
            }

            fn ac_encrypt_slice(&self, nonce: &[u8], plaintext_in_ciphertext_out: &mut [u8]) {
                self.encrypt_slice(nonce, &[], plaintext_in_ciphertext_out);
            }

            fn ac_decrypt_slice(
                &self,
                nonce: &[u8],
                ciphertext_in_plaintext_out: &mut [u8],
            ) -> bool {
                self.decrypt_slice(nonce, &[], ciphertext_in_plaintext_out)
            }
        }
    };
}

macro_rules! impl_siv_cmac_cipher {
    ($name:tt, $kind:tt) => {
        impl AeadCipherInner for $name {
            fn ac_kind(&self) -> CipherKind {
                CipherKind::$kind
            }
            fn ac_key_len(&self) -> usize {
                $name::KEY_LEN
            }
            fn ac_block_len(&self) -> usize {
                $name::BLOCK_LEN
            }
            fn ac_n_min(&self) -> usize {
                $name::N_MIN
            }
            fn ac_n_max(&self) -> usize {
                $name::N_MAX
            }
            fn ac_tag_len(&self) -> usize {
                $name::TAG_LEN
            }

            // NOTE: SIV-CMAC 模式,Nonce 在 AAD 数据的最后面。
            //       TAG 默认也在 PKT 的前面,为此我们这里需要手动把 TAG 数据放置在 密文的后面。
            fn ac_encrypt_slice(&self, nonce: &[u8], plaintext_in_ciphertext_out: &mut [u8]) {
                let len = plaintext_in_ciphertext_out.len();
                let plen = len - Self::TAG_LEN;
                let (plaintext, tag_out) = plaintext_in_ciphertext_out.split_at_mut(plen);
                self.encrypt_slice_detached(&[nonce], plaintext, tag_out);
            }

            fn ac_decrypt_slice(
                &self,
                nonce: &[u8],
                ciphertext_in_plaintext_out: &mut [u8],
            ) -> bool {
                let len = ciphertext_in_plaintext_out.len();
                let clen = len - Self::TAG_LEN;
                let (ciphertext, tag_in) = ciphertext_in_plaintext_out.split_at_mut(clen);
                self.decrypt_slice_detached(&[nonce], ciphertext, &tag_in)
            }
        }
    };
}

impl_aead_cipher!(Aes128Ccm, AES_128_CCM);
impl_aead_cipher!(Aes256Ccm, AES_256_CCM);
impl_aead_cipher!(Aes128Gcm, AES_128_GCM);
impl_aead_cipher!(Aes256Gcm, AES_256_GCM);

impl_aead_cipher!(Aes128GcmSiv, AES_128_GCM_SIV);
impl_aead_cipher!(Aes256GcmSiv, AES_256_GCM_SIV);

impl_aead_cipher!(Aes128OcbTag128, AES_128_OCB_TAGLEN128);
impl_aead_cipher!(Aes192OcbTag128, AES_192_OCB_TAGLEN128);
impl_aead_cipher!(Aes256OcbTag128, AES_256_OCB_TAGLEN128);

impl_aead_cipher!(Chacha20Poly1305, CHACHA20_POLY1305);

impl_siv_cmac_cipher!(AesSivCmac256, AES_SIV_CMAC_256);
impl_siv_cmac_cipher!(AesSivCmac384, AES_SIV_CMAC_384);
impl_siv_cmac_cipher!(AesSivCmac512, AES_SIV_CMAC_512);

pub struct AeadCipher {
    cipher: Box<dyn AeadCipherInner + Send + 'static>,
    nlen: usize,
    nonce: [u8; Self::N_MAX],
}

impl AeadCipher {
    const N_MAX: usize = 16;

    pub fn new(kind: CipherKind, key: &[u8]) -> Self {
        use self::CipherKind::*;

        let cipher: Box<dyn AeadCipherInner + Send + 'static> = match kind {
            AES_128_CCM => Box::new(Aes128Ccm::new(key)),
            AES_256_CCM => Box::new(Aes256Ccm::new(key)),
            AES_128_OCB_TAGLEN128 => Box::new(Aes128OcbTag128::new(key)),
            AES_192_OCB_TAGLEN128 => Box::new(Aes192OcbTag128::new(key)),
            AES_256_OCB_TAGLEN128 => Box::new(Aes256OcbTag128::new(key)),
            AES_128_GCM => Box::new(Aes128Gcm::new(key)),
            AES_256_GCM => Box::new(Aes256Gcm::new(key)),
            AES_SIV_CMAC_256 => Box::new(AesSivCmac256::new(key)),
            AES_SIV_CMAC_384 => Box::new(AesSivCmac384::new(key)),
            AES_SIV_CMAC_512 => Box::new(AesSivCmac512::new(key)),
            AES_128_GCM_SIV => Box::new(Aes128GcmSiv::new(key)),
            AES_256_GCM_SIV => Box::new(Aes256GcmSiv::new(key)),
            CHACHA20_POLY1305 => Box::new(Chacha20Poly1305::new(key)),
            _ => unreachable!(),
        };

        let nlen = std::cmp::min(cipher.ac_n_max(), Self::N_MAX);

        let nonce = [0u8; Self::N_MAX];

        Self {
            cipher,
            nonce,
            nlen,
        }
    }

    pub fn kind(&self) -> CipherKind {
        self.cipher.ac_kind()
    }

    pub fn tag_len(&self) -> usize {
        self.cipher.ac_tag_len()
    }

    #[inline]
    fn increase_nonce(&mut self) {
        let mut c = self.nonce[0] as u16 + 1;
        self.nonce[0] = c as u8;
        c >>= 8;
        let mut n = 1;
        while n < self.nlen {
            c += self.nonce[n] as u16;
            self.nonce[n] = c as u8;
            c >>= 8;
            n += 1;
        }
    }

    pub fn encrypt(&mut self, plaintext_in_ciphertext_out: &mut [u8]) {
        let nonce = &self.nonce[..self.nlen];
        self.cipher
            .ac_encrypt_slice(nonce, plaintext_in_ciphertext_out);
        self.increase_nonce();
    }

    pub fn decrypt(&mut self, ciphertext_in_plaintext_out: &mut [u8]) -> bool {
        let nonce = &self.nonce[..self.nlen];
        let ret = self
            .cipher
            .ac_decrypt_slice(nonce, ciphertext_in_plaintext_out);
        self.increase_nonce();
        ret
    }
}