Browse Source

fix: 双重随机,取异或,看看怎么样

Wendal Chen 3 years ago
parent
commit
7bae52a2aa
1 changed files with 12 additions and 7 deletions
  1. 12 7
      app/port/luat_crypto_air101.c

+ 12 - 7
app/port/luat_crypto_air101.c

@@ -3,6 +3,7 @@
 #include "wm_include.h"
 #include "wm_include.h"
 #include "wm_crypto_hard.h"
 #include "wm_crypto_hard.h"
 #include "aes.h"
 #include "aes.h"
+#include "wm_osal.h"
 
 
 #include "luat_base.h"
 #include "luat_base.h"
 #include "luat_crypto.h"
 #include "luat_crypto.h"
@@ -13,15 +14,19 @@
 #include "task.h"
 #include "task.h"
 
 
 int luat_crypto_trng(char* buff, size_t len) {
 int luat_crypto_trng(char* buff, size_t len) {
-    // tls_crypto_random_init(0, CRYPTO_RNG_SWITCH_32);
-    // vTaskDelay(5);
-    // tls_crypto_random_bytes_range(buff, len, 256);
-    // tls_crypto_random_bytes_range(buff, len, 256);
-    // tls_crypto_random_bytes_range(buff, len, 256);
-    // tls_crypto_random_stop();
-    // tls_crypto_trng(buff, len);
+    #define TMP_LEN (8)
+    char tmp[TMP_LEN];
+    tls_crypto_random_init(tls_os_get_time(), CRYPTO_RNG_SWITCH_32);
+    vTaskDelay(1);
+    tls_crypto_random_bytes_range(tmp, TMP_LEN, 255);
+    tls_crypto_random_stop();
+
     extern int random_get_bytes(void *buf, size_t len);
     extern int random_get_bytes(void *buf, size_t len);
 	random_get_bytes(buff, len);
 	random_get_bytes(buff, len);
+    for (size_t i = 0; i < len; i++)
+    {
+        buff[i] ^= tmp[i % TMP_LEN];
+    }
     return 0;
     return 0;
 }
 }