| 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 <stdint.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "crypto/random.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace crypto { | |
| 15 | |
| 16 // Test that the basic shared key exchange identity holds: that both parties end | |
| 17 // up with the same shared key. This test starts with a fixed private key for | |
| 18 // two parties: alice and bob. Runs ScalarBaseMult and ScalarMult to compute | |
| 19 // public key and shared key for alice and bob. It asserts that alice and bob | |
| 20 // have the same shared key. | |
| 21 TEST(Curve25519, SharedKeyIdentity) { | |
| 22 uint8_t alice_private_key[curve25519::kScalarBytes] = {3}; | |
| 23 uint8_t bob_private_key[curve25519::kScalarBytes] = {5}; | |
| 24 | |
| 25 // Get public key for alice and bob. | |
| 26 uint8_t alice_public_key[curve25519::kBytes]; | |
| 27 curve25519::ScalarBaseMult(alice_private_key, alice_public_key); | |
| 28 | |
| 29 uint8_t bob_public_key[curve25519::kBytes]; | |
| 30 curve25519::ScalarBaseMult(bob_private_key, bob_public_key); | |
| 31 | |
| 32 // Get the shared key for alice, by using alice's private key and bob's | |
| 33 // public key. | |
| 34 uint8_t alice_shared_key[curve25519::kBytes]; | |
| 35 curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key); | |
| 36 | |
| 37 // Get the shared key for bob, by using bob's private key and alice's public | |
| 38 // key. | |
| 39 uint8_t bob_shared_key[curve25519::kBytes]; | |
| 40 curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key); | |
| 41 | |
| 42 // Computed shared key of alice and bob should be the same. | |
| 43 ASSERT_EQ(0, memcmp(alice_shared_key, bob_shared_key, curve25519::kBytes)); | |
| 44 } | |
| 45 | |
| 46 TEST(Curve25519, SmallOrder) { | |
| 47 static const uint8_t kSmallOrderPoint[32] = { | |
| 48 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3, | |
| 49 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, | |
| 50 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, | |
| 51 }; | |
| 52 | |
| 53 uint8_t out[32], private_key[32]; | |
| 54 memset(private_key, 0x11, sizeof(private_key)); | |
| 55 | |
| 56 EXPECT_FALSE(curve25519::ScalarMult(private_key, kSmallOrderPoint, out)); | |
| 57 } | |
| 58 | |
| 59 } // namespace crypto | |
| OLD | NEW |