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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::{ffi::CStr, ptr, result, str::Utf8Error};

use bitflags::bitflags;
use cstr_argument::CStrArgument;
use ffi;
use libc::c_int;

use crate::{error::return_err, NonNull, Result};

ffi_enum_wrapper! {
    #[allow(non_camel_case_types)]
    pub enum Algorithm: c_int {
        Idea             = ffi::GCRY_CIPHER_IDEA,
        TripleDes        = ffi::GCRY_CIPHER_3DES,
        Cast5            = ffi::GCRY_CIPHER_CAST5,
        Blowfish         = ffi::GCRY_CIPHER_BLOWFISH,
        SaferSk128       = ffi::GCRY_CIPHER_SAFER_SK128,
        DesSk            = ffi::GCRY_CIPHER_DES_SK,
        Aes              = ffi::GCRY_CIPHER_AES,
        Aes128           = ffi::GCRY_CIPHER_AES128,
        Aes192           = ffi::GCRY_CIPHER_AES192,
        Aes256           = ffi::GCRY_CIPHER_AES256,
        Rijndael         = ffi::GCRY_CIPHER_RIJNDAEL,
        Rijndael128      = ffi::GCRY_CIPHER_RIJNDAEL128,
        Rijndael192      = ffi::GCRY_CIPHER_RIJNDAEL192,
        Rijndael256      = ffi::GCRY_CIPHER_RIJNDAEL256,
        Twofish          = ffi::GCRY_CIPHER_TWOFISH,
        Arcfour          = ffi::GCRY_CIPHER_ARCFOUR,
        Des              = ffi::GCRY_CIPHER_DES,
        Twofish128       = ffi::GCRY_CIPHER_TWOFISH128,
        Serpent128       = ffi::GCRY_CIPHER_SERPENT128,
        Serpent192       = ffi::GCRY_CIPHER_SERPENT192,
        Serpent256       = ffi::GCRY_CIPHER_SERPENT256,
        Rfc2268_40       = ffi::GCRY_CIPHER_RFC2268_40,
        Rfc2268_128      = ffi::GCRY_CIPHER_RFC2268_128,
        Seed             = ffi::GCRY_CIPHER_SEED,
        Camellia128      = ffi::GCRY_CIPHER_CAMELLIA128,
        Camellia192      = ffi::GCRY_CIPHER_CAMELLIA192,
        Camellia256      = ffi::GCRY_CIPHER_CAMELLIA256,
        Salsa20          = ffi::GCRY_CIPHER_SALSA20,
        Salsa20r12       = ffi::GCRY_CIPHER_SALSA20R12,
        Gost28147        = ffi::GCRY_CIPHER_GOST28147,
        Chacha20         = ffi::GCRY_CIPHER_CHACHA20,
    }
}

impl Algorithm {
    #[inline]
    pub fn from_name(name: impl CStrArgument) -> Option<Algorithm> {
        let name = name.into_cstr();
        let result = unsafe { ffi::gcry_cipher_map_name(name.as_ref().as_ptr()) };
        if result != 0 {
            unsafe { Some(Algorithm::from_raw(result)) }
        } else {
            None
        }
    }

    #[inline]
    pub fn is_available(&self) -> bool {
        let _ = crate::init_default();
        unsafe { ffi::gcry_cipher_test_algo(self.raw()) == 0 }
    }

    #[inline]
    pub fn name(&self) -> result::Result<&'static str, Option<Utf8Error>> {
        self.name_raw()
            .map_or(Err(None), |s| s.to_str().map_err(Some))
    }

    #[inline]
    pub fn name_raw(&self) -> Option<&'static CStr> {
        unsafe {
            ffi::gcry_cipher_algo_name(self.raw())
                .as_ref()
                .map(|s| CStr::from_ptr(s))
        }
    }

    #[inline]
    pub fn key_len(&self) -> usize {
        unsafe { ffi::gcry_cipher_get_algo_keylen(self.raw()) }
    }

    #[inline]
    pub fn block_len(&self) -> usize {
        unsafe { ffi::gcry_cipher_get_algo_blklen(self.raw()) }
    }
}

ffi_enum_wrapper! {
    pub enum Mode: c_int {
        Ecb      = ffi::GCRY_CIPHER_MODE_ECB,
        Cfb      = ffi::GCRY_CIPHER_MODE_CFB,
        Cbc      = ffi::GCRY_CIPHER_MODE_CBC,
        Stream   = ffi::GCRY_CIPHER_MODE_STREAM,
        Ofb      = ffi::GCRY_CIPHER_MODE_OFB,
        Ctr      = ffi::GCRY_CIPHER_MODE_CTR,
        AesWrap  = ffi::GCRY_CIPHER_MODE_AESWRAP,
        Ccm      = ffi::GCRY_CIPHER_MODE_CCM,
        Gcm      = ffi::GCRY_CIPHER_MODE_GCM,
        Poly1305 = ffi::GCRY_CIPHER_MODE_POLY1305,
        Ocb      = ffi::GCRY_CIPHER_MODE_OCB,
        Cfb8     = ffi::GCRY_CIPHER_MODE_CFB8,
        Xts      = ffi::GCRY_CIPHER_MODE_XTS,
    }
}

