Debian Codegraph @

external/boringssl/crypto/curve25519/spake25519_test.cc

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