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