Bläddra i källkod

Add basic support for Particle targets

Dan Kouba 6 år sedan
förälder
incheckning
de3f2cfabd
3 ändrade filer med 72 tillägg och 2 borttagningar
  1. 40 0
      Makefile.particle
  2. 30 0
      impl/random.h
  3. 2 2
      library.properties

+ 40 - 0
Makefile.particle

@@ -0,0 +1,40 @@
+PARTICLE_PACKAGE ?= hydrogen-crypto.zip
+SRC = \
+	hydrogen.c \
+	hydrogen.h \
+	impl/common.h \
+	impl/core.h \
+	impl/gimli-core.h \
+	impl/hash.h \
+	impl/hydrogen_p.h \
+	impl/kdf.h \
+	impl/kx.h \
+	impl/pwhash.h \
+	impl/random.h \
+	impl/secretbox.h \
+	impl/sign.h \
+	impl/x25519.h \
+	impl/gimli-core/portable.h
+
+all: package
+
+package: $(PARTICLE_PACKAGE)
+
+$(PARTICLE_PACKAGE):
+	mkdir particle
+	mkdir particle/src
+	cp library.properties particle/.
+	cp README.md particle/.
+	cp LICENSE particle/.
+	cp -r impl particle/src/.
+	cp hydrogen.h particle/src/.
+	cp hydrogen.c particle/src/hydrogen.cpp
+	7z a -tzip -mx=9 -r $(PARTICLE_PACKAGE) particle/.
+	rm -rf particle
+
+.PHONY: clean
+
+clean:
+	rm -f tests/tests
+	rm -rf particle
+	rm -f $(PARTICLE_PACKAGE)

+ 30 - 0
impl/random.h

@@ -98,6 +98,36 @@ hydro_random_init(void)
     return 0;
 }
 
+#elif defined(PARTICLE) && defined(PLATFORM_ID) && PLATFORM_ID > 2 && !defined(__unix__)
+
+// Note: All particle platforms except for the Spark Core have a HW RNG.  Only allow building on supported platforms for now.
+// PLATFORM_ID definitions: https://github.com/particle-iot/device-os/blob/mesh-develop/hal/shared/platforms.h
+
+#include "Particle.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 = HAL_RNG_GetRandomNumber();
+
+        delay(10);
+        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;
+}
+
 #elif (defined(NRF52832_XXAA) || defined(NRF52832_XXAB)) && !defined(__unix__)
 
 // Important: The SoftDevice *must* be activated to enable reading from the RNG

+ 2 - 2
library.properties

@@ -1,4 +1,4 @@
-architectures=avr,nrf52
+architectures=avr,nrf52,particle-photon,particle-electron,particle-p1
 author=Frank Denis <libhydrogen@pureftpd.org>
 category=Other
 includes=hydrogen.h
@@ -7,4 +7,4 @@ name=hydrogen-crypto
 paragraph=Consistent high-level API, inspired by libsodium. Instead of low-level primitives, it exposes simple functions to solve common problems that cryptography can solve.
 sentence=An easy-to-use, hard-to-misuse cryptographic library
 url=https://github.com/jedisct1/libhydrogen
-version=0.1
+version=0.1.0