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 TEST(Curve25519, SharedKeyIdentity) { | |
20 uint8 alice_private_key[curve25519::kScalarBytes] = {3}; | |
21 uint8 bob_private_key[curve25519::kScalarBytes] = {5}; | |
22 | |
23 // Get public key for alice and bob. | |
24 uint8 alice_public_key[curve25519::kBytes]; | |
25 curve25519::ScalarBaseMult(alice_private_key, alice_public_key); | |
26 | |
27 uint8 bob_public_key[curve25519::kBytes]; | |
28 curve25519::ScalarBaseMult(bob_private_key, bob_public_key); | |
29 | |
30 // Get the shared key for alice, by using alice's private key and bob's | |
31 // public key. | |
32 uint8 alice_shared_key[curve25519::kBytes]; | |
33 curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key); | |
34 std::string alice_shared; | |
35 alice_shared.assign(reinterpret_cast<char*>(alice_shared_key), | |
36 sizeof(alice_shared_key)); | |
37 | |
38 // Get the shared key for bob, by using bob's private key and alice's public | |
39 // key. | |
40 uint8 bob_shared_key[curve25519::kBytes]; | |
41 curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key); | |
42 std::string bob_shared; | |
43 bob_shared.assign(reinterpret_cast<char*>(bob_shared_key), | |
44 sizeof(bob_shared_key)); | |
45 | |
46 // Computed shared key of alice and bob should be the same. | |
47 ASSERT_EQ(alice_shared, bob_shared); | |
Ryan Sleevi
2013/03/08 18:37:27
Since |bob_shared| and |alice_shared| may contain
ramant (doing other things)
2013/03/08 19:09:45
Done.
| |
48 } | |
49 | |
50 } // namespace crypto | |
OLD | NEW |