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