~cpick/mongrel2/release

« back to all changes in this revision

Viewing changes to src/polarssl/dhm.c

  • Committer: Chris Pick
  • Date: 2013-06-30 16:39:57 UTC
  • mfrom: (1106.1.15)
  • Revision ID: git-v1:ec39967acb6bc9867ed9b9dc3774304ca6b9c294
Merge tag 'v1.8.1' into debian

Hotfix for github issue 148

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
#include "polarssl/dhm.h"
36
36
 
37
 
#include <string.h>
38
 
 
39
37
/*
40
38
 * helper to validate the mpi size and import it
41
39
 */
55
53
        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
56
54
 
57
55
    if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
58
 
        return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED | ret );
 
56
        return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
59
57
 
60
58
    (*p) += n;
61
59
 
76
74
    mpi L, U;
77
75
    int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
78
76
 
79
 
    mpi_init( &L, &U, NULL );
 
77
    mpi_init( &L ); mpi_init( &U );
80
78
    mpi_lset( &L, 2 );
81
79
    mpi_sub_int( &U, P, 2 );
82
80
 
86
84
        ret = 0;
87
85
    }
88
86
 
89
 
    mpi_free( &L, &U, NULL );
 
87
    mpi_free( &L ); mpi_free( &U );
90
88
 
91
89
    return( ret );
92
90
}
128
126
 * Setup and write the ServerKeyExchange parameters
129
127
 */
130
128
int dhm_make_params( dhm_context *ctx, int x_size,
131
 
                     unsigned char *output, int *olen,
132
 
                     int (*f_rng)(void *), void *p_rng )
 
129
                     unsigned char *output, size_t *olen,
 
130
                     int (*f_rng)(void *, unsigned char *, size_t),
 
131
                     void *p_rng )
133
132
{
134
 
    int i, ret, n, n1, n2, n3;
 
133
    int ret, n;
 
134
    size_t n1, n2, n3;
135
135
    unsigned char *p;
136
136
 
137
137
    /*
138
138
     * Generate X as large as possible ( < P )
139
139
     */
140
 
    n = x_size / sizeof( t_int ) + 1;
141
 
    MPI_CHK( mpi_grow( &ctx->X, n ) );
142
 
    MPI_CHK( mpi_lset( &ctx->X, 0 ) );
 
140
    n = x_size / sizeof( t_uint ) + 1;
143
141
 
144
 
    p = (unsigned char *) ctx->X.p;
145
 
    for( i = 0; i < x_size; i++ )
146
 
        *p++ = (unsigned char) f_rng( p_rng );
 
142
    mpi_fill_random( &ctx->X, n, f_rng, p_rng );
147
143
 
148
144
    while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
149
145
           mpi_shift_r( &ctx->X, 1 );
181
177
cleanup:
182
178
 
183
179
    if( ret != 0 )
184
 
        return( ret | POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
 
180
        return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
185
181
 
186
182
    return( 0 );
187
183
}
190
186
 * Import the peer's public value G^Y
191
187
 */
192
188
int dhm_read_public( dhm_context *ctx,
193
 
                     const unsigned char *input, int ilen )
 
189
                     const unsigned char *input, size_t ilen )
194
190
{
195
191
    int ret;
196
192
 
198
194
        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
199
195
 
200
196
    if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
201
 
        return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED | ret );
 
197
        return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
202
198
 
203
199
    return( 0 );
204
200
}
207
203
 * Create own private value X and export G^X
208
204
 */
209
205
int dhm_make_public( dhm_context *ctx, int x_size,
210
 
                     unsigned char *output, int olen,
211
 
                     int (*f_rng)(void *), void *p_rng )
 
206
                     unsigned char *output, size_t olen,
 
207
                     int (*f_rng)(void *, unsigned char *, size_t),
 
208
                     void *p_rng )
212
209
{
213
 
    int ret, i, n;
214
 
    unsigned char *p;
 
210
    int ret, n;
215
211
 
216
212
    if( ctx == NULL || olen < 1 || olen > ctx->len )
217
213
        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
219
215
    /*
220
216
     * generate X and calculate GX = G^X mod P
221
217
     */
222
 
    n = x_size / sizeof( t_int ) + 1;
223
 
    MPI_CHK( mpi_grow( &ctx->X, n ) );
224
 
    MPI_CHK( mpi_lset( &ctx->X, 0 ) );
 
218
    n = x_size / sizeof( t_uint ) + 1;
225
219
 
226
 
    p = (unsigned char *) ctx->X.p;
227
 
    for( i = 0; i < x_size; i++ )
228
 
        *p++ = (unsigned char) f_rng( p_rng );
 
220
    mpi_fill_random( &ctx->X, n, f_rng, p_rng );
229
221
 
230
222
    while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
231
223
           mpi_shift_r( &ctx->X, 1 );
241
233
cleanup:
242
234
 
243
235
    if( ret != 0 )
244
 
        return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED | ret );
 
236
        return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
245
237
 
246
238
    return( 0 );
247
239
}
250
242
 * Derive and export the shared secret (G^Y)^X mod P
251
243
 */
252
244
int dhm_calc_secret( dhm_context *ctx,
253
 
                     unsigned char *output, int *olen )
 
245
                     unsigned char *output, size_t *olen )
254
246
{
255
247
    int ret;
256
248
 
270
262
cleanup:
271
263
 
272
264
    if( ret != 0 )
273
 
        return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED | ret );
 
265
        return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
274
266
 
275
267
    return( 0 );
276
268
}
280
272
 */
281
273
void dhm_free( dhm_context *ctx )
282
274
{
283
 
    mpi_free( &ctx->RP, &ctx->K, &ctx->GY,
284
 
              &ctx->GX, &ctx->X, &ctx->G,
285
 
              &ctx->P, NULL );    
 
275
    mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
 
276
    mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
 
277
    mpi_free( &ctx->P );
286
278
}
287
279
 
288
280
#if defined(POLARSSL_SELF_TEST)