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 "../test/file_test.h"
     21 
     22 
     23 static bool TestSignature(FileTest *t, void *arg) {
     24   std::vector<uint8_t> private_key, public_key, message, expected_signature;
     25   if (!t->GetBytes(&private_key, "PRIV") ||
     26       private_key.size() != 64 ||
     27       !t->GetBytes(&public_key, "PUB") ||
     28       public_key.size() != 32 ||
     29       !t->GetBytes(&message, "MESSAGE") ||
     30       !t->GetBytes(&expected_signature, "SIG") ||
     31       expected_signature.size() != 64) {
     32     return false;
     33   }
     34 
     35   uint8_t signature[64];
     36   if (!ED25519_sign(signature, message.data(), message.size(),
     37                     private_key.data())) {
     38     t->PrintLine("ED25519_sign failed");
     39     return false;
     40   }
     41 
     42   if (!t->ExpectBytesEqual(expected_signature.data(), expected_signature.size(),
     43                            signature, sizeof(signature))) {
     44     return false;
     45   }
     46 
     47   if (!ED25519_verify(message.data(), message.size(), signature,
     48                       public_key.data())) {
     49     t->PrintLine("ED25519_verify failed");
     50     return false;
     51   }
     52 
     53   return true;
     54 }
     55 
     56 int main(int argc, char **argv) {
     57   if (argc != 2) {
     58     fprintf(stderr, "%s <test input.txt>\n", argv[0]);
     59     return 1;
     60   }
     61 
     62   return FileTestMain(TestSignature, nullptr, argv[1]);
     63 }
     64