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.

open.c 2.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_OPEN xmssmt_sign_open
  9. #else
  10. #define XMSS_PARSE_OID xmss_parse_oid
  11. #define XMSS_SIGN_OPEN xmss_sign_open
  12. #endif
  13. int main(int argc, char **argv) {
  14. FILE *keypair_file;
  15. FILE *sm_file;
  16. xmss_params params;
  17. uint32_t oid = 0;
  18. uint8_t buffer[XMSS_OID_LEN];
  19. int parse_oid_result;
  20. unsigned long long smlen;
  21. int ret;
  22. if (argc != 3) {
  23. fprintf(stderr, "Expected keypair and signature + message filenames "
  24. "as two parameters.\n"
  25. "Keypair file needs only to contain the public key.\n"
  26. "The return code 0 indicates verification success.\n");
  27. return -1;
  28. }
  29. keypair_file = fopen(argv[1], "rb");
  30. if (keypair_file == NULL) {
  31. fprintf(stderr, "Could not open keypair file.\n");
  32. return -1;
  33. }
  34. sm_file = fopen(argv[2], "rb");
  35. if (sm_file == NULL) {
  36. fprintf(stderr, "Could not open signature + message file.\n");
  37. fclose(keypair_file);
  38. return -1;
  39. }
  40. /* Find out the message length. */
  41. fseek(sm_file, 0, SEEK_END);
  42. smlen = ftell(sm_file);
  43. fread(&buffer, 1, XMSS_OID_LEN, keypair_file);
  44. oid = (uint32_t)bytes_to_ull(buffer, XMSS_OID_LEN);
  45. parse_oid_result = XMSS_PARSE_OID(&params, oid);
  46. if (parse_oid_result != 0) {
  47. fprintf(stderr, "Error parsing oid.\n");
  48. fclose(keypair_file);
  49. fclose(sm_file);
  50. return parse_oid_result;
  51. }
  52. unsigned char pk[XMSS_OID_LEN + params.pk_bytes];
  53. unsigned char *sm = malloc(smlen);
  54. unsigned char *m = malloc(smlen);
  55. unsigned long long mlen;
  56. fseek(keypair_file, 0, SEEK_SET);
  57. fseek(sm_file, 0, SEEK_SET);
  58. fread(pk, 1, XMSS_OID_LEN + params.pk_bytes, keypair_file);
  59. fread(sm, 1, smlen, sm_file);
  60. ret = XMSS_SIGN_OPEN(m, &mlen, sm, smlen, pk);
  61. if (ret) {
  62. printf("Verification failed!\n");
  63. }
  64. else {
  65. printf("Verification succeeded.\n");
  66. }
  67. fclose(keypair_file);
  68. fclose(sm_file);
  69. free(m);
  70. free(sm);
  71. return ret;
  72. }