AES 加密算法

       AES 加密算法是通过征召各类加密算法,最终确定为比利时密码学家 Joan Daemen 和 Vincent Rijmen

设计的分组密码算法 Rijndael 。标准AES算法的分组长度有 128bits,192bits,256bits。

     AES加密过程:把明文进行分组处理,每组长度为128bits,即16字节。

1. 逐个的对这16字节数据进行 SubBytes 处理。SubBytes 其实就是利用输入的数据作为索引,

通过查表替换原来的数据。

2.进行 ShiftRow 处理。将刚刚处理的数据按字节进行打乱处理。

3.进行 MixColumns 处理。即对一个4字节的值进行比特运算,将其变为另一个4字节值。

4.最后 AddRoundKey 处理。

AES 解密过程是加密过程的逆过程:

分组密码: 每次处理特定长度的数据块的一类加密算法。分组长度就是一个分组的比特数。

流密码:  对数据流进行连续处理的一类加密算法。一般以1bit,8bit,32bit进行加解密。

--------------------------------------------------------------------------------------------

分组密码的主要5种模式:

ECB(Electronic CodeBook mode) 电子密码模式

CBC(Cipher Block Chaining mode)密码分组链式模式

CFB(Cipher FeedBack mode)密文反馈模式

OFB(Output FeedBack mode)输出反馈模式

CTR(CounTeR mode)计算器模式

ECB模式图解:

在使用ECB模式进行加密时,有一个明显特点就是相同的明文经过加密会得到相同的密文。

就像是参照某个表一样,缺点就是相同的明文经加密得到相同的密文,很容易使用暴力破解,

对密文进行猜解。实际应用不推荐这种模式。

CBC模式图解:

用初始化向量先对明文进行异或操作,然后在进行加密,并把加密得到的密文,与下一组

明文进行异或操作,然后再进行下一次的加密。 因为除了第一次,后面每组明文异或的数据,

都来自密文,所以把这种模式称为 Cipher Block Chaining 模式- 密码分组链式模式。

其中,初始化向量(Initialization vector)需要在加密前先进行初始化,切每次加密时要使用

不同的 初始化向量 。

CFB模式图解:

在CFB模式中,明文分组和密文分组仅仅是简单的异或,并没有加密这一过程。

由CFB加密算法得来的比特序列被称作 密钥流。密码算法相当于用来生成密钥流

伪随机数生成器(PRNG),而 初始化向量 就相当于生成随机数的 种子

 对CFB模式生成的密文,可以实施 重放攻击(Replay Attack)。具体参见《图解密码4.5.5》

OFB模式:

密码算法的输出会反馈到密码算法的输入中。

OFB和CFB有些类似,都是将生成的密钥,和明文异或,得到密文。

CTR模式图解:

 CTR模式通过对计数器累加,然后加密生成密钥流,来异或加密明文。

 因为计数器是可以直接计算出来的,所以加密和解密过程可以进行

并行计算

个人总结:
//*********************************************************************************
AES 加密要点:

1. 明文分组

AES 加密算法处理的明文 分组长度 都是 16 字节。


2. 密码处理

可以根据 AES版本,选择密码的长度。

16 字节(AES128), 24字节(AES192),32字节(AES256) 的3种密码长度

3. 加密

用输入的密码进行 密钥扩展 ,得到加密时需要的 RoundKey

AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key)
{
  KeyExpansion(ctx->RoundKey, key);
}


4. 加密模式

常用模式 CBC 和 CTR 模式。

两种模式都需要一个 初始化向量(IV)。用 初始化向量(IV) 对 明文分组 先进行 亦或处理再进行加密

这样就能保证即使有相同的 明文分组 得到的 密文分组 也是不同的

AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv)

{
  KeyExpansion(ctx->RoundKey, key);
  memcpy (ctx->Iv, iv, AES_BLOCKLEN);
}

AES 加密过程

1. 先进行 密钥扩展 ,得到 RoundKey ,然后根据加密模式, 初始化 IV

2. 用 初始化向量 亦或 明文

XorWithIv(buf, Iv);

然后 调用加密过程 加密刚刚亦或得到的数据,得到最终的 密文分组

Cipher((state_t*)buf, ctx->RoundKey);
//*********************************************************************************

//*********************************************************************************
AES 解密要点

1. 密码

用输入的密码进行 密钥扩展 ,得到解密时需要的 RoundKey

AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key)
{
  KeyExpansion(ctx->RoundKey, key);
}


2. 解密

根据不同的模式,要获得加密时用的 初始化向量 才能正确的进行解密


AES 解密过程:

