forked from SQISign/the-sqisign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sqisign.c
More file actions
110 lines (89 loc) · 2.26 KB
/
test_sqisign.c
File metadata and controls
110 lines (89 loc) · 2.26 KB
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
// SPDX-License-Identifier: Apache-2.0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <rng.h>
#include <sig.h>
#include <api.h>
#ifdef ENABLE_CT_TESTING
#include <valgrind/memcheck.h>
#endif
#ifdef ENABLE_CT_TESTING
static void print_hex(const unsigned char *hex, int len) {
unsigned char *copy = calloc(len, 1);
memcpy(copy, hex, len); // make a copy that we can tell valgrind is okay to leak
VALGRIND_MAKE_MEM_DEFINED(copy, len);
for (int i = 0; i < len; ++i) {
printf("%02x", copy[i]);
}
printf("\n");
free(copy);
}
#else
static void print_hex(const unsigned char *hex, int len) {
for (int i = 0; i < len; ++i) {
printf("%02x", hex[i]);
}
printf("\n");
}
#endif
static int test_sqisign() {
unsigned char *pk = calloc(CRYPTO_PUBLICKEYBYTES, 1);
unsigned char *sk = calloc(CRYPTO_SECRETKEYBYTES, 1);
unsigned char *sig = calloc(CRYPTO_BYTES + 32, 1);
unsigned char seed[48] = { 0 };
unsigned char msg[32] = { 0 };
unsigned long long msglen = 32;
randombytes_init(seed, NULL, 256);
printf("Testing Keygen, Sign, Open: %s\n", CRYPTO_ALGNAME);
int res = sqisign_keypair(pk, sk);
if (res != 0) {
res = -1;
goto err;
}
#ifdef ENABLE_CT_TESTING
VALGRIND_MAKE_MEM_DEFINED(pk, CRYPTO_PUBLICKEYBYTES);
#endif
unsigned long long smlen = CRYPTO_BYTES + 32;
res = sqisign_sign(sig, &smlen, msg, 32, sk);
if (res != 0) {
res = -1;
goto err;
}
printf("pk: ");
print_hex(pk, CRYPTO_PUBLICKEYBYTES);
printf("sk: ");
print_hex(sk, CRYPTO_SECRETKEYBYTES);
printf("sm: ");
print_hex(sig, smlen);
#ifdef ENABLE_CT_TESTING
VALGRIND_MAKE_MEM_DEFINED(sig, smlen);
#endif
res = sqisign_open(msg, &msglen, sig, smlen, pk);
if (res != 0) {
res = -1;
goto err;
}
sig[0] = ~sig[0];
res = sqisign_open(msg, &msglen, sig, smlen, pk);
if (res != 1) {
res = -1;
goto err;
} else {
res = 0;
}
err:
free(pk);
free(sk);
free(sig);
return res;
}
int main(int argc, char *argv[]) {
int rc = 0;
rc = test_sqisign();
if (rc != 0) {
printf("test failed for %s\n", argv[1]);
}
return rc;
}