diff options
| author | J08nY | 2017-08-31 21:13:00 +0200 |
|---|---|---|
| committer | J08nY | 2017-08-31 21:13:50 +0200 |
| commit | 390046e05d5f14f07c6adbe879b51c48bbe27733 (patch) | |
| tree | 8095406d7c4dfbcfbeb7812890fbe67e9037c85b | |
| parent | 98672b5bc2c2897e372892d16124612cd0b1c96a (diff) | |
| download | ecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.tar.gz ecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.tar.zst ecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.zip | |
| -rw-r--r-- | src/gen/types.h | 10 | ||||
| -rw-r--r-- | src/util/binascii.c | 43 | ||||
| -rw-r--r-- | src/util/binascii.h | 34 | ||||
| -rw-r--r-- | test/src/util/test_binascii.c | 54 |
4 files changed, 139 insertions, 2 deletions
diff --git a/src/gen/types.h b/src/gen/types.h index 5500e85..da03aeb 100644 --- a/src/gen/types.h +++ b/src/gen/types.h @@ -16,9 +16,10 @@ * @brief */ typedef struct seed_t { - char *raw; - size_t raw_len; GEN seed; + char *hex; + size_t hex_len; + unsigned char *hash; } seed_t; /** @@ -84,6 +85,11 @@ typedef struct { } arg_t; /** + * @brief + */ +typedef enum { ENDIAN_BIG = 0, ENDIAN_LITTLE } endian_e; + +/** * @brief A generator function type. * @param curve A curve_t being generated * @param cfg An application config diff --git a/src/util/binascii.c b/src/util/binascii.c new file mode 100644 index 0000000..ae3f1ff --- /dev/null +++ b/src/util/binascii.c @@ -0,0 +1,43 @@ +/* + * 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; +} + +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); + char *result = try_malloc((size_t)blen); + for (long j = 1; j <= blen; ++j) { + result[j - 1] = (char)gel(digits, j); + } + + avma = ltop; + return result; +} + +GEN binascii_btoi(const 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 new file mode 100644 index 0000000..3826a6a --- /dev/null +++ b/src/util/binascii.h @@ -0,0 +1,34 @@ +/* + * 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 + */ +char *binascii_itob(GEN i, endian_e endianity); + +/** + * @brief + * @param bytes + * @param endianity + * @return + */ +GEN binascii_btoi(const char *bytes, size_t len, endian_e endianity); + +#endif // ECGEN_BINASCII_H diff --git a/test/src/util/test_binascii.c b/test/src/util/test_binascii.c new file mode 100644 index 0000000..47f3f61 --- /dev/null +++ b/test/src/util/test_binascii.c @@ -0,0 +1,54 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + +#include <criterion/criterion.h> +#include "test/default.h" +#include "util/binascii.h" +#include "util/memory.h" + +TestSuite(binascii, .init = default_setup, .fini = default_teardown); + +Test(binascii, test_itob_big) { + GEN i = stoi((5 << 8) | 4); + char *bytes = binascii_itob(i, ENDIAN_BIG); + size_t len = binascii_blen(i); + cr_assert_eq(len, 2, ); + cr_assert_eq(bytes[0], 5, ); + cr_assert_eq(bytes[1], 4, ); + try_free(bytes); +} + +Test(binascii, test_itob_little) { + GEN i = stoi((5 << 8) | 4); + char *bytes = binascii_itob(i, ENDIAN_LITTLE); + size_t len = binascii_blen(i); + cr_assert_eq(len, 2, ); + cr_assert_eq(bytes[0], 4, ); + cr_assert_eq(bytes[1], 5, ); + try_free(bytes); +} + +Test(binascii, test_btoi_big) { + char bytes[] = {5, 4}; + GEN i = binascii_btoi(bytes, 2, ENDIAN_BIG); + GEN expected = stoi((5 << 8) | 4); + cr_assert(equalii(expected, i), ); +} + +Test(binascii, test_btoi_little) { + char bytes[] = {5, 4}; + GEN i = binascii_btoi(bytes, 2, ENDIAN_LITTLE); + GEN expected = stoi((4 << 8) | 5); + cr_assert(equalii(expected, i), ); +} + +Test(binascii, test_both) { + GEN expected = stoi((5 << 8) | 4); + char *bytes = binascii_itob(expected, ENDIAN_BIG); + size_t len = binascii_blen(expected); + GEN i = binascii_btoi(bytes, len, ENDIAN_BIG); + cr_assert(equalii(expected, i), ); + try_free(bytes); +}
\ No newline at end of file |
