You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "../params.h"
  4. #include "../xmss.h"
  5. #include "../utils.h"
  6. #ifdef XMSSMT
  7. #define XMSS_PARSE_OID xmssmt_parse_oid
  8. #define XMSS_SIGN xmssmt_sign
  9. #else
  10. #define XMSS_PARSE_OID xmss_parse_oid
  11. #define XMSS_SIGN xmss_sign
  12. #endif
  13. int main(int argc, char **argv) {
  14. FILE *keypair_file;
  15. FILE *m_file;
  16. xmss_params params;
  17. uint32_t oid_pk = 0;
  18. uint32_t oid_sk = 0;
  19. uint8_t buffer[XMSS_OID_LEN];
  20. int parse_oid_result;
  21. unsigned long long mlen;
  22. if (argc != 3) {
  23. fprintf(stderr, "Expected keypair and message filenames as two "
  24. "parameters.\n"
  25. "The keypair is updated with the changed state, "
  26. "and the message + signature is output via stdout.\n");
  27. return -1;
  28. }
  29. keypair_file = fopen(argv[1], "r+b");
  30. if (keypair_file == NULL) {
  31. fprintf(stderr, "Could not open keypair file.\n");
  32. return -1;
  33. }
  34. m_file = fopen(argv[2], "rb");
  35. if (m_file == NULL) {
  36. fprintf(stderr, "Could not open message file.\n");
  37. fclose(keypair_file);
  38. return -1;
  39. }
  40. /* Find out the message length. */
  41. fseek(m_file, 0, SEEK_END);
  42. mlen = ftell(m_file);
  43. /* Read the OID from the public key, as we need its length to seek past it */
  44. fread(&buffer, 1, XMSS_OID_LEN, keypair_file);
  45. /* The XMSS_OID_LEN bytes in buffer are a big-endian uint32. */
  46. oid_pk = (uint32_t)bytes_to_ull(buffer, XMSS_OID_LEN);
  47. parse_oid_result = XMSS_PARSE_OID(&params, oid_pk);
  48. if (parse_oid_result != 0) {
  49. fprintf(stderr, "Error parsing public key oid.\n");
  50. fclose(keypair_file);
  51. fclose(m_file);
  52. return parse_oid_result;
  53. }
  54. /* fseek past the public key */
  55. fseek(keypair_file, params.pk_bytes, SEEK_CUR);
  56. /* This is the OID we're actually going to use. Likely the same, but still. */
  57. fread(&buffer, 1, XMSS_OID_LEN, keypair_file);
  58. oid_sk = (uint32_t)bytes_to_ull(buffer, XMSS_OID_LEN);
  59. parse_oid_result = XMSS_PARSE_OID(&params, oid_sk);
  60. if (parse_oid_result != 0) {
  61. fprintf(stderr, "Error parsing secret key oid.\n");
  62. fclose(keypair_file);
  63. fclose(m_file);
  64. return parse_oid_result;
  65. }
  66. unsigned char sk[XMSS_OID_LEN + params.sk_bytes];
  67. unsigned char *m = malloc(mlen);
  68. unsigned char *sm = malloc(params.sig_bytes + mlen);
  69. unsigned long long smlen;
  70. /* fseek back to start of sk. */
  71. fseek(keypair_file, -((long int)XMSS_OID_LEN), SEEK_CUR);
  72. fseek(m_file, 0, SEEK_SET);
  73. fread(sk, 1, XMSS_OID_LEN + params.sk_bytes, keypair_file);
  74. fread(m, 1, mlen, m_file);
  75. XMSS_SIGN(sk, sm, &smlen, m, mlen);
  76. fseek(keypair_file, -((long int)params.sk_bytes), SEEK_CUR);
  77. fwrite(sk + XMSS_OID_LEN, 1, params.sk_bytes, keypair_file);
  78. fwrite(sm, 1, smlen, stdout);
  79. fclose(keypair_file);
  80. fclose(m_file);
  81. free(m);
  82. free(sm);
  83. return 0;
  84. }