This commit is contained in:
y4my4my4m 2023-12-28 05:28:23 -05:00
parent 8cf3b95bcb
commit 2b847de048

323
src/Home/AES128/AES128.ZC Normal file
View file

@ -0,0 +1,323 @@
#define Nb 4
#define Nk 4
#define Nr 10 // amount of rounds to expand the key
// could be calculated on the fly too... i guess
// https://en.wikipedia.org/wiki/Rijndael_S-box
U8 sBox[256] = {
//0 1 2 3 4 5 6 7 8 9 A B C D E F
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0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0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb
};
// Mul2 and Mul3 could be precomputed, but here's a simple implementation
// Note: This is a simplified and not optimized version
U8 xtime(U8 x) {
return (x << 1) ^ (((x >> 7) & 1) * 0x1b);
}
U8 Mul2[256], Mul3[256];
U0 InitMulTables() {
I64 i;
for (i = 0; i < 256; i++) {
Mul2[i] = xtime(i);
Mul3[i] = xtime(i) ^ i;
}
}
U0 KeyExpansion(U8 *RoundKey, U8 *Key) {
I64 i, j;
// Copy the original key to the round key
for (i = 0; i < Nk; i++) {
RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
}
// Expand the round key
for (i = Nk; i < Nb * (Nr + 1); i++) {
U8 temp[4];
for (j = 0; j < 4; j++) {
temp[j] = RoundKey[(i-1) * 4 + j];
}
if (i % Nk == 0) {
// Perform the rotation within temp array, substitute with S-box, then XOR with Rcon[i/Nk]
I64 k = temp[0]; // should be I64 but kept throwing a warning about 64bit variable somehow???
k = k & 0xff; // this is an attempt at forcing U8
temp[0] = sBox[temp[1]] ^ Rcon[i/Nk];
temp[1] = sBox[temp[2]];
temp[2] = sBox[temp[3]];
temp[3] = sBox[k];
}
for (j = 0; j < 4; j++) {
RoundKey[i * 4 + j] = RoundKey[(i - Nk) * 4 + j] ^ temp[j];
}
}
}
U0 AddRoundKey(U8 round, U8 *state, U8 *RoundKey) {
I64 i,j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
state[j * 4 + i] ^= RoundKey[round * Nb * 4 + i * Nb + j];
}
}
}
U0 SubBytes(U8 *state) {
I64 i;
for (i = 0; i < 16; i++) {
state[i] = sBox[state[i]];
}
}
U0 ShiftRows(U8 *state) {
I64 i;
U8 temp[16];
// Store state in temp
for (i = 0; i < 16; i++) {
temp[i] = state[i];
}
// Shift first row 1 columns to left
state[0] = temp[0];
state[4] = temp[5];
state[8] = temp[10];
state[12] = temp[15];
// Shift third row 2 steps to the left
state[2] = temp[10];
state[6] = temp[14];
state[10] = temp[2];
state[14] = temp[6];
// Shift fourth row 3 steps to the left
state[3] = temp[15];
state[7] = temp[3];
state[11] = temp[7];
state[15] = temp[11];
}
U0 MixColumns(U8 *state) {
U8 temp[16];
I64 i;
// Store state in temp
for (i = 0; i < 16; i++) {
temp[i] = state[i];
}
for (i = 0; i < 4; i++) {
state[i * 4 + 0] = Mul2[temp[i * 4 + 0]] ^ Mul3[temp[i * 4 + 1]] ^ temp[i * 4 + 2] ^ temp[i * 4 + 3];
state[i * 4 + 1] = temp[i * 4 + 0] ^ Mul2[temp[i * 4 + 1]] ^ Mul3[temp[i * 4 + 2]] ^ temp[i * 4 + 3];
state[i * 4 + 2] = temp[i * 4 + 0] ^ temp[i * 4 + 1] ^ Mul2[temp[i * 4 + 2]] ^ Mul3[temp[i * 4 + 3]];
state[i * 4 + 3] = Mul3[temp[i * 4 + 0]] ^ temp[i * 4 + 1] ^ temp[i * 4 + 2] ^ Mul2[temp[i * 4 + 3]];
}
}
// U0 InvSubBytes(U8 *state) {
// // Implement inverse of SubBytes
// }
// U0 InvShiftRows(U8 *state) {
// // Implement inverse of ShiftRows
// }
// U0 InvMixColumns(U8 *state) {
// // Implement inverse of MixColumns
// }
// U0 InvCipher(U8 *state, U8 *RoundKey) {
// I64 round;
// // Start by adding the last round key
// AddRoundKey(Nr, state, RoundKey);
// // Perform the rounds in reverse order
// for (round = Nr - 1; round > 0; round--) {
// InvShiftRows(state);
// InvSubBytes(state);
// AddRoundKey(round, state, RoundKey);
// InvMixColumns(state);
// }
// // Final round (without MixColumns)
// InvShiftRows(state);
// InvSubBytes(state);
// AddRoundKey(0, state, RoundKey);
// }
U0 Cipher(U8 *state, U8 *RoundKey) {
I64 round;
// Add the First round key to the state before starting the rounds
AddRoundKey(0, state, RoundKey);
// There will be Nr rounds
for (round = 1; round < Nr; ++round) {
SubBytes(state);
ShiftRows(state);
MixColumns(state);
AddRoundKey(round, state, RoundKey);
}
// The last round is given below
SubBytes(state);
ShiftRows(state);
AddRoundKey(Nr, state, RoundKey);
}
U0 AES_init_ctx(U8 *ctx, U8 *key) {
KeyExpansion(ctx, key);
}
U0 AES_ECB_encrypt(U8 *ctx, U8 *buf) {
// Buffer size is assumed to be multiple of 16 bytes! (AES block size)
Cipher(buf, ctx);
}
// U0 AES_ECB_decrypt(U8 *ctx, U8 *buf) {
// InvCipher(buf, ctx);
// }
U0 PrintHex(U8 *buf, I64 len) {
I64 i;
for (i = 0; i < len; i++) "%02X ", buf[i];
"\n";
}
U8 HexCharToByte(U8 ch) {
if (ch >= '0' && ch <= '9') return ch - '0';
if (ch >= 'a' && ch <= 'f') return 10 + ch - 'a';
if (ch >= 'A' && ch <= 'F') return 10 + ch - 'A';
return 0; // Non-hex character
}
U0 HexStringToByteArray(U8 *hexString, U8 *byteArray, I64 byteArrayLength) {
I64 i;
for (i = 0; i < byteArrayLength; i++) {
byteArray[i] = HexCharToByte(hexString[2*i]) << 4 | HexCharToByte(hexString[2*i + 1]);
}
}
// 9d3492551250741cef1bf7892fabd07b
U8 key[16] = {0x9d, 0x34, 0x92, 0x55, 0x12, 0x50, 0x74, 0x1c, 0xef, 0x1b, 0xf7, 0x89, 0x2f, 0xab, 0xd0, 0x7b};
// 0c40d19e21ac91fad03b1a8e5349f245
U8 buf[16] = {0x0c, 0x40, 0xd1, 0x9e, 0x21, 0xac, 0x91, 0xfa, 0xd0, 0x3b, 0x1a, 0x8e, 0x53, 0x49, 0xf2, 0x45};
U0 Main() {
U8 ctx[176]; // Context (expanded key)
InitMulTables();
AES_init_ctx(ctx, key);
AES_ECB_encrypt(ctx, buf);
// `buf` now contains the ciphertext
"Result:\n$$LTRED$$";
PrintHex(buf, 16);
"\n$$FG$$";
}
Main;
// U0 Main() {
// U8 ctx[176]; // Context (expanded key)
// InitMulTables;
// AES_init_ctx(ctx, key);
// "ctx:\n";
// PrintHex(ctx, 176);
// "\n";
// AES_ECB_encrypt(ctx, buf);
// "buf:\n$$LTGREEN$$";
// PrintHex(buf, 16);
// "\n$$FG$$";
// }
// Main;
// // 9d3492551250741cef1bf7892fabd07b
// U8 key[16] = {0x9d, 0x34, 0x92, 0x55, 0x12, 0x50, 0x74, 0x1c, 0xef, 0x1b, 0xf7, 0x89, 0x2f, 0xab, 0xd0, 0x7b};
// // 0c40d19e21ac91fad03b1a8e5349f245
// U8 buf[16] = {0x0c, 0x40, 0xd1, 0x9e, 0x21, 0xac, 0x91, 0xfa, 0xd0, 0x3b, 0x1a, 0x8e, 0x53, 0x49, 0xf2, 0x45};
// U0 Main(U8 *keyHexString, U8 *plaintextHexString) {
// U8 ctx[176]; // Context (expanded key)
// InitMulTables();
// // Convert hex string to byte array for the key
// HexStringToByteArray(keyHexString, key, 16);
// // Convert hex string to byte array for the plaintext
// HexStringToByteArray(plaintextHexString, buf, 16);
// AES_init_ctx(ctx, key);
// AES_ECB_encrypt(ctx, buf);
// // `buf` now contains the ciphertext
// PrintHex(buf, 16);
// }
// // Main("9d3492551250741cef1bf7892fabd07b", "0c40d19e21ac91fad03b1a8e5349f245");