Hi all,
My code is giving the desired o/p, but at the end its producing segmentation fault.
(I know this is very column and also I have checked other posts, but couldn't understand the solution)
Here is my code

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
 
#include "blake.h" 
 
 
 
 
int    genShortMsg(int hashbitlen); 
 
 
 
int 
main() 
{ 
    int ret_val;
 
    ret_val = genShortMsg(256);
     
    return 0; 
     
} 
 
 
 
int  
genShortMsg(int hashbitlen) 
{ 
 
    int            msglen, msgbytelen; 
    BitSequence    Msg[7], MD[64]; 
 
        msglen=55;     
        msgbytelen = (msglen+7)/8; 
     
 
 
    int        ch, started; 
    BitSequence    ich; 
    int i; 
    if ( msgbytelen == 0 ) { 
        Msg[0] = 0x00; 
         
    } 
    memset(Msg, 0x00, msgbytelen); 
    started = 0; 
    char str[] = "14B06DD54EB364"; 
    int j; 
 
    for(j=0;j<strlen(str);j++){ 
        ch=str[j]; 
            if ( (ch >= '0') && (ch <= '9') ) 
                ich = ch - '0'; 
            else if ( (ch >= 'A') && (ch <= 'F') ) 
                ich = ch - 'A' + 10; 
             
            else if ( (ch >= 'a') && (ch <= 'f') ) 
                ich = ch - 'a' + 10; 
             
            for ( i=0; i<msgbytelen-1; i++ )Msg[i] = (Msg[i] << 4) | (Msg[i+1] >> 4); 
            Msg[msgbytelen-1] = (Msg[msgbytelen-1] << 4) | ich; 
 
    } 
          
     
        for(i=0;i<64;i++)MD[i]=0; 
     
         
        Hash(256, Msg, msglen, MD); 
     
        for(i=0;i<32;++i)printf("%02X",MD[i]);
 
        printf("\n");

 
    return 0; 
}

It prints correct o/p(computed by Hash(.......)), but crashes when genShortMsg() returns.

Thanks in advance for the reply

Could you please also post blake.h and source of Hash().

I suspect Hash() is the culprit or arguments passed to Hash() should be the problem. But I cannot say for sure with out the full source code.

Thanks,
VIneeth

@Vineeth
Yes Vineeth, i have also found that the problem is with Hash() because when i commented it , segmentation fault disaapears.
But when i debug Hash() using gdb , there was no fault and also the values were correct and as before , it shows fault in genShortMsg()

Anyways , here is blake.h and blake.c

#ifndef blake_h

#define blake_h

typedef unsigned int myuint;

typedef unsigned long long DataLength;

typedef unsigned char BitSequence;

typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHBITLEN = 2 } HashReturn;



typedef struct

{

        myuint int_state[16];//4 X 4 matrix of words

        myuint chainvalue[8];//hash

        myuint counter[2];//#bits hashed so far

        myuint salt[4];//provided by user------default is 0

}hashstate;









HashReturn Hash(int hashbitlen,const BitSequence *data, DataLength databitlen, BitSequence *hashval);



void compress (hashstate *state,myuint m[16]);



void G(int i,int r,myuint m[16],myuint *a,myuint *b ,myuint *c,myuint *d);



#endif
#include "blake.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>









#define ROTATE_DOWN16(a) (((a) << 16) | ((a) >> 16))

#define ROTATE_DOWN12(a) (((a) << 20) | ((a) >> 12))

#define ROTATE_DOWN8(a) (((a) << 24) | ((a) >> 8))

#define ROTATE_DOWN7(a) (((a) << 25) | ((a) >> 7))

static const myuint IV[8] = {//initial values to hash

0x6A09E667,

0xBB67AE85,

0x3C6EF372,

0xA54FF53A,

0x510E527F,

0x9B05688C,

0x1F83D9AB,

0x5BE0CD19

};



static const myuint c_blake[16] ={ 

0x243F6A88,

0x85A308D3,

0x13198A2E,

0x03707344,

0xA4093822,

0x299F31D0,

0x082EFA98,

0xEC4E6C89,

0x452821E6,

0x38D01377,

0xBE5466CF,

0x34E90C6C,

0xC0AC29B7,

0xC97C50DD,

0x3F84D5B5,

0xB5470917,

};



//10 permutations

static const myuint sigma[10][16]={

{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},

{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},

{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},

{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},

{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},

{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},

{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},

{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},

{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},

{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0}

};

myuint  U8_to_U32(const BitSequence *d){

     return ((( myuint)(*d ))<< 24 | ((myuint)(*(d+1)) )<< 16 | ((myuint)(*(d+2))) << 8 | (myuint)(*(d+3)) );

}



void U32_to_U8(BitSequence *h, myuint s){

     *h= (BitSequence)(s>>24);

     *(h+1)=(BitSequence)((s <<8 )>>24);

     *(h+2)=(BitSequence)((s <<16 )>>24);

     *(h+3)=(BitSequence)((s <<24 )>>24);

}

