mbedtls_platform_support/
self_test.rs

1/* Copyright (c) Fortanix, Inc.
2 *
3 * Licensed under the GNU General Public License, version 2 <LICENSE-GPL or
4 * https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version
5 * 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your
6 * option. This file may not be copied, modified, or distributed except
7 * according to those terms. */
8
9//! MbedTLS self tests.
10//!
11//! Calling MbedTLS self test functions before they're enabled using the
12//! `enable()` function here will result in a panic.
13//!
14//! Using this module in multithreaded or async environment will fail. The self 
15//! test functions rely on global variables to track operations and anything 
16//! non-self-test related operations will clobber these variables, resulting in 
17//! self test failures. Make sure no other code uses MbedTLS while running the 
18//! self tests. Multiple self test operations done simultaneously may also 
19//! return failures.
20
21use mbedtls_sys::types::raw_types::{c_char, c_int};
22
23cfg_if::cfg_if! {
24    if #[cfg(feature = "std")] {
25        // needs to be pub for global visiblity
26        #[doc(hidden)]
27        #[no_mangle]
28        pub unsafe extern "C" fn mbedtls_log(msg: *const std::os::raw::c_char) {
29            print!("{}", std::ffi::CStr::from_ptr(msg).to_string_lossy());
30        }
31    } else {
32        #[allow(non_upper_case_globals)]
33        static mut log_f: Option<unsafe fn(*const c_char)> = None;
34
35        // needs to be pub for global visiblity
36        #[doc(hidden)]
37        #[no_mangle]
38        pub unsafe extern "C" fn mbedtls_log(msg: *const c_char) {
39            log_f.expect("Called self-test log without enabling self-test")(msg)
40        }
41    }
42}
43
44#[cfg(any(not(feature = "std"), target_env = "sgx"))]
45#[allow(non_upper_case_globals)]
46static mut rand_f: Option<fn() -> c_int> = None;
47
48// needs to be pub for global visiblity
49#[cfg(all(any(not(feature = "std"), target_env = "sgx"), not(target_env = "msvc")))]
50#[doc(hidden)]
51#[no_mangle]
52pub unsafe extern "C" fn rand() -> c_int {
53    rand_f.expect("Called self-test rand without enabling self-test")()
54}
55
56/// Set callback functions to enable the MbedTLS self tests.
57///
58/// `rand` only needs to be set on platforms that don't have a `rand()` 
59/// function in libc. `log` only needs to be set when using `no_std`, i.e. 
60/// the `std` feature of this create is not enabled. If neither function 
61/// needs to be set, you don't have to call `enable()`.
62///
63/// # Safety
64///
65/// The caller needs to ensure this function is not called while any other
66/// function in this module is called.
67#[allow(unused)]
68pub unsafe fn enable(rand: fn() -> c_int, log: Option<unsafe fn(*const c_char)>) {
69    #[cfg(any(not(feature = "std"), target_env = "sgx"))] {
70        rand_f = Some(rand);
71    }
72    #[cfg(not(feature = "std"))] {
73        log_f = log;
74    }
75}
76
77/// # Safety
78///
79/// The caller needs to ensure this function is not called while any other
80/// function in this module is called.
81pub unsafe fn disable() {
82    #[cfg(any(not(feature = "std"), target_env = "sgx"))] {
83        rand_f = None;
84    }
85    #[cfg(not(feature = "std"))] {
86        log_f = None;
87    }
88}
89
90/// # Safety
91/// 
92/// The caller needs to ensure this function is not called while *any other*
93/// MbedTLS function is called. See the module documentation for more
94/// information.
95pub use mbedtls_sys::{
96    aes_self_test as aes, arc4_self_test as arc4, aria_self_test as aria, base64_self_test as base64,
97    camellia_self_test as camellia, ccm_self_test as ccm, ctr_drbg_self_test as ctr_drbg,
98    des_self_test as des, dhm_self_test as dhm, ecjpake_self_test as ecjpake, ecp_self_test as ecp,
99    entropy_self_test as entropy, gcm_self_test as gcm, hmac_drbg_self_test as hmac_drbg,
100    md2_self_test as md2, md4_self_test as md4, md5_self_test as md5, mpi_self_test as mpi,
101    pkcs5_self_test as pkcs5, ripemd160_self_test as ripemd160, rsa_self_test as rsa,
102    sha1_self_test as sha1, sha256_self_test as sha256, sha512_self_test as sha512,
103    x509_self_test as x509, xtea_self_test as xtea, nist_kw_self_test as nist_kw, cmac_self_test as cmac
104};