blob: ebed106eb967312598ac2c4a69e9aacaa5f9bcdf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file dhm.h
3 */
4#ifndef XYSSL_DHM_H
5#define XYSSL_DHM_H
6
Paul Bakker8e831ed2009-01-03 21:24:11 +00007#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00008
9#define XYSSL_ERR_DHM_BAD_INPUT_DATA -0x0480
10#define XYSSL_ERR_DHM_READ_PARAMS_FAILED -0x0490
11#define XYSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x04A0
12#define XYSSL_ERR_DHM_READ_PUBLIC_FAILED -0x04B0
13#define XYSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x04C0
14#define XYSSL_ERR_DHM_CALC_SECRET_FAILED -0x04D0
15
16typedef struct
17{
18 int len; /*!< size(P) in chars */
19 mpi P; /*!< prime modulus */
20 mpi G; /*!< generator */
21 mpi X; /*!< secret value */
22 mpi GX; /*!< self = G^X mod P */
23 mpi GY; /*!< peer = G^Y mod P */
24 mpi K; /*!< key = GY^X mod P */
25 mpi RP; /*!< cached R^2 mod P */
26}
27dhm_context;
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/**
34 * \brief Parse the ServerKeyExchange parameters
35 *
36 * \param ctx DHM context
37 * \param p &(start of input buffer)
38 * \param end end of buffer
39 *
40 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
41 */
42int dhm_read_params( dhm_context *ctx,
43 unsigned char **p,
44 unsigned char *end );
45
46/**
47 * \brief Setup and write the ServerKeyExchange parameters
48 *
49 * \param ctx DHM context
50 * \param x_size private value size in bits
51 * \param output destination buffer
52 * \param olen number of chars written
53 * \param f_rng RNG function
54 * \param p_rng RNG parameter
55 *
56 * \note This function assumes that ctx->P and ctx->G
57 * have already been properly set (for example
58 * using mpi_read_string or mpi_read_binary).
59 *
60 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
61 */
62int dhm_make_params( dhm_context *ctx, int s_size,
63 unsigned char *output, int *olen,
64 int (*f_rng)(void *), void *p_rng );
65
66/**
67 * \brief Import the peer's public value G^Y
68 *
69 * \param ctx DHM context
70 * \param input input buffer
71 * \param ilen size of buffer
72 *
73 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
74 */
75int dhm_read_public( dhm_context *ctx,
76 unsigned char *input, int ilen );
77
78/**
79 * \brief Create own private value X and export G^X
80 *
81 * \param ctx DHM context
82 * \param x_size private value size in bits
83 * \param output destination buffer
84 * \param olen must be equal to ctx->P.len
85 * \param f_rng RNG function
86 * \param p_rng RNG parameter
87 *
88 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
89 */
90int dhm_make_public( dhm_context *ctx, int s_size,
91 unsigned char *output, int olen,
92 int (*f_rng)(void *), void *p_rng );
93
94/**
95 * \brief Derive and export the shared secret (G^Y)^X mod P
96 *
97 * \param ctx DHM context
98 * \param output destination buffer
99 * \param olen number of chars written
100 *
101 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
102 */
103int dhm_calc_secret( dhm_context *ctx,
104 unsigned char *output, int *olen );
105
106/*
107 * \brief Free the components of a DHM key
108 */
109void dhm_free( dhm_context *ctx );
110
111/**
112 * \brief Checkup routine
113 *
114 * \return 0 if successful, or 1 if the test failed
115 */
116int dhm_self_test( int verbose );
117
118#ifdef __cplusplus
119}
120#endif
121
122#endif