void G(int i,int r,myuint m[16],myuint *a,myuint *b ,myuint *c,myuint *d){

  

   *a = *a + *b +  ( m[(sigma[r][2*i])] ^ c_blake[(sigma[r][2*i + 1])] );

   

  

   *d = ROTATE_DOWN16((*d ^ *a));



   *c = *c + *d;

  

   *b = ROTATE_DOWN12((*b ^ *c));

  

   *a = *a + *b +  ( m[(sigma[r][2*i + 1])] ^ c_blake[(sigma[r][2*i])] );

   

   *d = ROTATE_DOWN8((*d ^ *a));

  

   *c = *c + *d;

   

   *b = ROTATE_DOWN7((*b ^ *c));

  

     

}



void compress(hashstate *state, myuint m[16])

{

     //initialization

   



    // int i;

   // for(i=0;i<16;i++)printf("%02X\n",state->int_state[i]);

     state->int_state[0]=state->chainvalue[0];

     state->int_state[1]=state->chainvalue[1];

     state->int_state[2]=state->chainvalue[2];

     state->int_state[3]=state->chainvalue[3];

     state->int_state[4]=state->chainvalue[4];

     state->int_state[5]=state->chainvalue[5];

     state->int_state[6]=state->chainvalue[6];

     state->int_state[7]=state->chainvalue[7];



    state->int_state[8]  = state->salt[0] ^ c_blake[0];

    state->int_state[9]  = state->salt[1] ^ c_blake[1];

    state->int_state[10] = state->salt[2] ^ c_blake[2];

    state->int_state[11] = state->salt[3] ^ c_blake[3];

    

    state->int_state[12] = state->counter[0] ^ c_blake[4];

    state->int_state[13] = state->counter[0] ^ c_blake[5];

    state->int_state[14] = state->counter[1] ^ c_blake[6];

    state->int_state[15] = state->counter[1] ^ c_blake[7];

    

  //  for(i=0;i<16;i++)printf("%02X\n",state->int_state[i]);

    

    

    //RRound

    int r;

    for(r=0;r<10;r++){

                      //COLUMN STEP

         G(0,r,m,&(state->int_state[0]),&(state->int_state[4]),&(state->int_state[8]),&(state->int_state[12]));

         G(1,r,m,&(state->int_state[1]),&(state->int_state[5]),&(state->int_state[9]),&(state->int_state[13]));

         G(2,r,m,&(state->int_state[2]),&(state->int_state[6]),&(state->int_state[10]),&(state->int_state[14]));

         G(3,r,m,&(state->int_state[3]),&(state->int_state[7]),&(state->int_state[11]),&(state->int_state[15]));

         //DIAGONAL STEP

         G(4,r,m,&(state->int_state[0]),&(state->int_state[5]),&(state->int_state[10]),&(state->int_state[15]));

         G(5,r,m,&(state->int_state[1]),&(state->int_state[6]),&(state->int_state[11]),&(state->int_state[12]));

         G(6,r,m,&(state->int_state[2]),&(state->int_state[7]),&(state->int_state[8]),&(state->int_state[13]));

         G(7,r,m,&(state->int_state[3]),&(state->int_state[4]),&(state->int_state[9]),&(state->int_state[14]));

    

    }



   

       //finalization

        

       state->chainvalue[0] ^= state->salt[0] ^ state->int_state[0] ^ state->int_state[8];

       state->chainvalue[1] ^= state->salt[1] ^ state->int_state[1] ^ state->int_state[9];

       state->chainvalue[2] ^= state->salt[2] ^ state->int_state[2] ^ state->int_state[10];

       state->chainvalue[3] ^= state->salt[3] ^ state->int_state[3] ^ state->int_state[11];

       state->chainvalue[4] ^= state->salt[0] ^ state->int_state[4] ^ state->int_state[12];

       state->chainvalue[5] ^= state->salt[1] ^ state->int_state[5] ^ state->int_state[13];

       state->chainvalue[6] ^= state->salt[2] ^ state->int_state[6] ^ state->int_state[14];

       state->chainvalue[7] ^= state->salt[3] ^ state->int_state[7] ^ state->int_state[15];

 

    

    

}



HashReturn 

Hash(int hashbitlen,const BitSequence *data, DataLength databitlen, BitSequence *hashval)

