Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file dhm.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Diffie-Hellman-Merkle key exchange |
| 5 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_DHM_H |
| 28 | #define POLARSSL_DHM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 30 | #include "bignum.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 32 | /* |
| 33 | * DHM Error codes |
| 34 | */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 35 | #define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */ |
| 36 | #define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */ |
| 37 | #define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */ |
| 38 | #define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */ |
Paul Bakker | c9b3e1e | 2012-04-26 18:59:23 +0000 | [diff] [blame] | 39 | #define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 40 | #define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 42 | /** |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 43 | * RFC 5114 defines a number of standardized Diffie-Hellman groups |
| 44 | * that can be used. Some are included here for convenience. |
| 45 | * |
| 46 | * Included are: |
| 47 | * 2.1. 1024-bit MODP Group with 160-bit Prime Order Subgroup |
| 48 | * 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup |
| 49 | */ |
| 50 | #define POLARSSL_DHM_RFC5114_MODP_1024_P \ |
| 51 | "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6" \ |
| 52 | "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0" \ |
| 53 | "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70" \ |
| 54 | "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0" \ |
| 55 | "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708" \ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame^] | 56 | "DF1FB2BC2E4A4371" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 57 | |
| 58 | #define POLARSSL_DHM_RFC5114_MODP_1024_G \ |
| 59 | "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F" \ |
| 60 | "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213" \ |
| 61 | "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1" \ |
| 62 | "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A" \ |
| 63 | "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24" \ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame^] | 64 | "855E6EEB22B3B2E5" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 65 | |
| 66 | #define POLARSSL_DHM_RFC5114_MODP_2048_P \ |
| 67 | "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \ |
| 68 | "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \ |
| 69 | "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \ |
| 70 | "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \ |
| 71 | "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \ |
| 72 | "B3BF8A317091883681286130BC8985DB1602E714415D9330" \ |
| 73 | "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \ |
| 74 | "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \ |
| 75 | "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \ |
| 76 | "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame^] | 77 | "CF9DE5384E71B81C0AC4DFFE0C10E64F" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 78 | |
| 79 | #define POLARSSL_DHM_RFC5114_MODP_2048_G \ |
| 80 | "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\ |
| 81 | "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\ |
| 82 | "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\ |
| 83 | "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\ |
| 84 | "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\ |
| 85 | "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\ |
| 86 | "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\ |
| 87 | "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\ |
| 88 | "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\ |
| 89 | "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame^] | 90 | "81BC087F2A7065B384B890D3191F2BFA" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 93 | * \brief DHM context structure |
| 94 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | typedef struct |
| 96 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 97 | size_t len; /*!< size(P) in chars */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | mpi P; /*!< prime modulus */ |
| 99 | mpi G; /*!< generator */ |
| 100 | mpi X; /*!< secret value */ |
| 101 | mpi GX; /*!< self = G^X mod P */ |
| 102 | mpi GY; /*!< peer = G^Y mod P */ |
| 103 | mpi K; /*!< key = GY^X mod P */ |
| 104 | mpi RP; /*!< cached R^2 mod P */ |
| 105 | } |
| 106 | dhm_context; |
| 107 | |
| 108 | #ifdef __cplusplus |
| 109 | extern "C" { |
| 110 | #endif |
| 111 | |
| 112 | /** |
| 113 | * \brief Parse the ServerKeyExchange parameters |
| 114 | * |
| 115 | * \param ctx DHM context |
| 116 | * \param p &(start of input buffer) |
| 117 | * \param end end of buffer |
| 118 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 119 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | */ |
| 121 | int dhm_read_params( dhm_context *ctx, |
| 122 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 123 | const unsigned char *end ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * \brief Setup and write the ServerKeyExchange parameters |
| 127 | * |
| 128 | * \param ctx DHM context |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 129 | * \param x_size private value size in bytes |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | * \param output destination buffer |
| 131 | * \param olen number of chars written |
| 132 | * \param f_rng RNG function |
| 133 | * \param p_rng RNG parameter |
| 134 | * |
| 135 | * \note This function assumes that ctx->P and ctx->G |
| 136 | * have already been properly set (for example |
| 137 | * using mpi_read_string or mpi_read_binary). |
| 138 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 139 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | */ |
Paul Bakker | f55ec08 | 2010-07-18 09:22:04 +0000 | [diff] [blame] | 141 | int dhm_make_params( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 142 | unsigned char *output, size_t *olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 143 | int (*f_rng)(void *, unsigned char *, size_t), |
| 144 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * \brief Import the peer's public value G^Y |
| 148 | * |
| 149 | * \param ctx DHM context |
| 150 | * \param input input buffer |
| 151 | * \param ilen size of buffer |
| 152 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 153 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | */ |
| 155 | int dhm_read_public( dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 156 | const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * \brief Create own private value X and export G^X |
| 160 | * |
| 161 | * \param ctx DHM context |
Paul Bakker | 84bef1d | 2012-04-20 13:42:02 +0000 | [diff] [blame] | 162 | * \param x_size private value size in bytes |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | * \param output destination buffer |
| 164 | * \param olen must be equal to ctx->P.len |
| 165 | * \param f_rng RNG function |
| 166 | * \param p_rng RNG parameter |
| 167 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 168 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 170 | int dhm_make_public( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 171 | unsigned char *output, size_t olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 172 | int (*f_rng)(void *, unsigned char *, size_t), |
| 173 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * \brief Derive and export the shared secret (G^Y)^X mod P |
| 177 | * |
| 178 | * \param ctx DHM context |
| 179 | * \param output destination buffer |
| 180 | * \param olen number of chars written |
| 181 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 182 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | */ |
| 184 | int dhm_calc_secret( dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 185 | unsigned char *output, size_t *olen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * \brief Free the components of a DHM key |
| 189 | */ |
| 190 | void dhm_free( dhm_context *ctx ); |
| 191 | |
| 192 | /** |
| 193 | * \brief Checkup routine |
| 194 | * |
| 195 | * \return 0 if successful, or 1 if the test failed |
| 196 | */ |
| 197 | int dhm_self_test( int verbose ); |
| 198 | |
| 199 | #ifdef __cplusplus |
| 200 | } |
| 201 | #endif |
| 202 | |
| 203 | #endif |