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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mpi.h"
#include "mpi-priv.h"
/* #define OLD_WAY 1 */
/* This key is the 1024-bit test key used for speed testing of RSA private
** key ops.
*/
#define CONST const
static CONST unsigned char default_n[128] = {
0xc2, 0xae, 0x96, 0x89, 0xaf, 0xce, 0xd0, 0x7b, 0x3b, 0x35, 0xfd, 0x0f, 0xb1, 0xf4, 0x7a, 0xd1,
0x3c, 0x7d, 0xb5, 0x86, 0xf2, 0x68, 0x36, 0xc9, 0x97, 0xe6, 0x82, 0x94, 0x86, 0xaa, 0x05, 0x39,
0xec, 0x11, 0x51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xd6, 0x96, 0x08, 0x8a, 0xe8, 0x63, 0xe6, 0x3d, 0x6c, 0x20,
0x49, 0xc7, 0xaf, 0x0f, 0x25, 0x73, 0xd3, 0x69, 0x43, 0x3b, 0xf2, 0x32, 0xf8, 0x3d, 0x5e, 0xee,
0x7a, 0xca, 0xd6, 0x94, 0x55, 0xe5, 0xbd, 0x25, 0x34, 0x8d, 0x63, 0x40, 0xb5, 0x8a, 0xc3, 0x01
};
#define DEFAULT_ITERS 50
typedef clock_t timetype;
#define gettime(x) *(x) = clock()
#define subtime(a, b) a -= b
#define msec(x) ((clock_t)((double)x * 1000.0 / CLOCKS_PER_SEC))
#define sec(x) (x / CLOCKS_PER_SEC)
struct TimingContextStr {
timetype start;
timetype end;
timetype interval;
int minutes;
int seconds;
int millisecs;
};
typedef struct TimingContextStr TimingContext;
TimingContext *
CreateTimingContext(void)
{
return (TimingContext *)malloc(sizeof(TimingContext));
}
void
DestroyTimingContext(TimingContext *ctx)
{
free(ctx);
}
void
TimingBegin(TimingContext *ctx)
{
gettime(&ctx->start);
}
static void
timingUpdate(TimingContext *ctx)
{
ctx->millisecs = msec(ctx->interval) % 1000;
ctx->seconds = sec(ctx->interval);
ctx->minutes = ctx->seconds / 60;
ctx->seconds %= 60;
}
void
TimingEnd(TimingContext *ctx)
{
gettime(&ctx->end);
ctx->interval = ctx->end;
subtime(ctx->interval, ctx->start);
timingUpdate(ctx);
}
char *
TimingGenerateString(TimingContext *ctx)
{
static char sBuf[4096];
sprintf(sBuf, "%d minutes, %d.%03d seconds", ctx->minutes,
ctx->seconds, ctx->millisecs);
return sBuf;
}
static void
dumpBytes(unsigned char *b, int l)
{
int i;
if (l <= 0)
return;
for (i = 0; i < l; ++i) {
if (i % 16 == 0)
printf("\t");
printf(" %02x", b[i]);
if (i % 16 == 15)
printf("\n");
}
if ((i % 16) != 0)
printf("\n");
printf("\n");
}
static mp_err
testNewFuncs(const unsigned char *modulusBytes, int modulus_len)
{
mp_err mperr = MP_OKAY;
mp_int modulus;
unsigned char buf[512];
mperr = mp_init(&modulus);
mperr = mp_read_unsigned_octets(&modulus, modulusBytes, modulus_len);
mperr = mp_to_fixlen_octets(&modulus, buf, modulus_len);
mperr = mp_to_fixlen_octets(&modulus, buf, modulus_len + 1);
mperr = mp_to_fixlen_octets(&modulus, buf, modulus_len + 4);
mperr = mp_to_unsigned_octets(&modulus, buf, modulus_len);
mperr = mp_to_signed_octets(&modulus, buf, modulus_len + 1);
mp_clear(&modulus);
return mperr;
}
int
testModExp(const unsigned char *modulusBytes,
const unsigned int expo,
const unsigned char *input,
unsigned char *output,
int modulus_len)
{
mp_err mperr = MP_OKAY;
mp_int modulus;
mp_int base;
mp_int exponent;
mp_int result;
mperr = mp_init(&modulus);
mperr += mp_init(&base);
mperr += mp_init(&exponent);
mperr += mp_init(&result);
/* we initialize all mp_ints unconditionally, even if some fail.
** This guarantees that the DIGITS pointer is valid (even if null).
** So, mp_clear will do the right thing below.
*/
if (mperr == MP_OKAY) {
mperr = mp_read_unsigned_octets(&modulus,
modulusBytes + (sizeof default_n - modulus_len), modulus_len);
mperr += mp_read_unsigned_octets(&base, input, modulus_len);
mp_set(&exponent, expo);
if (mperr == MP_OKAY) {
#if OLD_WAY
mperr = s_mp_exptmod(&base, &exponent, &modulus, &result);
#else
mperr = mp_exptmod(&base, &exponent, &modulus, &result);
#endif
if (mperr == MP_OKAY) {
mperr = mp_to_fixlen_octets(&result, output, modulus_len);
}
}
}
mp_clear(&base);
mp_clear(&result);
mp_clear(&modulus);
mp_clear(&exponent);
return (int)mperr;
}
int
doModExp(const unsigned char *modulusBytes,
const unsigned char *exponentBytes,
const unsigned char *input,
unsigned char *output,
int modulus_len)
{
mp_err mperr = MP_OKAY;
mp_int modulus;
mp_int base;
mp_int exponent;
mp_int result;
mperr = mp_init(&modulus);
mperr += mp_init(&base);
mperr += mp_init(&exponent);
mperr += mp_init(&result);
/* we initialize all mp_ints unconditionally, even if some fail.
** This guarantees that the DIGITS pointer is valid (even if null).
** So, mp_clear will do the right thing below.
*/
if (mperr == MP_OKAY) {
mperr = mp_read_unsigned_octets(&modulus,
modulusBytes + (sizeof default_n - modulus_len), modulus_len);
mperr += mp_read_unsigned_octets(&exponent, exponentBytes, modulus_len);
mperr += mp_read_unsigned_octets(&base, input, modulus_len);
if (mperr == MP_OKAY) {
#if OLD_WAY
mperr = s_mp_exptmod(&base, &exponent, &modulus, &result);
#else
mperr = mp_exptmod(&base, &exponent, &modulus, &result);
#endif
if (mperr == MP_OKAY) {
mperr = mp_to_fixlen_octets(&result, output, modulus_len);
}
}
}
mp_clear(&base);
mp_clear(&result);
mp_clear(&modulus);
mp_clear(&exponent);
return (int)mperr;
}
int
main(int argc, char **argv)
{
TimingContext *timeCtx;
char *progName;
long iters = DEFAULT_ITERS;
unsigned int modulus_len;
int i;
int rv;
unsigned char buf[1024];
unsigned char buf2[1024];
progName = strrchr(argv[0], '/');
if (!progName)
progName = strrchr(argv[0], '\\');
progName = progName ? progName + 1 : argv[0];
if (argc >= 2) {
iters = atol(argv[1]);
}
if (argc >= 3) {
modulus_len = atol(argv[2]);
} else
modulus_len = sizeof default_n;
/* no library init function !? */
memset(buf, 0x41, sizeof buf);
if (iters < 2) {
testNewFuncs(default_n, modulus_len);
testNewFuncs(default_n + 1, modulus_len - 1);
testNewFuncs(default_n + 2, modulus_len - 2);
testNewFuncs(default_n + 3, modulus_len - 3);
rv = testModExp(default_n, 0, buf, buf2, modulus_len);
dumpBytes((unsigned char *)buf2, modulus_len);
rv = testModExp(default_n, 1, buf, buf2, modulus_len);
dumpBytes((unsigned char *)buf2, modulus_len);
rv = testModExp(default_n, 2, buf, buf2, modulus_len);
dumpBytes((unsigned char *)buf2, modulus_len);
rv = testModExp(default_n, 3, buf, buf2, modulus_len);
dumpBytes((unsigned char *)buf2, modulus_len);
}
rv = doModExp(default_n, default_d, buf, buf2, modulus_len);
if (rv != 0) {
fprintf(stderr, "Error in modexp operation:\n");
exit(1);
}
dumpBytes((unsigned char *)buf2, modulus_len);
timeCtx = CreateTimingContext();
TimingBegin(timeCtx);
i = iters;
while (i--) {
rv = doModExp(default_n, default_d, buf, buf2, modulus_len);
if (rv != 0) {
fprintf(stderr, "Error in modexp operation\n");
exit(1);
}
}
TimingEnd(timeCtx);
printf("%ld iterations in %s\n", iters, TimingGenerateString(timeCtx));
return 0;
}
|