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