dualcone 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE.txt +12 -18
- data/README.md +10 -17
- data/ext/dualcone/dualcone.c +45 -5
- data/ext/dualcone/extconf.rb +9 -8
- data/lib/dualcone/version.rb +1 -1
- data/vendor/libhydrogen/LICENSE +1 -1
- data/vendor/libhydrogen/Makefile +1 -1
- data/vendor/libhydrogen/README.md +3 -1
- data/vendor/libhydrogen/hydrogen.c +2 -1
- data/vendor/libhydrogen/hydrogen.h +24 -24
- data/vendor/libhydrogen/impl/common.h +3 -3
- data/vendor/libhydrogen/impl/core.h +2 -4
- data/vendor/libhydrogen/impl/gimli-core/sse2.h +1 -1
- data/vendor/libhydrogen/impl/hash.h +6 -6
- data/vendor/libhydrogen/impl/hydrogen_p.h +12 -12
- data/vendor/libhydrogen/impl/pwhash.h +6 -6
- data/vendor/libhydrogen/impl/random/avr.h +61 -0
- data/vendor/libhydrogen/impl/random/esp32.h +32 -0
- data/vendor/libhydrogen/impl/random/mbed.h +44 -0
- data/vendor/libhydrogen/impl/random/nrf52832.h +41 -0
- data/vendor/libhydrogen/impl/random/particle.h +26 -0
- data/vendor/libhydrogen/impl/random/riot.h +10 -0
- data/vendor/libhydrogen/impl/random/rtthread.h +37 -0
- data/vendor/libhydrogen/impl/random/stm32.h +47 -0
- data/vendor/libhydrogen/impl/random/unix.h +85 -0
- data/vendor/libhydrogen/impl/random/wasi.h +12 -0
- data/vendor/libhydrogen/impl/random/windows.h +20 -0
- data/vendor/libhydrogen/impl/random.h +19 -337
- data/vendor/libhydrogen/impl/secretbox.h +1 -1
- data/vendor/libhydrogen/impl/sign.h +2 -2
- data/vendor/libhydrogen/impl/x25519.h +22 -20
- metadata +46 -6
@@ -0,0 +1,44 @@
|
|
1
|
+
#include <mbedtls/ctr_drbg.h>
|
2
|
+
#include <mbedtls/entropy.h>
|
3
|
+
|
4
|
+
#if defined(MBEDTLS_ENTROPY_C)
|
5
|
+
|
6
|
+
static int
|
7
|
+
hydro_random_init(void)
|
8
|
+
{
|
9
|
+
mbedtls_entropy_context entropy;
|
10
|
+
uint16_t pos = 0;
|
11
|
+
|
12
|
+
mbedtls_entropy_init(&entropy);
|
13
|
+
|
14
|
+
// Pull data directly out of the entropy pool for the state, as it's small enough.
|
15
|
+
if (mbedtls_entropy_func(&entropy, (uint8_t *) &hydro_random_context.counter,
|
16
|
+
sizeof hydro_random_context.counter) != 0) {
|
17
|
+
return -1;
|
18
|
+
}
|
19
|
+
// mbedtls_entropy_func can't provide more than MBEDTLS_ENTROPY_BLOCK_SIZE in one go.
|
20
|
+
// This constant depends of mbedTLS configuration (whether the PRNG is backed by SHA256/SHA512
|
21
|
+
// at this time) Therefore, if necessary, we get entropy multiple times.
|
22
|
+
|
23
|
+
do {
|
24
|
+
const uint8_t dataLeftToConsume = gimli_BLOCKBYTES - pos;
|
25
|
+
const uint8_t currentChunkSize = (dataLeftToConsume > MBEDTLS_ENTROPY_BLOCK_SIZE)
|
26
|
+
? MBEDTLS_ENTROPY_BLOCK_SIZE
|
27
|
+
: dataLeftToConsume;
|
28
|
+
|
29
|
+
// Forces mbedTLS to fetch fresh entropy, then get some to feed libhydrogen.
|
30
|
+
if (mbedtls_entropy_gather(&entropy) != 0 ||
|
31
|
+
mbedtls_entropy_func(&entropy, &hydro_random_context.state[pos], currentChunkSize) !=
|
32
|
+
0) {
|
33
|
+
return -1;
|
34
|
+
}
|
35
|
+
pos += MBEDTLS_ENTROPY_BLOCK_SIZE;
|
36
|
+
} while (pos < gimli_BLOCKBYTES);
|
37
|
+
|
38
|
+
mbedtls_entropy_free(&entropy);
|
39
|
+
|
40
|
+
return 0;
|
41
|
+
}
|
42
|
+
#else
|
43
|
+
#error Need an entropy source
|
44
|
+
#endif
|
@@ -0,0 +1,41 @@
|
|
1
|
+
// Important: The SoftDevice *must* be activated to enable reading from the RNG
|
2
|
+
// http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Frng.html
|
3
|
+
|
4
|
+
#include <nrf_soc.h>
|
5
|
+
|
6
|
+
static int
|
7
|
+
hydro_random_init(void)
|
8
|
+
{
|
9
|
+
const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
|
10
|
+
hydro_hash_state st;
|
11
|
+
const uint8_t total_bytes = 32;
|
12
|
+
uint8_t remaining_bytes = total_bytes;
|
13
|
+
uint8_t available_bytes;
|
14
|
+
uint8_t rand_buffer[32];
|
15
|
+
|
16
|
+
hydro_hash_init(&st, ctx, NULL);
|
17
|
+
|
18
|
+
for (;;) {
|
19
|
+
if (sd_rand_application_bytes_available_get(&available_bytes) != NRF_SUCCESS) {
|
20
|
+
return -1;
|
21
|
+
}
|
22
|
+
if (available_bytes > 0) {
|
23
|
+
if (available_bytes > remaining_bytes) {
|
24
|
+
available_bytes = remaining_bytes;
|
25
|
+
}
|
26
|
+
if (sd_rand_application_vector_get(rand_buffer, available_bytes) != NRF_SUCCESS) {
|
27
|
+
return -1;
|
28
|
+
}
|
29
|
+
hydro_hash_update(&st, rand_buffer, total_bytes);
|
30
|
+
remaining_bytes -= available_bytes;
|
31
|
+
}
|
32
|
+
if (remaining_bytes <= 0) {
|
33
|
+
break;
|
34
|
+
}
|
35
|
+
delay(10);
|
36
|
+
}
|
37
|
+
hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
|
38
|
+
hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
|
39
|
+
|
40
|
+
return 0;
|
41
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
// Note: All particle platforms except for the Spark Core have a HW RNG. Only allow building on
|
2
|
+
// supported platforms for now. PLATFORM_ID definitions:
|
3
|
+
// https://github.com/particle-iot/device-os/blob/mesh-develop/hal/shared/platforms.h
|
4
|
+
|
5
|
+
#include <Particle.h>
|
6
|
+
|
7
|
+
static int
|
8
|
+
hydro_random_init(void)
|
9
|
+
{
|
10
|
+
const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
|
11
|
+
hydro_hash_state st;
|
12
|
+
uint16_t ebits = 0;
|
13
|
+
|
14
|
+
hydro_hash_init(&st, ctx, NULL);
|
15
|
+
|
16
|
+
while (ebits < 256) {
|
17
|
+
uint32_t r = HAL_RNG_GetRandomNumber();
|
18
|
+
hydro_hash_update(&st, (const uint32_t *) &r, sizeof r);
|
19
|
+
ebits += 32;
|
20
|
+
}
|
21
|
+
|
22
|
+
hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
|
23
|
+
hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
|
24
|
+
|
25
|
+
return 0;
|
26
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#include <rtthread.h>
|
2
|
+
#include <hw_rng.h>
|
3
|
+
|
4
|
+
#define DBG_TAG "libhydrogen"
|
5
|
+
#define DBG_LVL DBG_LOG
|
6
|
+
#include <rtdbg.h>
|
7
|
+
|
8
|
+
static int
|
9
|
+
hydrogen_init(void) {
|
10
|
+
if (hydro_init() != 0) {
|
11
|
+
abort();
|
12
|
+
}
|
13
|
+
LOG_I("libhydrogen initialized");
|
14
|
+
return 0;
|
15
|
+
}
|
16
|
+
INIT_APP_EXPORT(hydrogen_init);
|
17
|
+
|
18
|
+
static int
|
19
|
+
hydro_random_init(void)
|
20
|
+
{
|
21
|
+
const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
|
22
|
+
hydro_hash_state st;
|
23
|
+
uint16_t ebits = 0;
|
24
|
+
|
25
|
+
hydro_hash_init(&st, ctx, NULL);
|
26
|
+
|
27
|
+
while (ebits < 256) {
|
28
|
+
uint32_t r = rt_hwcrypto_rng_update();
|
29
|
+
hydro_hash_update(&st, (const uint32_t *) &r, sizeof r);
|
30
|
+
ebits += 32;
|
31
|
+
}
|
32
|
+
|
33
|
+
hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
|
34
|
+
hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
|
35
|
+
|
36
|
+
return 0;
|
37
|
+
}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
// Use hardware RNG peripheral
|
3
|
+
// Working with HAL, LL Driver (untested)
|
4
|
+
|
5
|
+
#ifdef STM32F4
|
6
|
+
|
7
|
+
#include "stm32f4xx.h"
|
8
|
+
|
9
|
+
static int
|
10
|
+
hydro_random_init(void)
|
11
|
+
{
|
12
|
+
const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
|
13
|
+
hydro_hash_state st;
|
14
|
+
uint16_t ebits = 0;
|
15
|
+
|
16
|
+
__IO uint32_t tmpreg;
|
17
|
+
|
18
|
+
// Enable RNG clock source
|
19
|
+
SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_RNGEN);
|
20
|
+
|
21
|
+
// Delay after an RCC peripheral clock enabling
|
22
|
+
tmpreg = READ_BIT(RCC->AHB2ENR, RCC_AHB2ENR_RNGEN);
|
23
|
+
UNUSED(tmpreg);
|
24
|
+
|
25
|
+
// RNG Peripheral enable
|
26
|
+
SET_BIT(RNG->CR, RNG_CR_RNGEN);
|
27
|
+
|
28
|
+
hydro_hash_init(&st, ctx, NULL);
|
29
|
+
|
30
|
+
while (ebits < 256) {
|
31
|
+
while (!(READ_BIT(RNG->SR, RNG_SR_DRDY))) {
|
32
|
+
}
|
33
|
+
|
34
|
+
uint32_t r = RNG->DR;
|
35
|
+
hydro_hash_update(&st, (const uint32_t*) &r, sizeof r);
|
36
|
+
ebits += 32;
|
37
|
+
}
|
38
|
+
|
39
|
+
hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
|
40
|
+
hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
|
41
|
+
|
42
|
+
return 0;
|
43
|
+
}
|
44
|
+
|
45
|
+
#else
|
46
|
+
# error SMT32 implementation missing!
|
47
|
+
#endif
|
@@ -0,0 +1,85 @@
|
|
1
|
+
#include <errno.h>
|
2
|
+
#include <fcntl.h>
|
3
|
+
#ifdef __linux__
|
4
|
+
#include <poll.h>
|
5
|
+
#endif
|
6
|
+
#include <sys/types.h>
|
7
|
+
#include <unistd.h>
|
8
|
+
|
9
|
+
#ifdef __linux__
|
10
|
+
static int
|
11
|
+
hydro_random_block_on_dev_random(void)
|
12
|
+
{
|
13
|
+
struct pollfd pfd;
|
14
|
+
int fd;
|
15
|
+
int pret;
|
16
|
+
|
17
|
+
fd = open("/dev/random", O_RDONLY);
|
18
|
+
if (fd == -1) {
|
19
|
+
return 0;
|
20
|
+
}
|
21
|
+
pfd.fd = fd;
|
22
|
+
pfd.events = POLLIN;
|
23
|
+
pfd.revents = 0;
|
24
|
+
do {
|
25
|
+
pret = poll(&pfd, 1, -1);
|
26
|
+
} while (pret < 0 && (errno == EINTR || errno == EAGAIN));
|
27
|
+
if (pret != 1) {
|
28
|
+
(void) close(fd);
|
29
|
+
errno = EIO;
|
30
|
+
return -1;
|
31
|
+
}
|
32
|
+
return close(fd);
|
33
|
+
}
|
34
|
+
#endif
|
35
|
+
|
36
|
+
static ssize_t
|
37
|
+
hydro_random_safe_read(const int fd, void *const buf_, size_t len)
|
38
|
+
{
|
39
|
+
unsigned char *buf = (unsigned char *) buf_;
|
40
|
+
ssize_t readnb;
|
41
|
+
|
42
|
+
do {
|
43
|
+
while ((readnb = read(fd, buf, len)) < (ssize_t) 0 && (errno == EINTR || errno == EAGAIN)) {
|
44
|
+
}
|
45
|
+
if (readnb < (ssize_t) 0) {
|
46
|
+
return readnb;
|
47
|
+
}
|
48
|
+
if (readnb == (ssize_t) 0) {
|
49
|
+
break;
|
50
|
+
}
|
51
|
+
len -= (size_t) readnb;
|
52
|
+
buf += readnb;
|
53
|
+
} while (len > (ssize_t) 0);
|
54
|
+
|
55
|
+
return (ssize_t) (buf - (unsigned char *) buf_);
|
56
|
+
}
|
57
|
+
|
58
|
+
static int
|
59
|
+
hydro_random_init(void)
|
60
|
+
{
|
61
|
+
uint8_t tmp[gimli_BLOCKBYTES + 8];
|
62
|
+
int fd;
|
63
|
+
int ret = -1;
|
64
|
+
|
65
|
+
#ifdef __linux__
|
66
|
+
if (hydro_random_block_on_dev_random() != 0) {
|
67
|
+
return -1;
|
68
|
+
}
|
69
|
+
#endif
|
70
|
+
do {
|
71
|
+
fd = open("/dev/urandom", O_RDONLY);
|
72
|
+
if (fd == -1 && errno != EINTR) {
|
73
|
+
return -1;
|
74
|
+
}
|
75
|
+
} while (fd == -1);
|
76
|
+
if (hydro_random_safe_read(fd, tmp, sizeof tmp) == (ssize_t) sizeof tmp) {
|
77
|
+
memcpy(hydro_random_context.state, tmp, gimli_BLOCKBYTES);
|
78
|
+
memcpy(&hydro_random_context.counter, tmp + gimli_BLOCKBYTES, 8);
|
79
|
+
hydro_memzero(tmp, sizeof tmp);
|
80
|
+
ret = 0;
|
81
|
+
}
|
82
|
+
ret |= close(fd);
|
83
|
+
|
84
|
+
return ret;
|
85
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#include <unistd.h>
|
2
|
+
|
3
|
+
static int
|
4
|
+
hydro_random_init(void)
|
5
|
+
{
|
6
|
+
if (getentropy(hydro_random_context.state, sizeof hydro_random_context.state) != 0) {
|
7
|
+
return -1;
|
8
|
+
}
|
9
|
+
hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
|
10
|
+
|
11
|
+
return 0;
|
12
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#include <windows.h>
|
2
|
+
#define RtlGenRandom SystemFunction036
|
3
|
+
#if defined(__cplusplus)
|
4
|
+
extern "C"
|
5
|
+
#endif
|
6
|
+
BOOLEAN NTAPI
|
7
|
+
RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
|
8
|
+
#pragma comment(lib, "advapi32.lib")
|
9
|
+
|
10
|
+
static int
|
11
|
+
hydro_random_init(void)
|
12
|
+
{
|
13
|
+
if (!RtlGenRandom((PVOID) hydro_random_context.state,
|
14
|
+
(ULONG) sizeof hydro_random_context.state)) {
|
15
|
+
return -1;
|
16
|
+
}
|
17
|
+
hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
|
18
|
+
|
19
|
+
return 0;
|
20
|
+
}
|