Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "api.h"
  2. #include "randombytes.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define NTESTS 10
  6. const unsigned char canary[8] = {
  7. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
  8. };
  9. /* allocate a bit more for all keys and messages and
  10. * make sure it is not touched by the implementations.
  11. */
  12. static void write_canary(unsigned char *d) {
  13. for (int i = 0; i < 8; i++) {
  14. d[i] = canary[i];
  15. }
  16. }
  17. static int check_canary(const unsigned char *d) {
  18. for (int i = 0; i < 8; i++) {
  19. if (d[i] != canary[i])
  20. return -1;
  21. }
  22. return 0;
  23. }
  24. // https://stackoverflow.com/a/1489985/1711232
  25. #define PASTER(x, y) x##_##y
  26. #define EVALUATOR(x, y) PASTER(x, y)
  27. #define NAMESPACE(fun) EVALUATOR(PQCLEAN_NAMESPACE, fun)
  28. #define crypto_kem_keypair NAMESPACE(crypto_kem_keypair)
  29. #define crypto_kem_enc NAMESPACE(crypto_kem_enc)
  30. #define crypto_kem_dec NAMESPACE(crypto_kem_dec)
  31. #define RETURNS_ZERO(f) \
  32. if ((f) != 0) { \
  33. puts(#f " returned non-zero returncode"); \
  34. return -1; \
  35. }
  36. static int test_keys(void) {
  37. unsigned char key_a[CRYPTO_BYTES + 16], key_b[CRYPTO_BYTES + 16];
  38. unsigned char pk[CRYPTO_PUBLICKEYBYTES + 16];
  39. unsigned char sendb[CRYPTO_CIPHERTEXTBYTES + 16];
  40. unsigned char sk_a[CRYPTO_SECRETKEYBYTES + 16];
  41. write_canary(key_a);
  42. write_canary(key_a + sizeof(key_a) - 8);
  43. write_canary(key_b);
  44. write_canary(key_b + sizeof(key_b) - 8);
  45. write_canary(pk);
  46. write_canary(pk + sizeof(pk) - 8);
  47. write_canary(sendb);
  48. write_canary(sendb + sizeof(sendb) - 8);
  49. write_canary(sk_a);
  50. write_canary(sk_a + sizeof(sk_a) - 8);
  51. int i;
  52. for (i = 0; i < NTESTS; i++) {
  53. // Alice generates a public key
  54. RETURNS_ZERO(crypto_kem_keypair(pk + 8, sk_a + 8));
  55. // Bob derives a secret key and creates a response
  56. RETURNS_ZERO(crypto_kem_enc(sendb + 8, key_b + 8, pk + 8));
  57. // Alice uses Bobs response to get her secret key
  58. RETURNS_ZERO(crypto_kem_dec(key_a + 8, sendb + 8, sk_a + 8));
  59. if (memcmp(key_a + 8, key_b + 8, CRYPTO_BYTES) != 0) {
  60. printf("ERROR KEYS\n");
  61. return -1;
  62. }
  63. if (check_canary(key_a) || check_canary(key_a + sizeof(key_a) - 8) ||
  64. check_canary(key_b) || check_canary(key_b + sizeof(key_b) - 8) ||
  65. check_canary(pk) || check_canary(pk + sizeof(pk) - 8) ||
  66. check_canary(sendb) || check_canary(sendb + sizeof(sendb) - 8) ||
  67. check_canary(sk_a) || check_canary(sk_a + sizeof(sk_a) - 8)) {
  68. printf("ERROR canary overwritten\n");
  69. return -1;
  70. }
  71. }
  72. return 0;
  73. }
  74. static int test_invalid_sk_a(void) {
  75. unsigned char sk_a[CRYPTO_SECRETKEYBYTES];
  76. unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
  77. unsigned char pk[CRYPTO_PUBLICKEYBYTES];
  78. unsigned char sendb[CRYPTO_CIPHERTEXTBYTES];
  79. int i;
  80. int returncode;
  81. for (i = 0; i < NTESTS; i++) {
  82. // Alice generates a public key
  83. RETURNS_ZERO(crypto_kem_keypair(pk, sk_a));
  84. // Bob derives a secret key and creates a response
  85. RETURNS_ZERO(crypto_kem_enc(sendb, key_b, pk));
  86. // Replace secret key with random values
  87. randombytes(sk_a, CRYPTO_SECRETKEYBYTES);
  88. // Alice uses Bobs response to get her secret key
  89. if ((returncode = crypto_kem_dec(key_a, sendb, sk_a)) > 0) {
  90. printf("ERROR failing crypto_kem_dec returned %d instead of "
  91. "negative or zero code\n",
  92. returncode);
  93. return -1;
  94. }
  95. if (!memcmp(key_a, key_b, CRYPTO_BYTES)) {
  96. printf("ERROR invalid sk_a\n");
  97. return 1;
  98. }
  99. }
  100. return 0;
  101. }
  102. static int test_invalid_ciphertext(void) {
  103. unsigned char sk_a[CRYPTO_SECRETKEYBYTES];
  104. unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
  105. unsigned char pk[CRYPTO_PUBLICKEYBYTES];
  106. unsigned char sendb[CRYPTO_CIPHERTEXTBYTES];
  107. int i;
  108. size_t pos;
  109. int returncode;
  110. for (i = 0; i < NTESTS; i++) {
  111. randombytes((unsigned char *)&pos, sizeof(size_t));
  112. // Alice generates a public key
  113. RETURNS_ZERO(crypto_kem_keypair(pk, sk_a));
  114. // Bob derives a secret key and creates a response
  115. RETURNS_ZERO(crypto_kem_enc(sendb, key_b, pk));
  116. // Change some byte in the ciphertext (i.e., encapsulated key)
  117. sendb[pos % CRYPTO_CIPHERTEXTBYTES] ^= 23;
  118. // Alice uses Bobs response to get her secret key
  119. if ((returncode = crypto_kem_dec(key_a, sendb, sk_a)) > 0) {
  120. printf("ERROR crypto_kem_dec should either fail (negative "
  121. "returncode) or succeed (return 0) but returned %d\n",
  122. returncode);
  123. return -1;
  124. }
  125. if (!memcmp(key_a, key_b, CRYPTO_BYTES)) {
  126. printf("ERROR invalid ciphertext\n");
  127. return 1;
  128. }
  129. }
  130. return 0;
  131. }
  132. int main(void) {
  133. int result = 0;
  134. result += test_keys();
  135. result += test_invalid_sk_a();
  136. result += test_invalid_ciphertext();
  137. if (result != 0) {
  138. puts("Errors occurred");
  139. }
  140. return result;
  141. }