0

I'm trying to understand the OpenSSL library. I have written a program in C which should decrypt an IDEA/CFB mode encrypted using OpenSSL 1.1.0g. This is the code so far:

#include <fcntl.h>                     /* O_RDONLY */
#include <stdio.h>                       /* printf */
#include <string.h>                      /* memcpy */
#include <unistd.h>                        /* read */
#include <openssl/idea.h>                  /* BF_* */
#include <openssl/evp.h>
#include <openssl/ripemd.h>

#define BUFFER_SIZE 861

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

int read_file(char *file, unsigned char *buffer);

int main(void)
{

    unsigned char buffer[BUFFER_SIZE];
    unsigned char cipher_buffer[BUFFER_SIZE];
    unsigned char plain[BUFFER_SIZE];
    unsigned char iv_buffer[BUFFER_SIZE];
    unsigned char hash_buffer[BUFFER_SIZE];
    unsigned char final[BUFFER_SIZE];
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); 
    EVP_CIPHER *ciph;    
    char *cipher = "./cipher.bin";
    char *key1 = "./key.bin";

    //read key
    int blah = read_file(key1,buffer);
    printf("\nKeylength: %d\n", EVP_CIPHER_key_length(EVP_idea_cfb64()));
    printf("IV Length: %d\n",EVP_CIPHER_iv_length(EVP_idea_cfb64()));
    printf("\nKEY:");
    for(int i = 0;i<16;i++){
        key[i] = buffer[i];
        printf("%d ", buffer[i]);
    }

    printf("\nIV : ");
    for(int i = 16;i<24;i++){
        iv[i] = buffer[i];
        printf("%d ", iv[i]);
    }

    //read cipher
    int cipherLen = read_file(cipher,cipher_buffer);
    unsigned char cipher_text[cipherLen];
    for(int i = 0; i<861;i++){
        cipher_text[i] = cipher_buffer[i];
    }

    int outlen = 0;  
    unsigned char lastl[BUFFER_SIZE];
    int last;
    EVP_CIPHER_CTX_init(ctx);
    EVP_CipherInit_ex(ctx,EVP_idea_cfb(),NULL,key,iv,0);
    EVP_CipherUpdate(ctx,plain,&last,cipher_buffer,BUFFER_SIZE);
    EVP_DecryptFinal_ex(ctx,plain,&last);

    printf("%s",plain);

    return 0;
}

int read_file(char *file, unsigned char *buffer){
    int fp = open(file,O_RDONLY);
    //printf("fd:%d File: %s\n",fp,file);
    if(fp == -1){
        perror("File not found");
    }
    int f_size = lseek (fp , 0 , SEEK_END);
    lseek (fp , 0 ,SEEK_SET);
    printf("     f_size:%d\n",f_size);
    read(fp,buffer,BUFFER_SIZE);

    //printf("lol:%d\n", f_size);
    return f_size;
}

The problem I have is that when I print the result everything is fine but the first block of the text is corrupted ( the first 8 byte) I think it has something to do with the CFB mode, but I can't wrap my head around this. :(

This is the Output: Output

And these are the two files (cipher.bin and key.bin):

cipher.bin

key.bin

jww
  • 97,681
  • 90
  • 411
  • 885
Twin322
  • 170
  • 1
  • 8
  • 2
    You should show example input and output data. Also, that's not the code so far, because that code wouldn't compile. Post all of the code required to replicate/analyse the problem. – underscore_d Jan 03 '18 at 15:06
  • Why are you using IDEA, AES is more secure and is often has hardware support for performance. – zaph Jan 03 '18 at 15:22
  • Yeah... I know, but it is a task at my university. – Twin322 Jan 03 '18 at 15:30
  • You should add a hex dump of `cipher.bin` and `key.bin`. Also see [EVP Symmetric Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) on the OpenSSL wiki. – jww Jan 04 '18 at 04:14

1 Answers1

0

Big mistake on my side

I read the keys out of the file and messed up with the index...

the IV vector was corrupted

int j=0;
for(int i = 16;i<24;i++){
    iv[j] = buffer[i];
    printf("0x%02x,", iv[i]);
    j++;
}

That fixed it

Twin322
  • 170
  • 1
  • 8