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