Hi all i develped this program, but when i run it i receive Segmentation Fault, pls help me this this software is my project for exame :(
I attach source this is error from gdb: EC_write_point (point=0x804c998, group=0x804c4b8, ctx=0x804c318, str=0x804971d "Punto condiviso P (x,y): ", out=0x804c008) at myecdh.c:22 22 BIGNUM *x = NULL, *y = NULL; (gdb) 24 x = BN_new(); (gdb) 25 y = BN_new(); (gdb) 27 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) { (gdb) 33 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err; (gdb) 48 ERR_print_errors_fp(stderr); (gdb) 17565:error:100B7065:lib(16):func(183):reason(101):ec_lib.c:904: 50 }
#include "myecdh.h" #include <unistd.h> static const char rnd_seed[] = "21o4h32rfon4d3ornou53gnwqpegbnng"; main() { int pid; //pid_t pid; int nid = NID_sect571r1; BIO *out; out = BIO_new(BIO_s_file()); FILE* fp; if((fp = fopen("ECDH-keys", "w")) == NULL) { printf("Error in fopen!\n"); return 0; } if (out == NULL) exit(1); BIO_set_fp(out, fp, BIO_NOCLOSE); RAND_seed(rnd_seed, sizeof rnd_seed); EC_POINT *P = NULL; ECDH_PUB_KEY *dad_pubkey = (ECDH_PUB_KEY *)malloc(sizeof(ECDH_PUB_KEY)); ECDH_PUB_KEY *child_pubkey = (ECDH_PUB_KEY *)malloc(sizeof(ECDH_PUB_KEY)); ECDH_PRIV_KEY *dad_privkey = (ECDH_PRIV_KEY *)malloc(sizeof(ECDH_PRIV_KEY)); ECDH_PRIV_KEY *child_privkey = (ECDH_PRIV_KEY *)malloc(sizeof(ECDH_PRIV_KEY)); ECDH_SHARED_KEY *dad_shared_key = (ECDH_SHARED_KEY *)malloc(sizeof(ECDH_SHARED_KEY)); ECDH_SHARED_KEY *child_shared_key = (ECDH_SHARED_KEY *)malloc(sizeof(ECDH_SHARED_KEY)); BN_CTX *ctx = NULL; ctx = BN_CTX_new(); const EC_GROUP *group = NULL; /* * * Inizializzazione del punto condiviso P, del gruppo generatore group * e generazione delle chiavi pubbliche e private per il processo padre * */ ECDH_key_gen(nid,&P,&dad_pubkey,&dad_privkey,&group,ctx); /* * * Inizializzazione del punto condiviso P, del gruppo generatore group * e generazione delle chiavi pubbliche e private per il processo figlio * */ ECDH_key_gen(nid, &P, &child_pubkey, &child_privkey, &group, ctx); EC_write_point(P, group, ctx, "Punto condiviso P (x,y): ", out); pid = fork(); if(pid == 0) { /* Processo figlio. * Se fork() ritorna 0, siamo all'interno * del processo figlio */ ECDH_shared_key_gen(&child_shared_key, dad_pubkey, child_privkey, group, ctx); BIO_printf(out, "**** Child's KEYS ****\n"); BIO_printf(out, "Child's privKEY: "); BN_print(out, child_privkey->a); BIO_printf(out, "\n"); EC_write_point(child_pubkey->Q, group, ctx, "Child's pubKEY = [child privKEY]P (x,y): ", out); EC_write_point(child_shared_key->shared_key, group, ctx, "SharedKEY computed by Child = [child_privKEY]dad_pubKEY: ", out); fclose(fp); ERR_print_errors_fp(stderr); if (ctx) BN_CTX_free(ctx); BIO_free(out); CRYPTO_cleanup_all_ex_data(); ERR_remove_state(0); CRYPTO_mem_leaks_fp(stderr); _exit(0); } else if(pid > 0) { /* Processo padre, la fork ha restituito * il pid del figlio. */ wait(); ECDH_shared_key_gen(&dad_shared_key, child_pubkey, dad_privkey, group, ctx); BIO_printf(out, "**** Dad's KEYS ****\n"); BIO_printf(out, "Dad's privKEY: "); BN_print(out, dad_privkey->a); BIO_printf(out, "\n"); EC_write_point(dad_pubkey->Q, group, ctx, "Dad's pubKEY = [dad privKEY]P (x,y): ", out); EC_write_point(dad_shared_key->shared_key, group, ctx, "SharedKEY computed by Dad = [dad_privKEY]child_privKEY: ", out); } else { /* Errore. */ fprintf(stderr, "Errore nel fork"); exit(1); } }
#include "myecdh.h" #include "e_os.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/opensslconf.h> /* for OPENSSL_NO_ECDH */ #include <openssl/crypto.h> #include <openssl/bio.h> #include <openssl/bn.h> #include <openssl/objects.h> #include <openssl/rand.h> #include <openssl/sha.h> #include <openssl/err.h> #include <openssl/ec.h> #include <openssl/ecdh.h> void EC_write_point(EC_POINT *point, const EC_GROUP *group, BN_CTX *ctx, const char *str, BIO *out){ BIGNUM *x = NULL, *y = NULL; x = BN_new(); y = BN_new(); if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) { if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err; }else { if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err; } if (x) BN_free(x); if (y) BN_free(y); BIO_printf(out, str); BN_print(out, x); BIO_printf(out, ","); BN_print(out, y); BIO_printf(out, "\n"); err: ERR_print_errors_fp(stderr); } int ECDH_shared_key_gen(ECDH_SHARED_KEY **shared_key, ECDH_PUB_KEY *her_pub_hey, ECDH_PRIV_KEY *priv_key, const EC_GROUP *group, BN_CTX *ctx) { int ret = 0; BIGNUM *x = NULL, *y = NULL; x = BN_new(); y = BN_new(); (*shared_key)->shared_key = EC_POINT_new(group); EC_POINT_mul(group, (*shared_key)->shared_key, NULL, her_pub_hey->Q, priv_key->a, ctx); if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) { if (!EC_POINT_get_affine_coordinates_GFp(group, (*shared_key)->shared_key, x, y, ctx)) goto err; }else { if (!EC_POINT_get_affine_coordinates_GF2m(group, (*shared_key)->shared_key, x, y, ctx)) goto err; } if (x) BN_free(x); if (y) BN_free(y); ret = 1; err: ERR_print_errors_fp(stderr); return ret; } int ECDH_key_gen(int nid, EC_POINT **P, ECDH_PUB_KEY **pub_key, ECDH_PRIV_KEY **priv_key, const EC_GROUP **group, BN_CTX *ctx){ EC_KEY *ec = NULL; int ret = 0; (*priv_key)->a = BN_new(); ec = EC_KEY_new_by_curve_name(nid); if (ec == NULL) goto err; *group = EC_KEY_get0_group(ec); if (!EC_KEY_generate_key(ec)) goto err; if( *P == NULL ) { *P = EC_POINT_new(*group); EC_POINT_copy(*P, EC_KEY_get0_public_key(ec)); } BN_copy((*priv_key)->a, EC_KEY_get0_private_key(ec)); (*pub_key)->Q = EC_POINT_new(*group); EC_POINT_mul(*group, (*pub_key)->Q, NULL, *P, (*priv_key)->a, ctx); if (ec) EC_KEY_free(ec); ret = 1; err: ERR_print_errors_fp(stderr); return ret; }
/* * File: myecdh.h * Author: palban * * Created on 3 settembre 2008, 12.04 */ #include <stdio.h> #include <stdlib.h> #include <string.h> //#include "e_os.h" #include <openssl/opensslconf.h> /* for OPENSSL_NO_ECDH */ #include <openssl/crypto.h> #include <openssl/bio.h> #include <openssl/bn.h> #include <openssl/objects.h> #include <openssl/rand.h> #include <openssl/sha.h> #include <openssl/err.h> #include <openssl/ec.h> #include <openssl/ecdh.h> #ifndef _MYECDH_H #define _MYECDH_H #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif #endif /* _MYECDH_H */ /* * Definizioni delle funzioni e strutture dati per Cramer Shoup */ /* definizioni dei tipi di dati per le * chiavi pubbliche, private e cifrato. */ typedef struct ecdh_pub_key ECDH_PUB_KEY; typedef struct ecdh_priv_key ECDH_PRIV_KEY; typedef struct ecdh_shared_key ECDH_SHARED_KEY; /* Chiave pubblica * * * */ struct ecdh_pub_key{ EC_POINT *Q; }; /* Chiave privata */ struct ecdh_priv_key{ BIGNUM *a; }; /* Chiave condivisa */ struct ecdh_shared_key{ EC_POINT *shared_key; }; void EC_write_point(EC_POINT *point, const EC_GROUP *group, BN_CTX *ctx, const char *str, BIO *out); int ECDH_shared_key_gen(ECDH_SHARED_KEY **shared_key, ECDH_PUB_KEY *her_pub_hey, ECDH_PRIV_KEY *priv_key, const EC_GROUP *group, BN_CTX *ctx); /* metodo di generazione della chiave pubblica e privata * * inserisce nelle strutture passate come parametri la * gli elementi che costituiranno i parametri della * chiave pubblica e privata */ int ECDH_key_gen(int nid, EC_POINT **P, ECDH_PUB_KEY **pub_key, ECDH_PRIV_KEY **priv_key, const EC_GROUP **group, BN_CTX *ctx);