選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

103 行
2.8 KiB

  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include "api.h"
  9. #include "randombytes.h"
  10. // https://stackoverflow.com/a/1489985/1711232
  11. #define PASTER(x, y) x##_##y
  12. #define EVALUATOR(x, y) PASTER(x, y)
  13. #define NAMESPACE(fun) EVALUATOR(PQCLEAN_NAMESPACE, fun)
  14. #define CRYPTO_PUBLICKEYBYTES NAMESPACE(CRYPTO_PUBLICKEYBYTES)
  15. #define CRYPTO_SECRETKEYBYTES NAMESPACE(CRYPTO_SECRETKEYBYTES)
  16. #define CRYPTO_BYTES NAMESPACE(CRYPTO_BYTES)
  17. #define CRYPTO_ALGNAME NAMESPACE(CRYPTO_ALGNAME)
  18. #define crypto_sign_keypair NAMESPACE(crypto_sign_keypair)
  19. #define crypto_sign NAMESPACE(crypto_sign)
  20. #define crypto_sign_open NAMESPACE(crypto_sign_open)
  21. void nist_kat_init(unsigned char *entropy_input, unsigned char *personalization_string, int security_strength);
  22. static void fprintBstr(FILE *fp, const char *S, const uint8_t *A, size_t L) {
  23. size_t i;
  24. fprintf(fp, "%s", S);
  25. for (i = 0; i < L; i++) {
  26. fprintf(fp, "%02X", A[i]);
  27. }
  28. if (L == 0) {
  29. fprintf(fp, "00");
  30. }
  31. fprintf(fp, "\n");
  32. }
  33. int main() {
  34. uint8_t entropy_input[48];
  35. uint8_t seed[48];
  36. FILE *fh = stdout;
  37. uint8_t public_key[CRYPTO_PUBLICKEYBYTES];
  38. uint8_t secret_key[CRYPTO_SECRETKEYBYTES];
  39. size_t mlen = 33;
  40. size_t smlen, mlen1;
  41. uint8_t m[33];
  42. uint8_t sm[33 + CRYPTO_BYTES];
  43. int rc;
  44. for (uint8_t i = 0; i < 48; i++) {
  45. entropy_input[i] = i;
  46. }
  47. nist_kat_init(entropy_input, NULL, 256);
  48. fprintf(fh, "count = 0\n");
  49. randombytes(seed, 48);
  50. fprintBstr(fh, "seed = ", seed, 48);
  51. fprintf(fh, "mlen = 33\n");
  52. randombytes(m, mlen);
  53. fprintBstr(fh, "msg = ", m, mlen);
  54. nist_kat_init(seed, NULL, 256);
  55. rc = crypto_sign_keypair(public_key, secret_key);
  56. if (rc != 0) {
  57. fprintf(stderr, "[kat_kem] %s ERROR: crypto_kem_keypair failed!\n", CRYPTO_ALGNAME);
  58. return -1;
  59. }
  60. fprintBstr(fh, "pk = ", public_key, CRYPTO_PUBLICKEYBYTES);
  61. fprintBstr(fh, "sk = ", secret_key, CRYPTO_SECRETKEYBYTES);
  62. rc = crypto_sign(sm, &smlen, m, mlen, secret_key);
  63. if (rc != 0) {
  64. fprintf(stderr, "[kat_kem] %s ERROR: crypto_sign failed!\n", CRYPTO_ALGNAME);
  65. return -2;
  66. }
  67. fprintf(fh, "smlen = %zu\n", smlen);
  68. fprintBstr(fh, "sm = ", sm, smlen);
  69. rc = crypto_sign_open(sm, &mlen1, sm, smlen, public_key);
  70. if (rc != 0) {
  71. fprintf(stderr, "[kat_kem] %s ERROR: crypto_sign_open failed!\n", CRYPTO_ALGNAME);
  72. return -3;
  73. }
  74. if ( mlen != mlen1 ) {
  75. printf("crypto_sign_open returned bad 'mlen': got <%zu>, expected <%zu>\n", mlen1, mlen);
  76. return -4;
  77. }
  78. if (memcmp(m, sm, mlen)) {
  79. printf("crypto_sign_open returned bad 'm' value\n");
  80. return -5;
  81. }
  82. return 0;
  83. }