📄 aes.h
字号:
#ifndef AES_H
#define AES_H 1
#define AES_OK 1
#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. // Nb
#define KEYBITS 128 // Use AES128. // Nk
#define ROUNDS 10 // Number of rounds. // Nr
#define KEYLENGTH 16 // Key length in number of bytes.
extern unsigned char Seed_Key[KEYLENGTH]; // Seed key
#define EXPANDED_KEY_SIZE (BLOCKSIZE * (ROUNDS+1)) //!< 176, 208 or 240 bytes.
extern unsigned char expand_Key[EXPANDED_KEY_SIZE]; // expand key table
/***************************************************************************
| |
| In summary, there are four operations that are at the heart of the AES |
| encryption algorithm. AddRoundKey substitutes groups of 4 bytes using |
| round keys generated from the seed key value. SubBytes substitutes |
| individual bytes using a substitution table. ShiftRows permutes groups of|
| 4 bytes by rotating 4-byte rows. MixColumns substitutes bytes using a |
| combination of both field addition and multiplication. |
| |
| The four operations SubBytes, ShiftRows, MixColumns, and AddRoundKey are |
| called inside a loop that executes Nr times—the number of rounds for a |
| given key size, less 1. The number of rounds that the encryption |
| algorithm uses is either 10, 12, or 14 and depends on whether the seed |
| key size is 128, 192, or 256 bits. After this iteration completes, the |
| encryption algorithm finishes by calling SubBytes, ShiftRows, and |
| AddRoundKey before copying the State matrix to the output parameter |
| |
****************************************************************************/
/*
void CalcPowLog(unsigned char * powTbl, unsigned char * logTbl );
void CalcSBox( unsigned char * sBox );
void CalcSBoxInv( unsigned char * sBox, unsigned char * sBoxInv );
*/
unsigned char Multiply( unsigned char num, unsigned char factor );
// This method circularly shifts the array left by once
void CycleLeft( unsigned char * row );
unsigned char DotProduct( unsigned char * vector1, unsigned char * vector2 );
// Performs the column mixing step of the cipher.
void MixColumn( unsigned char * column );
void InvMixColumn( unsigned char * column );
/***************************************************************************
| |
| This routine is a substitution operation that takes each byte in the |
| State matrix and substitutes a new byte deternined by the Sbox table. |
| |
****************************************************************************/
void SubBytes( unsigned char * bytes, unsigned char count );
void InvSubBytesAndXOR( unsigned char * bytes, unsigned char * key, unsigned char count );
/***************************************************************************
| |
| This routine is a permutation operation that rotates bytes in the |
| State matrix to the left. |
| |
****************************************************************************/
void ShiftRows( unsigned char * state );
void InvShiftRows( unsigned char * state );
/***************************************************************************
| |
| This routine applies the MixColumns diffusion operator to the whole |
| state matrix. The code is used for both encryption and decryption. |
| |
| It is a substitution operation that is the trickiest part of the AES |
| algorithm to understand. It replaces each byte with the result of |
| mathematical field additions and multiplications of values in the byte's |
| column. |
| |
| Note: This routine is part of the encryption and decryption routines. You|
| normally wont be interested in calling this routine directly. |
| |
****************************************************************************/
void MixColumns( unsigned char * state );
void InvMixColumns( unsigned char * state );
void XORBytes( unsigned char * bytes1, unsigned char * bytes2, unsigned char count );
void CopyBytes( unsigned char * to, unsigned char * from, unsigned char count );
/***************************************************************************
| |
| The following routine implements the Rijndael key expansion algorithm. |
| This function creates the expanded key from the input (128/192/256-bit) |
| key. The parameter key is an array of bytes holding the value of the key.|
| Note: the key expansion is necessary for both encryption and decryption.|
| |
****************************************************************************/
void KeyExpansion( unsigned char * expandedKey );
/***************************************************************************
| |
| Cipher is the basic encryption function. It takes parameters block, |
| an array of bytes representing a plaintext block, and expandedKey, an |
| array of words representing the expanded key previously returned by |
| keyExpansion(). The ciphertext block is returned as an array of bytes. |
| |
****************************************************************************/
void Cipher( unsigned char * block, unsigned char * expandedKey );
/***************************************************************************
| |
| InvCipher is the basic decryption function. It takes parameters block, |
| an array of bytes representing a ciphertext block, and expandedKey, |
| an array of words representing the expanded key previously returned by |
| keyExpansion(). The decrypted block is returned as an array of bytes. |
| |
****************************************************************************/
void InvCipher( unsigned char * block, unsigned char * expandedKey );
void aesInit( void );
void BlockEncrypt(unsigned char * buffer, unsigned char blocklen);
void BlockInvEncrypt(unsigned char * buffer, unsigned char blocklen);
void GetSeedKey(void);
extern void GetMacAddress(unsigned char *MacInfo);
#endif // AES_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -