blob: 85f1bef8ffa0f24a58d330511eb9898cef2dad04 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001/**
2 * \file dhm.h
3 */
4#ifndef XYSSL_DHM_H
5#define XYSSL_DHM_H
6
7#include "bignum.h"
8
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}dhm_context;
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * \brief Parse the ServerKeyExchange parameters
34 *
35 * \param ctx DHM context
36 * \param p &(start of input buffer)
37 * \param end end of buffer
38 *
39 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
40 */
41int dhm_read_params( dhm_context *ctx,
42 unsigned char **p,
43 unsigned char *end );
44
45/**
46 * \brief Setup and write the ServerKeyExchange parameters
47 *
48 * \param ctx DHM context
49 * \param x_size private value size in bits
50 * \param output destination buffer
51 * \param olen number of chars written
52 * \param f_rng RNG function
53 * \param p_rng RNG parameter
54 *
55 * \note This function assumes that ctx->P and ctx->G
56 * have already been properly set (for example
57 * using che_mpi_read_string or che_mpi_read_binary).
58 *
59 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
60 */
61int dhm_make_params( dhm_context *ctx, int s_size,
62 unsigned char *output, int *olen,
63 int (*f_rng)(void *), void *p_rng );
64
65int dhm_make_params_internal( dhm_context *ctx, int x_size,
66 int (*f_rng)(void *), void *p_rng );
67
68/**
69 * \brief Import the peer's public value G^Y
70 *
71 * \param ctx DHM context
72 * \param input input buffer
73 * \param ilen size of buffer
74 *
75 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
76 */
77int dhm_read_public( dhm_context *ctx,
78 unsigned char *input, int ilen );
79
80/**
81 * \brief Create own private value X and export G^X
82 *
83 * \param ctx DHM context
84 * \param x_size private value size in bits
85 * \param output destination buffer
86 * \param olen must be equal to ctx->P.len
87 * \param f_rng RNG function
88 * \param p_rng RNG parameter
89 *
90 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
91 */
92int dhm_make_public( dhm_context *ctx, int s_size,
93 unsigned char *output, int olen,
94 int (*f_rng)(void *), void *p_rng );
95int dhm_make_public_internal( dhm_context *ctx, int x_size,
96 int (*f_rng)(void *), void *p_rng );
97
98/**
99 * \brief Derive and export the shared secret (G^Y)^X mod P
100 *
101 * \param ctx DHM context
102 * \param output destination buffer
103 * \param olen number of chars written
104 *
105 * \return 0 if successful, or an XYSSL_ERR_DHM_XXX error code
106 */
107int dhm_calc_secret( dhm_context *ctx,
108 unsigned char *output, int *olen );
109int dhm_calc_secret_internal( dhm_context *ctx);
110
111/*
112 * \brief Free the components of a DHM key
113 */
114void dhm_free( dhm_context *ctx );
115
116/**
117 * \brief Checkup routine
118 *
119 * \return 0 if successful, or 1 if the test failed
120 */
121int dhm_self_test( int verbose );
122
123#ifdef __cplusplus
124}
125#endif
126
127#endif