xmss-KAT-generator/ui/open.c

90 lines
2.2 KiB
C
Raw Normal View 히스토리

#include <stdio.h>
#include <stdlib.h>
2017-10-26 13:47:34 +01:00
#include "../params.h"
#include "../xmss.h"
#include "../utils.h"
2017-10-26 12:47:29 +01:00
#ifdef XMSSMT
#define XMSS_PARSE_OID xmssmt_parse_oid
#define XMSS_SIGN_OPEN xmssmt_sign_open
2017-10-26 12:47:29 +01:00
#else
#define XMSS_PARSE_OID xmss_parse_oid
#define XMSS_SIGN_OPEN xmss_sign_open
2017-10-26 12:47:29 +01:00
#endif
int main(int argc, char **argv) {
2017-10-26 13:47:34 +01:00
FILE *keypair_file;
FILE *sm_file;
xmss_params params;
uint32_t oid = 0;
uint8_t buffer[XMSS_OID_LEN];
int parse_oid_result;
2017-10-26 13:47:34 +01:00
unsigned long long smlen;
int ret;
2017-10-26 13:47:34 +01:00
if (argc != 3) {
fprintf(stderr, "Expected keypair and signature + message filenames "
"as two parameters.\n"
"Keypair file needs only to contain the public key.\n"
"The return code 0 indicates verification success.\n");
return -1;
}
2017-10-26 13:47:34 +01:00
keypair_file = fopen(argv[1], "rb");
if (keypair_file == NULL) {
fprintf(stderr, "Could not open keypair file.\n");
return -1;
}
2017-10-26 13:47:34 +01:00
sm_file = fopen(argv[2], "rb");
if (sm_file == NULL) {
fprintf(stderr, "Could not open signature + message file.\n");
fclose(keypair_file);
2017-10-26 13:47:34 +01:00
return -1;
}
/* Find out the message length. */
fseek(sm_file, 0, SEEK_END);
smlen = ftell(sm_file);
fread(&buffer, 1, XMSS_OID_LEN, keypair_file);
oid = (uint32_t)bytes_to_ull(buffer, XMSS_OID_LEN);
parse_oid_result = XMSS_PARSE_OID(&params, oid);
if (parse_oid_result != 0) {
fprintf(stderr, "Error parsing oid.\n");
fclose(keypair_file);
fclose(sm_file);
return parse_oid_result;
}
unsigned char pk[XMSS_OID_LEN + params.pk_bytes];
unsigned char *sm = malloc(smlen);
unsigned char *m = malloc(smlen);
unsigned long long mlen;
2017-10-26 13:47:34 +01:00
fseek(keypair_file, 0, SEEK_SET);
fseek(sm_file, 0, SEEK_SET);
fread(pk, 1, XMSS_OID_LEN + params.pk_bytes, keypair_file);
fread(sm, 1, smlen, sm_file);
2017-10-26 13:47:34 +01:00
ret = XMSS_SIGN_OPEN(m, &mlen, sm, smlen, pk);
if (ret) {
printf("Verification failed!\n");
}
else {
printf("Verification succeeded.\n");
}
2017-10-26 13:47:34 +01:00
fclose(keypair_file);
fclose(sm_file);
free(m);
free(sm);
return ret;
}