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

📄 vmrijndael.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   Implementation of the rijndael encryption class. 

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision:   $ : $Date:   $";
/*****************************************************************************/

#include <cstring>
#include <exception>
#include "VMRijndael.h"

//Error Messages
//
char const* VMRijndael::m_pchErrorMsg1 = "Object not Initialized";
char const* VMRijndael::m_pchErrorMsg2 = "Data not multiple of Block Size";


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

     FUNCTION NAME:  VMRijndael::VMRijndael

       DESCRIPTION:  ctor.

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  none
*/
VMRijndael::VMRijndael( void ) 
: m_ppiEncRoundKey( NULL ), 
  m_ppiDecRoundKey( NULL ), 
  m_pchChain0( NULL ), 
  m_pchChain( NULL ), 
  m_bKeyInit( false )
{
}
/* End of function "VMRijndael::VMRijndael"
/*****************************************************************************/


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

     FUNCTION NAME:  VMRijndael::~VMRijndael

       DESCRIPTION:  dtor. release resources

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  none
*/
VMRijndael::~VMRijndael( void )
{
  int iLoop;

  for( iLoop = 0; iLoop <= m_iROUNDS; iLoop++ )
  {
    delete [] m_ppiEncRoundKey[ iLoop ];
  }

  delete [] m_ppiEncRoundKey;

  for( iLoop = 0; iLoop <= m_iROUNDS; iLoop++ )
  {
    delete [] m_ppiDecRoundKey[ iLoop ];
  }

  delete [] m_ppiDecRoundKey;
  delete [] m_pchChain0;
  delete [] m_pchChain;
}
/* End of function "VMRijndael::~VMRijndael"
/*****************************************************************************/



//Expand a user-supplied key material into a session key.
// key        - The 128/192/256-bit user-key to use.
// chain      - initial chain block for CBC and CFB modes.
// keylength  - 16, 24 or 32 bytes
// blockSize  - The block size in bytes of this Rijndael (16, 24 or 32 bytes).
void VMRijndael::MakeKey( char const* key, char const* chain, int keylength, int blockSize )
{
  if(NULL == key)
    throw exception("Empty key");
  if(!(16==keylength || 24==keylength || 32==keylength))
    throw exception("Incorrect key length");
  if(!(16==blockSize || 24==blockSize || 32==blockSize))
    throw exception("Incorrect block length");
  m_iKeyLength = keylength;
  m_iBlockSize = blockSize;

  if ( NULL != chain )
  {
    //Initialize the chain
    if(m_pchChain0 != NULL)
      delete [] m_pchChain0;
    m_pchChain0 = new char[m_iBlockSize];
    if(m_pchChain != NULL)
      delete [] m_pchChain;
    m_pchChain = new char[m_iBlockSize];
    memcpy(m_pchChain0, chain, m_iBlockSize);
    memcpy(m_pchChain, chain, m_iBlockSize);
  }
  int i;
  if(m_ppiEncRoundKey != NULL)
  {
    for(i=0; i<=m_iROUNDS; i++)
      delete [] m_ppiEncRoundKey[i];
    delete [] m_ppiEncRoundKey;
  }
  if(m_ppiDecRoundKey != NULL)
  {
    for(i=0; i<=m_iROUNDS; i++)
      delete [] m_ppiDecRoundKey[i];
    delete [] m_ppiDecRoundKey;
  }
  //Calculate Number of Rounds
  switch(m_iKeyLength)
  {
    case 16:
      m_iROUNDS = (m_iBlockSize == 16) ? 10 : (m_iBlockSize == 24 ? 12 : 14);
      break;

    case 24:
      m_iROUNDS = (m_iBlockSize != 32) ? 12 : 14;
      break;

    default: // 32 bytes = 256 bits
      m_iROUNDS = 14;
  }
  int BC = m_iBlockSize / 4;
  int j;
  m_ppiEncRoundKey = new int*[m_iROUNDS + 1]; //Encryption round keys
  for(i=0; i<=m_iROUNDS; i++)
  {
    m_ppiEncRoundKey[i] = new int[BC];
    for(j=0; j<BC; j++)
      m_ppiEncRoundKey[i][j] = 0;
  }
  m_ppiDecRoundKey = new int*[m_iROUNDS + 1]; //Decryption round keys
  for(i=0; i<=m_iROUNDS; i++)
  {
    m_ppiDecRoundKey[i] = new int[BC];
    for(j=0; j<BC; j++)
      m_ppiDecRoundKey[i][j] = 0;
  }
  int ROUND_KEY_COUNT = (m_iROUNDS + 1) * BC;
  int KC = m_iKeyLength/4;
  int* tk = new int[KC];
  //Copy user material bytes into temporary ints
  int* pi = tk;
  char const* pc = key;
  for(i=0; i<KC; i++)
  {
    *pi = (unsigned char)*(pc++) << 24;
    *pi |= (unsigned char)*(pc++) << 16;
    *pi |= (unsigned char)*(pc++) << 8;
    *(pi++) |= (unsigned char)*(pc++);
  }
  //Copy values into round key arrays
  int t = 0;
  for(j=0; (j<KC)&&(t<ROUND_KEY_COUNT); j++,t++)
  {
    m_ppiEncRoundKey[t/BC][t%BC] = tk[j];
    m_ppiDecRoundKey[m_iROUNDS - (t/BC)][t%BC] = tk[j];
  }
  int tt, rconpointer = 0;
  while(t < ROUND_KEY_COUNT)
  {
    //Extrapolate using phi (the round key evolution function)
    tt = tk[KC-1];
    tk[0] ^= (sm_S[(tt >> 16) & 0xFF] & 0xFF) << 24 ^
      (sm_S[(tt >>  8) & 0xFF] & 0xFF) << 16 ^
      (sm_S[ tt & 0xFF] & 0xFF) <<  8 ^
      (sm_S[(tt >> 24) & 0xFF] & 0xFF) ^
      (sm_rcon[rconpointer++]  & 0xFF) << 24;
    if(KC != 8)
      for(i=1, j=0; i<KC;)
        tk[i++] ^= tk[j++];
    else
    {
      for(i=1, j=0; i<KC/2; )
        tk[i++] ^= tk[j++];
      tt = tk[KC/2-1];
      tk[KC/2] ^= (sm_S[ tt & 0xFF] & 0xFF) ^
        (sm_S[(tt >>  8) & 0xFF] & 0xFF) <<  8 ^
        (sm_S[(tt >> 16) & 0xFF] & 0xFF) << 16 ^
        (sm_S[(tt >> 24) & 0xFF] & 0xFF) << 24;
      for(j = KC/2, i=j+1; i<KC; )
        tk[i++] ^= tk[j++];
    }
    //Copy values into round key arrays
    for(j=0; (j<KC) && (t<ROUND_KEY_COUNT); j++, t++)
    {
      m_ppiEncRoundKey[t/BC][t%BC] = tk[j];
      m_ppiDecRoundKey[m_iROUNDS - (t/BC)][t%BC] = tk[j];
    }
  }
  delete [] tk;
  //Inverse MixColumn where needed
  for(int r=1; r<m_iROUNDS; r++)
    for(j=0; j<BC; j++)
    {
      tt = m_ppiDecRoundKey[r][j];
      m_ppiDecRoundKey[r][j] = sm_U1[(tt >> 24) & 0xFF] ^
        sm_U2[(tt >> 16) & 0xFF] ^
        sm_U3[(tt >>  8) & 0xFF] ^
        sm_U4[tt & 0xFF];
    }
  m_bKeyInit = true;
}

