wuhanstudio 3 лет назад
Родитель
Сommit
0d1d69d098
2 измененных файлов с 29 добавлено и 0 удалено
  1. 2 0
      impl/random.h
  2. 27 0
      impl/random/rtthread.h

+ 2 - 0
impl/random.h

@@ -25,6 +25,8 @@ static TLS struct {
 # include "random/riot.h"
 #elif defined(STM32F4)
 # include "random/stm32.h"
+#elif defined(__RTTHREAD__)
+# include "random/rtthread.h"
 #else
 # error Unsupported platform
 #endif

+ 27 - 0
impl/random/rtthread.h

@@ -0,0 +1,27 @@
+#include <rtthread.h>
+#include <hw_rng.h>
+
+#define DBG_TAG "libhydrogen"
+#define DBG_LVL DBG_LOG
+#include <rtdbg.h>
+
+static int
+hydro_random_init(void)
+{
+    const char       ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
+    hydro_hash_state st;
+    uint16_t         ebits = 0;
+
+    hydro_hash_init(&st, ctx, NULL);
+
+    while (ebits < 256) {
+        uint32_t r = rt_hwcrypto_rng_update();
+        hydro_hash_update(&st, (const uint32_t *) &r, sizeof r);
+        ebits += 32;
+    }
+
+    hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
+    hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
+
+    return 0;
+}