Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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