Chromium Code Reviews| Index: crypto/curve25519_unittest.cc |
| =================================================================== |
| --- crypto/curve25519_unittest.cc (revision 0) |
| +++ crypto/curve25519_unittest.cc (revision 0) |
| @@ -0,0 +1,63 @@ |
| +// 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; |
|
wtc
2013/03/06 19:21:07
I would avoid this using statement. Is this file s
ramant (doing other things)
2013/03/06 20:03:12
Done.
|
| + |
| +namespace crypto { |
| + |
| +// 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_secret[curve25519::kCryptoScalarMultCurve25519ScalarBytes]; |
| + crypto::RandBytes(alice_secret, sizeof(alice_secret)); |
| + EXPECT_TRUE(curve25519::ConvertToPrivateKey(alice_secret, |
| + sizeof(alice_secret))); |
| + string alice_key(reinterpret_cast<char*>(alice_secret), |
| + sizeof(alice_secret)); |
| + |
| + uint8 alice_public_key[curve25519::kCryptoScalarMultCurve25519Bytes]; |
| + uint8 alice_private_key[curve25519::kCryptoScalarMultCurve25519ScalarBytes]; |
| + ASSERT_EQ(static_cast<size_t>( |
| + curve25519::kCryptoScalarMultCurve25519ScalarBytes), alice_key.size()); |
| + memcpy(alice_private_key, alice_key.data(), alice_key.size()); |
| + curve25519::ScalarBaseMult(alice_private_key, alice_public_key); |
| + |
| + uint8 bob_secret[curve25519::kCryptoScalarMultCurve25519ScalarBytes]; |
| + crypto::RandBytes(bob_secret, sizeof(bob_secret)); |
| + EXPECT_TRUE(curve25519::ConvertToPrivateKey(bob_secret, |
| + sizeof(bob_secret))); |
| + string bob_key(reinterpret_cast<char*>(bob_secret), sizeof(bob_secret)); |
| + |
| + uint8 bob_public_key[curve25519::kCryptoScalarMultCurve25519Bytes]; |
| + uint8 bob_private_key[curve25519::kCryptoScalarMultCurve25519ScalarBytes]; |
| + ASSERT_EQ(static_cast<size_t>( |
| + curve25519::kCryptoScalarMultCurve25519ScalarBytes), bob_key.size()); |
| + memcpy(bob_private_key, bob_key.data(), bob_key.size()); |
| + curve25519::ScalarBaseMult(bob_private_key, bob_public_key); |
| + |
| + uint8 alice_shared_key[curve25519::kCryptoScalarMultCurve25519Bytes]; |
| + curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key); |
| + string alice_shared; |
| + alice_shared.assign(reinterpret_cast<char*>(alice_shared_key), |
| + sizeof(alice_shared_key)); |
| + |
| + uint8 bob_shared_key[curve25519::kCryptoScalarMultCurve25519Bytes]; |
| + curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key); |
| + string bob_shared; |
| + bob_shared.assign(reinterpret_cast<char*>(bob_shared_key), |
| + sizeof(bob_shared_key)); |
| + |
| + ASSERT_EQ(alice_shared, bob_shared); |
| + } |
| +} |
| + |
| +} // namespace crypto |