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). |
| 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 // Get a private key for two users - alice and bob. |
| 32 curve25519::ConvertToPrivateKey(alice_private_key); |
| 33 curve25519::ConvertToPrivateKey(bob_private_key); |
| 34 |
| 35 // Get public key for alice and bob. |
| 36 uint8 alice_public_key[curve25519::kBytes]; |
| 37 curve25519::ScalarBaseMult(alice_private_key, alice_public_key); |
| 38 |
| 39 uint8 bob_public_key[curve25519::kBytes]; |
| 40 curve25519::ScalarBaseMult(bob_private_key, bob_public_key); |
| 41 |
| 42 // Get the shared key for alice, by using alice's private key and bob's |
| 43 // public key. |
| 44 uint8 alice_shared_key[curve25519::kBytes]; |
| 45 curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key); |
| 46 std::string alice_shared; |
| 47 alice_shared.assign(reinterpret_cast<char*>(alice_shared_key), |
| 48 sizeof(alice_shared_key)); |
| 49 |
| 50 // Get the shared key for bob, by using bob's private key and alice's public |
| 51 // key. |
| 52 uint8 bob_shared_key[curve25519::kBytes]; |
| 53 curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key); |
| 54 std::string bob_shared; |
| 55 bob_shared.assign(reinterpret_cast<char*>(bob_shared_key), |
| 56 sizeof(bob_shared_key)); |
| 57 |
| 58 // Computed shared key of alice and bob should be the same. |
| 59 ASSERT_EQ(alice_shared, bob_shared); |
| 60 |
| 61 // Changed alice's private key with the answer from bob's public key |
| 62 // computed by ScalarBaseMult. |
| 63 for (size_t j = 0; j < curve25519::kScalarBytes; ++j) { |
| 64 SCOPED_TRACE(j); |
| 65 alice_private_key[j] ^= bob_public_key[j]; |
| 66 } |
| 67 |
| 68 // Changed bob's private key with the answer from alice's public key |
| 69 // computed by ScalarBaseMult. |
| 70 for (size_t j = 0; j < curve25519::kScalarBytes; ++j) { |
| 71 SCOPED_TRACE(j); |
| 72 bob_private_key[j] ^= alice_public_key[j]; |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 } // namespace crypto |
OLD | NEW |