You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "acassis (via GitHub)" <gi...@apache.org> on 2023/01/26 23:11:41 UTC

[GitHub] [nuttx] acassis opened a new pull request, #8264: esp32s3: Add support to RNG (random number generator)

acassis opened a new pull request, #8264:
URL: https://github.com/apache/nuttx/pull/8264

   ## Summary
   Add support to RNG (random number generator)
   ## Impact
   Use of hardware RNG
   ## Testing
   esp32s3-devkit
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #8264: esp32s3: Add support to RNG (random number generator)

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on code in PR #8264:
URL: https://github.com/apache/nuttx/pull/8264#discussion_r1088431905


##########
arch/xtensa/src/esp32s3/esp32s3_rng.c:
##########
@@ -0,0 +1,235 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s3/esp32s3_rng.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/drivers/drivers.h>
+
+#include <arch/xtensa/core_macros.h>
+
+#include "xtensa.h"
+#include "xtensa_attr.h"
+#include "hardware/wdev_reg.h"
+#include "esp32s3_clockconfig.h"
+
+#if defined(CONFIG_ESP32S3_RNG)
+#if defined(CONFIG_DEV_RANDOM) || defined(CONFIG_DEV_URANDOM_ARCH)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))

Review Comment:
   ```suggestion
   #  define MIN(a, b) (((a) < (b)) ? (a) : (b))
   ```



##########
arch/xtensa/src/esp32s3/esp32s3_rng.c:
##########
@@ -0,0 +1,235 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s3/esp32s3_rng.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <debug.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/mutex.h>
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/drivers/drivers.h>
+
+#include <arch/xtensa/core_macros.h>
+
+#include "xtensa.h"
+#include "xtensa_attr.h"
+#include "hardware/wdev_reg.h"
+#include "esp32s3_clockconfig.h"
+
+#if defined(CONFIG_ESP32S3_RNG)
+#if defined(CONFIG_DEV_RANDOM) || defined(CONFIG_DEV_URANDOM_ARCH)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static ssize_t esp32s3_rng_read(struct file *filep, char *buffer,
+                                size_t buflen);
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct rng_dev_s
+{
+  uint8_t *rd_buf;
+  mutex_t  rd_lock;        /* mutex for read RNG data */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct rng_dev_s g_rngdev =
+{
+  .rd_lock = NXMUTEX_INITIALIZER,
+};
+
+static const struct file_operations g_rngops =
+{
+  .read = esp32s3_rng_read,       /* read */
+};
+
+/****************************************************************************
+ * Private functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32s3_random
+ *
+ * Description:
+ *   Read a random number (unsigned int 32-bit) from ESP32S3 PRNG.
+ *
+ * Input Parameters:
+ *   none
+ *
+ * Returned Value:
+ *   A uint32_t random number.
+ *
+ ****************************************************************************/
+
+uint32_t IRAM_ATTR esp_random(void)
+{
+  /* The PRNG which implements WDEV_RANDOM register gets 2 bits
+   * of extra entropy from a hardware randomness source every APB clock cycle
+   * (provided Wi-Fi or BT are enabled). To make sure entropy is not drained
+   * faster than it is added, this function needs to wait for at least 1778
+   * APB clock cycles after reading previous word. This implementation may
+   * actually wait a bit longer due to extra time spent in arithmetic and
+   * branch statements.
+   *
+   * As a (probably unnecessary) precaution to avoid returning the
+   * RNG state as-is, the result is XORed with additional
+   * WDEV_RND_REG reads while waiting.
+   */
+
+  uint32_t cpu_to_apb_freq_ratio = esp_clk_cpu_freq() / esp_clk_apb_freq();
+
+  static uint32_t last_ccount = 0;
+  uint32_t ccount;
+  uint32_t result = 0;
+
+  do
+    {
+      ccount = XTHAL_GET_CCOUNT();
+      result ^= getreg32(WDEV_RND_REG);
+    }
+  while (ccount - last_ccount < cpu_to_apb_freq_ratio * 1778);
+
+  last_ccount = ccount;
+  return result ^ getreg32(WDEV_RND_REG);
+}
+
+/****************************************************************************
+ * Name: esp32s3_rng_read
+ *
+ * Description:
+ *   Read a certain amount of random data.
+ *
+ * Input Parameters:
+ *   filep  - File structure pointer
+ *   buffer - Buffer to write data coming from RNG device
+ *   buflen - Amount of bytes to read
+ *
+ * Returned Value:
+ *   The number of random bytes generated.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32s3_rng_read(struct file *filep, char *buffer,
+                              size_t buflen)

Review Comment:
   ```suggestion
   static ssize_t esp32s3_rng_read(struct file *filep, char *buffer,
                                   size_t buflen)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 merged pull request #8264: esp32s3: Add support to RNG (random number generator)

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #8264:
URL: https://github.com/apache/nuttx/pull/8264


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org