Exemplo n.º 1
0
/////////////////////////////////////////////////////////////////////////////
// Same as above, but only changing the AES mode of operation (CBC, not CFB)
SecureBinaryData CryptoAES::DecryptCBC(SecureBinaryData & data, 
                                       SecureBinaryData & key,
                                       SecureBinaryData   iv  )
{
   if(CRYPTO_DEBUG)
   {
      cout << "AES Decrypt" << endl;
      cout << "   BinData: " << data.toHexStr() << endl;
      cout << "   BinKey : " << key.toHexStr() << endl;
      cout << "   BinIV  : " << iv.toHexStr() << endl;
   }

   if(data.getSize() == 0)
      return SecureBinaryData(0);

   SecureBinaryData unencrData(data.getSize());

   BTC_CBC_MODE<BTC_AES>::Decryption aes_enc( (byte*)key.getPtr(), 
                                                     key.getSize(), 
                                              (byte*)iv.getPtr());

   aes_enc.ProcessData( (byte*)unencrData.getPtr(), 
                        (byte*)data.getPtr(), 
                               data.getSize());
   return unencrData;
}
Exemplo n.º 2
0
/////////////////////////////////////////////////////////////////////////////
// Same as above, but only changing the AES mode of operation (CBC, not CFB)
SecureBinaryData CryptoAES::EncryptCBC(SecureBinaryData & data, 
                                       SecureBinaryData & key,
                                       SecureBinaryData & iv)
{
   if(CRYPTO_DEBUG)
   {
      cout << "AES Decrypt" << endl;
      cout << "   BinData: " << data.toHexStr() << endl;
      cout << "   BinKey : " << key.toHexStr() << endl;
      cout << "   BinIV  : " << iv.toHexStr() << endl;
   }

   if(data.getSize() == 0)
      return SecureBinaryData(0);

   SecureBinaryData encrData(data.getSize());

   // Caller can supply their own IV/entropy, or let it be generated here
   // (variable "iv" is a reference, so check it on the way out)
   if(iv.getSize() == 0)
      iv = SecureBinaryData().GenerateRandom(BTC_AES::BLOCKSIZE);


   BTC_CBC_MODE<BTC_AES>::Encryption aes_enc( (byte*)key.getPtr(), 
                                                     key.getSize(), 
                                              (byte*)iv.getPtr());

   aes_enc.ProcessData( (byte*)encrData.getPtr(), 
                        (byte*)data.getPtr(), 
                               data.getSize());

   return encrData;
}
Exemplo n.º 3
0
void aes_ofb(const unsigned char* in, unsigned char *out, 
			int length, const char *expkey, const char* iv)
{
	uint8x16_t block, cipher;
	block = vld1q_u8((int8_t *)iv);
	for (int i = 0; i < length; i += 16){
		block = aes_enc(block, (uint8x16_t *)expkey);	
		cipher = veorq_u8(vld1q_u8(&((int8_t *)in)[i]), block);
		vst1q_u8(&((int8_t*)out)[i], cipher);
	}
}
Exemplo n.º 4
0
SecureBinaryData CryptoAES::Encrypt(SecureBinaryData & data, 
                                    SecureBinaryData & key,
                                    SecureBinaryData & iv)
{
   if(CRYPTO_DEBUG)
   {
      cout << "AES Decrypt" << endl;
      cout << "   BinData: " << data.toHexStr() << endl;
      cout << "   BinKey : " << key.toHexStr() << endl;
      cout << "   BinIV  : " << iv.toHexStr() << endl;
   }


   if(data.getSize() == 0)
      return SecureBinaryData(0);

   SecureBinaryData encrData(data.getSize());
   //cout << "   StartPlain: " << data.toHexStr() << endl;
   //cout << "   Key Data  : " << key.toHexStr() << endl;

   // Caller can supply their own IV/entropy, or let it be generated here
   if(iv.getSize() == 0)
      iv = SecureBinaryData().GenerateRandom(BTC_AES::BLOCKSIZE);


   BTC_AES_MODE<BTC_AES>::Encryption aes_enc( (byte*)key.getPtr(), 
                                                     key.getSize(), 
                                              (byte*)iv.getPtr());

   aes_enc.ProcessData( (byte*)encrData.getPtr(), 
                        (byte*)data.getPtr(), 
                               data.getSize());

   //cout << "   IV Data   : " << iv.toHexStr() << endl;
   //cout << "   Ciphertext: " << encrData.toHexStr() << endl;
   return encrData;
}
Exemplo n.º 5
0
static
bool
encrypt_file(PVOLINFO vi, byte *scratch, char *rfilename, int rfilesize, char *wfilename, int wfilesize)
{
    int ms;
    int ms2;
    int filesize;
    FILEINFO rfi;
    FILEINFO wfi;
    uint32 actual;
    uint32 result;
    uint32 initial;
    header_t *header;
    byte chain[16];
    
    ms = ticks;
    initial = seconds;    
    
    // open the input file, if specified
    if (rfilename && DFS_OpenFile(vi, (unsigned char *)rfilename, DFS_READ, scratch, &rfi)) {
        return false;
    }
    
    // open the output file
    if (DFS_OpenFile(vi, (unsigned char *)wfilename, DFS_WRITE, scratch, &wfi)) {
        return false;
    }
    
    filesize = 0;
    if (rfilename) {
        // first encrypt the header
        memset(big_buffer, 0, sizeof(big_buffer));
        header = (header_t *)big_buffer;
        header->filesize = rfilesize;
        strcpy(header->filename, rfilename);
        
        aes_pre_enc(params.aesbits, params.aeskey, chain);
        
        // encrypt the next block of data
        aes_enc(chain, sizeof(big_buffer), big_buffer);

        ms2 = ticks;
        if (DFS_WriteFile(&wfi, scratch, big_buffer, &actual, sizeof(big_buffer))) {
            return false;
        }
        write_ticks += ticks-ms2;
        assert(actual == sizeof(big_buffer));
        filesize += actual;
        encrypt_bytes += actual;
    
        // then read all the data of the file
        for (;;) {
            if (panic) {
                return false;
            }
            
            ms2 = ticks;
            result = DFS_ReadFile(&rfi, scratch, big_buffer, &actual, sizeof(big_buffer));
            if (result == DFS_EOF || (! result && ! actual)) {
                break;
            }
            read_ticks += ticks-ms2;
            
            if (result) {
                return false;
            }
            
            assert(actual && actual <= sizeof(big_buffer));
            memset(big_buffer+actual, 0, sizeof(big_buffer)-actual);
            
            // encrypt the next block of data
            aes_enc(chain, sizeof(big_buffer), big_buffer);
            
            ms2 = ticks;
            result = DFS_WriteFile(&wfi, scratch, big_buffer, &actual, sizeof(big_buffer));
            if (result) {
                return false;
            }
            write_ticks += ticks-ms2;
            
            assert(actual == sizeof(big_buffer));
            filesize += actual;
            encrypt_bytes += actual;
        }
    }
    
    // wipe the input file up to its original length
    memset(big_buffer, 0, sizeof(big_buffer));
    while (filesize < wfilesize) {
        if (panic) {
            return false;
        }
        
        ms2 = ticks;
        if (DFS_WriteFile(&wfi, scratch, big_buffer, &actual, sizeof(big_buffer))) {
            return false;
        }
        write_ticks += ticks-ms2;
        
        assert(actual == sizeof(big_buffer));
        filesize += actual;
    }
    
    DFS_HostFlush(0);
    
    encrypt_ticks += ticks-ms;
    
#if ! _WIN32
    led_happy_progress();
#endif
    printf("  %d seconds\n", seconds-initial);
    
    return true;
}