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 <unistd.h>
28 #include <sys/time.h>
29 #include "esp_random.h"
30 #include "utils/common.h"
31 #include "mbedtls/platform_util.h"
32 
os_get_time(struct os_time * t)33 int os_get_time(struct os_time *t)
34 {
35     struct timeval tv;
36     int ret = gettimeofday(&tv, NULL);
37     t->sec = (os_time_t) tv.tv_sec;
38     t->usec = tv.tv_usec;
39     return ret;
40 }
41 
os_random(void)42 unsigned long os_random(void)
43 {
44     return esp_random();
45 }
46 
os_get_random(unsigned char * buf,size_t len)47 int os_get_random(unsigned char *buf, size_t len)
48 {
49     esp_fill_random(buf, len);
50     return 0;
51 }
52 
os_sleep(os_time_t sec,os_time_t usec)53 void os_sleep(os_time_t sec, os_time_t usec)
54 {
55     if (sec) {
56         sleep(sec);
57     }
58     if (usec) {
59         usleep(usec);
60     }
61 }
62 
z_strdup(const char * s)63 char *z_strdup(const char *s) {
64     size_t size = strlen(s) + 1;
65     char *p = k_malloc(size);
66     if (p) {
67         memcpy(p, s, size);
68     }
69     return p;
70 }
71 
tolower(unsigned char ch)72 int tolower(unsigned char ch) {
73     if (ch >= 'A' && ch <= 'Z')
74         ch = 'a' + (ch - 'A');
75 
76     return ch;
77 }
78 
z_strcasecmp(const char * str1,const char * str2)79 int z_strcasecmp(const char * str1, const char * str2)
80 {
81     const unsigned char *u1 = (const u_char *)s1,
82     const unsigned char *u2 = (const u_char *)s2;
83 
84     while (tolower(*u1) == tolower(*u2++)) {
85         if (*u1++ == '\0') {
86             return 0;
87         }
88     }
89 
90     return (tolower(*u1) - tolower(*--u2));
91 }
92 
z_strcasecmp(const char * str1,const char * str2,size_t num)93 int z_strcasecmp(const char * str1, const char * str2, size_t num)
94 {
95     const unsigned char *u1 = (const u_char *)s1,
96     const unsigned char *u2 = (const u_char *)s2;
97     const size_t ch_compared = 0;
98 
99     while ((ch_compared < num) && (tolower(*u1) == tolower(*u2++))) {
100         if (*u1++ == '\0') {
101             return 0;
102         }
103         ch_compared++;
104     }
105 
106     return (tolower(*u1) - tolower(*--u2));
107 }
108 
109 #ifdef CONFIG_CRYPTO_MBEDTLS
forced_memzero(void * ptr,size_t len)110 void forced_memzero(void *ptr, size_t len)
111 {
112     mbedtls_platform_zeroize(ptr, len);
113 }
114 #endif
115