Block Cipher (File Handling) - Error: malloc.c

Hi,

First of all, sorry about somewhat long code (~400) lines. Relevant bits should be small.
I am writing a code for block cipher IDEA http://en.wikipedia.org/wiki/International_Data_Encryption_Algorithm
(mixture of C/C++).

Usage: Encoding: ./a.out -e [plaintext-file] [key-file] [cipher-file]
Usage: Decoding: ./a.out -d [cipher-file] [key-file] [plaintext-file]

While decoding, I am getting following error:
root@laptop:~/coding/block_cipher/idea$ ./a.out -d idea_output idea_key idea_output-2
IDEA:: decrypt
Key File Size:26
Key:000000000000
000
Key looks all shiny..Size: 8. (8 means uint16 cast right)
Initialized Encryption and Decryption Key arrays.
Just expanded that key..
-- Inside Decrypt::idea_output
-- The usual constant setting
a.out: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted (core dumped)


I don't know what is going wrong. I was wondering if someone could take a look at the code and help out.
Thanks a lot.

Code: http://pastebin.com/c3VPZaRY

"main" function. (for complete code check above link)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
int main(int argc,char *argv[]){
  
  if(argc != 5) {
    cout << "Usage: Encoding: " << argv[0] << " -e [plaintext-file] [key-file] [cipher-file]" << endl;
    cout << "Usage: Decoding: " << argv[0] << " -d [cipher-file] [key-file] [plaintext-file]" << endl;
    return 0;
  }
  const char *modes[2] = { "de", "en" };

  uint16 i, j;

  char *Mode = argv[1];
  char *InFile = argv[2];
  char *KeyFile = argv[3];
  char *OutFile = argv[4];
  
  // If mode is Encryption or Decryption
  if ((Mode[0] != '-') || ((Mode[1] != 'e' ) && (Mode[1] != 'd' ))) {
    cerr << "Invalid mode: `" << Mode << "'. Please specify `-e' or `-d'." << endl;
      exit(1);
    }
  bool isEncrypt = (Mode[1] == 'e') ? true : false;
 
  cout << "IDEA:: " << modes[isEncrypt] << "crypt" << endl;

  // read keyfile
  FILE* key_file = fopen(KeyFile,"rb");
  if(!key_file){
    cout << "Error opening key file: `" << KeyFile << "'" << endl;
    return 0;
  }

  fseek(key_file,0,SEEK_END);
  unsigned int key_size = ftell(key_file);
  D(cout << "Key File Size:" << key_size << endl;);

  unsigned char char_key[16] = { 0 };
  
  if (key_size >= 16) {
    rewind(key_file);
    fread(char_key,1,16,key_file);
    char_key[16]='\0';
    fclose(key_file);
  }
  D(cout << "Key:" << char_key << endl;);  

  // 128 bit-key (16 characters read)
  // converting into 8-length array of 16-bit integers (short)
 
  uint16 *key;
  key = reinterpret_cast<uint16 *>(char_key);  
  D(cout << "Key looks all shiny..Size: " << sizeof(key) << ". (8 means uint16 cast right)" << endl);                                
   

  int **Encryption_Key = reinterpret_cast<int **>(calloc(9, sizeof(int *)));
  int **Decryption_Key = reinterpret_cast<int **>(calloc(9, sizeof(int *)));

  for (i = 0; i < 9; i++) {
    Encryption_Key[i] = reinterpret_cast<int *>(calloc(6, sizeof(int)));
    Decryption_Key[i] = reinterpret_cast<int *>(calloc(6, sizeof(int)));
    for (j = 0; j < 6; j++) Encryption_Key[i][j] = Decryption_Key[i][j] = 0;
  }
  
  D(cout << "Initialized Encryption and Decryption Key arrays." << endl);                                

  /**** Key Expansion ****/
  // Required to expand the key irrespective of encryption or decryption
  Build_Encryption_Key(key,Encryption_Key);

  D(cout << "Just expanded that key.." << endl);                                

  int char_count;
  
  // Encrypt or Decrypt according to mode
  if (isEncrypt) {
    D(cout << "Okay, now going to encrypt.." << endl);
    char_count = Encrypt(InFile,OutFile,Encryption_Key);
    D(cout << "It seems like that encryption worked.." << endl);
  } else {
    Build_Decryption_Key(Encryption_Key, Decryption_Key);
    char_count = Decrypt(InFile,OutFile,Decryption_Key);
  }

  cout << "IDEA:: Total " << char_count << " characters " << modes[isEncrypt] << "crypted." << endl; 
}

Topic archived. No new replies allowed.