Home | History | Annotate | Download | only in curve25519
      1 /* Copyright (c) 2015, Google Inc.
      2  *
      3  * Permission to use, copy, modify, and/or distribute this software for any
      4  * purpose with or without fee is hereby granted, provided that the above
      5  * copyright notice and this permission notice appear in all copies.
      6  *
      7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
     12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
     14 
     15 #include <stdint.h>
     16 #include <string.h>
     17 
     18 #include <gtest/gtest.h>
     19 
     20 #include <openssl/curve25519.h>
     21 
     22 #include "../internal.h"
     23 #include "../test/file_test.h"
     24 #include "../test/test_util.h"
     25 
     26 
     27 TEST(Ed25519Test, TestVectors) {
     28   FileTestGTest("crypto/curve25519/ed25519_tests.txt", [](FileTest *t) {
     29     std::vector<uint8_t> private_key, public_key, message, expected_signature;
     30     ASSERT_TRUE(t->GetBytes(&private_key, "PRIV"));
     31     ASSERT_EQ(64u, private_key.size());
     32     ASSERT_TRUE(t->GetBytes(&public_key, "PUB"));
     33     ASSERT_EQ(32u, public_key.size());
     34     ASSERT_TRUE(t->GetBytes(&message, "MESSAGE"));
     35     ASSERT_TRUE(t->GetBytes(&expected_signature, "SIG"));
     36     ASSERT_EQ(64u, expected_signature.size());
     37 
     38     uint8_t signature[64];
     39     ASSERT_TRUE(ED25519_sign(signature, message.data(), message.size(),
     40                              private_key.data()));
     41     EXPECT_EQ(Bytes(expected_signature), Bytes(signature));
     42     EXPECT_TRUE(ED25519_verify(message.data(), message.size(), signature,
     43                                public_key.data()));
     44   });
     45 }
     46 
     47 TEST(Ed25519Test, KeypairFromSeed) {
     48   uint8_t public_key1[32], private_key1[64];
     49   ED25519_keypair(public_key1, private_key1);
     50 
     51   uint8_t seed[32];
     52   OPENSSL_memcpy(seed, private_key1, sizeof(seed));
     53 
     54   uint8_t public_key2[32], private_key2[64];
     55   ED25519_keypair_from_seed(public_key2, private_key2, seed);
     56 
     57   EXPECT_EQ(Bytes(public_key1), Bytes(public_key2));
     58   EXPECT_EQ(Bytes(private_key1), Bytes(private_key2));
     59 }
     60