3 Copyright (C) 2014 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
32 #include "testutils.h"
41 decode_hex (size_t length, uint8_t *dst, const char *src)
43 struct base16_decode_ctx ctx;
45 base16_decode_init (&ctx);
46 ASSERT (base16_decode_update (&ctx, &out_size, dst, 2*length, src));
47 ASSERT (out_size == length);
48 ASSERT (base16_decode_final (&ctx));
51 /* Processes a single line in the format of
52 http://ed25519.cr.yp.to/python/sign.input:
54 sk pk : pk : m : s m :
56 where sk (secret key) and pk (public key) are 32 bytes each, m is
57 variable size, and s is 64 bytes. All values hex encoded.
60 test_one (const char *line)
64 uint8_t sk[ED25519_KEY_SIZE];
65 uint8_t pk[ED25519_KEY_SIZE];
66 uint8_t t[ED25519_KEY_SIZE];
67 uint8_t s[ED25519_SIGNATURE_SIZE];
70 uint8_t s2[ED25519_SIGNATURE_SIZE];
72 decode_hex (ED25519_KEY_SIZE, sk, line);
74 p = strchr (line, ':');
75 ASSERT (p == line + 128);
77 decode_hex (ED25519_KEY_SIZE, pk, p);
79 ASSERT (p == line + 193);
83 ASSERT ((p - mp) % 2 == 0);
84 msg_size = (p - mp) / 2;
86 decode_hex (ED25519_SIGNATURE_SIZE, s, p+1);
88 msg = xalloc (msg_size + 1);
91 decode_hex (msg_size, msg, mp);
93 ed25519_sha512_public_key (t, sk);
94 ASSERT (MEMEQ(ED25519_KEY_SIZE, t, pk));
96 ed25519_sha512_sign (pk, sk, msg_size, msg, s2);
97 ASSERT (MEMEQ (ED25519_SIGNATURE_SIZE, s, s2));
99 ASSERT (ed25519_sha512_verify (pk, msg_size, msg, s));
101 s2[ED25519_SIGNATURE_SIZE/3] ^= 0x40;
102 ASSERT (!ed25519_sha512_verify (pk, msg_size, msg, s2));
104 memcpy (s2, s, ED25519_SIGNATURE_SIZE);
105 s2[2*ED25519_SIGNATURE_SIZE/3] ^= 0x40;
106 ASSERT (!ed25519_sha512_verify (pk, msg_size, msg, s2));
108 ASSERT (!ed25519_sha512_verify (pk, msg_size + 1, msg, s));
112 msg[msg_size-1] ^= 0x20;
113 ASSERT (!ed25519_sha512_verify (pk, msg_size, msg, s));
120 getline(char **lineptr, size_t *n, FILE *f)
127 *lineptr = xalloc (*n);
141 (*lineptr) [i++] = c;
145 *lineptr = realloc (*lineptr, *n);
147 die ("Virtual memory exhausted.\n");
159 const char *input = getenv ("ED25519_SIGN_INPUT");
164 FILE *f = fopen (input, "r");
166 die ("Opening input file '%s' failed: %s\n",
167 input, strerror (errno));
169 for (buf = NULL; getline (&buf, &buf_size, f) >= 0; )
177 /* First few lines only */
178 test_one ("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a:d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a::e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b:");
179 test_one ("4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c:3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c:72:92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c0072:");
180 test_one ("c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025:fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025:af82:6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40aaf82:");
181 test_one ("0d4a05b07352a5436e180356da0ae6efa0345ff7fb1572575772e8005ed978e9e61a185bcef2613a6c7cb79763ce945d3b245d76114dd440bcf5f2dc1aa57057:e61a185bcef2613a6c7cb79763ce945d3b245d76114dd440bcf5f2dc1aa57057:cbc77b:d9868d52c2bebce5f3fa5a79891970f309cb6591e3e1702a70276fa97c24b3a8e58606c38c9758529da50ee31b8219cba45271c689afa60b0ea26c99db19b00ccbc77b:");