380
|
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 * Tom St Denis, [email protected], http://libtomcrypt.com |
|
10 */ |
|
11 #include "tomcrypt.h" |
|
12 |
|
13 /** |
|
14 @file dsa_shared_secret.c |
|
15 DSA Crypto, Tom St Denis |
|
16 */ |
|
17 |
|
18 #ifdef MDSA |
|
19 |
|
20 /** |
|
21 Create a DSA shared secret between two keys |
|
22 @param private_key The private DSA key (the exponent) |
|
23 @param base The base of the exponentiation (allows this to be used for both encrypt and decrypt) |
|
24 @param public_key The public key |
|
25 @param out [out] Destination of the shared secret |
|
26 @param outlen [in/out] The max size and resulting size of the shared secret |
|
27 @return CRYPT_OK if successful |
|
28 */ |
|
29 int dsa_shared_secret(void *private_key, void *base, |
|
30 dsa_key *public_key, |
|
31 unsigned char *out, unsigned long *outlen) |
|
32 { |
|
33 unsigned long x; |
|
34 void *res; |
|
35 int err; |
|
36 |
|
37 LTC_ARGCHK(private_key != NULL); |
|
38 LTC_ARGCHK(public_key != NULL); |
|
39 LTC_ARGCHK(out != NULL); |
|
40 LTC_ARGCHK(outlen != NULL); |
|
41 |
|
42 /* make new point */ |
|
43 if ((err = mp_init(&res)) != CRYPT_OK) { |
|
44 return err; |
|
45 } |
|
46 |
|
47 if ((err = mp_exptmod(base, private_key, public_key->p, res)) != CRYPT_OK) { |
|
48 mp_clear(res); |
|
49 return err; |
|
50 } |
|
51 |
|
52 x = (unsigned long)mp_unsigned_bin_size(res); |
|
53 if (*outlen < x) { |
|
54 *outlen = x; |
|
55 err = CRYPT_BUFFER_OVERFLOW; |
|
56 goto done; |
|
57 } |
|
58 zeromem(out, x); |
|
59 if ((err = mp_to_unsigned_bin(res, out + (x - mp_unsigned_bin_size(res)))) != CRYPT_OK) { goto done; } |
|
60 |
|
61 err = CRYPT_OK; |
|
62 *outlen = x; |
|
63 done: |
|
64 mp_clear(res); |
|
65 return err; |
|
66 } |
|
67 |
|
68 #endif |
|
69 /* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_shared_secret.c,v $ */ |
|
70 /* $Revision: 1.7 $ */ |
|
71 /* $Date: 2006/12/04 03:18:43 $ */ |
|
72 |