AES(Advanced Encryption Standard,高级加密标准)是一个对称密钥加密算法,密钥长度则可以是128,192或256比特。
算法思路(详细信息请见易做图):
AES加密过程是在一个4×4的字节矩阵上运作,这个矩阵又称为“体(state)”,其初值就是一个明文区块(矩阵中一个元素大小就是明文区块中的一个Byte)。(Rijndael加密法因支持更大的区块,其矩阵行数可视情况增加)加密时,各轮AES加密循环(除最后一轮外)均包含4个步骤:
AddRoundKey — 矩阵中的每一个字节都与该次回合金钥(round key)做XOR运算;每个子密钥由密钥生成方案产生。
SubBytes — 通过一个非线性的替换函数,用查找表的方式把每个字节替换成对应的字节。
ShiftRows — 将矩阵中的每个横列进行循环式移位。
MixColumns — 为了充分混合矩阵中各个直行的操作。这个步骤使用线性转换来混合每内联的四个字节。
最后一个加密循环中省略MixColumns步骤,而以另一个AddRoundKey取代。
这个算法比较复杂,所以完全没理解,所以这个文章就不能说是解读了,贴出AMPS中AES的实现代码,有兴趣的朋友可以自己理解一下,我是没有这个兴趣了。
AMPS_AES.h
[cpp]
#ifndef __HEADER_AMPS_AES_H
#define __HEADER_AMPS_AES_H
#ifdef __cplusplus
extern "C" {
#endif
#include "AMPS_Defines.h"
//platform-independant 32-bit integer manipulation macros
#define GET_UINT32(n,b,i) \
{ \
(n) = ( (b)[(i)] << 24 ) \
| ( (b)[(i) + 1] << 16 ) \
| ( (b)[(i) + 2] << 8 ) \
| ( (b)[(i) + 3] ); \
}
#define PUT_UINT32(n,b,i) \
{ \
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 3] = (unsigned char) ( (n) ); \
}
typedef struct _AMPSAESContext t_AMPSAESContext;
struct _AMPSAESContext
{
int nNoOfRounds;
unsigned long ulEncRoundKeys[64];
unsigned long ulDecRoundKeys[64];
};
void* AAES_Init(void* r_pvAMPSContext);
void AAES_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSAESContext);
int AAES_SetKey(void* r_pvAMPSContext, void* r_pvAMPSAESContext, unsigned char* r_puchKey, int r_nBits);
void AAES_Encrypt(void* r_pvAMPSContext, void* r_pvAMPSAESContext, unsigned char* r_puchInputBuff, unsigned int r_unInputBuffLength, unsigned char* r_puchOutputBuff);
void AAES_Decrypt(void* r_pvAMPSContext, void* r_pvAMPSAESContext, unsigned char* r_puchInputBuff, unsigned int r_unInputBuffLength, unsigned char* r_puchOutputBuff);
#ifdef __cplusplus
}
#endif
#endif //__HEADER_AMPS_AES_H
#ifndef __HEADER_AMPS_AES_H
#define __HEADER_AMPS_AES_H
#ifdef __cplusplus
extern "C" {
#endif
#include "AMPS_Defines.h"
//platform-independant 32-bit integer manipulation macros
#define GET_UINT32(n,b,i) \
{ \
(n) = ( (b)[(i)] << 24 ) \
| ( (b)[(i) + 1] << 16 ) \
| ( (b)[(i) + 2] << 8 ) \
| ( (b)[(i) + 3] ); \
}
#define PUT_UINT32(n,b,i) \
{ \
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 3] = (unsigned char) ( (n) ); \
}
typedef struct _AMPSAESContext t_AMPSAESContext;
struct _AMPSAESContext
{
int nNoOfRounds;
unsigned long ulEncRoundKeys[64];
unsigned long ulDecRoundKeys[64];
};
void* AAES_Init(void* r_pvAMPSContext);
void AAES_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSAESContext);
int AAES_SetKey(void* r_pvAMPSContext, void* r_pvAMPSAESContext, unsigned char* r_puchKey, int r_nBits);
void AAES_Encrypt(void* r_pvAMPSContext, void* r_pvAMPSAESContext, unsigned char* r_puchInputBuff, unsigned int r_unInputBuffLength, unsigned char* r_puchOutputBuff);
void AAES_Decrypt(void* r_pvAMPSContext, void* r_pvAMPSAESContext, unsigned char* r_puchInputBuff, unsigned int r_unInputBuffLength, unsigned char* r_puchOutputBuff);
#ifdef __cplusplus
}
#endif
#endif //__HEADER_AMPS_AES_H
AMPS_AES.c
[cpp]
#include "AMPS_Core.h"
#include "AMPS_Defines.h"
#include "AMPS_MemMgt.h"
#include "AMPS_AES.h"
#include "AMPS_LinkList.h"
void* AAES_Init(void* r_pvAMPSContext)
{