This example shows how to implement a client side "provider".
This example shows how to implement a client side "provider".There are three important parts to this:
#include <QtCrypto>
#include <QCoreApplication>
#include <QDebug>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
{
Q_OBJECT
public:
{
}
{
int overflow = 0;
for (
int i = array.
size() - 1; i >= 0; --i) {
out[i] = array[i] << 1;
out[i] |= overflow;
overflow = (array[i] & 0x80) ? 1 : 0;
}
return out;
}
{
for (
int i = 0; i < array1.
size(); ++i)
result[i] = array1[i] ^ array2[i];
return result;
}
{
return;
m_key = key;
const_Rb[15] = (char)0x87;
m_X = const_Zero;
if (0 == (L[0] & 0x80))
m_k1 = leftShift(L);
else
m_k1 = xorArray(leftShift(L), const_Rb);
if (0 == (m_k1[0] & 0x80))
m_k2 = leftShift(m_k1);
else
m_k2 = xorArray(leftShift(m_k1), const_Rb);
}
{
return new AESCMACContext(*this);
}
void clear()
{
}
{
}
{
int blockNum;
for (blockNum = 0; blockNum < ((bytesToProcess.
size() - 1) / 16); ++blockNum) {
for (int yalv = 0; yalv < 16; ++yalv)
thisBlock[yalv] = bytesToProcess[blockNum * 16 + yalv];
m_Y = xorArray(m_X, thisBlock);
}
int numBytesLeft = bytesToProcess.
size() - 16 * blockNum;
m_residual.resize(numBytesLeft);
for (int yalv = 0; yalv < numBytesLeft; ++yalv)
m_residual[yalv] = bytesToProcess[blockNum * 16 + yalv];
}
{
int numBytesLeft = m_residual.
size();
if (numBytesLeft != 16) {
m_residual.resize(16);
m_residual[numBytesLeft] = (char)0x80;
lastBlock = xorArray(m_residual, m_k2);
} else {
lastBlock = xorArray(m_residual, m_k1);
}
m_Y = xorArray(m_X, lastBlock);
}
protected:
};
{
public:
{
}
QString
name()
const override
{
return QStringLiteral("exampleClientSideProvider");
}
{
QStringList list;
list += QStringLiteral("cmac(aes)");
return list;
}
{
if (type == QLatin1String("cmac(aes)"))
return new AESCMACContext(this);
else
return nullptr;
}
};
{
public:
:
QCA::MessageAuthenticationCode(QStringLiteral(
"cmac(aes)"), key, provider)
{
}
};
int main(int argc, char **argv)
{
qDebug() << "This example shows AES CMAC";
QCoreApplication app(argc, argv);
qDebug() << "AES not supported!";
}
qDebug() << "Inserted our provider";
else
qDebug() << "our provider could not be added";
qDebug() << "AES CMAC not supported!";
} else {
AES_CMAC cmacObject;
cmacObject.setup(key);
"ae2d8a571e03ac9c9eb76fac45af8e51"
"30c81c46a35ce411e5fbc1191a0a52ef"
"f69f2445df4f9b17ad2b417be66c3710"));
message1.resize(0);
qDebug();
qDebug() << "Expecting: bb1d6929e95937287fa37d129b756746";
qDebug() <<
"AES-CMAC: " <<
QCA::arrayToHex(cmacObject.process(message1).toByteArray());
cmacObject.clear();
message2.resize(16);
qDebug();
qDebug() << "Expecting: 070a16b46b4d4144f79bdd9dd04a287c";
qDebug() <<
"AES-CMAC: " <<
QCA::arrayToHex(cmacObject.process(message2).toByteArray());
cmacObject.clear();
message3.resize(40);
qDebug();
qDebug() << "Expecting: dfa66747de9ae63030ca32611497c827";
qDebug() <<
"AES-CMAC " <<
QCA::arrayToHex(cmacObject.process(message3).toByteArray());
cmacObject.clear();
message4.resize(64);
qDebug();
qDebug() << "Expecting: 51f0bebf7e3b9d92fc49741779363cfe";
qDebug() <<
"AES-CMAC: " <<
QCA::arrayToHex(cmacObject.process(message4).toByteArray());
}
return 0;
}
#include "aes-cmac.moc"
General class for cipher (encryption / decryption) algorithms.
Definition qca_basic.h:582
@ ECB
operate in Electronic Code Book mode
Definition qca_basic.h:595
@ DefaultPadding
Default for cipher-mode.
Definition qca_basic.h:610
MemoryRegion process(const MemoryRegion &a)
Perform an "all in one" update, returning the result.
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
Simple container for acceptable key lengths.
Definition qca_core.h:701
Message authentication code provider.
Definition qcaprovider.h:298
virtual void setup(const SymmetricKey &key)=0
Set up the object for hashing.
virtual KeyLength keyLength() const =0
Returns the KeyLength for this MAC algorithm.
virtual void update(const MemoryRegion &in)=0
Process a chunk of data.
Array of bytes that may be optionally secured.
Definition qca_tools.h:91
General class for message authentication code (MAC) algorithms.
Definition qca_basic.h:828
Internal context class used for the plugin.
Algorithm provider.
Definition qca_core.h:765
virtual int qcaVersion() const =0
Target QCA version for the provider.
virtual QStringList features() const =0
virtual Context * createContext(const QString &type)=0
virtual QString name() const =0
Secure array of bytes.
Definition qca_tools.h:317
int size() const
Returns the number of bytes in the array.
Container for keys for symmetric encryption algorithms.
Definition qca_core.h:1264
QCA - the Qt Cryptographic Architecture.
Definition qca_basic.h:41
QCA_EXPORT void init()
Initialise QCA.
QCA_EXPORT QString arrayToHex(const QByteArray &array)
Convert a byte array to printable hexadecimal representation.
QCA_EXPORT bool insertProvider(Provider *p, int priority=0)
Add a provider to the current list of providers.
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
QCA_EXPORT QByteArray hexToArray(const QString &hexString)
Convert a QString containing a hexadecimal representation of a byte array into a QByteArray.
@ Encode
Operate in the "forward" direction; for example, encrypting.
Definition qca_core.h:142
#define QCA_VERSION
The current version of QCA.
Definition qca_version.h:78