Skip to main content

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::Cow;
20    pub(crate) use rust_alloc::borrow::ToOwned;
21    pub(crate) use rust_alloc::boxed::Box;
22    pub(crate) use rust_alloc::string::String;
23    pub(crate) use rust_alloc::string::ToString;
24    pub(crate) use rust_alloc::sync::Arc;
25    pub(crate) use rust_alloc::vec::Vec;
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", target_env = "fortanixvme"))]
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", target_env = "fortanixvme"))]
43#[doc(hidden)]
44#[no_mangle]
45// needs to be pub for global visibility
46pub extern "C" fn mbedtls_internal_aes_encrypt(
47    _ctx: *mut mbedtls_sys::types::raw_types::c_void,
48    _input: *const u8,
49    _output: *mut u8,
50) -> i32 {
51    panic!("AES-NI support is forced but the T-tables code was invoked")
52}
53
54#[cfg(any(feature = "force_aesni_support", target_env = "sgx", target_env = "fortanixvme"))]
55#[doc(hidden)]
56#[no_mangle]
57// needs to be pub for global visibility
58pub extern "C" fn mbedtls_internal_aes_decrypt(
59    _ctx: *mut mbedtls_sys::types::raw_types::c_void,
60    _input: *const u8,
61    _output: *mut u8,
62) -> i32 {
63    panic!("AES-NI support is forced but the T-tables code was invoked")
64}
65
66#[cfg(any(all(feature = "time", feature = "custom_gmtime_r"), sys_time_component = "custom"))]
67#[doc(hidden)]
68#[no_mangle]
69// needs to be pub for global visibility
70pub unsafe extern "C" fn mbedtls_platform_gmtime_r(
71    tt: *const mbedtls_sys::types::time_t,
72    tp: *mut mbedtls_sys::types::tm,
73) -> *mut mbedtls_sys::types::tm {
74    use chrono::prelude::*;
75
76    //0 means no TZ offset
77    let naive = if tp.is_null() {
78        return core::ptr::null_mut();
79    } else {
80        match NaiveDateTime::from_timestamp_opt(*tt, 0) {
81            Some(t) => t,
82            None => return core::ptr::null_mut(),
83        }
84    };
85    let utc = DateTime::<Utc>::from_utc(naive, Utc);
86
87    let tp = &mut *tp;
88    tp.tm_sec = utc.second() as i32;
89    tp.tm_min = utc.minute() as i32;
90    tp.tm_hour = utc.hour() as i32;
91    tp.tm_mday = utc.day() as i32;
92    tp.tm_mon = utc.month0() as i32;
93    tp.tm_year = match (utc.year() as i32).checked_sub(1900) {
94        Some(year) => year,
95        None => return core::ptr::null_mut(),
96    };
97    tp.tm_wday = utc.weekday().num_days_from_sunday() as i32;
98    tp.tm_yday = utc.ordinal0() as i32;
99    tp.tm_isdst = 0;
100
101    tp
102}
103
104#[cfg(any(all(feature = "time", feature = "custom_time"), sys_time_component = "custom"))]
105#[doc(hidden)]
106#[no_mangle]
107// needs to be pub for global visibility
108pub unsafe extern "C" fn mbedtls_time(tp: *mut mbedtls_sys::types::time_t) -> mbedtls_sys::types::time_t {
109    let timestamp = chrono::Utc::now().timestamp() as mbedtls_sys::types::time_t;
110    if !tp.is_null() {
111        *tp = timestamp;
112    }
113    timestamp
114}