// EGPublicKey::EncryptData
// Encrypts a block of specified length into the specified	queue.  Determines the
// number of individual blocks to be encrypted.  Adds block count to queue.  Then
// calls EncryptBlock() to encrypt each block into the queue.  Note that mCryptP
// must be valid before this method is called.
void
EGPublicKey::EncryptData(BufferedTransformation& aQueue, const unsigned char* theMsgP,
                         unsigned long theLen) const
{
	WTRACE("EGPublicKey::EncryptData");
	WDBG_LL("EGPublicKey::EncryptData, len=" << theLen);

	// Determine block length and number of blocks
	unsigned long aBlockLen = mCryptP->MaxPlainTextLength();
	unsigned long aNumBlock = theLen / aBlockLen;
	if ((theLen % aBlockLen) != 0) aNumBlock++;
	WDBG_LL("EGPublicKey::EncryptBlock NumBlocks=" << aNumBlock << ", BlockLen=" << aBlockLen);

	// A num blocks to output
	unsigned long tmpNumBlock = getLittleEndian(aNumBlock);
	aQueue.Put(reinterpret_cast<unsigned char*>(&tmpNumBlock), sizeof(tmpNumBlock));

	// Encrypt the data, one block at a time
	while (theLen > aBlockLen)
	{
		EncryptBlock(aQueue, theMsgP, aBlockLen);
		theMsgP += aBlockLen;
		theLen  -= aBlockLen;
	}

	// Encrypt the last block and close the queue
	EncryptBlock(aQueue, theMsgP, theLen);
	aQueue.Close();
}
Exemplo n.º 2
0
void Blowfish::SetKey(const unsigned char* key, int byte_length)
{
    std::memcpy(pary_, initial_pary, sizeof(initial_pary));
    std::memcpy(sbox_, initial_sbox, sizeof(initial_sbox));
    
    static const int pary_length = sizeof(pary_) / sizeof(uint32_t);
    static const int sbox_length = sizeof(sbox_) / sizeof(uint32_t);
    
    {
        int buffer_length = byte_length / GCD(byte_length, sizeof(uint32_t));
        uint32_t *key_buffer = new uint32_t[buffer_length];
        
        for (int i = 0; i < buffer_length; ++i)
        {
            Converter32 converter;
            
            converter.bit_8.byte0 = key[(i * 4) % byte_length];
            converter.bit_8.byte1 = key[(i * 4 + 1) % byte_length];
            converter.bit_8.byte2 = key[(i * 4 + 2) % byte_length];
            converter.bit_8.byte3 = key[(i * 4 + 3) % byte_length];
            
            key_buffer[i] = converter.bit_32;
        }
        
        for (int i = 0; i < pary_length; ++i)
        {
            uint32_t key_uint32 = key_buffer[i % buffer_length];
            pary_[i] ^= key_uint32;
        }
        
        delete[] key_buffer;
    }
    
    uint32_t left  = 0x00000000;
    uint32_t right = 0x00000000;
    
    for (int i = 0; i < (pary_length / 2); ++i)
    {
        EncryptBlock(&left, &right);
        
        pary_[i * 2] = left;
        pary_[i * 2 + 1] = right;
    }
    
    for (int i = 0; i < (sbox_length / 2); ++i)
    {
        EncryptBlock(&left, &right);
        
        reinterpret_cast<uint32_t*>(sbox_)[i * 2] = left;
        reinterpret_cast<uint32_t*>(sbox_)[i * 2 + 1] = right;
    }
}
Exemplo n.º 3
0
void Blowfish::Encrypt(unsigned char* dst, const unsigned char* src, int byte_length) const
{
    if (dst != src)
    {
        memcpy(dst, src, byte_length);
    }
    
    for (int i = 0; i < byte_length / sizeof(uint64_t); ++i)
    {
        uint32_t* left  = &reinterpret_cast<uint32_t*>(dst)[i * 2];
        uint32_t* right = &reinterpret_cast<uint32_t*>(dst)[i * 2 + 1];
        EncryptBlock(left, right);
    }
}
Exemplo n.º 4
0
std::vector<char> Blowfish::Encrypt(const std::vector<char> &src) const {
    std::vector<char> dst = src;

    size_t padding_length = dst.size() % sizeof(uint64_t);
    if (padding_length == 0) {
        padding_length = sizeof(uint64_t);
    } else {
        padding_length = sizeof(uint64_t) - padding_length;
    }

    for (size_t i = 0; i < padding_length; ++i) {
        dst.push_back(static_cast<char>(padding_length));
    }

    for (int i = 0; i < dst.size() / sizeof(uint64_t); ++i) {
        uint32_t *left = &reinterpret_cast<uint32_t *>(dst.data())[i * 2];
        uint32_t *right = &reinterpret_cast<uint32_t *>(dst.data())[i * 2 + 1];
        EncryptBlock(left, right);
    }

    return dst;
}
Exemplo n.º 5
0
// CBCEncrypt()
void CBCEncrypt(void *pTextIn, void* pBuffer, u_int32_ard length,
                u_int32_ard padding, const u_int32_ard *pKeys,
                const u_int16_ard *pIV)
{
  byte_ard *pText = (byte_ard*)pTextIn;
  byte_ard *cBuffer = (byte_ard*)pBuffer;
  byte_ard lastblock[BLOCK_BYTE_SIZE];
  byte_ard currblock[BLOCK_BYTE_SIZE];
  u_int32_ard blocks = (length + padding) / BLOCK_BYTE_SIZE;

  memcpy(lastblock,pIV,BLOCK_BYTE_SIZE);

  if (padding == (BLOCK_BYTE_SIZE +1) )
  {
    // Decide on the padding
    if ((length % BLOCK_BYTE_SIZE) == 0)
      padding = 0;
    else
      padding = BLOCK_BYTE_SIZE - (length % BLOCK_BYTE_SIZE);  
  }

  
  // Copy and pad the 
  for (u_int32_ard i = 0; i<(length + padding); i++)
  {
    if (i < length)
    {
      // Copy the string
      cBuffer[i] = pText[i];
    }
    else
    {
      cBuffer[i] = 0x80;
    }
     
  }
  

  // C_i = E_k(P_i XOR C_{i-1})
  for (u_int32_ard i = 0; i < blocks; i++) 
  {
    #if defined(unroll_cbc_encrypt_loop)
    currblock[0] = cBuffer[(i*BLOCK_BYTE_SIZE)+0] ^ lastblock[0];
    currblock[1] = cBuffer[(i*BLOCK_BYTE_SIZE)+1] ^ lastblock[1];
    currblock[2] = cBuffer[(i*BLOCK_BYTE_SIZE)+2] ^ lastblock[2];
    currblock[3] = cBuffer[(i*BLOCK_BYTE_SIZE)+3] ^ lastblock[3];
    currblock[4] = cBuffer[(i*BLOCK_BYTE_SIZE)+4] ^ lastblock[4];
    currblock[5] = cBuffer[(i*BLOCK_BYTE_SIZE)+5] ^ lastblock[5];
    currblock[6] = cBuffer[(i*BLOCK_BYTE_SIZE)+6] ^ lastblock[6];
    currblock[7] = cBuffer[(i*BLOCK_BYTE_SIZE)+7] ^ lastblock[7];
    currblock[8] = cBuffer[(i*BLOCK_BYTE_SIZE)+8] ^ lastblock[8];
    currblock[9] = cBuffer[(i*BLOCK_BYTE_SIZE)+9] ^ lastblock[9];
    currblock[10] = cBuffer[(i*BLOCK_BYTE_SIZE)+10] ^ lastblock[10];
    currblock[11] = cBuffer[(i*BLOCK_BYTE_SIZE)+11] ^ lastblock[11];
    currblock[12] = cBuffer[(i*BLOCK_BYTE_SIZE)+12] ^ lastblock[12];
    currblock[13] = cBuffer[(i*BLOCK_BYTE_SIZE)+13] ^ lastblock[13];
    currblock[14] = cBuffer[(i*BLOCK_BYTE_SIZE)+14] ^ lastblock[14];
    currblock[15] = cBuffer[(i*BLOCK_BYTE_SIZE)+15] ^ lastblock[15];

    EncryptBlock((void*)currblock, pKeys);

    cBuffer[(i*BLOCK_BYTE_SIZE)+0] = currblock[0];
    cBuffer[(i*BLOCK_BYTE_SIZE)+1] = currblock[1];
    cBuffer[(i*BLOCK_BYTE_SIZE)+2] = currblock[2];
    cBuffer[(i*BLOCK_BYTE_SIZE)+3] = currblock[3];
    cBuffer[(i*BLOCK_BYTE_SIZE)+4] = currblock[4];
    cBuffer[(i*BLOCK_BYTE_SIZE)+5] = currblock[5];
    cBuffer[(i*BLOCK_BYTE_SIZE)+6] = currblock[6];
    cBuffer[(i*BLOCK_BYTE_SIZE)+7] = currblock[7];
    cBuffer[(i*BLOCK_BYTE_SIZE)+8] = currblock[8];
    cBuffer[(i*BLOCK_BYTE_SIZE)+9] = currblock[9];
    cBuffer[(i*BLOCK_BYTE_SIZE)+10] = currblock[10];
    cBuffer[(i*BLOCK_BYTE_SIZE)+11] = currblock[11];
    cBuffer[(i*BLOCK_BYTE_SIZE)+12] = currblock[12];
    cBuffer[(i*BLOCK_BYTE_SIZE)+13] = currblock[13];
    cBuffer[(i*BLOCK_BYTE_SIZE)+14] = currblock[14];
    cBuffer[(i*BLOCK_BYTE_SIZE)+15] = currblock[15];
    
    #else

    for (u_int16_ard j = 0; j < BLOCK_BYTE_SIZE; j++)
    {
      currblock[j] = cBuffer[(i*BLOCK_BYTE_SIZE)+j] ^ lastblock[j];
    }
    
    EncryptBlock((void*)currblock, pKeys);

    // Copy the ciphered block into the buffer again. 
    for (u_int16_ard j = 0; j < BLOCK_BYTE_SIZE; j++)
    {
      cBuffer[(i*BLOCK_BYTE_SIZE)+j] = currblock[j];
    }

    #endif
//    lastblock = currblock;
	memcpy(lastblock,currblock,BLOCK_BYTE_SIZE);

  } // for (blocks)
} // CBCEncrypt()
void Cbc512NoPaddingCryptoProvider::Encrypt(const uint8_t *pbIn,
                                            uint32_t       cbInUnsafe,
                                            uint32_t       dwStartingBlockNumberUnsafe,
                                            bool           isFinal,
                                            uint8_t       *pbOut,
                                            uint32_t       cbOutUnsafe,
                                            uint32_t      *pcbOut)
{
  auto cbIn                  = cbInUnsafe, cbOut = cbOutUnsafe,
       dwStartingBlockNumber = dwStartingBlockNumberUnsafe;

  if (pbIn == nullptr) {
    throw exceptions::RMSCryptoNullPointerException("Null pointer pbIn exception");
  }

  if (!isFinal && (0 != cbIn % CBC512_BLOCK_SIZE)) {
    throw exceptions::RMSCryptoInvalidArgumentException("Block is not aligned");
  }

  if (nullptr == pcbOut) {
    throw exceptions::RMSCryptoNullPointerException("Null pointer pcbOut exception");
  }

  if ((cbIn % AES128_BLOCK_SIZE) != 0) {
    throw exceptions::RMSCryptoInvalidArgumentException("Block is not aligned");
  }

  auto cbResult = 0;

  if (nullptr == pbOut)
  {
    // No need to do the encryption, just return the number of uint8_ts required
    *pcbOut = (unsigned long)cbIn;
    return;
  }


  while (cbIn > CBC512_BLOCK_SIZE)
  {
    if (cbOut < CBC512_BLOCK_SIZE) {
      throw exceptions::RMSCryptoInvalidArgumentException("Invalid buffer size");
    }

    // Encrypt the current block
    EncryptBlock(pbIn,
                 CBC512_BLOCK_SIZE,
                 dwStartingBlockNumber,
                 false,
                 pbOut,
                 cbOut);

    // Go to the next block
    pbIn += CBC512_BLOCK_SIZE;
    cbIn -= CBC512_BLOCK_SIZE;

    pbOut += CBC512_BLOCK_SIZE;
    cbOut -= CBC512_BLOCK_SIZE;

    ++dwStartingBlockNumber;
    cbResult += CBC512_BLOCK_SIZE;
  }

  if (!isFinal && (cbIn > CBC512_BLOCK_SIZE)) {
    throw exceptions::RMSCryptoInvalidArgumentException("Invalid aligment");
  }
  cbResult += EncryptBlock(pbIn, cbIn, dwStartingBlockNumber, true, pbOut, cbOut);
  *pcbOut   = cbResult;
}
Exemplo n.º 7
0
void
LRWMode::Encrypt(ThreadContext& context, uint8 *data, size_t length)
{
	EncryptBlock(context, data, length, fOffset);
}
Exemplo n.º 8
0
void
XTSMode::Encrypt(ThreadContext& context, uint8 *data, size_t length)
{
	EncryptBlock(context, data, length, 0);
}
Exemplo n.º 9
0
void RX_ISR(void)
{
    received = U1ARXREG;
    PORTD = 2;
    if(received == 10)
    {
        i = 0;
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        TMR2 = 0;
        while(username[i] != NULL)
        {
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = username[i];
            }
            i++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        DelayMsec(5000);

        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        i = 0;
        while(password[i] != NULL)
        {
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = password[i];
            }
            i++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }

    }

    else if(received == -7)
    {
        if(receive_user == -1)
            receive_user = 1;
        else if(receive_user == 1)
            receive_user = 0;
        else
            receive_user = -1;
        i = 0;
    }

    else if(received == -4)
    {
        receive_file = 1;
        file_pt = 0;
    }

    else if(received == -5)
    {
        receive_file = 0;
        file_pt = 0;
        //BYTE Key[2] = {'1','2'};
	BYTE RoundKey[6];
	Extend_Key(Key,RoundKey);
	while(file[file_pt] != NULL)
	{
		BYTE temp[2];
		BYTE temp_result[2];
		temp[0] = file[file_pt];
		temp[1] = file[file_pt+1];
		EncryptBlock(temp,RoundKey,temp_result);
		result[file_pt] = temp_result[0];
		result[file_pt+1] = temp_result[1];
		file_pt = file_pt + 2;
	}
        file_pt = 0;
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        while(result[file_pt] != NULL)
        {
            char to_send = result[file_pt];
            file_pt++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = to_send;
            }
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }
        file_pt = 0;
    }

    else if(received == -6)
    {
        receive_file = 0;
        file_pt = 0;
        //BYTE Key[2] = {'1','2'};
	BYTE RoundKey[6];
	Extend_Key(Key,RoundKey);
	while(file[file_pt] != NULL)
	{
		BYTE temp[2];
		BYTE temp_result[2];
		temp_result[0] = file[file_pt];
		temp_result[1] = file[file_pt+1];
		DecryptBlock(temp_result,RoundKey,temp);
		file[file_pt] = temp[0];
		file[file_pt+1] = temp[1];
		file_pt = file_pt + 2;
	}
        file_pt = 0;
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        while(file[file_pt] != NULL)
        {
            char to_send = file[file_pt];
            file_pt++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = to_send;
            }
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }
        file_pt = 0;
    }

    if(receive_file == 1 && received != -4 && received != -5)
    {
        file[file_pt] = received;
        file_pt ++;
    }

    if(receive_user == 1 && received != -7)
    {
        username[i] = received;
        i++;
    }

    else if(receive_user == 0 && received != -7)
    {
        password[i] = received;
        i++;
    }
    PORTDINV = 2;
    IFS0bits.U1RXIF = 0;
}