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.
 
 
 
 
 
 

169 regels
4.7 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 <string>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <openssl/curve25519.h>
  19. struct SPAKE2Run {
  20. bool Run() {
  21. bssl::UniquePtr<SPAKE2_CTX> alice(SPAKE2_CTX_new(
  22. spake2_role_alice,
  23. reinterpret_cast<const uint8_t *>(alice_names.first.data()),
  24. alice_names.first.size(),
  25. reinterpret_cast<const uint8_t *>(alice_names.second.data()),
  26. alice_names.second.size()));
  27. bssl::UniquePtr<SPAKE2_CTX> bob(SPAKE2_CTX_new(
  28. spake2_role_bob,
  29. reinterpret_cast<const uint8_t *>(bob_names.first.data()),
  30. bob_names.first.size(),
  31. reinterpret_cast<const uint8_t *>(bob_names.second.data()),
  32. bob_names.second.size()));
  33. if (!alice || !bob) {
  34. return false;
  35. }
  36. uint8_t alice_msg[SPAKE2_MAX_MSG_SIZE];
  37. uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE];
  38. size_t alice_msg_len, bob_msg_len;
  39. if (!SPAKE2_generate_msg(
  40. alice.get(), alice_msg, &alice_msg_len, sizeof(alice_msg),
  41. reinterpret_cast<const uint8_t *>(alice_password.data()),
  42. alice_password.size()) ||
  43. !SPAKE2_generate_msg(
  44. bob.get(), bob_msg, &bob_msg_len, sizeof(bob_msg),
  45. reinterpret_cast<const uint8_t *>(bob_password.data()),
  46. bob_password.size())) {
  47. return false;
  48. }
  49. if (alice_corrupt_msg_bit >= 0 &&
  50. static_cast<size_t>(alice_corrupt_msg_bit) < 8 * alice_msg_len) {
  51. alice_msg[alice_corrupt_msg_bit/8] ^= 1 << (alice_corrupt_msg_bit & 7);
  52. }
  53. uint8_t alice_key[64], bob_key[64];
  54. size_t alice_key_len, bob_key_len;
  55. if (!SPAKE2_process_msg(alice.get(), alice_key, &alice_key_len,
  56. sizeof(alice_key), bob_msg, bob_msg_len) ||
  57. !SPAKE2_process_msg(bob.get(), bob_key, &bob_key_len, sizeof(bob_key),
  58. alice_msg, alice_msg_len)) {
  59. return false;
  60. }
  61. key_matches_ = (alice_key_len == bob_key_len &&
  62. memcmp(alice_key, bob_key, alice_key_len) == 0);
  63. return true;
  64. }
  65. bool key_matches() const {
  66. return key_matches_;
  67. }
  68. std::string alice_password = "password";
  69. std::string bob_password = "password";
  70. std::pair<std::string, std::string> alice_names = {"alice", "bob"};
  71. std::pair<std::string, std::string> bob_names = {"bob", "alice"};
  72. int alice_corrupt_msg_bit = -1;
  73. private:
  74. bool key_matches_ = false;
  75. };
  76. static bool TestSPAKE2() {
  77. for (unsigned i = 0; i < 20; i++) {
  78. SPAKE2Run spake2;
  79. if (!spake2.Run()) {
  80. fprintf(stderr, "TestSPAKE2: SPAKE2 failed.\n");
  81. return false;
  82. }
  83. if (!spake2.key_matches()) {
  84. fprintf(stderr, "Key didn't match for equal passwords.\n");
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. static bool TestWrongPassword() {
  91. SPAKE2Run spake2;
  92. spake2.bob_password = "wrong password";
  93. if (!spake2.Run()) {
  94. fprintf(stderr, "TestSPAKE2: SPAKE2 failed.\n");
  95. return false;
  96. }
  97. if (spake2.key_matches()) {
  98. fprintf(stderr, "Key matched for unequal passwords.\n");
  99. return false;
  100. }
  101. return true;
  102. }
  103. static bool TestWrongNames() {
  104. SPAKE2Run spake2;
  105. spake2.alice_names.second = "charlie";
  106. spake2.bob_names.second = "charlie";
  107. if (!spake2.Run()) {
  108. fprintf(stderr, "TestSPAKE2: SPAKE2 failed.\n");
  109. return false;
  110. }
  111. if (spake2.key_matches()) {
  112. fprintf(stderr, "Key matched for unequal names.\n");
  113. return false;
  114. }
  115. return true;
  116. }
  117. static bool TestCorruptMessages() {
  118. for (int i = 0; i < 8 * SPAKE2_MAX_MSG_SIZE; i++) {
  119. SPAKE2Run spake2;
  120. spake2.alice_corrupt_msg_bit = i;
  121. if (spake2.Run() && spake2.key_matches()) {
  122. fprintf(stderr, "Passed after corrupting Alice's message, bit %d\n", i);
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. /* TODO(agl): add tests with fixed vectors once SPAKE2 is nailed down. */
  129. int main(int argc, char **argv) {
  130. if (!TestSPAKE2() ||
  131. !TestWrongPassword() ||
  132. !TestWrongNames() ||
  133. !TestCorruptMessages()) {
  134. return 1;
  135. }
  136. printf("PASS\n");
  137. return 0;
  138. }