1. 先进行 密钥扩展 ,得到 RoundKey ,然后根据加密模式, 初始化 IV

2. 调用解密过程用 RoundKey 解密 密文分组,得到的结果与 IV 亦或得到明文

InvCipher((state_t*)buf, ctx->RoundKey);    //用 RoundKey 解密密文分组
    
XorWithIv(buf, ctx->Iv);            //初始化向量 亦或 解密得到的 明文分组 ,得到真正的明文

//*********************************************************************************

C 语言实现AES :

定义Ctx 结构体存储 加密过程用到的RoundKey 和 IV 。

struct AES_ctx
{
  uint8_t RoundKey[AES_keyExpSize];
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
  uint8_t Iv[AES_BLOCKLEN];
#endif
};

以下给出AES 加密算法用到的一些函数的定义:

KeyExpansion 函数扩展输入的密码(密钥)。如果输入的密码长度太短,则需要你自己实现一个

密钥对齐函数,把密钥对齐到 128bits,192bits或者256bits。

// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. 
static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
{
  unsigned i, j, k;
  uint8_t tempa[4]; // Used for the column/row operations
  
  // The first round key is the key itself.
  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];
  }

  // All other round keys are found from the previous round keys.
  for (i = Nk; i < Nb * (Nr + 1); ++i)
  {
    {
      k = (i - 1) * 4;
      tempa[0]=RoundKey[k + 0];
      tempa[1]=RoundKey[k + 1];
      tempa[2]=RoundKey[k + 2];
      tempa[3]=RoundKey[k + 3];

    }

    if (i % Nk == 0)
    {
      // This function shifts the 4 bytes in a word to the left once.
      // [a0,a1,a2,a3] becomes [a1,a2,a3,a0]

      // Function RotWord()
      {
        k = tempa[0];
        tempa[0] = tempa[1];
        tempa[1] = tempa[2];
        tempa[2] = tempa[3];
        tempa[3] = k;
      }

      // SubWord() is a function that takes a four-byte input word and 
      // applies the S-box to each of the four bytes to produce an output word.

      // Function Subword()
      {
        tempa[0] = getSBoxValue(tempa[0]);
        tempa[1] = getSBoxValue(tempa[1]);
        tempa[2] = getSBoxValue(tempa[2]);
        tempa[3] = getSBoxValue(tempa[3]);
      }

      tempa[0] = tempa[0] ^ Rcon[i/Nk];
    }
#if defined(AES256) && (AES256 == 1)
    if (i % Nk == 4)
    {
      // Function Subword()
      {
        tempa[0] = getSBoxValue(tempa[0]);
        tempa[1] = getSBoxValue(tempa[1]);
        tempa[2] = getSBoxValue(tempa[2]);
        tempa[3] = getSBoxValue(tempa[3]);
      }
    }
#endif
    j = i * 4; k=(i - Nk) * 4;
    RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
    RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
    RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
    RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
  }// End of For
}

 AES_init_ctx_iv 函数调用 KeyExpansion 函数扩展密钥,并把 IV 拷贝到 Ctx 中。

//如果定义了 CBC模式 或者 CTR模式,则需要 初始化向量 
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv)
{
  KeyExpansion(ctx->RoundKey, key);
  memcpy (ctx->Iv, iv, AES_BLOCKLEN);
}

 AES_CBC_encrypt_buffer 是CBC加密模式下的加密主函数。

第一个参数是前面 调用 AES_init_ctx_iv  函数初始化好的 Ctx 指针。

第二个参数是要加密的明文地址。

第三个参数是明文的长度。

//CBC 加密模式函数
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* buf, uint32_t length)
{
  uintptr_t i;
  uint8_t *Iv = ctx->Iv;
  for (i = 0; i < length; i += AES_BLOCKLEN)
  {
    XorWithIv(buf, Iv);						//明文分组 与 初始化向量 亦或
    Cipher((state_t*)buf, ctx->RoundKey);	//加密 亦或后的 明文分组
    Iv = buf;								//下一个 初始化向量 是上一个加密得到的 密文分组
    buf += AES_BLOCKLEN;					//移动明文指针指向下一个 明文分组
    //printf("Step %d - %d", i/16, i);
  }
  /* store Iv in ctx for next call */		//最后一个 IV备份下,不是很理解, 不应该用最开始的 IV来 初始化解密第一个密文分组吗?	
  memcpy(ctx->Iv, Iv, AES_BLOCKLEN);		//应该是用来加密最后一个 明文分组,即 不能对其到 明文 分组长度 的最后一个 明文分组
}

下面列出 AES_CBC_encrypt_buffer 函数需要的子函数代码:

