Selaa lähdekoodia

Update to latest libhydrogen 1.0.1a65b7a

woollybah 6 vuotta sitten
vanhempi
commit
33b77c17e2

+ 0 - 2
libhydrogen.mod/libhydrogen/impl/common.h

@@ -6,8 +6,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "../hydrogen.h"
-
 #if !defined(__unix__) && (defined(__APPLE__) || defined(__linux__))
 # define __unix__ 1
 #endif

+ 63 - 5
libhydrogen.mod/libhydrogen/impl/random.h

@@ -80,8 +80,6 @@ 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;
-    uint16_t         tc;
-    bool             a, b;
 
     hydro_hash_init(&st, ctx, NULL);
 
@@ -99,6 +97,50 @@ hydro_random_init(void)
     return 0;
 }
 
+#elif (defined(NRF52832_XXAA) || defined(NRF52832_XXAB)) && !defined(__unix__)
+
+// Important: The SoftDevice *must* be activated to enable reading from the RNG
+// http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Frng.html
+
+#include <nrf_soc.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;
+    const uint8_t    total_bytes = 32;
+    uint8_t          remaining_bytes = total_bytes;
+    uint8_t          available_bytes;
+    uint8_t          rand_buffer[32];
+
+    hydro_hash_init(&st, ctx, NULL);
+
+    for (;;) {
+        if (sd_rand_application_bytes_available_get(&available_bytes) != NRF_SUCCESS) {
+            return -1;
+        }
+        if (available_bytes > 0) {
+            if (available_bytes > remaining_bytes) {
+                available_bytes = remaining_bytes;
+            }
+            if (sd_rand_application_vector_get(rand_buffer, available_bytes) != NRF_SUCCESS) {
+                return -1;
+            }
+            hydro_hash_update(&st, rand_buffer, total_bytes);
+            remaining_bytes -= available_bytes;
+        }
+        if (remaining_bytes <= 0) {
+            break;
+        }
+        delay(10);
+    }
+    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;
+}
+
 #elif defined(_WIN32)
 
 #include <windows.h>
@@ -122,12 +164,28 @@ hydro_random_init(void)
     return 0;
 }
 
+#elif defined(__wasi__)
+
+#include <unistd.h>
+
+static int
+hydro_random_init(void)
+{
+    if (getentropy(hydro_random_context.state,
+                   sizeof hydro_random_context.state) != 0) {
+        return -1;
+    }
+    hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
+
+    return 0;
+}
+
 #elif defined(__unix__)
 
 #include <errno.h>
 #include <fcntl.h>
 #ifdef __linux__
-#include <poll.h>
+# include <poll.h>
 #endif
 #include <sys/types.h>
 #include <unistd.h>
@@ -254,11 +312,11 @@ hydro_random_init(void)
 }
 
 #else
-#error Need an entropy source
+# error Need an entropy source
 #endif
 
 #else
-#error Unsupported platform
+# error Unsupported platform
 #endif
 
 static void

+ 5 - 5
libhydrogen.mod/libhydrogen/impl/x25519.h

@@ -109,10 +109,10 @@ hydro_x25519_sub(hydro_x25519_fe out, const hydro_x25519_fe a, const hydro_x2551
     int                   i;
 
     for (i = 0; i < hydro_x25519_NLIMBS; i++) {
-        out[i] = carry = carry + a[i] - b[i];
+        out[i] = (hydro_x25519_limb_t) (carry = carry + a[i] - b[i]);
         carry >>= hydro_x25519_WBITS;
     }
-    hydro_x25519_propagate(out, 1 + carry);
+    hydro_x25519_propagate(out, (hydro_x25519_limb_t) (1 + carry));
 }
 
 static void
@@ -207,7 +207,7 @@ hydro_x25519_canon(hydro_x25519_fe x)
     carry = -19;
     res   = 0;
     for (i = 0; i < hydro_x25519_NLIMBS; i++) {
-        res |= x[i] = carry += x[i];
+        res |= x[i] = (hydro_x25519_limb_t) (carry += x[i]);
         carry >>= hydro_x25519_WBITS;
     }
     return ((hydro_x25519_dlimb_t) res - 1) >> hydro_x25519_WBITS;
@@ -371,10 +371,10 @@ hydro_x25519_sc_montmul(hydro_x25519_scalar_t out, const hydro_x25519_scalar_t a
     /* Reduce */
     hydro_x25519_sdlimb_t scarry = 0;
     for (i = 0; i < hydro_x25519_NLIMBS; i++) {
-        out[i] = scarry = scarry + out[i] - hydro_x25519_sc_p[i];
+        out[i] = (hydro_x25519_limb_t) (scarry = scarry + out[i] - hydro_x25519_sc_p[i]);
         scarry >>= hydro_x25519_WBITS;
     }
-    hydro_x25519_limb_t need_add = -(scarry + hic);
+    hydro_x25519_limb_t need_add = (hydro_x25519_limb_t) -(scarry + hic);
 
     hydro_x25519_limb_t carry = 0;
     for (i = 0; i < hydro_x25519_NLIMBS; i++) {

+ 1 - 1
libhydrogen.mod/libhydrogen/library.properties

@@ -1,4 +1,4 @@
-architectures=avr
+architectures=avr,nrf52
 author=Frank Denis <[email protected]>
 category=Other
 includes=hydrogen.h

+ 9 - 1
libhydrogen.mod/libhydrogen/tests/tests.c

@@ -2,7 +2,7 @@
 #include <stdio.h>
 #include <string.h>
 
-#include "../hydrogen.h"
+#include "hydrogen.h"
 
 static const char *ctx = "libtests";
 
@@ -411,6 +411,14 @@ test_pwhash(void)
 int
 main(void)
 {
+#if defined(_WIN32)
+    /*
+     * On Windows, disable the "Abort - Retry - Ignore" GUI dialog that otherwise pops up on
+     * assertion failure.
+     */
+    _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
+#endif
+
     int ret;
 
     ret = hydro_init();