update
[silc.git] / lib / silccrypt / silcdh.h
1 /*
2
3   silcdh.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2001 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /* PKCS #3 compliant Diffie Hellman key agreement protocol implementation.
21    This is used as part of SKE (SILC Key Exchange) protocol. */
22
23 #ifndef SILCDH_H
24 #define SILCDH_H
25
26 #include "silcmp.h"
27 #include "silcrng.h"
28
29 /* Forward declaration of Diffie Hellman context */
30 typedef struct SilcDHStruct *SilcDH;
31
32 /* XXX Move to source file */
33 /* Diffie Hellman context. This includes the DH parameters including the
34    negotiated key material. */
35 struct SilcDHStruct {
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) */
42
43   SilcRng rng;            /* RNG */
44 };
45
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);
51
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);
55
56 /* Generates random private value `x' such that 1 < x < n. Returns FALSE
57    if the random number could not be generated. Returns the generated
58    value into `x' pointer sent as argument, unless the `x' is NULL. The
59    returned `x' must no be freed by the caller. */
60 int silc_dh_generate_private(SilcDH dh, SilcInt **x);
61
62 /* Computes the public key y = g ^ x mod p, and returns it to the `y'
63    pointer sent as argument, unless the `y' is NULL. Returns FALSE if
64    the computation could not be performed. The returned `y' must not be
65    freed by the caller. */
66 int silc_dh_compute_public(SilcDH dh, SilcInt **y);
67
68 /* Sets the remote end's public value y' into the DH context. This must be
69    done before computing the secret key. Returns FALSE on error. */
70 int silc_dh_set_remote_public(SilcDH dh, SilcInt **y);
71
72 /* Computes the secret key z = y' ^ x mod p, and returns the key to the
73    `z' pointer sent as argument, unless the `z' is NULL. Returns FALSE if
74    the computation could not be performed. The returned `z' must not be
75    freed by the caller. */
76 int silc_dh_compute_key(SilcDH dh, SilcInt **z);
77
78 /* Same as above but returns the computed secret key as octet binary
79    string. */
80 int silc_dh_compute_key_data(SilcDH dh, unsigned char **z, 
81                              unsigned int *z_len);
82
83 #endif