XorWithIv 函数用 IV 对输入的明文分组进行异或操作。这样就保证了即使有相同的明文分组,

加密得到的密文分组数据也是不同的。

static void XorWithIv(uint8_t* buf, uint8_t* Iv)
{
  uint8_t i;
  for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
  {
    buf[i] ^= Iv[i];
  }
}

 Cipher 函数是进行主要的加密函数调用的包装函数(Wrapper)。

第一个参数就是 明文分组

第二个参数是 RoundKey

// Cipher is the main function that encrypts the PlainText.
static void Cipher(state_t* state, uint8_t* RoundKey)
{
  uint8_t round = 0;

  // Add the First round key to the state before starting the rounds.
  AddRoundKey(0, state, RoundKey); 
  
  // There will be Nr rounds.
  // The first Nr-1 rounds are identical.
  // These Nr-1 rounds are executed in the loop below.
  for (round = 1; round < Nr; ++round)
  {
    SubBytes(state);
    ShiftRows(state);
    MixColumns(state);
    AddRoundKey(round, state, RoundKey);
  }
  
  // The last round is given below.
  // The MixColumns function is not here in the last round.
  SubBytes(state);
  ShiftRows(state);
  AddRoundKey(Nr, state, RoundKey);
}

下面给出 Cipher 函数需要的子函数的实现:

AddRoundKey 函数根据 加密的轮次,对明文分组进行不同的异或操作。

第一个参数是 轮次

第二个参数是 明文分组

第三个参数是 RoundKey

// This function adds the round key to state.
// The round key is added to the state by an XOR function.
static void AddRoundKey(uint8_t round,state_t* state,uint8_t* RoundKey)
{
  uint8_t i,j;
  for (i = 0; i < 4; ++i)
  {
    for (j = 0; j < 4; ++j)
    {
      (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
    }
  }
}

 SubBytes 函数用输入的明文分组 的值作为索引,从一个 Substitute Box 替换盒子(数组)中取值,来替换原来的明文。

参数为 明文分组

// The SubBytes Function Substitutes the values in the
// state matrix with values in an S-box.
static void SubBytes(state_t* state)
{
  uint8_t i, j;
  for (i = 0; i < 4; ++i)
  {
    for (j = 0; j < 4; ++j)
    {
      (*state)[j][i] = getSBoxValue((*state)[j][i]);
    }
  }
}

ShiftRows 函数对明文分组进行移位操作,每行移动的次数就是行号。第0行移动0次,第1行移动1次……

参数为 明文分组

// The ShiftRows() function shifts the rows in the state to the left.
// Each row is shifted with different offset.
// Offset = Row number. So the first row is not shifted.
static void ShiftRows(state_t* state)
{
  uint8_t temp;

  // Rotate first row 1 columns to left  
  temp           = (*state)[0][1];
  (*state)[0][1] = (*state)[1][1];
  (*state)[1][1] = (*state)[2][1];
  (*state)[2][1] = (*state)[3][1];
  (*state)[3][1] = temp;

  // Rotate second row 2 columns to left  
  temp           = (*state)[0][2];
  (*state)[0][2] = (*state)[2][2];
  (*state)[2][2] = temp;

  temp           = (*state)[1][2];
  (*state)[1][2] = (*state)[3][2];
  (*state)[3][2] = temp;

  // Rotate third row 3 columns to left
  temp           = (*state)[0][3];
  (*state)[0][3] = (*state)[3][3];
  (*state)[3][3] = (*state)[2][3];
  (*state)[2][3] = (*state)[1][3];
  (*state)[1][3] = temp;
}

 MixColumns 函数进行列的混淆。

参数为 明文分组

// MixColumns function mixes the columns of the state matrix
static void MixColumns(state_t* state)
{
  uint8_t i;
  uint8_t Tmp, Tm, t;
  for (i = 0; i < 4; ++i)
  {  
    t   = (*state)[i][0];
    Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
    Tm  = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm);  (*state)[i][0] ^= Tm ^ Tmp ;
    Tm  = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm);  (*state)[i][1] ^= Tm ^ Tmp ;
    Tm  = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm);  (*state)[i][2] ^= Tm ^ Tmp ;
    Tm  = (*state)[i][3] ^ t ;              Tm = xtime(Tm);  (*state)[i][3] ^= Tm ^ Tmp ;
  }
}

xtime 函数不是很明白……

static uint8_t xtime(uint8_t x)
{
  return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
}

参考:

《图解密码技术-结城浩》

代码出自 GitHub : tiny-AES-c-master (链接忘记存了……Sorry)

猜你喜欢

转载自blog.csdn.net/cwg2552298/article/details/81462725