diff --git a/misc/debug-bn.c b/misc/debug-bn.c new file mode 100644 index 0000000..c9092da --- /dev/null +++ b/misc/debug-bn.c @@ -0,0 +1,220 @@ +/* + * debug-bn.c - Debug Bignum + * Copyright (C) 2014 Free Software Initiative of Japan + * Author: NIIBE Yutaka + * + */ + +#include +#include +#include +#include +#include + +#include "bn.h" + +void +print_le_bn256 (const bn256 *X) +{ + int i; + const uint8_t *p = (const uint8_t *)X; + + for (i = 0; i < 32; i++) + printf ("%02x", p[i]); + puts (""); +} + +void +print_be_bn256 (const bn256 *X) +{ + int i; + + for (i = 7; i >= 0; i--) + printf ("%08x", X->word[i]); + puts (""); +} + +#define MAXLINE 4096 + +static int lineno; +static int test_no; +static bn256 sk[1]; +static bn256 pk[1]; +static unsigned char msg[MAXLINE]; +static size_t msglen; +static bn512 sig[1]; + +const char * +skip_white_space (const char *l) +{ + while (*l != '\n' && isspace (*l)) + l++; + + return l; +} + + +static int +read_hex_4bit (char c) +{ + int r; + + if (c >= '0' && c <= '9') + r = c - '0'; + else if (c >= 'a' && c <= 'f') + r = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + r = c - 'A' + 10; + else + r = -1; + return r; +} + +static int +read_hex_8bit (const char **l_p) +{ + const char *l = *l_p; + int r, v; + + r = read_hex_4bit (*l++); + if (r < 0) + return -1; + v = r*16; + r = read_hex_4bit (*l++); + if (r < 0) + return -1; + v += r; + + *l_p = l; + return v; +} + +static int +read_msg (unsigned char *msg, const char *l, int len) +{ + int i, r; + + for (i = 0; i < len; i++) + { + r = read_hex_8bit (&l); + if (r < 0) + return -1; + msg[i] = r; + } + + return 0; +} + + +static int +read_le_bn256 (bn256 *sk, const char *l) +{ + int i; + uint8_t *p = (uint8_t *)sk; + + for (i = 0; i < sizeof (bn256); i++) + { + int r; + + if (*l == '\n') + { + /* should support small input??? */ + return -1; + } + + r = read_hex_8bit (&l); + if (r < 0) + return -1; + + p[i] = r; + } + + return 0; +} + +static int +read_be_bn256 (bn256 *sk, const char *l) +{ + int i; + uint8_t *p = (uint8_t *)sk; + + for (i = 0; i < sizeof (bn256); i++) + { + int r; + + if (*l == '\n') + { + /* should support small input??? */ + return -1; + } + + r = read_hex_8bit (&l); + if (r < 0) + return -1; + + p[31 - i] = r; + } + + return 0; +} + + +static int +read_pk (bn256 *pk, const char *l, int len) +{ + int r; + + if (len == 64) /* 64 chars == 32-byte */ + { /* compressed form */ + r = read_le_bn256 (pk, l); + if (r < 0) + return -1; + return 0; + } + else + { + bn256 x[1]; + + r = read_hex_8bit (&l); + if (r < 0) + return -1; + if (r != 4) + return -1; + + r = read_be_bn256 (x, l); + if (r < 0) + return -1; + r = read_be_bn256 (pk, l+64); + if (r < 0) + return -1; + + pk->word[7] ^= (x->word[0] & 1) * 0x80000000; + return 0; + } +} + +static int +read_le_bn512 (bn512 *sig, const char *l) +{ + int i; + uint8_t *p = (uint8_t *)sig; + + for (i = 0; i < sizeof (bn512); i++) + { + int r; + + if (*l == '\n') + { + /* should support small input??? */ + return -1; + } + + r = read_hex_8bit (&l); + if (r < 0) + return -1; + + p[i] = r; + } + + return 0; +} diff --git a/misc/t-mont.c b/misc/t-mont.c new file mode 100644 index 0000000..6a1b2f1 --- /dev/null +++ b/misc/t-mont.c @@ -0,0 +1,92 @@ +/* + * t-eddsa.c - testing EdDSA + * Copyright (C) 2014 Free Software Initiative of Japan + * Author: NIIBE Yutaka + * + * Run following commands. The file t-ed25519.inp is available in GNU + * libgcrypt source code under 'tests' directory. + + gcc -Wall -c -DBN256_C_IMPLEMENTATION ecc-mont.c + gcc -Wall -c -DBN256_NO_RANDOM -DBN256_C_IMPLEMENTATION bn.c + gcc -Wall -c mod.c + gcc -Wall -c -DBN256_C_IMPLEMENTATION mod25638.c + gcc -Wall -c t-mont.c + gcc -Wall -c debug-bn.c + gcc -o t-mont t-mont.o ecc-mont.o bn.o mod.o mod25638.o debug-bn.o + + + * + */ + +#include +#include +#include +#include +#include + +#include "bn.h" + +const uint8_t k[32] = { + 0x30, 0x01, 0x33, 0xE7, 0xDC, 0x52, 0xAD, 0x9F, + 0x89, 0xFE, 0xC0, 0x59, 0x4A, 0x6D, 0x65, 0xE5, + 0xF8, 0x7A, 0xD6, 0xA9, 0xA4, 0x89, 0x00, 0xB1, + 0x93, 0x7E, 0xD3, 0x6F, 0x09, 0x1E, 0xB7, 0x76, +}; + +int +main (int argc, char *argv[]) +{ + int all_good = 1; + int r; + bn256 *pk; + bn256 a[1]; + uint8_t out[32]; + + extern void ecdh_decrypt_curve25519 (const uint8_t *input, + uint8_t *output, + const bn256 *k); + extern uint8_t *ecdh_compute_public_25519 (const uint8_t*k); + extern void print_le_bn256 (const bn256 *X); + + while (1) + { +#if 0 + hash[0] &= 248; + hash[31] &= 127; + hash[31] |= 64; + memcpy (a, hash, sizeof (bn256)); /* Lower half of hash */ +#endif + + pk = ecdh_compute_public_25519 (k); + print_le_bn256 (pk); + return 0; + +#if 0 + if (memcmp (pk, pk_calculated, sizeof (bn256)) != 0) + { + printf ("ERR PK: %d\n", test_no); + print_be_bn256 (sk); + print_be_bn256 (pk); + print_be_bn256 (pk_calculated); + all_good = 0; + continue; + } + + ecdh_decrypt_25519 (msg, out, a); + if (memcmp (sig, R, sizeof (bn256)) != 0 + || memcmp (((const uint8_t *)sig)+32, S, sizeof (bn256)) != 0) + { + printf ("ERR SIG: %d\n", test_no); + print_le_bn256 (R); + print_le_bn256 (S); + print_le_bn256 ((const bn256 *)sig); + print_le_bn256 ((const bn256 *)(((const uint8_t *)sig)+32)); + all_good = 0; + continue; + } + + printf ("%d\n", test_no); +#endif + } + return all_good == 1?0:1; +}