Commit b146090a authored by kokke's avatar kokke
Browse files

Merge pull request #7 from mb300sd/master

No-copy operation - thank you for that contribution mbed300sd!
parents 6a3c6429 df5b2d57
...@@ -60,7 +60,9 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) ...@@ -60,7 +60,9 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
// in - pointer to the CipherText to be decrypted. // in - pointer to the CipherText to be decrypted.
// out - pointer to buffer to hold output of the decryption. // out - pointer to buffer to hold output of the decryption.
// state - array holding the intermediate results during decryption. // state - array holding the intermediate results during decryption.
static uint8_t* in, *out, state[4][4]; //static uint8_t* in, *out, state[4][4];
typedef uint8_t state_t[4][4];
static state_t* state;
// The array that stores the round keys. // The array that stores the round keys.
static uint8_t RoundKey[176]; static uint8_t RoundKey[176];
...@@ -219,7 +221,7 @@ static void AddRoundKey(uint8_t round) ...@@ -219,7 +221,7 @@ static void AddRoundKey(uint8_t round)
{ {
for(j = 0; j < 4; ++j) for(j = 0; j < 4; ++j)
{ {
state[j][i] ^= RoundKey[round * Nb * 4 + i * Nb + j]; (*state)[i][j] ^= RoundKey[round * Nb * 4 + i * Nb + j];
} }
} }
} }
...@@ -233,7 +235,7 @@ static void SubBytes(void) ...@@ -233,7 +235,7 @@ static void SubBytes(void)
{ {
for(j = 0; j < 4; ++j) for(j = 0; j < 4; ++j)
{ {
state[i][j] = getSBoxValue(state[i][j]); (*state)[j][i] = getSBoxValue((*state)[j][i]);
} }
} }
} }
...@@ -246,27 +248,27 @@ static void ShiftRows(void) ...@@ -246,27 +248,27 @@ static void ShiftRows(void)
uint8_t temp; uint8_t temp;
// Rotate first row 1 columns to left // Rotate first row 1 columns to left
temp = state[1][0]; temp = (*state)[0][1];
state[1][0] = state[1][1]; (*state)[0][1] = (*state)[1][1];
state[1][1] = state[1][2]; (*state)[1][1] = (*state)[2][1];
state[1][2] = state[1][3]; (*state)[2][1] = (*state)[3][1];
state[1][3] = temp; (*state)[3][1] = temp;
// Rotate second row 2 columns to left // Rotate second row 2 columns to left
temp = state[2][0]; temp = (*state)[0][2];
state[2][0] = state[2][2]; (*state)[0][2] = (*state)[2][2];
state[2][2] = temp; (*state)[2][2] = temp;
temp = state[2][1]; temp = (*state)[1][2];
state[2][1] = state[2][3]; (*state)[1][2] = (*state)[3][2];
state[2][3] = temp; (*state)[3][2] = temp;
// Rotate third row 3 columns to left // Rotate third row 3 columns to left
temp = state[3][0]; temp = (*state)[0][3];
state[3][0] = state[3][3]; (*state)[0][3] = (*state)[3][3];
state[3][3] = state[3][2]; (*state)[3][3] = (*state)[2][3];
state[3][2] = state[3][1]; (*state)[2][3] = (*state)[1][3];
state[3][1] = temp; (*state)[1][3] = temp;
} }
static uint8_t xtime(uint8_t x) static uint8_t xtime(uint8_t x)
...@@ -281,12 +283,12 @@ static void MixColumns(void) ...@@ -281,12 +283,12 @@ static void MixColumns(void)
uint8_t Tmp,Tm,t; uint8_t Tmp,Tm,t;
for(i = 0; i < 4; ++i) for(i = 0; i < 4; ++i)
{ {
t = state[0][i]; t = (*state)[i][0];
Tmp = state[0][i] ^ state[1][i] ^ state[2][i] ^ state[3][i] ; Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
Tm = state[0][i] ^ state[1][i] ; Tm = xtime(Tm); state[0][i] ^= Tm ^ Tmp ; Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
Tm = state[1][i] ^ state[2][i] ; Tm = xtime(Tm); state[1][i] ^= Tm ^ Tmp ; Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
Tm = state[2][i] ^ state[3][i] ; Tm = xtime(Tm); state[2][i] ^= Tm ^ Tmp ; Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
Tm = state[3][i] ^ t ; Tm = xtime(Tm); state[3][i] ^= Tm ^ Tmp ; Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
} }
} }
...@@ -322,16 +324,16 @@ static void InvMixColumns(void) ...@@ -322,16 +324,16 @@ static void InvMixColumns(void)
for(i=0;i<4;++i) for(i=0;i<4;++i)
{ {
a = state[0][i]; a = (*state)[i][0];
b = state[1][i]; b = (*state)[i][1];
c = state[2][i]; c = (*state)[i][2];
d = state[3][i]; d = (*state)[i][3];
state[0][i] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
state[1][i] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
state[2][i] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
state[3][i] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
} }
} }
...@@ -345,7 +347,7 @@ static void InvSubBytes(void) ...@@ -345,7 +347,7 @@ static void InvSubBytes(void)
{ {
for(j=0;j<4;++j) for(j=0;j<4;++j)
{ {
state[i][j] = getSBoxInvert(state[i][j]); (*state)[j][i] = getSBoxInvert((*state)[j][i]);
} }
} }
} }
...@@ -355,43 +357,44 @@ static void InvShiftRows(void) ...@@ -355,43 +357,44 @@ static void InvShiftRows(void)
uint8_t temp; uint8_t temp;
// Rotate first row 1 columns to right // Rotate first row 1 columns to right
temp=state[1][3]; temp=(*state)[3][1];
state[1][3]=state[1][2]; (*state)[3][1]=(*state)[2][1];
state[1][2]=state[1][1]; (*state)[2][1]=(*state)[1][1];
state[1][1]=state[1][0]; (*state)[1][1]=(*state)[0][1];
state[1][0]=temp; (*state)[0][1]=temp;
// Rotate second row 2 columns to right // Rotate second row 2 columns to right
temp=state[2][0]; temp=(*state)[0][2];
state[2][0]=state[2][2]; (*state)[0][2]=(*state)[2][2];
state[2][2]=temp; (*state)[2][2]=temp;
temp=state[2][1]; temp=(*state)[1][2];
state[2][1]=state[2][3]; (*state)[1][2]=(*state)[3][2];
state[2][3]=temp; (*state)[3][2]=temp;
// Rotate third row 3 columns to right // Rotate third row 3 columns to right
temp=state[3][0]; temp=(*state)[0][3];
state[3][0]=state[3][1]; (*state)[0][3]=(*state)[1][3];
state[3][1]=state[3][2]; (*state)[1][3]=(*state)[2][3];
state[3][2]=state[3][3]; (*state)[2][3]=(*state)[3][3];
state[3][3]=temp; (*state)[3][3]=temp;
} }
// Cipher is the main function that encrypts the PlainText. // Cipher is the main function that encrypts the PlainText.
static void Cipher(void) static void Cipher(void)
{ {
uint8_t i, j, round = 0; //uint8_t i, j,
uint8_t round = 0;
// Copy the input PlainText to state array. // Copy the input PlainText to state array.
for(i = 0; i < 4; ++i) // for(i = 0; i < 4; ++i)
{ // {
for(j = 0; j < 4 ; ++j) // for(j = 0; j < 4 ; ++j)
{ // {
state[j][i] = in[(i * 4) + j]; // (*state)[i][j] = in[(i * 4) + j];
} // }
} // }
// Add the First round key to the state before starting the rounds. // Add the First round key to the state before starting the rounds.
AddRoundKey(0); AddRoundKey(0);
...@@ -415,27 +418,28 @@ static void Cipher(void) ...@@ -415,27 +418,28 @@ static void Cipher(void)
// The encryption process is over. // The encryption process is over.
// Copy the state array to output array. // Copy the state array to output array.
for(i = 0; i < 4; ++i) // for(i = 0; i < 4; ++i)
{ // {
for(j = 0; j < 4; ++j) // for(j = 0; j < 4; ++j)
{ // {
out[(i * 4) + j] = state[j][i]; // out[(i * 4) + j] = (*state)[i][j];
} // }
} // }
} }
static void InvCipher(void) static void InvCipher(void)
{ {
uint8_t i,j,round=0; //uint8_t i,j,
uint8_t round=0;
// Copy the input CipherText to state array. // Copy the input CipherText to state array.
for(i=0;i<4;++i) // for(i=0;i<4;++i)
{ // {
for(j=0;j<4;++j) // for(j=0;j<4;++j)
{ // {
state[j][i] = in[i*4 + j]; // (*state)[i][j] = in[i*4 + j];
} // }
} // }
// Add the First round key to the state before starting the rounds. // Add the First round key to the state before starting the rounds.
AddRoundKey(Nr); AddRoundKey(Nr);
...@@ -459,13 +463,13 @@ static void InvCipher(void) ...@@ -459,13 +463,13 @@ static void InvCipher(void)
// The decryption process is over. // The decryption process is over.
// Copy the state array to output array. // Copy the state array to output array.
for(i=0;i<4;++i) // for(i=0;i<4;++i)
{ // {
for(j=0;j<4;++j) // for(j=0;j<4;++j)
{ // {
out[i*4+j]=state[j][i]; // out[i*4+j]=(*state)[i][j];
} // }
} // }
} }
...@@ -473,12 +477,11 @@ static void InvCipher(void) ...@@ -473,12 +477,11 @@ static void InvCipher(void)
/* Public functions: */ /* Public functions: */
/*****************************************************************************/ /*****************************************************************************/
void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output) void AES128_ECB_encrypt_nc(uint8_t* data, const uint8_t* key)
{ {
// Copy the Key and CipherText // Copy the Key and CipherText
Key = key; Key = key;
in = input; state = (state_t*)data;
out = output;
// The KeyExpansion routine must be called before encryption. // The KeyExpansion routine must be called before encryption.
KeyExpansion(); KeyExpansion();
...@@ -487,14 +490,28 @@ void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output) ...@@ -487,14 +490,28 @@ void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output)
Cipher(); Cipher();
} }
void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output) void AES128_ECB_decrypt_nc(uint8_t* data, const uint8_t* key)
{ {
Key = key; Key = key;
in = input; state = (state_t*)data;
out = output;
KeyExpansion(); KeyExpansion();
InvCipher(); InvCipher();
} }
void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output)
{
uint8_t i;
for (i=0;i<16;++i)
output[i] = input[i];
AES128_ECB_encrypt_nc(output, key);
}
void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output)
{
uint8_t i;
for (i=0;i<16;++i)
output[i] = input[i];
AES128_ECB_decrypt_nc(output, key);
}
...@@ -5,5 +5,7 @@ ...@@ -5,5 +5,7 @@
void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output); void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output);
void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output); void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output);
void AES128_ECB_encrypt_nc(uint8_t* data, const uint8_t* key);
void AES128_ECB_decrypt_nc(uint8_t* data, const uint8_t* key);
#endif //_AES_H_ #endif //_AES_H_
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment