Re: openssl - DH_compute_key
Citation:
Envoyé par Tex-Twil
Si vous voulez le code complete dites moi, il n'est pas tres long.
Je ne connais pas du tout openssl, mais je pense que tout le code (et commenté) serait utile pour t'aider et explique ce que tu obtiens et ce que tu devrais obtenir...
Jc
Re: openssl - DH_compute_key
Citation:
Envoyé par fearyourself
Citation:
Envoyé par Tex-Twil
Si vous voulez le code complete dites moi, il n'est pas tres long.
Je ne connais pas du tout openssl, mais je pense que tout le code (et commenté) serait utile pour t'aider et explique ce que tu obtiens et ce que tu devrais obtenir...
Jc
ok.
Je dois faire un client et un serveur qui s'echangent des cles de cryptage selon Diffie Hellman Key Agreemen pour un future cryptage des donnees. Sans parler du reseau, j ai d'abord essaye faire une mini appli qui simiule l'echange et le calcul des cles privees et publiques.
man (pas tres explicites ! ) sur BN et DH
http://www.openssl.org/docs/crypto/bn.html#
http://www.openssl.org/docs/crypto/dh.html#
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <openssl/dh.h>
#include <openssl/engine.h>
#define PRIME_LEN 64
#define GENERATOR 5
int main()
{
void printDH(char *name, DH *dh);
DH *dhAlice;
DH *dhBob;
// Final secret shared keys. should be equal :)
unsigned char *keyAlice;
unsigned char *keyBob;
int i;
int ret;
int codes = -1;
srand( (unsigned)time( NULL ) );
dhAlice = DH_new();
dhBob = DH_new();
printf("Generating DH params ... this can take a while \n");
dhAlice = DH_generate_parameters(PRIME_LEN, GENERATOR, NULL, NULL);
// Check the return value !
DH_check(dhAlice, &codes);
if( codes == DH_UNABLE_TO_CHECK_GENERATOR)
printf("DH_UNABLE_TO_CHECK_GENERATOR");
printf("Generating public & private keys ... \n");
DH_generate_key(dhAlice);
printDH("Alice", dhAlice);
//Giving p & g parameters to Bob. This exchange will be done
//through network sockets, dont know yet how.
dhBob->p = BN_new();
dhBob->g = BN_new();
BN_copy(dhBob->p, dhAlice->p);
BN_copy(dhBob->g, dhAlice->g);
// Calculing Bob's key with shared p & g parames. Check the ret value !
DH_generate_key(dhBob);
printDH("Bob", dhBob);
// Computing secret shared keys with other peers's public key
//Alice
keyAlice = malloc(DH_size(dhAlice));
DH_compute_key(keyAlice, dhBob->pub_key, dhAlice);
printf("Alice calculated: %s\n", keyAlice);
free(keyAlice);
//Bob
keyBob = malloc(DH_size(dhBob));
DH_compute_key(keyBob, dhAlice->pub_key, dhBob);
printf("Bob calculated: %s\n", keyBob);
free(keyBob);
DH_free(dhAlice);
DH_free(dhBob);
// system("openssl dhparam 64 -out dhparams.pem 2>/dev/null");
return EXIT_SUCCESS;
}
void printDH(char *name, DH *dh)
{
//char *BN_bn2dec(const BIGNUM *a);
printf("--------------\n");
printf("%s :\n", name);
printf("%i bits\n", PRIME_LEN);
printf("p: %s \n", BN_bn2hex(dh->p));
printf("g: %s \n", BN_bn2hex(dh->g));
printf("priv_key: %s \n", BN_bn2hex(dh->priv_key));
printf("pub_key: %s \n", BN_bn2hex(dh->pub_key));
printf("\n");
} |