-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
126 lines (114 loc) · 4.31 KB
/
main.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
/*
* main.c
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "supp.h"
#include "qdsv.h"
void dump_bytes(const char *title, uint8_t *b, uint n)
{
printf("%s:\n", title);
for (int i = 0; i < n; i++) {
printf("0x%02x, ", b[i]);
if (i % 8 == 7) printf("\n");
}
if (n % 8) printf("\n");
}
uint8_t _align4 seed[32];
uint8_t _align4 pk[32];
uint8_t _align4 sk[64];
uint8_t _align4 msg[32];
uint8_t _align4 sig[64];
int devrand;
typedef struct {
uint8_t _align4 sig[64];
uint8_t _align4 pk[32];
uint8_t _align4 msg[32];
} test_vector;
const test_vector tv[3] = {
// all 0.
{
.sig = { 0x85, 0xc6, 0xde, 0x61, 0xdf, 0x48, 0x81, 0x91, 0xb7, 0x29, 0x98,
0x47, 0x81, 0x5b, 0x16, 0xe4, 0xbb, 0x80, 0xaa, 0x2a, 0x1d, 0x5d, 0x78,
0x93, 0x52, 0x70, 0x8f, 0xd7, 0xd4, 0xf9, 0x97, 0xa7, 0xf3, 0x5c, 0x4b,
0x86, 0x00, 0x8f, 0xa1, 0x86, 0xe5, 0xd5, 0x2f, 0x21, 0x0d, 0x84, 0xab,
0x8b, 0xb6, 0x6f, 0xa2, 0x97, 0x87, 0x31, 0x24, 0xae, 0xf3, 0xb8, 0x87,
0x9f, 0x9e, 0xeb, 0x22, 0x02 },
.pk = { 0x58, 0x75, 0x4e, 0x99, 0xcc, 0x62, 0xf9, 0xa7, 0x39, 0xa1, 0x79,
0xf8, 0xeb, 0xa8, 0x26, 0xec, 0xbd, 0xdc, 0x3e, 0x9a, 0x85, 0xc5, 0x60,
0xa8, 0x3c, 0xca, 0x2f, 0xe4, 0xd5, 0x40, 0xef, 0xf6 },
.msg = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
},
// all 1.
{
.sig = { 0x90, 0x24, 0x66, 0x02, 0xd8, 0x29, 0x39, 0x21, 0x01, 0xe0, 0x1b,
0xb1, 0x02, 0x2d, 0x60, 0xeb, 0x9f, 0x15, 0x60, 0xc2, 0x1a, 0xcc, 0x7b,
0x1c, 0x50, 0x1f, 0x8b, 0xbf, 0xde, 0x7b, 0x81, 0x07, 0x26, 0x3c, 0x8d,
0xf6, 0x46, 0x1e, 0xbf, 0xc1, 0x2d, 0xa6, 0x8a, 0x20, 0x46, 0x8b, 0xf4,
0x53, 0x21, 0xd2, 0x41, 0x1f, 0x6d, 0x5a, 0x84, 0x89, 0xbd, 0x14, 0xc2,
0xc5, 0xac, 0xbb, 0xed, 0x03 },
.pk = { 0x75, 0x45, 0x04, 0xcd, 0x31, 0x2d, 0xc5, 0x2c, 0x43, 0x88, 0x36,
0xf2, 0xa2, 0xb4, 0x49, 0x9c, 0xac, 0xf0, 0x65, 0xb6, 0x86, 0x97, 0x0d,
0xf9, 0x30, 0x39, 0x89, 0xcb, 0xc4, 0x2e, 0x91, 0x2a },
.msg = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
},
// 0-31.
{
.sig = { 0xff, 0x93, 0xf3, 0xa1, 0xd8, 0x19, 0x07, 0x8b, 0x4f, 0x2f, 0xb4,
0xe1, 0xc5, 0x4c, 0x28, 0x99, 0x28, 0xfd, 0x9d, 0x55, 0xed, 0xeb, 0x80,
0xad, 0xa5, 0x75, 0x5d, 0xc5, 0x5e, 0x03, 0xa2, 0x43, 0x33, 0xd4, 0xd2,
0x89, 0xc6, 0x53, 0x7c, 0xe9, 0xce, 0x76, 0x1a, 0xa9, 0xe4, 0x15, 0xc0,
0xb2, 0x59, 0x96, 0x6a, 0xc8, 0x9c, 0xa3, 0x50, 0xb7, 0xe1, 0x99, 0x8a,
0x8b, 0xe2, 0xf6, 0x8a, 0x00 },
.pk = { 0xa2, 0xcb, 0xc7, 0xb8, 0xa4, 0x89, 0x8c, 0xa4, 0xd2, 0x6b, 0xdf,
0x8a, 0x34, 0x0b, 0xa4, 0xb8, 0xc9, 0x66, 0x14, 0x2b, 0x8f, 0x83, 0x57,
0xbf, 0xcb, 0x13, 0x8a, 0x66, 0xed, 0xcb, 0x3c, 0xba },
.msg = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
}
};
int test_sign_verify()
{
int n = read(devrand, seed, 32);
n += read(devrand, msg, 32);
qdsa_keypair(pk, sk, seed);
qdsa_sign(sig, msg, pk, sk);
return qdsa_verify(sig, pk, msg);
}
int main(void)
{
devrand = open("/dev/random", O_RDONLY);
if (devrand < 0) {
printf("Can't open /dev/random\n");
return -1;
}
printf("Test vectors for verify:\n");
for (int i = 0; i < 3; i++) {
if (qdsa_verify(tv[i].sig, tv[i].pk, tv[i].msg) == 0) {
printf("Pass %d\n", i + 1);
} else {
printf("Fail! %d\n", i + 1);
}
}
printf("Sign-verify test with random seeds and messages:\n");
for (int i = 0; i < 10; i++) {
if (test_sign_verify() == 0) {
printf("Pass %d\n", i + 1);
} else {
printf("Fail!\n");
dump_bytes("seed", seed, 32);
dump_bytes("msg", msg, 32);
break;
}
}
close(devrand);
return 0;
}
/* vim: set syn=c cin et sw=3 ts=3 tw=80 fo=cjMmnoqr: */