20

I just want to test AES from openSSL with this 3 modes: with 128,192 and 256 key length but my decrypted text is different from my input and I dont know why. Also, when I pass a huge inputs length (lets say 1024 bytes) my program shows core dumped... My input is always the same but it doesnt matter, at least for now. Heres the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>

int main(int argc, char **argv)
{
    int i;
    int keylength;
    printf("Give a key length [only 128 or 192 or 256!]:\n");
    scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength];
    memset(aes_key, 0, sizeof(aes_key));
    if (!RAND_bytes(aes_key, keylength))
    {
        exit(-1);
    }
    aes_key[keylength-1] = '\0';

    int inputslength;
    printf("Give an input's length:\n");
    scanf("%d", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength+1];
    memset(aes_input, '0', sizeof(aes_input));
    aes_input[inputslength] = '\0';

    /*printf("original:\t");
    for(i=0; i<inputslength; i++)
    {
        printf("%c ", aes_input[i]);
    }
    printf("\n");*/

    /* init vector */
    unsigned char iv[AES_BLOCK_SIZE];
    if (!RAND_bytes(iv, AES_BLOCK_SIZE))
    {
        exit(-1);
    }

    //printf("AES_BLOCK_SIZE = %d\n", AES_BLOCK_SIZE); // aes block size is 16 bytes = 128 bits
    AES_KEY enc_key, dec_key;
    unsigned char enc_out[AES_BLOCK_SIZE];
    unsigned char dec_out[AES_BLOCK_SIZE];

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_decrypt(enc_out, dec_out, &dec_key);

    printf("original:\t");
    for(i=0;*(aes_input+i)!=0x00;i++)
        printf("%X ",*(aes_input+i));
    printf("\nencrypted:\t");

    for(i=0;*(enc_out+i)!=0x00;i++)
        printf("%X ",*(enc_out+i));

    printf("\ndecrypted:\t");
    for(i=0;*(dec_out+i)!=0x00;i++)
        printf("%X ",*(dec_out+i));
    printf("\n");

    /*printf("\n\noriginal:\t");
    for(i=0; i<inputslength; i++)
    {
        printf("%x ", dec_out[i]);
    }
    printf("\n");*/


    return 0;
}

EDIT:

When I changed outputs sizes to inputslength instead of AES_BLOCK_SIZE I got results:

Give a key length [only 128 or 192 or 256!]:
128
Give an input's length:
5
original:       30 30 30 30 30 
encrypted:      94 56 50 7E 19 B2 1C CE 20 23 4A E7 10 AF DB E3 30 30 30 30 30 
decrypted:      E1 5F F4 3D E8 8D 91 19 CD 3E 22 1E AF 1C 8F 5A 94 56 50 7E 19 B2 1C CE 20 23 4A E7 10 AF DB E3 30 30 30 30 30

So is it possible that theres an issue with outpus sizes and the size of the iv? What sizes they should have (for AES-CBC-128, AES-CBC-192, AES-CBC-256)?

ivy
  • 1,445
  • 3
  • 15
  • 20
  • 2
    Sidenote: Your AES key need not be null terminated. its a random block of bytes; thats all. There's nothing null-term about it, so `aes_key[keylength-1] = '\0'` is pointless (except it always sets the last byte of your key (which is incidentally 8x larger than it needs to be) to zero.) – WhozCraig Aug 09 '13 at 19:28
  • @WhozCraig: thanks, good to know that. How about the main problem, do you have any ideas? – ivy Aug 09 '13 at 19:52
  • 1
    Plenty. The buffer sizes for encryption and decryption are nowhere *near* big enough. and you need a general hex print function rather than trying to treat this stuff as character data. *It's binary*. Forming an official answer now, but start looking there. – WhozCraig Aug 09 '13 at 19:58
  • 1
    You should *not* use `AES_encrypt` and friends. You should be using `EVP_*` functions. See [EVP Symmetric Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) on the OpenSSL wiki. In fact, you should probably be using authenticated encryption because it provides *both* confidentiality and authenticity. See [EVP Authenticated Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption) on the OpenSSL wiki. – jww May 15 '15 at 20:14

2 Answers2

29

Take a peek at this modified version of your code. Note the following:

  1. Added hex_print (minor)
  2. Added proper sizing of key buffer (medium).
  3. Added proper sizing of output encryption buffer (which must be a block-size multiple, and if original source buffer is an exact block-size multiple, you still need one full block of padding (see PKCS 5 padding for more info).
  4. Same IV used for both encrypt and decrypt.
  5. Finally, odd as it may seem AES_cbc_encrypt() is used for both encryption and decryption (see the last parameter in the call).

Source Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength;
    printf("Give a key length [only 128 or 192 or 256!]:\n");
    scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength/8];
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength];
    memset(aes_input, 'X', inputslength);

    /* init vector */
    unsigned char iv_enc[AES_BLOCK_SIZE], iv_dec[AES_BLOCK_SIZE];
    RAND_bytes(iv_enc, AES_BLOCK_SIZE);
    memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);

    // buffers for encryption and decryption
    const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char enc_out[encslength];
    unsigned char dec_out[inputslength];
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    return 0;
}

Test Output

Give a key length [only 128 or 192 or 256!]:
128
Give an input's length:
10
original:   58 58 58 58 58 58 58 58 58 58 
encrypt:    A9 66 C5 24 A4 02 AB 96 08 65 F7 22 A5 FB BE 26 
decrypt:    58 58 58 58 58 58 58 58 58 58 

Second Test Output

Give a key length [only 128 or 192 or 256!]:
128
Give an input's length:
10
original:   58 58 58 58 58 58 58 58 58 58 
encrypt:    C2 47 6D B1 A1 68 29 53 55 74 C5 CC 3F 27 0A 3F 
decrypt:    58 58 58 58 58 58 58 58 58 58 

I sincerely hope this helps.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • sorry for bothering you, you're right, everything is fine now:) – ivy Aug 10 '13 at 19:05
  • I think this code is wrong. My test case: keylen=128, inputlen=100. You should test it again. – g10guang Aug 06 '18 at 03:59
  • @g10guang If you can describe what you think it is supposed to be doing, what it is actually doing, and how they differ, I'll be interested in why you think it is wrong. It isn't. It is doing *exactly* what I intended: generating a random key, a random IV, encrypting a constant block of data filled with `'X'` characters, decrypting the same data, and demonstrating the original, encrypted, and decrypted data (where original and decrypted had better be the same, and they are), using a hex dump for output. It may not be doing what you think it is, and that may be where the disconnect is. – WhozCraig Aug 06 '18 at 09:15
3

@WhozCraig: thank you so much for help! It explained a lot to me! But theres just one more issue. I changed static arrays into dynamic ones. When I did it, some erros occured. But they occure only when I give a huge inputs size, take a look at valgrind output: http://pastie.org/private/bzofrrtgrlzr0doyb3g. Error occurs only when I pass a huge input, when I pass a small size (like in your example, 10) its ok. Everything else is working perfectly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    int keylength;
    printf("Give a key length [only 128 or 192 or 256!]:\n");
    scanf("%d", &keylength);


    //  generate a key with a given length
    unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char) * (keylength/8));
    memset(aes_key, 0, keylength/8);
    RAND_bytes(aes_key, keylength/8);

    //  generate input with a given length
    unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength));
    memset(aes_input, 'X', sizeof(aes_input));

    // init vectors
    unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE));
    unsigned char *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE));
    // iv_dec == iv_enc
    RAND_bytes(iv_enc, AES_BLOCK_SIZE);
    memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);

    // buffers for encryption and decryption
    const size_t length = (((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE);
    unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char) * (length));
    unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength));

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
    AES_KEY encKey, decKey;
    AES_set_encrypt_key(aes_key, keylength, &encKey);
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &encKey, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &decKey);
    AES_cbc_encrypt(enc_out, dec_out, length, &decKey, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    free(aes_key);
    aes_key = NULL;
    free(aes_input);
    aes_input = NULL;
    free(iv_enc);
    iv_enc = NULL;
    free(iv_dec);
    iv_dec = NULL;
    free(enc_out);
    enc_out = NULL;
    free(dec_out);
    dec_out = NULL;

    return 0;
}

EDIT:

Ok, something was wrong with the prev code I posted, heres a new one, working perfectly, even for a huge inputs. Cheers once again for helping me!:)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength;
    printf("Give a key length [only 128 or 192 or 256!]:\n");
    scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char) * (keylength/8));
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char) *inputslength);
    memset(aes_input, 'X', inputslength);

    /* init vector */
    unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) *AES_BLOCK_SIZE), *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) *AES_BLOCK_SIZE);
    RAND_bytes(iv_enc, AES_BLOCK_SIZE);
    memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);

    // buffers for encryption and decryption
    const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char) *encslength);
    unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) *inputslength);
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, inputslength);

    printf("encrypt:\t");
    hex_print(enc_out, encslength);

    printf("decrypt:\t");
    hex_print(dec_out, inputslength);

    // free memory here

    return 0;
}
ivy
  • 1,445
  • 3
  • 15
  • 20
  • 6
    Look again. `sizeof()` takes on a whole new meaning when used with a pointer rather than a fixed or variadic array. It becomes the size of a *pointer*, ***not*** the size of the data allocated for it. All of your sizes should be variables; **not** `sizeof(something)`. Fix that and I bet your final problems will be gone. (sidenote: don't cast `malloc()` when programming in regular C. It can cause very subtle problems, especially for portability. C++ requires it, but then again, you shouldn't be using `malloc()` in C++ in the first place.) =P – WhozCraig Aug 10 '13 at 19:11
  • 1
    thanks again sooo much! Here's working example: http://pastie.org/private/dtvmftozmru02webkzzq. I casted `malloc` beacuese my gcc told me to - `malloc` returns void as I recall, right? When I write in C++, I use `new` and `delete`, I know I shouldnt use `malloc` in C++ ;) – ivy Aug 10 '13 at 19:23
  • 2
    Casting `malloc()` in C programming has potential pitfalls of its own. Obviously it is required in C++, but as you said, you use `new` and `delete`. Lookup "casting malloc [c]" on this site. Its a fairly debated issue. Aside of that, very glad you're up and running. – WhozCraig Aug 10 '13 at 20:46
  • Thanks for the implementation!! – Dano Feb 05 '14 at 11:20
  • Minor issue is that for input which is multiples of the block size, the calculated encoded size is wrong (1 block extra). This corrects the 'encslength' calculation (below; note the -1) Thanks for sharing the example. const size_t encslength = ((inputslength + (AES_BLOCK_SIZE-1)) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; – IanJ Mar 03 '15 at 21:12
  • 1
    @Puffin that is NOT correct. There must be room for up to one *full* block of padding. That's how PKCS#5 padding works. Anything that "works" for you without taking that case into account does so only because the plaintext being encrypted is *not* an exact multiple of the AES block size (16 bytes), and is consequently a ticking time bomb for when that case eventually arrises. [See PKCS5/7 padding here](https://en.wikipedia.org/wiki/Padding_%28cryptography%29#PKCS7), and specifically, the paragraph describing what happens when an exact block-size multiple is encountered. – WhozCraig Aug 05 '17 at 07:46
  • if input_len = 16 then out_enc is an array of 0 – Slim_user71169 Jan 17 '18 at 06:06