jeudi 20 août 2020

Botan decryption error handling on incorrect key scenario

i currently need to handle a scenario where a user pass in an incorrect key for password decryption. Below is my prove of concept code

#include <botan/rng.h>
#include <botan/auto_rng.h>
#include <botan/cipher_mode.h>
#include <botan/hex.h>
#include <fstream>
#include <iostream>

int main(int argc, char** argv)
{
    Botan::AutoSeeded_RNG rng;

    std::cout << argc << std::endl; 

    std::unique_ptr<Botan::Cipher_Mode> enc = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::ENCRYPTION);
    std::unique_ptr<Botan::Cipher_Mode> dec = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::DECRYPTION);
    const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
    
    const std::string encText ="A9B7DC28Cdgjlpuy";
    Botan::secure_vector<uint8_t> myText(encText.data(), encText.data()+encText.length());
    Botan::secure_vector<uint8_t> iv = myText; 

    std::string encordedText;


    if(argv[1][1] == 'e')
    {   
        //Botan::secure_vector<uint8_t> iv
        const std::string plaintext(argv[2]);
        std::cout<<"plaintext"<<plaintext<<std::endl;
        Botan::secure_vector<uint8_t> pt (plaintext.data(), plaintext.data()+plaintext.length());
        
        std::ofstream myfile;
        myfile.open("new.txt");
        
        enc->set_key(key);
        
        enc->start(iv);
        enc->finish(pt);

        std::cout <<"enc->name()"<< enc->name() << " with iv " <<std::endl;
        std::cout<<"Botan::hex_encode(iv)"<<Botan::hex_encode(iv) <<std::endl; 
        std::cout<<"Botan::hex_encode(pt)"<<Botan::hex_encode(pt) << std::endl;
        myfile <<Botan::hex_encode(pt);
        myfile.close();
    }
    else
    {
        //overwrite the secure key: assume user pass in incorrect key 
        const std::vector<uint8_t> key = Botan::hex_decode("2B7E151618AED2A6ABF7158809CF4F3C");
        std::ifstream readfile;
        readfile.open("new.txt");
        
        readfile>>encordedText;
        std::cout<<"encordedText content is "<<std::endl;
        std::cout<<encordedText<<std::endl;
        
        Botan::secure_vector<uint8_t> tmpPlainText(Botan::hex_decode_locked(encordedText));

        dec->set_key(key);
        dec->start(iv);
        dec->finish(tmpPlainText);
        std::cout<<tmpPlainText.data()<<std::endl;
        readfile.close();
    }   

    return 0;
}

I observe that when i replace the key with something different from the encryption, we will encountered error in finish() function , where the exception invalid cbc padding is will be call.

I intended to let user attempt to decrypt using different key up to three times. But since it calling void CBC_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset) from botan/src/lib/modes/cbc/cbc.cpp , i am not really sure what i can do to satisfy the scenario i mentioned.

Here what i thought on the work around: During encryption, use "something" to hold the key ,for example a text file, than during decryption, we read the key and compare to the key content input by user up to 3 times.

Is there possible to implement any exception handling for incorrect description key scenario? Correct me if i am wrong but should i attempt to make changes on void CBC_Decryption::finish in botan/src/lib/modes/cbc/cbc.cpp to suit my need?

3 commentaires: