Skip to content

Commit

Permalink
AES: fix for 64 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
pvtom committed Jul 16, 2022
1 parent d397403 commit 874eeeb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
71 changes: 36 additions & 35 deletions AES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

// todo - make faster 128 blocksize version with 128 blocksize hardcoded as necessary

Expand All @@ -57,29 +58,29 @@ unsigned char inv_byte_sub[256];

// this table needs Nb*(Nr+1)/Nk entries - up to 8*(15)/4 = 60
// todo - remove table, note cycles every 17(?) elements
unsigned long Rcon[60];
uint32_t Rcon[60];

// long tables for encryption stuff
unsigned long T0[256];
unsigned long T1[256];
unsigned long T2[256];
unsigned long T3[256];
uint32_t T0[256];
uint32_t T1[256];
uint32_t T2[256];
uint32_t T3[256];

// long tables for decryption stuff
unsigned long I0[256];
unsigned long I1[256];
unsigned long I2[256];
unsigned long I3[256];
uint32_t I0[256];
uint32_t I1[256];
uint32_t I2[256];
uint32_t I3[256];

// huge tables - todo - ifdef out
unsigned long T4[256];
unsigned long T5[256];
unsigned long T6[256];
unsigned long T7[256];
unsigned long I4[256];
unsigned long I5[256];
unsigned long I6[256];
unsigned long I7[256];
uint32_t T4[256];
uint32_t T5[256];
uint32_t T6[256];
uint32_t T7[256];
uint32_t I4[256];
uint32_t I5[256];
uint32_t I6[256];
uint32_t I7[256];

// have the tables been initialized?
bool tablesInitialized = false;
Expand All @@ -89,7 +90,7 @@ bool tablesInitialized = false;
#define xmult(a) ((a)<<1) ^ (((a)&128) ? 0x01B : 0)

// make 4 bytes (LSB first) into a 4 byte vector
#define VEC4(a,b,c,d) (((unsigned long)(a)) | (((unsigned long)(b))<<8) | (((unsigned long)(c))<<16) | (((unsigned long)(d))<<24))
#define VEC4(a,b,c,d) (((uint32_t)(a)) | (((uint32_t)(b))<<8) | (((uint32_t)(c))<<16) | (((uint32_t)(d))<<24))

// get byte 0 to 3 from word a
#define GetByte(a,n) ((unsigned char)((a) >> (n<<3)))
Expand Down Expand Up @@ -457,7 +458,7 @@ bool CheckRcon(bool create)
compute_one_final_inv(d,s,6,1,3,4,8); \
compute_one_final_inv(d,s,7,1,3,4,8);

