1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
15 Create a DH shared secret.
16 @param private_key The private DH key in the pair
17 @param public_key The public DH key in the pair
18 @param out [out] The destination of the shared data
19 @param outlen [in/out] The max size and resulting size of the shared data.
20 @return CRYPT_OK if successful
22 int dh_shared_secret(dh_key *private_key, dh_key *public_key,
23 unsigned char *out, unsigned long *outlen)
29 LTC_ARGCHK(private_key != NULL);
30 LTC_ARGCHK(public_key != NULL);
31 LTC_ARGCHK(out != NULL);
32 LTC_ARGCHK(outlen != NULL);
35 if (private_key->type != PK_PRIVATE) {
36 return CRYPT_PK_NOT_PRIVATE;
40 if (mp_cmp(private_key->prime, public_key->prime) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; }
41 if (mp_cmp(private_key->base, public_key->base) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; }
43 /* init big numbers */
44 if ((err = mp_init(&tmp)) != CRYPT_OK) {
48 /* check public key */
49 if ((err = dh_check_pubkey(public_key)) != CRYPT_OK) {
53 /* compute tmp = y^x mod p */
54 if ((err = mp_exptmod(public_key->y, private_key->x, private_key->prime, tmp)) != CRYPT_OK) {
58 /* enough space for output? */
59 x = (unsigned long)mp_unsigned_bin_size(tmp);
62 err = CRYPT_BUFFER_OVERFLOW;
65 if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) {
78 /* ref: $Format:%D$ */
79 /* git commit: $Format:%H$ */
80 /* commit time: $Format:%ai$ */