1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file dsa_shared_secret.c
14   DSA Crypto, Tom St Denis
15 */
16 
17 #ifdef LTC_MDSA
18 
19 /**
20   Create a DSA shared secret between two keys
21   @param private_key      The private DSA key (the exponent)
22   @param base             The base of the exponentiation (allows this to be used for both encrypt and decrypt)
23   @param public_key       The public key
24   @param out              [out] Destination of the shared secret
25   @param outlen           [in/out] The max size and resulting size of the shared secret
26   @return CRYPT_OK if successful
27 */
dsa_shared_secret(void * private_key,void * base,const dsa_key * public_key,unsigned char * out,unsigned long * outlen)28 int dsa_shared_secret(void          *private_key, void *base,
29                       const dsa_key *public_key,
30                       unsigned char *out,         unsigned long *outlen)
31 {
32    unsigned long  x;
33    void          *res;
34    int            err;
35 
36    LTC_ARGCHK(private_key != NULL);
37    LTC_ARGCHK(public_key  != NULL);
38    LTC_ARGCHK(out         != NULL);
39    LTC_ARGCHK(outlen      != NULL);
40 
41    /* make new point */
42    if ((err = mp_init(&res)) != CRYPT_OK) {
43       return err;
44    }
45 
46    if ((err = mp_exptmod(base, private_key, public_key->p, res)) != CRYPT_OK) {
47       mp_clear(res);
48       return err;
49    }
50 
51    x = (unsigned long)mp_unsigned_bin_size(res);
52    if (*outlen < x) {
53       *outlen = x;
54       err = CRYPT_BUFFER_OVERFLOW;
55       goto done;
56    }
57    zeromem(out, x);
58    if ((err = mp_to_unsigned_bin(res, out + (x - mp_unsigned_bin_size(res))))   != CRYPT_OK)          { goto done; }
59 
60    err     = CRYPT_OK;
61    *outlen = x;
62 done:
63    mp_clear(res);
64    return err;
65 }
66 
67 #endif
68 /* ref:         $Format:%D$ */
69 /* git commit:  $Format:%H$ */
70 /* commit time: $Format:%ai$ */
71 
72