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
//! ## Initialization
//! Libgcrypt requires initialization before first use. The functions `init` and `init_fips` can be
//! used to initialize the library. The closure passed to these functions is used to configure the
//! library. More information on configuration options can be found in the libgcrypt
//! [documentation](https://www.gnupg.org/documentation/manuals/gcrypt/Initializing-the-library.html#Initializing-the-library).
//!
//! An example:
//!
//! ```rust
//! let gcrypt = gcrypt::init(|x| {
//!     x.disable_secmem();
//!     Ok::<_, ()>(())
//! });
//! ```
//!
//! Calling any function in the wrapper that requires initialization before `init` or `init_fips`
//! are called will cause the wrapper to attempt to initialize the library with a default
//! configuration.
#![deny(missing_debug_implementations)]

use std::{
    ffi::CStr,
    ptr, result,
    sync::{
        atomic::{AtomicBool, Ordering},
        Mutex,
    },
};

use cstr_argument::CStrArgument;
use libc::c_int;
use once_cell::sync::Lazy;

use self::error::return_err;

pub use crate::{
    buffer::Buffer,
    error::{Error, Result},
};

pub use ffi::{require_gcrypt_ver, MIN_GCRYPT_VERSION};
pub use gpg_error as error;

#[macro_use]
mod utils;
pub mod buffer;
pub mod cipher;
pub mod digest;
pub mod kdf;
pub mod mac;
pub mod mpi;
pub mod pkey;
pub mod rand;
pub mod sexp;

type NonNull<T> = ptr::NonNull<<T as utils::Ptr>::Inner>;

static INITIALIZED: AtomicBool = AtomicBool::new(false);
static CONTROL_LOCK: Lazy<Mutex<()>> = Lazy::new(Mutex::default);

#[derive(Debug)]
pub struct Initializer(());

impl Initializer {
    #[inline]
    pub fn check_version(&mut self, version: impl CStrArgument) -> bool {
        let version = version.into_cstr();
        unsafe { !ffi::gcry_check_version(version.as_ref().as_ptr()).is_null() }
    }

    #[inline]
    pub fn enable_quick_random(&mut self) -> &mut Self {
        unsafe {
            ffi::gcry_control(ffi::GCRYCTL_ENABLE_QUICK_RANDOM, 0);
        }
        self
    }

    #[inline]
    pub fn enable_secure_rndpool(&mut self) -> &mut Self {
        unsafe {
            ffi::gcry_control(ffi::GCRYCTL_USE_SECURE_RNDPOOL, 0);
        }
        self
    }

    #[inline]
    pub fn disable_secmem(&mut self) -> &mut Self {
        unsafe {
            ffi::gcry_control(ffi::GCRYCTL_DISABLE_SECMEM, 0);
        }
        self
    }

    #[inline]
    pub fn enable_secmem(&mut self, amt: usize) -> Result<&mut Self> {
        unsafe {
            return_err!(ffi::gcry_control(ffi::GCRYCTL_INIT_SECMEM, amt as c_int));
        }
        Ok(self)
    }

    #[inline]
    pub fn run_self_tests(&mut self) -> Result<&mut Self> {
        unsafe {
            return_err!(ffi::gcry_control(ffi::GCRYCTL_SELFTEST, 0));
        }
        Ok(self)
    }
}

#[inline]
fn is_init_started() -> bool {
    unsafe { ffi::gcry_control(ffi::GCRYCTL_ANY_INITIALIZATION_P, 0) != 0 }
}

#[inline]
fn is_init_finished() -> bool {
    unsafe { ffi::gcry_control(ffi::GCRYCTL_INITIALIZATION_FINISHED_P, 0) != 0 }
}

pub fn enable_memory_guard() -> bool {
    let _lock = CONTROL_LOCK.lock().unwrap();
    let started = is_init_started();
    if !started {
        unsafe {
            ffi::gcry_control(ffi::GCRYCTL_ENABLE_M_GUARD, 0);
        }
    }
    !started
}

#[inline]
pub fn is_initialized() -> bool {
    if INITIALIZED.load(Ordering::Acquire) {
        return true;
    }
    let _lock = CONTROL_LOCK.lock();
    is_init_finished()
}

fn init_internal<E>(
    fips: bool, f: impl FnOnce(&mut Initializer) -> result::Result<(), E>,
) -> result::Result<Gcrypt, E> {
    if INITIALIZED.load(Ordering::Acquire) {
        return Ok(Gcrypt(()));
    }

    let _lock = CONTROL_LOCK.lock().unwrap();
    if !is_init_finished() {
        unsafe {
            if !is_init_started() {
                if cfg!(unix) {
                    ffi::gcry_control(
                        ffi::GCRYCTL_SET_THREAD_CBS,
                        ffi::gcry_threads_pthread_shim(),
                    );
                }
                if fips {
                    ffi::gcry_control(ffi::GCRYCTL_FORCE_FIPS_MODE, 0);
                }
            }
            assert!(
                !ffi::gcry_check_version(MIN_GCRYPT_VERSION.as_ptr().cast()).is_null(),
                "the library linked is not the correct version"
            );
        }
        f(&mut Initializer(()))?;
        unsafe {
            ffi::gcry_control(ffi::GCRYCTL_INITIALIZATION_FINISHED, 0);
        }
    }
    INITIALIZED.store(true, Ordering::Release);
    Ok(Gcrypt(()))
}

#[inline]
pub fn init<E>(
    f: impl FnOnce(&mut Initializer) -> result::Result<(), E>,
) -> result::Result<Gcrypt, E> {
    init_internal(false, f)
}

#[inline]
pub fn init_fips_mode<E>(
    f: impl FnOnce(&mut Initializer) -> result::Result<(), E>,
) -> result::Result<Gcrypt, E> {
    init_internal(true, f)
}

#[inline]
pub fn init_default() -> Gcrypt {
    let _ = init(|x| {
        x.enable_secure_rndpool().disable_secmem();
        Ok::<_, ()>(())
    });
    Gcrypt(())
}

#[derive(Debug, Copy, Clone)]
pub struct Gcrypt(());

impl Gcrypt {
    #[inline]
    pub fn is_fips_mode_active(&self) -> bool {
        unsafe { ffi::gcry_fips_mode_active() }
    }

    #[inline]
    pub fn check_version(&self, version: impl CStrArgument) -> bool {
        let version = version.into_cstr();
        unsafe { !ffi::gcry_check_version(version.as_ref().as_ptr()).is_null() }
    }

    #[inline]
    pub fn version(&self) -> &'static str {
        unsafe {
            CStr::from_ptr(ffi::gcry_check_version(ptr::null()))
                .to_str()
                .expect("Version is invalid")
        }
    }

    #[inline]
    pub fn run_self_tests(&self) -> Result<()> {
        unsafe {
            return_err!(ffi::gcry_control(ffi::GCRYCTL_SELFTEST, 0));
        }
        Ok(())
    }

    #[inline]
    pub fn destroy_secmem(&self) {
        unsafe {
            ffi::gcry_control(ffi::GCRYCTL_TERM_SECMEM, 0);
        }
    }
}