I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X
Dependencies: WncControllerK64F
mbedtls/source/dhm.c@16:8a4105d407d3, 2016-11-14 (annotated)
- Committer:
- JMF
- Date:
- Mon Nov 14 21:15:42 2016 +0000
- Revision:
- 16:8a4105d407d3
- Parent:
- 12:0071cb144c7a
updated to ensure it builds with TLS by correcting config defaults.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 12:0071cb144c7a | 1 | /* |
JMF | 12:0071cb144c7a | 2 | * Diffie-Hellman-Merkle key exchange |
JMF | 12:0071cb144c7a | 3 | * |
JMF | 12:0071cb144c7a | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
JMF | 12:0071cb144c7a | 5 | * SPDX-License-Identifier: Apache-2.0 |
JMF | 12:0071cb144c7a | 6 | * |
JMF | 12:0071cb144c7a | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
JMF | 12:0071cb144c7a | 8 | * not use this file except in compliance with the License. |
JMF | 12:0071cb144c7a | 9 | * You may obtain a copy of the License at |
JMF | 12:0071cb144c7a | 10 | * |
JMF | 12:0071cb144c7a | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 12:0071cb144c7a | 12 | * |
JMF | 12:0071cb144c7a | 13 | * Unless required by applicable law or agreed to in writing, software |
JMF | 12:0071cb144c7a | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
JMF | 12:0071cb144c7a | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 12:0071cb144c7a | 16 | * See the License for the specific language governing permissions and |
JMF | 12:0071cb144c7a | 17 | * limitations under the License. |
JMF | 12:0071cb144c7a | 18 | * |
JMF | 12:0071cb144c7a | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
JMF | 12:0071cb144c7a | 20 | */ |
JMF | 12:0071cb144c7a | 21 | /* |
JMF | 12:0071cb144c7a | 22 | * The following sources were referenced in the design of this implementation |
JMF | 12:0071cb144c7a | 23 | * of the Diffie-Hellman-Merkle algorithm: |
JMF | 12:0071cb144c7a | 24 | * |
JMF | 12:0071cb144c7a | 25 | * [1] Handbook of Applied Cryptography - 1997, Chapter 12 |
JMF | 12:0071cb144c7a | 26 | * Menezes, van Oorschot and Vanstone |
JMF | 12:0071cb144c7a | 27 | * |
JMF | 12:0071cb144c7a | 28 | */ |
JMF | 12:0071cb144c7a | 29 | |
JMF | 12:0071cb144c7a | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
JMF | 12:0071cb144c7a | 31 | #include "mbedtls/config.h" |
JMF | 12:0071cb144c7a | 32 | #else |
JMF | 12:0071cb144c7a | 33 | #include MBEDTLS_CONFIG_FILE |
JMF | 12:0071cb144c7a | 34 | #endif |
JMF | 12:0071cb144c7a | 35 | |
JMF | 12:0071cb144c7a | 36 | #if defined(MBEDTLS_DHM_C) |
JMF | 12:0071cb144c7a | 37 | |
JMF | 12:0071cb144c7a | 38 | #include "mbedtls/dhm.h" |
JMF | 12:0071cb144c7a | 39 | |
JMF | 12:0071cb144c7a | 40 | #include <string.h> |
JMF | 12:0071cb144c7a | 41 | |
JMF | 12:0071cb144c7a | 42 | #if defined(MBEDTLS_PEM_PARSE_C) |
JMF | 12:0071cb144c7a | 43 | #include "mbedtls/pem.h" |
JMF | 12:0071cb144c7a | 44 | #endif |
JMF | 12:0071cb144c7a | 45 | |
JMF | 12:0071cb144c7a | 46 | #if defined(MBEDTLS_ASN1_PARSE_C) |
JMF | 12:0071cb144c7a | 47 | #include "mbedtls/asn1.h" |
JMF | 12:0071cb144c7a | 48 | #endif |
JMF | 12:0071cb144c7a | 49 | |
JMF | 12:0071cb144c7a | 50 | #if defined(MBEDTLS_PLATFORM_C) |
JMF | 12:0071cb144c7a | 51 | #include "mbedtls/platform.h" |
JMF | 12:0071cb144c7a | 52 | #else |
JMF | 12:0071cb144c7a | 53 | #include <stdlib.h> |
JMF | 12:0071cb144c7a | 54 | #include <stdio.h> |
JMF | 12:0071cb144c7a | 55 | #define mbedtls_printf pc.printf |
JMF | 12:0071cb144c7a | 56 | #define mbedtls_calloc calloc |
JMF | 12:0071cb144c7a | 57 | #define mbedtls_free free |
JMF | 12:0071cb144c7a | 58 | #endif |
JMF | 12:0071cb144c7a | 59 | |
JMF | 12:0071cb144c7a | 60 | /* Implementation that should never be optimized out by the compiler */ |
JMF | 12:0071cb144c7a | 61 | static void mbedtls_zeroize( void *v, size_t n ) { |
JMF | 12:0071cb144c7a | 62 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
JMF | 12:0071cb144c7a | 63 | } |
JMF | 12:0071cb144c7a | 64 | |
JMF | 12:0071cb144c7a | 65 | /* |
JMF | 12:0071cb144c7a | 66 | * helper to validate the mbedtls_mpi size and import it |
JMF | 12:0071cb144c7a | 67 | */ |
JMF | 12:0071cb144c7a | 68 | static int dhm_read_bignum( mbedtls_mpi *X, |
JMF | 12:0071cb144c7a | 69 | unsigned char **p, |
JMF | 12:0071cb144c7a | 70 | const unsigned char *end ) |
JMF | 12:0071cb144c7a | 71 | { |
JMF | 12:0071cb144c7a | 72 | int ret, n; |
JMF | 12:0071cb144c7a | 73 | |
JMF | 12:0071cb144c7a | 74 | if( end - *p < 2 ) |
JMF | 12:0071cb144c7a | 75 | return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); |
JMF | 12:0071cb144c7a | 76 | |
JMF | 12:0071cb144c7a | 77 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
JMF | 12:0071cb144c7a | 78 | (*p) += 2; |
JMF | 12:0071cb144c7a | 79 | |
JMF | 12:0071cb144c7a | 80 | if( (int)( end - *p ) < n ) |
JMF | 12:0071cb144c7a | 81 | return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); |
JMF | 12:0071cb144c7a | 82 | |
JMF | 12:0071cb144c7a | 83 | if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 ) |
JMF | 12:0071cb144c7a | 84 | return( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED + ret ); |
JMF | 12:0071cb144c7a | 85 | |
JMF | 12:0071cb144c7a | 86 | (*p) += n; |
JMF | 12:0071cb144c7a | 87 | |
JMF | 12:0071cb144c7a | 88 | return( 0 ); |
JMF | 12:0071cb144c7a | 89 | } |
JMF | 12:0071cb144c7a | 90 | |
JMF | 12:0071cb144c7a | 91 | /* |
JMF | 12:0071cb144c7a | 92 | * Verify sanity of parameter with regards to P |
JMF | 12:0071cb144c7a | 93 | * |
JMF | 12:0071cb144c7a | 94 | * Parameter should be: 2 <= public_param <= P - 2 |
JMF | 12:0071cb144c7a | 95 | * |
JMF | 12:0071cb144c7a | 96 | * For more information on the attack, see: |
JMF | 12:0071cb144c7a | 97 | * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf |
JMF | 12:0071cb144c7a | 98 | * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643 |
JMF | 12:0071cb144c7a | 99 | */ |
JMF | 12:0071cb144c7a | 100 | static int dhm_check_range( const mbedtls_mpi *param, const mbedtls_mpi *P ) |
JMF | 12:0071cb144c7a | 101 | { |
JMF | 12:0071cb144c7a | 102 | mbedtls_mpi L, U; |
JMF | 12:0071cb144c7a | 103 | int ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA; |
JMF | 12:0071cb144c7a | 104 | |
JMF | 12:0071cb144c7a | 105 | mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U ); |
JMF | 12:0071cb144c7a | 106 | |
JMF | 12:0071cb144c7a | 107 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) ); |
JMF | 12:0071cb144c7a | 108 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) ); |
JMF | 12:0071cb144c7a | 109 | |
JMF | 12:0071cb144c7a | 110 | if( mbedtls_mpi_cmp_mpi( param, &L ) >= 0 && |
JMF | 12:0071cb144c7a | 111 | mbedtls_mpi_cmp_mpi( param, &U ) <= 0 ) |
JMF | 12:0071cb144c7a | 112 | { |
JMF | 12:0071cb144c7a | 113 | ret = 0; |
JMF | 12:0071cb144c7a | 114 | } |
JMF | 12:0071cb144c7a | 115 | |
JMF | 12:0071cb144c7a | 116 | cleanup: |
JMF | 12:0071cb144c7a | 117 | mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U ); |
JMF | 12:0071cb144c7a | 118 | return( ret ); |
JMF | 12:0071cb144c7a | 119 | } |
JMF | 12:0071cb144c7a | 120 | |
JMF | 12:0071cb144c7a | 121 | void mbedtls_dhm_init( mbedtls_dhm_context *ctx ) |
JMF | 12:0071cb144c7a | 122 | { |
JMF | 12:0071cb144c7a | 123 | memset( ctx, 0, sizeof( mbedtls_dhm_context ) ); |
JMF | 12:0071cb144c7a | 124 | } |
JMF | 12:0071cb144c7a | 125 | |
JMF | 12:0071cb144c7a | 126 | /* |
JMF | 12:0071cb144c7a | 127 | * Parse the ServerKeyExchange parameters |
JMF | 12:0071cb144c7a | 128 | */ |
JMF | 12:0071cb144c7a | 129 | int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx, |
JMF | 12:0071cb144c7a | 130 | unsigned char **p, |
JMF | 12:0071cb144c7a | 131 | const unsigned char *end ) |
JMF | 12:0071cb144c7a | 132 | { |
JMF | 12:0071cb144c7a | 133 | int ret; |
JMF | 12:0071cb144c7a | 134 | |
JMF | 12:0071cb144c7a | 135 | if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 || |
JMF | 12:0071cb144c7a | 136 | ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 || |
JMF | 12:0071cb144c7a | 137 | ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 ) |
JMF | 12:0071cb144c7a | 138 | return( ret ); |
JMF | 12:0071cb144c7a | 139 | |
JMF | 12:0071cb144c7a | 140 | if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) |
JMF | 12:0071cb144c7a | 141 | return( ret ); |
JMF | 12:0071cb144c7a | 142 | |
JMF | 12:0071cb144c7a | 143 | ctx->len = mbedtls_mpi_size( &ctx->P ); |
JMF | 12:0071cb144c7a | 144 | |
JMF | 12:0071cb144c7a | 145 | return( 0 ); |
JMF | 12:0071cb144c7a | 146 | } |
JMF | 12:0071cb144c7a | 147 | |
JMF | 12:0071cb144c7a | 148 | /* |
JMF | 12:0071cb144c7a | 149 | * Setup and write the ServerKeyExchange parameters |
JMF | 12:0071cb144c7a | 150 | */ |
JMF | 12:0071cb144c7a | 151 | int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size, |
JMF | 12:0071cb144c7a | 152 | unsigned char *output, size_t *olen, |
JMF | 12:0071cb144c7a | 153 | int (*f_rng)(void *, unsigned char *, size_t), |
JMF | 12:0071cb144c7a | 154 | void *p_rng ) |
JMF | 12:0071cb144c7a | 155 | { |
JMF | 12:0071cb144c7a | 156 | int ret, count = 0; |
JMF | 12:0071cb144c7a | 157 | size_t n1, n2, n3; |
JMF | 12:0071cb144c7a | 158 | unsigned char *p; |
JMF | 12:0071cb144c7a | 159 | |
JMF | 12:0071cb144c7a | 160 | if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ) |
JMF | 12:0071cb144c7a | 161 | return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); |
JMF | 12:0071cb144c7a | 162 | |
JMF | 12:0071cb144c7a | 163 | /* |
JMF | 12:0071cb144c7a | 164 | * Generate X as large as possible ( < P ) |
JMF | 12:0071cb144c7a | 165 | */ |
JMF | 12:0071cb144c7a | 166 | do |
JMF | 12:0071cb144c7a | 167 | { |
JMF | 12:0071cb144c7a | 168 | mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ); |
JMF | 12:0071cb144c7a | 169 | |
JMF | 12:0071cb144c7a | 170 | while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
JMF | 12:0071cb144c7a | 171 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) ); |
JMF | 12:0071cb144c7a | 172 | |
JMF | 12:0071cb144c7a | 173 | if( count++ > 10 ) |
JMF | 12:0071cb144c7a | 174 | return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED ); |
JMF | 12:0071cb144c7a | 175 | } |
JMF | 12:0071cb144c7a | 176 | while( dhm_check_range( &ctx->X, &ctx->P ) != 0 ); |
JMF | 12:0071cb144c7a | 177 | |
JMF | 12:0071cb144c7a | 178 | /* |
JMF | 12:0071cb144c7a | 179 | * Calculate GX = G^X mod P |
JMF | 12:0071cb144c7a | 180 | */ |
JMF | 12:0071cb144c7a | 181 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
JMF | 12:0071cb144c7a | 182 | &ctx->P , &ctx->RP ) ); |
JMF | 12:0071cb144c7a | 183 | |
JMF | 12:0071cb144c7a | 184 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
JMF | 12:0071cb144c7a | 185 | return( ret ); |
JMF | 12:0071cb144c7a | 186 | |
JMF | 12:0071cb144c7a | 187 | /* |
JMF | 12:0071cb144c7a | 188 | * export P, G, GX |
JMF | 12:0071cb144c7a | 189 | */ |
JMF | 12:0071cb144c7a | 190 | #define DHM_MPI_EXPORT(X,n) \ |
JMF | 12:0071cb144c7a | 191 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, p + 2, n ) ); \ |
JMF | 12:0071cb144c7a | 192 | *p++ = (unsigned char)( n >> 8 ); \ |
JMF | 12:0071cb144c7a | 193 | *p++ = (unsigned char)( n ); p += n; |
JMF | 12:0071cb144c7a | 194 | |
JMF | 12:0071cb144c7a | 195 | n1 = mbedtls_mpi_size( &ctx->P ); |
JMF | 12:0071cb144c7a | 196 | n2 = mbedtls_mpi_size( &ctx->G ); |
JMF | 12:0071cb144c7a | 197 | n3 = mbedtls_mpi_size( &ctx->GX ); |
JMF | 12:0071cb144c7a | 198 | |
JMF | 12:0071cb144c7a | 199 | p = output; |
JMF | 12:0071cb144c7a | 200 | DHM_MPI_EXPORT( &ctx->P , n1 ); |
JMF | 12:0071cb144c7a | 201 | DHM_MPI_EXPORT( &ctx->G , n2 ); |
JMF | 12:0071cb144c7a | 202 | DHM_MPI_EXPORT( &ctx->GX, n3 ); |
JMF | 12:0071cb144c7a | 203 | |
JMF | 12:0071cb144c7a | 204 | *olen = p - output; |
JMF | 12:0071cb144c7a | 205 | |
JMF | 12:0071cb144c7a | 206 | ctx->len = n1; |
JMF | 12:0071cb144c7a | 207 | |
JMF | 12:0071cb144c7a | 208 | cleanup: |
JMF | 12:0071cb144c7a | 209 | |
JMF | 12:0071cb144c7a | 210 | if( ret != 0 ) |
JMF | 12:0071cb144c7a | 211 | return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret ); |
JMF | 12:0071cb144c7a | 212 | |
JMF | 12:0071cb144c7a | 213 | return( 0 ); |
JMF | 12:0071cb144c7a | 214 | } |
JMF | 12:0071cb144c7a | 215 | |
JMF | 12:0071cb144c7a | 216 | /* |
JMF | 12:0071cb144c7a | 217 | * Import the peer's public value G^Y |
JMF | 12:0071cb144c7a | 218 | */ |
JMF | 12:0071cb144c7a | 219 | int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx, |
JMF | 12:0071cb144c7a | 220 | const unsigned char *input, size_t ilen ) |
JMF | 12:0071cb144c7a | 221 | { |
JMF | 12:0071cb144c7a | 222 | int ret; |
JMF | 12:0071cb144c7a | 223 | |
JMF | 12:0071cb144c7a | 224 | if( ctx == NULL || ilen < 1 || ilen > ctx->len ) |
JMF | 12:0071cb144c7a | 225 | return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); |
JMF | 12:0071cb144c7a | 226 | |
JMF | 12:0071cb144c7a | 227 | if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 ) |
JMF | 12:0071cb144c7a | 228 | return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret ); |
JMF | 12:0071cb144c7a | 229 | |
JMF | 12:0071cb144c7a | 230 | return( 0 ); |
JMF | 12:0071cb144c7a | 231 | } |
JMF | 12:0071cb144c7a | 232 | |
JMF | 12:0071cb144c7a | 233 | /* |
JMF | 12:0071cb144c7a | 234 | * Create own private value X and export G^X |
JMF | 12:0071cb144c7a | 235 | */ |
JMF | 12:0071cb144c7a | 236 | int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size, |
JMF | 12:0071cb144c7a | 237 | unsigned char *output, size_t olen, |
JMF | 12:0071cb144c7a | 238 | int (*f_rng)(void *, unsigned char *, size_t), |
JMF | 12:0071cb144c7a | 239 | void *p_rng ) |
JMF | 12:0071cb144c7a | 240 | { |
JMF | 12:0071cb144c7a | 241 | int ret, count = 0; |
JMF | 12:0071cb144c7a | 242 | |
JMF | 12:0071cb144c7a | 243 | if( ctx == NULL || olen < 1 || olen > ctx->len ) |
JMF | 12:0071cb144c7a | 244 | return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); |
JMF | 12:0071cb144c7a | 245 | |
JMF | 12:0071cb144c7a | 246 | if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ) |
JMF | 12:0071cb144c7a | 247 | return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); |
JMF | 12:0071cb144c7a | 248 | |
JMF | 12:0071cb144c7a | 249 | /* |
JMF | 12:0071cb144c7a | 250 | * generate X and calculate GX = G^X mod P |
JMF | 12:0071cb144c7a | 251 | */ |
JMF | 12:0071cb144c7a | 252 | do |
JMF | 12:0071cb144c7a | 253 | { |
JMF | 12:0071cb144c7a | 254 | mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ); |
JMF | 12:0071cb144c7a | 255 | |
JMF | 12:0071cb144c7a | 256 | while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
JMF | 12:0071cb144c7a | 257 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) ); |
JMF | 12:0071cb144c7a | 258 | |
JMF | 12:0071cb144c7a | 259 | if( count++ > 10 ) |
JMF | 12:0071cb144c7a | 260 | return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED ); |
JMF | 12:0071cb144c7a | 261 | } |
JMF | 12:0071cb144c7a | 262 | while( dhm_check_range( &ctx->X, &ctx->P ) != 0 ); |
JMF | 12:0071cb144c7a | 263 | |
JMF | 12:0071cb144c7a | 264 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
JMF | 12:0071cb144c7a | 265 | &ctx->P , &ctx->RP ) ); |
JMF | 12:0071cb144c7a | 266 | |
JMF | 12:0071cb144c7a | 267 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
JMF | 12:0071cb144c7a | 268 | return( ret ); |
JMF | 12:0071cb144c7a | 269 | |
JMF | 12:0071cb144c7a | 270 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) ); |
JMF | 12:0071cb144c7a | 271 | |
JMF | 12:0071cb144c7a | 272 | cleanup: |
JMF | 12:0071cb144c7a | 273 | |
JMF | 12:0071cb144c7a | 274 | if( ret != 0 ) |
JMF | 12:0071cb144c7a | 275 | return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED + ret ); |
JMF | 12:0071cb144c7a | 276 | |
JMF | 12:0071cb144c7a | 277 | return( 0 ); |
JMF | 12:0071cb144c7a | 278 | } |
JMF | 12:0071cb144c7a | 279 | |
JMF | 12:0071cb144c7a | 280 | /* |
JMF | 12:0071cb144c7a | 281 | * Use the blinding method and optimisation suggested in section 10 of: |
JMF | 12:0071cb144c7a | 282 | * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA, |
JMF | 12:0071cb144c7a | 283 | * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer |
JMF | 12:0071cb144c7a | 284 | * Berlin Heidelberg, 1996. p. 104-113. |
JMF | 12:0071cb144c7a | 285 | */ |
JMF | 12:0071cb144c7a | 286 | static int dhm_update_blinding( mbedtls_dhm_context *ctx, |
JMF | 12:0071cb144c7a | 287 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
JMF | 12:0071cb144c7a | 288 | { |
JMF | 12:0071cb144c7a | 289 | int ret, count; |
JMF | 12:0071cb144c7a | 290 | |
JMF | 12:0071cb144c7a | 291 | /* |
JMF | 12:0071cb144c7a | 292 | * Don't use any blinding the first time a particular X is used, |
JMF | 12:0071cb144c7a | 293 | * but remember it to use blinding next time. |
JMF | 12:0071cb144c7a | 294 | */ |
JMF | 12:0071cb144c7a | 295 | if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 ) |
JMF | 12:0071cb144c7a | 296 | { |
JMF | 12:0071cb144c7a | 297 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) ); |
JMF | 12:0071cb144c7a | 298 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) ); |
JMF | 12:0071cb144c7a | 299 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) ); |
JMF | 12:0071cb144c7a | 300 | |
JMF | 12:0071cb144c7a | 301 | return( 0 ); |
JMF | 12:0071cb144c7a | 302 | } |
JMF | 12:0071cb144c7a | 303 | |
JMF | 12:0071cb144c7a | 304 | /* |
JMF | 12:0071cb144c7a | 305 | * Ok, we need blinding. Can we re-use existing values? |
JMF | 12:0071cb144c7a | 306 | * If yes, just update them by squaring them. |
JMF | 12:0071cb144c7a | 307 | */ |
JMF | 12:0071cb144c7a | 308 | if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 ) |
JMF | 12:0071cb144c7a | 309 | { |
JMF | 12:0071cb144c7a | 310 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) ); |
JMF | 12:0071cb144c7a | 311 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) ); |
JMF | 12:0071cb144c7a | 312 | |
JMF | 12:0071cb144c7a | 313 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) ); |
JMF | 12:0071cb144c7a | 314 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) ); |
JMF | 12:0071cb144c7a | 315 | |
JMF | 12:0071cb144c7a | 316 | return( 0 ); |
JMF | 12:0071cb144c7a | 317 | } |
JMF | 12:0071cb144c7a | 318 | |
JMF | 12:0071cb144c7a | 319 | /* |
JMF | 12:0071cb144c7a | 320 | * We need to generate blinding values from scratch |
JMF | 12:0071cb144c7a | 321 | */ |
JMF | 12:0071cb144c7a | 322 | |
JMF | 12:0071cb144c7a | 323 | /* Vi = random( 2, P-1 ) */ |
JMF | 12:0071cb144c7a | 324 | count = 0; |
JMF | 12:0071cb144c7a | 325 | do |
JMF | 12:0071cb144c7a | 326 | { |
JMF | 12:0071cb144c7a | 327 | mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng ); |
JMF | 12:0071cb144c7a | 328 | |
JMF | 12:0071cb144c7a | 329 | while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 ) |
JMF | 12:0071cb144c7a | 330 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) ); |
JMF | 12:0071cb144c7a | 331 | |
JMF | 12:0071cb144c7a | 332 | if( count++ > 10 ) |
JMF | 12:0071cb144c7a | 333 | return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); |
JMF | 12:0071cb144c7a | 334 | } |
JMF | 12:0071cb144c7a | 335 | while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 ); |
JMF | 12:0071cb144c7a | 336 | |
JMF | 12:0071cb144c7a | 337 | /* Vf = Vi^-X mod P */ |
JMF | 12:0071cb144c7a | 338 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) ); |
JMF | 12:0071cb144c7a | 339 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) ); |
JMF | 12:0071cb144c7a | 340 | |
JMF | 12:0071cb144c7a | 341 | cleanup: |
JMF | 12:0071cb144c7a | 342 | return( ret ); |
JMF | 12:0071cb144c7a | 343 | } |
JMF | 12:0071cb144c7a | 344 | |
JMF | 12:0071cb144c7a | 345 | /* |
JMF | 12:0071cb144c7a | 346 | * Derive and export the shared secret (G^Y)^X mod P |
JMF | 12:0071cb144c7a | 347 | */ |
JMF | 12:0071cb144c7a | 348 | int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx, |
JMF | 12:0071cb144c7a | 349 | unsigned char *output, size_t output_size, size_t *olen, |
JMF | 12:0071cb144c7a | 350 | int (*f_rng)(void *, unsigned char *, size_t), |
JMF | 12:0071cb144c7a | 351 | void *p_rng ) |
JMF | 12:0071cb144c7a | 352 | { |
JMF | 12:0071cb144c7a | 353 | int ret; |
JMF | 12:0071cb144c7a | 354 | mbedtls_mpi GYb; |
JMF | 12:0071cb144c7a | 355 | |
JMF | 12:0071cb144c7a | 356 | if( ctx == NULL || output_size < ctx->len ) |
JMF | 12:0071cb144c7a | 357 | return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); |
JMF | 12:0071cb144c7a | 358 | |
JMF | 12:0071cb144c7a | 359 | if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) |
JMF | 12:0071cb144c7a | 360 | return( ret ); |
JMF | 12:0071cb144c7a | 361 | |
JMF | 12:0071cb144c7a | 362 | mbedtls_mpi_init( &GYb ); |
JMF | 12:0071cb144c7a | 363 | |
JMF | 12:0071cb144c7a | 364 | /* Blind peer's value */ |
JMF | 12:0071cb144c7a | 365 | if( f_rng != NULL ) |
JMF | 12:0071cb144c7a | 366 | { |
JMF | 12:0071cb144c7a | 367 | MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) ); |
JMF | 12:0071cb144c7a | 368 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) ); |
JMF | 12:0071cb144c7a | 369 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) ); |
JMF | 12:0071cb144c7a | 370 | } |
JMF | 12:0071cb144c7a | 371 | else |
JMF | 12:0071cb144c7a | 372 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) ); |
JMF | 12:0071cb144c7a | 373 | |
JMF | 12:0071cb144c7a | 374 | /* Do modular exponentiation */ |
JMF | 12:0071cb144c7a | 375 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X, |
JMF | 12:0071cb144c7a | 376 | &ctx->P, &ctx->RP ) ); |
JMF | 12:0071cb144c7a | 377 | |
JMF | 12:0071cb144c7a | 378 | /* Unblind secret value */ |
JMF | 12:0071cb144c7a | 379 | if( f_rng != NULL ) |
JMF | 12:0071cb144c7a | 380 | { |
JMF | 12:0071cb144c7a | 381 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) ); |
JMF | 12:0071cb144c7a | 382 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) ); |
JMF | 12:0071cb144c7a | 383 | } |
JMF | 12:0071cb144c7a | 384 | |
JMF | 12:0071cb144c7a | 385 | *olen = mbedtls_mpi_size( &ctx->K ); |
JMF | 12:0071cb144c7a | 386 | |
JMF | 12:0071cb144c7a | 387 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) ); |
JMF | 12:0071cb144c7a | 388 | |
JMF | 12:0071cb144c7a | 389 | cleanup: |
JMF | 12:0071cb144c7a | 390 | mbedtls_mpi_free( &GYb ); |
JMF | 12:0071cb144c7a | 391 | |
JMF | 12:0071cb144c7a | 392 | if( ret != 0 ) |
JMF | 12:0071cb144c7a | 393 | return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret ); |
JMF | 12:0071cb144c7a | 394 | |
JMF | 12:0071cb144c7a | 395 | return( 0 ); |
JMF | 12:0071cb144c7a | 396 | } |
JMF | 12:0071cb144c7a | 397 | |
JMF | 12:0071cb144c7a | 398 | /* |
JMF | 12:0071cb144c7a | 399 | * Free the components of a DHM key |
JMF | 12:0071cb144c7a | 400 | */ |
JMF | 12:0071cb144c7a | 401 | void mbedtls_dhm_free( mbedtls_dhm_context *ctx ) |
JMF | 12:0071cb144c7a | 402 | { |
JMF | 12:0071cb144c7a | 403 | mbedtls_mpi_free( &ctx->pX); mbedtls_mpi_free( &ctx->Vf ); mbedtls_mpi_free( &ctx->Vi ); |
JMF | 12:0071cb144c7a | 404 | mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->K ); mbedtls_mpi_free( &ctx->GY ); |
JMF | 12:0071cb144c7a | 405 | mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X ); mbedtls_mpi_free( &ctx->G ); |
JMF | 12:0071cb144c7a | 406 | mbedtls_mpi_free( &ctx->P ); |
JMF | 12:0071cb144c7a | 407 | |
JMF | 12:0071cb144c7a | 408 | mbedtls_zeroize( ctx, sizeof( mbedtls_dhm_context ) ); |
JMF | 12:0071cb144c7a | 409 | } |
JMF | 12:0071cb144c7a | 410 | |
JMF | 12:0071cb144c7a | 411 | #if defined(MBEDTLS_ASN1_PARSE_C) |
JMF | 12:0071cb144c7a | 412 | /* |
JMF | 12:0071cb144c7a | 413 | * Parse DHM parameters |
JMF | 12:0071cb144c7a | 414 | */ |
JMF | 12:0071cb144c7a | 415 | int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin, |
JMF | 12:0071cb144c7a | 416 | size_t dhminlen ) |
JMF | 12:0071cb144c7a | 417 | { |
JMF | 12:0071cb144c7a | 418 | int ret; |
JMF | 12:0071cb144c7a | 419 | size_t len; |
JMF | 12:0071cb144c7a | 420 | unsigned char *p, *end; |
JMF | 12:0071cb144c7a | 421 | #if defined(MBEDTLS_PEM_PARSE_C) |
JMF | 12:0071cb144c7a | 422 | mbedtls_pem_context pem; |
JMF | 12:0071cb144c7a | 423 | |
JMF | 12:0071cb144c7a | 424 | mbedtls_pem_init( &pem ); |
JMF | 12:0071cb144c7a | 425 | |
JMF | 12:0071cb144c7a | 426 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
JMF | 12:0071cb144c7a | 427 | if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' ) |
JMF | 12:0071cb144c7a | 428 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
JMF | 12:0071cb144c7a | 429 | else |
JMF | 12:0071cb144c7a | 430 | ret = mbedtls_pem_read_buffer( &pem, |
JMF | 12:0071cb144c7a | 431 | "-----BEGIN DH PARAMETERS-----", |
JMF | 12:0071cb144c7a | 432 | "-----END DH PARAMETERS-----", |
JMF | 12:0071cb144c7a | 433 | dhmin, NULL, 0, &dhminlen ); |
JMF | 12:0071cb144c7a | 434 | |
JMF | 12:0071cb144c7a | 435 | if( ret == 0 ) |
JMF | 12:0071cb144c7a | 436 | { |
JMF | 12:0071cb144c7a | 437 | /* |
JMF | 12:0071cb144c7a | 438 | * Was PEM encoded |
JMF | 12:0071cb144c7a | 439 | */ |
JMF | 12:0071cb144c7a | 440 | dhminlen = pem.buflen; |
JMF | 12:0071cb144c7a | 441 | } |
JMF | 12:0071cb144c7a | 442 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
JMF | 12:0071cb144c7a | 443 | goto exit; |
JMF | 12:0071cb144c7a | 444 | |
JMF | 12:0071cb144c7a | 445 | p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin; |
JMF | 12:0071cb144c7a | 446 | #else |
JMF | 12:0071cb144c7a | 447 | p = (unsigned char *) dhmin; |
JMF | 12:0071cb144c7a | 448 | #endif /* MBEDTLS_PEM_PARSE_C */ |
JMF | 12:0071cb144c7a | 449 | end = p + dhminlen; |
JMF | 12:0071cb144c7a | 450 | |
JMF | 12:0071cb144c7a | 451 | /* |
JMF | 12:0071cb144c7a | 452 | * DHParams ::= SEQUENCE { |
JMF | 12:0071cb144c7a | 453 | * prime INTEGER, -- P |
JMF | 12:0071cb144c7a | 454 | * generator INTEGER, -- g |
JMF | 12:0071cb144c7a | 455 | * privateValueLength INTEGER OPTIONAL |
JMF | 12:0071cb144c7a | 456 | * } |
JMF | 12:0071cb144c7a | 457 | */ |
JMF | 12:0071cb144c7a | 458 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
JMF | 12:0071cb144c7a | 459 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
JMF | 12:0071cb144c7a | 460 | { |
JMF | 12:0071cb144c7a | 461 | ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret; |
JMF | 12:0071cb144c7a | 462 | goto exit; |
JMF | 12:0071cb144c7a | 463 | } |
JMF | 12:0071cb144c7a | 464 | |
JMF | 12:0071cb144c7a | 465 | end = p + len; |
JMF | 12:0071cb144c7a | 466 | |
JMF | 12:0071cb144c7a | 467 | if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 || |
JMF | 12:0071cb144c7a | 468 | ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 ) |
JMF | 12:0071cb144c7a | 469 | { |
JMF | 12:0071cb144c7a | 470 | ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret; |
JMF | 12:0071cb144c7a | 471 | goto exit; |
JMF | 12:0071cb144c7a | 472 | } |
JMF | 12:0071cb144c7a | 473 | |
JMF | 12:0071cb144c7a | 474 | if( p != end ) |
JMF | 12:0071cb144c7a | 475 | { |
JMF | 12:0071cb144c7a | 476 | /* This might be the optional privateValueLength. |
JMF | 12:0071cb144c7a | 477 | * If so, we can cleanly discard it */ |
JMF | 12:0071cb144c7a | 478 | mbedtls_mpi rec; |
JMF | 12:0071cb144c7a | 479 | mbedtls_mpi_init( &rec ); |
JMF | 12:0071cb144c7a | 480 | ret = mbedtls_asn1_get_mpi( &p, end, &rec ); |
JMF | 12:0071cb144c7a | 481 | mbedtls_mpi_free( &rec ); |
JMF | 12:0071cb144c7a | 482 | if ( ret != 0 ) |
JMF | 12:0071cb144c7a | 483 | { |
JMF | 12:0071cb144c7a | 484 | ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret; |
JMF | 12:0071cb144c7a | 485 | goto exit; |
JMF | 12:0071cb144c7a | 486 | } |
JMF | 12:0071cb144c7a | 487 | if ( p != end ) |
JMF | 12:0071cb144c7a | 488 | { |
JMF | 12:0071cb144c7a | 489 | ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + |
JMF | 12:0071cb144c7a | 490 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
JMF | 12:0071cb144c7a | 491 | goto exit; |
JMF | 12:0071cb144c7a | 492 | } |
JMF | 12:0071cb144c7a | 493 | } |
JMF | 12:0071cb144c7a | 494 | |
JMF | 12:0071cb144c7a | 495 | ret = 0; |
JMF | 12:0071cb144c7a | 496 | |
JMF | 12:0071cb144c7a | 497 | dhm->len = mbedtls_mpi_size( &dhm->P ); |
JMF | 12:0071cb144c7a | 498 | |
JMF | 12:0071cb144c7a | 499 | exit: |
JMF | 12:0071cb144c7a | 500 | #if defined(MBEDTLS_PEM_PARSE_C) |
JMF | 12:0071cb144c7a | 501 | mbedtls_pem_free( &pem ); |
JMF | 12:0071cb144c7a | 502 | #endif |
JMF | 12:0071cb144c7a | 503 | if( ret != 0 ) |
JMF | 12:0071cb144c7a | 504 | mbedtls_dhm_free( dhm ); |
JMF | 12:0071cb144c7a | 505 | |
JMF | 12:0071cb144c7a | 506 | return( ret ); |
JMF | 12:0071cb144c7a | 507 | } |
JMF | 12:0071cb144c7a | 508 | |
JMF | 12:0071cb144c7a | 509 | #if defined(MBEDTLS_FS_IO) |
JMF | 12:0071cb144c7a | 510 | /* |
JMF | 12:0071cb144c7a | 511 | * Load all data from a file into a given buffer. |
JMF | 12:0071cb144c7a | 512 | * |
JMF | 12:0071cb144c7a | 513 | * The file is expected to contain either PEM or DER encoded data. |
JMF | 12:0071cb144c7a | 514 | * A terminating null byte is always appended. It is included in the announced |
JMF | 12:0071cb144c7a | 515 | * length only if the data looks like it is PEM encoded. |
JMF | 12:0071cb144c7a | 516 | */ |
JMF | 12:0071cb144c7a | 517 | static int load_file( const char *path, unsigned char **buf, size_t *n ) |
JMF | 12:0071cb144c7a | 518 | { |
JMF | 12:0071cb144c7a | 519 | FILE *f; |
JMF | 12:0071cb144c7a | 520 | long size; |
JMF | 12:0071cb144c7a | 521 | |
JMF | 12:0071cb144c7a | 522 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
JMF | 12:0071cb144c7a | 523 | return( MBEDTLS_ERR_DHM_FILE_IO_ERROR ); |
JMF | 12:0071cb144c7a | 524 | |
JMF | 12:0071cb144c7a | 525 | fseek( f, 0, SEEK_END ); |
JMF | 12:0071cb144c7a | 526 | if( ( size = ftell( f ) ) == -1 ) |
JMF | 12:0071cb144c7a | 527 | { |
JMF | 12:0071cb144c7a | 528 | fclose( f ); |
JMF | 12:0071cb144c7a | 529 | return( MBEDTLS_ERR_DHM_FILE_IO_ERROR ); |
JMF | 12:0071cb144c7a | 530 | } |
JMF | 12:0071cb144c7a | 531 | fseek( f, 0, SEEK_SET ); |
JMF | 12:0071cb144c7a | 532 | |
JMF | 12:0071cb144c7a | 533 | *n = (size_t) size; |
JMF | 12:0071cb144c7a | 534 | |
JMF | 12:0071cb144c7a | 535 | if( *n + 1 == 0 || |
JMF | 12:0071cb144c7a | 536 | ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL ) |
JMF | 12:0071cb144c7a | 537 | { |
JMF | 12:0071cb144c7a | 538 | fclose( f ); |
JMF | 12:0071cb144c7a | 539 | return( MBEDTLS_ERR_DHM_ALLOC_FAILED ); |
JMF | 12:0071cb144c7a | 540 | } |
JMF | 12:0071cb144c7a | 541 | |
JMF | 12:0071cb144c7a | 542 | if( fread( *buf, 1, *n, f ) != *n ) |
JMF | 12:0071cb144c7a | 543 | { |
JMF | 12:0071cb144c7a | 544 | fclose( f ); |
JMF | 12:0071cb144c7a | 545 | mbedtls_free( *buf ); |
JMF | 12:0071cb144c7a | 546 | return( MBEDTLS_ERR_DHM_FILE_IO_ERROR ); |
JMF | 12:0071cb144c7a | 547 | } |
JMF | 12:0071cb144c7a | 548 | |
JMF | 12:0071cb144c7a | 549 | fclose( f ); |
JMF | 12:0071cb144c7a | 550 | |
JMF | 12:0071cb144c7a | 551 | (*buf)[*n] = '\0'; |
JMF | 12:0071cb144c7a | 552 | |
JMF | 12:0071cb144c7a | 553 | if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL ) |
JMF | 12:0071cb144c7a | 554 | ++*n; |
JMF | 12:0071cb144c7a | 555 | |
JMF | 12:0071cb144c7a | 556 | return( 0 ); |
JMF | 12:0071cb144c7a | 557 | } |
JMF | 12:0071cb144c7a | 558 | |
JMF | 12:0071cb144c7a | 559 | /* |
JMF | 12:0071cb144c7a | 560 | * Load and parse DHM parameters |
JMF | 12:0071cb144c7a | 561 | */ |
JMF | 12:0071cb144c7a | 562 | int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path ) |
JMF | 12:0071cb144c7a | 563 | { |
JMF | 12:0071cb144c7a | 564 | int ret; |
JMF | 12:0071cb144c7a | 565 | size_t n; |
JMF | 12:0071cb144c7a | 566 | unsigned char *buf; |
JMF | 12:0071cb144c7a | 567 | |
JMF | 12:0071cb144c7a | 568 | if( ( ret = load_file( path, &buf, &n ) ) != 0 ) |
JMF | 12:0071cb144c7a | 569 | return( ret ); |
JMF | 12:0071cb144c7a | 570 | |
JMF | 12:0071cb144c7a | 571 | ret = mbedtls_dhm_parse_dhm( dhm, buf, n ); |
JMF | 12:0071cb144c7a | 572 | |
JMF | 12:0071cb144c7a | 573 | mbedtls_zeroize( buf, n ); |
JMF | 12:0071cb144c7a | 574 | mbedtls_free( buf ); |
JMF | 12:0071cb144c7a | 575 | |
JMF | 12:0071cb144c7a | 576 | return( ret ); |
JMF | 12:0071cb144c7a | 577 | } |
JMF | 12:0071cb144c7a | 578 | #endif /* MBEDTLS_FS_IO */ |
JMF | 12:0071cb144c7a | 579 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
JMF | 12:0071cb144c7a | 580 | |
JMF | 12:0071cb144c7a | 581 | #if defined(MBEDTLS_SELF_TEST) |
JMF | 12:0071cb144c7a | 582 | |
JMF | 12:0071cb144c7a | 583 | static const char mbedtls_test_dhm_params[] = |
JMF | 12:0071cb144c7a | 584 | "-----BEGIN DH PARAMETERS-----\r\n" |
JMF | 12:0071cb144c7a | 585 | "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n" |
JMF | 12:0071cb144c7a | 586 | "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n" |
JMF | 12:0071cb144c7a | 587 | "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n" |
JMF | 12:0071cb144c7a | 588 | "-----END DH PARAMETERS-----\r\n"; |
JMF | 12:0071cb144c7a | 589 | |
JMF | 12:0071cb144c7a | 590 | static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params ); |
JMF | 12:0071cb144c7a | 591 | |
JMF | 12:0071cb144c7a | 592 | /* |
JMF | 12:0071cb144c7a | 593 | * Checkup routine |
JMF | 12:0071cb144c7a | 594 | */ |
JMF | 12:0071cb144c7a | 595 | int mbedtls_dhm_self_test( int verbose ) |
JMF | 12:0071cb144c7a | 596 | { |
JMF | 12:0071cb144c7a | 597 | int ret; |
JMF | 12:0071cb144c7a | 598 | mbedtls_dhm_context dhm; |
JMF | 12:0071cb144c7a | 599 | |
JMF | 12:0071cb144c7a | 600 | mbedtls_dhm_init( &dhm ); |
JMF | 12:0071cb144c7a | 601 | |
JMF | 12:0071cb144c7a | 602 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 603 | mbedtls_printf( " DHM parameter load: " ); |
JMF | 12:0071cb144c7a | 604 | |
JMF | 12:0071cb144c7a | 605 | if( ( ret = mbedtls_dhm_parse_dhm( &dhm, |
JMF | 12:0071cb144c7a | 606 | (const unsigned char *) mbedtls_test_dhm_params, |
JMF | 12:0071cb144c7a | 607 | mbedtls_test_dhm_params_len ) ) != 0 ) |
JMF | 12:0071cb144c7a | 608 | { |
JMF | 12:0071cb144c7a | 609 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 610 | mbedtls_printf( "failed\n" ); |
JMF | 12:0071cb144c7a | 611 | |
JMF | 12:0071cb144c7a | 612 | ret = 1; |
JMF | 12:0071cb144c7a | 613 | goto exit; |
JMF | 12:0071cb144c7a | 614 | } |
JMF | 12:0071cb144c7a | 615 | |
JMF | 12:0071cb144c7a | 616 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 617 | mbedtls_printf( "passed\n\n" ); |
JMF | 12:0071cb144c7a | 618 | |
JMF | 12:0071cb144c7a | 619 | exit: |
JMF | 12:0071cb144c7a | 620 | mbedtls_dhm_free( &dhm ); |
JMF | 12:0071cb144c7a | 621 | |
JMF | 12:0071cb144c7a | 622 | return( ret ); |
JMF | 12:0071cb144c7a | 623 | } |
JMF | 12:0071cb144c7a | 624 | |
JMF | 12:0071cb144c7a | 625 | #endif /* MBEDTLS_SELF_TEST */ |
JMF | 12:0071cb144c7a | 626 | |
JMF | 12:0071cb144c7a | 627 | #endif /* MBEDTLS_DHM_C */ |