Difference between revisions of "Simple Crypt IO Device"

From Qt Wiki
Jump to: navigation, search
m (Small layout changes)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Snippets]]
 
[[Category:Snippets]]
 
[toc align_right="yes" depth="3"]
 
 
 
= Writing a Custom I/O Device with encryption via SimpleCrypt class =
 
= Writing a Custom I/O Device with encryption via SimpleCrypt class =
 
 
Creating a custom IO device was already described in [[Custom_IO_Device|Writing a Custom I/O Device]]. The encryption is used from [[Simple_encryption|Simple encryption with SimpleCrypt]].
 
Creating a custom IO device was already described in [[Custom_IO_Device|Writing a Custom I/O Device]]. The encryption is used from [[Simple_encryption|Simple encryption with SimpleCrypt]].
 
The example app can be found on gitorious: "qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide":https://www.gitorious.org/qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide .
 
  
 
== Usage ==
 
== Usage ==
 
 
The following code snippet shows how we would use the custom I/O device to encrypt data and store the result in a file:
 
The following code snippet shows how we would use the custom I/O device to encrypt data and store the result in a file:
  
 
<code>
 
<code>
 
  QFile file("output.dat");
 
  QFile file("output.dat");
  SimpleCryptDevice device(&amp;file); // stream to store the encrypted data
+
  SimpleCryptDevice device(&file); // stream to store the encrypted data
  
device.setBlockSize(256);
+
device.setBlockSize(256);
 
  device.setKey(Q_UINT64_C(0x0c2ad4a4acb9f023));
 
  device.setKey(Q_UINT64_C(0x0c2ad4a4acb9f023));
 
  device.setCompressionMode(SimpleCrypt::CompressionAlways);
 
  device.setCompressionMode(SimpleCrypt::CompressionAlways);
 
  device.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);
 
  device.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);
 
  device.open(QIODevice::WriteOnly);
 
  device.open(QIODevice::WriteOnly);
  QTextStream stream(&amp;device);
+
  QTextStream stream(&device);
 
  out << "Hello World";
 
  out << "Hello World";
 
  out << "My text to encrypt";
 
  out << "My text to encrypt";
 
</code>
 
</code>
  
The <code>compressionMode</code> and <code>Integrity Protection</code> can be changed if needed.
+
The '''compressionMode''' and '''Integrity Protection''' can be changed if needed.
  
Also, if needed, a signal <code>blockWritten</code> can be connected.
+
Also, if needed, a signal '''blockWritten''' can be connected.
  
 
== Implementation ==
 
== Implementation ==
 
 
The basic implementation is the same as in [[Custom_IO_Device|Custom I/O Device]]. The big difference is, that the data can't be stored directly when the client writes it to the device, as the encryption/decryption is done block wise.
 
The basic implementation is the same as in [[Custom_IO_Device|Custom I/O Device]]. The big difference is, that the data can't be stored directly when the client writes it to the device, as the encryption/decryption is done block wise.
  
This means <code>readData</code> and <code>writeData</code> must be changed.
+
This means '''readData''' and '''writeData''' must be changed.
  
<code>SimpleCryptIoDevice</code> has a property blockSize. Data that is written is stored in an internal buffer of size blockSize. When the buffer size is reached, the data is encrypted and stored. This is needed, as <code>SimpleCrypt</code> (in it's used version) does not allow to encrypt to a stream.
+
'''SimpleCryptIoDevice''' has a property blockSize. Data that is written is stored in an internal buffer of size blockSize. When the buffer size is reached, the data is encrypted and stored. This is needed, as '''SimpleCrypt''' (in it's used version) does not allow to encrypt to a stream.
  
 
=== Efficiency ===
 
=== Efficiency ===
 
+
Note that because '''SimpleCrypt''' uses a header and both the compression and the data protection hash or checksum are calculated and stored at the the block level, using '''SimpleCryptDevice''' in this form results in a larger output stream than when using the '''SimpleCrypt''' class directly. Perhaps a future version of '''SimpleCrypt''' will support a streaming interface to increase efficiency in use cases such as these.
Note that because <code>SimpleCrypt</code> uses a header and both the compression and the data protection hash or checksum are calculated and stored at the the block level, using <code>SimpleCryptDevice</code> in this form results in a larger output stream than when using the <code>SimpleCrypt</code> class directly. Perhaps a future version of <code>SimpleCrypt</code> will support a streaming interface to increase efficiency in use cases such as these.
 
  
 
=== readData ===
 
=== readData ===
 
 
For reading, alway a complete block must be read from the device. Then the needed data is moved to the data buffer of the client. As there might be data left in the buffer, each read furst gets the data of the internal buffer. when it's empty, new data is read from the underlying device.
 
For reading, alway a complete block must be read from the device. Then the needed data is moved to the data buffer of the client. As there might be data left in the buffer, each read furst gets the data of the internal buffer. when it's empty, new data is read from the underlying device.
  
Line 59: Line 49:
 
  }
 
  }
  
