📄 aes_ref_encr.c
字号:
/* rijndael-alg-ref.c v2.0 August '99 * Reference ANSI C code * authors: Paulo Barreto * Vincent Rijmen * * (NIST AES reference version) * modification (no decryption, no encrypt round functions, 128 bit only): * Reinhard Wobst, @(#) Sep 21 2004, 09:28:30 * This heavily reduced version is dedicated to the German * ministry of finances. */#include <stdio.h>#include <stdlib.h>#include "aes_ref_encr.h"#include "boxes-ref.dat"#define SC ((BC - 4) >> 1)static word8 shifts[3][4][2] = { { {0, 0}, {1, 3}, {2, 2}, {3, 1} }, { {0, 0}, {1, 5}, {2, 4}, {3, 3} }, { {0, 0}, {1, 7}, {3, 5}, {4, 4} }};/* multiply two elements of GF(2^m) * needed for MixColumn */static word8 mul(word8 a, word8 b){ if (a && b) return Alogtable[(Logtable[a] + Logtable[b]) % 255]; else return 0;}/* Exor corresponding text input and round key input bytes */static void KeyAddition(word8 a[4][MAXBC], word8 rk[4][MAXBC], word8 BC){ int i, j; for (i = 0; i < 4; i++) for (j = 0; j < BC; j++) a[i][j] ^= rk[i][j];}/* Row 0 remains unchanged * the other three rows are shifted a variable amount */static void ShiftRow(word8 a[4][MAXBC], word8 d, word8 BC){ word8 tmp[MAXBC]; int i, j; for (i = 1; i < 4; i++) { for (j = 0; j < BC; j++) tmp[j] = a[i][(j + shifts[SC][i][d]) % BC]; for (j = 0; j < BC; j++) a[i][j] = tmp[j]; }}/* Replace every byte of the input by the byte at that place * in the nonlinear S-box */static void Substitution(word8 a[4][MAXBC], word8 box[256], word8 BC){ int i, j; for (i = 0; i < 4; i++) for (j = 0; j < BC; j++) a[i][j] = box[a[i][j]];}/* Mix the four bytes of every column in a linear way */static void MixColumn(word8 a[4][MAXBC], word8 BC){ word8 b[4][MAXBC]; int i, j; for (j = 0; j < BC; j++) for (i = 0; i < 4; i++) b[i][j] = mul(2, a[i][j]) ^ mul(3, a[(i + 1) % 4][j]) ^ a[(i + 2) % 4][j] ^ a[(i + 3) % 4][j]; for (i = 0; i < 4; i++) for (j = 0; j < BC; j++) a[i][j] = b[i][j];}/* Calculate the necessary round keys * The number of calculations depends on keyBits and blockBits */int rijndaelKeySched(word8 k[4][MAXKC], int keyBits, int blockBits, word8 W[MAXROUNDS + 1][4][MAXBC]){ const int KC = 4; const int BC = 4; const int ROUNDS = 10; int i, j, t, rconpointer = 0; word8 tk[4][MAXKC]; for (j = 0; j < KC; j++) for (i = 0; i < 4; i++) tk[i][j] = k[i][j]; t = 0; /* copy values into round key array */ for (j = 0; (j < KC) && (t < (ROUNDS + 1) * BC); j++, t++) for (i = 0; i < 4; i++) W[t / BC][i][t % BC] = tk[i][j]; /* while not enough round key material calculated * calculate new values */ while (t < (ROUNDS + 1) * BC) { for (i = 0; i < 4; i++) tk[i][0] ^= S[tk[(i + 1) % 4][KC - 1]]; tk[0][0] ^= rcon[rconpointer++]; if (KC != 8) for (j = 1; j < KC; j++) for (i = 0; i < 4; i++) tk[i][j] ^= tk[i][j - 1]; else { for (j = 1; j < KC / 2; j++) for (i = 0; i < 4; i++) tk[i][j] ^= tk[i][j - 1]; for (i = 0; i < 4; i++) tk[i][KC / 2] ^= S[tk[i][KC / 2 - 1]]; for (j = KC / 2 + 1; j < KC; j++) for (i = 0; i < 4; i++) tk[i][j] ^= tk[i][j - 1]; } /* copy values into round key array */ for (j = 0; (j < KC) && (t < (ROUNDS + 1) * BC); j++, t++) for (i = 0; i < 4; i++) W[t / BC][i][t % BC] = tk[i][j]; } return 0;}int rijndaelEncrypt(word8 a[4][MAXBC], int keyBits, int blockBits, word8 rk[MAXROUNDS + 1][4][MAXBC]){ /* Encryption of one block. */ int r, BC, ROUNDS; BC = 4; ROUNDS = 10; /* begin with a key addition */ KeyAddition(a, rk[0], BC); /* ROUNDS-1 ordinary rounds */ for (r = 1; r < ROUNDS; r++) { Substitution(a, (word8 *) S, BC); ShiftRow(a, 0, BC); MixColumn(a, BC); KeyAddition(a, rk[r], BC); } /* Last round is special: there is no MixColumn */ Substitution(a, (word8 *) S, BC); ShiftRow(a, 0, BC); KeyAddition(a, rk[ROUNDS], BC); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -