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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +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 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 24 | #ifndef MBEDTLS_DHM_H |
| 25 | #define MBEDTLS_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 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 32 | #define MBEDTLS_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */ |
| 33 | #define MBEDTLS_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */ |
| 34 | #define MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */ |
| 35 | #define MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */ |
| 36 | #define MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */ |
| 37 | #define MBEDTLS_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */ |
| 38 | #define MBEDTLS_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */ |
| 39 | #define MBEDTLS_ERR_DHM_MALLOC_FAILED -0x3400 /**< Allocation of memory failed. */ |
| 40 | #define MBEDTLS_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file 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 | 911c5dc | 2014-05-28 11:48:00 +0200 | [diff] [blame] | 43 | * RFC 2409 defines a number of standardized Diffie-Hellman groups |
| 44 | * that can be used. |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 45 | * RFC 3526 defines a number of standardized Diffie-Hellman groups |
| 46 | * for IKE. |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 47 | * RFC 5114 defines a number of standardized Diffie-Hellman groups |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 48 | * that can be used. |
| 49 | * |
| 50 | * Some are included here for convenience. |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 51 | * |
| 52 | * Included are: |
Paul Bakker | 911c5dc | 2014-05-28 11:48:00 +0200 | [diff] [blame] | 53 | * RFC 2409 6.2. 1024-bit MODP Group (Second Oakley Group) |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 54 | * RFC 3526 3. 2048-bit MODP Group |
| 55 | * RFC 3526 4. 3072-bit MODP Group |
| 56 | * RFC 5114 2.1. 1024-bit MODP Group with 160-bit Prime Order Subgroup |
| 57 | * 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] | 58 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 59 | #define MBEDTLS_DHM_RFC2409_MODP_1024_P \ |
Paul Bakker | 911c5dc | 2014-05-28 11:48:00 +0200 | [diff] [blame] | 60 | "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ |
| 61 | "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ |
| 62 | "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ |
| 63 | "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ |
| 64 | "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \ |
| 65 | "FFFFFFFFFFFFFFFF" |
| 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 67 | #define MBEDTLS_DHM_RFC2409_MODP_1024_G "02" |
Paul Bakker | 911c5dc | 2014-05-28 11:48:00 +0200 | [diff] [blame] | 68 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 69 | #define MBEDTLS_DHM_RFC3526_MODP_2048_P \ |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 70 | "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ |
| 71 | "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ |
| 72 | "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ |
| 73 | "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ |
| 74 | "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ |
| 75 | "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ |
| 76 | "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ |
| 77 | "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ |
| 78 | "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ |
| 79 | "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ |
| 80 | "15728E5A8AACAA68FFFFFFFFFFFFFFFF" |
| 81 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 82 | #define MBEDTLS_DHM_RFC3526_MODP_2048_G "02" |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 84 | #define MBEDTLS_DHM_RFC3526_MODP_3072_P \ |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 85 | "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ |
| 86 | "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ |
| 87 | "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ |
| 88 | "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ |
| 89 | "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ |
| 90 | "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ |
| 91 | "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ |
| 92 | "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ |
| 93 | "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ |
| 94 | "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ |
| 95 | "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ |
| 96 | "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ |
| 97 | "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ |
| 98 | "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ |
| 99 | "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ |
| 100 | "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF" |
| 101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 102 | #define MBEDTLS_DHM_RFC3526_MODP_3072_G "02" |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 104 | #define MBEDTLS_DHM_RFC5114_MODP_1024_P \ |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 105 | "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6" \ |
| 106 | "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0" \ |
| 107 | "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70" \ |
| 108 | "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0" \ |
| 109 | "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708" \ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame] | 110 | "DF1FB2BC2E4A4371" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 112 | #define MBEDTLS_DHM_RFC5114_MODP_1024_G \ |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 113 | "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F" \ |
| 114 | "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213" \ |
| 115 | "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1" \ |
| 116 | "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A" \ |
| 117 | "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24" \ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame] | 118 | "855E6EEB22B3B2E5" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 120 | #define MBEDTLS_DHM_RFC5114_MODP_2048_P \ |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 121 | "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \ |
| 122 | "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \ |
| 123 | "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \ |
| 124 | "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \ |
| 125 | "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \ |
| 126 | "B3BF8A317091883681286130BC8985DB1602E714415D9330" \ |
| 127 | "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \ |
| 128 | "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \ |
| 129 | "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \ |
| 130 | "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame] | 131 | "CF9DE5384E71B81C0AC4DFFE0C10E64F" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 133 | #define MBEDTLS_DHM_RFC5114_MODP_2048_G \ |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 134 | "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\ |
| 135 | "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\ |
| 136 | "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\ |
| 137 | "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\ |
| 138 | "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\ |
| 139 | "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\ |
| 140 | "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\ |
| 141 | "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\ |
| 142 | "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\ |
| 143 | "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\ |
Paul Bakker | a864f2e | 2012-09-26 08:29:20 +0000 | [diff] [blame] | 144 | "81BC087F2A7065B384B890D3191F2BFA" |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 145 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 146 | #ifdef __cplusplus |
| 147 | extern "C" { |
| 148 | #endif |
| 149 | |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 150 | /** |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 151 | * \brief DHM context structure |
| 152 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | typedef struct |
| 154 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 155 | size_t len; /*!< size(P) in chars */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 156 | mbedtls_mpi P; /*!< prime modulus */ |
| 157 | mbedtls_mpi G; /*!< generator */ |
| 158 | mbedtls_mpi X; /*!< secret value */ |
| 159 | mbedtls_mpi GX; /*!< self = G^X mod P */ |
| 160 | mbedtls_mpi GY; /*!< peer = G^Y mod P */ |
| 161 | mbedtls_mpi K; /*!< key = GY^X mod P */ |
| 162 | mbedtls_mpi RP; /*!< cached R^2 mod P */ |
| 163 | mbedtls_mpi Vi; /*!< blinding value */ |
| 164 | mbedtls_mpi Vf; /*!< un-blinding value */ |
| 165 | mbedtls_mpi pX; /*!< previous X */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 167 | mbedtls_dhm_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | /** |
Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 170 | * \brief Initialize DHM context |
| 171 | * |
| 172 | * \param ctx DHM context to be initialized |
| 173 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 174 | void mbedtls_dhm_init( mbedtls_dhm_context *ctx ); |
Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 175 | |
| 176 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | * \brief Parse the ServerKeyExchange parameters |
| 178 | * |
| 179 | * \param ctx DHM context |
| 180 | * \param p &(start of input buffer) |
| 181 | * \param end end of buffer |
| 182 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 183 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 185 | int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 187 | const unsigned char *end ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * \brief Setup and write the ServerKeyExchange parameters |
| 191 | * |
| 192 | * \param ctx DHM context |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 193 | * \param x_size private value size in bytes |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 194 | * \param output destination buffer |
| 195 | * \param olen number of chars written |
| 196 | * \param f_rng RNG function |
| 197 | * \param p_rng RNG parameter |
| 198 | * |
| 199 | * \note This function assumes that ctx->P and ctx->G |
| 200 | * have already been properly set (for example |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 201 | * using mbedtls_mpi_read_string or mbedtls_mpi_read_binary). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 203 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 205 | int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 206 | unsigned char *output, size_t *olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 207 | int (*f_rng)(void *, unsigned char *, size_t), |
| 208 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * \brief Import the peer's public value G^Y |
| 212 | * |
| 213 | * \param ctx DHM context |
| 214 | * \param input input buffer |
| 215 | * \param ilen size of buffer |
| 216 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 217 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 218 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 219 | int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 220 | const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * \brief Create own private value X and export G^X |
| 224 | * |
| 225 | * \param ctx DHM context |
Paul Bakker | 84bef1d | 2012-04-20 13:42:02 +0000 | [diff] [blame] | 226 | * \param x_size private value size in bytes |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | * \param output destination buffer |
| 228 | * \param olen must be equal to ctx->P.len |
| 229 | * \param f_rng RNG function |
| 230 | * \param p_rng RNG parameter |
| 231 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 232 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 234 | int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 235 | unsigned char *output, size_t olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 236 | int (*f_rng)(void *, unsigned char *, size_t), |
| 237 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * \brief Derive and export the shared secret (G^Y)^X mod P |
| 241 | * |
| 242 | * \param ctx DHM context |
| 243 | * \param output destination buffer |
Manuel Pégourié-Gonnard | 5af0e5b | 2014-06-25 13:03:50 +0200 | [diff] [blame] | 244 | * \param olen on entry, must hold the size of the destination buffer |
| 245 | * on exit, holds the actual number of bytes written |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 246 | * \param f_rng RNG function, for blinding purposes |
| 247 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 248 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 249 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 250 | * |
Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 251 | * \note If non-NULL, f_rng is used to blind the input as |
| 252 | * countermeasure against timing attacks. Blinding is |
| 253 | * automatically used if and only if our secret value X is |
| 254 | * re-used and costs nothing otherwise, so it is recommended |
| 255 | * to always pass a non-NULL f_rng argument. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 256 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 257 | int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx, |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 258 | unsigned char *output, size_t *olen, |
| 259 | int (*f_rng)(void *, unsigned char *, size_t), |
| 260 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 261 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 262 | /** |
Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 263 | * \brief Free and clear the components of a DHM key |
| 264 | * |
| 265 | * \param ctx DHM context to free and clear |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 266 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 267 | void mbedtls_dhm_free( mbedtls_dhm_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 269 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 270 | /** \ingroup x509_module */ |
| 271 | /** |
| 272 | * \brief Parse DHM parameters |
| 273 | * |
| 274 | * \param dhm DHM context to be initialized |
| 275 | * \param dhmin input buffer |
| 276 | * \param dhminlen size of the buffer |
| 277 | * |
| 278 | * \return 0 if successful, or a specific DHM or PEM error code |
| 279 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 280 | int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin, |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 281 | size_t dhminlen ); |
| 282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 283 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 284 | /** \ingroup x509_module */ |
| 285 | /** |
| 286 | * \brief Load and parse DHM parameters |
| 287 | * |
| 288 | * \param dhm DHM context to be initialized |
| 289 | * \param path filename to read the DHM Parameters from |
| 290 | * |
| 291 | * \return 0 if successful, or a specific DHM or PEM error code |
| 292 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 293 | int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path ); |
| 294 | #endif /* MBEDTLS_FS_IO */ |
| 295 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 296 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | /** |
| 298 | * \brief Checkup routine |
| 299 | * |
| 300 | * \return 0 if successful, or 1 if the test failed |
| 301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 302 | int mbedtls_dhm_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 303 | |
| 304 | #ifdef __cplusplus |
| 305 | } |
| 306 | #endif |
| 307 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 308 | #endif /* dhm.h */ |