| Index: crypto/curve25519_unittest.cc
|
| ===================================================================
|
| --- crypto/curve25519_unittest.cc (revision 0)
|
| +++ crypto/curve25519_unittest.cc (revision 0)
|
| @@ -0,0 +1,66 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "crypto/curve25519.h"
|
| +
|
| +#include <string>
|
| +
|
| +#include "crypto/random.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using std::string;
|
| +
|
| +namespace crypto {
|
| +namespace test {
|
| +
|
| +// SharedKey just tests that the basic key exchange identity holds: that both
|
| +// parties end up with the same key.
|
| +TEST(Curve25519, SharedKey) {
|
| + for (int i = 0; i < 5; i++) {
|
| + uint8 alice_random_bytes[crypto_scalarmult_curve25519_SCALARBYTES];
|
| + crypto::RandBytes(alice_random_bytes, sizeof(alice_random_bytes));
|
| + string alice_key;
|
| + EXPECT_TRUE(Curve25519::ConvertToPrivateKey(
|
| + alice_random_bytes, sizeof(alice_random_bytes), &alice_key));
|
| +
|
| + uint8 alice_public_key[crypto_scalarmult_curve25519_BYTES];
|
| + uint8 alice_private_key[crypto_scalarmult_curve25519_SCALARBYTES];
|
| + ASSERT_EQ(static_cast<size_t>(crypto_scalarmult_curve25519_SCALARBYTES),
|
| + alice_key.size());
|
| + memcpy(alice_private_key, alice_key.data(), alice_key.size());
|
| + Curve25519::ScalarMultiplyBase(alice_public_key, alice_private_key);
|
| +
|
| + uint8 bob_random_bytes[crypto_scalarmult_curve25519_SCALARBYTES];
|
| + crypto::RandBytes(bob_random_bytes, sizeof(bob_random_bytes));
|
| + string bob_key;
|
| + EXPECT_TRUE(Curve25519::ConvertToPrivateKey(
|
| + bob_random_bytes, sizeof(bob_random_bytes), &bob_key));
|
| +
|
| + uint8 bob_public_key[crypto_scalarmult_curve25519_BYTES];
|
| + uint8 bob_private_key[crypto_scalarmult_curve25519_SCALARBYTES];
|
| + ASSERT_EQ(static_cast<size_t>(crypto_scalarmult_curve25519_SCALARBYTES),
|
| + bob_key.size());
|
| + memcpy(bob_private_key, bob_key.data(), bob_key.size());
|
| + Curve25519::ScalarMultiplyBase(bob_public_key, bob_private_key);
|
| +
|
| + uint8 alice_shared_key[crypto_scalarmult_curve25519_BYTES];
|
| + Curve25519::ScalarMultiply(
|
| + alice_shared_key, alice_private_key, bob_public_key);
|
| + string alice_shared;
|
| + alice_shared.assign(reinterpret_cast<char*>(alice_shared_key),
|
| + sizeof(alice_shared_key));
|
| +
|
| + uint8 bob_shared_key[crypto_scalarmult_curve25519_BYTES];
|
| + Curve25519::ScalarMultiply(
|
| + bob_shared_key, bob_private_key, alice_public_key);
|
| + string bob_shared;
|
| + bob_shared.assign(reinterpret_cast<char*>(bob_shared_key),
|
| + sizeof(bob_shared_key));
|
| +
|
| + ASSERT_EQ(alice_shared, bob_shared);
|
| + }
|
| +}
|
| +
|
| +} // namespace test
|
| +} // namespace crypto
|
|
|