Saturday, May 8, 2010

NIS HMAC Implementation

#include
#include
#include

#define SIZE 64

typedef unsigned char uchar ;
int main ( int argc, char * argv[] )
{
FILE * kfp ;
FILE * ifp ;
FILE * ofp ;
FILE * tfp ;
uchar c[2] ;
uchar ch;
uchar key[SIZE] = { 0x00 } ;
uchar ipad[SIZE] ;
uchar opad[SIZE] ;
uchar command[100] ;
int i;

if ( 4 != argc )
{
printf( "Incorrect Usage!\n" ) ;
printf( "Correct Usage is ./hmac " ) ;
}
for ( i = 0; i < SIZE; i++ )
{
ipad[i] = 0x36 ;
opad[i] = 0x5C ;
}


kfp = fopen( argv[1], "r+b" ) ;
ofp = fopen( argv[3], "w+b" ) ;
tfp = fopen( "temp", "w+b" ) ;

fread( key, sizeof( uchar ), SIZE, kfp ) ;
fclose ( kfp );

for ( i = 0; i < SIZE; i++ )
{
key[i] = key[i] ^ ipad[i] ;
}

fwrite( key, sizeof( uchar ), SIZE, tfp ) ;
fclose( tfp ) ;

sprintf( command, "cat %s >> temp", argv[2]) ;
system( command ) ;
system( "./md5 temp > temp1" ) ;

kfp = fopen( argv[1], "r+b" ) ;
fread( key, sizeof( uchar ), SIZE, kfp ) ;
fclose ( kfp );
for ( i = 0; i < SIZE; i++ )
{
key[i] = key[i] ^ opad[i] ;
fwrite( &key[i], sizeof( uchar ), 1, ofp ) ;
}

tfp = fopen( "temp1", "r+b" ) ;
for ( i = 0; i < 16; i++ )
{
fread( c, sizeof( uchar ), 2, tfp );
c[0] -= '0' ;
if ( c[0] > 9 )
{
c[0] = c[0] + '0' - 'a' + 10 ;
}

c[1] -= '0' ;
if ( c[1] > 9 )
{
c[1] = c[1] + '0' - 'a' + 10 ;
}
ch = ( c[0] << 4 ) | c[1] ;
fwrite( &ch, sizeof( uchar ), 1, ofp ) ;
}
fclose( ofp ) ;

sprintf( command, "./md5 %s > temp", argv[3] ) ;
system( command ) ;
system( "cat temp" ) ;
printf( "\n" ) ;

return 0;
}

//OUTPUT:

root@root:~$ ./a.out key.txt hmac.c output.txt
7d1ef72ee877ec646f700288e0693462


Kaeyfile contents:
MHCVGFDC&^$$#@%^MHCVGFDC&^$$#@%^MHCVGFDC&^$$#@%^MHCVGFDC&^$$#@%^

No comments: