OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/curve25519.h" | 5 #include "crypto/curve25519.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "crypto/random.h" | 9 #include "crypto/random.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace crypto { | 12 namespace crypto { |
13 | 13 |
14 // Test that the basic shared key exchange identity holds: that both parties end | 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 | 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 | 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 | 17 // public key and shared key for alice and bob. It asserts that alice and bob |
18 // have the same shared key. | 18 // have the same shared key. |
19 TEST(Curve25519, SharedKeyIdentity) { | 19 TEST(Curve25519, SharedKeyIdentity) { |
20 uint8 alice_private_key[curve25519::kScalarBytes] = {3}; | 20 uint8_t alice_private_key[curve25519::kScalarBytes] = {3}; |
21 uint8 bob_private_key[curve25519::kScalarBytes] = {5}; | 21 uint8_t bob_private_key[curve25519::kScalarBytes] = {5}; |
22 | 22 |
23 // Get public key for alice and bob. | 23 // Get public key for alice and bob. |
24 uint8 alice_public_key[curve25519::kBytes]; | 24 uint8_t alice_public_key[curve25519::kBytes]; |
25 curve25519::ScalarBaseMult(alice_private_key, alice_public_key); | 25 curve25519::ScalarBaseMult(alice_private_key, alice_public_key); |
26 | 26 |
27 uint8 bob_public_key[curve25519::kBytes]; | 27 uint8_t bob_public_key[curve25519::kBytes]; |
28 curve25519::ScalarBaseMult(bob_private_key, bob_public_key); | 28 curve25519::ScalarBaseMult(bob_private_key, bob_public_key); |
29 | 29 |
30 // Get the shared key for alice, by using alice's private key and bob's | 30 // Get the shared key for alice, by using alice's private key and bob's |
31 // public key. | 31 // public key. |
32 uint8 alice_shared_key[curve25519::kBytes]; | 32 uint8_t alice_shared_key[curve25519::kBytes]; |
33 curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key); | 33 curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key); |
34 | 34 |
35 // Get the shared key for bob, by using bob's private key and alice's public | 35 // Get the shared key for bob, by using bob's private key and alice's public |
36 // key. | 36 // key. |
37 uint8 bob_shared_key[curve25519::kBytes]; | 37 uint8_t bob_shared_key[curve25519::kBytes]; |
38 curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key); | 38 curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key); |
39 | 39 |
40 // Computed shared key of alice and bob should be the same. | 40 // Computed shared key of alice and bob should be the same. |
41 ASSERT_EQ(0, memcmp(alice_shared_key, bob_shared_key, curve25519::kBytes)); | 41 ASSERT_EQ(0, memcmp(alice_shared_key, bob_shared_key, curve25519::kBytes)); |
42 } | 42 } |
43 | 43 |
| 44 TEST(Curve25519, SmallOrder) { |
| 45 static const uint8_t kSmallOrderPoint[32] = { |
| 46 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3, |
| 47 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, |
| 48 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, |
| 49 }; |
| 50 |
| 51 uint8_t out[32], private_key[32]; |
| 52 memset(private_key, 0x11, sizeof(private_key)); |
| 53 |
| 54 EXPECT_FALSE(curve25519::ScalarMult(private_key, kSmallOrderPoint, out)); |
| 55 } |
| 56 |
44 } // namespace crypto | 57 } // namespace crypto |
OLD | NEW |