-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinycrypt.patch
More file actions
242 lines (230 loc) · 7.31 KB
/
tinycrypt.patch
File metadata and controls
242 lines (230 loc) · 7.31 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
diff --git a/config.mk b/config.mk
index a5b9fb7..00ca1cf 100644
--- a/config.mk
+++ b/config.mk
@@ -8,7 +8,7 @@
# EDIT HERE:
CC:=gcc
-CFLAGS:=-Os -std=c99 -Wall -Wextra -D_ISOC99_SOURCE -MMD -I../lib/include/ -I../lib/source/ -I../tests/include/
+CFLAGS:=-Os -flto -std=c99 -Wall -Wextra -D_ISOC99_SOURCE -MMD -I../lib/include/ -I../lib/source/ -I../tests/include/
vpath %.c ../lib/source/
ENABLE_TESTS=true
diff --git a/lib/Makefile b/lib/Makefile
index 9c4d8e2..9da863c 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -9,20 +9,23 @@
include ../config.mk
# Edit the OBJS content to add/remove primitives needed from TinyCrypt library:
-OBJS:=aes_decrypt.o \
- aes_encrypt.o \
- cbc_mode.o \
- ctr_mode.o \
- ctr_prng.o \
- hmac.o \
- hmac_prng.o \
- sha256.o \
- ecc.o \
- ecc_dh.o \
- ecc_dsa.o \
- ccm_mode.o \
- cmac_mode.o \
- utils.o
+# OBJS:=aes_decrypt.o \
+# aes_encrypt.o \
+# cbc_mode.o \
+# ctr_mode.o \
+# ctr_prng.o \
+# hmac.o \
+# hmac_prng.o \
+# sha256.o \
+# ecc.o \
+# ecc_dh.o \
+# ecc_dsa.o \
+# ccm_mode.o \
+# cmac_mode.o \
+# utils.o
+
+OBJS:= ecc_dsa.o \
+ ecc.o
DEPS:=$(OBJS:.o=.d)
diff --git a/lib/include/tinycrypt/ecc_platform_specific.h b/lib/include/tinycrypt/ecc_platform_specific.h
index e2c8823..a55adf4 100644
--- a/lib/include/tinycrypt/ecc_platform_specific.h
+++ b/lib/include/tinycrypt/ecc_platform_specific.h
@@ -74,7 +74,7 @@
* for some platforms, such as Unix and Linux. For other platforms, you may need
* to provide another PRNG function.
*/
-#define default_RNG_defined 1
+#define default_RNG_defined 0
int default_CSPRNG(uint8_t *dest, unsigned int size);
diff --git a/lib/source/ecc.c b/lib/source/ecc.c
index 46080bf..dc996aa 100644
--- a/lib/source/ecc.c
+++ b/lib/source/ecc.c
@@ -408,7 +408,7 @@ static void vli_modInv_update(uECC_word_t *uv,
void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
const uECC_word_t *mod, wordcount_t num_words)
{
- uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
+ uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
cmpresult_t cmpResult;
@@ -422,11 +422,27 @@ void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
uECC_vli_clear(u, num_words);
u[0] = 1;
uECC_vli_clear(v, num_words);
+ /* StepOverflow
+ * We use this div-slide as a marker for when the while loop starts.
+ * This serves as a simulated signal that would be visible to an attacker
+ * using a page-fault controlled-channel when the function is called from main
+ */
+ int r = 5;
+ unsigned long dividend = 1000000;
+ __asm__ volatile(
+ "movq %[dividend], %%rax\n\t" // load dividend into RAX
+ "xorq %%rdx, %%rdx\n\t" // clear RDX (high bits)
+ ".rept 105;\n\t"
+ "divq %%rbx;\n\t" //requires "movq (%0), %%rbx\n\t"
+ ".endr;\n\t"
+ :: "b" (&r), [dividend]"r"(dividend)
+ : "rax", "rdx"
+ );
while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
if (EVEN(a)) {
uECC_vli_rshift1(a, num_words);
- vli_modInv_update(u, mod, num_words);
- } else if (EVEN(b)) {
+ vli_modInv_update(u, mod, num_words);
+ } else if (EVEN(b)) {
uECC_vli_rshift1(b, num_words);
vli_modInv_update(v, mod, num_words);
} else if (cmpResult > 0) {
@@ -434,18 +450,18 @@ void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
uECC_vli_rshift1(a, num_words);
if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
uECC_vli_add(u, u, mod, num_words);
- }
- uECC_vli_sub(u, u, v, num_words);
- vli_modInv_update(u, mod, num_words);
- } else {
- uECC_vli_sub(b, b, a, num_words);
- uECC_vli_rshift1(b, num_words);
- if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
- uECC_vli_add(v, v, mod, num_words);
- }
- uECC_vli_sub(v, v, u, num_words);
- vli_modInv_update(v, mod, num_words);
- }
+ }
+ uECC_vli_sub(u, u, v, num_words);
+ vli_modInv_update(u, mod, num_words);
+ } else {
+ uECC_vli_sub(b, b, a, num_words);
+ uECC_vli_rshift1(b, num_words);
+ if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
+ uECC_vli_add(v, v, mod, num_words);
+ }
+ uECC_vli_sub(v, v, u, num_words);
+ vli_modInv_update(v, mod, num_words);
+ }
}
uECC_vli_set(result, u, num_words);
}
@@ -612,7 +628,7 @@ void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
}
while (carry < 0);
} else {
- while (carry ||
+ while (carry ||
uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
}
@@ -729,7 +745,7 @@ static void XYcZ_addC(uECC_word_t * X1, uECC_word_t * Y1,
void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
const uECC_word_t * scalar,
const uECC_word_t * initial_Z,
- bitcount_t num_bits, uECC_Curve curve)
+ bitcount_t num_bits, uECC_Curve curve)
{
/* R0 and R1 */
uECC_word_t Rx[2][NUM_ECC_WORDS];
@@ -937,6 +953,3 @@ int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
curve->num_bytes, curve->num_bytes, _public + curve->num_words);
return 1;
}
-
-
-
diff --git a/tests/Makefile b/tests/Makefile
index eff5a88..705135c 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -16,7 +16,8 @@ TEST_DEPS:=$(TEST_SOURCE:.c=.d)
TEST_BINARY:=$(TEST_SOURCE:.c=$(DOTEXE))
# Edit the 'all' content to add/remove tests needed from TinyCrypt library:
-all: $(TEST_BINARY)
+#all: $(TEST_BINARY)
+all: ecc_modinv$(DOTEXE)
clean:
-$(RM) $(TEST_BINARY) $(TEST_OBJECTS) $(TEST_DEPS)
@@ -63,5 +64,7 @@ test_ecc_dsa$(DOTEXE): test_ecc_dsa.o ecc.o utils.o ecc_dh.o \
ecc_dsa.o sha256.o test_ecc_utils.o ecc_platform_specific.o
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
+ecc_modinv$(DOTEXE): ecc_modinv.o ecc.o
+ $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
-include $(TEST_DEPS)
diff --git a/tests/ecc_modinv.c b/tests/ecc_modinv.c
new file mode 100644
index 0000000..1d0264c
--- /dev/null
+++ b/tests/ecc_modinv.c
@@ -0,0 +1,47 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include <tinycrypt/ecc.h>
+#include <tinycrypt/ecc_platform_specific.h>
+#include <tinycrypt/ecc_dsa.h>
+#include <tinycrypt/ecc_dh.h>
+
+int main(int argc, char *argv[]){
+ const struct uECC_Curve_t * curve = uECC_secp256r1();
+
+ uECC_word_t k[NUM_ECC_WORDS] = {0};
+
+ if (argc > NUM_ECC_WORDS + 1) {
+ printf("Provide 1 to %d 32-bit hex values.\n", NUM_ECC_WORDS);
+ return 1;
+ }
+
+ // Fill from LSB upward
+ for (int i = 1; i < argc; i++) {
+ unsigned long value = strtoul(argv[i], NULL, 16);
+ k[i - 1] = (uECC_word_t)value;
+ }
+
+ // Print result for verification
+ printf("k =\n");
+ for (int i = NUM_ECC_WORDS - 1; i >= 0; i--) {
+ printf("%08x ", (unsigned int)k[i]);
+ }
+ printf("\n");
+
+ printf("Waiting for you to press enter...\n");
+ int c = 0;
+ while ((c = getchar()) != '\n' && c != EOF) {}
+
+ uECC_vli_modInv(k, k, curve->n, NUM_ECC_WORDS);
+ // uECC_word_t tmp[2*NUM_ECC_WORDS];
+ // tmp[0] = 0;
+ // uECC_vli_mmod(k, tmp, curve->n, NUM_ECC_WORDS);
+ printf("Waiting for you to press enter...\n");
+ c = 0;
+ while ((c = getchar()) != '\n' && c != EOF) {}
+ return 0;
+}