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