xmss-KAT-generator/ui/sign.c

61 lines
1.7 KiB
C
Raw Normal View History

#include "../params.h"
#include "../xmss_core.h"
#include <stdio.h>
#define MLEN 32
2017-10-26 12:47:29 +01:00
#ifdef XMSSMT
#define XMSS_PARSE_OID xmssmt_parse_oid
#define XMSS_CORE_SIGN xmssmt_core_sign
#else
#define XMSS_PARSE_OID xmss_parse_oid
#define XMSS_CORE_SIGN xmss_core_sign
#endif
int main(int argc, char **argv) {
FILE *keypair;
xmss_params params;
uint32_t oid_pk;
uint32_t oid_sk;
if (argc != 2) {
fprintf(stderr, "Expected keypair filename as only parameter, "
"and the message via stdin.\n"
"The keypair is updated with the changed state, "
"and the message + signature is output via stdout.\n");
return -1;
}
keypair = fopen(argv[1], "r+b");
if (keypair == NULL) {
fprintf(stderr, "Could not open keypair file.\n");
return -1;
}
/* Read the OID from the public key, as we need its length to seek past it */
fread(&oid_pk, 1, XMSS_OID_LEN, keypair);
2017-10-26 12:47:29 +01:00
XMSS_PARSE_OID(&params, oid_pk);
/* fseek past the public key */
fseek(keypair, params.pk_bytes, SEEK_CUR);
/* This is the OID we're actually going to use. Likely the same, but still. */
fread(&oid_sk, 1, XMSS_OID_LEN, keypair);
2017-10-26 12:47:29 +01:00
XMSS_PARSE_OID(&params, oid_sk);
unsigned char sk[params.sk_bytes];
unsigned char m[MLEN];
unsigned char sm[params.sig_bytes + MLEN];
unsigned long long smlen;
fread(sk, 1, params.sk_bytes, keypair);
fread(m, 1, MLEN, stdin);
2017-10-26 12:47:29 +01:00
XMSS_CORE_SIGN(&params, sk, sm, &smlen, m, MLEN);
fseek(keypair, -((long int)params.sk_bytes), SEEK_CUR);
fwrite(sk, 1, params.sk_bytes, keypair);
fwrite(sm, 1, params.sig_bytes + MLEN, stdout);
fclose(keypair);
fclose(stdout);
}