0

I am coding a cipher, for which key file is 256 bit long. Out of which 128 bits have to go in key and 128 into IV. I am explicitly defining the sizes of those arrays and then reading, and still output I get is Key having like 148 bits set while IV has lesser thus. and in the end it gives stack smashing error.

int main(int argc,char *argv[]) {

    if(argc < 4 || !argv[1] || !argv[2]) {
        cout << "Usage: Encoding: " << argv[0] << " [plaintext-file] [key-file] [cipher-file]" << endl;
        cout << "Usage: Decoding: " << argv[0] << " [cipher-file] [key-file] [plaintext-file]" << endl;
        return 0;
    }

    unsigned char key[16], iv[16];
    unsigned long i;

    // read input file and size etc
    FILE* in_file = fopen(argv[1],"rb");
    if(!in_file) {
        cout << "Error opening read file" << endl;
        return 0;
    }

    fseek(in_file,0,SEEK_END);
    unsigned int msglength = ftell(in_file);
    cout << "Msglength:" << msglength << endl;

    unsigned char * mem_ptr =  (unsigned char*)malloc(msglength);
    if(!mem_ptr) {
        cout << "Error allocating memory" << endl;
        fclose(in_file);
        return 0;
    }

    rewind(in_file);
    fread(mem_ptr,1,msglength,in_file);
    fclose(in_file);

    // read keyfile etc
    FILE* key_file = fopen(argv[2],"rb");
    if(!key_file) {
        cout << "Error opening key file" << endl;
        return 0;
    }

    fseek(key_file,0,SEEK_END);
    unsigned int key_size = ftell(key_file);
    cout << "Key Size:" << key_size << endl;
    unsigned char * key_ptr = (unsigned char*)malloc(key_size);
    if(!key_ptr) {
        cout << "Error allocating memory" << endl;
        fclose(key_file);
        return 0;
    }

    rewind(key_file);
    fread(key_ptr,1,key_size,key_file);

    cout << "sizeof key"<<sizeof(key)<<"  sizeof iv" << sizeof(iv) << endl;
    for (i=0; i<256; i++) {
        if (i<128) key[i] = key_ptr[i];
        else iv[i-128] = key_ptr[i];
    }
    cout << key_ptr << endl;
    cout << "Key:" << key << endl << "IV:" << iv << endl;
}

Output

Msglength:15
Key Size:257
sizeof key16  sizeof iv16
abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678

Key:abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678
IV:abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678

*** stack smashing detected ***: ./a.out terminated
Segmentation fault (core dumped)

Any idea where am I going wrong?

higuaro
  • 15,730
  • 4
  • 36
  • 43
Hrishikesh
  • 1,076
  • 1
  • 8
  • 22
  • i changed declaration to key[128] etc, and initialized to 0 and the stack smashing is gone, however the key and IV aren't still the same length – Hrishikesh May 16 '13 at 15:36

3 Answers3

0

The size of your key and iv are both 16.

unsigned char key[16], iv[16];

So this is no good.

for (i=0; i<256; i++) {
    if (i<128) key[i] = key_ptr[i];
    else iv[i-128] = key_ptr[i];
}

You're going wrong there, by writing past the end of both those arrays.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

You have a buffer overflow: you define key and iv as unsigned char[16] but in your for loop you try to access them as if they were unsigned char[128].

Are you confusing bits and bytes at some point?

syam
  • 14,701
  • 3
  • 41
  • 65
0

As per this post Stack Smashing is actually a protection mechanism used by gcc to detect buffer overflow attacks

and i could see

unsigned char key[16], iv[16];

is uninitialized may be you can consider initializing them to zero or NULL

also

  for (i=0;i<256;i++) {
      if (i<128) key[i] = key_ptr[i];
      else iv[i-128] = key_ptr[i];
    }

There is no NULL termination in this case perhaps you can look at these to get a bit more idea

Community
  • 1
  • 1
asio_guy
  • 3,667
  • 2
  • 19
  • 35