aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2018-02-27 16:53:41 +0100
committerJ08nY2018-02-27 16:53:41 +0100
commit054de4be5dd329fb4b7c694bc8a8274f748fd8c7 (patch)
tree32c846dbb80e07ffa932d44fd5064df61d96a865
parent76b8866222b9abd5f25a795ff719914e852e14ab (diff)
parente9d7343868ecca9eb144e2d4c237e6cd1570a327 (diff)
downloadecgen-054de4be5dd329fb4b7c694bc8a8274f748fd8c7.tar.gz
ecgen-054de4be5dd329fb4b7c694bc8a8274f748fd8c7.tar.zst
ecgen-054de4be5dd329fb4b7c694bc8a8274f748fd8c7.zip
-rw-r--r--Makefile1
-rw-r--r--README.md7
-rw-r--r--docs/output.md44
-rw-r--r--lib/parson/parson.c194
-rw-r--r--lib/parson/parson.h2
-rw-r--r--src/Makefile10
-rw-r--r--src/econvert.c29
-rw-r--r--src/exhaustive/brainpool.c10
-rw-r--r--src/exhaustive/brainpool_rfc.c8
-rw-r--r--src/io/cli.c13
-rw-r--r--src/io/output.c117
-rw-r--r--src/io/output.h25
-rw-r--r--src/misc/config.h2
-rw-r--r--test/Makefile7
-rw-r--r--test/data/econvert_format.csv1
-rw-r--r--test/data/econvert_format.json29
-rw-r--r--test/data/f2m_10_a.csv1
-rw-r--r--test/data/fp_10_a.csv1
-rwxr-xr-xtest/ecgen.sh16
-rwxr-xr-xtest/econvert.sh20
20 files changed, 142 insertions, 395 deletions
diff --git a/Makefile b/Makefile
index cbe5125..3f5d88a 100644
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,6 @@ help:
@echo "Available targets:"
@echo " - all : builds all"
@echo " - ecgen : builds the main binary"
- @echo " - econvert : build the format conversion binary"
@echo " - docs : generate doxygen docs"
@echo " - test : test the main binary"
@echo " - unittest : "
diff --git a/README.md b/README.md
index b067550..a94bb9b 100644
--- a/README.md
+++ b/README.md
@@ -32,7 +32,6 @@ Tool for generating Elliptic curve domain parameters.
#### IO options
-- `-t / --format=FORMAT` Format to output in. One of [csv,json], default is json.
- `-f / --input=FILE` Input from `FILE`.
- `-o / --output=FILE` Output into `FILE`. Overwrites any existing file!
- `-a / --append` Append to output file (don't overwrite).
@@ -52,9 +51,9 @@ Generate a prime field, uniquely generated, prime order, 192-bit curve, don't as
> ecgen --fp -u -p -r 192
-Generate 5 random, binary field, 163-bit koblitz curves:
+Generate 5 random, binary field, 163-bit curves:
- > ecgen --f2m -r -K -c5 163
+ > ecgen --f2m -r -c5 163
Generate invalid curves to a file, for a given prime field 192-bit curve:
@@ -115,7 +114,7 @@ Three different EC curve parameters generation methods are implemented.
- given input
- Can generate curves repeatedly until one satisfies requested properties:
- `-p / --prime` generates curves until a prime order curve is found.
- - `-K / --koblitz` generates a curve with fixed *A = 0* parameter.
+ - `-K / --koblitz` generates a Koblitz curve.
- `-u / --unique` generates a uniquely generated curve (with one generator/cyclic group).
- etc..
diff --git a/docs/output.md b/docs/output.md
index 26b30db..ec18419 100644
--- a/docs/output.md
+++ b/docs/output.md
@@ -1,6 +1,6 @@
# Output
-There are two output formats currently supported in ecgen, JSON and CSV. These are enabled by using the `-tjson / --format=json`, `-tcsv / --format=csv` options, respectively.
+There are only output format currently supported in ecgen is JSON.
## JSON
@@ -102,45 +102,3 @@ Self-explanatory format. The curve dictionaries are enclosed in an array as you
}
]
}]
-
-
-## CSV
-
-This format is useful mostly for input and is not uniquely parseable for most curves, thus is deprecated. It is uniquely parseable for the set of common cryptographic domain parameters.
-
-#### Notation
-
- - `p` - prime F_p
- - `m` - binary field exponent F_2^m
- - `e1` - largest exponent of the field polynomial
- - `e2` - middle exponenet of the field polynomial, or `0000` if field poly is a trinomial
- - `e3` - smallest exponent (except zero) of the field polynomial, or `0000` if field poly is a trinomial
- - `a` - a parameter in short Weierstrass curve equation
- - `b` - b parameter in short Weierstrass curve equation
- - `gx` - x coordinate of the curve base-point g
- - `gy` - y coordinate of the curve base-point g
- - `n` - the base-point order
- - `h` - the base-point cofactor
- - `px` - the x coordinate of the point
- - `py` - the y coordinate of the point
- - `op` - the order of the point
-
-#### Prime field
-
- p, a, b, gx, gy, n, h
-
-More generaly:
-
- p, a, b, gx, gy(, gx2, gy2)?, n, (px, py, op)*, h
-
-#### Binary field
-
- m, e1, e2, e3, a, b, gx, gy, n, h
-
-More generaly:
-
- m, e1, e2, e3, a, b, gx, gy(, gx2, gy2)?, n, (px, py, op)*, h
-
-#### Example:
-
- 0x9bbd697be66af329,0x49ec7aa186e501f7,0x88e93b218fff3b55,0x9bbd697c54a8e780,0x206c5696b67702bb,0x328f7cb39358ae23,0x26ef5a5f152a39e0,0x4,0x1af56d479450c41b,0x01902f3d0c1ae199,0x9bbd697c54a8e78,0x10,0x21fdf0cd456005f6,0x0000000000000000,0x2,0x682f77abb827b2fa,0x8b7adcd4b4bbdb7f,0x685,0x94bc802fc965c386,0x212c07693dbabc19,0x2fc6c2a43943 \ No newline at end of file
diff --git a/lib/parson/parson.c b/lib/parson/parson.c
index b914ae8..583524b 100644
--- a/lib/parson/parson.c
+++ b/lib/parson/parson.c
@@ -1,6 +1,6 @@
/*
Parson ( http://kgabis.github.com/parson/ )
- Copyright (c) 2012 - 2016 Krzysztof Gabis
+ Copyright (c) 2012 - 2017 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -35,15 +35,17 @@
#include <math.h>
#include <errno.h>
-#define STARTING_CAPACITY 15
-#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */
-#define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */
-#define MAX_NESTING 19
-#define DOUBLE_SERIALIZATION_FORMAT "%f"
+/* Apparently sscanf is not implemented in some "standard" libraries, so don't use it, if you
+ * don't have to. */
+#define sscanf THINK_TWICE_ABOUT_USING_SSCANF
+
+#define STARTING_CAPACITY 16
+#define MAX_NESTING 2048
+#define FLOAT_FORMAT "%1.17g"
#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
#define SKIP_CHAR(str) ((*str)++)
-#define SKIP_WHITESPACES(str) while (isspace(**str)) { SKIP_CHAR(str); }
+#define SKIP_WHITESPACES(str) while (isspace((unsigned char)(**str))) { SKIP_CHAR(str); }
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#undef malloc
@@ -90,7 +92,8 @@ static char * read_file(const char *filename);
static void remove_comments(char *string, const char *start_token, const char *end_token);
static char * parson_strndup(const char *string, size_t n);
static char * parson_strdup(const char *string);
-static int is_utf16_hex(const unsigned char *string);
+static int hex_char_to_int(char c);
+static int parse_utf16_hex(const char *string, unsigned int *result);
static int num_bytes_in_utf8_sequence(unsigned char c);
static int verify_utf8_sequence(const unsigned char *string, int *len);
static int is_valid_utf8(const char *string, size_t string_len);
@@ -114,7 +117,7 @@ static JSON_Value * json_value_init_string_no_copy(char *string);
/* Parser */
static JSON_Status skip_quotes(const char **string);
-static int parse_utf_16(const char **unprocessed, char **processed);
+static int parse_utf16(const char **unprocessed, char **processed);
static char * process_string(const char *input, size_t len);
static char * get_quoted_string(const char **string);
static JSON_Value * parse_object_value(const char **string, size_t nesting);
@@ -146,8 +149,31 @@ static char * parson_strdup(const char *string) {
return parson_strndup(string, strlen(string));
}
-static int is_utf16_hex(const unsigned char *s) {
- return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]);
+static int hex_char_to_int(char c) {
+ if (c >= '0' && c <= '9') {
+ return c - '0';
+ } else if (c >= 'a' && c <= 'f') {
+ return c - 'a' + 10;
+ } else if (c >= 'A' && c <= 'F') {
+ return c - 'A' + 10;
+ }
+ return -1;
+}
+
+static int parse_utf16_hex(const char *s, unsigned int *result) {
+ int x1, x2, x3, x4;
+ if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0' || s[3] == '\0') {
+ return 0;
+ }
+ x1 = hex_char_to_int(s[0]);
+ x2 = hex_char_to_int(s[1]);
+ x3 = hex_char_to_int(s[2]);
+ x4 = hex_char_to_int(s[3]);
+ if (x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) {
+ return 0;
+ }
+ *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4);
+ return 1;
}
static int num_bytes_in_utf8_sequence(unsigned char c) {
@@ -236,7 +262,8 @@ static int is_decimal(const char *string, size_t length) {
static char * read_file(const char * filename) {
FILE *fp = fopen(filename, "r");
- size_t file_size;
+ size_t size_to_read = 0;
+ size_t size_read = 0;
long pos;
char *file_contents;
if (!fp) {
@@ -248,22 +275,21 @@ static char * read_file(const char * filename) {
fclose(fp);
return NULL;
}
- file_size = pos;
+ size_to_read = pos;
rewind(fp);
- file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1));
+ file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1));
if (!file_contents) {
fclose(fp);
return NULL;
}
- if (fread(file_contents, file_size, 1, fp) < 1) {
- if (ferror(fp)) {
- fclose(fp);
- parson_free(file_contents);
- return NULL;
- }
+ size_read = fread(file_contents, 1, size_to_read, fp);
+ if (size_read == 0 || ferror(fp)) {
+ fclose(fp);
+ parson_free(file_contents);
+ return NULL;
}
fclose(fp);
- file_contents[file_size] = '\0';
+ file_contents[size_read] = '\0';
return file_contents;
}
@@ -326,9 +352,6 @@ static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_V
}
if (object->count >= object->capacity) {
size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
- if (new_capacity > OBJECT_MAX_CAPACITY) {
- return JSONFailure;
- }
if (json_object_resize(object, new_capacity) == JSONFailure) {
return JSONFailure;
}
@@ -389,9 +412,10 @@ static JSON_Value * json_object_nget_value(const JSON_Object *object, const char
}
static void json_object_free(JSON_Object *object) {
- while(object->count--) {
- parson_free(object->names[object->count]);
- json_value_free(object->values[object->count]);
+ size_t i;
+ for (i = 0; i < object->count; i++) {
+ parson_free(object->names[i]);
+ json_value_free(object->values[i]);
}
parson_free(object->names);
parson_free(object->values);
@@ -414,9 +438,6 @@ static JSON_Array * json_array_init(JSON_Value *wrapping_value) {
static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
if (array->count >= array->capacity) {
size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
- if (new_capacity > ARRAY_MAX_CAPACITY) {
- return JSONFailure;
- }
if (json_array_resize(array, new_capacity) == JSONFailure) {
return JSONFailure;
}
@@ -446,8 +467,9 @@ static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) {
}
static void json_array_free(JSON_Array *array) {
- while (array->count--) {
- json_value_free(array->items[array->count]);
+ size_t i;
+ for (i = 0; i < array->count; i++) {
+ json_value_free(array->items[i]);
}
parson_free(array->items);
parson_free(array);
@@ -486,37 +508,43 @@ static JSON_Status skip_quotes(const char **string) {
return JSONSuccess;
}
-static int parse_utf_16(const char **unprocessed, char **processed) {
+static int parse_utf16(const char **unprocessed, char **processed) {
unsigned int cp, lead, trail;
+ int parse_succeeded = 0;
char *processed_ptr = *processed;
const char *unprocessed_ptr = *unprocessed;
unprocessed_ptr++; /* skips u */
- if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) {
+ parse_succeeded = parse_utf16_hex(unprocessed_ptr, &cp);
+ if (!parse_succeeded) {
return JSONFailure;
}
if (cp < 0x80) {
- *processed_ptr = cp; /* 0xxxxxxx */
+ processed_ptr[0] = (char)cp; /* 0xxxxxxx */
} else if (cp < 0x800) {
- *processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
- *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
+ processed_ptr[1] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr += 1;
} else if (cp < 0xD800 || cp > 0xDFFF) {
- *processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
- *processed_ptr++ = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
- *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
+ processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr[2] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
+ processed_ptr += 2;
} else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
lead = cp;
unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
- if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */
- !is_utf16_hex((const unsigned char*)unprocessed_ptr) ||
- sscanf(unprocessed_ptr, "%4x", &trail) == EOF ||
- trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
- return JSONFailure;
+ if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u') {
+ return JSONFailure;
}
- cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000;
- *processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
- *processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
- *processed_ptr++ = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
- *processed_ptr = (((cp ) & 0x3F) | 0x80); /* 10xxxxxx */
+ parse_succeeded = parse_utf16_hex(unprocessed_ptr, &trail);
+ if (!parse_succeeded || trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
+ return JSONFailure;
+ }
+ cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) + 0x010000;
+ processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
+ processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
+ processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
+ processed_ptr[3] = (((cp) & 0x3F) | 0x80); /* 10xxxxxx */
+ processed_ptr += 3;
} else { /* trail surrogate before lead surrogate */
return JSONFailure;
}
@@ -533,9 +561,12 @@ static char* process_string(const char *input, size_t len) {
const char *input_ptr = input;
size_t initial_size = (len + 1) * sizeof(char);
size_t final_size = 0;
- char *output = (char*)parson_malloc(initial_size);
- char *output_ptr = output;
- char *resized_output = NULL;
+ char *output = NULL, *output_ptr = NULL, *resized_output = NULL;
+ output = (char*)parson_malloc(initial_size);
+ if (output == NULL) {
+ goto error;
+ }
+ output_ptr = output;
while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) {
if (*input_ptr == '\\') {
input_ptr++;
@@ -549,7 +580,7 @@ static char* process_string(const char *input, size_t len) {
case 'r': *output_ptr = '\r'; break;
case 't': *output_ptr = '\t'; break;
case 'u':
- if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) {
+ if (parse_utf16(&input_ptr, &output_ptr) == JSONFailure) {
goto error;
}
break;
@@ -633,8 +664,13 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) {
}
while (**string != '\0') {
new_key = get_quoted_string(string);
+ if (new_key == NULL) {
+ json_value_free(output_value);
+ return NULL;
+ }
SKIP_WHITESPACES(string);
- if (new_key == NULL || **string != ':') {
+ if (**string != ':') {
+ parson_free(new_key);
json_value_free(output_value);
return NULL;
}
@@ -645,9 +681,9 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) {
json_value_free(output_value);
return NULL;
}
- if(json_object_add(output_object, new_key, new_value) == JSONFailure) {
+ if (json_object_add(output_object, new_key, new_value) == JSONFailure) {
parson_free(new_key);
- parson_free(new_value);
+ json_value_free(new_value);
json_value_free(output_value);
return NULL;
}
@@ -683,12 +719,12 @@ static JSON_Value * parse_array_value(const char **string, size_t nesting) {
}
while (**string != '\0') {
new_array_value = parse_value(string, nesting);
- if (!new_array_value) {
+ if (new_array_value == NULL) {
json_value_free(output_value);
return NULL;
}
if (json_array_add(output_array, new_array_value) == JSONFailure) {
- parson_free(new_array_value);
+ json_value_free(new_array_value);
json_value_free(output_value);
return NULL;
}
@@ -885,13 +921,7 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le
if (buf != NULL) {
num_buf = buf;
}
- if (num == ((double)(int)num)) { /* check if num is integer */
- written = sprintf(num_buf, "%d", (int)num);
- } else if (num == ((double)(unsigned int)num)) {
- written = sprintf(num_buf, "%u", (unsigned int)num);
- } else {
- written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num);
- }
+ written = sprintf(num_buf, FLOAT_FORMAT, num);
if (written < 0) {
return -1;
}
@@ -1271,8 +1301,12 @@ JSON_Value * json_value_init_string(const char *string) {
}
JSON_Value * json_value_init_number(double number) {
- JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
- if (!new_value) {
+ JSON_Value *new_value = NULL;
+ if ((number * 0.0) != 0.0) { /* nan and inf test */
+ return NULL;
+ }
+ new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
+ if (new_value == NULL) {
return NULL;
}
new_value->parent = NULL;
@@ -1407,7 +1441,7 @@ JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename
if (serialized_string == NULL) {
return JSONFailure;
}
- fp = fopen (filename, "w");
+ fp = fopen(filename, "w");
if (fp == NULL) {
json_free_serialized_string(serialized_string);
return JSONFailure;
@@ -1467,7 +1501,7 @@ JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *f
if (serialized_string == NULL) {
return JSONFailure;
}
- fp = fopen (filename, "w");
+ fp = fopen(filename, "w");
if (fp == NULL) {
json_free_serialized_string(serialized_string);
return JSONFailure;
@@ -1506,20 +1540,13 @@ void json_free_serialized_string(char *string) {
}
JSON_Status json_array_remove(JSON_Array *array, size_t ix) {
- JSON_Value *temp_value = NULL;
- size_t last_element_ix = 0;
+ size_t to_move_bytes = 0;
if (array == NULL || ix >= json_array_get_count(array)) {
return JSONFailure;
}
- last_element_ix = json_array_get_count(array) - 1;
json_value_free(json_array_get_value(array, ix));
- if (ix != last_element_ix) { /* Replace value with one from the end of array */
- temp_value = json_array_get_value(array, last_element_ix);
- if (temp_value == NULL) {
- return JSONFailure;
- }
- array->items[ix] = temp_value;
- }
+ to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value*);
+ memmove(array->items + ix, array->items + ix + 1, to_move_bytes);
array->count -= 1;
return JSONSuccess;
}
@@ -1691,7 +1718,7 @@ JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON
char *current_name = NULL;
JSON_Object *temp_obj = NULL;
JSON_Value *new_value = NULL;
- if (value == NULL || name == NULL || value == NULL) {
+ if (object == NULL || name == NULL || value == NULL) {
return JSONFailure;
}
dot_pos = strchr(name, '.');
@@ -1796,11 +1823,10 @@ JSON_Status json_object_dotremove(JSON_Object *object, const char *name) {
} else {
current_name = parson_strndup(name, dot_pos - name);
temp_obj = json_object_get_object(object, current_name);
+ parson_free(current_name);
if (temp_obj == NULL) {
- parson_free(current_name);
return JSONFailure;
}
- parson_free(current_name);
return json_object_dotremove(temp_obj, dot_pos + 1);
}
}
@@ -1878,7 +1904,7 @@ JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) {
}
}
-JSON_Status json_value_equals(const JSON_Value *a, const JSON_Value *b) {
+int json_value_equals(const JSON_Value *a, const JSON_Value *b) {
JSON_Object *a_object = NULL, *b_object = NULL;
JSON_Array *a_array = NULL, *b_array = NULL;
const char *a_string = NULL, *b_string = NULL;
diff --git a/lib/parson/parson.h b/lib/parson/parson.h
index e4cd21e..6438c93 100644
--- a/lib/parson/parson.h
+++ b/lib/parson/parson.h
@@ -1,6 +1,6 @@
/*
Parson ( http://kgabis.github.com/parson/ )
- Copyright (c) 2012 - 2016 Krzysztof Gabis
+ Copyright (c) 2012 - 2017 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/src/Makefile b/src/Makefile
index 65b0135..06d3642 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -27,24 +27,17 @@ LIBS = -lrt -lpari -lpthread -lparson -lsha1
ECGEN_SRC = ecgen.c $(wildcard */*.c)
ECGEN_OBJ = $(patsubst %.c,%.o, $(ECGEN_SRC))
-ECONVERT_SRC = econvert.c
-ECONVERT_OBJ = $(patsubst %.c,%.o, $(ECONVERT_SRC))
-
SRC = $(wildcard *.c) $(wildcard */*.c)
HDR = $(wildcard */*.h)
####
-all: ecgen econvert
+all: ecgen
ecgen: ecgen.o $(ECGEN_OBJ)
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(LDFLAGS) $(LIBS)
mv ecgen ..
-econvert: econvert.o $(ECONVERT_OBJ)
- $(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(LDFLAGS) $(LIBS)
- mv econvert ..
-
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
@@ -52,7 +45,6 @@ econvert: econvert.o $(ECONVERT_OBJ)
clean-all: clean clean-cov
rm -f ../ecgen
- rm -f ../econvert
clean:
find . -type f -name '*.o' -exec rm {} +
diff --git a/src/econvert.c b/src/econvert.c
deleted file mode 100644
index ae1e143..0000000
--- a/src/econvert.c
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * ecgen, tool for generating Elliptic curve domain parameters
- * Copyright (C) 2017-2018 J08nY
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
- * USA.
- */
-#include <stdio.h>
-
-/**
- * @file econvert.c
- * @author J08nY <johny@neuromancer.sk>
- * @version 0.6.3
- * @copyright GPL v2.0
- */
-
-int main(void) { fprintf(stderr, "This is not implemented *yet*.\n"); }
diff --git a/src/exhaustive/brainpool.c b/src/exhaustive/brainpool.c
index 30558eb..17c2c65 100644
--- a/src/exhaustive/brainpool.c
+++ b/src/exhaustive/brainpool.c
@@ -169,7 +169,7 @@ GENERATOR(brainpool_gen_equation) {
avma = btop;
continue;
}
- //seed->brainpool.seed_a = bits_copy(seed->seed);
+ // seed->brainpool.seed_a = bits_copy(seed->seed);
brainpool_update_seed(seed->seed);
@@ -179,11 +179,11 @@ GENERATOR(brainpool_gen_equation) {
bits_free(&b_bits);
if (!Fp_issquare(b, curve->field)) {
brainpool_update_seed(seed->seed);
- //bits_free(&seed->brainpool.seed_a);
+ // bits_free(&seed->brainpool.seed_a);
avma = btop;
continue;
}
- //seed->brainpool.seed_b = bits_copy(seed->seed);
+ // seed->brainpool.seed_b = bits_copy(seed->seed);
GEN mod_a = gmodulo(a, curve->field);
GEN mod_b = gmodulo(b, curve->field);
@@ -191,8 +191,8 @@ GENERATOR(brainpool_gen_equation) {
if (gequal0(gmulsg(-16, gadd(gmulsg(4, gpowgs(mod_a, 3)),
gmulsg(27, gsqr(mod_b)))))) {
brainpool_update_seed(seed->seed);
- //bits_free(&seed->brainpool.seed_a);
- //bits_free(&seed->brainpool.seed_b);
+ // bits_free(&seed->brainpool.seed_a);
+ // bits_free(&seed->brainpool.seed_b);
avma = btop;
continue;
}
diff --git a/src/exhaustive/brainpool_rfc.c b/src/exhaustive/brainpool_rfc.c
index 0c0eed6..4e41dec 100644
--- a/src/exhaustive/brainpool_rfc.c
+++ b/src/exhaustive/brainpool_rfc.c
@@ -57,7 +57,7 @@ GENERATOR(brainpool_rfc_gen_equation) {
avma = btop;
continue;
}
- //seed->brainpool.seed_a = bits_copy(seed->seed);
+ // seed->brainpool.seed_a = bits_copy(seed->seed);
GEN b = NULL;
pari_sp bbtop = avma;
@@ -72,7 +72,7 @@ GENERATOR(brainpool_rfc_gen_equation) {
bits_free(&b_bits);
} while (Fp_issquare(b, curve->field));
- //seed->brainpool.seed_b = bits_copy(seed->seed);
+ // seed->brainpool.seed_b = bits_copy(seed->seed);
GEN mod_a = gmodulo(a, curve->field);
GEN mod_b = gmodulo(b, curve->field);
@@ -80,8 +80,8 @@ GENERATOR(brainpool_rfc_gen_equation) {
if (gequal0(gmulsg(-16, gadd(gmulsg(4, gpowgs(mod_a, 3)),
gmulsg(27, gsqr(mod_b)))))) {
brainpool_update_seed(seed->seed);
- //bits_free(&seed->brainpool.seed_a);
- //bits_free(&seed->brainpool.seed_b);
+ // bits_free(&seed->brainpool.seed_a);
+ // bits_free(&seed->brainpool.seed_b);
avma = btop;
continue;
}
diff --git a/src/io/cli.c b/src/io/cli.c
index 2ea14f8..c192021 100644
--- a/src/io/cli.c
+++ b/src/io/cli.c
@@ -72,7 +72,6 @@ struct argp_option cli_options[] = {
{"count", OPT_COUNT, "COUNT", 0, "Generate multiple curves.", 3},
{0, 0, 0, 0, "Input/Output options:", 4},
- {"format", OPT_FORMAT, "FORMAT", 0, "Format to output in. One of {csv, json}, default is json.", 4},
{"input", OPT_INPUT, "FILE", 0, "Input from file.", 4},
{"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!", 4},
{"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite).", 4},
@@ -198,6 +197,7 @@ static void cli_end(struct argp_state *state) {
if (!cfg->points.type) {
cfg->points.type = POINTS_PRIME;
}
+ cfg->format = FORMAT_JSON;
}
error_t cli_parse(int key, char *arg, struct argp_state *state) {
@@ -335,17 +335,6 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
break;
}
/* IO options */
- case OPT_FORMAT:
- if (!strcmp(arg, "csv")) {
- cfg->format = FORMAT_CSV;
- } else if (!strcmp(arg, "json")) {
- cfg->format = FORMAT_JSON;
- } else {
- argp_failure(state, 1, 0,
- "Invalid format specified. One of [csv, json] "
- "is valid.");
- }
- break;
case OPT_INPUT:
cfg->input = arg;
break;
diff --git a/src/io/output.c b/src/io/output.c
index df3d6a4..4d8fca0 100644
--- a/src/io/output.c
+++ b/src/io/output.c
@@ -19,105 +19,6 @@ char *output_malloc(const char *what) {
return s;
}
-char *output_scsv(curve_t *curve) {
- pari_sp ltop = avma;
- char *params[OFFSET_END] = {NULL};
-
- switch (cfg->field) {
- case FIELD_PRIME:
- params[OFFSET_FIELD] =
- pari_sprintf("%P0#*x", cfg->hex_digits, curve->field);
- break;
- case FIELD_BINARY: {
- GEN field = field_params(curve->field);
- params[OFFSET_FIELD] =
- pari_sprintf("%P#x,%P#x,%P#x,%P#x", gel(field, 1),
- gel(field, 2), gel(field, 3), gel(field, 4));
- break;
- }
- }
-
- if (curve->a)
- params[OFFSET_A] =
- pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->a));
- if (curve->b)
- params[OFFSET_B] =
- pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->b));
-
- if (curve->generators) {
- char *gens[curve->ngens];
- size_t len = 0;
- for (size_t i = 0; i < curve->ngens; ++i) {
- point_t *generator = curve->generators[i];
- GEN x = field_elementi(gel(generator->point, 1));
- GEN y = field_elementi(gel(generator->point, 2));
- gens[i] = pari_sprintf("%P0#*x,%P0#*x,%P#x,%P#x", cfg->hex_digits,
- x, cfg->hex_digits, y, generator->order,
- generator->cofactor);
- len += strlen(gens[i]);
- }
- size_t lenn = sizeof(char) * (len + curve->ngens);
- params[OFFSET_GENERATORS] = pari_calloc(lenn);
- for (size_t i = 0; i < curve->ngens; ++i) {
- if (i > 0) strncat(params[OFFSET_GENERATORS], ",", lenn - 1);
- strncat(params[OFFSET_GENERATORS], gens[i], lenn - 1);
- pari_free(gens[i]);
- }
- }
-
- if (curve->order)
- params[OFFSET_ORDER] =
- pari_sprintf("%P0#*x", cfg->hex_digits, curve->order);
-
- if (curve->points) {
- char *points[curve->npoints];
- size_t len = 0;
- for (size_t i = 0; i < curve->npoints; ++i) {
- point_t *point = curve->points[i];
- GEN x = field_elementi(gel(point->point, 1));
- GEN y = field_elementi(gel(point->point, 2));
- points[i] = pari_sprintf("%P0#*x,%P0#*x,%P#x", cfg->hex_digits, x,
- cfg->hex_digits, y, point->order);
- len += strlen(points[i]);
- }
- size_t lenn = sizeof(char) * (len + curve->npoints);
- params[OFFSET_POINTS] = pari_calloc(lenn);
- for (size_t i = 0; i < curve->npoints; ++i) {
- if (i > 0) strncat(params[OFFSET_POINTS], ",", lenn - 1);
- strncat(params[OFFSET_POINTS], points[i], lenn - 1);
- pari_free(points[i]);
- }
- }
-
- size_t len = 0;
- size_t count = 0;
- for (int i = OFFSET_FIELD; i < OFFSET_END; ++i) {
- if (params[i]) {
- len += strlen(params[i]);
- ++count;
- }
- }
- size_t lenn = sizeof(char) * (len + count);
- char *result = try_calloc(lenn);
-
- for (int i = OFFSET_FIELD; i < OFFSET_END; ++i) {
- if (params[i]) {
- if (i > OFFSET_FIELD) strncat(result, ",", lenn - 1);
- strncat(result, params[i], lenn - 1);
- pari_free(params[i]);
- }
- }
-
- avma = ltop;
- return result;
-}
-
-char *output_scsv_separator() { return output_malloc("\n"); }
-
-char *output_scsv_begin() { return NULL; }
-
-char *output_scsv_end() { return output_malloc("\n"); }
-
static JSON_Value *output_jjson(curve_t *curve) {
pari_sp ltop = avma;
// root object/value is curve
@@ -333,22 +234,22 @@ bool output_init() {
output_s_end = &output_sjson_end;
break;
}
- case FORMAT_CSV: {
- output_s = &output_scsv;
- output_s_separator = &output_scsv_separator;
- output_s_begin = &output_scsv_begin;
- output_s_end = &output_scsv_end;
- break;
- }
}
return true;
}
+static bool output_is_std(FILE *stream) {
+ return (stream == stdout || stream == stderr || stream == stdin);
+}
+
void output_quit(void) {
- if (out != NULL && out != stdout) {
+ if (!output_is_std(out)) {
fclose(out);
}
- if (err != NULL && err != stdout) {
+ if (!output_is_std(err)) {
fclose(err);
}
+ if (!output_is_std(verbose)) {
+ fclose(verbose); // My name is fClose Verbose!
+ }
}
diff --git a/src/io/output.h b/src/io/output.h
index 1037c00..6c048d9 100644
--- a/src/io/output.h
+++ b/src/io/output.h
@@ -78,31 +78,6 @@
#define output_log(...) pari_fprintf(out, __VA_ARGS__)
/**
- * @brief Output curve to a malloc'ed string in CSV format.
- * @param curve
- * @return
- */
-char *output_scsv(curve_t *curve);
-
-/**
- * @brief Output CSV separator(newline) to a malloc'ed string in CSV format.
- * @return
- */
-char *output_scsv_separator();
-
-/**
- * @brief Output CSV output header to a malloc'ed string in CSV format.
- * @return
- */
-char *output_scsv_begin();
-
-/**
- * @brief Output CSV output footer to a malloc'ed string in CSV format.
- * @return
- */
-char *output_scsv_end();
-
-/**
* @brief Output curve to a malloc'ed string in JSON format.
* @param curve
* @return
diff --git a/src/misc/config.h b/src/misc/config.h
index 367de52..9a8f8af 100644
--- a/src/misc/config.h
+++ b/src/misc/config.h
@@ -12,7 +12,7 @@
#include <stddef.h>
enum field_e { FIELD_PRIME = 1 << 0, FIELD_BINARY = 1 << 1 };
-enum format_e { FORMAT_JSON, FORMAT_CSV };
+enum format_e { FORMAT_JSON };
enum points_e {
POINTS_NONE = 0,
POINTS_PRIME,
diff --git a/test/Makefile b/test/Makefile
index 0823862..13bcd32 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -5,9 +5,9 @@
#
####
-test: ecgen econvert
+test: ecgen
-unittest: unit ecgen econvert
+unittest: unit ecgen
unit:
cd lib/criterion && mkdir -p build && cd build && cmake .. >/dev/null && cmake --build . >/dev/null
@@ -16,9 +16,6 @@ unit:
ecgen:
./ecgen.sh
-econvert:
- ./econvert.sh
-
clean:
+$(MAKE) -C src clean
diff --git a/test/data/econvert_format.csv b/test/data/econvert_format.csv
deleted file mode 100644
index 358d807..0000000
--- a/test/data/econvert_format.csv
+++ /dev/null
@@ -1 +0,0 @@
-0xb,0x1,0x2,0x2,0xa,0x8,0x2,0x7,0x0,0x2,0x8,0x10,0x7,0x0,0x2 \ No newline at end of file
diff --git a/test/data/econvert_format.json b/test/data/econvert_format.json
deleted file mode 100644
index 8bb9bc2..0000000
--- a/test/data/econvert_format.json
+++ /dev/null
@@ -1,29 +0,0 @@
-{
- "field": {
- "p": "0xb"
- },
- "a": "0x1",
- "b": "0x2",
- "order": "0x10",
- "generators": [
- {
- "x": "0x2",
- "y": "0xa",
- "order": "0x8",
- "cofactor": "0x2"
- },
- {
- "x": "0x7",
- "y": "0x0",
- "order": "0x2",
- "cofactor": "0x8"
- }
- ],
- "points": [
- {
- "x": "0x7",
- "y": "0x0",
- "order": "0x2"
- }
- ]
-}
diff --git a/test/data/f2m_10_a.csv b/test/data/f2m_10_a.csv
deleted file mode 100644
index e668f66..0000000
--- a/test/data/f2m_10_a.csv
+++ /dev/null
@@ -1 +0,0 @@
-0xa,0x3,0x0,0x0,0x02ed,0x00b7,0x03de,0x[0-9a-f]{4},0x[0-9a-f]{4},0x3de,0x1,0x0000,0x02d5,0x2,0x03f6,0x0(36b|09d),0x3,0x[0-9a-f]{4},0x[0-9a-f]{4},0x5,0x[0-9a-f]{4},0x[0-9a-f]{4},0xb
diff --git a/test/data/fp_10_a.csv b/test/data/fp_10_a.csv
deleted file mode 100644
index b9ec96c..0000000
--- a/test/data/fp_10_a.csv
+++ /dev/null
@@ -1 +0,0 @@
-0x000b,0x0001,0x0002,0x0010,0x[0-9a-f]{4},0x[0-9a-f]{4},0x8,0x2,0x[0-9a-f]{4},0x[0-9a-f]{4},0x[0-9a-f]{1,4},0x[0-9a-f]{1,4},0x[0-9a-f]{4},0x[0-9a-f]{4},0x2.*
diff --git a/test/ecgen.sh b/test/ecgen.sh
index 145c3a4..22ac164 100755
--- a/test/ecgen.sh
+++ b/test/ecgen.sh
@@ -15,18 +15,12 @@ function runs() {
assert_raises "${ecgen}" 64
}
-function csv() {
- start_test
- assert_matches "${ecgen} --fp -tcsv --input=data/fp_10_a.csv.in 10" "$(cat data/fp_10_a.csv)"
- assert_matches "${ecgen} --f2m -tcsv --input=data/f2m_10_a.csv.in 10" "$(cat data/f2m_10_a.csv)"
-}
-
function json() {
start_test
- assert_raises "${ecgen} --fp -tjson --input=data/fp_10_a.csv.in 10"
- assert_raises "${ecgen} --f2m -tjson --input=data/f2m_10_a.csv.in 10"
- fp=$(${ecgen} --fp -tjson --input=data/fp_10_a.csv.in 10 2>/dev/null)
- f2m=$(${ecgen} --f2m -tjson --input=data/f2m_10_a.csv.in 10 2>/dev/null)
+ assert_raises "${ecgen} --fp --input=data/fp_10_a.csv.in 10"
+ assert_raises "${ecgen} --f2m --input=data/f2m_10_a.csv.in 10"
+ fp=$(${ecgen} --fp --input=data/fp_10_a.csv.in 10 2>/dev/null)
+ f2m=$(${ecgen} --f2m --input=data/f2m_10_a.csv.in 10 2>/dev/null)
assert_raises "${JSON}" 0 "${fp}"
assert_matches "${JSON} -x field\\\",\\\"p" "0x000b" "${fp}"
assert_matches "${JSON} -x \\\"a\\\"" "0x0001" "${fp}"
@@ -116,7 +110,6 @@ function twist() {
function cli() {
start_test
assert_raises "${ecgen} --threads=a" 1
- assert_raises "${ecgen} --format=something" 1
assert_raises "${ecgen} --koblitz=2" 1
assert_raises "${ecgen} --points=something" 1
assert_raises "${ecgen} --seed=some" 64
@@ -137,7 +130,6 @@ function hex() {
. ${ASSERT} -v
start_suite
runs
-csv
json
exhaustive
ansix962
diff --git a/test/econvert.sh b/test/econvert.sh
deleted file mode 100755
index ed1943c..0000000
--- a/test/econvert.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/usr/bin/env bash
-####
-#
-# ecgen, tool for generating Elliptic curve domain parameters
-# Copyright (C) 2017 J08nY
-#
-####
-
-. ./common.sh
-
-function runs() {
- start_test
- assert_raises "${econvert} --help"
- assert_raises "${econvert} --version"
-}
-
-. ${ASSERT} -v
-start_suite
-runs
-end_suite econvert \ No newline at end of file