384b228c58
Previous code allocated an array on the stack of mlen bytes, but it should be possible to also sign heap-space messages. By relying on the fact that sm and m fit the message + signature, we move the message so that 4*n bytes of prefix can be added.
79 строки
1.8 KiB
C
79 строки
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "../params.h"
|
|
#include "../xmss.h"
|
|
|
|
#ifdef XMSSMT
|
|
#define XMSS_PARSE_OID xmssmt_parse_oid
|
|
#define XMSS_SIGN_OPEN xmssmt_sign_open
|
|
#else
|
|
#define XMSS_PARSE_OID xmss_parse_oid
|
|
#define XMSS_SIGN_OPEN xmss_sign_open
|
|
#endif
|
|
|
|
int main(int argc, char **argv) {
|
|
FILE *keypair_file;
|
|
FILE *sm_file;
|
|
|
|
xmss_params params;
|
|
uint32_t oid;
|
|
|
|
unsigned long long smlen;
|
|
int ret;
|
|
|
|
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;
|
|
}
|
|
|
|
keypair_file = fopen(argv[1], "rb");
|
|
if (keypair_file == NULL) {
|
|
fprintf(stderr, "Could not open keypair file.\n");
|
|
return -1;
|
|
}
|
|
|
|
sm_file = fopen(argv[2], "rb");
|
|
if (sm_file == NULL) {
|
|
fprintf(stderr, "Could not open signature + message file.\n");
|
|
return -1;
|
|
}
|
|
|
|
/* Find out the message length. */
|
|
fseek(sm_file, 0, SEEK_END);
|
|
smlen = ftell(sm_file);
|
|
|
|
fread(&oid, 1, XMSS_OID_LEN, keypair_file);
|
|
XMSS_PARSE_OID(¶ms, oid);
|
|
|
|
unsigned char pk[XMSS_OID_LEN + params.pk_bytes];
|
|
unsigned char *sm = malloc(smlen);
|
|
unsigned char *m = malloc(smlen);
|
|
unsigned long long mlen;
|
|
|
|
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);
|
|
|
|
ret = XMSS_SIGN_OPEN(m, &mlen, sm, smlen, pk);
|
|
|
|
if (ret) {
|
|
printf("Verification failed!\n");
|
|
}
|
|
else {
|
|
printf("Verification succeeded.\n");
|
|
}
|
|
|
|
fclose(keypair_file);
|
|
fclose(sm_file);
|
|
|
|
free(m);
|
|
free(sm);
|
|
|
|
return ret;
|
|
}
|