Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

142 rindas
4.5 KiB

  1. /* Copyright (c) 2016, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/rand.h>
  19. #include "internal.h"
  20. // Set to 10 for quick execution. Tested up to 1,000,000.
  21. static const int kNumTests = 10;
  22. static bool TestKeys(void) {
  23. // Alice generates a public key.
  24. bssl::UniquePtr<NEWHOPE_POLY> sk(NEWHOPE_POLY_new());
  25. uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
  26. NEWHOPE_offer(offer_msg, sk.get());
  27. // Bob derives a secret key and creates a response.
  28. uint8_t accept_msg[NEWHOPE_ACCEPTMSG_LENGTH];
  29. uint8_t accept_key[SHA256_DIGEST_LENGTH];
  30. if (!NEWHOPE_accept(accept_key, accept_msg, offer_msg, sizeof(offer_msg))) {
  31. fprintf(stderr, "ERROR accept key exchange failed\n");
  32. return false;
  33. }
  34. // Alice uses Bob's response to get her secret key.
  35. uint8_t offer_key[SHA256_DIGEST_LENGTH];
  36. if (!NEWHOPE_finish(offer_key, sk.get(), accept_msg, sizeof(accept_msg))) {
  37. fprintf(stderr, "ERROR finish key exchange failed\n");
  38. return false;
  39. }
  40. if (memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH) != 0) {
  41. fprintf(stderr, "ERROR keys did not agree\n");
  42. return false;
  43. }
  44. return true;
  45. }
  46. static bool TestInvalidSK(void) {
  47. // Alice generates a public key.
  48. uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
  49. bssl::UniquePtr<NEWHOPE_POLY> sk(NEWHOPE_POLY_new());
  50. NEWHOPE_offer(offer_msg, sk.get());
  51. // Bob derives a secret key and creates a response.
  52. uint8_t accept_key[SHA256_DIGEST_LENGTH];
  53. uint8_t accept_msg[NEWHOPE_ACCEPTMSG_LENGTH];
  54. if (!NEWHOPE_accept(accept_key, accept_msg, offer_msg, sizeof(offer_msg))) {
  55. fprintf(stderr, "ERROR accept key exchange failed\n");
  56. return false;
  57. }
  58. // Corrupt the secret key. It turns out that you need to corrupt a lot of
  59. // bits to ensure that the key exchange always fails!
  60. sk->coeffs[PARAM_N - 1] = 0;
  61. sk->coeffs[PARAM_N - 2] = 0;
  62. sk->coeffs[PARAM_N - 3] = 0;
  63. sk->coeffs[PARAM_N - 4] = 0;
  64. // Alice uses Bob's response to get her secret key.
  65. uint8_t offer_key[SHA256_DIGEST_LENGTH];
  66. if (!NEWHOPE_finish(offer_key, sk.get(), accept_msg, sizeof(accept_msg))) {
  67. fprintf(stderr, "ERROR finish key exchange failed\n");
  68. return false;
  69. }
  70. if (memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH) == 0) {
  71. fprintf(stderr, "ERROR keys agreed despite corrupt sk\n");
  72. return false;
  73. }
  74. return true;
  75. }
  76. static bool TestInvalidAcceptMsg(void) {
  77. // Alice generates a public key.
  78. bssl::UniquePtr<NEWHOPE_POLY> sk(NEWHOPE_POLY_new());
  79. uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
  80. NEWHOPE_offer(offer_msg, sk.get());
  81. // Bob derives a secret key and creates a response.
  82. uint8_t accept_key[SHA256_DIGEST_LENGTH];
  83. uint8_t accept_msg[NEWHOPE_ACCEPTMSG_LENGTH];
  84. if (!NEWHOPE_accept(accept_key, accept_msg, offer_msg, sizeof(offer_msg))) {
  85. fprintf(stderr, "ERROR accept key exchange failed\n");
  86. return false;
  87. }
  88. // Corrupt the (polynomial part of the) accept message. It turns out that
  89. // you need to corrupt a lot of bits to ensure that the key exchange always
  90. // fails!
  91. accept_msg[PARAM_N - 1] = 0;
  92. accept_msg[PARAM_N - 2] = 0;
  93. accept_msg[PARAM_N - 3] = 0;
  94. accept_msg[PARAM_N - 4] = 0;
  95. // Alice uses Bob's response to get her secret key.
  96. uint8_t offer_key[SHA256_DIGEST_LENGTH];
  97. if (!NEWHOPE_finish(offer_key, sk.get(), accept_msg, sizeof(accept_msg))) {
  98. fprintf(stderr, "ERROR finish key exchange failed\n");
  99. return false;
  100. }
  101. if (!memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH)) {
  102. fprintf(stderr, "ERROR keys agreed despite corrupt accept message\n");
  103. return false;
  104. }
  105. return true;
  106. }
  107. int main(void) {
  108. for (int i = 0; i < kNumTests; i++) {
  109. if (!TestKeys() ||
  110. !TestInvalidSK() ||
  111. !TestInvalidAcceptMsg()) {
  112. return 1;
  113. }
  114. }
  115. printf("PASS\n");
  116. return 0;
  117. }