while(m_byteBuffer.isEmpty() &amp;&amp; (bytesRead < maxSize) &amp;&amp; !m_underlyingDevice->atEnd())
+
while(m_byteBuffer.isEmpty() && (bytesRead < maxSize) && !m_underlyingDevice->atEnd())
 
  {
 
  {
 
  int sizeOfCypher = 0;
 
  int sizeOfCypher = 0;
  int bytesReallyRead = m_underlyingDevice->read((char*)&amp;sizeOfCypher, sizeof(sizeOfCypher));
+
  int bytesReallyRead = m_underlyingDevice->read((char*)&sizeOfCypher, sizeof(sizeOfCypher));
  
 
if(bytesReallyRead != sizeof(sizeOfCypher))
 
if(bytesReallyRead != sizeof(sizeOfCypher))
Line 81: Line 71:
 
  {
 
  {
 
  int copyByte = 0;
 
  int copyByte = 0;
  for(copyByte = 0; (copyByte < m_byteBuffer.size()) &amp;&amp; (bytesRead < (int)maxSize); +''copyByte,bytesRead)
+
  for(copyByte = 0; (copyByte < m_byteBuffer.size()) && (bytesRead < (int)maxSize); +''copyByte,bytesRead)
 
  {
 
  {
 
  data[bytesRead] = m_byteBuffer[copyByte];
 
  data[bytesRead] = m_byteBuffer[copyByte];
Line 95: Line 85:
 
The stored data always contains an int with the size of the encrypted buffer.
 
The stored data always contains an int with the size of the encrypted buffer.
  
h3. writeData
+
=== writeData ===
 
+
To write the data to the underlying device, first the current block needs to be filled. To achieve this, all data is attached to the buffer '''m_byteBuffer'''. unless the buffer is smaller than the block size, one block is removed of the buffer and stored in the underlying device.
To write the data to the underlying device, first the current block needs to be filled. To achieve this, all data is attached to the buffer <code>m_byteBuffer</code>. unless the buffer is smaller than the block size, one block is removed of the buffer and stored in the underlying device.
 
  
 
<code>
 
<code>
Line 120: Line 109:
 
</code>
 
</code>
  
writing one block is fairly easy. The block is encrypted by a call to <code>SimpleCrypt::encryptToByteArray</code> and the size of the encrypted data and the data itself is written to the underlying device.
+
writing one block is fairly easy. The block is encrypted by a call to '''SimpleCrypt::encryptToByteArray''' and the size of the encrypted data and the data itself is written to the underlying device.
  
 
<code>
 
<code>
int SimpleCryptDevice::writeBlock(const QByteArray&amp;amp; bytesToWrite)
+
int SimpleCryptDevice::writeBlock(const QByteArray& bytesToWrite)
 
{
 
{
 
  quint64 realBytesWritten = 0;
 
  quint64 realBytesWritten = 0;
Line 132: Line 121:
 
  int sizeOfCypher = myCypherBytes.size();
 
  int sizeOfCypher = myCypherBytes.size();
  
  realBytesWritten ''= m_underlyingDevice->write((const char*)&amp;sizeOfCypher, sizeof(sizeOfCypher));
+
  realBytesWritten ''= m_underlyingDevice->write((const char*)&sizeOfCypher, sizeof(sizeOfCypher));
 
  realBytesWritten''= m_underlyingDevice->write(myCypherBytes.data(), sizeOfCypher);
 
  realBytesWritten''= m_underlyingDevice->write(myCypherBytes.data(), sizeOfCypher);
 
  emit blockWritten();
 
  emit blockWritten();
Line 146: Line 135:
 
void SimpleCryptDevice::flushEnd()
 
void SimpleCryptDevice::flushEnd()
 
{
 
{
  if(openMode() &amp; WriteOnly)
+
  if(openMode() & WriteOnly)
 
  {
 
  {
 
  quint64 realBytesWritten = writeBlock(m_byteBuffer);
 
  quint64 realBytesWritten = writeBlock(m_byteBuffer);
Line 153: Line 142:
 
}
 
}
 
</code>
 
</code>
 
That's all.
 
 
== Example application screenshot ==
 
 
[[Image:https://lh4.googleusercontent.com/_m1PNLlZctqY/TY96RuQm8fI/AAAAAAAAAFY/KCKWPmclWpY/s800/SimpleCryptIoDevice.jpg|Example App]]
 

Latest revision as of 09:35, 16 June 2015

Writing a Custom I/O Device with encryption via SimpleCrypt class

Creating a custom IO device was already described in Writing a Custom I/O Device. The encryption is used from Simple encryption with SimpleCrypt.

Usage

The following code snippet shows how we would use the custom I/O device to encrypt data and store the result in a file:

 QFile file("output.dat");
 SimpleCryptDevice device(&file); // stream to store the encrypted data

 device.setBlockSize(256);
 device.setKey(Q_UINT64_C(0x0c2ad4a4acb9f023));
 device.setCompressionMode(SimpleCrypt::CompressionAlways);
 device.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);
 device.open(QIODevice::WriteOnly);
 QTextStream stream(&device);
 out << "Hello World";
 out << "My text to encrypt";

The compressionMode and Integrity Protection can be changed if needed.

Also, if needed, a signal blockWritten can be connected.

Implementation

The basic implementation is the same as in Custom I/O Device. The big difference is, that the data can't be stored directly when the client writes it to the device, as the encryption/decryption is done block wise.

This means readData and writeData must be changed.

SimpleCryptIoDevice has a property blockSize. Data that is written is stored in an internal buffer of size blockSize. When the buffer size is reached, the data is encrypted and stored. This is needed, as SimpleCrypt (in it's used version) does not allow to encrypt to a stream.

Efficiency

Note that because SimpleCrypt uses a header and both the compression and the data protection hash or checksum are calculated and stored at the the block level, using SimpleCryptDevice in this form results in a larger output stream than when using the SimpleCrypt class directly. Perhaps a future version of SimpleCrypt will support a streaming interface to increase efficiency in use cases such as these.

readData

For reading, alway a complete block must be read from the device. Then the needed data is moved to the data buffer of the client. As there might be data left in the buffer, each read furst gets the data of the internal buffer. when it's empty, new data is read from the underlying device.

qint64 SimpleCryptDevice::readData(char* data, qint64 maxSize)
{
 int bytesRead = 0;
 if(!m_byteBuffer.isEmpty())
 {
 for(int copyByte = 0; copyByte < qMin(m_byteBuffer.size(), (int)maxSize); +''copyByte,''+bytesRead)
 data[bytesRead] = m_byteBuffer[copyByte];

m_byteBuffer.remove(0, bytesRead);
 }

while(m_byteBuffer.isEmpty() && (bytesRead < maxSize) && !m_underlyingDevice->atEnd())
 {
 int sizeOfCypher = 0;
 int bytesReallyRead = m_underlyingDevice->read((char*)&sizeOfCypher, sizeof(sizeOfCypher));

if(bytesReallyRead != sizeof(sizeOfCypher))
 return -1;

 QByteArray myCypherText;
 myCypherText.resize(sizeOfCypher);
 bytesReallyRead = m_underlyingDevice->read(myCypherText.data(), sizeOfCypher);

if(bytesReallyRead != bytesRead)
 {
 m_byteBuffer = m_crypto.decryptToByteArray(myCypherText);
 if (m_crypto.lastError() != SimpleCrypt::ErrorNoError)
 {
 return -1;
 }
 else
 {
 int copyByte = 0;
 for(copyByte = 0; (copyByte < m_byteBuffer.size()) && (bytesRead < (int)maxSize); +''copyByte,bytesRead)
 {
 data[bytesRead] = m_byteBuffer[copyByte];
 }
 m_byteBuffer.remove(0, copyByte);
 }
 }
 }
 return bytesRead;
}

The stored data always contains an int with the size of the encrypted buffer.

writeData

To write the data to the underlying device, first the current block needs to be filled. To achieve this, all data is attached to the buffer m_byteBuffer. unless the buffer is smaller than the block size, one block is removed of the buffer and stored in the underlying device.

qint64 SimpleCryptDevice::writeData(const char* data, qint64 maxSize)
{
 m_byteBuffer.append(data, (int)maxSize);
 quint64 realBytesWritten = 0;

 // always write blocks of m_blockSize bytes [[Image:|Image:]]!
 while(m_byteBuffer.size() > m_blockSize)
 {
 QByteArray bytesToWrite = m_byteBuffer.left(m_blockSize);
 m_byteBuffer.remove(0, m_blockSize);

 realBytesWritten''= writeBlock(bytesToWrite);
 }

 emit encryptedBytesWritten(realBytesWritten);

 return maxSize;
}

writing one block is fairly easy. The block is encrypted by a call to SimpleCrypt::encryptToByteArray and the size of the encrypted data and the data itself is written to the underlying device.

int SimpleCryptDevice::writeBlock(const QByteArray& bytesToWrite)
{
 quint64 realBytesWritten = 0;
 QByteArray myCypherBytes = m_crypto.encryptToByteArray(bytesToWrite); // cypher the bytes
 if (m_crypto.lastError() == SimpleCrypt::ErrorNoError)
 {
 // store the byte block incl. the size
 int sizeOfCypher = myCypherBytes.size();

 realBytesWritten ''= m_underlyingDevice->write((const char*)&sizeOfCypher, sizeof(sizeOfCypher));
 realBytesWritten''= m_underlyingDevice->write(myCypherBytes.data(), sizeOfCypher);
 emit blockWritten();
 return realBytesWritten;
 }
 return 0;
}

To ensure no data is left when the device is closed, during close or destructor, the last buffer is flushed to the device.

void SimpleCryptDevice::flushEnd()
{
 if(openMode() & WriteOnly)
 {
 quint64 realBytesWritten = writeBlock(m_byteBuffer);
 emit encryptedBytesWritten(realBytesWritten);
 }
}