22-恰好我有。能运行的,C语言的。

  • 格式:doc
  • 大小:36.00 KB
  • 文档页数:14

恰好我有。能运行的,C语言的。 #include #include "aes.h" #include "commonage.h"

#define byte unsigned char

#define BPOLY 0x1b //!< Lower 8 bits of (x^8+x^4+x^3+x+1), ie. (x^4+x^3+x+1). #define BLOCKSIZE 16 //!< Block size in number of bytes.

#define KEYBITS 128 //!< Use AES128. #define ROUNDS 10 //!< Number of rounds. #define KEYLENGTH 16 //!< Key length in number of bytes.

byte xdata block1[ 256 ]; //!< Workspace 1. byte xdata block2[ 256 ]; //!< Worksapce 2.

byte xdata * powTbl; //!< Final location of exponentiation lookup table. byte xdata * logTbl; //!< Final location of logarithm lookup table. byte xdata * sBox; //!< Final location of s-box. byte xdata * sBoxInv; //!< Final location of inverse s-box. byte xdata * expandedKey; //!< Final location of expanded key.

void CalcPowLog( byte * powTbl, byte * logTbl ) { byte xdata i = 0; byte xdata t = 1;

do { // Use 0x03 as root for exponentiation and logarithms. powTbl[i] = t; logTbl[t] = i; i++; // Muliply t by 3 in GF(2^8). t ^= (t << 1) ^ (t & 0x80 ? BPOLY : 0); } while( t != 1 ); // Cyclic properties ensure that i < 255.

powTbl[255] = powTbl[0]; // 255 = '-0', 254 = -1, etc. }

void CalcSBox( byte * sBox ) { byte xdata i, rot; byte xdata temp; byte xdata result;

// Fill all entries of sBox[]. i = 0; do { // Inverse in GF(2^8). if( i > 0 ) { temp = powTbl[ 255 - logTbl[i] ]; } else { temp = 0; }

// Affine transformation in GF(2). result = temp ^ 0x63; // Start with adding a vector in GF(2). for( rot = 0; rot < 4; rot++ ) { // Rotate left. temp = (temp<<1) | (temp>>7);

// Add rotated byte in GF(2). result ^= temp; }

// Put result in table. sBox[i] = result; } while( ++i != 0 ); }

void CalcSBoxInv( byte * sBox, byte * sBoxInv ) { byte xdata i = 0; byte xdata j = 0;

// Iterate through all elements in sBoxInv using i. do { // Search through sBox using j. cleardog(); do { // Check if current j is the inverse of current i. if( sBox[ j ] == i ) { // If so, set sBoxInc and indicate search finished. sBoxInv[ i ] = j; j = 255; } } while( ++j != 0 ); } while( ++i != 0 ); }

void CycleLeft( byte * row ) { // Cycle 4 bytes in an array left once. byte xdata temp = row[0]; row[0] = row[1]; row[1] = row[2]; row[2] = row[3]; row[3] = temp; }

void InvMixColumn( byte * column ) { byte xdata r0, r1, r2, r3;

r0 = column[1] ^ column[2] ^ column[3]; r1 = column[0] ^ column[2] ^ column[3]; r2 = column[0] ^ column[1] ^ column[3]; r3 = column[0] ^ column[1] ^ column[2];

column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0); column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0); column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0); column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);

r0 ^= column[0] ^ column[1]; r1 ^= column[1] ^ column[2]; r2 ^= column[2] ^ column[3]; r3 ^= column[0] ^ column[3];

column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0); column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0); column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0); column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);

r0 ^= column[0] ^ column[2]; r1 ^= column[1] ^ column[3]; r2 ^= column[0] ^ column[2]; r3 ^= column[1] ^ column[3];

column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0); column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0); column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0); column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);

column[0] ^= column[1] ^ column[2] ^ column[3]; r0 ^= column[0]; r1 ^= column[0]; r2 ^= column[0]; r3 ^= column[0];

column[0] = r0; column[1] = r1; column[2] = r2; column[3] = r3; }

byte Multiply( unsigned char num, unsigned char factor ) { byte mask = 1; byte result = 0;

while( mask != 0 ) { // Check bit of factor given by mask. if( mask & factor ) { // Add current multiple of num in GF(2).