ECDH 秘钥交换




void handleErrors()

{

    printf("Error occurred.\n");

}



void disp(constchar *str, constvoid *pbuf, constint size)

{

    int i=0;

    if(str !=NULL){

        printf("%s:\n", str);

    }

    if(pbuf !=NULL && size > 0){

        for(i=0;i

            printf("%02x ", *((unsignedchar *)pbuf+i));

        putchar('\n');

    }

    putchar('\n');

}


EC_KEY *genECDHpubkey(unsignedchar *pubkey)

{

    int len;

    EC_KEY *ecdh =EC_KEY_new();

    

    //Generate Public

    ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1

    EC_KEY_generate_key(ecdh);

    constEC_POINT *point = EC_KEY_get0_public_key(ecdh);

    constEC_GROUP *group = EC_KEY_get0_group(ecdh);

    

    //unsigned char *pubkey = malloc(ECDH_SIZE);

    if(0 == (len =EC_POINT_point2oct(group, point,POINT_CONVERSION_COMPRESSED, pubkey,ECDH_SIZE, NULL))) handleErrors();

    printf("len=%d\n",len);

    

    //return pubkey;

    return ecdh;

}


unsigned char *genECDHsharedsecret(EC_KEY *ecdh,unsigned char *peerkey,size_t secret_len)

{

    int len;

    unsignedchar *shared = (unsignedchar *)malloc(ECDH_SIZE);

    constEC_GROUP *group = EC_KEY_get0_group(ecdh);

    

    //ComputeKey

    EC_POINT *point_peer =EC_POINT_new(group);

    EC_POINT_oct2point(group, point_peer, peerkey,ECDH_SIZE, NULL);

    

    //if (0 != EC_POINT_cmp(group, point2, point2c, NULL)) handleErrors();

    if(0 == (len =ECDH_compute_key(shared, secret_len, point_peer, ecdh,NULL))) handleErrors();

    printf("len=%d\n",len);

    disp("shared", shared, secret_len);

    

    return shared;

}



int testECDH() {

    unsignedchar *keydata = (unsignedchar *)malloc(ECDH_SIZE);

    unsignedchar *keydata2 = (unsignedchar *)malloc(ECDH_SIZE);

    

    EC_KEY *ecdh =genECDHpubkey(keydata);

    EC_KEY *ecdh2 =genECDHpubkey(keydata2);

    

    unsignedchar *ECDH_keydata = genECDHsharedsecret(ecdh2, keydata,ECDH_SIZE-1);

    unsignedchar *ECDH_keydata2 = genECDHsharedsecret(ecdh, keydata2,ECDH_SIZE-1);

    

    printf("To the end\n");

    

    free(keydata);

    free(keydata2);

    

    EC_KEY_free(ecdh);

    EC_KEY_free(ecdh2);

    

    free(ECDH_keydata);

    free(ECDH_keydata2);

    

    return0;

}



你可能感兴趣的:(openssl,安全,DH,ecdh)