OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "crypto/curve25519.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "crypto/random.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace crypto { | |
13 | |
14 // Test that the basic shared key exchange identity holds: that both parties end | |
15 // up with the same shared key. This test starts with a fixed private key for | |
16 // two parties: alice and bob. Runs ScalarBaseMult and ScalarMult to compute | |
17 // public key and shared key for alice and bob. It asserts that alice and bob | |
18 // have the same shared key. | |
19 // | |
20 // It feeds the answers from ScalarBaseMult to modify the private key of alice | |
21 // and bob and then performs the shared key identity test again. | |
22 // | |
23 // It repeats this test 5 times (running it for thousands of iterations takes | |
24 // long time, thus turned it down to 5 times). | |
Ryan Sleevi
2013/03/08 18:27:33
I can understand the thousands of iterations test,
ramant (doing other things)
2013/03/08 18:30:45
Removed the 5 times iteration.
Done.
| |
25 TEST(Curve25519, SharedKeyIdentity) { | |
26 uint8 alice_private_key[curve25519::kScalarBytes] = {3}; | |
27 uint8 bob_private_key[curve25519::kScalarBytes] = {5}; | |
28 | |
29 for (size_t i = 0; i < 5; i++) { | |
30 SCOPED_TRACE(i); | |
31 | |
32 // Get public key for alice and bob. | |
33 uint8 alice_public_key[curve25519::kBytes]; | |
34 curve25519::ScalarBaseMult(alice_private_key, alice_public_key); | |
35 | |
36 uint8 bob_public_key[curve25519::kBytes]; | |
37 curve25519::ScalarBaseMult(bob_private_key, bob_public_key); | |
38 | |
39 // Get the shared key for alice, by using alice's private key and bob's | |
40 // public key. | |
41 uint8 alice_shared_key[curve25519::kBytes]; | |
42 curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key); | |
43 std::string alice_shared; | |
44 alice_shared.assign(reinterpret_cast<char*>(alice_shared_key), | |
45 sizeof(alice_shared_key)); | |
46 | |
47 // Get the shared key for bob, by using bob's private key and alice's public | |
48 // key. | |
49 uint8 bob_shared_key[curve25519::kBytes]; | |
50 curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key); | |
51 std::string bob_shared; | |
52 bob_shared.assign(reinterpret_cast<char*>(bob_shared_key), | |
53 sizeof(bob_shared_key)); | |
54 | |
55 // Computed shared key of alice and bob should be the same. | |
56 ASSERT_EQ(alice_shared, bob_shared); | |
57 | |
58 // Changed alice's private key with the answer from bob's public key | |
59 // computed by ScalarBaseMult. | |
60 for (size_t j = 0; j < curve25519::kScalarBytes; ++j) { | |
61 SCOPED_TRACE(j); | |
62 alice_private_key[j] ^= bob_public_key[j]; | |
63 } | |
64 | |
65 // Changed bob's private key with the answer from alice's public key | |
66 // computed by ScalarBaseMult. | |
67 for (size_t j = 0; j < curve25519::kScalarBytes; ++j) { | |
68 SCOPED_TRACE(j); | |
69 bob_private_key[j] ^= alice_public_key[j]; | |
70 } | |
71 } | |
72 } | |
73 | |
74 } // namespace crypto | |
OLD | NEW |