1 /*
2  * wpa_supplicant/hostapd / Internal implementation of OS specific functions
3  * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * This file is an example of operating system specific  wrapper functions.
15  * This version implements many of the functions internally, so it can be used
16  * to fill in missing functions from the target system C libraries.
17  *
18  * Some of the functions are using standard C library calls in order to keep
19  * this file in working condition to allow the functions to be tested on a
20  * Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for
21  * this file to work correctly. Note that these implementations are only
22  * examples and are not optimized for speed.
23  */
24 
25 #include "os.h"
26 #include <stdlib.h>
27 #include "esp_system.h"
28 #include "utils/common.h"
29 #include <zephyr/random/random.h>
30 #include "mbedtls/platform_util.h"
31 
os_get_time(struct os_time * t)32 int os_get_time(struct os_time *t)
33 {
34 	if (t == NULL) {
35 		return -1;
36 	}
37 
38 	int64_t now = k_uptime_ticks();
39 	t->sec = now / CONFIG_SYS_CLOCK_TICKS_PER_SEC;
40 	t->usec = k_ticks_to_us_floor64(now);
41 
42 	return 0;
43 }
44 
os_random(void)45 unsigned long os_random(void)
46 {
47 	return sys_rand32_get();
48 }
49 
os_get_random(unsigned char * buf,size_t len)50 int os_get_random(unsigned char *buf, size_t len)
51 {
52 	sys_rand_get((void *)buf, len);
53 	return 0;
54 }
55 
os_sleep(os_time_t sec,os_time_t usec)56 void os_sleep(os_time_t sec, os_time_t usec)
57 {
58     if (sec) {
59         k_sleep(K_SECONDS(sec));
60     }
61     if (usec) {
62         k_sleep(K_USEC(usec));
63     }
64 }
65 
66 #ifdef CONFIG_CRYPTO_MBEDTLS
forced_memzero(void * ptr,size_t len)67 void forced_memzero(void *ptr, size_t len)
68 {
69     mbedtls_platform_zeroize(ptr, len);
70 }
71 #endif
72