unsigned long SubByte(unsigned long data)
uint32_t SubByte(uint32_t data)
{ // does the SBox on this 4 byte data
unsigned result = 0;
result = byte_sub[data>>24];
Expand Down Expand Up @@ -493,7 +494,7 @@ bool CreateAESTables(bool create)
void AES::KeyExpansion(const unsigned char * key)
{
int i;
unsigned long temp, * Wb = reinterpret_cast<unsigned long*>(W); // todo not portable - Endian problems
uint32_t temp, * Wb = reinterpret_cast<uint32_t*>(W); // todo not portable - Endian problems
if (Nk <= 6)
{
// todo - memcpy
Expand Down Expand Up @@ -562,12 +563,12 @@ void AES::EncryptBlock(const unsigned char * datain1, unsigned char * dataout1)
// todo - clean up - lots of repeated macros
// we only encrypt one block from now on

unsigned long state[8*2]; // 2 buffers
unsigned long * r_ptr = reinterpret_cast<unsigned long*>(W);
unsigned long * dest = state;
unsigned long * src = state;
const unsigned long * datain = reinterpret_cast<const unsigned long*>(datain1);
unsigned long * dataout = reinterpret_cast<unsigned long*>(dataout1);
uint32_t state[8*2]; // 2 buffers
uint32_t * r_ptr = reinterpret_cast<uint32_t*>(W);
uint32_t * dest = state;
uint32_t * src = state;
const uint32_t * datain = reinterpret_cast<const uint32_t*>(datain1);
uint32_t * dataout = reinterpret_cast<uint32_t*>(dataout1);

if (Nb == 4)
{
Expand Down Expand Up @@ -646,7 +647,7 @@ void AES::EncryptBlock(const unsigned char * datain1, unsigned char * dataout1)
} // Encrypt

// call this to encrypt any size block
void AES::Encrypt(const unsigned char * datain, unsigned char * dataout, unsigned long numBlocks, BlockMode mode)
void AES::Encrypt(const unsigned char * datain, unsigned char * dataout, uint32_t numBlocks, BlockMode mode)
{
if (0 == numBlocks)
return;
Expand Down Expand Up @@ -712,21 +713,21 @@ void AES::StartDecryption(const unsigned char * key)
}

// we reverse the rounds to make decryption faster
unsigned long * WL = reinterpret_cast<unsigned long*>(W);
uint32_t * WL = reinterpret_cast<uint32_t*>(W);
for (int pos = 0; pos < Nr/2; pos++)
for (int col = 0; col < Nb; col++)
swap(WL[col+pos*Nb],WL[col+(Nr-pos)*Nb]);
} // StartDecryption

void AES::DecryptBlock(const unsigned char * datain1, unsigned char * dataout1)
{
unsigned long state[8*2]; // 2 buffers
unsigned long * r_ptr = reinterpret_cast<unsigned long*>(W);
unsigned long * dest = state;
unsigned long * src = state;
uint32_t state[8*2]; // 2 buffers
uint32_t * r_ptr = reinterpret_cast<uint32_t*>(W);
uint32_t * dest = state;
uint32_t * src = state;

const unsigned long * datain = reinterpret_cast<const unsigned long*>(datain1);
unsigned long * dataout = reinterpret_cast<unsigned long*>(dataout1);
const uint32_t * datain = reinterpret_cast<const uint32_t*>(datain1);
uint32_t * dataout = reinterpret_cast<uint32_t*>(dataout1);

if (Nb == 4)
{
Expand Down Expand Up @@ -804,7 +805,7 @@ void AES::DecryptBlock(const unsigned char * datain1, unsigned char * dataout1)
} // Decrypt

// call this to decrypt any size block
void AES::Decrypt(const unsigned char * datain, unsigned char * dataout, unsigned long numBlocks, BlockMode mode)
void AES::Decrypt(const unsigned char * datain, unsigned char * dataout, uint32_t numBlocks, BlockMode mode)
{
if (0 == numBlocks)
return;
Expand Down
6 changes: 4 additions & 2 deletions AES.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#ifndef _AES_H
#define _AES_H

#include <stdint.h>

/* USAGE:
1. Create a AES class (or more as necessary)
2. Call class method SetParameters
Expand Down Expand Up @@ -91,7 +93,7 @@ class AES
// have enough space in datain and dataout to accomodate this. Pad your data before
// calling, preferably using the padding methods listed below.
// Decryption must use the same mode as the encryption.
void Encrypt(const unsigned char * datain, unsigned char * dataout, unsigned long numBlocks, BlockMode mode = CBC);
void Encrypt(const unsigned char * datain, unsigned char * dataout, uint32_t numBlocks, BlockMode mode = CBC);

// call this before any decryption with the key to use
void StartDecryption(const unsigned char * key);
Expand All @@ -102,7 +104,7 @@ class AES
// calling, preferably using the padding methods listed below. You must know the desired
// length of the output data, since all the blocks are returned decrypted.
// Encryption must use the same mode as the decryption.
void Decrypt(const unsigned char * datain, unsigned char * dataout, unsigned long numBlocks, BlockMode mode = CBC);
void Decrypt(const unsigned char * datain, unsigned char * dataout, uint32_t numBlocks, BlockMode mode = CBC);

private:

Expand Down

0 comments on commit 874eeeb

Please sign in to comment.