Home | History | Annotate | Download | only in crypto
      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 namespace crypto {
     13 
     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
     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
     18 // have the same shared key.
     19 TEST(Curve25519, SharedKeyIdentity) {
     20   uint8 alice_private_key[curve25519::kScalarBytes] = {3};
     21   uint8 bob_private_key[curve25519::kScalarBytes] = {5};
     22 
     23   // Get public key for alice and bob.
     24   uint8 alice_public_key[curve25519::kBytes];
     25   curve25519::ScalarBaseMult(alice_private_key, alice_public_key);
     26 
     27   uint8 bob_public_key[curve25519::kBytes];
     28   curve25519::ScalarBaseMult(bob_private_key, bob_public_key);
     29 
     30   // Get the shared key for alice, by using alice's private key and bob's
     31   // public key.
     32   uint8 alice_shared_key[curve25519::kBytes];
     33   curve25519::ScalarMult(alice_private_key, bob_public_key, alice_shared_key);
     34 
     35   // Get the shared key for bob, by using bob's private key and alice's public
     36   // key.
     37   uint8 bob_shared_key[curve25519::kBytes];
     38   curve25519::ScalarMult(bob_private_key, alice_public_key, bob_shared_key);
     39 
     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));
     42 }
     43 
     44 }  // namespace crypto
     45