updates. New data types.
[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, prime) */
38   SilcInt *lpf;           /* Largest prime factor (prime) */
39   SilcInt *my_x;          /* x, My private value (random) */
40   SilcInt *my_y;          /* y, My public value (y = g ^ x mod p) */
41   SilcInt *your_y;        /* y', Your public value (y' = g ^ x' mod p) */
42   SilcInt *z;             /* The computed secret key (z = y' ^ x mod p) */
43
44   SilcRng rng;            /* RNG */
45 };
46
47 /* Allocate DH context. The `rng' must be initialized random number generator
48    context, the `g' is the public base generator used in the negotiation,
49    the `p' is the public prime used in the negotiation and the `lpf' is
50    largest prime factor of p defined publicly as well. The `lpf' is optional
51    and if it is not supplied then the private values generated satifies
52    0 < x < p - 1 instead of 0 < x < lpf. Returns NULL on error or allocated
53    DH context on success. */
54 SilcDH silc_dh_alloc(SilcRng rng, SilcInt *g, SilcInt *p, SilcInt *lpf);
55
56 /* Frees the DH context. Does not free the RNG context given in the 
57    allocation. Frees all the allocated data inside the DH context. */
58 void silc_dh_free(SilcDH dh);
59
60 /* Generates random private value `x' such that 0 < x < lpf at most of
61    length of lpf. Returns FALSE if the random number could not be generated.
62    Returns the generated value into `x' pointer sent as argument, unless
63    the `x' is NULL. The returned `x' must no be freed by the caller. */
64 int silc_dh_generate_private(SilcDH dh, SilcInt **x);
65
66 /* Computes the public key y = g ^ x mod p, and returns it to the `y'
67    pointer sent as argument, unless the `y' is NULL. Returns FALSE if
68    the computation could not be performed. The returned `y' must not be
69    freed by the caller. */
70 int silc_dh_compute_public(SilcDH dh, SilcInt **y);
71
72 /* Sets the remote end's public value y' into the DH context. This must be
73    done before computing the secret key. Returns FALSE on error. */
74 int silc_dh_set_remote_public(SilcDH dh, SilcInt *y);
75
76 /* Computes the secret key z = y' ^ x mod p, and returns the key to the
77    `z' pointer sent as argument, unless the `z' is NULL. Returns FALSE if
78    the computation could not be performed. The returned `z' must not be
79    freed by the caller. */
80 int silc_dh_compute_key(SilcDH dh, SilcInt **z);
81
82 /* Same as above but returns the computed secret key as octet binary
83    string. */
84 int silc_dh_compute_key_data(SilcDH dh, unsigned char **z, 
85                              uint32 *z_len);
86
87 #endif