2017-10-17 16:11:18 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-10-26 13:47:34 +01:00
|
|
|
#include "../params.h"
|
|
|
|
#include "../xmss.h"
|
2017-10-17 16:11:18 +01:00
|
|
|
|
2017-10-26 12:47:29 +01:00
|
|
|
#ifdef XMSSMT
|
|
|
|
#define XMSS_PARSE_OID xmssmt_parse_oid
|
2017-10-26 13:24:10 +01:00
|
|
|
#define XMSS_SIGN xmssmt_sign
|
2017-10-26 12:47:29 +01:00
|
|
|
#else
|
|
|
|
#define XMSS_PARSE_OID xmss_parse_oid
|
2017-10-26 13:24:10 +01:00
|
|
|
#define XMSS_SIGN xmss_sign
|
2017-10-26 12:47:29 +01:00
|
|
|
#endif
|
|
|
|
|
2017-10-17 16:11:18 +01:00
|
|
|
int main(int argc, char **argv) {
|
2017-10-26 13:47:34 +01:00
|
|
|
FILE *keypair_file;
|
|
|
|
FILE *m_file;
|
|
|
|
|
2017-10-17 16:11:18 +01:00
|
|
|
xmss_params params;
|
|
|
|
uint32_t oid_pk;
|
|
|
|
uint32_t oid_sk;
|
|
|
|
|
2017-10-26 13:47:34 +01:00
|
|
|
unsigned long long mlen;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Expected keypair and message filenames as two "
|
|
|
|
"parameters.\n"
|
2017-10-17 16:11:18 +01:00
|
|
|
"The keypair is updated with the changed state, "
|
|
|
|
"and the message + signature is output via stdout.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:47:34 +01:00
|
|
|
keypair_file = fopen(argv[1], "r+b");
|
|
|
|
if (keypair_file == NULL) {
|
2017-10-17 16:11:18 +01:00
|
|
|
fprintf(stderr, "Could not open keypair file.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:47:34 +01:00
|
|
|
m_file = fopen(argv[2], "rb");
|
|
|
|
if (m_file == NULL) {
|
|
|
|
fprintf(stderr, "Could not open message file.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find out the message length. */
|
|
|
|
fseek(m_file, 0, SEEK_END);
|
|
|
|
mlen = ftell(m_file);
|
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Read the OID from the public key, as we need its length to seek past it */
|
2017-10-26 13:47:34 +01:00
|
|
|
fread(&oid_pk, 1, XMSS_OID_LEN, keypair_file);
|
2017-10-26 12:47:29 +01:00
|
|
|
XMSS_PARSE_OID(¶ms, oid_pk);
|
2017-10-17 16:11:18 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* fseek past the public key */
|
2017-10-26 13:47:34 +01:00
|
|
|
fseek(keypair_file, params.pk_bytes, SEEK_CUR);
|
2017-10-23 13:10:39 +01:00
|
|
|
/* This is the OID we're actually going to use. Likely the same, but still. */
|
2017-10-26 13:47:34 +01:00
|
|
|
fread(&oid_sk, 1, XMSS_OID_LEN, keypair_file);
|
2017-10-26 12:47:29 +01:00
|
|
|
XMSS_PARSE_OID(¶ms, oid_sk);
|
2017-10-17 16:11:18 +01:00
|
|
|
|
2017-10-26 13:24:10 +01:00
|
|
|
unsigned char sk[XMSS_OID_LEN + params.sk_bytes];
|
2017-10-26 13:47:34 +01:00
|
|
|
unsigned char m[mlen];
|
|
|
|
unsigned char sm[params.sig_bytes + mlen];
|
2017-10-17 16:11:18 +01:00
|
|
|
unsigned long long smlen;
|
|
|
|
|
2017-10-26 13:24:10 +01:00
|
|
|
/* fseek back to start of sk. */
|
2017-10-26 13:47:34 +01:00
|
|
|
fseek(keypair_file, -((long int)XMSS_OID_LEN), SEEK_CUR);
|
|
|
|
fseek(m_file, 0, SEEK_SET);
|
|
|
|
fread(sk, 1, XMSS_OID_LEN + params.sk_bytes, keypair_file);
|
|
|
|
fread(m, 1, mlen, m_file);
|
2017-10-26 13:24:10 +01:00
|
|
|
|
2017-10-26 13:47:34 +01:00
|
|
|
XMSS_SIGN(sk, sm, &smlen, m, mlen);
|
2017-10-17 16:11:18 +01:00
|
|
|
|
2017-10-26 13:47:34 +01:00
|
|
|
fseek(keypair_file, -((long int)params.sk_bytes), SEEK_CUR);
|
|
|
|
fwrite(sk + XMSS_OID_LEN, 1, params.sk_bytes, keypair_file);
|
|
|
|
fwrite(sm, 1, smlen, stdout);
|
2017-10-17 16:11:18 +01:00
|
|
|
|
2017-10-26 13:47:34 +01:00
|
|
|
fclose(keypair_file);
|
|
|
|
fclose(m_file);
|
2017-10-17 16:11:18 +01:00
|
|
|
}
|