1 /* ===================================================================
2 *
3 * Copyright (c) 2018, Helder Eijs <helderijs@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <assert.h>
33
34 #include "common.h"
35 #include "endianess.h"
36 #include "multiply.h"
37 #include "mont.h"
38 #include "ec.h"
39 #include "modexp_utils.h"
40
41 #include <sys/time.h>
42
43 #include "ec_ws.c"
44
main(void)45 int main(void)
46 {
47 const uint8_t p256_mod[32] = "\xff\xff\xff\xff\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
48 const uint8_t b[32] = "\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b";
49 const uint8_t order[32] = "\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xbc\xe6\xfa\xad\xa7\x17\x9e\x84\xf3\xb9\xca\xc2\xfc\x63\x25\x51";
50 const uint8_t p256_Gx[32] = "\x6b\x17\xd1\xf2\xe1\x2c\x42\x47\xf8\xbc\xe6\xe5\x63\xa4\x40\xf2\x77\x03\x7d\x81\x2d\xeb\x33\xa0\xf4\xa1\x39\x45\xd8\x98\xc2\x96";
51 const uint8_t p256_Gy[32] = "\x4f\xe3\x42\xe2\xfe\x1a\x7f\x9b\x8e\xe7\xeb\x4a\x7c\x0f\x9e\x16\x2b\xce\x33\x57\x6b\x31\x5e\xce\xcb\xb6\x40\x68\x37\xbf\x51\xf5";
52 uint8_t x[32], y[32];
53 uint8_t exp[32];
54 EcContext *ec_ctx;
55 EcPoint *ecp = NULL;
56 EcPoint *gp = NULL;
57 unsigned i;
58 struct timeval start, stop;
59 double duration_ms, rate;
60
61 #define ITERATIONS 1000U
62
63 /* Make almost-worst case exponent */
64 for (i=0; i<32; i++) {
65 exp[i] = (uint8_t)(0xFF - i);
66 }
67
68 ec_ws_new_context(&ec_ctx, p256_mod, b, order, 32, /* seed */ 4);
69
70 ec_ws_new_point(&gp, p256_Gx, p256_Gy, 32, ec_ctx);
71 ec_ws_clone(&ecp, gp);
72
73 /** Scalar multiplications by G **/
74 gettimeofday(&start, NULL);
75 for (i=0; i<ITERATIONS; i++) {
76 ec_ws_copy(ecp, gp);
77 ec_ws_scalar(ecp, exp, 32, 0xFFF);
78 ec_ws_get_xy(x, y, 32, ecp);
79 }
80 gettimeofday(&stop, NULL);
81 duration_ms = (double)(stop.tv_sec - start.tv_sec) * 1000 + (double)(stop.tv_usec - start.tv_usec) / 1000;
82 rate = ITERATIONS / (duration_ms/1000);
83 printf("Speed (scalar mult by G) = %.0f op/s\n", rate);
84
85 ec_ws_get_xy(x, y, 32, ecp);
86 printf("X: ");
87 for (i=0; i<32; i++)
88 printf("%02X", x[i]);
89 printf("\n");
90 printf("Y: ");
91 for (i=0; i<32; i++)
92 printf("%02X", y[i]);
93 printf("\n");
94
95 #if 1
96 /** Scalar multiplications by arbitrary point **/
97 ec_ws_double(ecp);
98 gettimeofday(&start, NULL);
99 for (i=0; i<ITERATIONS; i++) {
100 ec_ws_scalar(ecp, exp, 32, 0xFFF);
101 ec_ws_get_xy(x, y, 32, ecp);
102 }
103 gettimeofday(&stop, NULL);
104 duration_ms = (double)(stop.tv_sec - start.tv_sec) * 1000 + (double)(stop.tv_usec - start.tv_usec) / 1000;
105 rate = ITERATIONS / (duration_ms/1000);
106 printf("Speed (scalar mult by P) = %.0f op/s\n", rate);
107
108 ec_ws_get_xy(x, y, 32, ecp);
109 printf("X: ");
110 for (i=0; i<32; i++)
111 printf("%02X", x[i]);
112 printf("\n");
113 printf("Y: ");
114 for (i=0; i<32; i++)
115 printf("%02X", y[i]);
116 printf("\n");
117 #endif
118
119 ec_free_point(gp);
120 ec_free_point(ecp);
121 ec_free_context(ec_ctx);
122
123 return 0;
124 }
125