diff --git a/components/esp32/hw_random.c b/components/esp32/hw_random.c new file mode 100644 index 000000000..2c6e2cf5f --- /dev/null +++ b/components/esp32/hw_random.c @@ -0,0 +1,33 @@ +// Copyright 2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +#include +#include +#include +#include "esp_attr.h" +#include "soc/wdev_reg.h" +#include "freertos/FreeRTOSConfig.h" +#include "xtensa/core-macros.h" + +uint32_t IRAM_ATTR esp_random(void) +{ + static uint32_t last_ccount = 0; + uint32_t ccount; + do { + ccount = XTHAL_GET_CCOUNT(); + } while (ccount - last_ccount < XT_CLOCK_FREQ / APB_CLK_FREQ * 16); + last_ccount = ccount; + return REG_READ(WDEV_RND_REG); +} diff --git a/components/esp32/include/soc/soc.h b/components/esp32/include/soc/soc.h index bec4c85cf..61ec2e1f2 100755 --- a/components/esp32/include/soc/soc.h +++ b/components/esp32/include/soc/soc.h @@ -129,10 +129,10 @@ //}} //Periheral Clock {{ -#define APB_CLK_FREQ_ROM 26*1000000 +#define APB_CLK_FREQ_ROM ( 26*1000000 ) #define CPU_CLK_FREQ_ROM APB_CLK_FREQ_ROM #define CPU_CLK_FREQ APB_CLK_FREQ -#define APB_CLK_FREQ 80*1000000 //unit: Hz +#define APB_CLK_FREQ ( 80*1000000 ) //unit: Hz #define UART_CLK_FREQ APB_CLK_FREQ #define WDT_CLK_FREQ APB_CLK_FREQ #define TIMER_CLK_FREQ (80000000>>4) //80MHz divided by 16 diff --git a/components/esp32/include/soc/wdev_reg.h b/components/esp32/include/soc/wdev_reg.h new file mode 100644 index 000000000..7189c9ec3 --- /dev/null +++ b/components/esp32/include/soc/wdev_reg.h @@ -0,0 +1,18 @@ +// Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +/* Hardware random number generator register */ +#define WDEV_RND_REG 0x60035144 diff --git a/components/wpa_supplicant/component.mk b/components/wpa_supplicant/component.mk index b01eb83be..daac5ca70 100644 --- a/components/wpa_supplicant/component.mk +++ b/components/wpa_supplicant/component.mk @@ -1,4 +1,4 @@ COMPONENT_ADD_INCLUDEDIRS := include port/include -COMPONENT_SRCDIRS := src/crypto +COMPONENT_SRCDIRS := src/crypto port CFLAGS += -DEMBEDDED_SUPP -D__ets__ -Wno-strict-aliasing diff --git a/components/wpa_supplicant/port/include/endian.h b/components/wpa_supplicant/port/include/endian.h index e2df616b7..5e6a876fd 100644 --- a/components/wpa_supplicant/port/include/endian.h +++ b/components/wpa_supplicant/port/include/endian.h @@ -119,6 +119,7 @@ typedef __uint64_t uint64_t; #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ +#define INLINE __inline__ static INLINE uint16_t be16dec(const void *pp) diff --git a/components/wpa_supplicant/port/os_xtensa.c b/components/wpa_supplicant/port/os_xtensa.c new file mode 100644 index 000000000..9ecde1f11 --- /dev/null +++ b/components/wpa_supplicant/port/os_xtensa.c @@ -0,0 +1,64 @@ +/* + * wpa_supplicant/hostapd / Internal implementation of OS specific functions + * Copyright (c) 2005-2006, Jouni Malinen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * Alternatively, this software may be distributed under the terms of BSD + * license. + * + * See README and COPYING for more details. + * + * This file is an example of operating system specific wrapper functions. + * This version implements many of the functions internally, so it can be used + * to fill in missing functions from the target system C libraries. + * + * Some of the functions are using standard C library calls in order to keep + * this file in working condition to allow the functions to be tested on a + * Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for + * this file to work correctly. Note that these implementations are only + * examples and are not optimized for speed. + */ + +#include "crypto/common.h" +#include "os.h" +#include +#include +#include +#include "esp_system.h" + +int os_get_time(struct os_time *t) +{ + return gettimeofday((struct timeval*) t, NULL); +} + +unsigned long os_random(void) +{ + return esp_random(); +} + +unsigned long r_rand(void) __attribute__((alias("os_random"))); + + +int os_get_random(unsigned char *buf, size_t len) +{ + int i, j; + unsigned long tmp; + + for (i = 0; i < ((len + 3) & ~3) / 4; i++) { + tmp = r_rand(); + + for (j = 0; j < 4; j++) { + if ((i * 4 + j) < len) { + buf[i * 4 + j] = (uint8_t)(tmp >> (j * 8)); + } else { + break; + } + } + } + + return 0; +} +