⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aes.c

📁 Advanced encryption system of NIST( 美国国家标准局). The codes are in C.
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************/
int __FLINT_API 
AESCrypt_l (AESBLOCK OutBlock, AESWORKSPACE *ws, AESKEYSCHED *ks, 
            AESBLOCK InBlock, int InLength)
{
  int indx, blck, block, col, Nbc, Nbw, Nk, noofblocks;

  if (ws == NULL || ks == NULL || InBlock == NULL)
    {
#ifdef AES_TEST
      fprintf (stderr, "AES_ERR_INIT in line %d\n", __LINE__);
#endif
      return AES_ERR_INIT;
    }

  if (ws->mode != ks->mode)
    {
#ifdef AES_TEST
      fprintf (stderr, "AES_ERR_MODE in line %d\n", __LINE__);
#endif
      return AES_ERR_MODE;
    }

  if (ws->BlkLength != ks->BlkLength)
    {
#ifdef AES_TEST
      fprintf (stderr, "AES_ERR_BLKLENGTH in line %d\n", __LINE__);
#endif
      return AES_ERR_BLKLENGTH;
    }

  if ((ks->KeyLength != 128) && 
      (ks->KeyLength != 192) && 
      (ks->KeyLength != 256))
    {
#ifdef AES_TEST
      fprintf (stderr, "AES_ERR_KEYLENGTH in line %d\n", __LINE__);
#endif
      return AES_ERR_KEYLENGTH;
    }

  Nbc = ws->BlkLength/8;
  Nbw = ws->BlkLength/32;
  Nk = ks->KeyLength/32;
  noofblocks = InLength/Nbc;

  if (InLength % Nbc)
    {
#ifdef AES_TEST
      fprintf (stderr, "AES_ERR_BLKLENGTH in line %d\n", __LINE__);
#endif
      return AES_ERR_BLKLENGTH;
    } 

  switch (ws->mode)
    {
      case AES_ECB:
        switch (ks->DFlag)
          {
            case AES_ENC: /* ECB-Mode */
              for (block = 0; block < noofblocks; block++)
                {
                  blck = Nbc*block;
                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);
                      ws->State[col] = UC2UL(&InBlock[indx]);
                    }

                  AESEncState (ws->State, ks->ExpandedKey, Nk, Nbw);

                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);
                      OutBlock[indx]     = (UCHAR)(ws->State[col]      );
                      OutBlock[indx + 1] = (UCHAR)(ws->State[col] >>  8);
                      OutBlock[indx + 2] = (UCHAR)(ws->State[col] >> 16);
                      OutBlock[indx + 3] = (UCHAR)(ws->State[col] >> 24);
                    }
                }
              break;

            case AES_DEC: /* ECB-Mode */
              for (block = 0; block < noofblocks; block++)
                {
                  blck = Nbc*block;
                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);
                      ws->State[col] = UC2UL(&InBlock[indx]);
                    }

                  AESDecState (ws->State, ks->ExpandedKey, Nk, Nbw);

                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);
                      OutBlock[indx]     = (UCHAR)(ws->State[col]      );
                      OutBlock[indx + 1] = (UCHAR)(ws->State[col] >>  8);
                      OutBlock[indx + 2] = (UCHAR)(ws->State[col] >> 16);
                      OutBlock[indx + 3] = (UCHAR)(ws->State[col] >> 24);
                    }
                }
              break;
            default:
              return AES_ERR_DIRECTION;
          }
        break;

      case AES_CBC:
        switch (ks->DFlag)
          {
            case AES_ENC: /* CBC-Mode */
              for (block = 0; block < noofblocks; block++)
                {
                  blck = Nbc*block;
                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);
                      ws->State[col] ^= UC2UL(&InBlock[indx]);
                    }

                  AESEncState (ws->State, ks->ExpandedKey, Nk, Nbw);

                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);
                      OutBlock[indx]     = (UCHAR)(ws->State[col]      );
                      OutBlock[indx + 1] = (UCHAR)(ws->State[col] >>  8);
                      OutBlock[indx + 2] = (UCHAR)(ws->State[col] >> 16);
                      OutBlock[indx + 3] = (UCHAR)(ws->State[col] >> 24);
                    }
                }

              break;
            case AES_DEC: /* CBC-Mode */
              for (block = 0; block < noofblocks; block++)
                {
                  blck = Nbc*block;
                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);
                      ws->State[col] = UC2UL(&InBlock[indx]);
                    }

                  AESDecState (ws->State, ks->ExpandedKey, Nk, Nbw);

                  for (col = 0; col < Nbw; col++)
                    {
                      indx = blck + ((unsigned)col<<2);

                      ws->State[col] ^= ws->Block[col];

                      ws->Block[col] = UC2UL(&InBlock[indx]);

                      OutBlock[indx]     = (UCHAR)(ws->State[col]      );
                      OutBlock[indx + 1] = (UCHAR)(ws->State[col] >>  8);
                      OutBlock[indx + 2] = (UCHAR)(ws->State[col] >> 16);
                      OutBlock[indx + 3] = (UCHAR)(ws->State[col] >> 24);
                    }
                }

              break;
            default:
              return AES_ERR_DIRECTION;
          }
        break;
      default:             
        return AES_ERR_MODE;
    }

  return AES_OK;
}


