25#include <QCoreApplication>
29#include "import_plugins.h"
36 AESCMACContext(QCA::Provider *p)
37 : QCA::
MACContext(p, QStringLiteral(
"cmac(aes)"))
43 QCA::SecureArray leftShift(
const QCA::SecureArray &array)
46 QCA::SecureArray out(array.
size());
52 for (
int i = array.
size() - 1; i >= 0; --i) {
54 out[i] = array[i] << 1;
59 overflow = (array[i] & 0x80) ? 1 : 0;
65 QCA::SecureArray xorArray(
const QCA::SecureArray &array1,
const QCA::SecureArray &array2)
69 return QCA::SecureArray();
71 QCA::SecureArray result(array1.
size());
73 for (
int i = 0; i < array1.
size(); ++i)
74 result[i] = array1[i] ^ array2[i];
79 void setup(
const QCA::SymmetricKey &key)
override
88 QCA::SecureArray const_Zero(16);
89 QCA::SecureArray const_Rb(16);
90 const_Rb[15] = (char)0x87;
93 m_residual = QCA::SecureArray();
97 QCA::SecureArray L = aesObj.process(const_Zero);
100 if (0 == (L[0] & 0x80))
103 m_k1 = xorArray(leftShift(L), const_Rb);
106 if (0 == (m_k1[0] & 0x80))
107 m_k2 = leftShift(m_k1);
109 m_k2 = xorArray(leftShift(m_k1), const_Rb);
112 QCA::Provider::Context *clone()
const override
114 return new AESCMACContext(*
this);
122 QCA::KeyLength keyLength()
const override
124 return QCA::KeyLength(16, 16, 1);
129 void update(
const QCA::MemoryRegion &a)
override
131 QCA::SecureArray bytesToProcess = m_residual + a;
135 for (blockNum = 0; blockNum < ((bytesToProcess.
size() - 1) / 16); ++blockNum) {
137 QCA::SecureArray thisBlock(16);
138 for (
int yalv = 0; yalv < 16; ++yalv)
139 thisBlock[yalv] = bytesToProcess[blockNum * 16 + yalv];
141 m_Y = xorArray(m_X, thisBlock);
145 m_X = aesObj.process(m_Y);
148 int numBytesLeft = bytesToProcess.
size() - 16 * blockNum;
150 m_residual.resize(numBytesLeft);
151 for (
int yalv = 0; yalv < numBytesLeft; ++yalv)
152 m_residual[yalv] = bytesToProcess[blockNum * 16 + yalv];
155 void final(QCA::MemoryRegion *out)
override
157 QCA::SecureArray lastBlock;
158 int numBytesLeft = m_residual.size();
160 if (numBytesLeft != 16) {
162 m_residual.resize(16);
163 m_residual[numBytesLeft] = (char)0x80;
164 lastBlock = xorArray(m_residual, m_k2);
167 lastBlock = xorArray(m_residual, m_k1);
169 m_Y = xorArray(m_X, lastBlock);
171 *out = aesObj.process(m_Y);
176 QCA::SecureArray m_k1;
178 QCA::SecureArray m_k2;
180 QCA::SecureArray m_key;
183 QCA::SecureArray m_X;
184 QCA::SecureArray m_Y;
187 QCA::SecureArray m_residual;
193 int qcaVersion()
const override
198 QString name()
const override
200 return QStringLiteral(
"exampleClientSideProvider");
203 QStringList features()
const override
206 list += QStringLiteral(
"cmac(aes)");
211 Provider::Context *createContext(
const QString &type)
override
213 if (type == QLatin1String(
"cmac(aes)"))
214 return new AESCMACContext(
this);
228 AES_CMAC(
const QCA::SymmetricKey &key = QCA::SymmetricKey(),
const QString &
provider = QString())
234int main(
int argc,
char **argv)
240 qDebug() <<
"This example shows AES CMAC";
245 qDebug() <<
"AES not supported!";
249 qDebug() <<
"Inserted our provider";
251 qDebug() <<
"our provider could not be added";
255 qDebug() <<
"AES CMAC not supported!";
264 cmacObject.
setup(key);
268 "ae2d8a571e03ac9c9eb76fac45af8e51"
269 "30c81c46a35ce411e5fbc1191a0a52ef"
270 "f69f2445df4f9b17ad2b417be66c3710"));
275 qDebug() <<
"Expecting: bb1d6929e95937287fa37d129b756746";
283 qDebug() <<
"Expecting: 070a16b46b4d4144f79bdd9dd04a287c";
291 qDebug() <<
"Expecting: dfa66747de9ae63030ca32611497c827";
299 qDebug() <<
"Expecting: 51f0bebf7e3b9d92fc49741779363cfe";
306#include "aes-cmac.moc"
Provider * provider() const
The name of the provider.
MemoryRegion process(const MemoryRegion &a)
Perform an "all in one" update, returning the result.
@ ECB
operate in Electronic Code Book mode
@ DefaultPadding
Default for cipher-mode.
Convenience method for initialising and cleaning up QCA.
Message authentication code provider.
MACContext(Provider *p, const QString &type)
Standard constructor.
QByteArray toByteArray() const
Convert this memory region to a byte array.
General class for message authentication code (MAC) algorithms.
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.
int size() const
Returns the number of bytes in the array.
Container for keys for symmetric encryption algorithms.
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.
@ Encode
Operate in the "forward" direction; for example, encrypting.