diff options
| author | J08nY | 2017-09-13 00:40:13 +0200 |
|---|---|---|
| committer | J08nY | 2017-09-13 00:40:13 +0200 |
| commit | 607fe7b39f4e2ea579c935509c6e4dc68b43c457 (patch) | |
| tree | a27bf266515b6495c836de9a1ccb1f6398b1ecde /src | |
| parent | cceec4063e74bb29c79500f38aa40fc466180a0b (diff) | |
| download | ecgen-607fe7b39f4e2ea579c935509c6e4dc68b43c457.tar.gz ecgen-607fe7b39f4e2ea579c935509c6e4dc68b43c457.tar.zst ecgen-607fe7b39f4e2ea579c935509c6e4dc68b43c457.zip | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/gen/seed.c | 119 | ||||
| -rw-r--r-- | src/gen/seed.h | 25 | ||||
| -rw-r--r-- | src/gen/types.h | 32 | ||||
| -rw-r--r-- | src/io/cli.c | 6 | ||||
| -rw-r--r-- | src/util/binascii.c | 43 | ||||
| -rw-r--r-- | src/util/binascii.h | 34 | ||||
| -rw-r--r-- | src/util/bits.c | 26 | ||||
| -rw-r--r-- | src/util/bits.h | 2 |
8 files changed, 82 insertions, 205 deletions
diff --git a/src/gen/seed.c b/src/gen/seed.c index cc26cb4..1da7f2a 100644 --- a/src/gen/seed.c +++ b/src/gen/seed.c @@ -4,45 +4,32 @@ */ #include "seed.h" -#include <sha1/sha1.h> #include "io/output.h" -#include "util/binascii.h" +#include "util/bits.h" #include "util/memory.h" seed_t *seed_new(void) { return try_calloc(sizeof(seed_t)); } -static seed_t *seed_cpy(const seed_t *src, seed_t *dest) { - if (src->hex) { - dest->hex = try_strdup(src->hex); - dest->hex_len = src->hex_len; - } - if (src->raw) { - dest->raw = try_memdup(src->raw, src->raw_len); - dest->raw_len = src->raw_len; +seed_t *seed_copy(const seed_t *src, seed_t *dest) { + if (src->seed) { + dest->seed = bits_copy(src->seed); } if (src->hash20) { dest->hash20 = try_memdup(src->hash20, 20); } if (src->W) { - dest->W = try_memdup(src->W, src->W_len); - dest->W_len = src->W_len; + dest->W = bits_copy(src->W); } return dest; } -seed_t *seed_copy(const seed_t *src, seed_t *dest) { - if (src->seed) dest->seed = gcopy(src->seed); - return seed_cpy(src, dest); -} - seed_t *seed_new_copy(const seed_t *src) { seed_t *result = seed_new(); return seed_copy(src, result); } seed_t *seed_clone(const seed_t *src, seed_t *dest) { - if (src->seed) dest->seed = gclone(src->seed); - return seed_cpy(src, dest); + return seed_copy(src, dest); } seed_t *seed_new_clone(const seed_t *src) { @@ -52,95 +39,57 @@ seed_t *seed_new_clone(const seed_t *src) { void seed_free(seed_t **seed) { if (*seed) { - if ((*seed)->seed && isclone((*seed)->seed)) { - gunclone((*seed)->seed); - } - if ((*seed)->hex) { - try_free((*seed)->hex); - } - if ((*seed)->raw) { - try_free((*seed)->raw); + if ((*seed)->seed) { + bits_free(&(*seed)->seed); } if ((*seed)->hash20) { try_free((*seed)->hash20); } + if ((*seed)->W) { + bits_free(&(*seed)->W); + } try_free(*seed); *seed = NULL; } } -static GEN seed_stoi(const char *cstr) { - pari_sp ltop = avma; - - size_t len = strlen(cstr); - char *seed_str; - if (len <= 3 || !(cstr[0] == '0' && (cstr[1] == 'x' || cstr[1] == 'X'))) { - seed_str = try_malloc((size_t)(len + 3)); - strncpy(seed_str + 2, cstr, len); - seed_str[0] = '0'; - seed_str[1] = 'x'; - } else { - seed_str = try_malloc(len + 1); - strncpy(seed_str, cstr, len); +bool seed_valid(const char *hex_str) { + size_t len = strlen(hex_str); + if (len < 40) { + return false; } - GEN i = strtoi(seed_str); - - try_free(seed_str); - return gerepilecopy(ltop, i); -} - -static char *seed_itos(GEN seed) { - pari_sp ltop = avma; - char *result = pari_sprintf("%Px", seed); - char *seed_str = try_strdup(result); - - avma = ltop; - return seed_str; -} - -static char *seed_strip(const char *cstr) { - char *seed_str = try_malloc(strlen(cstr) + 1); - char *prefix = strstr(cstr, "0x"); - if (prefix != NULL) { - strcpy(seed_str, cstr + 2); - } else { - strcpy(seed_str, cstr); + const char *str_start = hex_str; + if (hex_str[0] == '0' && (hex_str[1] == 'x' || hex_str[1] == 'X')) { + str_start = hex_str + 2; + } + while (*str_start != 0) { + char c = *str_start++; + if (!isxdigit(c)) return false; } - return seed_str; + return true; } -static void seed_raw(seed_t *seed) { - seed->raw = binascii_itob(seed->seed, ENDIAN_BIG); - seed->raw_len = binascii_blen(seed->seed); +static bits_t *seed_stoi(const char *cstr) { + const char *seed_str = cstr; + const char *prefix = strstr(cstr, "0x"); + if (prefix != NULL) seed_str = prefix + 2; + return bits_from_hex(seed_str); } static void seed_hash(seed_t *seed) { seed->hash20 = try_malloc(20); - SHA_CTX ctx = {}; - SHA1_Init(&ctx); - SHA1_Update(&ctx, seed->raw, (int)seed->raw_len); - SHA1_Final(seed->hash20, &ctx); + bits_sha1(seed->seed, seed->hash20); } static void seed_W(seed_t *seed, const config_t *cfg) { GEN t = utoi(cfg->bits); GEN s = floorr(rdivii(subis(t, 1), stoi(160), DEFAULTPREC)); GEN h = subii(t, mulis(s, 160)); - GEN hash = binascii_btoi(seed->hash20, 20, ENDIAN_BIG); - GEN mask = subis(int2n(itos(h)), 1); - // TODO: what if I get zeros at the beginning? 0123 == 123 for PARI t_INT - // I should just convert to a t_VECSMALL of bits from the seed->hash and do - // everything with that. - // That's alot of custom code to handle bit strings. - GEN c0 = ibitand(hash, mask); } GENERATOR(seed_gen_random) { seed_t *seed = seed_new(); - seed->seed = random_int(160); - seed->hex = seed_itos(seed->seed); - seed->hex_len = strlen(seed->hex); - seed_raw(seed); + seed->seed = bits_from_i(random_int(160)); seed_hash(seed); seed_W(seed, cfg); curve->seed = seed; @@ -150,9 +99,6 @@ GENERATOR(seed_gen_random) { GENERATOR(seed_gen_argument) { seed_t *seed = seed_new(); seed->seed = seed_stoi(cfg->seed); - seed->hex = seed_strip(cfg->seed); - seed->hex_len = strlen(seed->hex); - seed_raw(seed); seed_hash(seed); seed_W(seed, cfg); curve->seed = seed; @@ -164,7 +110,7 @@ GENERATOR(seed_gen_input) { GEN str = input_string("seed:"); const char *cstr = GSTR(str); - if (strlen(cstr) < 40) { + if (!seed_valid(cstr)) { fprintf(err, "SEED must be at least 160 bits(40 hex characters).\n"); avma = ltop; return 0; @@ -172,9 +118,6 @@ GENERATOR(seed_gen_input) { seed_t *seed = seed_new(); seed->seed = seed_stoi(cstr); - seed->hex = seed_strip(cstr); - seed->hex_len = strlen(seed->hex); - seed_raw(seed); seed_hash(seed); seed_W(seed, cfg); curve->seed = seed; diff --git a/src/gen/seed.h b/src/gen/seed.h index 82eb499..1cd466d 100644 --- a/src/gen/seed.h +++ b/src/gen/seed.h @@ -12,13 +12,13 @@ #include "types.h" /** - * + * @brief * @return */ seed_t *seed_new(void); /** - * + * @brief * @param src * @param dest * @return @@ -26,14 +26,14 @@ seed_t *seed_new(void); seed_t *seed_copy(const seed_t *src, seed_t *dest); /** - * + * @brief * @param src * @return */ seed_t *seed_new_copy(const seed_t *src); /** - * + * @brief * @param src * @param dest * @return @@ -41,20 +41,27 @@ seed_t *seed_new_copy(const seed_t *src); seed_t *seed_clone(const seed_t *src, seed_t *dest); /** - * + * @brief * @param src * @return */ seed_t *seed_new_clone(const seed_t *src); /** - * + * @brief * @param seed */ void seed_free(seed_t **seed); /** - * + * @brief + * @param hex_str + * @return + */ +bool seed_valid(const char *hex_str); + +/** + * @brief * @param curve A curve_t being generated * @param cfg An application config * @param args unused @@ -63,7 +70,7 @@ void seed_free(seed_t **seed); GENERATOR(seed_gen_random); /** - * + * @brief * @param curve A curve_t being generated * @param cfg An application config * @param args unused @@ -72,7 +79,7 @@ GENERATOR(seed_gen_random); GENERATOR(seed_gen_argument); /** - * + * @brief * @param curve A curve_t being generated * @param cfg An application config * @param args unused diff --git a/src/gen/types.h b/src/gen/types.h index 8507847..b5de2db 100644 --- a/src/gen/types.h +++ b/src/gen/types.h @@ -14,16 +14,23 @@ /** * @brief + * @param bits + * @param bitlen + * @param allocated + */ +typedef struct { + unsigned char *bits; + size_t bitlen; + size_t allocated; +} bits_t; + +/** + * @brief */ typedef struct seed_t { - GEN seed; - char *hex; - size_t hex_len; - unsigned char *raw; - size_t raw_len; + bits_t *seed; unsigned char *hash20; - char *W; - size_t W_len; + bits_t *W; } seed_t; /** @@ -89,17 +96,6 @@ typedef struct { } arg_t; /** - * @brief - */ -typedef enum { ENDIAN_BIG = 0, ENDIAN_LITTLE } endian_e; - -typedef struct { - unsigned char *bits; - size_t bitlen; - size_t allocated; -} bits_t; - -/** * @brief A generator function type. * @param curve A curve_t being generated * @param cfg An application config diff --git a/src/io/cli.c b/src/io/cli.c index c37652c..dbffd89 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -6,6 +6,7 @@ #include <string.h> #include <unistd.h> #include "config.h" +#include "gen/seed.h" char cli_doc[] = "ecgen, tool for generating Elliptic curve domain parameters.\v(C) 2017 " @@ -200,10 +201,7 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { case OPT_SEED: cfg->from_seed = true; if (arg) { - // ANSI X9.62 specifies seed as at least 160 bits in length. - // TODO: validate that it is a hex string, or what actually? It - // can be any PARI int. so 123465689 or 0xab45 or 0b1101100100 - if (strlen(arg) < 40) { + if (!seed_valid(arg)) { argp_failure( state, 1, 0, "SEED must be at least 160 bits (40 characters)."); diff --git a/src/util/binascii.c b/src/util/binascii.c deleted file mode 100644 index bbc8e89..0000000 --- a/src/util/binascii.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ -#include "binascii.h" -#include "util/memory.h" - -size_t binascii_blen(GEN i) { - pari_sp ltop = avma; - size_t result = (size_t)glength(binary_2k_nv(i, 8)); - avma = ltop; - return result; -} - -unsigned char *binascii_itob(GEN i, endian_e endianity) { - pari_sp ltop = avma; - GEN digits = binary_2k_nv(i, 8); - if (endianity == ENDIAN_LITTLE) { - digits = vecsmall_reverse(digits); - } - long blen = glength(digits); - unsigned char *result = try_malloc((size_t)blen); - for (long j = 1; j <= blen; ++j) { - result[j - 1] = (unsigned char)gel(digits, j); - } - - avma = ltop; - return result; -} - -GEN binascii_btoi(const unsigned char *bytes, size_t len, endian_e endianity) { - pari_sp ltop = avma; - GEN result = gen_0; - for (size_t i = 0; i < len; ++i) { - size_t index = i; - if (endianity == ENDIAN_LITTLE) { - index = (len - 1) - i; - } - result = addis(result, bytes[index]); - if (i < len - 1) result = shifti(result, 8); - } - return gerepileupto(ltop, result); -} diff --git a/src/util/binascii.h b/src/util/binascii.h deleted file mode 100644 index a6301ef..0000000 --- a/src/util/binascii.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ -#ifndef ECGEN_BINASCII_H -#define ECGEN_BINASCII_H - -#include <pari/pari.h> -#include "gen/types.h" - -/** - * @brief - * @param i - * @return - */ -size_t binascii_blen(GEN i); - -/** - * @brief - * @param i - * @param endianity - * @return - */ -unsigned char *binascii_itob(GEN i, endian_e endianity); - -/** - * @brief - * @param bytes - * @param endianity - * @return - */ -GEN binascii_btoi(const unsigned char *bytes, size_t len, endian_e endianity); - -#endif // ECGEN_BINASCII_H diff --git a/src/util/bits.c b/src/util/bits.c index e573505..e2d5d79 100644 --- a/src/util/bits.c +++ b/src/util/bits.c @@ -3,8 +3,9 @@ * Copyright (C) 2017 J08nY */ -#include <gen/types.h> #include "bits.h" +#include <gen/types.h> +#include <sha1/sha1.h> #include "util/memory.h" bits_t *bits_new(size_t bit_len) { @@ -148,8 +149,8 @@ static unsigned char and_func(unsigned char one, unsigned char other) { } static bits_t *bits_bitwise(const bits_t *one, const bits_t *other, - unsigned char (*bitwise_func)(unsigned char, - unsigned char)) { + unsigned char (*bitwise_func)(unsigned char, + unsigned char)) { const bits_t *shorter; const bits_t *longer; if (one->bitlen > other->bitlen) { @@ -165,7 +166,7 @@ static bits_t *bits_bitwise(const bits_t *one, const bits_t *other, size_t longer_pos = longer->bitlen - i - 1; unsigned char longer_bit = - (unsigned char)GET_BIT(longer->bits, longer_pos); + (unsigned char)GET_BIT(longer->bits, longer_pos); unsigned char shorter_bit = 0; if (shorter->bitlen > i) { size_t shorter_pos = shorter->bitlen - i - 1; @@ -222,9 +223,9 @@ void bits_rotz(bits_t *bits) { size_t left_pos = i; size_t right_pos = bits->bitlen - i - 1; unsigned char left_bit = - (unsigned char)GET_BIT(original_bits, left_pos); + (unsigned char)GET_BIT(original_bits, left_pos); unsigned char right_bit = - (unsigned char)GET_BIT(original_bits, right_pos); + (unsigned char)GET_BIT(original_bits, right_pos); bits->bits[right_pos / 8] |= left_bit << (7 - (right_pos % 8)); bits->bits[left_pos / 8] |= right_bit << (7 - (left_pos % 8)); } @@ -232,7 +233,7 @@ void bits_rotz(bits_t *bits) { size_t middle_pos = bits->bitlen / 2; unsigned char middle_bit = - (unsigned char)GET_BIT(original_bits, middle_pos); + (unsigned char)GET_BIT(original_bits, middle_pos); bits->bits[middle_pos / 8] |= middle_bit << (7 - (middle_pos % 8)); } } @@ -253,7 +254,7 @@ void bits_shiftz(bits_t *bits, long amount) { for (size_t i = 0; i < bits->bitlen; ++i) { unsigned char new_bit = 0; if ((amount > 0 && i + amount < bits->bitlen) || - (amount < 0 && i >= -amount)) { + (amount < 0 && i >= -amount)) { new_bit = (unsigned char)GET_BIT(original_bits, i + amount); } bits->bits[i / 8] |= new_bit << (7 - (i % 8)); @@ -277,7 +278,7 @@ void bits_shiftrz(bits_t *bits, long amount) { unsigned char new_bit = 0; size_t new_pos = 0; if ((amount > 0 && i + amount < bits->bitlen) || - (amount < 0 && i >= -amount)) { + (amount < 0 && i >= -amount)) { new_pos = i + amount; } else if (amount > 0) { new_pos = (i + amount) % bits->bitlen; @@ -362,6 +363,13 @@ bits_t *bits_shorten(const bits_t *bits, long amount) { return result; } +void bits_sha1(const bits_t *bits, unsigned char hashout[20]) { + SHA_CTX ctx = {}; + SHA1_Init(&ctx); + SHA1_Update(&ctx, bits->bits, (int)BYTE_LEN(bits->bitlen)); + SHA1_Final(hashout, &ctx); +} + bool bits_eq(const bits_t *one, const bits_t *other) { if (one->bitlen != other->bitlen) return false; if (one->bitlen == 0) return true; diff --git a/src/util/bits.h b/src/util/bits.h index 3deeab0..376c736 100644 --- a/src/util/bits.h +++ b/src/util/bits.h @@ -74,6 +74,8 @@ void bits_shortenz(bits_t *bits, long amount); bits_t *bits_shorten(const bits_t *bits, long amount); +void bits_sha1(const bits_t *bits, unsigned char hashout[20]); + bool bits_eq(const bits_t *one, const bits_t *other); #endif // ECGEN_BITS_H |
