source: trunk/programs/pkey/dh_genprime.c @ 1143

Revision 1143, 4.2 KB checked in by paul, 6 months ago (diff)
  • Moved all examples programs to use the new entropy and CTR_DRBG
Line 
1/*
2 *  Diffie-Hellman-Merkle key exchange (prime generation)
3 *
4 *  Copyright (C) 2006-2011, Brainspark B.V.
5 *
6 *  This file is part of PolarSSL (http://www.polarssl.org)
7 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 *  All rights reserved.
10 *
11 *  This program is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  This program is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License along
22 *  with this program; if not, write to the Free Software Foundation, Inc.,
23 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <stdio.h>
31
32#include "polarssl/config.h"
33
34#include "polarssl/bignum.h"
35#include "polarssl/entropy.h"
36#include "polarssl/ctr_drbg.h"
37
38/*
39 * Note: G = 4 is always a quadratic residue mod P,
40 * so it is a generator of order Q (with P = 2*Q+1).
41 */
42#define DH_P_SIZE 1024
43#define GENERATOR "4"
44
45#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) ||   \
46    !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
47int main( int argc, char *argv[] )
48{
49    ((void) argc);
50    ((void) argv);
51
52    printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
53           "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
54    return( 0 );
55}
56#else
57int main( int argc, char *argv[] )
58{
59    int ret = 1;
60
61#if defined(POLARSSL_GENPRIME)
62    mpi G, P, Q;
63    entropy_context entropy;
64    ctr_drbg_context ctr_drbg;
65    char *pers = "dh_genprime";
66    FILE *fout;
67
68    ((void) argc);
69    ((void) argv);
70
71    mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
72    mpi_read_string( &G, 10, GENERATOR );
73
74    printf( "\n  . Seeding the random number generator..." );
75    fflush( stdout );
76
77    entropy_init( &entropy );
78    if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
79                               (unsigned char *) pers, strlen( pers ) ) ) != 0 )
80    {
81        printf( " failed\n  ! ctr_drbg_init returned %d\n", ret );
82        goto exit;
83    }
84
85    printf( " ok\n  . Generating the modulus, please wait..." );
86    fflush( stdout );
87
88    /*
89     * This can take a long time...
90     */
91    if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
92                               ctr_drbg_random, &ctr_drbg ) ) != 0 )
93    {
94        printf( " failed\n  ! mpi_gen_prime returned %d\n\n", ret );
95        goto exit;
96    }
97
98    printf( " ok\n  . Verifying that Q = (P-1)/2 is prime..." );
99    fflush( stdout );
100
101    if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
102    {
103        printf( " failed\n  ! mpi_sub_int returned %d\n\n", ret );
104        goto exit;
105    }
106
107    if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
108    {
109        printf( " failed\n  ! mpi_div_int returned %d\n\n", ret );
110        goto exit;
111    }
112
113    if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 )
114    {
115        printf( " failed\n  ! mpi_is_prime returned %d\n\n", ret );
116        goto exit;
117    }
118
119    printf( " ok\n  . Exporting the value in dh_prime.txt..." );
120    fflush( stdout );
121
122    if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
123    {
124        ret = 1;
125        printf( " failed\n  ! Could not create dh_prime.txt\n\n" );
126        goto exit;
127    }
128
129    if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
130        ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
131    {
132        printf( " failed\n  ! mpi_write_file returned %d\n\n", ret );
133        goto exit;
134    }
135
136    printf( " ok\n\n" );
137    fclose( fout );
138
139exit:
140
141    mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
142#else
143    printf( "\n  ! Prime-number generation is not available.\n\n" );
144#endif
145
146#if defined(_WIN32)
147    printf( "  Press Enter to exit this program.\n" );
148    fflush( stdout ); getchar();
149#endif
150
151    return( ret );
152}
153#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO &&
154          POLARSSL_CTR_DRBG_C */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?