Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 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 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 56 | static bool TestKeypairFromSeed() { |
| 57 | uint8_t public_key1[32], private_key1[64]; |
| 58 | ED25519_keypair(public_key1, private_key1); |
| 59 | |
| 60 | uint8_t seed[32]; |
| 61 | memcpy(seed, private_key1, sizeof(seed)); |
| 62 | |
| 63 | uint8_t public_key2[32], private_key2[64]; |
| 64 | ED25519_keypair_from_seed(public_key2, private_key2, seed); |
| 65 | |
| 66 | if (memcmp(public_key1, public_key2, sizeof(public_key1)) != 0 || |
| 67 | memcmp(private_key1, private_key2, sizeof(private_key1)) != 0) { |
| 68 | fprintf(stderr, "TestKeypairFromSeed: resulting keypairs did not match.\n"); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 75 | int main(int argc, char **argv) { |
| 76 | if (argc != 2) { |
| 77 | fprintf(stderr, "%s <test input.txt>\n", argv[0]); |
| 78 | return 1; |
| 79 | } |
| 80 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 81 | return TestKeypairFromSeed() && FileTestMain(TestSignature, nullptr, argv[1]); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 82 | } |