{



         hashstate *state;

         state->salt[0]=0;

         state->salt[1]=0;

         state->salt[2]=0;

         state->salt[3]=0;

         

         state->counter[0]=0;

         state->counter[1]=0;

         int i;

         for(i=0;i<8;++i)//initialize hash

         {

               state->chainvalue[i]=IV[i];

         }
    

        for(i=0;i<16;i++)state->int_state[i]=0;

         DataLength d=databitlen;

         

         myuint m[16];//msgblock consisiting of 16 words

         

         while(d >= 512)

         {

                 //extract bits from data to m

                 for(i=0;i<16;i++)m[i] = U8_to_U32(data+4*i);

                 

                 //increment data pointer

                 data +=64;

                 

                 compress(state,m);

                 

                 //increment counter

                 state->counter[0]+= 512;

                 if(state->counter[0] == 0)state->counter[1]++;

                 

                 //decremnt d

                 d -= 512;

         }

         

         //2 cases 

         if(d < 512-64-2){

              for(i=0 ; i<((d+31)/32)-1;i++)m[i]=U8_to_U32(data+4*i);

              data+= i*4;

              

              int j=d-(i)*32;

              

              unsigned char pow_2[] = {128,64,32,16,8,4,2,1};

              if(j< 8)

              {

                   m[i]= (*data | pow_2[j])<< 24;

              }

              else if(j<16)

              {

                    m[i]=*data << 24 | (*(data+1) | pow_2[j-8])<< 16;

              }

              else if(j < 24)

              {

                   m[i] = *data << 24 | *(data+1) << 16 | (*(data+2) | pow_2[j-16])<< 8;

              }

              else

              {

                   m[i]=  *data << 24 | *(data+1) << 16 | *(data+2) << 8 |(*(data+3) | pow_2[j-24]);

               }

               i++;

              

              for(;i<13;i++)m[i]=0;

              

              m[13]=(myuint)1;

              m[14]=(myuint)(databitlen >> 32);

        

             m[15]=(myuint)databitlen;

             state->counter[0] += d;

              if(state->counter[0] == 0)state->counter[1]++;
         //   for(i=0;i<16;i++)printf("%02X\n",m[i]);

              compress(state,m);

              

             

         }

              

         else{//d > 512-64-2 , in this case the last block will contain only padded bits

              for(i=0 ; i<((d+31)/32)-1;i++)m[i]=U8_to_U32(data+4*i);

              

              int j=d-(i)*32;

              

              unsigned char pow_2[] = {128,64,32,16,8,4,2,1};

              

              if(j< 8)

              {

                    m[i]= (*data | pow_2[j])<< 24;

              }

              else if(j<16)

              {

                     m[i]=*data << 24 | (*(data+1) | pow_2[j-8])<< 16;

              }

              else if(j < 24)

              {

                     m[i] = *data << 24 | *(data+1) << 16 | (*(data+2) | pow_2[j-16])<< 8;

              }

              else

              {

                     m[i]=  *data << 24 | *(data+1) << 16 | *(data+2) << 8 |(*data | pow_2[j-24]);

              }

              i++;

              for(;i<16;i++)m[i]=0;

              

              compress(state,m);

              state->counter[0] += d;

              if(state->counter[0] == 0)state->counter[1]++;

              

              

              m[13]=1;

              m[14]=(myuint)(databitlen >> 32);

              m[15]=(myuint)databitlen; 

              compress(state,m);

                              

              

              

              

                 

         }

         

         //finalization

         

         U32_to_U8(hashval     , state->chainvalue[0]);

         U32_to_U8((hashval+4) , state->chainvalue[1]);

         U32_to_U8((hashval+8) , state->chainvalue[2]);

         U32_to_U8((hashval+12), state->chainvalue[3]);

         U32_to_U8((hashval+16), state->chainvalue[4]);

         U32_to_U8((hashval+20), state->chainvalue[5]);

         U32_to_U8((hashval+24), state->chainvalue[6]);

         U32_to_U8((hashval+28), state->chainvalue[7]);

       

         return SUCCESS;



}

Vineeth,
Also i used valgrind and after printing the hash, (when genShortMsg() should return to main()), it shows invalid jump and then segmentation fault.

FDDDB6CD9E79C71E98108760B9EB4DB07281519B74CE59F65F9FFD3042BB6F0A
==3478== Jump to the invalid address stated on the next line
==3478==    at 0xE479C6B6: ???
==3478==  Address 0xe479c6b6 is not stack'd, malloc'd or (recently) free'd
==3478== 
==3478== 
==3478== Process terminating with default action of signal 11 (SIGSEGV)
==3478==  Bad permissions for mapped region at address 0xE479C6B6
==3478==    at 0xE479C6B6: ???
==3478== 
==3478== FILE DESCRIPTORS: 3 open at exit.
==3478== Open file descriptor 2: /dev/pts/1
==3478==    <inherited from parent>
==3478== 
==3478== Open file descriptor 1: /dev/pts/1
==3478==    <inherited from parent>
==3478== 
==3478== Open file descriptor 0: /dev/pts/1
==3478==    <inherited from parent>
==3478== 
==3478== 
==3478== HEAP SUMMARY:
==3478==     in use at exit: 0 bytes in 0 blocks
==3478==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3478== 
==3478== All heap blocks were freed -- no leaks are possible
==3478== 
==3478== For counts of detected and suppressed errors, rerun with: -v
==3478== Use --track-origins=yes to see where uninitialised values come from
==3478== ERROR SUMMARY: 2034 errors from 286 contexts (suppressed: 13 from 8)
Segmentation fault

So the problem is in the Hash function. You have a variable 'hashstate *state'. But it is not allocated and it dumps core when you try to dereference it in the line
state->salt[0] = 0;

malloc this and then you should be fine.

Hope this helps!

Thanks,
VIneeth

Thanks Vineeth,

I ddin't think about it , perhaps if you would not tell me ,i wont figure it out.
Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.