QCA
aes-cmac.cpp
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:
- the class derived from QCA::Provider (in this example called "ClientSideProvider"), that generates the context class
- one or more context classes (in this example only one, implementing AES-CMAC, called "AESCMACContext")
- a call to QCA::insertProvider, to add the QCA::Provider subclass into QCA
/*
Copyright (C) 2006 Brad Hards <bradh@frogmouth.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// QtCrypto has the declarations for all of QCA
#include <QtCrypto>
#include <QCoreApplication>
#include <QDebug>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
{
public:
AESCMACContext(QCA::Provider *p)
: QCA::MACContext(p, QStringLiteral("cmac(aes)"))
{
}
// Helper to left shift an arbitrary length array
// This is heavily based on the example in the I-D.
{
// We create an output of the same size as the input
// We handle one byte at a time - this is the high bit
// from the previous byte.
int overflow = 0;
// work through each byte.
// do the left shift on this byte.
out[i] = array[i] << 1;
// make the low bit on this byte be the high bit
// from the previous byte.
out[i] |= overflow;
// save the high bit for next time
overflow = (array[i] & 0x80) ? 1 : 0;
}
return out;
}
// Helper to XOR two arrays - must be same length
{
// empty array
return QCA::SecureArray();
QCA::SecureArray result(array1.size());
result[i] = array1[i] ^ array2[i];
return result;
}
{
// We might not have a real key, since this can get called
// from the constructor.
if (key.size() == 0)
return;
m_key = key;
// Generate the subkeys
QCA::SecureArray const_Zero(16);
QCA::SecureArray const_Rb(16);
const_Rb[15] = (char)0x87;
m_X = const_Zero;
m_residual = QCA::SecureArray();
// Figure 2.2, step 1.
QCA::Cipher aesObj(QStringLiteral("aes128"), QCA::Cipher::ECB, QCA::Cipher::DefaultPadding, QCA::Encode, key);
// Figure 2.2, step 2
if (0 == (L[0] & 0x80))
m_k1 = leftShift(L);
else
m_k1 = xorArray(leftShift(L), const_Rb);
// Figure 2.2, step 3
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()
{
setup(m_key);
}
{
return QCA::KeyLength(16, 16, 1);
}
// This is a bit different to the way the I-D does it,
// to allow for multiple update() calls.
{
QCA::SecureArray bytesToProcess = m_residual + a;
int blockNum;
// note that we don't want to do the last full block here, because
// it needs special treatment in final().
for (blockNum = 0; blockNum < ((bytesToProcess.size() - 1) / 16); ++blockNum) {
// copy a block of data
QCA::SecureArray thisBlock(16);
for (int yalv = 0; yalv < 16; ++yalv)
thisBlock[yalv] = bytesToProcess[blockNum * 16 + yalv];
m_Y = xorArray(m_X, thisBlock);
QCA::Cipher aesObj(
m_X = aesObj.process(m_Y);
}
// This can be between 1 and 16
int numBytesLeft = bytesToProcess.size() - 16 * blockNum;
// we copy the left over part
m_residual.resize(numBytesLeft);
for (int yalv = 0; yalv < numBytesLeft; ++yalv)
m_residual[yalv] = bytesToProcess[blockNum * 16 + yalv];
}
{
QCA::SecureArray lastBlock;
int numBytesLeft = m_residual.size();
if (numBytesLeft != 16) {
// no full block, so we have to pad.
m_residual.resize(16);
m_residual[numBytesLeft] = (char)0x80;
lastBlock = xorArray(m_residual, m_k2);
} else {
// this is a full block - no padding
lastBlock = xorArray(m_residual, m_k1);
}
m_Y = xorArray(m_X, lastBlock);
QCA::Cipher aesObj(QStringLiteral("aes128"), QCA::Cipher::ECB, QCA::Cipher::DefaultPadding, QCA::Encode, m_key);
*out = aesObj.process(m_Y);
}
protected:
// first subkey
QCA::SecureArray m_k1;
// second subkey
QCA::SecureArray m_k2;
// main key
QCA::SecureArray m_key;
// state
QCA::SecureArray m_X;
QCA::SecureArray m_Y;
// partial block that we can't do yet
QCA::SecureArray m_residual;
};
{
public:
int qcaVersion() const override
{
return QCA_VERSION;
}
{
return QStringLiteral("exampleClientSideProvider");
}
{
list += QStringLiteral("cmac(aes)");
// you can add more features in here, if you have some.
return list;
}
{
return new AESCMACContext(this);
// else if (type == some other feature)
// return some other context.
else
return nullptr;
}
};
// AES CMAC is a Message Authentication Code based on a block cipher
// instead of the more normal keyed hash.
// See RFC 4493 "The AES-CMAC Algorithm"
{
public:
{
}
};
int main(int argc, char **argv)
{
// the Initializer object sets things up, and
// also does cleanup when it goes out of scope
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";
// We should check AES CMAC is supported before using it.
qDebug() << "AES CMAC not supported!";
} else {
// create the required object
AES_CMAC cmacObject;
// create the key
// set the MAC to use the key
cmacObject.setup(key);
QCA::SecureArray message =
QCA::hexToArray(QStringLiteral("6bc1bee22e409f96e93d7e117393172a"
"ae2d8a571e03ac9c9eb76fac45af8e51"
"30c81c46a35ce411e5fbc1191a0a52ef"
"f69f2445df4f9b17ad2b417be66c3710"));
QCA::SecureArray message1(message);
message1.resize(0);
qDebug();
qDebug() << "Expecting: bb1d6929e95937287fa37d129b756746";
cmacObject.clear();
QCA::SecureArray message2(message);
message2.resize(16);
qDebug();
qDebug() << "Message2: " << QCA::arrayToHex(message2.toByteArray());
qDebug() << "Expecting: 070a16b46b4d4144f79bdd9dd04a287c";
cmacObject.clear();
QCA::SecureArray message3(message);
message3.resize(40);
qDebug();
qDebug() << "Message3: " << QCA::arrayToHex(message3.toByteArray());
qDebug() << "Expecting: dfa66747de9ae63030ca32611497c827";
cmacObject.clear();
QCA::SecureArray message4(message);
message4.resize(64);
qDebug();
qDebug() << "Message4: " << QCA::arrayToHex(message4.toByteArray());
qDebug() << "Expecting: 51f0bebf7e3b9d92fc49741779363cfe";
}
return 0;
}
#include "aes-cmac.moc"
MemoryRegion process(const MemoryRegion &a)
Perform an "all in one" update, returning the result.
MemoryRegion process(const MemoryRegion &a)
Perform an "all in one" update, returning the result.
QByteArray toByteArray() const
Convert this memory region to a byte array.
General class for message authentication code (MAC) algorithms.
Definition qca_basic.h:828
MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider=QString())
Standard constructor.
void setup(const SymmetricKey &key)
Initialise the MAC algorithm.
void clear() override
Reset a MessageAuthenticationCode, dumping all previous parts of the message.
Internal context class used for the plugin.
bool resize(int size)
Change the length of this array If the new length is less than the old length, the extra information ...
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
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.
Q_OBJECTQ_OBJECT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.