Bala Pitchandi wrote:

Yes, I do understand the DH exchange process. But with respect to the
OpenSSL DH Library usage, let's say I and another party have fixed p & g. I
calculate X using DH_generate_key() (I get a different X every time for the
same p & g, is that okay?). And later I receive the other party's Y (which
never changes by the way) and use it to calculate the secret key and is
different every time. So my comparison against their shared secret key
fails.

The function for calculating the shared secret is DH_compute_key().


Here's a starting code snippet, mostly lifted from the examples
for OpenSSL:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <openssl/crypto.h>
#include <openssl/bio.h>
#include <openssl/bn.h>

#include <openssl/dh.h>


int main(int argc, char *argv[]) { /* unsigned char apriv[] = { 0x00, 0x8d, 0x3a, 0x22, 0x0b, 0x78, 0x98, 0x51, 0x0b, 0xe2, 0x98, 0x77, 0xc0, 0xa8, 0x15, 0xf3, 0x91 }; /**/ /**/ unsigned char apriv[] = { 0x00, 0x23, 0xf9, 0x2c, 0xd5, 0x25, 0xb1, 0x78, 0xcb, 0x13, 0x57, 0x31, 0x9c, 0x1a, 0x53, 0x5c, 0xb4, 0x8d, 0x3a, 0x22, 0x0b, 0x78, 0x98, 0x51, 0x0b, 0xe2, 0x98, 0x77, 0xc0, 0xa8, 0x15, 0xf3, 0x91 }; /**/ unsigned char bpub[] = { 0x00, 0xd9, 0x64, 0xc9, 0xda, 0x12, 0x65, 0x5f, 0xf3, 0x07, 0x7a, 0x32, 0x13, 0x6f, 0xfc, 0x65, 0x66, 0x62, 0x0e, 0xaf, 0xef, 0xa2, 0x3e, 0x5e, 0x6d, 0xbf, 0xbe, 0x27, 0xfd, 0xc2, 0xc4, 0x4d, 0x74, 0x39, 0x7d, 0x36, 0x76, 0xe2, 0x71, 0xf3, 0x10, 0x38, 0x7d, 0x2c, 0x55, 0x12, 0x5b, 0x91, 0x49, 0x2f, 0xdf, 0xe3, 0x84, 0xbf, 0xfd, 0x15, 0x7c, 0xe8, 0x96, 0x3f, 0x0f, 0x4e, 0x7a, 0x42, 0x27, 0x96, 0xa8, 0x81, 0x16, 0x83, 0x7b, 0x53, 0xe5, 0x14, 0x29, 0x30, 0x34, 0x93, 0x6f, 0x4f, 0x9e, 0x49, 0xd3, 0x71, 0x9e, 0xde, 0xc6, 0x23, 0x6c, 0xc6, 0x3d, 0xcf, 0xed, 0x08, 0x98, 0x1f, 0xf4, 0x0b, 0xa7, 0xd9, 0xbe, 0x51, 0x38, 0x36, 0x9b, 0xb2, 0x7c, 0x92, 0x76, 0x97, 0xe2, 0x47, 0xb3, 0x7d, 0x55, 0x66, 0x12, 0x5b, 0x29, 0xf5, 0x75, 0x4c, 0x4d, 0x71, 0x4b, 0x26, 0x53, 0x54, 0xe7 };

    unsigned char primo[] = { 0x00,
        0xe1, 0x95, 0x37, 0xa2, 0xbf, 0xe3, 0x13, 0x9e,
        0x89, 0xf6, 0x4f, 0xf9, 0x26, 0x71, 0x03, 0x80,
        0x1b, 0x73, 0x7b, 0x8e, 0xe7, 0xe8, 0x7e, 0xc0,
        0xd1, 0x60, 0x10, 0x77, 0xf7, 0xf1, 0x26, 0x0c,
        0xef, 0x67, 0xc1, 0x00, 0x67, 0xd3, 0x8d, 0x84,
        0x2b, 0x23, 0x8b, 0x8b, 0xbb, 0x72, 0xd3, 0xfb,
        0x80, 0x57, 0x17, 0x2e, 0x3c, 0x5f, 0x1e, 0x28,
        0x4b, 0x87, 0x27, 0x6e, 0xe6, 0x87, 0x6f, 0x6a,
        0xb8, 0x45, 0x8d, 0x28, 0x3a, 0x0d, 0x88, 0xd1,
        0x1c, 0x74, 0xb3, 0xf8, 0x2c, 0xd2, 0x81, 0x60,
        0x7e, 0xc1, 0x77, 0x8b, 0x2d, 0xe0, 0x58, 0xc8,
        0x78, 0xe7, 0xaa, 0x81, 0x07, 0xc3, 0x32, 0xce,
        0xb4, 0x16, 0xaf, 0x74, 0xd7, 0xee, 0x95, 0xee,
        0xbf, 0x8d, 0xcb, 0xf0, 0xab, 0x3a, 0x10, 0xd1,
        0x3e, 0xb4, 0x61, 0xe5, 0x44, 0x8f, 0x9f, 0x81,
        0xae, 0xab, 0x6f, 0xb3, 0x54, 0xb7, 0x56, 0x8b };


DH *a; BIGNUM *bnbpub; char buf[256]; unsigned char *abuf=NULL; int i,alen,blen,aout,bout,ret=1; BIO *out;

    long long   time0, time1;
    struct timeval  tv;


out=BIO_new(BIO_s_file()); if (out == NULL) exit(1); BIO_set_fp(out,stdout,BIO_NOCLOSE);

    a=DH_new();
    a->p=BN_bin2bn(primo,sizeof(primo),NULL);
    a->g=BN_new();
    BN_set_word(a->g,2);
    a->priv_key = BN_bin2bn(apriv,sizeof(apriv),NULL);

bnbpub = BN_bin2bn(bpub, sizeof(bpub), NULL);

    BIO_puts(out,"\n\np    =\n");
    BN_print(out,a->p);
    BIO_puts(out,"\n\ng    = ");
    BN_print(out,a->g);
    BIO_puts(out,"\n\n");

    if (!DH_generate_key(a)) goto err;
    BIO_puts(out,"A's private key =\n");
    BN_print(out,a->priv_key);
    BIO_puts(out,"\n\nA's public key =\n");
    BN_print(out,a->pub_key);
    BIO_puts(out,"\n\nB's public key =\n");
    BN_print(out,bnbpub);
    BIO_puts(out,"\n\n");


alen=DH_size(a); abuf=(unsigned char *)OPENSSL_malloc(alen); aout=DH_compute_key(abuf,bnbpub,a);

    BIO_puts(out,"Kab =\n");
    for (i=0; i<aout; i++) {
        sprintf(buf,"%02X",abuf[i]);
        BIO_puts(out,buf);
    }
    BIO_puts(out,"\n\n");

    ret=0;
err:
    if (abuf != NULL) OPENSSL_free(abuf);
    if(a != NULL) DH_free(a);
    BIO_free(out);
    return(ret);
}


______________________________________________________________________ OpenSSL Project http://www.openssl.org Development Mailing List [EMAIL PROTECTED] Automated List Manager [EMAIL PROTECTED]

Reply via email to