aboutsummaryrefslogtreecommitdiff
path: root/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-10-27 09:25:53 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-10-27 09:25:53 +0200
commit25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c (patch)
tree079ea63fcc874506072a91b13d2612b510cf158e /libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c
parent9eaf47d5dfa56ca79ae903aabfc2cf52bdfb981e (diff)
downloadcmix-25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c.tar.gz
cmix-25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c.tar.bz2
cmix-25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c.zip
Adds libgcrypt implementation for elgamal in multiplicative group.
Also adapts the API to both handle sodium and gcrypt libraries.
Diffstat (limited to 'libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c')
-rw-r--r--libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c b/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c
new file mode 100644
index 0000000..113fb95
--- /dev/null
+++ b/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c
@@ -0,0 +1,137 @@
+
+#include "api.h"
+
+#include "gcrypt.h"
+
+#include <stddef.h>
+#include <stdbool.h>
+
+static gcry_mpi_t p;
+static gcry_mpi_t q;
+static gcry_mpi_t g;
+
+void check(gcry_error_t error) {
+ if (error) {
+ fprintf (stderr, "Error: %s/%s\n", gcry_strsource (error), gcry_strerror (error));
+ exit(1);
+ }
+}
+
+void print_sexp(gcry_sexp_t exp) {
+ size_t required_size = gcry_sexp_sprint(exp, GCRYSEXP_FMT_ADVANCED, NULL, 0); //passing NULL as the buffer will return the size needed.
+
+ char* str = (char *) malloc(sizeof(char)*required_size);
+
+ gcry_sexp_sprint(exp, GCRYSEXP_FMT_ADVANCED, str, 2000); //So we ignore the size here.
+
+ printf("size = %zu\n", required_size);
+ for(size_t i = 0; i < required_size; i++)
+ printf("%c", str[i]);
+ printf("\n");
+
+ free(str);
+}
+
+void elgamal_initialize(void) {
+ if (!gcry_check_version (GCRYPT_VERSION)) {
+ fprintf(stderr, "libgcrypt version mismatch\n");
+ exit(-1);
+ }
+
+ gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
+ gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
+ gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
+ gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
+
+ //leading as specified by libgcrypt
+ char p_hex[] = "0087A8E61DB4B6663CFFBBD19C651959998CEEF608660DD0F25D2CEED4435E3B00E00DF8F1D61957D4FAF7DF4561B2AA3016C3D91134096FAA3BF4296D830E9A7C209E0C6497517ABD5A8A9D306BCF67ED91F9E6725B4758C022E0B1EF4275BF7B6C5BFC11D45F9088B941F54EB1E59BB8BC39A0BF12307F5C4FDB70C581B23F76B63ACAE1CAA6B7902D52526735488A0EF13C6D9A51BFA4AB3AD8347796524D8EF6A167B5A41825D967E144E5140564251CCACB83E6B486F6B3CA3F7971506026C0B857F689962856DED4010ABD0BE621C3A3960A54E710C375F26375D7014103A4B54330C198AF126116D2276E11715F693877FAD7EF09CADB094AE91E1A1597";
+ char q_hex[] = "008CF83642A709A097B447997640129DA299B1A47D1EB3750BA308B0FE64F5FBD3";
+ char g_hex[] = "003FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0BA12510DBC15077BE463FFF4FED4AAC0BB555BE3A6C1B0C6B47B1BC3773BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A57F2DDF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428EBC831D14348F6F2F9193B5045AF2767164E1DFC967C1FB3F2E55A4BD1BFFE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E052588B9B7D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92B52C7891428CDC67EB6184B523D1DB246C32F63078490F00EF8D647D148D47954515E2327CFEF98C582664B4C0F6CC41659";
+
+ size_t nr_bytes_scanned;
+ gcry_error_t error;
+
+ error = gcry_mpi_scan(&p, GCRYMPI_FMT_HEX, p_hex, 0, &nr_bytes_scanned);
+ check(error);
+
+ error = gcry_mpi_scan(&q, GCRYMPI_FMT_HEX, q_hex, 0, &nr_bytes_scanned);
+ check(error);
+
+ error = gcry_mpi_scan(&g, GCRYMPI_FMT_HEX, g_hex, 0, &nr_bytes_scanned);
+ check(error);
+}
+
+struct KeyPair elgamal_create_keypair() {
+
+ size_t parse_error_offset;
+ gcry_error_t error;
+
+ void* bytes = gcry_random_bytes_secure(2048/8, GCRY_VERY_STRONG_RANDOM);
+ gcry_mpi_t x = gcry_mpi_snew(2048);
+ error = gcry_mpi_scan(&x, GCRYMPI_FMT_USG, bytes, 2048/8, &parse_error_offset);
+ check(error);
+
+ gcry_mpi_t y = gcry_mpi_new(0);
+ gcry_mpi_powm(y, g, x, p);
+
+ gcry_sexp_t priv_key;
+ error = gcry_sexp_build(&priv_key, &parse_error_offset, "(private-key (elg (p %M) (g %M) (y %M) (x %M)))", p, g, y, x);
+ check(error);
+
+ error = gcry_pk_testkey(priv_key);
+ check(error);
+
+ return (struct KeyPair){
+ x,
+ y,
+ };
+}
+
+void elgamal_keypair_deleter(struct KeyPair* p) {
+ gcry_mpi_release((gcry_mpi_t) p->sec);
+ gcry_mpi_release((gcry_mpi_t) p->pub);
+ p->sec = NULL;
+ p->pub = NULL;
+}
+
+void elgamal_get_key_array(unsigned char** buffer, size_t* len, void* pubkey) {
+ gcry_mpi_t mpi = (gcry_mpi_t) pubkey;
+
+ gcry_error_t error;
+
+ error = gcry_mpi_aprint(GCRYMPI_FMT_USG, buffer, len, mpi);
+ check(error);
+}
+
+void elgamal_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey) {
+
+ gcry_error_t error;
+
+ gcry_mpi_t y = (gcry_mpi_t) pubkey;
+
+ gcry_mpi_t mpi_share;
+ size_t parse_error_pos;
+ error = gcry_mpi_scan(&mpi_share, GCRYMPI_FMT_USG, share, in_len, &parse_error_pos);
+ check(error);
+
+ gcry_mpi_t result = gcry_mpi_new(0);
+ gcry_mpi_mulm(result, mpi_share, y, p);
+
+ error = gcry_mpi_aprint(GCRYMPI_FMT_USG, (unsigned char**) buffer, out_len, result);
+ check(error);
+}
+
+void elgamal_shared_key_deleter(struct SharedKey* s) {
+ gcry_mpi_release((gcry_mpi_t) s->shared);
+ s->shared = NULL;
+}
+
+struct SharedKey elgamal_derive_shared_key(struct KeyPair pair, void const* pub_key, bool swap_pub_order) {
+
+
+ return (struct SharedKey){
+ NULL,
+ };
+}
+
+void elgamal_deinitialize(void) {}