0002-Include-RIOT-Hardware-RNG-interface.patch
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
From 459241f2801c3b1a0d28c5669527e165ce4b384e Mon Sep 17 00:00:00 2001
From: Wentao Shang <wentaoshang@gmail.com>
Date: Mon, 12 Dec 2016 16:19:34 -0800
Subject: [PATCH 2/3] Include RIOT Hardware RNG interface
---
platform-specific.inc | 4 ++++
uECC.c | 22 ++++++++++++++++++++++
uECC.h | 8 ++++++++
3 files changed, 34 insertions(+)
diff --git a/platform-specific.inc b/platform-specific.inc
index 1bb595a..b13fdbe 100644
--- a/platform-specific.inc
+++ b/platform-specific.inc
@@ -5,6 +5,8 @@
#include "types.h"
+#ifdef FEATURE_PERIPH_HWRNG
+
#if (defined(_WIN32) || defined(_WIN64))
/* Windows */
@@ -64,4 +66,6 @@ static int default_RNG(uint8_t *dest, unsigned size) {
#endif /* platform */
+#endif /* FEATURE_PERIPH_HWRNG */
+
#endif /* _UECC_PLATFORM_SPECIFIC_H_ */
diff --git a/uECC.c b/uECC.c
index daa144a..3691fc4 100644
--- a/uECC.c
+++ b/uECC.c
@@ -2,6 +2,9 @@
#include "uECC.h"
#include "uECC_vli.h"
+#ifdef FEATURE_PERIPH_HWRNG
+#include "periph/hwrng.h"
+#endif
#ifndef uECC_RNG_MAX_TRIES
#define uECC_RNG_MAX_TRIES 64
@@ -181,9 +184,20 @@ static cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
#include "asm_avr.inc"
#endif
+#ifdef FEATURE_PERIPH_HWRNG
+int riot_hwrng(uint8_t *dest, unsigned size) {
+ hwrng_read(dest, size);
+ return 1;
+}
+#endif
+
+#ifdef FEATURE_PERIPH_HWRNG
#if default_RNG_defined
static uECC_RNG_Function g_rng_function = &default_RNG;
#else
+static uECC_RNG_Function g_rng_function = &riot_hwrng;
+#endif
+#else
static uECC_RNG_Function g_rng_function = 0;
#endif
@@ -1001,6 +1015,8 @@ uECC_VLI_API int uECC_generate_random_int(uECC_word_t *random,
return 0;
}
+#ifdef FEATURE_PERIPH_HWRNG
+
int uECC_make_key(uint8_t *public_key,
uint8_t *private_key,
uECC_Curve curve) {
@@ -1031,6 +1047,8 @@ int uECC_make_key(uint8_t *public_key,
return 0;
}
+#endif /* FEATURE_PERIPH_HWRNG */
+
int uECC_shared_secret(const uint8_t *public_key,
const uint8_t *private_key,
uint8_t *secret,
@@ -1303,6 +1321,8 @@ static int uECC_sign_with_k(const uint8_t *private_key,
return 1;
}
+#ifdef FEATURE_PERIPH_HWRNG
+
int uECC_sign(const uint8_t *private_key,
const uint8_t *message_hash,
unsigned hash_size,
@@ -1323,6 +1343,8 @@ int uECC_sign(const uint8_t *private_key,
return 0;
}
+#endif /* FEATURE_PERIPH_HWRNG */
+
/* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always
the same size as the hash result size. */
static void HMAC_init(const uECC_HashContext *hash_context, const uint8_t *K) {
diff --git a/uECC.h b/uECC.h
index 9911763..6433143 100644
--- a/uECC.h
+++ b/uECC.h
@@ -144,6 +144,8 @@ Returns the size of a public key for the curve in bytes.
*/
int uECC_curve_public_key_size(uECC_Curve curve);
+#ifdef FEATURE_PERIPH_HWRNG
+
/* uECC_make_key() function.
Create a public/private key pair.
@@ -162,6 +164,8 @@ Returns 1 if the key pair was generated successfully, 0 if an error occurred.
*/
int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve);
+#endif /* FEATURE_PERIPH_HWRNG */
+
/* uECC_shared_secret() function.
Compute a shared secret given your secret key and someone else's public key.
Note: It is recommended that you hash the result of uECC_shared_secret() before using it for
@@ -235,6 +239,8 @@ Returns 1 if the key was computed successfully, 0 if an error occurred.
*/
int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, uECC_Curve curve);
+#ifdef FEATURE_PERIPH_HWRNG
+
/* uECC_sign() function.
Generate an ECDSA signature for a given hash value.
@@ -258,6 +264,8 @@ int uECC_sign(const uint8_t *private_key,
uint8_t *signature,
uECC_Curve curve);
+#endif /* FEATURE_PERIPH_HWRNG */
+
/* uECC_HashContext structure.
This is used to pass in an arbitrary hash function to uECC_sign_deterministic().
The structure will be used for multiple hash computations; each time a new hash
--
2.7.4