mardi 22 mai 2018

(0xC0000005) error in C++

I have a function in which a for loop is causing this error, the function code is:

void AES_CxDecipher::keyExpansion(const uint8_t* key, uint8_t* keySchedule)
{
uint8_t temp[4];
unsigned i = 0;

for(; i < Nk; i++)
{
    for(unsigned short j = 0; j < 4; j++)
    {
        keySchedule[4 * i + j] = key[4 * i + j];
    }
}

i = Nk;

while(i < (4 * (Nr + 1)))
{
    for(unsigned short j = 0; j < 4; j++)
    {
        temp[j] = keySchedule[4 * (i - 1) + j];
    }

    if(i % this -> Nk == 0)
    {
        for(unsigned short j = 0; j < 3; j++) ///RotWord SubRoutine/
        {
            std::swap(temp[j], temp[j + 1]);
        }

        for(unsigned short j = 0; j < 4; j++) ///SubWord SubRoutine/
        {
            temp[j] = get_SBOX_value(temp[j]);
        }

        for(unsigned short j = 0; j < 4; j++)
        {
            temp[j] ^= R_CON[i / Nk];
        }

    }

    else if((Nk > 6) && (i % Nk == 4))
    {
        for(unsigned short j = 0; j < 4; j++) ///SubWord SubRoutine/
        {
            temp[j] = get_SBOX_value(temp[j]);
        }
    }

    for(unsigned short j = 0; j < 4; j++) //this loop causes the error, I've checked for it.
    {
        keySchedule[(4 * i) + j] = keySchedule[(4 * (i - Nk)) + j] ^ temp[j];
    }

    i += 1;
   }
  }

Initially I thought the problem is with accessing array through loop

    keySchedule[(4 * i) + 0] = keySchedule[(4 * (i - Nk)) + 0] ^ temp[0];
    keySchedule[(4 * i) + 1] = keySchedule[(4 * (i - Nk)) + 1] ^ temp[1];
    keySchedule[(4 * i) + 2] = keySchedule[(4 * (i - Nk)) + 2] ^ temp[2];
    keySchedule[(4 * i) + 3] = keySchedule[(4 * (i - Nk)) + 3] ^ temp[3];

so I replaced the loop with above statements.

But the error remained.

Also, when I replaced the loop with the above statements I forgot to comment out the loop compiled the program with loop and the equivalent set of statements and program didn't reported this runtime error, main simply returned 0.

main():

#include <iostream>
#include "AES_CxDecipher.h"

using namespace std;

int main()
{
 uint8_t i[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
const uint8_t k[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
uint8_t o[16], go[16] = {0x69,0xc4,0xe0,0xd8,0x6a,0x7b,0x04,0x30,0xd8,0xcd,0xb7,0x80,0x70,0xb4,0xc5,0x5a};

AES_CxDecipher enc(AES_CxDecipher::ENCRYPT, AES_CxDecipher::AES128);
uint8_t ks[4*(10+1)];// dimension of ks = nb(=4) * (nr(=10) + 1)

enc.keyExpansion(k,ks); //errornous routine
}

constructor of the class AES_CxDecipher:

AES_CxDecipher::AES_CxDecipher(const uint8_t CxD, const uint8_t bitMode)
{

if(CxD == AES_CxDecipher::ENCRYPT)
{
    AES_MODE = AES_CxDecipher::ENCRYPT;

    if(bitMode == AES_CxDecipher::AES128){ Nk = 4; Nr = 10; }
    else if(bitMode == AES_CxDecipher::AES192){ Nk = 6; Nr = 12; }
    else if(bitMode == AES_CxDecipher::AES256){ Nk = 8; Nr = 14; }
}

else if(CxD == AES_CxDecipher::DECRYPT)
{
    AES_MODE = AES_CxDecipher::DECRYPT;

    if(bitMode == AES_CxDecipher::AES128){ Nk = 4; Nr = 10; }
    else if(bitMode == AES_CxDecipher::AES192){ Nk = 6; Nr = 12; }
    else if(bitMode == AES_CxDecipher::AES256){ Nk = 8; Nr = 14; }
}
}

Declaration of variables used:

    public:
    static const uint8_t ENCRYPT = 0x01;
    static const uint8_t DECRYPT = 0x00;

    static const uint8_t AES128 = 0x80;
    static const uint8_t AES192 = 0xc0;
    static const uint8_t AES256 = 0x100;

    private:

    static const uint8_t S_BOX[256];
    static const uint8_t invS_BOX[256];
    static const uint8_t R_CON[11];

    unsigned short Nr;
    unsigned short Nk;

You may ignore the other blocks in the code if you need to compile this code, because the other class methods I've called have been debugged and checked, I am sure that the error is caused by the for() loop mentioned.

Compiler used is codeblocks 16.01

Thank you

Aucun commentaire:

Enregistrer un commentaire