aboutsummaryrefslogtreecommitdiff
path: root/libcmix-crypto/ed25519
diff options
context:
space:
mode:
Diffstat (limited to 'libcmix-crypto/ed25519')
-rw-r--r--libcmix-crypto/ed25519/gcrypt/gcrypt_ed25519.c112
1 files changed, 55 insertions, 57 deletions
diff --git a/libcmix-crypto/ed25519/gcrypt/gcrypt_ed25519.c b/libcmix-crypto/ed25519/gcrypt/gcrypt_ed25519.c
index 54202be..4839ce5 100644
--- a/libcmix-crypto/ed25519/gcrypt/gcrypt_ed25519.c
+++ b/libcmix-crypto/ed25519/gcrypt/gcrypt_ed25519.c
@@ -68,6 +68,9 @@ void DEF(initialize)(void) {
g = gcry_mpi_ec_get_point("g", ctx, false);
p = gcry_mpi_ec_get_mpi("p", ctx, false);
+
+ TRACE(__FILE__, __LINE__, g)
+ TRACE(__FILE__, __LINE__, p)
}
struct KeyPair DEF(create_keypair)(void) {
@@ -97,6 +100,9 @@ struct KeyPair DEF(create_keypair)(void) {
gcry_sexp_release(key_gen_sexp);
gcry_sexp_release(key_pair);
+ TRACE(__FILE__, __LINE__, d)
+ TRACE(__FILE__, __LINE__, q)
+
return (struct KeyPair) {
.sec = d,
.pub = q
@@ -125,6 +131,8 @@ void DEF(element_to_array)(unsigned char** buffer, size_t* len, GroupElement ele
*len = nr_bytes * 2;
*buffer = (unsigned char*) calloc(*len, sizeof(unsigned char));
+ TRACE(__FILE__, __LINE__, buffer)
+
unsigned char* temp = (unsigned char*) calloc(nr_bytes, sizeof(unsigned char));
size_t nwritten = 0;
@@ -146,17 +154,13 @@ GroupElement DEF(message_to_element)(char const* buffer, size_t len, bool secure
(void) secure;
gcry_error_t error;
- assert(len == 31);
+ //assert(len == 31);
gcry_mpi_t mpi;
gcry_mpi_scan(&mpi, GCRYMPI_FMT_USG, buffer, len, NULL);
//mpi * stride
- gcry_mpi_dump(mpi);
- printf("\n");
gcry_mpi_lshift(mpi, mpi, (unsigned) lround(log2(stride)));
- gcry_mpi_dump(mpi);
- printf("\n");
char* encoded_point = (char*) calloc(nr_bytes+1, sizeof(char));
gcry_mpi_point_t point = gcry_mpi_point_new(0);
@@ -165,7 +169,6 @@ GroupElement DEF(message_to_element)(char const* buffer, size_t len, bool secure
encoded_point[0] = 0x40;
unsigned int byte_size = (unsigned) ceil(gcry_mpi_get_nbits(mpi) / 8.0);
- printf("byte_size: %i\n", byte_size);
//gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*) encoded_point + 1 + (32 - byte_size), byte_size, NULL, mpi);
gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*) encoded_point + 1, 32, NULL, mpi);
@@ -175,35 +178,14 @@ GroupElement DEF(message_to_element)(char const* buffer, size_t len, bool secure
encoded_point[32-i] = temp;
}
- for(int i = 0; i < nr_bytes+1; i++) {
- printf("%02x", 0xFF & (unsigned) encoded_point[i]);
- }
- printf("\n");
-
gcry_mpi_t encoded_mpi = gcry_mpi_new(0);
gcry_mpi_set_opaque_copy(encoded_mpi, encoded_point, (nr_bytes + 1) * sizeof(char) * 8);
error = gcry_mpi_ec_decode_point(point, encoded_mpi, ctx);
check(error);
- gcry_mpi_t x = gcry_mpi_new(0);
- gcry_mpi_t y = gcry_mpi_new(0);
- gcry_mpi_ec_get_affine(x, y, point, ctx);
-
gcry_mpi_release(encoded_mpi);
- printf("%i %i\n", count, stride);
- gcry_mpi_dump(x);
- printf(",\n");
- gcry_mpi_dump(y);
- printf("\n");
- gcry_mpi_dump(mpi);
- printf("\n");
- gcry_mpi_dump(encoded_mpi);
- printf("\n");
- gcry_mpi_dump(p);
- printf("\n");
- assert(gcry_mpi_cmp(mpi, p) == -1);
assert(count < stride);
gcry_mpi_add_ui(mpi, mpi, 1);
count++;
@@ -218,23 +200,31 @@ GroupElement DEF(message_to_element)(char const* buffer, size_t len, bool secure
gcry_mpi_release(mpi);
free(encoded_point);
+ TRACE(__FILE__, __LINE__, point)
+
return point;
}
+size_t DEF(get_message_size)(void);
+
void DEF(element_to_message)(unsigned char** buffer, const GroupElement el) {
gcry_mpi_t y = gcry_mpi_new(0);
gcry_mpi_ec_get_affine(NULL, y, (gcry_mpi_point_t) el, ctx);
gcry_mpi_rshift(y, y, (unsigned) lround(log2(stride)));
- size_t nr_written;
- gcry_mpi_aprint(GCRYMPI_FMT_USG, buffer, &nr_written, y);
+ size_t mes_len = DEF(get_message_size)();
+ *buffer = (unsigned char*) calloc(mes_len, sizeof(unsigned char));
- gcry_mpi_dump(y);
- printf("\n");
+ TRACE(__FILE__, __LINE__, buffer)
- assert(nr_written == 31);
+ unsigned char* buffer2;
+ size_t nr_written;
+ gcry_mpi_aprint(GCRYMPI_FMT_USG, &buffer2, &nr_written, y);
+ memcpy(*buffer, buffer2, mes_len);
+
+ gcry_free(buffer2);
gcry_mpi_release(y);
}
@@ -247,13 +237,6 @@ GroupElement DEF(array_to_element)(char const* buffer, size_t len, bool secure)
size_t error_pos;
gcry_error_t error;
- printf("%i\n", len);
- for(int i = 0; i < len; i++) {
- if(i == 32) { printf("\n"); }
- printf("%02x", 0xFF & buffer[i]);
- }
- printf("\n");
-
gcry_mpi_t x;
error = gcry_mpi_scan(&x, GCRYMPI_FMT_USG, buffer, len/2, &error_pos);
check(error);
@@ -261,15 +244,14 @@ GroupElement DEF(array_to_element)(char const* buffer, size_t len, bool secure)
gcry_mpi_t y;
error = gcry_mpi_scan(&y, GCRYMPI_FMT_USG, buffer + len/2, len/2, &error_pos);
- gcry_mpi_dump(y);
- printf("\n");
-
gcry_mpi_point_t point = gcry_mpi_point_new(nr_point_bits);
gcry_mpi_point_set(point, x, y, GCRYMPI_CONST_ONE);
gcry_mpi_release(x);
gcry_mpi_release(y);
+ TRACE(__FILE__, __LINE__, point)
+
return point;
}
@@ -293,16 +275,7 @@ void DEF(get_pub_key_hash)(char** buffer, size_t* len, GroupElement const pub) {
gcry_mpi_t x = gcry_mpi_new(0);
gcry_mpi_t y = gcry_mpi_new(0);
- gcry_mpi_point_get(x, y, NULL, (gcry_mpi_point_t)pub);
-
- printf("pub: %p\n", pub);
-
- fprintf(stderr, "lel1: ");
- gcry_mpi_dump(x);
- printf("\n");
- fprintf(stderr, "lel2: ");
- gcry_mpi_dump(y);
- printf("\n");
+ gcry_mpi_ec_get_affine(x, y, (gcry_mpi_point_t)pub, ctx);
*len = DEF(get_pub_key_hash_length)();
*buffer = (char*) calloc(*len, sizeof(char));
@@ -319,7 +292,9 @@ void DEF(get_pub_key_hash)(char** buffer, size_t* len, GroupElement const pub) {
gcry_free(temp);
TRACE(__FILE__, __LINE__, *buffer);
- //gcry_sexp_release(pubkey);
+
+ gcry_mpi_release(x);
+ gcry_mpi_release(y);
}
GroupElement DEF(get_group_element)(bool secure) {
@@ -335,10 +310,12 @@ GroupElement DEF(get_group_element)(bool secure) {
bytes = gcry_random_bytes_secure(nr_bytes, GCRY_VERY_STRONG_RANDOM);
TRACE(__FILE__, __LINE__, bytes);
error = gcry_mpi_scan(&a, GCRYMPI_FMT_USG, bytes, nr_bytes, &parse_error_offset);
+ TRACE(__FILE__,__LINE__, a);
check(error);
} while(gcry_mpi_cmp_ui(a, 0) == 0 || gcry_mpi_cmp(a, p) != -1);
char* encoded_point = (char*) calloc(nr_bytes+1, sizeof(char));
+ TRACE(__FILE__, __LINE__, encoded_point)
gcry_mpi_point_t point = gcry_mpi_point_new(0);
do {
encoded_point[0] = 0x40;
@@ -353,6 +330,7 @@ GroupElement DEF(get_group_element)(bool secure) {
}
gcry_mpi_t encoded_mpi = gcry_mpi_new(0);
+ TRACE(__FILE__, __LINE__, encoded_mpi)
gcry_mpi_set_opaque_copy(encoded_mpi, encoded_point, (nr_bytes + 1) * sizeof(char) * 8);
error = gcry_mpi_ec_decode_point(point, encoded_mpi, ctx);
@@ -371,6 +349,9 @@ GroupElement DEF(get_group_element)(bool secure) {
gcry_mpi_release(a);
gcry_free(bytes);
+ free(encoded_point);
+
+ TRACE(__FILE__, __LINE__, point);
return point;
}
@@ -384,6 +365,8 @@ GroupElement DEF(get_key_exchange_value)(GroupElement group_el) {
gcry_mpi_release(x);
+ TRACE(__FILE__, __LINE__, mult)
+
return mult;
}
@@ -391,6 +374,9 @@ GroupElement DEF(combine)(GroupElement lh, GroupElement rh, bool secure) {
(void) secure;
gcry_mpi_point_t addition = gcry_mpi_point_new(nr_point_bits);
gcry_mpi_ec_add(addition, (gcry_mpi_point_t)lh, (gcry_mpi_point_t)rh, ctx);
+
+ TRACE(__FILE__, __LINE__, addition)
+
return addition;
}
@@ -409,6 +395,8 @@ GroupElement DEF(invert)(GroupElement const el) {
gcry_mpi_release(x);
gcry_mpi_release(y);
+ TRACE(__FILE__, __LINE__, point)
+
return point;
}
@@ -419,11 +407,12 @@ void DEF(delete_group_element)(GroupElement element) {
GroupElement DEF(uncombine)(GroupElement lh, GroupElement rh, bool secure) {
(void) secure;
- gcry_mpi_point_t inv_rh = DEF(invert)(rh);
- gcry_mpi_point_t ret = DEF(combine)(lh, inv_rh, secure);
+ GroupElement inv_rh = DEF(invert)((gcry_mpi_point_t)rh);
+ GroupElement ret = DEF(combine)(lh, inv_rh, secure);
+ DEF(delete_group_element)(inv_rh);
+
+ TRACE(__FILE__, __LINE__, ret)
- //gcry_mpi_point_t ret = gcry_mpi_point_new(0);
- //gcry_mpi_ec_sub(ret, (gcry_mpi_point_t)lh, (gcry_mpi_point_t)rh, ctx);
return ret;
}
@@ -434,6 +423,7 @@ GroupElement DEF(get_decryption_share)(GroupElement r, GroupElement e) {
GroupElement d = DEF(invert)(inv_d);
gcry_mpi_point_release(inv_d);
+ TRACE(__FILE__, __LINE__, d)
return d;
}
@@ -448,6 +438,7 @@ size_t DEF(get_message_size)(void) {
void DEF(add_public_share)(GroupElement* el, char const* share, size_t in_len, GroupElement pubkey) {
GroupElement share_el = DEF(array_to_element)(share, in_len, false);
*el = DEF(combine)(share_el, pubkey, false);
+ TRACE(__FILE__, __LINE__, *el);
gcry_mpi_point_release((gcry_mpi_point_t)share_el);
}
@@ -464,6 +455,8 @@ GroupElement DEF(derive_shared_key)(struct KeyPair keypair, unsigned char const*
gcry_mpi_point_release((gcry_mpi_point_t) ga);
+ TRACE(__FILE__, __LINE__, gab);
+
return gab;
}
@@ -489,6 +482,11 @@ void DEF(encrypt)(GroupElement* random_element, GroupElement* message_element, G
gcry_mpi_release(random_mpi);
gcry_mpi_point_release(c2);
+ gcry_mpi_point_release(random);
+
+ TRACE(__FILE__, __LINE__, *random_element);
+ TRACE(__FILE__, __LINE__, *message_element);
+
}
unsigned int DEF(get_uniform_int)(unsigned int upper) {