5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 /* PKCS #3 compliant Diffie Hellman key agreement protocol implementation.
21 This is used as part of SKE (SILC Key Exchange) protocol. */
29 /* Forward declaration of Diffie Hellman context */
30 typedef struct SilcDHStruct *SilcDH;
32 /* XXX Move to source file */
33 /* Diffie Hellman context. This includes the DH parameters including the
34 negotiated key material. */
36 SilcInt *g; /* Global base (generator) */
37 SilcInt *p; /* Global prime (modulus) */
38 SilcInt *my_x; /* x, My private value (random) */
39 SilcInt *my_y; /* y, My public value (y = g ^ x mod p) */
40 SilcInt *your_y; /* y', Your public value (y' = g ^ x' mod p) */
41 SilcInt *z; /* The computed secret key (z = y' ^ x mod p) */
43 SilcRng rng; /* RNG */
46 /* Allocate DH context. The `rng' must be initialized random number generator
47 context, the `g' is the public base generator used in the negotiation and
48 the `p' is the public prime used in the negotiation. Returns NULL on error
49 or allocated DH context on success. */
50 SilcDH silc_dh_alloc(SilcRng rng, SilcInt *g, SilcInt *p);
52 /* Frees the DH context. Does not free the RNG context given in the
53 allocation. Frees all the allocated data inside the DH context. */
54 void silc_dh_free(SilcDH dh);
56 /* Generates random private value `x' such that 1 < x < n. The rnd sent
57 as argument must be initialized. Returns FALSE if the random number
58 could not be generated. Returns the generated value into `x' pointer
59 sent as argument, unless the `x' is NULL. The returned `x' must no be
60 freed by the caller. */
61 int silc_dh_generate_private(SilcDH dh, SilcInt **x);
63 /* Computes the public key y = g ^ x mod p, and returns it to the `y'
64 pointer sent as argument, unless the `y' is NULL. Returns FALSE if
65 the computation could not be performed. The returned `y' must not be
66 freed by the caller. */
67 int silc_dh_compute_public(SilcDH dh, SilcInt **y);
69 /* Sets the remote end's public value y' into the DH context. This must be
70 done before computing the secret key. Returns FALSE on error. */
71 int silc_dh_set_remote_public(SilcDH dh, SilcInt **y);
73 /* Computes the secret key z = y' ^ x mod p, and returns the key to the
74 `z' pointer sent as argument, unless the `z' is NULL. Returns FALSE if
75 the computation could not be performed. The returned `z' must not be
76 freed by the caller. */
77 int silc_dh_compute_key(SilcDH dh, SilcInt **z);
79 /* Same as above but returns the computed secret key as octet binary
81 int silc_dh_compute_key_data(SilcDH dh, unsigned char **z,