Path: blob/master/libs/tomcrypt/src/pk/dsa/dsa_shared_secret.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file dsa_shared_secret.c12DSA Crypto, Tom St Denis13*/1415#ifdef LTC_MDSA1617/**18Create a DSA shared secret between two keys19@param private_key The private DSA key (the exponent)20@param base The base of the exponentiation (allows this to be used for both encrypt and decrypt)21@param public_key The public key22@param out [out] Destination of the shared secret23@param outlen [in/out] The max size and resulting size of the shared secret24@return CRYPT_OK if successful25*/26int dsa_shared_secret(void *private_key, void *base,27dsa_key *public_key,28unsigned char *out, unsigned long *outlen)29{30unsigned long x;31void *res;32int err;3334LTC_ARGCHK(private_key != NULL);35LTC_ARGCHK(public_key != NULL);36LTC_ARGCHK(out != NULL);37LTC_ARGCHK(outlen != NULL);3839/* make new point */40if ((err = mp_init(&res)) != CRYPT_OK) {41return err;42}4344if ((err = mp_exptmod(base, private_key, public_key->p, res)) != CRYPT_OK) {45mp_clear(res);46return err;47}4849x = (unsigned long)mp_unsigned_bin_size(res);50if (*outlen < x) {51*outlen = x;52err = CRYPT_BUFFER_OVERFLOW;53goto done;54}55zeromem(out, x);56if ((err = mp_to_unsigned_bin(res, out + (x - mp_unsigned_bin_size(res)))) != CRYPT_OK) { goto done; }5758err = CRYPT_OK;59*outlen = x;60done:61mp_clear(res);62return err;63}6465#endif666768