mirror of
https://github.com/libssh2/libssh2.git
synced 2025-11-21 14:00:51 +03:00
Implement Diffie-Hellman computations in crypto backends. (#149)
Not all backends feature the low level API needed to compute a Diffie-Hellman secret, but some of them directly implement Diffie-Hellman support with opaque private data. The later approach is now generalized and backends are responsible for all Diffie Hellman computations. As a side effect, procedures/macros _libssh2_bn_rand and _libssh2_bn_mod_exp are no longer needed outside the backends.
This commit is contained in:
committed by
Alexander Lamaison
parent
5abceec571
commit
f7daf3185a
@@ -1189,4 +1189,38 @@ _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session,
|
||||
return st;
|
||||
}
|
||||
|
||||
void
|
||||
_libssh2_dh_init(_libssh2_dh_ctx *dhctx)
|
||||
{
|
||||
*dhctx = BN_new(); /* Random from client */
|
||||
}
|
||||
|
||||
int
|
||||
_libssh2_dh_key_pair(_libssh2_dh_ctx *dhctx, _libssh2_bn *public,
|
||||
_libssh2_bn *g, _libssh2_bn *p, int group_order,
|
||||
_libssh2_bn_ctx *bnctx)
|
||||
{
|
||||
/* Generate x and e */
|
||||
BN_rand(*dhctx, group_order * 8 - 1, 0, -1);
|
||||
BN_mod_exp(public, g, *dhctx, p, bnctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
_libssh2_dh_secret(_libssh2_dh_ctx *dhctx, _libssh2_bn *secret,
|
||||
_libssh2_bn *f, _libssh2_bn *p,
|
||||
_libssh2_bn_ctx *bnctx)
|
||||
{
|
||||
/* Compute the shared secret */
|
||||
BN_mod_exp(secret, f, *dhctx, p, bnctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
_libssh2_dh_dtor(_libssh2_dh_ctx *dhctx)
|
||||
{
|
||||
BN_clear_free(*dhctx);
|
||||
*dhctx = NULL;
|
||||
}
|
||||
|
||||
#endif /* LIBSSH2_OPENSSL */
|
||||
|
||||
Reference in New Issue
Block a user