impl Mode {
    #[inline]
    pub fn from_oid(name: impl CStrArgument) -> Option<Mode> {
        let name = name.into_cstr();
        let result = unsafe { ffi::gcry_cipher_mode_from_oid(name.as_ref().as_ptr()) };
        if result != 0 {
            unsafe { Some(Mode::from_raw(result)) }
        } else {
            None
        }
    }
}

bitflags! {
    pub struct Flags: ffi::gcry_cipher_flags {
        const NONE       = 0;
        const SECURE      = ffi::GCRY_CIPHER_SECURE;
        const ENABLE_SYNC = ffi::GCRY_CIPHER_ENABLE_SYNC;
        const CBC_CTS     = ffi::GCRY_CIPHER_CBC_CTS;
        const CBC_MAC     = ffi::GCRY_CIPHER_CBC_MAC;
    }
}

#[derive(Debug)]
pub struct Cipher(NonNull<ffi::gcry_cipher_hd_t>);

impl Drop for Cipher {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            ffi::gcry_cipher_close(self.as_raw());
        }
    }
}

impl Cipher {
    impl_wrapper!(Cipher: ffi::gcry_cipher_hd_t);

    #[inline]
    pub fn new(algo: Algorithm, mode: Mode) -> Result<Cipher> {
        Cipher::with_flags(algo, mode, Flags::NONE)
    }

    #[inline]
    pub fn with_flags(algo: Algorithm, mode: Mode, flags: Flags) -> Result<Cipher> {
        let _ = crate::init_default();
        unsafe {
            let mut handle: ffi::gcry_cipher_hd_t = ptr::null_mut();
            return_err!(ffi::gcry_cipher_open(
                &mut handle,
                algo.raw(),
                mode.raw(),
                flags.bits()
            ));
            Ok(Cipher::from_raw(handle))
        }
    }

    #[inline]
    pub fn set_key(&mut self, key: impl AsRef<[u8]>) -> Result<()> {
        let key = key.as_ref();
        unsafe {
            return_err!(ffi::gcry_cipher_setkey(
                self.as_raw(),
                key.as_ptr().cast(),
                key.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn set_iv(&mut self, iv: impl AsRef<[u8]>) -> Result<()> {
        let iv = iv.as_ref();
        unsafe {
            return_err!(ffi::gcry_cipher_setiv(
                self.as_raw(),
                iv.as_ptr().cast(),
                iv.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn set_ctr(&mut self, ctr: impl AsRef<[u8]>) -> Result<()> {
        let ctr = ctr.as_ref();
        unsafe {
            return_err!(ffi::gcry_cipher_setctr(
                self.as_raw(),
                ctr.as_ptr().cast(),
                ctr.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn reset(&mut self) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_reset(self.as_raw()));
        }
        Ok(())
    }

    #[inline]
    pub fn authenticate(&mut self, bytes: &[u8]) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_authenticate(
                self.as_raw(),
                bytes.as_ptr().cast(),
                bytes.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn get_tag(&mut self, tag: &mut [u8]) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_gettag(
                self.as_raw(),
                tag.as_mut_ptr().cast(),
                tag.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn verify_tag(&mut self, tag: &[u8]) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_checktag(
                self.as_raw(),
                tag.as_ptr().cast(),
                tag.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn encrypt(&mut self, input: &[u8], output: &mut [u8]) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_encrypt(
                self.as_raw(),
                output.as_mut_ptr().cast(),
                output.len(),
                input.as_ptr().cast(),
                input.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn decrypt(&mut self, input: &[u8], output: &mut [u8]) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_decrypt(
                self.as_raw(),
                output.as_mut_ptr().cast(),
                output.len(),
                input.as_ptr().cast(),
                input.len()
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn encrypt_inplace(&mut self, buffer: &mut [u8]) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_encrypt(
                self.as_raw(),
                buffer.as_mut_ptr().cast(),
                buffer.len(),
                ptr::null(),
                0
            ));
        }
        Ok(())
    }

    #[inline]
    pub fn decrypt_inplace(&mut self, buffer: &mut [u8]) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_cipher_decrypt(
                self.as_raw(),
                buffer.as_mut_ptr().cast(),
                buffer.len(),
                ptr::null(),
                0
            ));
        }
        Ok(())
    }
}