|
| 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.h" |
| 29 | + |
| 30 | +#if defined(MBEDTLS_HAVE_TIME) || defined(MBEDTLS_HAVE_TIME_DATE) |
| 31 | +#include "rtc.h" |
| 32 | +#include "shared/timeutils/timeutils.h" |
| 33 | +#endif |
| 34 | + |
| 35 | +int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) { |
| 36 | + uint32_t val = 0; |
| 37 | + int n = 0; |
| 38 | + *olen = len; |
| 39 | + while (len--) { |
| 40 | + if (!n) { |
| 41 | + val = rng_read(); |
| 42 | + n = 4; |
| 43 | + } |
| 44 | + *output++ = val; |
| 45 | + val >>= 8; |
| 46 | + --n; |
| 47 | + } |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +#if defined(MBEDTLS_HAVE_TIME) |
| 52 | +time_t ra_rtctime_seconds(time_t *timer) { |
| 53 | + rtc_init_finalise(); |
| 54 | + RTC_TimeTypeDef time; |
| 55 | + RTC_DateTypeDef date; |
| 56 | + rtc_get_time(&time); |
| 57 | + rtc_get_date(&date); |
| 58 | + return timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds); |
| 59 | +} |
| 60 | +#endif |
| 61 | + |
| 62 | +#if defined(MBEDTLS_HAVE_TIME_DATE) |
| 63 | +struct tm *gmtime(const time_t *timep) { |
| 64 | + static struct tm tm; |
| 65 | + timeutils_struct_time_t tm_buf = {0}; |
| 66 | + timeutils_seconds_since_epoch_to_struct_time(*timep, &tm_buf); |
| 67 | + |
| 68 | + tm.tm_sec = tm_buf.tm_sec; |
| 69 | + tm.tm_min = tm_buf.tm_min; |
| 70 | + tm.tm_hour = tm_buf.tm_hour; |
| 71 | + tm.tm_mday = tm_buf.tm_mday; |
| 72 | + tm.tm_mon = tm_buf.tm_mon - 1; |
| 73 | + tm.tm_year = tm_buf.tm_year - 1900; |
| 74 | + tm.tm_wday = tm_buf.tm_wday; |
| 75 | + tm.tm_yday = tm_buf.tm_yday; |
| 76 | + tm.tm_isdst = -1; |
| 77 | + |
| 78 | + return &tm; |
| 79 | +} |
| 80 | +#endif |
0 commit comments