Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

78 рядки
2.2 KiB

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