mbedtls_platform_support/
lib.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#![cfg_attr(not(feature = "std"), no_std)]
10
11#[cfg(not(feature = "std"))]
12#[allow(unused)]
13#[macro_use]
14extern crate alloc as rust_alloc;
15
16#[cfg(not(feature = "std"))]
17mod alloc_prelude {
18    #![allow(unused)]
19    pub(crate) use rust_alloc::borrow::ToOwned;
20    pub(crate) use rust_alloc::boxed::Box;
21    pub(crate) use rust_alloc::sync::Arc;
22    pub(crate) use rust_alloc::string::String;
23    pub(crate) use rust_alloc::string::ToString;
24    pub(crate) use rust_alloc::vec::Vec;
25    pub(crate) use rust_alloc::borrow::Cow;
26}
27
28pub mod self_test;
29
30#[cfg(any(feature = "spin_threading", feature = "rust_threading", sys_threading_component = "custom"))]
31#[doc(hidden)]
32pub mod threading;
33
34#[cfg(any(feature = "force_aesni_support", target_env = "sgx"))]
35#[doc(hidden)]
36#[no_mangle]
37// needs to be pub for global visibility
38pub extern "C" fn mbedtls_aesni_has_support(_what: u32) -> i32 {
39    return 1;
40}
41
42#[cfg(any(feature = "force_aesni_support", target_env = "sgx"))]
43#[doc(hidden)]
44#[no_mangle]
45// needs to be pub for global visibility
46pub extern "C" fn mbedtls_internal_aes_encrypt(_ctx: *mut mbedtls_sys::types::raw_types::c_void,
47                                                _input: *const u8,
48                                                _output: *mut u8) -> i32 {
49    panic!("AES-NI support is forced but the T-tables code was invoked")
50}
51
52#[cfg(any(feature = "force_aesni_support", target_env = "sgx"))]
53#[doc(hidden)]
54#[no_mangle]
55// needs to be pub for global visibility
56pub extern "C" fn mbedtls_internal_aes_decrypt(_ctx: *mut mbedtls_sys::types::raw_types::c_void,
57                                                _input: *const u8,
58                                                _output: *mut u8) -> i32 {
59    panic!("AES-NI support is forced but the T-tables code was invoked")
60}
61
62
63#[cfg(any(all(feature = "time", feature = "custom_gmtime_r"), sys_time_component = "custom"))]
64#[doc(hidden)]
65#[no_mangle]
66// needs to be pub for global visibility
67pub unsafe extern "C" fn mbedtls_platform_gmtime_r(tt: *const mbedtls_sys::types::time_t, tp: *mut mbedtls_sys::types::tm) -> *mut mbedtls_sys::types::tm {
68    use chrono::prelude::*;
69
70    //0 means no TZ offset
71    let naive = if tp.is_null() {
72        return core::ptr::null_mut()
73    } else {
74        match NaiveDateTime::from_timestamp_opt(*tt, 0) {
75            Some(t) => t,
76            None => return core::ptr::null_mut()
77        }
78    };
79    let utc = DateTime::<Utc>::from_utc(naive, Utc);
80
81    let tp = &mut *tp;
82    tp.tm_sec   = utc.second()   as i32;
83    tp.tm_min   = utc.minute()   as i32;
84    tp.tm_hour  = utc.hour()     as i32;
85    tp.tm_mday  = utc.day()      as i32;
86    tp.tm_mon   = utc.month0()   as i32;
87    tp.tm_year  = match (utc.year() as i32).checked_sub(1900) {
88        Some(year) => year,
89        None => return core::ptr::null_mut()
90    };
91    tp.tm_wday  = utc.weekday().num_days_from_sunday() as i32;
92    tp.tm_yday  = utc.ordinal0() as i32;
93    tp.tm_isdst = 0;
94
95    tp
96}
97
98#[cfg(any(all(feature = "time", feature = "custom_time"), sys_time_component = "custom"))]
99#[doc(hidden)]
100#[no_mangle]
101// needs to be pub for global visibility
102pub unsafe extern "C" fn mbedtls_time(tp: *mut mbedtls_sys::types::time_t) -> mbedtls_sys::types::time_t {
103    let timestamp = chrono::Utc::now().timestamp() as mbedtls_sys::types::time_t;
104    if !tp.is_null() {
105        *tp = timestamp;
106    }
107    timestamp
108}