-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainbow-verify.c
87 lines (71 loc) · 1.81 KB
/
rainbow-verify.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
#include <stdio.h>
#include "rainbow_config.h"
//#include "rainbow.h"
#include "hash_utils.h"
#include "utils.h"
#include "api.h"
#ifndef SMALL_SEC_KEY_LEN
#define SMALL_SEC_KEY_LEN CRYPTO_SECRETKEYBYTES
#endif
int main(int argc, char** argv) {
printf("%s\n", CRYPTO_ALGNAME);
printf("sk size: %lu\n", SMALL_SEC_KEY_LEN);
printf("pk size: %d\n", CRYPTO_PUBLICKEYBYTES);
printf("hash size: %d\n", _HASH_LEN);
printf("signature size: %d\n\n", CRYPTO_BYTES);
if (4 != argc) {
printf("Usage:\n\n\trainbow-verify pk_file_name signature_file_name "
"message_file_name\n\n");
return -1;
}
// uint8_t pk[_PUB_KEY_LEN];
uint8_t* pk = (uint8_t*) malloc(CRYPTO_PUBLICKEYBYTES);
FILE* fp;
int r;
fp = fopen(argv[1], "r");
if (NULL == fp) {
printf("fail to open public key file.\n");
return -1;
}
r = byte_fget(fp, pk, CRYPTO_PUBLICKEYBYTES);
fclose(fp);
if (CRYPTO_PUBLICKEYBYTES != r) {
printf("fail to load key file.\n");
return -1;
}
unsigned char* msg = NULL;
unsigned long long mlen = 0;
r = byte_read_file(&msg, &mlen, argv[3]);
if (0 != r) {
printf("fail to read message file.\n");
return -1;
}
unsigned char* signature = malloc(mlen + CRYPTO_BYTES);
if (NULL == signature) {
printf("alloc memory for signature buffer fail.\n");
return -1;
}
memcpy(signature, msg, mlen);
fp = fopen(argv[2], "r");
if (NULL == fp) {
printf("fail to open signature file.\n");
return -1;
}
r = byte_fget(fp, signature + mlen, CRYPTO_BYTES);
fclose(fp);
if (CRYPTO_BYTES != r) {
printf("fail to load signature file.\n");
return -1;
}
r = crypto_sign_open(msg, &mlen, signature, mlen + CRYPTO_BYTES, pk);
free(msg);
free(signature);
free(pk);
if (0 == r) {
printf("%s verification success.\n", CRYPTO_ALGNAME);
return 0;
} else {
printf("Verification fail.\n");
return -1;
}
}