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.
 
 
 
 
 
 

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