forked from skair39/milagro-crypto-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mpin_sign_ZZZ.c
491 lines (427 loc) · 13 KB
/
test_mpin_sign_ZZZ.c
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
/**
* @file test_mpin_sign_ZZZ.c
* @author Kealan McCusker
* @brief Test function for M-Pin Signature
*
* LICENSE
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* Test M-Pin Signature */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mpin_ZZZ.h"
#include "pbc_support.h"
#include "randapi.h"
int main()
{
int i,PIN1,PIN2,rtn;
char id[256];
octet ID = {0,sizeof(id),id};
// Message to sign
char m[256];
octet M= {0,sizeof(m),m};
char x[PGS_ZZZ],y1[PGS_ZZZ],y2[PGS_ZZZ];
octet X= {0,sizeof(x),x};
octet Y1= {0,sizeof(y1),y1};
octet Y2= {0,sizeof(y2),y2};
/* Master secret shares */
char ms1[PGS_ZZZ], ms2[PGS_ZZZ];
octet MS1= {0,sizeof(ms1),ms1};
octet MS2= {0,sizeof(ms2),ms2};
/* Hash values of Client ID */
char hcid[PFS_ZZZ];
octet HCID= {0,sizeof(hcid), hcid};
/* Client secret and shares */
char cs1[2*PFS_ZZZ+1], cs2[2*PFS_ZZZ+1], sec[2*PFS_ZZZ+1];
octet SEC= {0,sizeof(sec),sec};
octet CS1= {0,sizeof(cs1), cs1};
octet CS2= {0,sizeof(cs2), cs2};
/* Server secret and shares */
char ss1[4*PFS_ZZZ], ss2[4*PFS_ZZZ], serverSecret[4*PFS_ZZZ];
octet ServerSecret= {0,sizeof(serverSecret),serverSecret};
octet SS1= {0,sizeof(ss1),ss1};
octet SS2= {0,sizeof(ss2),ss2};
/* Time Permit and shares */
char tp1[2*PFS_ZZZ+1], tp2[2*PFS_ZZZ+1], tp[2*PFS_ZZZ+1];
octet TP= {0,sizeof(tp),tp};
octet TP1= {0,sizeof(tp1),tp1};
octet TP2= {0,sizeof(tp2),tp2};
/* Token stored on computer */
char token[2*PFS_ZZZ+1];
octet TOKEN= {0,sizeof(token),token};
char ut[2*PFS_ZZZ+1];
octet UT= {0,sizeof(ut),ut};
char hid[2*PFS_ZZZ+1],htid[2*PFS_ZZZ+1];
octet HID= {0,sizeof(hid),hid};
octet HTID= {0,sizeof(htid),htid};
char e[12*PFS_ZZZ], f[12*PFS_ZZZ];
octet E= {0,sizeof(e),e};
octet F= {0,sizeof(f),f};
int TimeValue = 0;
PIN1 = 1234;
PIN2 = 1234;
/* Assign the End-User an ID */
char* user = "testuser@miracl.com";
OCT_jstring(&ID,user);
printf("CLIENT: ID %s\n", user);
int date = 0;
char seed[100] = {0};
octet SEED = {0,sizeof(seed),seed};
csprng RNG;
/* unrandom seed value! */
SEED.len=100;
for (i=0; i<100; i++) SEED.val[i]=i+1;
/* initialise random number generator */
CREATE_CSPRNG(&RNG,&SEED);
/* Hash ID */
HASH_ID(HASH_TYPE_MPIN_ZZZ,&ID,&HCID);
OCT_output(&HCID);
/* Generate Client master secret for MIRACL and Customer */
rtn = MPIN_ZZZ_RANDOM_GENERATE(&RNG,&MS1);
if (rtn != 0)
{
printf("MPIN_ZZZ_RANDOM_GENERATE(&RNG,&MS1) Error %d\n", rtn);
return 1;
}
rtn = MPIN_ZZZ_RANDOM_GENERATE(&RNG,&MS2);
if (rtn != 0)
{
printf("MPIN_ZZZ_RANDOM_GENERATE(&RNG,&MS2) Error %d\n", rtn);
return 1;
}
printf("MASTER SECRET MIRACL:= 0x");
OCT_output(&MS1);
printf("MASTER SECRET CUSTOMER:= 0x");
OCT_output(&MS2);
/* Generate server secret shares */
rtn = MPIN_ZZZ_GET_SERVER_SECRET(&MS1,&SS1);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_SERVER_SECRET(&MS1,&SS1) Error %d\n", rtn);
return 1;
}
rtn = MPIN_ZZZ_GET_SERVER_SECRET(&MS2,&SS2);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_SERVER_SECRET(&MS2,&SS2) Error %d\n", rtn);
return 1;
}
printf("SS1 = 0x");
OCT_output(&SS1);
printf("SS2 = 0x");
OCT_output(&SS2);
/* Combine server secret share */
rtn = MPIN_ZZZ_RECOMBINE_G2(&SS1, &SS2, &ServerSecret);
if (rtn != 0)
{
printf("MPIN_ZZZ_RECOMBINE_G2(&SS1, &SS2, &ServerSecret) Error %d\n", rtn);
return 1;
}
printf("ServerSecret = 0x");
OCT_output(&ServerSecret);
/* Generate client secret shares */
rtn = MPIN_ZZZ_GET_CLIENT_SECRET(&MS1,&HCID,&CS1);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_CLIENT_SECRET(&MS1,&HCID,&CS1) Error %d\n", rtn);
return 1;
}
rtn = MPIN_ZZZ_GET_CLIENT_SECRET(&MS2,&HCID,&CS2);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_CLIENT_SECRET(&MS2,&HCID,&CS2) Error %d\n", rtn);
return 1;
}
printf("CS1 = 0x");
OCT_output(&CS1);
printf("CS2 = 0x");
OCT_output(&CS2);
/* Combine client secret shares : TOKEN is the full client secret */
rtn = MPIN_ZZZ_RECOMBINE_G1(&CS1, &CS2, &TOKEN);
if (rtn != 0)
{
printf("MPIN_ZZZ_RECOMBINE_G1(&CS1, &CS2, &TOKEN) Error %d\n", rtn);
return 1;
}
printf("Client Secret = 0x");
OCT_output(&TOKEN);
/* Client extracts PIN1 from secret to create Token */
rtn = MPIN_ZZZ_EXTRACT_PIN(HASH_TYPE_MPIN_ZZZ,&ID, PIN1, &TOKEN);
if (rtn != 0)
{
printf("MPIN_ZZZ_EXTRACT_PIN( &ID, PIN, &TOKEN) Error %d\n", rtn);
return 1;
}
printf("Token = 0x");
OCT_output(&TOKEN);
/*********** Fix values for date and time ********/
printf("Fix values for date and time\n");
date = 17072;
printf("Date %d \n", date);
TimeValue = 1475079474;
printf("TimeValue %d \n", TimeValue);
rtn = MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS1,&HCID,&TP1);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS1,&HCID,&TP1) Error %d\n", rtn);
return 1;
}
rtn = MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS2,&HCID,&TP2);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS2,&HCID,&TP2) Error %d\n", rtn);
return 1;
}
printf("TP1 = 0x");
OCT_output(&TP1);
printf("TP2 = 0x");
OCT_output(&TP2);
/* Combine Time Permit shares */
rtn = MPIN_ZZZ_RECOMBINE_G1(&TP1, &TP2, &TP);
if (rtn != 0)
{
printf("MPIN_ZZZ_RECOMBINE_G1(&TP1, &TP2, &TP) Error %d\n", rtn);
return 1;
}
printf("Time Permit = 0x");
OCT_output(&TP);
/* Good signature */
printf("***** Fixed good signature *****\n");
/* Client */
char* message = "sign this message";
OCT_jstring(&M,message);
rtn = MPIN_ZZZ_CLIENT(HASH_TYPE_MPIN_ZZZ,date,&ID,&RNG,&X,PIN2,&TOKEN,&SEC,NULL,&UT,&TP,&M,TimeValue,&Y1);
if (rtn != 0)
{
printf("MPIN_ZZZ_CLIENT ERROR %d\n", rtn);
return 1;
}
printf("Y1 = 0x");
OCT_output(&Y1);
printf("V = 0x");
OCT_output(&SEC);
/* Server */
rtn = MPIN_ZZZ_SERVER(HASH_TYPE_MPIN_ZZZ,date,&HID,&HTID,&Y2,&ServerSecret,NULL,&UT,&SEC,&E,&F,&ID,&M,TimeValue,NULL);
printf("Y2 = 0x");
OCT_output(&Y2);
if (rtn != 0)
{
printf("TEST FAILED: valid signature not detected %d\n", rtn);
return 1;
}
else
{
printf("SUCCESS Error Code %d\n", rtn);
}
/* Time stamp expired */
printf("***** Fixed time stamp expired *****\n");
/* Client */
OCT_clear(&M);
message = "sign this message";
OCT_jstring(&M,message);
rtn = MPIN_ZZZ_CLIENT(HASH_TYPE_MPIN_ZZZ,date,&ID,&RNG,&X,PIN2,&TOKEN,&SEC,NULL,&UT,&TP,&M,TimeValue,&Y1);
if (rtn != 0)
{
printf("MPIN_ZZZ_CLIENT ERROR %d\n", rtn);
return 1;
}
printf("Y1 = 0x");
OCT_output(&Y1);
printf("V = 0x");
OCT_output(&SEC);
/* Server */
TimeValue += 10;
rtn = MPIN_ZZZ_SERVER(HASH_TYPE_MPIN_ZZZ,date,&HID,&HTID,&Y2,&ServerSecret,NULL,&UT,&SEC,&E,&F,&ID,&M,TimeValue,NULL);
printf("Y2 = 0x");
OCT_output(&Y2);
if (rtn != -19)
{
printf("TEST FAILED: Invalid signature not detected %d\n", rtn);
return 1;
}
else
{
printf("SUCCESS Error Code %d\n", rtn);
}
/* Invalid data */
printf("***** Fixed invalid data *****\n");
/* Client */
OCT_clear(&M);
message = "sign this message";
OCT_jstring(&M,message);
OCT_clear(&Y1);
rtn = MPIN_ZZZ_CLIENT(HASH_TYPE_MPIN_ZZZ,date,&ID,&RNG,&X,PIN2,&TOKEN,&SEC,NULL,&UT,&TP,&M,TimeValue,&Y1);
if (rtn != 0)
{
printf("MPIN_ZZZ_CLIENT ERROR %d\n", rtn);
return 1;
}
printf("Y1 = 0x");
OCT_output(&Y1);
printf("V = 0x");
OCT_output(&SEC);
/* Server */
OCT_clear(&M);
message = "bad message";
OCT_jstring(&M,message);
rtn = MPIN_ZZZ_SERVER(HASH_TYPE_MPIN_ZZZ,date,&HID,&HTID,&Y2,&ServerSecret,NULL,&UT,&SEC,&E,&F,&ID,&M,TimeValue,NULL);
printf("Y2 = 0x");
OCT_output(&Y2);
if (rtn != -19)
{
printf("TEST FAILED: Invalid signature not detected %d\n", rtn);
return 1;
}
else
{
printf("SUCCESS Error Code %d\n", rtn);
}
/*********** Current date and time ********/
printf("Current date and time\n");
date = today();
printf("Date %d \n", date);
TimeValue = GET_TIME();
printf("TimeValue %d \n", TimeValue);
/* Generate Time Permit shares */
rtn = MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS1,&HCID,&TP1);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS1,&HCID,&TP1) Error %d\n", rtn);
return 1;
}
rtn = MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS2,&HCID,&TP2);
if (rtn != 0)
{
printf("MPIN_ZZZ_GET_CLIENT_PERMIT(HASH_TYPE_MPIN_ZZZ,date,&MS2,&HCID,&TP2) Error %d\n", rtn);
return 1;
}
printf("TP1 = 0x");
OCT_output(&TP1);
printf("TP2 = 0x");
OCT_output(&TP2);
/* Combine Time Permit shares */
rtn = MPIN_ZZZ_RECOMBINE_G1(&TP1, &TP2, &TP);
if (rtn != 0)
{
printf("MPIN_ZZZ_RECOMBINE_G1(&TP1, &TP2, &TP) Error %d\n", rtn);
return 1;
}
printf("Time Permit = 0x");
OCT_output(&TP);
/* Good signature */
printf("***** Good signature *****\n");
;
/* Client */
message = "sign this message";
OCT_jstring(&M,message);
rtn = MPIN_ZZZ_CLIENT(HASH_TYPE_MPIN_ZZZ,date,&ID,&RNG,&X,PIN2,&TOKEN,&SEC,NULL,&UT,&TP,&M,TimeValue,&Y1);
if (rtn != 0)
{
printf("MPIN_ZZZ_CLIENT ERROR %d\n", rtn);
return 1;
}
printf("Y1 = 0x");
OCT_output(&Y1);
printf("V = 0x");
OCT_output(&SEC);
/* Server */
rtn = MPIN_ZZZ_SERVER(HASH_TYPE_MPIN_ZZZ,date,&HID,&HTID,&Y2,&ServerSecret,NULL,&UT,&SEC,&E,&F,&ID,&M,TimeValue,NULL);
printf("Y2 = 0x");
OCT_output(&Y2);
if (rtn != 0)
{
printf("TEST FAILED: valid signature not detected %d\n", rtn);
return 1;
}
else
{
printf("SUCCESS Error Code %d\n", rtn);
}
/* Time stamp expired */
printf("***** Time stamp expired *****\n");
/* Client */
OCT_clear(&M);
message = "sign this message";
OCT_jstring(&M,message);
TimeValue = GET_TIME();
printf("TimeValue %d \n", TimeValue);
rtn = MPIN_ZZZ_CLIENT(HASH_TYPE_MPIN_ZZZ,date,&ID,&RNG,&X,PIN2,&TOKEN,&SEC,NULL,&UT,&TP,&M,TimeValue,&Y1);
if (rtn != 0)
{
printf("MPIN_ZZZ_CLIENT ERROR %d\n", rtn);
return 1;
}
printf("Y1 = 0x");
OCT_output(&Y1);
printf("V = 0x");
OCT_output(&SEC);
/* Server */
TimeValue += 10;
rtn = MPIN_ZZZ_SERVER(HASH_TYPE_MPIN_ZZZ,date,&HID,&HTID,&Y2,&ServerSecret,NULL,&UT,&SEC,&E,&F,&ID,&M,TimeValue,NULL);
printf("Y2 = 0x");
OCT_output(&Y2);
if (rtn != -19)
{
printf("TEST FAILED: Invalid signature not detected %d\n", rtn);
return 1;
}
else
{
printf("SUCCESS Error Code %d\n", rtn);
}
/* Invalid data */
printf("***** Invalid data *****\n");
/* Client */
OCT_clear(&M);
message = "sign this message";
OCT_jstring(&M,message);
TimeValue = GET_TIME();
printf("TimeValue %d \n", TimeValue);
rtn = MPIN_ZZZ_CLIENT(HASH_TYPE_MPIN_ZZZ,date,&ID,&RNG,&X,PIN2,&TOKEN,&SEC,NULL,&UT,&TP,&M,TimeValue,&Y1);
if (rtn != 0)
{
printf("MPIN_ZZZ_CLIENT ERROR %d\n", rtn);
return 1;
}
printf("Y1 = 0x");
OCT_output(&Y1);
printf("V = 0x");
OCT_output(&SEC);
/* Server */
OCT_clear(&M);
message = "bad message";
OCT_jstring(&M,message);
rtn = MPIN_ZZZ_SERVER(HASH_TYPE_MPIN_ZZZ,date,&HID,&HTID,&Y2,&ServerSecret,NULL,&UT,&SEC,&E,&F,&ID,&M,TimeValue,NULL);
printf("Y2 = 0x");
OCT_output(&Y2);
if (rtn != -19)
{
printf("TEST FAILED: Invalid signature not detected %d\n", rtn);
return 1;
}
else
{
printf("SUCCESS Error Code %d\n", rtn);
}
printf("TEST PASSED\n");
KILL_CSPRNG(&RNG);
return 0;
}