2 * Copyright (C) 2010 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * @defgroup diffie_hellman diffie_hellman
23 #ifndef DIFFIE_HELLMAN_H_
24 #define DIFFIE_HELLMAN_H_
26 typedef enum diffie_hellman_group_t diffie_hellman_group_t
;
27 typedef struct diffie_hellman_t diffie_hellman_t
;
28 typedef struct diffie_hellman_params_t diffie_hellman_params_t
;
33 * Diffie-Hellman group.
35 * The modulus (or group) to use for a Diffie-Hellman calculation.
36 * See IKEv2 RFC 3.3.2 and RFC 3526.
38 * ECP groups are defined in RFC 4753 and RFC 5114.
39 * ECC Brainpool groups are defined in RFC 6954.
41 enum diffie_hellman_group_t
{
63 /** insecure NULL diffie hellman group for testing, in PRIVATE USE */
65 /** MODP group with custom generator/prime */
66 /** Parameters defined by IEEE 1363.1, in PRIVATE USE */
71 /** internally used DH group with additional parameters g and p, outside
72 * of PRIVATE USE (i.e. IKEv2 DH group range) so it can't be negotiated */
77 * enum name for diffie_hellman_group_t.
79 extern enum_name_t
*diffie_hellman_group_names
;
82 * Implementation of the Diffie-Hellman algorithm, as in RFC2631.
84 struct diffie_hellman_t
{
87 * Returns the shared secret of this diffie hellman exchange.
89 * Space for returned secret is allocated and must be freed by the caller.
91 * @param secret shared secret will be written into this chunk
92 * @return TRUE if shared secret computed successfully
94 bool (*get_shared_secret
)(diffie_hellman_t
*this, chunk_t
*secret
)
95 __attribute__((warn_unused_result
));
98 * Sets the public value of partner.
100 * Chunk gets cloned and can be destroyed afterwards.
102 * @param value public value of partner
104 void (*set_other_public_value
) (diffie_hellman_t
*this, chunk_t value
);
107 * Gets the own public value to transmit.
109 * Space for returned chunk is allocated and must be freed by the caller.
111 * @param value public value of caller is stored at this location
113 void (*get_my_public_value
) (diffie_hellman_t
*this, chunk_t
*value
);
116 * Get the DH group used.
118 * @return DH group set in construction
120 diffie_hellman_group_t (*get_dh_group
) (diffie_hellman_t
*this);
123 * Destroys a diffie_hellman_t object.
125 void (*destroy
) (diffie_hellman_t
*this);
129 * Parameters for a specific diffie hellman group.
131 struct diffie_hellman_params_t
{
134 * The prime of the group
139 * Generator of the group
141 const chunk_t generator
;
144 * Exponent length to use
149 * Prime order subgroup; for MODP Groups 22-24
151 const chunk_t subgroup
;
155 * Initialize diffie hellman parameters during startup.
157 void diffie_hellman_init();
160 * Get the parameters associated with the specified diffie hellman group.
162 * Before calling this method, use diffie_hellman_init() to initialize the
163 * DH group table. This is usually done by library_init().
165 * @param group DH group
166 * @return The parameters or NULL, if the group is not supported
168 diffie_hellman_params_t
*diffie_hellman_get_params(diffie_hellman_group_t group
);
171 * Check if a given DH group is an ECDH group
173 * @param group group to check
174 * @return TUE if group is an ECP group
176 bool diffie_hellman_group_is_ec(diffie_hellman_group_t group
);
178 #endif /** DIFFIE_HELLMAN_H_ @}*/