//Convenience method to encrypt exactly one block of plaintext, assuming
//Rijndael's default block size (128-bit).
// in         - The plaintext
// result     - The ciphertext generated from a plaintext using the key
void VMRijndael::DefEncryptBlock(char const* in, char* result)
{
  if(false==m_bKeyInit)
    throw exception(m_pchErrorMsg1);
  int* Ker = m_ppiEncRoundKey[0];
  int t0 = ((unsigned char)*(in++) << 24);
  t0 |= ((unsigned char)*(in++) << 16);
  t0 |= ((unsigned char)*(in++) << 8);
  (t0 |= (unsigned char)*(in++)) ^= Ker[0];
  int t1 = ((unsigned char)*(in++) << 24);
  t1 |= ((unsigned char)*(in++) << 16);
  t1 |= ((unsigned char)*(in++) << 8);
  (t1 |= (unsigned char)*(in++)) ^= Ker[1];
  int t2 = ((unsigned char)*(in++) << 24);
  t2 |= ((unsigned char)*(in++) << 16);
  t2 |= ((unsigned char)*(in++) << 8);
  (t2 |= (unsigned char)*(in++)) ^= Ker[2];
  int t3 = ((unsigned char)*(in++) << 24);
  t3 |= ((unsigned char)*(in++) << 16);
  t3 |= ((unsigned char)*(in++) << 8);
  (t3 |= (unsigned char)*(in++)) ^= Ker[3];
  int a0, a1, a2, a3;
  //Apply Round Transforms
  for (int r = 1; r < m_iROUNDS; r++)
  {
    Ker = m_ppiEncRoundKey[r];
    a0 = (sm_T1[(t0 >> 24) & 0xFF] ^
      sm_T2[(t1 >> 16) & 0xFF] ^
      sm_T3[(t2 >>  8) & 0xFF] ^
      sm_T4[t3 & 0xFF]) ^ Ker[0];
    a1 = (sm_T1[(t1 >> 24) & 0xFF] ^
      sm_T2[(t2 >> 16) & 0xFF] ^
      sm_T3[(t3 >>  8) & 0xFF] ^
      sm_T4[t0 & 0xFF]) ^ Ker[1];
    a2 = (sm_T1[(t2 >> 24) & 0xFF] ^
      sm_T2[(t3 >> 16) & 0xFF] ^
      sm_T3[(t0 >>  8) & 0xFF] ^
      sm_T4[t1 & 0xFF]) ^ Ker[2];
    a3 = (sm_T1[(t3 >> 24) & 0xFF] ^
      sm_T2[(t0 >> 16) & 0xFF] ^
      sm_T3[(t1 >>  8) & 0xFF] ^
      sm_T4[t2 & 0xFF]) ^ Ker[3];
    t0 = a0;

⌨️ 快捷键说明

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