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 <openssl/curve25519.h>
     19 
     20 #include "../internal.h"
     21 #include "../test/file_test.h"
     22 
     23 
     24 static bool TestSignature(FileTest *t, void *arg) {
     25   std::vector<uint8_t> private_key, public_key, message, expected_signature;
     26   if (!t->GetBytes(&private_key, "PRIV") ||
     27       private_key.size() != 64 ||
     28       !t->GetBytes(&public_key, "PUB") ||
     29       public_key.size() != 32 ||
     30       !t->GetBytes(&message, "MESSAGE") ||
     31       !t->GetBytes(&expected_signature, "SIG") ||
     32       expected_signature.size() != 64) {
     33     return false;
     34   }
     35 
     36   uint8_t signature[64];
     37   if (!ED25519_sign(signature, message.data(), message.size(),
     38                     private_key.data())) {
     39     t->PrintLine("ED25519_sign failed");
     40     return false;
     41   }
     42 
     43   if (!t->ExpectBytesEqual(expected_signature.data(), expected_signature.size(),
     44                            signature, sizeof(signature))) {
     45     return false;
     46   }
     47 
     48   if (!ED25519_verify(message.data(), message.size(), signature,
     49                       public_key.data())) {
     50     t->PrintLine("ED25519_verify failed");
     51     return false;
     52   }
     53 
     54   return true;
     55 }
     56 
     57 static bool TestKeypairFromSeed() {
     58   uint8_t public_key1[32], private_key1[64];
     59   ED25519_keypair(public_key1, private_key1);
     60 
     61   uint8_t seed[32];
     62   OPENSSL_memcpy(seed, private_key1, sizeof(seed));
     63 
     64   uint8_t public_key2[32], private_key2[64];
     65   ED25519_keypair_from_seed(public_key2, private_key2, seed);
     66 
     67   if (OPENSSL_memcmp(public_key1, public_key2, sizeof(public_key1)) != 0 ||
     68       OPENSSL_memcmp(private_key1, private_key2, sizeof(private_key1)) != 0) {
     69     fprintf(stderr, "TestKeypairFromSeed: resulting keypairs did not match.\n");
     70     return false;
     71   }
     72 
     73   return true;
     74 }
     75 
     76 int main(int argc, char **argv) {
     77   if (argc != 2) {
     78     fprintf(stderr, "%s <test input.txt>\n", argv[0]);
     79     return 1;
     80   }
     81 
     82   return TestKeypairFromSeed() && FileTestMain(TestSignature, nullptr, argv[1]);
     83 }
     84