Advertisement
Alberts00

SHATEST | main.c

Jan 4th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.23 KB | None | 0 0
  1. /* encryptedPassword ="$SHA$" + salt + "$" + sha256(sha256(password) + salt)
  2.  * Input
  3.  * SHATEST.exe dbdump.txt twitter-banned.txt twitter-banned.txt.res
  4.  * 1. File containing Username:Salt:SHA256 of password
  5.  * e.g creeperdaddy:633d06d3d95c64ac:349b5849410e88a67ce4fbd938b9e6f8ba53bd9a5167d9787cbfa2eeb520a1d1
  6.  * 2. Dictionary
  7.  * e.g twitter-banned.txt
  8.  * 3. Output file
  9.  *  
  10.  *  FIPS-180-2 compliant SHA-256 implementation
  11.  *
  12.  *  Copyright (C) 2001-2003  Christophe Devine
  13.  *
  14.  *  This program is free software; you can redistribute it and/or modify
  15.  *  it under the terms of the GNU General Public License as published by
  16.  *  the Free Software Foundation; either version 2 of the License, or
  17.  *  (at your option) any later version.
  18.  *
  19.  *  This program is distributed in the hope that it will be useful,
  20.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  *  GNU General Public License for more details.
  23.  *
  24.  *  You should have received a copy of the GNU General Public License
  25.  *  along with this program; if not, write to the Free Software
  26.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  27.  */
  28.  
  29. #include <string.h>
  30. #include "sha256.h"
  31. #define GET_UINT32(n,b,i)                       \
  32. {                                               \
  33.     (n) = ( (uint32) (b)[(i)    ] << 24 )       \
  34.         | ( (uint32) (b)[(i) + 1] << 16 )       \
  35.         | ( (uint32) (b)[(i) + 2] <<  8 )       \
  36.         | ( (uint32) (b)[(i) + 3]       );      \
  37. }
  38.  
  39. #define PUT_UINT32(n,b,i)                       \
  40. {                                               \
  41.     (b)[(i)    ] = (uint8) ( (n) >> 24 );       \
  42.     (b)[(i) + 1] = (uint8) ( (n) >> 16 );       \
  43.     (b)[(i) + 2] = (uint8) ( (n) >>  8 );       \
  44.     (b)[(i) + 3] = (uint8) ( (n)       );       \
  45. }
  46.  
  47. void sha256_starts( sha256_context *ctx )
  48. {
  49.     ctx->total[0] = 0;
  50.     ctx->total[1] = 0;
  51.  
  52.     ctx->state[0] = 0x6A09E667;
  53.     ctx->state[1] = 0xBB67AE85;
  54.     ctx->state[2] = 0x3C6EF372;
  55.     ctx->state[3] = 0xA54FF53A;
  56.     ctx->state[4] = 0x510E527F;
  57.     ctx->state[5] = 0x9B05688C;
  58.     ctx->state[6] = 0x1F83D9AB;
  59.     ctx->state[7] = 0x5BE0CD19;
  60. }
  61.  
  62. void sha256_process( sha256_context *ctx, uint8 data[64] )
  63. {
  64.     uint32 temp1, temp2, W[64];
  65.     uint32 A, B, C, D, E, F, G, H;
  66.  
  67.     GET_UINT32( W[0],  data,  0 );
  68.     GET_UINT32( W[1],  data,  4 );
  69.     GET_UINT32( W[2],  data,  8 );
  70.     GET_UINT32( W[3],  data, 12 );
  71.     GET_UINT32( W[4],  data, 16 );
  72.     GET_UINT32( W[5],  data, 20 );
  73.     GET_UINT32( W[6],  data, 24 );
  74.     GET_UINT32( W[7],  data, 28 );
  75.     GET_UINT32( W[8],  data, 32 );
  76.     GET_UINT32( W[9],  data, 36 );
  77.     GET_UINT32( W[10], data, 40 );
  78.     GET_UINT32( W[11], data, 44 );
  79.     GET_UINT32( W[12], data, 48 );
  80.     GET_UINT32( W[13], data, 52 );
  81.     GET_UINT32( W[14], data, 56 );
  82.     GET_UINT32( W[15], data, 60 );
  83.  
  84. #define  SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  85. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  86.  
  87. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^  SHR(x, 3))
  88. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^  SHR(x,10))
  89.  
  90. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  91. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  92.  
  93. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  94. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  95.  
  96. #define R(t)                                    \
  97. (                                               \
  98.     W[t] = S1(W[t -  2]) + W[t -  7] +          \
  99.            S0(W[t - 15]) + W[t - 16]            \
  100. )
  101.  
  102. #define P(a,b,c,d,e,f,g,h,x,K)                  \
  103. {                                               \
  104.     temp1 = h + S3(e) + F1(e,f,g) + K + x;      \
  105.     temp2 = S2(a) + F0(a,b,c);                  \
  106.     d += temp1; h = temp1 + temp2;              \
  107. }
  108.  
  109.     A = ctx->state[0];
  110.     B = ctx->state[1];
  111.     C = ctx->state[2];
  112.     D = ctx->state[3];
  113.     E = ctx->state[4];
  114.     F = ctx->state[5];
  115.     G = ctx->state[6];
  116.     H = ctx->state[7];
  117.  
  118.     P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
  119.     P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
  120.     P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
  121.     P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
  122.     P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
  123.     P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
  124.     P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
  125.     P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
  126.     P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
  127.     P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
  128.     P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
  129.     P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
  130.     P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
  131.     P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
  132.     P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
  133.     P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
  134.     P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
  135.     P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
  136.     P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
  137.     P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
  138.     P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
  139.     P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
  140.     P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
  141.     P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
  142.     P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
  143.     P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
  144.     P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
  145.     P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
  146.     P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
  147.     P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
  148.     P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
  149.     P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
  150.     P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
  151.     P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
  152.     P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
  153.     P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
  154.     P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
  155.     P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
  156.     P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
  157.     P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
  158.     P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
  159.     P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
  160.     P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
  161.     P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
  162.     P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
  163.     P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
  164.     P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
  165.     P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
  166.     P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
  167.     P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
  168.     P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
  169.     P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
  170.     P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
  171.     P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
  172.     P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
  173.     P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
  174.     P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
  175.     P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
  176.     P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
  177.     P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
  178.     P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
  179.     P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
  180.     P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
  181.     P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
  182.  
  183.     ctx->state[0] += A;
  184.     ctx->state[1] += B;
  185.     ctx->state[2] += C;
  186.     ctx->state[3] += D;
  187.     ctx->state[4] += E;
  188.     ctx->state[5] += F;
  189.     ctx->state[6] += G;
  190.     ctx->state[7] += H;
  191. }
  192.  
  193. void sha256_update( sha256_context *ctx, uint8 *input, uint32 length )
  194. {
  195.     uint32 left, fill;
  196.  
  197.     if( ! length ) return;
  198.  
  199.     left = ctx->total[0] & 0x3F;
  200.     fill = 64 - left;
  201.  
  202.     ctx->total[0] += length;
  203.     ctx->total[0] &= 0xFFFFFFFF;
  204.  
  205.     if( ctx->total[0] < length )
  206.         ctx->total[1]++;
  207.  
  208.     if( left && length >= fill )
  209.     {
  210.         memcpy( (void *) (ctx->buffer + left),
  211.                 (void *) input, fill );
  212.         sha256_process( ctx, ctx->buffer );
  213.         length -= fill;
  214.         input  += fill;
  215.         left = 0;
  216.     }
  217.  
  218.     while( length >= 64 )
  219.     {
  220.         sha256_process( ctx, input );
  221.         length -= 64;
  222.         input  += 64;
  223.     }
  224.  
  225.     if( length )
  226.     {
  227.         memcpy( (void *) (ctx->buffer + left),
  228.                 (void *) input, length );
  229.     }
  230. }
  231.  
  232. static uint8 sha256_padding[64] =
  233. {
  234.  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  235.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  236.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  237.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  238. };
  239.  
  240. void sha256_finish( sha256_context *ctx, uint8 digest[32] )
  241. {
  242.     uint32 last, padn;
  243.     uint32 high, low;
  244.     uint8 msglen[8];
  245.  
  246.     high = ( ctx->total[0] >> 29 )
  247.          | ( ctx->total[1] <<  3 );
  248.     low  = ( ctx->total[0] <<  3 );
  249.  
  250.     PUT_UINT32( high, msglen, 0 );
  251.     PUT_UINT32( low,  msglen, 4 );
  252.  
  253.     last = ctx->total[0] & 0x3F;
  254.     padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  255.  
  256.     sha256_update( ctx, sha256_padding, padn );
  257.     sha256_update( ctx, msglen, 8 );
  258.  
  259.     PUT_UINT32( ctx->state[0], digest,  0 );
  260.     PUT_UINT32( ctx->state[1], digest,  4 );
  261.     PUT_UINT32( ctx->state[2], digest,  8 );
  262.     PUT_UINT32( ctx->state[3], digest, 12 );
  263.     PUT_UINT32( ctx->state[4], digest, 16 );
  264.     PUT_UINT32( ctx->state[5], digest, 20 );
  265.     PUT_UINT32( ctx->state[6], digest, 24 );
  266.     PUT_UINT32( ctx->state[7], digest, 28 );
  267. }
  268.  
  269. #include <stdlib.h>
  270. #include <stdio.h>
  271.  
  272. /*
  273.  * those are the standard FIPS-180-2 test vectors
  274.  */
  275.  
  276. static char *msg[] =
  277. {
  278.     "abc",
  279.     "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  280.     NULL
  281. };
  282.  
  283. static char *val[] =
  284. {
  285.     "ba7816bf8f01cfea414140de5dae2223" \
  286.     "b00361a396177a9cb410ff61f20015ad",
  287.     "248d6a61d20638b8e5c026930c3e6039" \
  288.     "a33ce45964ff2167f6ecedd419db06c1",
  289.     "cdc76e5c9914fb9281a1c7e284d73e67" \
  290.     "f1809a48a497200e046d39ccc7112cd0"
  291. };
  292.  
  293. int main( int argc, char *argv[] )
  294. {
  295.     FILE *f,*fpass,*fuser,*fres;
  296.    
  297.     int i, j;
  298.     unsigned char output[65];
  299.     sha256_context ctx;
  300.     unsigned char buf[1000], cpass[100], cpasshash[65];
  301.     unsigned char sha256sum[32];
  302.     unsigned char sha256sum40[80];
  303.     unsigned char cuser[100],cseed[17],chash[65];
  304.  
  305.     if( argc < 2 )
  306.     {
  307.         printf( "\n SHA-256 Validation Tests:\n\n" );
  308.  
  309.         for( i = 0; i < 3; i++ )
  310.         {
  311.             printf( " Test %d ", i + 1 );
  312.  
  313.             sha256_starts( &ctx );
  314.  
  315.             if( i < 2 )
  316.             {
  317.                 sha256_update( &ctx, (uint8 *) msg[i],
  318.                                strlen( msg[i] ) );
  319.             }
  320.             else
  321.             {
  322.                 memset( buf, 'a', 1000 );
  323.  
  324.                 for( j = 0; j < 1000; j++ )
  325.                 {
  326.                     sha256_update( &ctx, (uint8 *) buf, 1000 );
  327.                 }
  328.             }
  329.  
  330.             sha256_finish( &ctx, sha256sum );
  331.  
  332.             for( j = 0; j < 32; j++ )
  333.             {
  334.                 sprintf( output + j * 2, "%02x", sha256sum[j] );
  335.             }
  336.  
  337.             if( memcmp( output, val[i], 64 ) )
  338.             {
  339.                 printf( "failed!\n" );
  340.                 return( 1 );
  341.             }
  342.  
  343.             printf( "passed.\n" );
  344.         }
  345.  
  346.         printf( "\n" );
  347.     }
  348.     else if ( argc < 3 )
  349.     {
  350.         if( ! ( f = fopen( argv[1], "rb" ) ) )
  351.         {
  352.             perror( "fopen" );
  353.             return( 1 );
  354.         }
  355.  
  356.         sha256_starts( &ctx );
  357.  
  358.         while( ( i = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  359.         {
  360.             sha256_update( &ctx, buf, i );
  361.         }
  362.  
  363.         sha256_finish( &ctx, sha256sum );
  364.  
  365.         for( j = 0; j < 32; j++ )
  366.         {
  367.             printf( "%02x", sha256sum[j] );
  368.         }
  369.  
  370.         printf( "  %s\n", argv[1] );
  371.     }
  372.     else
  373.     {
  374.         if( ! ( fuser = fopen( argv[1], "rt" ) ) )
  375.         {
  376.             perror( "fopen user file" );
  377.             return( 1 );
  378.         }
  379.         if( ! ( fpass = fopen( argv[2], "rt+" ) ) )
  380.         {
  381.             perror( "fopen pass file" );
  382.             return( 1 );
  383.         }
  384.         if( ! ( fres = fopen( argv[3], "wt" ) ) )
  385.         {
  386.             perror( "fopen result file" );
  387.             return( 1 );
  388.         }
  389.         while( fgets(cpass,100,fpass) != NULL ){
  390.             if (cpass[strlen(cpass) - 1] == '\n') cpass[strlen(cpass) - 1] = '\0';
  391.             sha256_starts( &ctx );
  392.             sha256_update( &ctx, cpass, strlen(cpass) );
  393.             sha256_finish( &ctx, sha256sum );
  394.             for( j = 0; j < 32; j++ ) sprintf( cpasshash + 2 * j, "%02x", sha256sum[j] );  
  395.             cpasshash[65] = '\0';
  396.             fprintf(fres,"Parole: %s Hash: %s\n",cpass, cpasshash);
  397.             fseek(fuser,0L,SEEK_SET);
  398.             while( fgets(buf,1000,fuser) != NULL ){
  399.                 i = strlen(buf);
  400.                 while( buf[i-1] == '\n' || buf[i-1] == '\r' ) buf[--i] = '\0';
  401.                 j = strlen(buf);
  402. //                printf("Strlen: %d %d\n",j,buf[j-1]);
  403.                 i = j - 82;
  404.                 strncpy(cuser,&buf[0],i);     cuser[j-82] = '\0';
  405.                 strncpy(cseed,&buf[j-81],16); cseed[16] = '\0';
  406.                 strncpy(chash,&buf[j-64],64); chash[64] = '\0';
  407. //                printf("User: %s\n",cuser);
  408. //                printf("Seed: %s\n",cseed);
  409. //                printf("Hash: %s\n",chash);
  410.                
  411.                 for( i = 0; i < 64; i++ ) sha256sum40[i] = cpasshash[i];
  412.                 for( i = 0; i < 16; i++ ) sha256sum40[i+64] = cseed[i];
  413.                 sha256_starts( &ctx );
  414.                 sha256_update( &ctx, sha256sum40, 80 );
  415.                 sha256_finish( &ctx, sha256sum );
  416.                 for( j = 0; j < 32; j++ ) sprintf( output + 2 * j, "%02x", sha256sum[j] );  
  417.                 output[65] = '\0';
  418. //                printf("Hash2: %s\n",output);
  419.  
  420.                 j = 1;
  421.                 for( i = 0; i < 64; i++ )
  422.                    if( output[i] !=  chash[i] ) { j = 0; break; }
  423.                 if( j == 1 ) fprintf(fres,"%s GOTCHA \n",cuser);
  424.                    
  425.             }      
  426.                
  427.                
  428.                
  429.         };
  430.        
  431.        
  432.         fclose(fuser);
  433.         fclose(fpass);
  434.         fclose(fres);
  435.        
  436.            
  437.          
  438.          
  439.      }    
  440.  
  441.     system("PAUSE");
  442.     return( 0 );
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement