1

I currently have some code that works correctly with the LibTomCrypt libraries, but not with Botan (I am trying to convert it over to Botan).

My (working) LibTomCrypt code:

    // read the initial vector
    unsigned char iv[0x20];
    fseek(inputFile, 0x20, SEEK_SET);
    fread(iv, 0x20, 1, inputFile);

    // call ctr_start
    res = ctr_start(0, iv, key, 0x20, 0, 0, &ctr);

    if (res == 0)
    {
        printf("decrypting data...\n");

        // read the encrypyted data
        unsigned char cipheredText[0x3A8];
        fread(cipheredText, 0x3A8, 1, inputFile);

        // decrypt the data
        unsigned char uncipheredText[0x3A8];
        if (ctr_decrypt(cipheredText, uncipheredText, 0x3A8, &ctr) != 0)
        {
            fclose(inputFile);
            printf("ERROR: ctr_decrypt did not return 0\n");
            return -1;
        }
        if (ctr_done(&ctr) != 0)
        {
            fclose(inputFile);
            printf("ERROR: ctr_done did not return 0\n");
            return -1;
        }

        printf("writing decrypted data...\n");

        // get the decrypted path
        char *decPath = concat(fileName, ".dec", 4);

        // write the decrypted data to disk
        FILE *outFile = fopen(decPath, "w");
        fwrite(uncipheredText, 0x3A8, 1, outFile);
        fclose(outFile);
    }
    else
    {
        printf("ERROR: ctr_start did not return 0\n");
    }

As you can see, my the size of my initial vector (IV), is 0x20 (32). I don't know why this would work with this library, but I went to the method and it seems like it has something to do with the 'blocklen' in LibTomCrypt.

Anyway, this is what I am trying to do with the Botan libraries:

// get the iv
t1Stream->setPosition(0x20);
BYTE rawIV[0x20];
t1Stream->readBytes(rawIV, 0x20);

// get the encrypted data
t1Stream->setPosition(0x40);
BYTE cipheredText[0x3A8];
t1Stream->readBytes(cipheredText, 0x3A8);

// setup the keys & IV
Botan::SymmetricKey symKey(key, 0x20);
Botan::InitializationVector IV(rawIV, 0x20);

// setup the 'pipe' ?
Botan::Pipe pipe(Botan::get_cipher("AES-256/CBC/NoPadding", symKey, IV, Botan::DECRYPTION));

But it keeps throwing this on the call of 'get_cipher':

terminate called after throwing an instance of 'Botan::Invalid_Key_Length'
  what():  Botan: AES-256 cannot accept a key of length 32

If I do change the IV size to 16, than it does work correctly, but cannot process stuff since the IV is incorrect.

Also, change the IV size in my encrypting code is not an option.

hetelek
  • 3,776
  • 5
  • 35
  • 56
  • 3
    AES has a block size of 128 bits, and the IV is one block. Your delusions notwithstanding, the IV that the encryption algorithm consumes *will* be 16 bytes long. – Kerrek SB Nov 24 '12 at 19:09
  • You should really use constants instead of hard coded literals, they make code hard to read, hard to change, and it is easier for other people to see your mistakes. If present, use library specific ones (e.g. I presume that in LibTomCrypt that AES will have constant 0 for the first argument of `ctr_start()`? – Maarten Bodewes Dec 17 '12 at 00:32

1 Answers1

0

We cannot really see what cipher mode you are using. If it is Rijndael-256 then the block size would be 256 bits. If it isn't, then the nonce is likely to be cut somewhere by the library - in that case it is likely that only the first 128 bits are used.

That said, the code will never work, as you are using counter mode encryption in the first example and CBC mode encryption in the other.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263