Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what should I do when I want get share key in pkey DH? #116

Open
TanyaWong opened this issue Dec 18, 2017 · 10 comments
Open

what should I do when I want get share key in pkey DH? #116

TanyaWong opened this issue Dec 18, 2017 · 10 comments

Comments

@TanyaWong
Copy link

I create a new objection whice type is DH, and I got priv_key,pub_key,p, g, Now, I want to get a share key ,what show I do? thanks

@daurnimator
Copy link
Collaborator

Is this a request for a bn:mod_exp function so that you can efficiently compute a shared secret?

@TanyaWong
Copy link
Author

I want user my priv_key and Other people's pub_key to calculate shared secret ,It's not clear if it has anything to do with bn:mod_exp

@daurnimator
Copy link
Collaborator

daurnimator commented Dec 18, 2017

I'm not sure why you're trying to do this manually.
However, take a look at the general algorithm at https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange#Secrecy_chart
The shared secret is calculated with s = (B^a)%p --> this is a mod_exp operation (which you can do explicitly. but will be much faster with the BN_mod_exp function in PR #117)

@TanyaWong
Copy link
Author

TanyaWong commented Dec 19, 2017

I search openssl C source code, and I am puzzled by how to call function DH_compute_key in luaossl? thanks

@daurnimator
Copy link
Collaborator

@TanyaWong
Copy link
Author

can you give me some examples about acquired shared secret which use lua? thanks

@TanyaWong
Copy link
Author

thanks very much , problem has been solved

@daurnimator
Copy link
Collaborator

problem has been solved

How? What did you do?

@TanyaWong
Copy link
Author

TanyaWong commented Dec 20, 2017

I used two method :

  1. Add bn_mod_exp function in bn_methods , it's same as you gave me
 3157 static int bn_mod_exp(lua_State *L) {
 3158     BIGNUM *r, *a, *b, *c;
 3159  
 3160     lua_settop(L, 3); 
 3161     bn_preptop(L, &r, &a, &b, &c);
 3162      
 3163     if (!BN_mod_exp(r, a, b, c, getctx(L))) {
 3164         return auxL_error(L, auxL_EOPENSSL, "bignum:mod_exp");
 3165     }
 3166      
 3167     return 1;
 3168 }    
 3169      
 3170 static const auxL_Reg bn_methods[] = {
 3171     { "add",       &bn__add },
 3172     { "sub",       &bn__sub },
 3173     { "mul",       &bn__mul },
 3174     { "sqr",       &bn_sqr },
 3175     { "idiv",      &bn__idiv },
 3176     { "mod",       &bn__mod },
 3177     { "nnmod",     &bn_nnmod },
 3178     { "exp",       &bn__pow },
 3179     { "mod_exp",   &bn_mod_exp },
  1. Add pk_dhComputeKey function to call DH_compute_key in pk_methods, In order to I can more easy to call .
 4528 static const auxL_Reg pk_methods[] = {
 4529     { "type",          &pk_type },
 4530     { "setPublicKey",  &pk_setPublicKey },
 4531     { "setPrivateKey", &pk_setPrivateKey },
 4532 #if HAVE_EVP_PKEY_CTX_NEW
 4533     { "decrypt",       &pk_decrypt },
 4534     { "encrypt",       &pk_encrypt },
 4535 #endif                  
 4536     { "sign",          &pk_sign },
 4537     { "verify",        &pk_verify },
 4538     { "getDefaultDigestName", &pk_getDefaultDigestName },
 4539     { "toPEM",         &pk_toPEM },
 4540     { "getParameters", &pk_getParameters },
 4541     { "setParameters", &pk_setParameters },
 4542     { "dhComputeKey", &pk_dhComputeKey },
 4543     { NULL,            NULL },
 4544 };       

@daurnimator
Copy link
Collaborator

Did you want to send in a PR for pk_dhComputeKey?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants