Difference between revisions of "Simple Crypt IO Device"

From Qt Wiki
Jump to: navigation, search
 
Line 1: Line 1:
=Writing a Custom I/O Device with encryption via SimpleCrypt class=
+
[[Category:Snippets]]
  
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]].
+
[toc align_right="yes" depth="3"]
  
The example app can be found on gitorious: [https://www.gitorious.org/qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide] ''[gitorious.org]'' .
+
= Writing a Custom I/O Device with encryption via SimpleCrypt class =
  
==Usage==
+
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 ==
  
 
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:
  
The <code>compressionMode</code> and <code>Integrity Protection</code> can be changed if needed.
+
<code><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 /> device.open(QIODevice::WriteOnly);<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 /></code>
 +
 
 +
The &lt;code&amp;gt;compressionMode&amp;lt;/code&amp;gt; and &lt;code&amp;gt;Integrity Protection&amp;lt;/code&amp;gt; can be changed if needed.
  
Also, if needed, a signal <code>blockWritten</code> can be connected.
+
Also, if needed, a signal &lt;code&amp;gt;blockWritten&amp;lt;/code&amp;gt; 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 &lt;code&amp;gt;readData&amp;lt;/code&amp;gt; and &lt;code&amp;gt;writeData&amp;lt;/code&amp;gt; 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.
+
&lt;code&amp;gt;SimpleCryptIoDevice&amp;lt;/code&amp;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 &lt;code&amp;gt;SimpleCrypt&amp;lt;/code&amp;gt; (in it's used version) does not allow to encrypt to a stream.
  
===Efficiency===
+
=== Efficiency ===
  
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.
+
Note that because &lt;code&amp;gt;SimpleCrypt&amp;lt;/code&amp;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 &lt;code&amp;gt;SimpleCryptDevice&amp;lt;/code&amp;gt; in this form results in a larger output stream than when using the &lt;code&amp;gt;SimpleCrypt&amp;lt;/code&amp;gt; class directly. Perhaps a future version of &lt;code&amp;gt;SimpleCrypt&amp;lt;/code&amp;gt; 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.
  
The stored data always contains an int with the size of the encrypted buffer.
+
<code><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];
  
===writeData===
+
m_byteBuffer.remove(0, bytesRead);<br /> }
  
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.
+
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));
  
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.
+
if(bytesReallyRead != sizeof(sizeOfCypher))<br /> return <s>1;
 +
<br /> QByteArray myCypherText;<br /> myCypherText.resize(sizeOfCypher);<br /> bytesReallyRead = m_underlyingDevice</s>&gt;read(myCypherText.data(), 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 /></code>
 +
<br />The stored data always contains an int with the size of the encrypted buffer.
 +
<br />h3. writeData
 +
<br />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 &lt;code&amp;gt;m_byteBuffer&amp;lt;/code&amp;gt;. unless the buffer is smaller than the block size, one block is removed of the buffer and stored in the underlying device.
 +
<br /><code><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 /></code>
 +
<br />writing one block is fairly easy. The block is encrypted by a call to &lt;code&amp;gt;SimpleCrypt::encryptToByteArray&amp;lt;/code&amp;gt; and the size of the encrypted data and the data itself is written to the underlying device.
 +
<br /><code><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(myCypherBytes.data(), sizeOfCypher);<br /> emit blockWritten();<br /> return realBytesWritten;<br /> }<br /> return 0;<br />}<br /></code>
  
 
To ensure no data is left when the device is closed, during close or destructor, the last buffer is flushed to the device.
 
To ensure no data is left when the device is closed, during close or destructor, the last buffer is flushed to the device.
  
That’s all.
+
<code><br />void SimpleCryptDevice::flushEnd()<br />{<br /> if(openMode() &amp; WriteOnly)<br /> {<br /> quint64 realBytesWritten = writeBlock(m_byteBuffer);<br /> emit encryptedBytesWritten(realBytesWritten);<br /> }<br />}<br /></code>
 
 
==Example application screenshot==
 
  
[[Image:SimpleCryptIoDevice.jpg|Example App]]
+
That's all.
  
===Categories:===
+
== Example application screenshot ==
  
* [[:Category:snippets|snippets]]
+
[[Image:https://lh4.googleusercontent.com/_m1PNLlZctqY/TY96RuQm8fI/AAAAAAAAAFY/KCKWPmclWpY/s800/SimpleCryptIoDevice.jpg|Example App]]

Revision as of 09:34, 24 February 2015


[toc align_right="yes&quot; depth="3&quot;]

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&quot;:https://www.gitorious.org/qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide .

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:

<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 /> device.open(QIODevice::WriteOnly);<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.

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 <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.

Efficiency

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.

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.

<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(myCypherText.data(), 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(myCypherBytes.data(), 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