| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2023 Damien P. George |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
| 10 |
* in the Software without restriction, including without limitation the rights |
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
* THE SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "rng.h" |
| 28 |
#include "mbedtls_config_port.h" |
| 29 |
|
| 30 |
#if defined(MBEDTLS_HAVE_TIME) || defined(MBEDTLS_HAVE_TIME_DATE) |
| 31 |
#include "rtc.h" |
| 32 |
#include "shared/timeutils/timeutils.h" |
| 33 |
#include "mbedtls/platform_time.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) { |
| 37 |
uint32_t val = 0; |
| 38 |
int n = 0; |
| 39 |
*olen = len; |
| 40 |
while (len--) { |
| 41 |
if (!n) { |
| 42 |
val = rng_read(); |
| 43 |
n = 4; |
| 44 |
} |
| 45 |
*output++ = val; |
| 46 |
val >>= 8; |
| 47 |
--n; |
| 48 |
} |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
#if defined(MBEDTLS_HAVE_TIME) |
| 53 |
time_t ra_rtctime_seconds(time_t *timer) { |
| 54 |
rtc_init_finalise(); |
| 55 |
RTC_TimeTypeDef time; |
| 56 |
RTC_DateTypeDef date; |
| 57 |
rtc_get_time(&time); |
| 58 |
rtc_get_date(&date); |
| 59 |
return timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds); |
| 60 |
} |
| 61 |
|
| 62 |
mbedtls_ms_time_t mbedtls_ms_time(void) { |
| 63 |
time_t *tv = NULL; |
| 64 |
mbedtls_ms_time_t current_ms; |
| 65 |
current_ms = ra_rtctime_seconds(tv) * 1000; |
| 66 |
return current_ms; |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
#if defined(MBEDTLS_HAVE_TIME_DATE) |
| 71 |
struct tm *gmtime(const time_t *timep) { |
| 72 |
static struct tm tm; |
| 73 |
timeutils_struct_time_t tm_buf = {0}; |
| 74 |
timeutils_seconds_since_epoch_to_struct_time(*timep, &tm_buf); |
| 75 |
|
| 76 |
tm.tm_sec = tm_buf.tm_sec; |
| 77 |
tm.tm_min = tm_buf.tm_min; |
| 78 |
tm.tm_hour = tm_buf.tm_hour; |
| 79 |
tm.tm_mday = tm_buf.tm_mday; |
| 80 |
tm.tm_mon = tm_buf.tm_mon - 1; |
| 81 |
tm.tm_year = tm_buf.tm_year - 1900; |
| 82 |
tm.tm_wday = tm_buf.tm_wday; |
| 83 |
tm.tm_yday = tm_buf.tm_yday; |
| 84 |
tm.tm_isdst = -1; |
| 85 |
|
| 86 |
return &tm; |
| 87 |
} |
| 88 |
#endif |