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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
//======================================================================
//
// modexp_tester.c
// ---------------
// Simple test sw for the modexp.
//
//
// Author: Joachim Strombergson, Rob Austein,
// Copyright (c) 2014-2015, NORDUnet A/S All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// - Neither the name of the NORDUnet nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
//======================================================================
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "cryptech.h"
//------------------------------------------------------------------
// Global defines.
//------------------------------------------------------------------
#define VERBOSE 0
#define CHECK_WRITE 0
//------------------------------------------------------------------
// Robs macros. Scary scary.
//------------------------------------------------------------------
#define check(_expr_) \
do { \
if ((_expr_) != 0) { \
printf("%s failed\n", #_expr_); \
exit(1); \
} \
} while (0)
//------------------------------------------------------------------
// tc_w32()
//
// Write 32-bit word to given address.
//------------------------------------------------------------------
static void tc_w32(const off_t addr, const uint32_t data)
{
uint8_t w[4];
w[0] = data >> 24 & 0xff;
w[1] = data >> 16 & 0xff;
w[2] = data >> 8 & 0xff;
w[3] = data & 0xff;
check(tc_write(addr, w, 4));
}
//------------------------------------------------------------------
// tc_r32
//
// Read 32-bit word from given address.
//------------------------------------------------------------------
static uint32_t tc_r32(const off_t addr)
{
uint8_t w[4];
check(tc_read(addr, w, 4));
return (uint32_t)((w[0] << 24) + (w[1] << 16) + (w[2] << 8) + w[3]);
}
//------------------------------------------------------------------
// check_modexp_access
//
// Check that we can read from the modexp core by trying to
// read out the name and version.
//------------------------------------------------------------------
static void check_modexp_access(void)
{
uint8_t name0[4], name1[4], version[4];
printf("Trying to read the modexp core name\n");
check(tc_read(MODEXP_ADDR_NAME0, name0, sizeof(name0)));
check(tc_read(MODEXP_ADDR_NAME1, name1, sizeof(name1)));
check(tc_read(MODEXP_ADDR_VERSION, version, sizeof(version)));
printf("%4.4s%4.4s %4.4s\n\n", name0, name1, version);
}
//------------------------------------------------------------------
// check_modulus_mem()
//
// Check that we can write and read to the modulus memory.
//------------------------------------------------------------------
static void check_modulus_mem(void)
{
uint8_t i;
uint32_t j;
printf("Testing modulus mem access.\n");
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
// Write test data to modulus mempory.
for (i = 0 ; i < 64; i = i + 1) {
j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) +
((i * 4 + 1) << 8) + i * 4;
tc_w32(MODEXP_MODULUS_DATA, j);
}
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
// Read out test data from modulus mempory.
for (i = 0 ; i < 64 ; i = i + 4) {
printf("modulus mem: 0x%08x 0x%08x 0x%08x 0x%08x\n",
tc_r32(MODEXP_MODULUS_DATA),
tc_r32(MODEXP_MODULUS_DATA),
tc_r32(MODEXP_MODULUS_DATA),
tc_r32(MODEXP_MODULUS_DATA));
}
}
//------------------------------------------------------------------
// check_exponent_mem()
//
// Check that we can write and read to the exponent memory.
//------------------------------------------------------------------
static void check_exponent_mem(void)
{
uint8_t i;
uint32_t j;
printf("Testing exponent mem access.\n");
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
// Write test data to exponent memory.
for (i = 0 ; i < 64; i = i + 1) {
j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) +
((i * 4 + 1) << 8) + i * 4;
tc_w32(MODEXP_EXPONENT_DATA, j);
}
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
// Read out test data from exponent memory.
for (i = 0 ; i < 64 ; i = i + 4) {
printf("exponent mem: 0x%08x 0x%08x 0x%08x 0x%08x\n",
tc_r32(MODEXP_EXPONENT_DATA),
tc_r32(MODEXP_EXPONENT_DATA),
tc_r32(MODEXP_EXPONENT_DATA),
tc_r32(MODEXP_EXPONENT_DATA));
}
}
//------------------------------------------------------------------
// check_message_mem()
//
// Check that we can write and read to the message memory.
//------------------------------------------------------------------
static void check_message_mem(void)
{
uint8_t i;
uint32_t j;
printf("Testing message mem access.\n");
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
// Write test data to message memory.
for (i = 0 ; i < 64; i = i + 1) {
j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) +
((i * 4 + 1) << 8) + i * 4;
tc_w32(MODEXP_MESSAGE_DATA, j);
}
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
// Read out test data from messsage memory.
for (i = 0 ; i < 64 ; i = i + 4) {
printf("message mem: 0x%08x 0x%08x 0x%08x 0x%08x\n",
tc_r32(MODEXP_MESSAGE_DATA),
tc_r32(MODEXP_MESSAGE_DATA),
tc_r32(MODEXP_MESSAGE_DATA),
tc_r32(MODEXP_MESSAGE_DATA));
}
}
//------------------------------------------------------------------
// clear_mems()
//
// Zero fill the memories.
//------------------------------------------------------------------
static void clear_mems()
{
uint32_t i;
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
for (i = 0 ; i < 256 ; i++) {
tc_w32(MODEXP_MESSAGE_DATA, 0x00000000);
tc_w32(MODEXP_EXPONENT_DATA, 0x00000000);
tc_w32(MODEXP_MODULUS_DATA, 0x00000000);
}
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
}
//------------------------------------------------------------------
// dump_mems()
//
// Dump the first words from the memories.
//------------------------------------------------------------------
static void dump_mems()
{
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
printf("First words in messagee mem:\n");
printf("0x%08x 0x%08x 0x%08x 0x%08x\n",
tc_r32(MODEXP_MESSAGE_DATA), tc_r32(MODEXP_MESSAGE_DATA),
tc_r32(MODEXP_MESSAGE_DATA), tc_r32(MODEXP_MESSAGE_DATA));
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
printf("First words in exponent mem:\n");
printf("0x%08x 0x%08x 0x%08x 0x%08x\n",
tc_r32(MODEXP_EXPONENT_DATA), tc_r32(MODEXP_EXPONENT_DATA),
tc_r32(MODEXP_EXPONENT_DATA), tc_r32(MODEXP_EXPONENT_DATA));
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
printf("First words in modulus mem:\n");
printf("0x%08x 0x%08x 0x%08x 0x%08x\n",
tc_r32(MODEXP_MODULUS_DATA), tc_r32(MODEXP_MODULUS_DATA),
tc_r32(MODEXP_MODULUS_DATA), tc_r32(MODEXP_MODULUS_DATA));
tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000);
printf("First words in result mem:\n");
printf("0x%08x 0x%08x 0x%08x 0x%08x\n",
tc_r32(MODEXP_RESULT_DATA), tc_r32(MODEXP_RESULT_DATA),
tc_r32(MODEXP_RESULT_DATA), tc_r32(MODEXP_RESULT_DATA));
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000);
}
//------------------------------------------------------------------
// tc1()
//
// c = m ** e % N with the following (decimal) test values:
// m = 3
// e = 7
// n = 11 (0x0b)
// c = 3 ** 7 % 11 = 9
//------------------------------------------------------------------
static void tc1()
{
uint32_t result;
printf("Running TC1: 0x03 ** 0x07 mod 0x0b = 0x09\n");
// Write operands and set associated lengths.
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_MESSAGE_DATA, 0x00000003);
tc_w32(MODEXP_LENGTH, 0x00000001);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_DATA, 0x00000007);
tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000001);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_DATA, 0x0000000b);
tc_w32(MODEXP_MODULUS_LENGTH, 0x00000001);
// Start processing and wait for ready.
tc_w32(MODEXP_ADDR_CTRL, 0x00000001);
check(tc_wait_ready(MODEXP_ADDR_STATUS));
// Check result with expected value.
tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000);
result = tc_r32(MODEXP_RESULT_DATA);
if (result == 0x00000009)
printf("TC1: OK\n");
else
printf("TC1: Error. Expected 0x00000009, got 0x%08x\n", result);
}
//------------------------------------------------------------------
// tc2()
//
// c = m ** e % N with the following test values:
// m = 251 (0xfb)
// e = 251 (0xfb)
// n = 257 (0x101)
// c = 251 ** 251 % 257 = 183 (0xb7)
//------------------------------------------------------------------
static void tc2()
{
uint32_t result;
printf("Running TC2: 0xfb ** 0xfb mod 0x101 = 0xb7\n");
// Write operands and set associated lengths.
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_MESSAGE_DATA, 0x000000fb);
tc_w32(MODEXP_MESSAGE_DATA, 0x00000000);
tc_w32(MODEXP_LENGTH, 0x00000001);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_DATA, 0x000000fb);
tc_w32(MODEXP_EXPONENT_DATA, 0x00000000);
tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000001);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_DATA, 0x00000101);
tc_w32(MODEXP_MODULUS_DATA, 0x00000000);
tc_w32(MODEXP_MODULUS_LENGTH, 0x00000001);
// Start processing and wait for ready.
printf("TC2: Starting processing. Waiting for ready...\n");
tc_w32(MODEXP_ADDR_CTRL, 0x00000001);
check(tc_wait_ready(MODEXP_ADDR_STATUS));
printf("TC2: Ready seen.\n");
// Check result with expected value.
tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000);
result = tc_r32(MODEXP_RESULT_DATA);
if (result == 0x000000b7)
printf("TC2: OK\n");
else
printf("TC2: Error. Expected 0x000000b7, got 0x%08x\n", result);
}
//------------------------------------------------------------------
// tc3()
//
// c = m ** e % N with the following test values:
// m = 0x81
// e = 0x41
// n = 0x87
// c = 0x81 ** 0x41 % 0x87 = 0x36
//------------------------------------------------------------------
static void tc3()
{
uint32_t result;
printf("Running TC3: 0x81 ** 0x41 mod 0x87 = 0x36\n");
// Write operands and set associated lengths.
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_MESSAGE_DATA, 0x00000081);
tc_w32(MODEXP_LENGTH, 0x00000001);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_DATA, 0x00000041);
tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000001);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_DATA, 0x00000087);
tc_w32(MODEXP_MODULUS_LENGTH, 0x00000001);
// Start processing and wait for ready.
tc_w32(MODEXP_ADDR_CTRL, 0x00000001);
check(tc_wait_ready(MODEXP_ADDR_STATUS));
// Check result with expected value.
tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000);
result = tc_r32(MODEXP_RESULT_DATA);
if (result == 0x00000036)
printf("TC3: OK\n");
else
printf("TC3: Error. Expected 0x00000036, got 0x%08x\n", result);
}
//------------------------------------------------------------------
// tc4()
//
// c = m ** e % N with the following test values:
// m = 0x00000001946473e1
// e = 0xh000000010e85e74f
// n = 0x0000000170754797
// c = 0x000000007761ed4f
//
// These operands spans two 32-bit words.
//------------------------------------------------------------------
static void tc4()
{
uint32_t result0, result1;
uint32_t ready = 0;
printf("Running TC4: 0x00000001946473e1 ** 0xh000000010e85e74f mod 0x0000000170754797 = 0x000000007761ed4f\n");
// Write operands and set associated lengths.
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_MESSAGE_DATA, 0x00000001);
tc_w32(MODEXP_MESSAGE_DATA, 0x946473e1);
tc_w32(MODEXP_LENGTH, 0x00000002);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_DATA, 0x00000001);
tc_w32(MODEXP_EXPONENT_DATA, 0x0e85e74f);
tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000002);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_DATA, 0x00000001);
tc_w32(MODEXP_MODULUS_DATA, 0x70754797);
tc_w32(MODEXP_MODULUS_LENGTH, 0x00000002);
// Start processing and wait for ready.
tc_w32(MODEXP_ADDR_CTRL, 0x00000001);
while (!ready)
ready = tc_r32(MODEXP_ADDR_STATUS);
// Check result with expected value.
tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000);
result0 = tc_r32(MODEXP_RESULT_DATA);
result1 = tc_r32(MODEXP_RESULT_DATA);
if ((result0 == 0x00000000) && (result1 == 0x7761ed4f))
printf("TC4: OK\n");
else
printf("TC4: Error. Expected 0x000000007761ed4f, got 0x%08x%08x\n",
result0, result1);
}
//------------------------------------------------------------------
// tc5()
//
// c = m ** e % N with 128 bit operands.
//------------------------------------------------------------------
static void tc5()
{
uint32_t result0, result1, result2, result3;
printf("Running TC5: 128 bit operands.\n");
// Write operands and set associated lengths.
tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000);
tc_w32(MODEXP_MESSAGE_DATA, 0x29462882);
tc_w32(MODEXP_MESSAGE_DATA, 0x12caa2d5);
tc_w32(MODEXP_MESSAGE_DATA, 0xb80e1c66);
tc_w32(MODEXP_MESSAGE_DATA, 0x1006807f);
tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000);
tc_w32(MODEXP_EXPONENT_DATA, 0x3285c343);
tc_w32(MODEXP_EXPONENT_DATA, 0x2acbcb0f);
tc_w32(MODEXP_EXPONENT_DATA, 0x4d023228);
tc_w32(MODEXP_EXPONENT_DATA, 0x2ecc73db);
tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000);
tc_w32(MODEXP_MODULUS_DATA, 0x267d2f2e);
tc_w32(MODEXP_MODULUS_DATA, 0x51c216a7);
tc_w32(MODEXP_MODULUS_DATA, 0xda752ead);
tc_w32(MODEXP_MODULUS_DATA, 0x48d22d89);
tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000004);
tc_w32(MODEXP_MODULUS_LENGTH, 0x00000004);
// Start processing and wait for ready.
tc_w32(MODEXP_ADDR_CTRL, 0x00000001);
check(tc_wait_ready(MODEXP_ADDR_STATUS));
// Check result with expected value.
tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000);
result0 = tc_r32(MODEXP_RESULT_DATA);
result1 = tc_r32(MODEXP_RESULT_DATA);
result2 = tc_r32(MODEXP_RESULT_DATA);
result3 = tc_r32(MODEXP_RESULT_DATA);
if ((result0 == 0x0ddc404d) && (result1 == 0x91600596) &&
(result2 == 0x7425a8d8) && (result3 == 0xa066ca56))
printf("TC5: OK\n");
else
printf("TC5: Error. Expected 0x0ddc404d 0x91600596 0x7425a8d8 0xa066ca56, got 0x%08x %08x0x %08x %08x\n",
result0, result1, result2, result3);
}
//------------------------------------------------------------------
// main()
//------------------------------------------------------------------
int main(void)
{
check_modexp_access();
// tc_set_debug(1);
// check_modulus_mem();
// check_exponent_mem();
// check_message_mem();
tc1();
tc2();
tc3();
tc4();
return 0;
}
//======================================================================
// EOF modexp_tester.c
//======================================================================
|