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 using std::string; |
| 13 |
| 14 namespace crypto { |
| 15 namespace test { |
| 16 |
| 17 // SharedKey just tests that the basic key exchange identity holds: that both |
| 18 // parties end up with the same key. |
| 19 TEST(Curve25519, SharedKey) { |
| 20 for (int i = 0; i < 5; i++) { |
| 21 uint8 alice_random_bytes[crypto_scalarmult_curve25519_SCALARBYTES]; |
| 22 crypto::RandBytes(alice_random_bytes, sizeof(alice_random_bytes)); |
| 23 string alice_key; |
| 24 EXPECT_TRUE(Curve25519::ConvertToPrivateKey( |
| 25 alice_random_bytes, sizeof(alice_random_bytes), &alice_key)); |
| 26 |
| 27 uint8 alice_public_key[crypto_scalarmult_curve25519_BYTES]; |
| 28 uint8 alice_private_key[crypto_scalarmult_curve25519_SCALARBYTES]; |
| 29 ASSERT_EQ(static_cast<size_t>(crypto_scalarmult_curve25519_SCALARBYTES), |
| 30 alice_key.size()); |
| 31 memcpy(alice_private_key, alice_key.data(), alice_key.size()); |
| 32 Curve25519::ScalarMultiplyBase(alice_public_key, alice_private_key); |
| 33 |
| 34 uint8 bob_random_bytes[crypto_scalarmult_curve25519_SCALARBYTES]; |
| 35 crypto::RandBytes(bob_random_bytes, sizeof(bob_random_bytes)); |
| 36 string bob_key; |
| 37 EXPECT_TRUE(Curve25519::ConvertToPrivateKey( |
| 38 bob_random_bytes, sizeof(bob_random_bytes), &bob_key)); |
| 39 |
| 40 uint8 bob_public_key[crypto_scalarmult_curve25519_BYTES]; |
| 41 uint8 bob_private_key[crypto_scalarmult_curve25519_SCALARBYTES]; |
| 42 ASSERT_EQ(static_cast<size_t>(crypto_scalarmult_curve25519_SCALARBYTES), |
| 43 bob_key.size()); |
| 44 memcpy(bob_private_key, bob_key.data(), bob_key.size()); |
| 45 Curve25519::ScalarMultiplyBase(bob_public_key, bob_private_key); |
| 46 |
| 47 uint8 alice_shared_key[crypto_scalarmult_curve25519_BYTES]; |
| 48 Curve25519::ScalarMultiply( |
| 49 alice_shared_key, alice_private_key, bob_public_key); |
| 50 string alice_shared; |
| 51 alice_shared.assign(reinterpret_cast<char*>(alice_shared_key), |
| 52 sizeof(alice_shared_key)); |
| 53 |
| 54 uint8 bob_shared_key[crypto_scalarmult_curve25519_BYTES]; |
| 55 Curve25519::ScalarMultiply( |
| 56 bob_shared_key, bob_private_key, alice_public_key); |
| 57 string bob_shared; |
| 58 bob_shared.assign(reinterpret_cast<char*>(bob_shared_key), |
| 59 sizeof(bob_shared_key)); |
| 60 |
| 61 ASSERT_EQ(alice_shared, bob_shared); |
| 62 } |
| 63 } |
| 64 |
| 65 } // namespace test |
| 66 } // namespace crypto |
OLD | NEW |