123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- #include "aes.h"
- #if defined(_MSC_VER)
- #include <intrin.h>
- #else
- #include <x86intrin.h>
- #endif
- extern const uint32_t accelc_aes_rcon[11];
- extern const uint8_t accelc_aes_SBox[256];
- extern const uint8_t accelc_aes_InverseSBox[256];
- extern const uint8_t accelc_aes_GF2p8_Mul_0x02[256];
- extern const uint8_t accelc_aes_GF2p8_Mul_0x03[256];
- extern const uint8_t accelc_aes_GF2p8_Mul_0x09[256];
- extern const uint8_t accelc_aes_GF2p8_Mul_0x0B[256];
- extern const uint8_t accelc_aes_GF2p8_Mul_0x0D[256];
- extern const uint8_t accelc_aes_GF2p8_Mul_0x0E[256];
- #define Swap(X, Y, Temp) \
- Temp = X; \
- X = Y; \
- Y = Temp;
- void accelc_AES128_encrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
- uint8_t ShiftTemp = 0;
- for (int i = 1; i < 10; ++i) {
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
- //Shift rows starts;
- //Shift the second row;
- Swap(srcBytes[1], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[13], ShiftTemp)
- //Shift the third row;
- Swap(srcBytes[2], srcBytes[10], ShiftTemp)
- Swap(srcBytes[6], srcBytes[14], ShiftTemp)
- //Shift the fourth row;
- Swap(srcBytes[3], srcBytes[15], ShiftTemp)
- Swap(srcBytes[15], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[7], ShiftTemp)
- //Shift rows ends;
- for (int j = 0; j < 16; j += 4) {
- uint8_t temp[4];
- *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
- srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x02[temp[0]] ^ accelc_aes_GF2p8_Mul_0x03[temp[1]] ^ temp[2] ^ temp[3]);
- srcBytes[j + 1] = (uint8_t)(temp[0] ^ accelc_aes_GF2p8_Mul_0x02[temp[1]] ^ accelc_aes_GF2p8_Mul_0x03[temp[2]] ^ temp[3]);
- srcBytes[j + 2] = (uint8_t)(temp[0] ^ temp[1] ^ accelc_aes_GF2p8_Mul_0x02[temp[2]] ^ accelc_aes_GF2p8_Mul_0x03[temp[3]]);
- srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x03[temp[0]] ^ temp[1] ^ temp[2] ^ accelc_aes_GF2p8_Mul_0x02[temp[3]]);
- }
- ((uint64_t*)(srcBytes))[0] ^= srcKey->qword[i * 2];
- ((uint64_t*)(srcBytes))[1] ^= srcKey->qword[i * 2 + 1];
- }
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
- //Shift rows starts;
- //Shift the second row;
- Swap(srcBytes[1], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[13], ShiftTemp)
- //Shift the third row;
- Swap(srcBytes[2], srcBytes[10], ShiftTemp)
- Swap(srcBytes[6], srcBytes[14], ShiftTemp)
- //Shift the fourth row;
- Swap(srcBytes[3], srcBytes[15], ShiftTemp)
- Swap(srcBytes[15], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[7], ShiftTemp)
- //Shift rows ends;
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[20];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[21];
- }
- void accelc_AES128_decrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[20];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[21];
- uint8_t ShiftTemp = 0;
- for (int i = 9; i > 0; --i) {
- //Inverse Shift rows starts;
- //Inverse shift the second row;
- Swap(srcBytes[13], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[1], ShiftTemp)
- //Inverse shift the third row;
- Swap(srcBytes[14], srcBytes[6], ShiftTemp)
- Swap(srcBytes[10], srcBytes[2], ShiftTemp)
- //Inverse shift the fourth row;
- Swap(srcBytes[3], srcBytes[7], ShiftTemp)
- Swap(srcBytes[7], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[15], ShiftTemp)
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
- for (int j = 0; j < 16; j += 4) {
- uint8_t temp[4];
- *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
- srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0E[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[2]] ^ accelc_aes_GF2p8_Mul_0x09[temp[3]]);
- srcBytes[j + 1] = (uint8_t)(accelc_aes_GF2p8_Mul_0x09[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[3]]);
- srcBytes[j + 2] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0D[temp[0]] ^ accelc_aes_GF2p8_Mul_0x09[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[3]]);
- srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0B[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[1]] ^ accelc_aes_GF2p8_Mul_0x09[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[3]]);
- }
- }
- //Inverse Shift rows starts;
- //Inverse shift the second row;
- Swap(srcBytes[13], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[1], ShiftTemp)
- //Inverse shift the third row;
- Swap(srcBytes[14], srcBytes[6], ShiftTemp)
- Swap(srcBytes[10], srcBytes[2], ShiftTemp)
- //Inverse shift the fourth row;
- Swap(srcBytes[3], srcBytes[7], ShiftTemp)
- Swap(srcBytes[7], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[15], ShiftTemp)
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
- }
- void accelc_AES128_set_key(const uint8_t srcUserKey[16], AES_KEY* dstKey) {
- dstKey->qword[0] = ((const uint64_t*)srcUserKey)[0];
- dstKey->qword[1] = ((const uint64_t*)srcUserKey)[1];
- for (int i = 4; i < 44; ++i) {
- uint32_t temp = dstKey->dword[i - 1];
- if (i % 4 == 0) {
- temp = _rotr(temp, 8);
- ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
- ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
- ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
- ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
- temp ^= accelc_aes_rcon[i / 4];
- }
- dstKey->dword[i] = dstKey->dword[i - 4] ^ temp;
- }
- }
- void accelc_AES192_encrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
- uint8_t ShiftTemp = 0;
- for (int i = 1; i < 12; ++i) {
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
- //Shift rows starts;
- //Shift the second row;
- Swap(srcBytes[1], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[13], ShiftTemp)
- //Shift the third row;
- Swap(srcBytes[2], srcBytes[10], ShiftTemp)
- Swap(srcBytes[6], srcBytes[14], ShiftTemp)
- //Shift the fourth row;
- Swap(srcBytes[3], srcBytes[15], ShiftTemp)
- Swap(srcBytes[15], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[7], ShiftTemp)
- //Shift rows ends;
- for (int j = 0; j < 16; j += 4) {
- uint8_t temp[4];
- *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
- srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x02[temp[0]] ^ accelc_aes_GF2p8_Mul_0x03[temp[1]] ^ temp[2] ^ temp[3]);
- srcBytes[j + 1] = (uint8_t)(temp[0] ^ accelc_aes_GF2p8_Mul_0x02[temp[1]] ^ accelc_aes_GF2p8_Mul_0x03[temp[2]] ^ temp[3]);
- srcBytes[j + 2] = (uint8_t)(temp[0] ^ temp[1] ^ accelc_aes_GF2p8_Mul_0x02[temp[2]] ^ accelc_aes_GF2p8_Mul_0x03[temp[3]]);
- srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x03[temp[0]] ^ temp[1] ^ temp[2] ^ accelc_aes_GF2p8_Mul_0x02[temp[3]]);
- }
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
- }
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
- //Shift rows starts;
- //Shift the second row;
- Swap(srcBytes[1], srcBytes[5], ShiftTemp) //Swap is a MACRO, no need to add ';'.
- Swap(srcBytes[5], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[13], ShiftTemp)
- //Shift the third row;
- Swap(srcBytes[2], srcBytes[10], ShiftTemp)
- Swap(srcBytes[6], srcBytes[14], ShiftTemp)
- //Shift the fourth row;
- Swap(srcBytes[3], srcBytes[15], ShiftTemp)
- Swap(srcBytes[15], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[7], ShiftTemp)
- //Shift rows ends;
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[24];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[25];
- }
- void accelc_AES192_decrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[24];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[25];
- uint8_t ShiftTemp = 0;
- for (int i = 11; i > 0; --i) {
- //Inverse Shift rows starts;
- //Inverse shift the second row;
- Swap(srcBytes[13], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[1], ShiftTemp)
- //Inverse shift the third row;
- Swap(srcBytes[14], srcBytes[6], ShiftTemp)
- Swap(srcBytes[10], srcBytes[2], ShiftTemp)
- //Inverse shift the fourth row;
- Swap(srcBytes[3], srcBytes[7], ShiftTemp)
- Swap(srcBytes[7], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[15], ShiftTemp)
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
- for (int j = 0; j < 16; j += 4) {
- uint8_t temp[4];
- *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
- srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0E[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[2]] ^ accelc_aes_GF2p8_Mul_0x09[temp[3]]);
- srcBytes[j + 1] = (uint8_t)(accelc_aes_GF2p8_Mul_0x09[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[3]]);
- srcBytes[j + 2] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0D[temp[0]] ^ accelc_aes_GF2p8_Mul_0x09[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[3]]);
- srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0B[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[1]] ^ accelc_aes_GF2p8_Mul_0x09[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[3]]);
- }
- }
- //Inverse Shift rows starts;
- //Inverse shift the second row;
- Swap(srcBytes[13], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[1], ShiftTemp)
- //Inverse shift the third row;
- Swap(srcBytes[14], srcBytes[6], ShiftTemp)
- Swap(srcBytes[10], srcBytes[2], ShiftTemp)
- //Inverse shift the fourth row;
- Swap(srcBytes[3], srcBytes[7], ShiftTemp)
- Swap(srcBytes[7], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[15], ShiftTemp)
- for (uint8_t j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
- }
- void accelc_AES192_set_key(const uint8_t srcUserKey[24], AES_KEY* dstKey) {
- dstKey->qword[0] = ((const uint64_t*)srcUserKey)[0];
- dstKey->qword[1] = ((const uint64_t*)srcUserKey)[1];
- dstKey->qword[2] = ((const uint64_t*)srcUserKey)[2];
- for (int i = 6; i < 52; ++i) {
- uint32_t temp = dstKey->dword[i - 1];
- if (i % 6 == 0) {
- temp = _rotr(temp, 8);
- ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
- ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
- ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
- ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
- temp ^= accelc_aes_rcon[i / 6];
- }
- dstKey->dword[i] = dstKey->dword[i - 6] ^ temp;
- }
- }
- void accelc_AES256_encrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
- uint8_t ShiftTemp = 0;
- for (int i = 1; i < 14; ++i) {
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
- //Shift rows starts;
- //Shift the second row;
- Swap(srcBytes[1], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[13], ShiftTemp)
- //Shift the third row;
- Swap(srcBytes[2], srcBytes[10], ShiftTemp)
- Swap(srcBytes[6], srcBytes[14], ShiftTemp)
- //Shift the fourth row;
- Swap(srcBytes[3], srcBytes[15], ShiftTemp)
- Swap(srcBytes[15], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[7], ShiftTemp)
- //Shift rows ends;
- for (int j = 0; j < 16; j += 4) {
- uint8_t temp[4];
- *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
- srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x02[temp[0]] ^ accelc_aes_GF2p8_Mul_0x03[temp[1]] ^ temp[2] ^ temp[3]);
- srcBytes[j + 1] = (uint8_t)(temp[0] ^ accelc_aes_GF2p8_Mul_0x02[temp[1]] ^ accelc_aes_GF2p8_Mul_0x03[temp[2]] ^ temp[3]);
- srcBytes[j + 2] = (uint8_t)(temp[0] ^ temp[1] ^ accelc_aes_GF2p8_Mul_0x02[temp[2]] ^ accelc_aes_GF2p8_Mul_0x03[temp[3]]);
- srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x03[temp[0]] ^ temp[1] ^ temp[2] ^ accelc_aes_GF2p8_Mul_0x02[temp[3]]);
- }
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
- }
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
- //Shift rows starts;
- //Shift the second row;
- Swap(srcBytes[1], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[13], ShiftTemp)
- //Shift the third row;
- Swap(srcBytes[2], srcBytes[10], ShiftTemp)
- Swap(srcBytes[6], srcBytes[14], ShiftTemp)
- //Shift the fourth row;
- Swap(srcBytes[3], srcBytes[15], ShiftTemp)
- Swap(srcBytes[15], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[7], ShiftTemp)
- //Shift rows ends;
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[28];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[29];
- }
- void accelc_AES256_decrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[28];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[29];
- uint8_t ShiftTemp = 0;
- for (int i = 13; i > 0; --i) {
- //Inverse Shift rows starts;
- //Inverse shift the second row;
- Swap(srcBytes[13], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[1], ShiftTemp)
- //Inverse shift the third row;
- Swap(srcBytes[14], srcBytes[6], ShiftTemp)
- Swap(srcBytes[10], srcBytes[2], ShiftTemp)
- //Inverse shift the fourth row;
- Swap(srcBytes[3], srcBytes[7], ShiftTemp)
- Swap(srcBytes[7], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[15], ShiftTemp)
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
- for (int j = 0; j < 16; j += 4) {
- uint8_t temp[4];
- *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
- srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0E[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[2]] ^ accelc_aes_GF2p8_Mul_0x09[temp[3]]);
- srcBytes[j + 1] = (uint8_t)(accelc_aes_GF2p8_Mul_0x09[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[3]]);
- srcBytes[j + 2] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0D[temp[0]] ^ accelc_aes_GF2p8_Mul_0x09[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[3]]);
- srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0B[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[1]] ^ accelc_aes_GF2p8_Mul_0x09[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[3]]);
- }
- }
- //Inverse Shift rows starts;
- //Inverse shift the second row;
- Swap(srcBytes[13], srcBytes[9], ShiftTemp)
- Swap(srcBytes[9], srcBytes[5], ShiftTemp)
- Swap(srcBytes[5], srcBytes[1], ShiftTemp)
- //Inverse shift the third row;
- Swap(srcBytes[14], srcBytes[6], ShiftTemp)
- Swap(srcBytes[10], srcBytes[2], ShiftTemp)
- //Inverse shift the fourth row;
- Swap(srcBytes[3], srcBytes[7], ShiftTemp)
- Swap(srcBytes[7], srcBytes[11], ShiftTemp)
- Swap(srcBytes[11], srcBytes[15], ShiftTemp)
- for (int j = 0; j < 16; ++j)
- srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
- ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
- ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
- }
- void accelc_AES256_set_key(const uint8_t srcUserKey[32], AES_KEY* dstKey) {
- dstKey->qword[0] = ((const uint64_t*)srcUserKey)[0];
- dstKey->qword[1] = ((const uint64_t*)srcUserKey)[1];
- dstKey->qword[2] = ((const uint64_t*)srcUserKey)[2];
- dstKey->qword[3] = ((const uint64_t*)srcUserKey)[3];
- for (int i = 8; i < 60; ++i) {
- uint32_t temp = dstKey->dword[i - 1];
- if (i % 8 == 0) {
- temp = _rotr(temp, 8);
- ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
- ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
- ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
- ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
- temp ^= accelc_aes_rcon[i / 8];
- }
- if (i % 8 == 4) {
- ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
- ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
- ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
- ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
- }
- dstKey->dword[i] = dstKey->dword[i - 8] ^ temp;
- }
- }
|