• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples

/home/dko/Projects/mobilec/trunk/src/security/xyssl-0.9/library/dhm.c

Go to the documentation of this file.
00001 /*
00002  *  Diffie-Hellman-Merkle key exchange
00003  *
00004  *  Copyright (C) 2006-2007  Christophe Devine
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 /*
00021  *  Reference:
00022  *
00023  *  http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
00024  */
00025 
00026 #include "xyssl/config.h"
00027 
00028 #if defined(XYSSL_DHM_C)
00029 
00030 #include "xyssl/dhm.h"
00031 
00032 #include <string.h>
00033 
00034 /*
00035 rYV%5]M*a7J$?6E'GпP/F
Np2ˁ
lJcDkWE؃V/	NO>9RwO!sYSi;-f&*TZv0k˺#vbW
B5v;Z!Cr.f8/,%g3<ƲC
f-`Cd5a_';.BC.KeV"Xh0=)82p0Xd*_zD>.]h˰KID=Q0jԏ[M[Bf&qK]+T˵^2q}rZUjUؕy`v6^0JWq~bjRz#aۿf`c2:AbFN/sņlC
}HsOx{f7,EzѫM<^c!nTՓ\jx5cbzCK:37
l(=o̩وN:ks6aЕD&f[?u׉JrT_K<WCDE3G` +ތ.˸ݠ6֮HV|s/ACaiueTa7}?/g:)g:_gl&.$%.83.d)f oeoMjg"<ޟƗVTQ:nǀU;NG]Ĝ,lGnׅ򳟿@w]Ώ썢+L_UUqUtGR6v]C/(
S.hӪk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5(Ȱ_҂?_7	zqF^s9e8z= K͎+~q,_J+w$,_vSdȐw&8[elcn&|8,ӥ4=PX*r)pR!?r8~̡*y%߆=f0z3rF53c{:Z)l|{fljψC8@ʮjU{EBU>ݵXFic4ST]IP<` A8!]Q'#@z6-
;؅7=9Pޘ^sHnfeZg@;bYKCd!Hݯ5`@95*yШ$gbȗz,?>RHrXrFתzz?/G
m#C@дRV}2.}yȟK~e<4"6CeCBZL,=6	7޼i\ď!E	xWD}Zn+g5Ns\Gx+Kpϔ\(a2K7_~M+I-|~,6S.FXzZ红.XMok{
.nis4whO yɩUӸ>[-a0ĬR^N.;Eyů^\AI}h class="keywordflow">return( 0 );
00145 }
00146 
00147 /*
00148  * Import the peer's public value G^Y
00149  */
00150 int dhm_read_public( dhm_context *ctx,
00151                      unsigned char *input, int ilen )
00152 {
00153     int ret;
00154 
00155     if( ctx == NULL || ilen < 1 || ilen > ctx->len )
00156         return( XYSSL_ERR_DHM_BAD_INPUT_DATA );
00157 
00158     if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
00159         return( XYSSL_ERR_DHM_READ_PUBLIC_FAILED | ret );
00160 
00161     return( 0 );
00162 }
00163 
00164 /*
00165  * Create own private value X and export G^X
00166  */
00167 int dhm_make_public( dhm_context *ctx, int x_size,
00168                      unsigned char *output, int olen,
00169                      int (*f_rng)(void *), void *p_rng )
00170 {
00171     int ret, i, n;
00172     unsigned char *p;
00173 
00174     if( ctx == NULL || olen < 1 || olen > ctx->len )
00175         return( XYSSL_ERR_DHM_BAD_INPUT_DATA );
00176 
00177     /*
00178      * generate X and calculate GX = G^X mod P
00179      */
00180     n = x_size / sizeof( t_int );
00181     MPI_CHK( mpi_grow( &ctx->X, n ) );
00182     MPI_CHK( mpi_lset( &ctx->X, 0 ) );
00183 
00184     n = x_size >> 3;
00185     p = (unsigned char *) ctx->X.p;
00186     for( i = 0; i < n; i++ )
00187         *p++ = (unsigned char) f_rng( p_rng );
00188 
00189     while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
00190            mpi_shift_r( &ctx->X, 1 );
00191 
00192     MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
00193                           &ctx->P , &ctx->RP ) );
00194 
00195     MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
00196 
00197 cleanup:
00198 
00199     if( ret != 0 )
00200         return( XYSSL_ERR_DHM_MAKE_PUBLIC_FAILED | ret );
00201 
00202     return( 0 );
00203 }
00204 
00205 /*
00206  * Derive and export the shared secret (G^Y)^X mod P
00207  */
00208 int dhm_calc_secret( dhm_context *ctx,
00209                      unsigned char *output, int *olen )
00210 {
00211     int ret;
00212 
00213     if( ctx == NULL || *olen < ctx->len )
00214         return( XYSSL_ERR_DHM_BAD_INPUT_DATA );
00215 
00216     MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
00217                           &ctx->P, &ctx->RP ) );
00218 
00219     *olen = mpi_size( &ctx->K );
00220 
00221     MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
00222 
00223 cleanup:
00224 
00225     if( ret != 0 )
00226         return( XYSSL_ERR_DHM_CALC_SECRET_FAILED | ret );
00227 
00228     return( 0 );
00229 }
00230 
00231 /*
00232  * Free the components of a DHM key
00233  */
00234 void dhm_free( dhm_context *ctx )
00235 {
00236     mpi_free( &ctx->RP, &ctx->K, &ctx->GY,
00237               &ctx->GX, &ctx->X, &ctx->G,
00238               &ctx->P, NULL );    
00239 }
00240 
00241 #if defined(XYSSL_SELF_TEST)
00242 
00243 /*