blob: d2a3a404c011e1d93d7b2c032b0f1a27e60dfada [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
2 Copyright (C) 2006-2009 Werner Dittmann
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
20 */
21
22#ifndef _ZRTPDH_H__
23#define _ZRTPDH_H__
24
25#include <stdint.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050026
27/**
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050028 * @file zrtpDH.h
Alexandre Lision51140e12013-12-02 10:54:09 -050029 * @brief Class that implemets Diffie-Helman key agreement for ZRTP
30 *
31 * @ingroup GNU_ZRTP
32 * @{
33 */
34
35/**
36 * Generates a number of random bytes.
37 *
38 * @param buf
39 * Pointer to a buffer that receives the random data. Must have a size
40 * of at least <code>length</code> bytes.
41 *
42 * @param length
43 * Number of random bytes to produce.
44 */
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050045#if defined(__cplusplus)
46extern "C"
47{
48#endif
Alexandre Lision51140e12013-12-02 10:54:09 -050049void randomZRTP(uint8_t *buf, int32_t length);
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050050#if defined(__cplusplus)
51}
52#endif
53
54#if defined(__cplusplus)
55
56#include <libzrtpcpp/ZrtpConfigure.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050057
58const int32_t DH2K = 0;
59const int32_t DH3K = 1;
60const int32_t EC25 = 2;
61const int32_t EC38 = 3;
Alexandre Lision907ed2e2014-02-04 10:33:09 -050062const int32_t E255 = 4;
63const int32_t E414 = 5;
Alexandre Lision51140e12013-12-02 10:54:09 -050064
65
66/**
67 * Implementation of Diffie-Helman for ZRTP
68 *
69 * This class defines functions to generate and compute the
70 * Diffie-Helman public and secret data and the shared secret. According to
71 * the ZRTP specification we use the MODP groups as defined by RFC 3526 for
72 * length 3072 and 4096.
73 *
74 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
75 */
76
77class ZrtpDH {
78
79private:
80 void* ctx; ///< Context the DH
81 int pkType; ///< Which type of DH to use
82
83public:
84 /**
85 * Create a Diffie-Helman key agreement algorithm
86 *
87 * @param type
88 * Name of the DH algorithm to use
89 */
90 ZrtpDH(const char* type);
91
92 ~ZrtpDH();
93
94 /**
95 * Generates a public key based on the DH parameters and a random
96 * private key.
97 *
98 * @return 1 on success, 0 on failure
99 */
100 int32_t generatePublicKey();
101
102 /**
103 * Returns the size in bytes of the DH parameter p.
104 *
105 * @return Size in bytes.
106 */
107 int32_t getDhSize() const;
108
109 /**
110 * Returns the size in bytes of computed public key.
111 *
112 * @return Size in bytes.
113 */
114 int32_t getPubKeySize() const;
115
116 /**
117 * Returns the bytes of computed secret key.
118 *
119 * Returns the bytes of the public key in network (big endian) order.#
120 *
121 * @param buf
122 * Pointer to a buffer of at least <code>getPubKeySize()</code> bytes.
123 *
124 * @return Size in bytes.
125 */
126 int32_t getPubKeyBytes(uint8_t *buf) const;
127
128 /**
129 * Compute the secret key and returns it to caller.
130 *
131 * This method computes the secret key based on the DH parameters, the
132 * private key and the peer's public key.
133 *
134 * @param pubKeyBytes
135 * Pointer to the peer's public key bytes. Must be in big endian order.
136 *
137 * @param secret
138 * Pointer to a buffer that receives the secret key. This buffer must
139 * have a length of at least <code>getSecretSize()</code> bytes.
140 *
141 * @return the size of the shared secret on success, -1 on error.
142 */
143 int32_t computeSecretKey(uint8_t *pubKeyBytes, uint8_t *secret);
144
145 /**
146 * Check and validate the public key received from peer.
147 *
148 * Check if this is a correct Diffie-Helman public key. If the public
149 * key value is either one or (P-1) then this is a wrong public key
150 * value.
151 *
152 * @param pubKeyBytes
153 * Pointer to the peer's public key bytes. Must be in big endian order.
154 *
155 * @return 0 if check faild, 1 if public key value is ok.
156 */
157 int32_t checkPubKey(uint8_t* pubKeyBytes) const;
158
159 /**
160 * Get type of DH algorithm.
161 *
162 * @return
163 * Pointer to DH algorithm name
164 */
165 const char* getDHtype();
166};
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500167#endif /*__cpluscplus */
168#endif
Alexandre Lision51140e12013-12-02 10:54:09 -0500169
170/**
171 * @}
172 */
173
174/** EMACS **
175 * Local variables:
176 * mode: c++
177 * c-default-style: ellemtel
178 * c-basic-offset: 4
179 * End:
180 */