diff options
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| -rw-r--r-- | src/Makefile | 4 | ||||
| -rw-r--r-- | src/cm/cm.c | 2 | ||||
| -rw-r--r-- | src/cm/cm.h | 9 | ||||
| -rw-r--r-- | src/ecgen.c | 63 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.c (renamed from src/random/generators.c) | 31 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.h (renamed from src/random/generators.h) | 16 | ||||
| -rw-r--r-- | src/exhaustive/seed.c (renamed from src/random/seed.c) | 0 | ||||
| -rw-r--r-- | src/exhaustive/seed.h (renamed from src/random/seed.h) | 2 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 24 | ||||
| -rw-r--r-- | src/invalid/invalid.h | 9 | ||||
| -rw-r--r-- | src/io/cli.c | 52 | ||||
| -rw-r--r-- | src/io/input.c | 4 | ||||
| -rw-r--r-- | src/io/output.c | 4 | ||||
| -rw-r--r-- | src/math/curve.c | 19 | ||||
| -rw-r--r-- | src/math/equation.c | 2 | ||||
| -rw-r--r-- | src/math/field.c | 2 | ||||
| -rw-r--r-- | src/math/order.c | 21 | ||||
| -rw-r--r-- | src/math/order.h | 28 | ||||
| -rw-r--r-- | src/math/types.c | 7 | ||||
| -rw-r--r-- | src/math/types.h (renamed from src/types.h) | 13 |
21 files changed, 223 insertions, 94 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 673aa0b..1dd078a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,11 @@ add_custom_target(gp2c ALL DEPENDS gp.c gp.h) include_directories(src) include_directories(lib) -file(GLOB SOURCES "src/*.c" "src/cm/*.c" "src/invalid/*.c" "src/io/*.c" "src/random/*.c" "src/math/*.c") +file(GLOB SOURCES "src/*.c" "src/math/*.c" "src/cm/*.c" "src/invalid/*.c" "src/io/*.c" "src/exhaustive/*.c" ) add_executable(ecgen ${SOURCES}) -target_link_libraries(ecgen pari) find_library(parson parson/libparson.a) + +target_link_libraries(ecgen pari) target_link_libraries(ecgen ${parson})
\ No newline at end of file diff --git a/src/Makefile b/src/Makefile index d28102c..397ed22 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,12 +14,12 @@ LDFLAGS=-L../lib GP_CFLAGS=-O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC GPFLAGS=-g -i4 -INCLUDES=-I. -I../lib -Icm -Iinvalid -Iio -Irandom +INCLUDES=-I. -I../lib -Icm -Iinvalid -Iio -Irandom -Iexhaustive LIBS=-lpari -lparson #### -VPATH = cm:invalid:io:random:math +VPATH = cm:invalid:io:exhaustive:math GP = GPC = $(addsuffix .c, $(GP)) diff --git a/src/cm/cm.c b/src/cm/cm.c index a86dcdd..63a0005 100644 --- a/src/cm/cm.c +++ b/src/cm/cm.c @@ -3,3 +3,5 @@ * Copyright (C) 2017 J08nY */ #include "cm.h" + +int cm_do(config_t *cfg) {}
\ No newline at end of file diff --git a/src/cm/cm.h b/src/cm/cm.h index ba7c2c4..fe54f18 100644 --- a/src/cm/cm.h +++ b/src/cm/cm.h @@ -2,7 +2,16 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ +#include "io/cli.h" + #ifndef ECGEN_CM_H #define ECGEN_CM_H +/** + * + * @param cfg + * @return + */ +int cm_do(config_t *cfg); + #endif // ECGEN_CM_H diff --git a/src/ecgen.c b/src/ecgen.c index 54d7f30..3b98e12 100644 --- a/src/ecgen.c +++ b/src/ecgen.c @@ -17,18 +17,19 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ +#include <pari/pari.h> +#include "cm/cm.h" +#include "exhaustive/exhaustive.h" +#include "invalid/invalid.h" +#include "io/input.h" +#include "io/output.h" + /** * @author J08nY <johny@neuromancer.sk> * @license GPL v2.0 * @version 0.2 */ -#include <time.h> -#include "io/input.h" -#include "io/output.h" -#include "math/curve.h" -#include "random/generators.h" - const char *argp_program_version = "ecgen 0.2\n" "Copyright (C) 2017 J08nY\n" @@ -71,6 +72,38 @@ int quit(int status) { return status; } +/** + * Three fundamentally different Elliptic curve generation approaches can be + * taken. + * - Complex Multiplication: + * - Capable of generating a curve of a given prime order. + * - Generates a subset of all Elliptic Curves over a given field. + * - Used with the -n / --order option + * + * - [Broker, Stevenhagen] - https://arxiv.org/abs/0712.2022 + * + * - Invalid curve generation: + * - Generates *invalid* curves for a given curve. + * - These curves have the same field, and *A* parameter in the short + * Weierstrass equation. + * - Multiplication using some(most?) scalar multiplication algorithm + * proceeds the same way + * multiplication on the input curve would. + * + * - [Antipa, Brown, Menezes, Struik, Vanstone] - + * https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf + * - [Biehl, Mayer, Muller] - + * http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.107.3920&rep=rep1&type=pdf + * - [Jager, Schwenk, Somorovksy] - + * http://euklid.org/pdf/ECC_Invalid_Curve.pdf + * + * - Exhaustive/Random approach: + * - Generates field and equation parameters: + * - randomly + * - using ANSI X9.62 verifiably random method(from seed) + * - given input + * , until a curve with requested properties appears. + */ int main(int argc, char *argv[]) { // Parse cli args memset(&cfg, 0, sizeof(cfg)); @@ -80,21 +113,13 @@ int main(int argc, char *argv[]) { return quit(1); } + int status = 0; if (cfg.cm) { + status = cm_do(&cfg); } else if (cfg.invalid) { + status = invalid_do(&cfg); } else { - gen_t generators[5]; - gen_init(generators, &cfg); - - curve_t *curve = curve_new(); - int state = 0; - while (state != 5) { - int diff = generators[state](curve, &cfg); - state += diff; - } - output_csv(out, "%Px", ';', curve_params(curve)); - curve_free(&curve); + status = exhaustive_do(&cfg); } - - return quit(0); + return quit(status); } diff --git a/src/random/generators.c b/src/exhaustive/exhaustive.c index 9769166..402443c 100644 --- a/src/random/generators.c +++ b/src/exhaustive/exhaustive.c @@ -2,15 +2,15 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ -#include "generators.h" +#include "exhaustive.h" +#include "io/output.h" #include "math/curve.h" #include "math/equation.h" #include "math/field.h" -#include "random/seed.h" +#include "math/order.h" +#include "seed.h" -int gen_skip(curve_t *curve, config_t *config, ...) { return 1; } - -void gen_init(gen_t generators[], config_t *config) { +void exhaustive_init(gen_t generators[], config_t *config) { if (config->from_seed) { if (config->seed) { generators[OFFSET_SEED] = &seed_argument; @@ -39,10 +39,12 @@ void gen_init(gen_t generators[], config_t *config) { generators[OFFSET_A] = &a_zero; } + generators[OFFSET_CURVE] = &curve_nonzero; + if (config->prime) { - generators[OFFSET_CURVE] = &curve_prime; + generators[OFFSET_ORDER] = &order_prime; } else { - generators[OFFSET_CURVE] = &curve_nonzero; + generators[OFFSET_ORDER] = &order_init; } } @@ -51,4 +53,19 @@ void gen_init(gen_t generators[], config_t *config) { } else { generators[OFFSET_FIELD] = &field_input; } +} + +int exhaustive_do(config_t *cfg) { + gen_t generators[OFFSET_END]; + exhaustive_init(generators, cfg); + + curve_t *curve = curve_new(); + int state = 0; + while (state != OFFSET_POINTS) { + int diff = generators[state](curve, cfg); + state += diff; + } + output_csv(out, "%Px", ';', curve_params(curve)); + curve_free(&curve); + return 0; }
\ No newline at end of file diff --git a/src/random/generators.h b/src/exhaustive/exhaustive.h index 87e599b..ee103f4 100644 --- a/src/random/generators.h +++ b/src/exhaustive/exhaustive.h @@ -5,21 +5,13 @@ #ifndef ECGEN_GENERATORS_H #define ECGEN_GENERATORS_H -#include "types.h" - -enum gen_offset { - OFFSET_SEED, - OFFSET_FIELD, - OFFSET_A, - OFFSET_B, - OFFSET_CURVE, - OFFSET_POINTS -}; +#include "math/types.h" /** * - * @param generators + * @param cfg + * @return */ -void gen_init(gen_t generators[], config_t *config); +int exhaustive_do(config_t *cfg); #endif // ECGEN_GENERATORS_H diff --git a/src/random/seed.c b/src/exhaustive/seed.c index a78ff24..a78ff24 100644 --- a/src/random/seed.c +++ b/src/exhaustive/seed.c diff --git a/src/random/seed.h b/src/exhaustive/seed.h index 998bdcd..7c96372 100644 --- a/src/random/seed.h +++ b/src/exhaustive/seed.h @@ -6,7 +6,7 @@ #define ECGEN_SEED_H #include "io/cli.h" -#include "types.h" +#include "math/types.h" /** * diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index a600dcb..3259908 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -3,3 +3,27 @@ * Copyright (C) 2017 J08nY */ #include "invalid.h" +#include "math/curve.h" +#include "math/field.h" +#include "math/equation.h" + +int invalid_do(config_t *cfg) { + // create the curve to invalidate + // Either from input or random with -r + + curve_t *curve = curve_new(); + gen_t gen[OFFSET_END]; + gen[OFFSET_SEED] = &gen_skip; + if (cfg->random) { + gen[OFFSET_FIELD] = &field_random; + gen[OFFSET_A] = &a_random; + gen[OFFSET_B] = &b_random; + } else { + gen[OFFSET_FIELD] = &field_input; + gen[OFFSET_A] = &a_input; + gen[OFFSET_B] = &b_input; + } + curve_free(&curve); + + return 0; +}
\ No newline at end of file diff --git a/src/invalid/invalid.h b/src/invalid/invalid.h index 180c409..0790af7 100644 --- a/src/invalid/invalid.h +++ b/src/invalid/invalid.h @@ -2,7 +2,16 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ +#include "io/cli.h" + #ifndef ECGEN_INVALID_H #define ECGEN_INVALID_H +/** + * + * @param cfg + * @return + */ +int invalid_do(config_t *cfg); + #endif // ECGEN_INVALID_H diff --git a/src/io/cli.c b/src/io/cli.c index 12ceabd..011e6a7 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -6,8 +6,8 @@ #include <string.h> char doc[] = - "ecgen, tool for generating Elliptic curve domain parameters.\v(C) 2017 " - "Eastern Seaboard Phishing Authority"; + "ecgen, tool for generating Elliptic curve domain parameters.\v(C) 2017 " + "Eastern Seaboard Phishing Authority"; char args_doc[] = "bits"; enum opt_keys { @@ -28,20 +28,20 @@ enum opt_keys { // clang-format off struct argp_option options[] = { // Field specification - {"fp", OPT_FP, 0, 0, "Prime field."}, - {"f2m", OPT_F2M, 0, 0, "Binary field."}, + {"fp", OPT_FP, 0, 0, "Prime field."}, + {"f2m", OPT_F2M, 0, 0, "Binary field."}, // Curve specification - {"random", OPT_RANDOM, 0, 0, "Generate a random curve."}, - {"prime", OPT_PRIME, 0, 0, "Generate a curve with prime order."}, - {"seed", OPT_SEED, "SEED", OPTION_ARG_OPTIONAL, "Generate a curve from SEED (ANSI X9.62 verifiable procedure)."}, - {"invalid", OPT_INVALID, 0, 0, "Generate a set of invalid curves (for a given curve)."}, - {"order", OPT_ORDER, "ORDER", 0, "Generate a curve with given order (using Complex Multiplication)."}, - {"koblitz", OPT_KOBLITZ, 0, 0, "Generate a Koblitz curve."}, + {"exhaustive", OPT_RANDOM, 0, 0, "Generate a exhaustive curve."}, + {"prime", OPT_PRIME, 0, 0, "Generate a curve with prime order."}, + {"seed", OPT_SEED, "SEED", OPTION_ARG_OPTIONAL, "Generate a curve from SEED (ANSI X9.62 verifiable procedure)."}, + {"invalid", OPT_INVALID, 0, 0, "Generate a set of invalid curves (for a given curve)."}, + {"order", OPT_ORDER, "ORDER", 0, "Generate a curve with given order (using Complex Multiplication)."}, + {"koblitz", OPT_KOBLITZ, 0, 0, "Generate a Koblitz curve."}, // Other - {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package)."}, - {"input", OPT_INPUT, "FILE", 0, "Input from file."}, - {"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!"}, - {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite)."}, + {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package)."}, + {"input", OPT_INPUT, "FILE", 0, "Input from file."}, + {"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!"}, + {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite)."}, {0}}; // clang-format on @@ -85,8 +85,8 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { // ANSI X9.62 specifies seed as at least 160 bits in length. if (strlen(arg) < 20) { argp_failure( - state, 1, 0, - "SEED must be at least 160 bits(20 characters)."); + state, 1, 0, + "SEED must be at least 160 bits(20 characters)."); } cfg->seed = arg; } @@ -111,22 +111,22 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { // Only one field if (!cfg->prime_field && !cfg->binary_field) { argp_failure(state, 1, 0, - "Specify field type, prime or binary, with --fp / " - "--f2m(but not both)."); + "Specify field type, prime or binary, with --fp / " + "--f2m(but not both)."); } - // Invalid is not prime or seed or random by definition. + // Invalid is not prime or seed or exhaustive by definition. if (cfg->invalid && (cfg->prime || cfg->from_seed || cfg->random)) { - // not seed, not prime, not random + // not seed, not prime, not exhaustive argp_failure(state, 1, 0, - "Invalid curve generation can not generate curves " - "from seed, random or prime order."); + "Invalid curve generation can not generate curves " + "from seed, exhaustive or prime order."); } if (cfg->cm && (cfg->prime || cfg->from_seed || cfg->invalid)) { argp_failure(state, 1, 0, - "Fixed order curve generation can not generate " - "curves from seed, or invalid curves. Prime order " - "also doesn't make sense if the given one isn't " - "prime."); + "Fixed order curve generation can not generate " + "curves from seed, or invalid curves. Prime order " + "also doesn't make sense if the given one isn't " + "prime."); } break; case ARGP_KEY_NO_ARGS: diff --git a/src/io/input.c b/src/io/input.c index 3dcca00..7d6b614 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -2,8 +2,8 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ -#include <parson/parson.h> #include "input.h" +#include <parson/parson.h> FILE *in; @@ -75,7 +75,7 @@ GEN fread_string(FILE *stream, const char *prompt, int delim) { } GEN fread_param(param_t param, FILE *stream, const char *prompt, long bits, - int delim) { + int delim) { switch (param) { case PARAM_PRIME: return fread_prime(stream, prompt, bits, delim); diff --git a/src/io/output.c b/src/io/output.c index 86de015..141360e 100644 --- a/src/io/output.c +++ b/src/io/output.c @@ -47,9 +47,7 @@ void output_csv(FILE *out, const char *format, char delim, GEN vector) { free(string); } -char *output_sjson(GEN vector) { - parson -} +char *output_sjson(GEN vector) {} void output_json(FILE *out, GEN vector) {} diff --git a/src/math/curve.c b/src/math/curve.c index 422156b..e9d1364 100644 --- a/src/math/curve.c +++ b/src/math/curve.c @@ -3,8 +3,8 @@ * Copyright (C) 2017 J08nY */ #include "curve.h" +#include "exhaustive/seed.h" #include "field.h" -#include "random/seed.h" curve_t *curve_new() { curve_t *curve = pari_malloc(sizeof(curve_t)); @@ -59,23 +59,6 @@ int curve_nonzero(curve_t *curve, config_t *config, ...) { } } -int curve_prime(curve_t *curve, config_t *config, ...) { - pari_sp ltop = avma; - int nonzero = curve_nonzero(curve, config); - if (nonzero == 1) { - curve->order = ellsea(curve->curve, 1); - if (gequal0(curve->order) || !(isprime(curve->order))) { - avma = ltop; - return -3; - } else { - return 1; - } - } else { - avma = ltop; - return nonzero; - } -} - int curve_seed_fp(curve_t *curve, config_t *config, ...) {} int curve_seed_f2m(curve_t *curve, config_t *config, ...) {} diff --git a/src/math/equation.c b/src/math/equation.c index 571ee71..b699b9a 100644 --- a/src/math/equation.c +++ b/src/math/equation.c @@ -2,8 +2,8 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ -#include <io/input.h> #include "equation.h" +#include "io/input.h" int eq_random(curve_t *curve, config_t *config, ...) { int r = a_random(curve, config) + b_random(curve, config); diff --git a/src/math/field.c b/src/math/field.c index ba2972c..a4b038e 100644 --- a/src/math/field.c +++ b/src/math/field.c @@ -32,7 +32,7 @@ int field_random(curve_t *curve, config_t *config, ...) { } int field_input(curve_t *curve, config_t *config, ...) { - return -1; // NOT IMPLEMENTED + return INT_MIN; // NOT IMPLEMENTED } GEN field_params(GEN field) { diff --git a/src/math/order.c b/src/math/order.c new file mode 100644 index 0000000..5e15279 --- /dev/null +++ b/src/math/order.c @@ -0,0 +1,21 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#include "order.h" + +int order_init(curve_t *curve, config_t *cfg, ...) { + curve->order = ellff_get_card(curve->curve); + return 1; +} + +int order_prime(curve_t *curve, config_t *cfg, ...) { + pari_sp ltop = avma; + curve->order = ellsea(curve->curve, 1); + if (gequal0(curve->order) || !(isprime(curve->order))) { + avma = ltop; + return -4; + } else { + return 1; + } +}
\ No newline at end of file diff --git a/src/math/order.h b/src/math/order.h new file mode 100644 index 0000000..4af994a --- /dev/null +++ b/src/math/order.h @@ -0,0 +1,28 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#ifndef ECGEN_ORDER_H +#define ECGEN_ORDER_H + +#include "types.h" + +/** + * + * @param curve + * @param cfg + * @param ... + * @return + */ +int order_init(curve_t *curve, config_t *cfg, ...); + +/** + * + * @param curve + * @param cfg + * @param ... + * @return + */ +int order_prime(curve_t *curve, config_t *cfg, ...); + +#endif //ECGEN_ORDER_H diff --git a/src/math/types.c b/src/math/types.c new file mode 100644 index 0000000..958de7f --- /dev/null +++ b/src/math/types.c @@ -0,0 +1,7 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#include "types.h" + +int gen_skip(curve_t *curve, config_t *config, ...) { return 1; } diff --git a/src/types.h b/src/math/types.h index dba2bc7..8185078 100644 --- a/src/types.h +++ b/src/math/types.h @@ -26,6 +26,19 @@ typedef struct curve { size_t npoints; } curve_t; +enum curve_offset { + OFFSET_SEED, + OFFSET_FIELD, + OFFSET_A, + OFFSET_B, + OFFSET_CURVE, + OFFSET_ORDER, + OFFSET_POINTS, + OFFSET_END +}; + typedef int (*gen_t)(curve_t *, config_t *, ...); +int gen_skip(curve_t *curve, config_t *config, ...); + #endif // ECGEN_TYPES_H |
