]> granicus.if.org Git - esp-idf/commitdiff
Add API for random number generator
authorIvan Grokhotkov <ivan@espressif.com>
Mon, 21 Nov 2016 14:59:46 +0000 (22:59 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 22 Nov 2016 13:14:37 +0000 (21:14 +0800)
components/esp32/hw_random.c [new file with mode: 0644]
components/esp32/include/soc/soc.h
components/esp32/include/soc/wdev_reg.h [new file with mode: 0644]
components/wpa_supplicant/component.mk
components/wpa_supplicant/port/include/endian.h
components/wpa_supplicant/port/os_xtensa.c [new file with mode: 0644]

diff --git a/components/esp32/hw_random.c b/components/esp32/hw_random.c
new file mode 100644 (file)
index 0000000..2c6e2cf
--- /dev/null
@@ -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 <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#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);
+}
index bec4c85cfcc98a0c016b8aad201e46e78efcdb8a..61ec2e1f2dc5a321c5980e4bd2078f1af054abea 100755 (executable)
 //}}
 
 //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 (file)
index 0000000..7189c9e
--- /dev/null
@@ -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
index b01eb83be9bd15cdfd719c181a4e0b22ad32b992..daac5ca70ee9c2c998493c0c34ab7b63b9cce823 100644 (file)
@@ -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
index e2df616b79b68fa7e6cbe250d17d94e518b4a6d6..5e6a876fda3dd107f8e93c0125a15ed26a9ec02d 100644 (file)
@@ -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 (file)
index 0000000..9ecde1f
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * wpa_supplicant/hostapd / Internal implementation of OS specific functions
+ * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
+ *
+ * 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 <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+#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;
+}
+