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

📄 aes.c

📁 flint库 RSA算法
💻 C
📖 第 1 页 / 共 5 页
字号:
    {#ifdef AES_TEST      fprintf (stderr, "AES_ERR_DIRECTION in line %d\n", __LINE__);#endif      return AES_ERR_DIRECTION;    }  TestEndian (&IsBigEndian);  ws->mode = mode;  ws->BlkLength = BlkLength;  Nb = BlkLength/32;  AESKeySched (CookedKey, RawKey, KeyLength, BlkLength, DFlag, mode);  if (ws->mode == AES_CBC)    {      switch (CookedKey->DFlag)        {          case AES_ENC:            for (col = col4 = 0; col < Nb; col++, col4+=4)              {                ws->State[col] = UC2UL(&IV[col4]);              }            break;          case AES_DEC:            for (col = col4 = 0; col < Nb; col++, col4+=4)              {                ws->Block[col] = UC2UL(&IV[col4]);              }            break;        }    }  return AES_OK;}  /******************************************************************************//*                                                                            *//*  Function:   AES encryption and decryption with support for ECB/CBC modes  *//*              Processing of several portions of text by subsequent calls to *//*              AESCrypt_l is supported for ECB and CBC mode                  *//*              If not #defined AES_COMPACT fast table lookup is performed    *//*  Syntax:     int                                                           *//*              AESCrypt_l (AESBLOCK OutBlock, AESWORKSPACE *ws,              *//*                          AESKEYSCHED *ks, AESBLOCK InBlock, int InLength); *//*  Input:      AESWORKSPACE *ws (Initialized buffer)                         *//*              AESKEYSCHED *ks (Initialized Keyschedule)                     *//*              AESBLOCK InBlock (Plaintext/Ciphertext)                       *//*              int InLength (Length of InBlock in bytes                      *//*                            InLength = 0 mod BlkLength/8 given in AESInit_l *//*  Output:     AESBLOCK OutBlock (Ciphertext/Plaintext)                      *//*  Returns:    E_AES_OK if everything is O.K.                                *//*              E_AES_DIRECTION if DFlag differs in ws and ks                 *//*              E_AES_MODE if mode differs in ws and ks                       *//*              E_AES_KEYLENGTH if KeyLength given in ks != 128, 192 or 256   *//*              E_AES_BLKLENGTH if BlkLength differs in ws and ks             *//*                                                                            *//******************************************************************************/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_APIAESKeyExpansion_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))

⌨️ 快捷键说明

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