Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
| 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 Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 26 | |
| 27 | /** |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 28 | * @file zrtpDH.h |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 29 | * @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 Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 45 | #if defined(__cplusplus) |
| 46 | extern "C" |
| 47 | { |
| 48 | #endif |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 49 | void randomZRTP(uint8_t *buf, int32_t length); |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 50 | #if defined(__cplusplus) |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | #if defined(__cplusplus) |
| 55 | |
| 56 | #include <libzrtpcpp/ZrtpConfigure.h> |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 57 | |
| 58 | const int32_t DH2K = 0; |
| 59 | const int32_t DH3K = 1; |
| 60 | const int32_t EC25 = 2; |
| 61 | const int32_t EC38 = 3; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Implementation of Diffie-Helman for ZRTP |
| 66 | * |
| 67 | * This class defines functions to generate and compute the |
| 68 | * Diffie-Helman public and secret data and the shared secret. According to |
| 69 | * the ZRTP specification we use the MODP groups as defined by RFC 3526 for |
| 70 | * length 3072 and 4096. |
| 71 | * |
| 72 | * @author Werner Dittmann <Werner.Dittmann@t-online.de> |
| 73 | */ |
| 74 | |
| 75 | class ZrtpDH { |
| 76 | |
| 77 | private: |
| 78 | void* ctx; ///< Context the DH |
| 79 | int pkType; ///< Which type of DH to use |
| 80 | |
| 81 | public: |
| 82 | /** |
| 83 | * Create a Diffie-Helman key agreement algorithm |
| 84 | * |
| 85 | * @param type |
| 86 | * Name of the DH algorithm to use |
| 87 | */ |
| 88 | ZrtpDH(const char* type); |
| 89 | |
| 90 | ~ZrtpDH(); |
| 91 | |
| 92 | /** |
| 93 | * Generates a public key based on the DH parameters and a random |
| 94 | * private key. |
| 95 | * |
| 96 | * @return 1 on success, 0 on failure |
| 97 | */ |
| 98 | int32_t generatePublicKey(); |
| 99 | |
| 100 | /** |
| 101 | * Returns the size in bytes of the DH parameter p. |
| 102 | * |
| 103 | * @return Size in bytes. |
| 104 | */ |
| 105 | int32_t getDhSize() const; |
| 106 | |
| 107 | /** |
| 108 | * Returns the size in bytes of computed public key. |
| 109 | * |
| 110 | * @return Size in bytes. |
| 111 | */ |
| 112 | int32_t getPubKeySize() const; |
| 113 | |
| 114 | /** |
| 115 | * Returns the bytes of computed secret key. |
| 116 | * |
| 117 | * Returns the bytes of the public key in network (big endian) order.# |
| 118 | * |
| 119 | * @param buf |
| 120 | * Pointer to a buffer of at least <code>getPubKeySize()</code> bytes. |
| 121 | * |
| 122 | * @return Size in bytes. |
| 123 | */ |
| 124 | int32_t getPubKeyBytes(uint8_t *buf) const; |
| 125 | |
| 126 | /** |
| 127 | * Compute the secret key and returns it to caller. |
| 128 | * |
| 129 | * This method computes the secret key based on the DH parameters, the |
| 130 | * private key and the peer's public key. |
| 131 | * |
| 132 | * @param pubKeyBytes |
| 133 | * Pointer to the peer's public key bytes. Must be in big endian order. |
| 134 | * |
| 135 | * @param secret |
| 136 | * Pointer to a buffer that receives the secret key. This buffer must |
| 137 | * have a length of at least <code>getSecretSize()</code> bytes. |
| 138 | * |
| 139 | * @return the size of the shared secret on success, -1 on error. |
| 140 | */ |
| 141 | int32_t computeSecretKey(uint8_t *pubKeyBytes, uint8_t *secret); |
| 142 | |
| 143 | /** |
| 144 | * Check and validate the public key received from peer. |
| 145 | * |
| 146 | * Check if this is a correct Diffie-Helman public key. If the public |
| 147 | * key value is either one or (P-1) then this is a wrong public key |
| 148 | * value. |
| 149 | * |
| 150 | * @param pubKeyBytes |
| 151 | * Pointer to the peer's public key bytes. Must be in big endian order. |
| 152 | * |
| 153 | * @return 0 if check faild, 1 if public key value is ok. |
| 154 | */ |
| 155 | int32_t checkPubKey(uint8_t* pubKeyBytes) const; |
| 156 | |
| 157 | /** |
| 158 | * Get type of DH algorithm. |
| 159 | * |
| 160 | * @return |
| 161 | * Pointer to DH algorithm name |
| 162 | */ |
| 163 | const char* getDHtype(); |
| 164 | }; |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 165 | #endif /*__cpluscplus */ |
| 166 | #endif |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * @} |
| 170 | */ |
| 171 | |
| 172 | /** EMACS ** |
| 173 | * Local variables: |
| 174 | * mode: c++ |
| 175 | * c-default-style: ellemtel |
| 176 | * c-basic-offset: 4 |
| 177 | * End: |
| 178 | */ |