Simple Crypt IO Device

From Qt Wiki
Revision as of 09:34, 24 February 2015 by Maintenance script (talk | contribs)

Jump to: navigation, search

[toc align_right="yes" depth="3"]

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.

The example app can be found on gitorious: "qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide": .


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

<br /> QFile file&amp;amp;#40;&quot;output.dat&amp;quot;&amp;#41;;<br /> SimpleCryptDevice device(&amp;file); // stream to store the encrypted data

device.setBlockSize(256);<br /> device.setKey(Q_UINT64_C(0x0c2ad4a4acb9f023));<br /> device.setCompressionMode(SimpleCrypt::CompressionAlways);<br /> device.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);<br />;<br /> QTextStream stream(&amp;device);<br /> out &lt;&lt; &quot;Hello World&amp;quot;;<br /> out &lt;&lt; &quot;My text to encrypt&amp;quot;;<br />

The <code&gt;compressionMode&lt;/code&gt; and <code&gt;Integrity Protection&lt;/code&gt; can be changed if needed.

Also, if needed, a signal <code&gt;blockWritten&lt;/code&gt; can be connected.


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 <code&gt;readData&lt;/code&gt; and <code&gt;writeData&lt;/code&gt; must be changed.

<code&gt;SimpleCryptIoDevice&lt;/code&gt; 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&gt;SimpleCrypt&lt;/code&gt; (in it's used version) does not allow to encrypt to a stream.


Note that because <code&gt;SimpleCrypt&lt;/code&gt; 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&gt;SimpleCryptDevice&lt;/code&gt; in this form results in a larger output stream than when using the <code&gt;SimpleCrypt&lt;/code&gt; class directly. Perhaps a future version of <code&gt;SimpleCrypt&lt;/code&gt; will support a streaming interface to increase efficiency in use cases such as these.


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.

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

m_byteBuffer.remove(0, bytesRead);<br /> }

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

if(bytesReallyRead != sizeof(sizeOfCypher))<br /> return <s>1;
<br /> QByteArray myCypherText;<br /> myCypherText.resize(sizeOfCypher);<br /> bytesReallyRead = m_underlyingDevice</s>&gt;read(, sizeOfCypher);

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

The stored data always contains an int with the size of the encrypted buffer.
h3. 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 <code&gt;m_byteBuffer&lt;/code&gt;. unless the buffer is smaller than the block size, one block is removed of the buffer and stored in the underlying device.

<br />qint64 SimpleCryptDevice::writeData(const char* data, qint64 maxSize)<br />{<br /> m_byteBuffer.append(data, (int)maxSize);<br /> quint64 realBytesWritten = 0;
<br /> // always write blocks of m_blockSize bytes [[Image:|Image:]]!<br /> while(m_byteBuffer.size() &gt; m_blockSize)<br /> {<br /> QByteArray bytesToWrite = m_byteBuffer.left(m_blockSize);<br /> m_byteBuffer.remove(0, m_blockSize);
<br /> realBytesWritten''= writeBlock(bytesToWrite);<br /> }
<br /> emit encryptedBytesWritten(realBytesWritten);
<br /> return maxSize;<br />}<br />

writing one block is fairly easy. The block is encrypted by a call to <code&gt;SimpleCrypt::encryptToByteArray&lt;/code&gt; and the size of the encrypted data and the data itself is written to the underlying device.

<br />int SimpleCryptDevice::writeBlock(const QByteArray&amp;amp; bytesToWrite)<br />{<br /> quint64 realBytesWritten = 0;<br /> QByteArray myCypherBytes = m_crypto.encryptToByteArray(bytesToWrite); // cypher the bytes<br /> if (m_crypto.lastError() == SimpleCrypt::ErrorNoError)<br /> {<br /> // store the byte block incl. the size<br /> int sizeOfCypher = myCypherBytes.size();
<br /> realBytesWritten ''= m_underlyingDevice-&gt;write((const char*)&amp;sizeOfCypher, sizeof(sizeOfCypher));<br /> realBytesWritten''= m_underlyingDevice</s>&gt;write(, sizeOfCypher);<br /> emit blockWritten();<br /> return realBytesWritten;<br /> }<br /> return 0;<br />}<br />

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

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

That's all.

Example application screenshot

Example App