/******************************************************************************/
/* AES Public Kernel Functions                                                */


/******************************************************************************/
/*                                                                            */
/*  Function:   AES key expansion to key schedule for encryption              */
/*  Syntax:     int                                                           */
/*              AESKeyExpansion_l (AESXPKEY ExpandedKey, AESKEY RawKey,       */
/*                                        int KeyLength, int BlockLength);    */
/*  Input:      AESKEY RawKey (User Key as byte array)                        */
/*              int KeyLength (Length of user key 128, 192 or 256 bit)        */
/*              int BlockLength (Length of Message block 128, 192 or 256 bit) */
/*  Output:     AESEXPKEY ExpandedKey (Key schedule)                          */
/*  Returns:    0                                                             */
/*                                                                            */
/******************************************************************************/
int __FLINT_API
AESKeyExpansion_l (AESXPKEY ExpandedKey, AESKEY RawKey, 
                         int KeyLength, int BlockLength)
{
  int col, col4, Nb, Nk, Nr;
  ULONG *keyptr, *keyptr1, *keyptr2, tmp;
  
  Nb = BlockLength/32;
  Nk = KeyLength/32;
  Nr = NR[(Nk - 4)/2][(Nb - 4)/2];
  
  if ((BlockLength != 128) && (BlockLength != 192) && (BlockLength != 256))
    {
#ifdef AES_TEST
      fprintf (stderr, "AES_ERR_BLKLENGTH in line %d\n", __LINE__);
#endif
      return AES_ERR_BLKLENGTH;
    }

  if ((KeyLength != 128) && (KeyLength != 192) && (KeyLength != 256))
    {
#ifdef AES_TEST
      fprintf (stderr, "AES_ERR_KEYLENGTH in line %d\n", __LINE__);
#endif
      return AES_ERR_KEYLENGTH;
    }

  for (col = col4 = 0; col < Nk - 1; col++, col4+=4)
    {
      keyptr = &ExpandedKey[col];
      *keyptr = UC2UL(&RawKey[col4]);
    }

  keyptr = &ExpandedKey[Nk-1];
  tmp = (*keyptr = UC2UL(&RawKey[(unsigned)(Nk-1)<<2]));

  for (col = Nk; col < Nb*(Nr + 1); col++)
    {
      keyptr1 = &ExpandedKey[col];
      keyptr2 = &ExpandedKey[col-Nk];

      if (col % Nk == 0)
        {
          tmp = ROR (tmp, 8);
          SubWord (tmp);
          tmp ^= rc[col/Nk];
        }

      else if ((Nk == 8) && (col % Nk == 4))
        {
          SubWord (tmp); /*lint !e771 At this point tmp is initialized! */
        }

      tmp = (*keyptr1 = *keyptr2 ^ tmp);
    }

#ifdef AES_SECURE
  ZeroUlong (&tmp);
#endif

  return 0;
}


/******************************************************************************/
/*                                                                            */
/*  Function:   AES key expansion to inverse key schedule for decryption      */
/*  Syntax:     int                                                           */
/*              AESInvKeyExpansion_l (AESXPKEY InvExpandedKey, AESKEY RawKey, */
/*                                           int KeyLength, int BlockLength); */
/*  Input:      AESKEY RawKey (User Key as byte array)                        */
/*              int KeyLength (Length of user key 128, 192 or 256 bit)        */
/*              int BlockLength (Length of Message block 128, 192 or 256 bit) */
/*  Output:     AESEXPKEY InvExpandedKey (Invers key schedule)                */
/*  Returns:    0                                                             */
/*                                                                            */
/******************************************************************************/
int __FLINT_API
AESInvKeyExpansion_l (AESXPKEY InvExpandedKey, AESKEY RawKey, 
                                int KeyLength, int BlockLength)
{
  int round, Nb, Nk, Nr, error;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -