Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
29
Добавлен:
16.04.2013
Размер:
110.62 Кб
Скачать

for (i=j=0;i<Nb;i++,j+=4) unpack(s[i],(BYTE *)&result[j]);

return;

}

void gen_random(BYTE *pb, int size)

{

for (int i=0; i < size; i++) pb[i]^=rand();

}

/* This function will add one to the given number (as a byte string). * has been reached.

*/

static void increase_counter( BYTE *x, int x_size)

{

int i, y;

for (i = x_size - 1; i >= 0; i--)

{

y = 0;

if ( x[i] == 0xff)

{

x[i] = 0; y = 1;

}

else x[i]++;

if (y==0) break;

}

return;

}

BYTE cipher_IV[32];

static BYTE block[32]; static BYTE *iv;

BOOL CFB_Debug = false;

BOOL OFB_Debug = false;

BOOL CTR_Debug = false;

BOOL CBC_Debug = false;

BOOL ECB_Debug = false;

int CFB_bits = 1; int OFB_bits = 1;

int blockEncrypt(CWinaesDlg* dlg, BYTE *input, int inputLen, BYTE* result, int cipher_mode)

{

int i, j, k, l, numBlocks;

numBlocks = inputLen/(Nb<<2);

switch (cipher_mode)

{

case MODE_ECB:

for (i = 1; i <= numBlocks ; i++)

{

if (ECB_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4);

sprintf(strTmp,"= ECB AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

}

Encrypt(dlg, input, result);

if (ECB_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

72

input += Nb<<2; result += Nb<<2;

}

break;

case MODE_CBC:

iv = cipher_IV;

for (i = 1; i <= numBlocks; i++)

{

if (CBC_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4);

sprintf(strTmp,"= CBC AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

for (j=0; j < Nb; j++) ((DWORD*)block)[j] = ((DWORD*)input)[j] ^

((DWORD*)iv)[j];

Encrypt(dlg, block, result);

iv = result;

if (CBC_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

input += Nb<<2; result += Nb<<2;

}

break;

case MODE_CFB1:

iv = (BYTE*) malloc(Nb<<2); memcpy(iv, cipher_IV, Nb<<2);

for (i = 1; i <= numBlocks ; i++)

{

memcpy(result, input, Nb<<2);

if (CFB_Debug)

{

sprintf(strTmp,"= CFB AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

for (k = 0; k < (Nb<<5); k+=CFB_bits)

{

Encrypt(dlg, iv, block);

for (l = k; (l < (k + CFB_bits)) && (l < Nb<<5); l++)

{

result[l >> 3] ^= (block[0] & 0x80U) >> (l & 7);

for (j = 0; j < (Nb<<2) - 1; j++)

{

iv[j] = (iv[j] << 1) | (iv[j + 1] >> 7); block[j] = (block[j] << 1) | (block[j + 1] >>

7);

}

73

iv[(Nb<<2)-1] = (iv[(Nb<<2)-1] << 1) | ((result[l >>

3] >> (7 - (l & 7))) & 1);

}

}

if (CFB_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

result += Nb<<2; input += Nb<<2;

}

free(iv);

break; case MODE_OFB:

iv = (BYTE*) malloc(Nb<<2); memcpy(iv, cipher_IV, Nb<<2);

for (i = 1; i <= numBlocks ; i++)

{

memcpy(result, input, Nb<<2);

if (OFB_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4);

sprintf(strTmp,"= OFB AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

for (k = 0; k < (Nb<<5); k+= OFB_bits)

{

Encrypt(dlg, iv, block);

for (l = k; (l < (k + OFB_bits)) && (l < Nb<<5); l++)

{

result[l >> 3] ^= (block[0] & 0x80U) >> (l & 7);

for (j = 0; j < (Nb<<2) - 1; j++)

{

iv[j] = (iv[j] << 1) | (iv[j + 1] >> 7); block[j] = (block[j] << 1) | (block[j + 1] >>

7);

}

iv[(Nb<<2)-1] = (iv[(Nb<<2)-1] << 1) | ((block[0] &

0x80U) >> 7 );

}

}

if (OFB_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

result += Nb<<2; input += Nb<<2;

}

free(iv);

break;

74

case MODE_CTR:

iv = (BYTE*) malloc(Nb<<2); memcpy(iv, cipher_IV, Nb<<2);

for (i = 1; i <= numBlocks; i++)

{

if (CTR_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4);

sprintf(strTmp,"= CTR AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

Encrypt(dlg, iv, result);

for (j=0; j < Nb; j++) ((DWORD*)result)[j] ^= ((DWORD*)input)[j];

increase_counter( iv, Nb<<2);

if (CTR_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

input += Nb<<2; result += Nb<<2;

}

free(iv);

break;

default:

return BAD_CIPHER_STATE;

}

return 128*numBlocks;

}

int blockDecrypt(CWinaesDlg* dlg, BYTE *input, int inputLen, BYTE* result, int decrypt_mode, int cipher_mode)

{

int i, j, k, l, numBlocks;

numBlocks = inputLen/(Nb<<2);

switch (cipher_mode)

{

case MODE_ECB:

for (i = 1; i <= numBlocks; i++) {

switch (decrypt_mode)

{

case DIR_DECRYPTINV: if (ECB_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4); sprintf(strTmp,"= ECB AES Nb=%u Nk=%u, block

%04u inverse decrypt ===============", Nb, Nk, i);

dlg->m_eDebug.AddString(strTmp);

}

InvDecrypt(dlg, input, result); break;

case DIR_DECRYPTEQU: if (ECB_Debug)

75

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4); sprintf(strTmp,"= ECB AES Nb=%u Nk=%u, block

%04u equivalent decrypt ============", Nb, Nk, i);

dlg->m_eDebug.AddString(strTmp);

}

EquDecrypt(dlg, input, result); break;

}

if (ECB_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

input += Nb<<2; result += Nb<<2;

}

break;

case MODE_CBC:

iv = cipher_IV;

for (i = numBlocks; i > 0; i--) {

switch (decrypt_mode)

{

case DIR_DECRYPTINV: if (CBC_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4); sprintf(strTmp,"= CBC AES Nb=%u Nk=%u, block

%04u inverse decrypt ===============", Nb, Nk, i);

dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

InvDecrypt(dlg, input, block); break;

case DIR_DECRYPTEQU: if (ECB_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4); sprintf(strTmp,"= CBC AES Nb=%u Nk=%u, block

%04u equivalent decrypt ============", Nb, Nk, i);

dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

EquDecrypt(dlg, input, block); break;

}

for (j=0; j < Nb; j++) ((DWORD*)block)[j] ^= ((DWORD*)iv)[j];

memcpy(result, block, Nb<<2);

if (CBC_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

input += Nb<<2; result += Nb<<2;

76

}

break;

case MODE_CFB1:

iv = (BYTE*) malloc(Nb<<2); memcpy(iv, cipher_IV, Nb<<2);

for (i = 1; i <= numBlocks; i++)

{

memcpy(result, input, Nb<<2);

if (CFB_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4);

sprintf(strTmp,"= CFB AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

for (k = 0; k < (Nb<<5); k+=CFB_bits)

{

Encrypt(dlg, iv, block);

for (l = k; (l < (k + CFB_bits)) && (l < (Nb<<5)); l++)

{

result[l >> 3] ^= (block[0] & 0x80U) >> (l & 7);

for (j = 0; j < (Nb<<2)-1; j++)

{

iv[j] = (iv[j] << 1) | (iv[j + 1] >> 7); block[j] = (block[j] << 1) | (block[j + 1] >>

7);

}

iv[(Nb<<2)-1] = (iv[(Nb<<2)-1] << 1) | ((input[l >>

3] >> (7 - (l & 7))) & 1);

}

}

if (CFB_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

result += Nb<<2; input += Nb<<2;

}

free(iv);

break; case MODE_OFB:

iv = (BYTE*) malloc(Nb<<2); memcpy(iv, cipher_IV, Nb<<2);

for (i = 1; i <= numBlocks; i++)

{

memcpy(result, input, Nb<<2);

if (OFB_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4);

sprintf(strTmp,"= OFB AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

for (k = 0; k < (Nb<<5); k+=OFB_bits)

{

77

Encrypt(dlg, iv, block);

for (l = k; (l < (k + OFB_bits)) && (l < (Nb<<5)); l++)

{

result[l >> 3] ^= (block[0] & 0x80U) >> (l & 7);

for (j = 0; j < (Nb<<2)-1; j++)

{

iv[j] = (iv[j] << 1) | (iv[j + 1] >> 7); block[j] = (block[j] << 1) | (block[j + 1] >>

7);

}

iv[(Nb<<2)-1] = (iv[(Nb<<2)-1] << 1) | ((block[0] &

0x80U) >> 7 );

}

}

if (OFB_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

result += Nb<<2; input += Nb<<2;

}

free(iv);

break; case MODE_CTR:

iv = (BYTE*) malloc(Nb<<2); memcpy(iv, cipher_IV, Nb<<2);

for (i = 1; i <= numBlocks; i++)

{

if (CTR_Debug)

{

//CharStr2HexStr((BYTE*)s, Tmp, Nb*4);

sprintf(strTmp,"= CTR AES Nb=%u Nk=%u, block %04u encrypt

=======================", Nb, Nk, i); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)iv, Tmp, Nb*4); sprintf(strTmp," IV[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

Encrypt(dlg, iv, result);

for (j=0; j < Nb; j++) ((DWORD*)result)[j] ^= ((DWORD*)input)[j];

increase_counter( iv, Nb<<2);

if (CTR_Debug)

{

CharStr2HexStr((BYTE*)input, Tmp, Nb*4); sprintf(strTmp," in[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

CharStr2HexStr((BYTE*)result, Tmp, Nb*4); sprintf(strTmp,"out[%04u]=%s", i, Tmp); dlg->m_eDebug.AddString(strTmp);

}

input += Nb<<2; result += Nb<<2;

}

free(iv);

break;

default:

return BAD_CIPHER_STATE;

}

return 128*numBlocks;

}

78

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в папке Шифрование AES