aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJ08nY2017-08-31 21:13:00 +0200
committerJ08nY2017-08-31 21:13:50 +0200
commit390046e05d5f14f07c6adbe879b51c48bbe27733 (patch)
tree8095406d7c4dfbcfbeb7812890fbe67e9037c85b /src
parent98672b5bc2c2897e372892d16124612cd0b1c96a (diff)
downloadecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.tar.gz
ecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.tar.zst
ecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.zip
Diffstat (limited to 'src')
-rw-r--r--src/gen/types.h10
-rw-r--r--src/util/binascii.c43
-rw-r--r--src/util/binascii.h34
3 files changed, 85 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