File indexing completed on 2024-04-28 04:44:10
0001 /** 0002 * Copyright (C) 2004-2006 Brad Hards <bradh@frogmouth.net> 0003 * 0004 * Redistribution and use in source and binary forms, with or without 0005 * modification, are permitted provided that the following conditions 0006 * are met: 0007 * 0008 * 1. Redistributions of source code must retain the above copyright 0009 * notice, this list of conditions and the following disclaimer. 0010 * 2. Redistributions in binary form must reproduce the above copyright 0011 * notice, this list of conditions and the following disclaimer in the 0012 * documentation and/or other materials provided with the distribution. 0013 * 0014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0015 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0017 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0018 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0019 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0020 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0021 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0024 */ 0025 0026 #include <QtCrypto> 0027 #include <QtTest/QtTest> 0028 0029 #ifdef QT_STATICPLUGIN 0030 #include "import_plugins.h" 0031 #endif 0032 0033 class MACUnitTest : public QObject 0034 { 0035 Q_OBJECT 0036 0037 private Q_SLOTS: 0038 void initTestCase(); 0039 void cleanupTestCase(); 0040 void HMACMD5(); 0041 void HMACSHA1(); 0042 void HMACSHA256(); 0043 void HMACSHA224(); 0044 void HMACSHA384(); 0045 void HMACSHA512(); 0046 void HMACRMD160(); 0047 0048 private: 0049 QCA::Initializer *m_init; 0050 }; 0051 0052 void MACUnitTest::initTestCase() 0053 { 0054 m_init = new QCA::Initializer; 0055 } 0056 0057 void MACUnitTest::cleanupTestCase() 0058 { 0059 delete m_init; 0060 } 0061 0062 void MACUnitTest::HMACMD5() 0063 { 0064 QStringList providersToTest; 0065 providersToTest.append(QStringLiteral("qca-ossl")); 0066 providersToTest.append(QStringLiteral("qca-gcrypt")); 0067 providersToTest.append(QStringLiteral("qca-botan")); 0068 providersToTest.append(QStringLiteral("qca-nss")); 0069 0070 foreach (const QString provider, providersToTest) { 0071 if (!QCA::isSupported("hmac(md5)", provider)) 0072 QWARN((QStringLiteral("HMAC(MD5) not supported for ") + provider).toLocal8Bit().constData()); 0073 else { 0074 QCA::MessageAuthenticationCode md5hmacLenTest(QStringLiteral("hmac(md5)"), QCA::SymmetricKey(), provider); 0075 QCOMPARE(md5hmacLenTest.validKeyLength(0), true); 0076 QCOMPARE(md5hmacLenTest.validKeyLength(1), true); 0077 QCOMPARE(md5hmacLenTest.validKeyLength(848888), true); 0078 QCOMPARE(md5hmacLenTest.validKeyLength(-2), false); 0079 0080 QCA::MessageAuthenticationCode copy = md5hmacLenTest; 0081 copy.context(); // detach 0082 0083 // These tests are from RFC2202, Section 2. 0084 // The first three are also in the Appendix to RFC2104 0085 QCA::MessageAuthenticationCode md5hmac1(QStringLiteral("hmac(md5)"), QCA::SymmetricKey(), provider); 0086 QCA::SymmetricKey key1(QCA::SecureArray("Jefe")); 0087 md5hmac1.setup(key1); 0088 QCA::SecureArray data1("what do ya want for nothing?"); 0089 md5hmac1.update(data1); 0090 QCOMPARE(QCA::arrayToHex(md5hmac1.final().toByteArray()), 0091 QStringLiteral("750c783e6ab0b503eaa86e310a5db738")); 0092 0093 QCA::MessageAuthenticationCode md5hmac2(QStringLiteral("hmac(md5)"), QCA::SymmetricKey(), provider); 0094 QCA::SymmetricKey key2(QCA::hexToArray(QStringLiteral("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))); 0095 md5hmac2.setup(key2); 0096 QCA::SecureArray data2 = QCA::SecureArray("Hi There"); 0097 md5hmac2.update(data2); 0098 QCOMPARE(QCA::arrayToHex(md5hmac2.final().toByteArray()), 0099 QStringLiteral("9294727a3638bb1c13f48ef8158bfc9d")); 0100 0101 // test reuse 0102 md5hmac2.clear(); 0103 QCA::SymmetricKey key3(QCA::hexToArray(QStringLiteral("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"))); 0104 md5hmac2.setup(key3); 0105 QCA::SecureArray data3(50); 0106 for (int i = 0; i < data3.size(); i++) 0107 data3[i] = (char)0xDD; 0108 md5hmac2.update(data3); 0109 QCOMPARE(QCA::arrayToHex(md5hmac2.final().toByteArray()), 0110 QStringLiteral("56be34521d144c88dbb8c733f0e8b3f6")); 0111 0112 QCA::SymmetricKey key4( 0113 QCA::hexToArray(QStringLiteral("0102030405060708090a0b0c0d0e0f10111213141516171819"))); 0114 QCA::MessageAuthenticationCode md5hmac4(QStringLiteral("hmac(md5)"), key4, provider); 0115 QCA::SecureArray data4(50); 0116 for (int i = 0; i < data4.size(); i++) 0117 data4[i] = (char)0xcd; 0118 md5hmac4.update(data4); 0119 QCOMPARE(QCA::arrayToHex(md5hmac4.final().toByteArray()), 0120 QStringLiteral("697eaf0aca3a3aea3a75164746ffaa79")); 0121 0122 QCA::MessageAuthenticationCode md5hmac5(QStringLiteral("hmac(md5)"), QCA::SecureArray()); 0123 QCA::SymmetricKey key5(QCA::hexToArray(QStringLiteral("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"))); 0124 md5hmac5.setup(key5); 0125 QCA::SecureArray data5("Test With Truncation"); 0126 md5hmac5.update(data5); 0127 QCOMPARE(QCA::arrayToHex(md5hmac5.final().toByteArray()), 0128 QStringLiteral("56461ef2342edc00f9bab995690efd4c")); 0129 0130 QCA::MessageAuthenticationCode md5hmac6(QStringLiteral("hmac(md5)"), QCA::SymmetricKey(), provider); 0131 QCA::SymmetricKey key6(80); 0132 for (int i = 0; i < key6.size(); i++) 0133 key6[i] = (char)0xaa; 0134 md5hmac6.setup(key6); 0135 QCA::SecureArray data6("Test Using Larger Than Block-Size Key - Hash Key First"); 0136 md5hmac6.update(data6); 0137 QCOMPARE(QCA::arrayToHex(md5hmac6.final().toByteArray()), 0138 QStringLiteral("6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")); 0139 0140 md5hmac6.clear(); // reuse the same key 0141 QCA::SecureArray data7("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"); 0142 md5hmac6.update(data7); 0143 QCOMPARE(QCA::arrayToHex(md5hmac6.final().toByteArray()), 0144 QStringLiteral("6f630fad67cda0ee1fb1f562db3aa53e")); 0145 } 0146 } 0147 } 0148 0149 void MACUnitTest::HMACSHA256() 0150 { 0151 QStringList providersToTest; 0152 providersToTest.append(QStringLiteral("qca-ossl")); 0153 providersToTest.append(QStringLiteral("qca-gcrypt")); 0154 providersToTest.append(QStringLiteral("qca-botan")); 0155 providersToTest.append(QStringLiteral("qca-nss")); 0156 0157 foreach (const QString provider, providersToTest) { 0158 if (!QCA::isSupported("hmac(sha256)", provider)) 0159 QWARN((QStringLiteral("HMAC(SHA256) not supported for ") + provider).toLocal8Bit().constData()); 0160 else { 0161 QCA::MessageAuthenticationCode hmacLenTest(QStringLiteral("hmac(sha256)"), QCA::SymmetricKey(), provider); 0162 QCOMPARE(hmacLenTest.validKeyLength(0), true); 0163 QCOMPARE(hmacLenTest.validKeyLength(1), true); 0164 QCOMPARE(hmacLenTest.validKeyLength(848888), true); 0165 QCOMPARE(hmacLenTest.validKeyLength(-2), false); 0166 0167 QCA::MessageAuthenticationCode copy = hmacLenTest; 0168 copy.context(); // detach 0169 0170 QCA::MessageAuthenticationCode hmac1(QStringLiteral("hmac(sha256)"), QCA::SymmetricKey(), provider); 0171 QCA::SymmetricKey key1(QCA::SecureArray("Jefe")); 0172 hmac1.setup(key1); 0173 QCA::SecureArray data1("what do ya want for nothing?"); 0174 hmac1.update(data1); 0175 QCOMPARE(QCA::arrayToHex(hmac1.final().toByteArray()), 0176 QStringLiteral("5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843")); 0177 0178 QCA::MessageAuthenticationCode hmac2(QStringLiteral("hmac(sha256)"), QCA::SymmetricKey(), provider); 0179 QCA::SymmetricKey key2(QCA::hexToArray(QStringLiteral("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))); 0180 hmac2.setup(key2); 0181 QCA::SecureArray data2 = QCA::SecureArray("Hi There"); 0182 hmac2.update(data2); 0183 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0184 QStringLiteral("b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7")); 0185 0186 // test reuse 0187 hmac2.clear(); 0188 QCA::SymmetricKey key3(QCA::hexToArray(QStringLiteral("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); 0189 hmac2.setup(key3); 0190 QCA::SecureArray data3(50); 0191 for (int i = 0; i < data3.size(); i++) 0192 data3[i] = (char)0xDD; 0193 hmac2.update(data3); 0194 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0195 QStringLiteral("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe")); 0196 0197 QCA::SymmetricKey key4( 0198 QCA::hexToArray(QStringLiteral("0102030405060708090a0b0c0d0e0f10111213141516171819"))); 0199 QCA::MessageAuthenticationCode hmac4(QStringLiteral("hmac(sha256)"), key4, provider); 0200 QCA::SecureArray data4(50); 0201 for (int i = 0; i < data4.size(); i++) 0202 data4[i] = (char)0xcd; 0203 hmac4.update(data4); 0204 QCOMPARE(QCA::arrayToHex(hmac4.final().toByteArray()), 0205 QStringLiteral("82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b")); 0206 0207 QCA::MessageAuthenticationCode hmac5(QStringLiteral("hmac(sha256)"), QCA::SymmetricKey(), provider); 0208 QCA::SymmetricKey key5(QCA::hexToArray(QStringLiteral("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"))); 0209 hmac5.setup(key5); 0210 QCA::SecureArray data5("Test With Truncation"); 0211 hmac5.update(data5); 0212 QString resultWithTrunc = QCA::arrayToHex(hmac5.final().toByteArray()); 0213 resultWithTrunc.resize(32); 0214 QCOMPARE(resultWithTrunc, QStringLiteral("a3b6167473100ee06e0c796c2955552b")); 0215 0216 QCA::MessageAuthenticationCode hmac6(QStringLiteral("hmac(sha256)"), QCA::SymmetricKey(), provider); 0217 QCA::SymmetricKey key6(131); 0218 for (int i = 0; i < key6.size(); i++) 0219 key6[i] = (char)0xaa; 0220 hmac6.setup(key6); 0221 QCA::SecureArray data6("Test Using Larger Than Block-Size Key - Hash Key First"); 0222 hmac6.update(data6); 0223 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0224 QStringLiteral("60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54")); 0225 0226 hmac6.clear(); // reuse the same key 0227 QCA::SecureArray data7( 0228 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to " 0229 "be hashed before being used by the HMAC algorithm."); 0230 hmac6.update(data7); 0231 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0232 QStringLiteral("9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2")); 0233 } 0234 } 0235 } 0236 0237 void MACUnitTest::HMACSHA224() 0238 { 0239 QStringList providersToTest; 0240 providersToTest.append(QStringLiteral("qca-ossl")); 0241 providersToTest.append(QStringLiteral("qca-gcrypt")); 0242 providersToTest.append(QStringLiteral("qca-botan")); 0243 0244 foreach (const QString provider, providersToTest) { 0245 if (!QCA::isSupported("hmac(sha224)", provider)) 0246 QWARN((QStringLiteral("HMAC(SHA224) not supported for ") + provider).toLocal8Bit().constData()); 0247 else { 0248 QCA::MessageAuthenticationCode hmacLenTest(QStringLiteral("hmac(sha224)"), QCA::SymmetricKey(), provider); 0249 QCOMPARE(hmacLenTest.validKeyLength(0), true); 0250 QCOMPARE(hmacLenTest.validKeyLength(1), true); 0251 QCOMPARE(hmacLenTest.validKeyLength(848888), true); 0252 QCOMPARE(hmacLenTest.validKeyLength(-2), false); 0253 0254 QCA::MessageAuthenticationCode copy = hmacLenTest; 0255 copy.context(); // detach 0256 0257 QCA::MessageAuthenticationCode hmac1(QStringLiteral("hmac(sha224)"), QCA::SymmetricKey(), provider); 0258 QCA::SymmetricKey key1(QCA::SecureArray("Jefe")); 0259 hmac1.setup(key1); 0260 QCA::SecureArray data1("what do ya want for nothing?"); 0261 hmac1.update(data1); 0262 QCOMPARE(QCA::arrayToHex(hmac1.final().toByteArray()), 0263 QStringLiteral("a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44")); 0264 0265 QCA::MessageAuthenticationCode hmac2(QStringLiteral("hmac(sha224)"), QCA::SymmetricKey(), provider); 0266 QCA::SymmetricKey key2(QCA::hexToArray(QStringLiteral("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))); 0267 hmac2.setup(key2); 0268 QCA::SecureArray data2 = QCA::SecureArray("Hi There"); 0269 hmac2.update(data2); 0270 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0271 QStringLiteral("896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22")); 0272 0273 // test reuse 0274 hmac2.clear(); 0275 QCA::SymmetricKey key3(QCA::hexToArray(QStringLiteral("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); 0276 hmac2.setup(key3); 0277 QCA::SecureArray data3(50); 0278 for (int i = 0; i < data3.size(); i++) 0279 data3[i] = (char)0xDD; 0280 hmac2.update(data3); 0281 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0282 QStringLiteral("7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea")); 0283 0284 QCA::SymmetricKey key4( 0285 QCA::hexToArray(QStringLiteral("0102030405060708090a0b0c0d0e0f10111213141516171819"))); 0286 QCA::MessageAuthenticationCode hmac4(QStringLiteral("hmac(sha224)"), key4, provider); 0287 QCA::SecureArray data4(50); 0288 for (int i = 0; i < data4.size(); i++) 0289 data4[i] = (char)0xcd; 0290 hmac4.update(data4); 0291 QCOMPARE(QCA::arrayToHex(hmac4.final().toByteArray()), 0292 QStringLiteral("6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a")); 0293 0294 QCA::MessageAuthenticationCode hmac5(QStringLiteral("hmac(sha224)"), QCA::SymmetricKey(), provider); 0295 QCA::SymmetricKey key5(QCA::hexToArray(QStringLiteral("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"))); 0296 hmac5.setup(key5); 0297 QCA::SecureArray data5("Test With Truncation"); 0298 hmac5.update(data5); 0299 QString resultWithTrunc = QCA::arrayToHex(hmac5.final().toByteArray()); 0300 resultWithTrunc.resize(32); 0301 QCOMPARE(resultWithTrunc, QStringLiteral("0e2aea68a90c8d37c988bcdb9fca6fa8")); 0302 0303 QCA::MessageAuthenticationCode hmac6(QStringLiteral("hmac(sha224)"), QCA::SymmetricKey(), provider); 0304 QCA::SymmetricKey key6(131); 0305 for (int i = 0; i < key6.size(); i++) 0306 key6[i] = (char)0xaa; 0307 hmac6.setup(key6); 0308 QCA::SecureArray data6("Test Using Larger Than Block-Size Key - Hash Key First"); 0309 hmac6.update(data6); 0310 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0311 QStringLiteral("95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e")); 0312 0313 hmac6.clear(); // reuse the same key 0314 QCA::SecureArray data7( 0315 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to " 0316 "be hashed before being used by the HMAC algorithm."); 0317 hmac6.update(data7); 0318 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0319 QStringLiteral("3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1")); 0320 } 0321 } 0322 } 0323 0324 void MACUnitTest::HMACSHA384() 0325 { 0326 QStringList providersToTest; 0327 providersToTest.append(QStringLiteral("qca-ossl")); 0328 providersToTest.append(QStringLiteral("qca-gcrypt")); 0329 providersToTest.append(QStringLiteral("qca-botan")); 0330 providersToTest.append(QStringLiteral("qca-nss")); 0331 0332 foreach (const QString provider, providersToTest) { 0333 if (!QCA::isSupported("hmac(sha384)", provider)) 0334 QWARN((QStringLiteral("HMAC(SHA384) not supported for ") + provider).toLocal8Bit().constData()); 0335 else { 0336 QCA::MessageAuthenticationCode hmacLenTest(QStringLiteral("hmac(sha384)"), QCA::SymmetricKey(), provider); 0337 QCOMPARE(hmacLenTest.validKeyLength(0), true); 0338 QCOMPARE(hmacLenTest.validKeyLength(1), true); 0339 QCOMPARE(hmacLenTest.validKeyLength(848888), true); 0340 QCOMPARE(hmacLenTest.validKeyLength(-2), false); 0341 0342 QCA::MessageAuthenticationCode copy = hmacLenTest; 0343 copy.context(); // detach 0344 0345 QCA::MessageAuthenticationCode hmac1(QStringLiteral("hmac(sha384)"), QCA::SymmetricKey(), provider); 0346 QCA::SymmetricKey key1(QCA::SecureArray("Jefe")); 0347 hmac1.setup(key1); 0348 QCA::SecureArray data1("what do ya want for nothing?"); 0349 hmac1.update(data1); 0350 QCOMPARE(QCA::arrayToHex(hmac1.final().toByteArray()), 0351 QStringLiteral("af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b" 0352 "3239ecfab21649")); 0353 0354 QCA::MessageAuthenticationCode hmac2(QStringLiteral("hmac(sha384)"), QCA::SymmetricKey(), provider); 0355 QCA::SymmetricKey key2(QCA::hexToArray(QStringLiteral("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))); 0356 hmac2.setup(key2); 0357 QCA::SecureArray data2 = QCA::SecureArray("Hi There"); 0358 hmac2.update(data2); 0359 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0360 QStringLiteral("afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4a" 0361 "f152e8b2fa9cb6")); 0362 0363 // test reuse 0364 hmac2.clear(); 0365 QCA::SymmetricKey key3(QCA::hexToArray(QStringLiteral("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); 0366 hmac2.setup(key3); 0367 QCA::SecureArray data3(50); 0368 for (int i = 0; i < data3.size(); i++) 0369 data3[i] = (char)0xDD; 0370 hmac2.update(data3); 0371 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0372 QStringLiteral("88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e" 0373 "3ab6e101a34f27")); 0374 0375 QCA::SymmetricKey key4( 0376 QCA::hexToArray(QStringLiteral("0102030405060708090a0b0c0d0e0f10111213141516171819"))); 0377 QCA::MessageAuthenticationCode hmac4(QStringLiteral("hmac(sha384)"), key4, provider); 0378 QCA::SecureArray data4(50); 0379 for (int i = 0; i < data4.size(); i++) 0380 data4[i] = (char)0xcd; 0381 hmac4.update(data4); 0382 QCOMPARE(QCA::arrayToHex(hmac4.final().toByteArray()), 0383 QStringLiteral("3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679cc" 0384 "f8a386c674cffb")); 0385 0386 QCA::MessageAuthenticationCode hmac5(QStringLiteral("hmac(sha384)"), QCA::SecureArray(), provider); 0387 QCA::SymmetricKey key5(QCA::hexToArray(QStringLiteral("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"))); 0388 hmac5.setup(key5); 0389 QCA::SecureArray data5("Test With Truncation"); 0390 hmac5.update(data5); 0391 QString resultWithTrunc = QCA::arrayToHex(hmac5.final().toByteArray()); 0392 resultWithTrunc.resize(32); 0393 QCOMPARE(resultWithTrunc, QStringLiteral("3abf34c3503b2a23a46efc619baef897")); 0394 0395 QCA::MessageAuthenticationCode hmac6(QStringLiteral("hmac(sha384)"), QCA::SymmetricKey(), provider); 0396 QCA::SymmetricKey key6(131); 0397 for (int i = 0; i < key6.size(); i++) 0398 key6[i] = (char)0xaa; 0399 hmac6.setup(key6); 0400 QCA::SecureArray data6("Test Using Larger Than Block-Size Key - Hash Key First"); 0401 hmac6.update(data6); 0402 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0403 QStringLiteral("4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296" 0404 "248df163f44952")); 0405 0406 hmac6.clear(); // reuse the same key 0407 QCA::SecureArray data7( 0408 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to " 0409 "be hashed before being used by the HMAC algorithm."); 0410 hmac6.update(data7); 0411 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0412 QStringLiteral("6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d38" 0413 "60e6110c46523e")); 0414 } 0415 } 0416 } 0417 0418 void MACUnitTest::HMACSHA512() 0419 { 0420 QStringList providersToTest; 0421 providersToTest.append(QStringLiteral("qca-ossl")); 0422 providersToTest.append(QStringLiteral("qca-gcrypt")); 0423 providersToTest.append(QStringLiteral("qca-botan")); 0424 providersToTest.append(QStringLiteral("qca-nss")); 0425 0426 foreach (const QString provider, providersToTest) { 0427 if (!QCA::isSupported("hmac(sha512)", provider)) 0428 QWARN((QStringLiteral("HMAC(SHA512) not supported for ") + provider).toLocal8Bit().constData()); 0429 else { 0430 QCA::MessageAuthenticationCode hmacLenTest(QStringLiteral("hmac(sha512)"), QCA::SymmetricKey(), provider); 0431 QCOMPARE(hmacLenTest.validKeyLength(0), true); 0432 QCOMPARE(hmacLenTest.validKeyLength(1), true); 0433 QCOMPARE(hmacLenTest.validKeyLength(848888), true); 0434 QCOMPARE(hmacLenTest.validKeyLength(-2), false); 0435 0436 QCA::MessageAuthenticationCode copy = hmacLenTest; 0437 copy.context(); // detach 0438 0439 QCA::MessageAuthenticationCode hmac1(QStringLiteral("hmac(sha512)"), QCA::SymmetricKey(), provider); 0440 QCA::SymmetricKey key1(QCA::SecureArray("Jefe")); 0441 hmac1.setup(key1); 0442 QCA::SecureArray data1("what do ya want for nothing?"); 0443 hmac1.update(data1); 0444 QCOMPARE(QCA::arrayToHex(hmac1.final().toByteArray()), 0445 QStringLiteral("164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d" 0446 "034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737")); 0447 0448 QCA::MessageAuthenticationCode hmac2(QStringLiteral("hmac(sha512)"), QCA::SymmetricKey(), provider); 0449 QCA::SymmetricKey key2(QCA::hexToArray(QStringLiteral("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))); 0450 hmac2.setup(key2); 0451 QCA::SecureArray data2 = QCA::SecureArray("Hi There"); 0452 hmac2.update(data2); 0453 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0454 QStringLiteral("87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a70203" 0455 "8b274eaea3f4e4be9d914eeb61f1702e696c203a126854")); 0456 0457 // test reuse 0458 hmac2.clear(); 0459 QCA::SymmetricKey key3(QCA::hexToArray(QStringLiteral("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); 0460 hmac2.setup(key3); 0461 QCA::SecureArray data3(50); 0462 for (int i = 0; i < data3.size(); i++) 0463 data3[i] = (char)0xDD; 0464 hmac2.update(data3); 0465 QCOMPARE(QCA::arrayToHex(hmac2.final().toByteArray()), 0466 QStringLiteral("fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806" 0467 "b485a47e67c807b946a337bee8942674278859e13292fb")); 0468 0469 QCA::SymmetricKey key4( 0470 QCA::hexToArray(QStringLiteral("0102030405060708090a0b0c0d0e0f10111213141516171819"))); 0471 QCA::MessageAuthenticationCode hmac4(QStringLiteral("hmac(sha512)"), key4, provider); 0472 QCA::SecureArray data4(50); 0473 for (int i = 0; i < data4.size(); i++) 0474 data4[i] = (char)0xcd; 0475 hmac4.update(data4); 0476 QCOMPARE(QCA::arrayToHex(hmac4.final().toByteArray()), 0477 QStringLiteral("b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d6" 0478 "79275cc5788063a5f19741120c4f2de2adebeb10a298dd")); 0479 0480 QCA::MessageAuthenticationCode hmac5(QStringLiteral("hmac(sha512)"), QCA::SecureArray(), provider); 0481 QCA::SymmetricKey key5(QCA::hexToArray(QStringLiteral("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"))); 0482 hmac5.setup(key5); 0483 QCA::SecureArray data5("Test With Truncation"); 0484 hmac5.update(data5); 0485 QString resultWithTrunc = QCA::arrayToHex(hmac5.final().toByteArray()); 0486 resultWithTrunc.resize(32); 0487 QCOMPARE(resultWithTrunc, QStringLiteral("415fad6271580a531d4179bc891d87a6")); 0488 0489 QCA::MessageAuthenticationCode hmac6(QStringLiteral("hmac(sha512)"), QCA::SymmetricKey(), provider); 0490 QCA::SymmetricKey key6(131); 0491 for (int i = 0; i < key6.size(); i++) 0492 key6[i] = (char)0xaa; 0493 hmac6.setup(key6); 0494 QCA::SecureArray data6("Test Using Larger Than Block-Size Key - Hash Key First"); 0495 hmac6.update(data6); 0496 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0497 QStringLiteral("80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd" 0498 "0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598")); 0499 0500 hmac6.clear(); // reuse the same key 0501 QCA::SecureArray data7( 0502 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to " 0503 "be hashed before being used by the HMAC algorithm."); 0504 hmac6.update(data7); 0505 QCOMPARE(QCA::arrayToHex(hmac6.final().toByteArray()), 0506 QStringLiteral("e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d" 0507 "5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58")); 0508 } 0509 } 0510 } 0511 0512 void MACUnitTest::HMACSHA1() 0513 { 0514 QStringList providersToTest; 0515 providersToTest.append(QStringLiteral("qca-ossl")); 0516 providersToTest.append(QStringLiteral("qca-gcrypt")); 0517 providersToTest.append(QStringLiteral("qca-botan")); 0518 providersToTest.append(QStringLiteral("qca-nss")); 0519 0520 foreach (const QString provider, providersToTest) { 0521 if (!QCA::isSupported("hmac(sha1)", provider)) 0522 QWARN((QStringLiteral("HMAC(SHA1) not supported for ") + provider).toLocal8Bit().constData()); 0523 else { 0524 QCA::MessageAuthenticationCode sha1hmacLenTest(QStringLiteral("hmac(sha1)"), QCA::SymmetricKey(), provider); 0525 QCOMPARE(sha1hmacLenTest.validKeyLength(0), true); 0526 QCOMPARE(sha1hmacLenTest.validKeyLength(1), true); 0527 QCOMPARE(sha1hmacLenTest.validKeyLength(848888), true); 0528 QCOMPARE(sha1hmacLenTest.validKeyLength(-2), false); 0529 0530 QCA::MessageAuthenticationCode copy = sha1hmacLenTest; 0531 copy.context(); // detach 0532 0533 // These tests are from RFC2202, Section 3. 0534 QCA::MessageAuthenticationCode test1(QStringLiteral("hmac(sha1)"), QCA::SecureArray()); 0535 QCA::SymmetricKey key1(QCA::hexToArray(QStringLiteral("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))); 0536 test1.setup(key1); 0537 QCA::SecureArray data1("Hi There"); 0538 test1.update(data1); 0539 QCOMPARE(QCA::arrayToHex(test1.final().toByteArray()), 0540 QStringLiteral("b617318655057264e28bc0b6fb378c8ef146be00")); 0541 0542 QCA::MessageAuthenticationCode test2(QStringLiteral("hmac(sha1)"), QCA::SymmetricKey(), provider); 0543 QCA::SymmetricKey key2(QCA::SecureArray("Jefe")); 0544 test2.setup(key2); 0545 QCA::SecureArray data2("what do ya want for nothing?"); 0546 test2.update(data2); 0547 QCOMPARE(QCA::arrayToHex(test2.final().toByteArray()), 0548 QStringLiteral("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")); 0549 0550 QCA::MessageAuthenticationCode test3(QStringLiteral("hmac(sha1)"), QCA::SymmetricKey(), provider); 0551 QCA::SymmetricKey key3(QCA::hexToArray(QStringLiteral("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); 0552 test3.setup(key3); 0553 QCA::SecureArray data3(50); 0554 for (int i = 0; i < data3.size(); i++) 0555 data3[i] = (char)0xDD; 0556 test3.update(data3); 0557 QCOMPARE(QCA::arrayToHex(test3.final().toByteArray()), 0558 QStringLiteral("125d7342b9ac11cd91a39af48aa17b4f63f175d3")); 0559 0560 QCA::MessageAuthenticationCode test4(QStringLiteral("hmac(sha1)"), QCA::SymmetricKey(), provider); 0561 QCA::SymmetricKey key4( 0562 QCA::hexToArray(QStringLiteral("0102030405060708090a0b0c0d0e0f10111213141516171819"))); 0563 test4.setup(key4); 0564 QCA::SecureArray data4(50); 0565 for (int i = 0; i < data4.size(); i++) 0566 data4[i] = (char)0xcd; 0567 test4.update(data4); 0568 QCOMPARE(QCA::arrayToHex(test4.final().toByteArray()), 0569 QStringLiteral("4c9007f4026250c6bc8414f9bf50c86c2d7235da")); 0570 0571 QCA::MessageAuthenticationCode test5(QStringLiteral("hmac(sha1)"), QCA::SymmetricKey(), provider); 0572 QCA::SymmetricKey key5(QCA::hexToArray(QStringLiteral("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"))); 0573 test5.setup(key5); 0574 QCA::SecureArray data5("Test With Truncation"); 0575 test5.update(data5); 0576 QCOMPARE(QCA::arrayToHex(test5.final().toByteArray()), 0577 QStringLiteral("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")); 0578 0579 QCA::MessageAuthenticationCode test6(QStringLiteral("hmac(sha1)"), QCA::SymmetricKey(), provider); 0580 QCA::SymmetricKey key6(80); 0581 for (int i = 0; i < key6.size(); i++) 0582 key6[i] = (char)0xAA; 0583 test6.setup(key6); 0584 QCA::SecureArray data6("Test Using Larger Than Block-Size Key - Hash Key First"); 0585 test6.update(data6); 0586 QCOMPARE(QCA::arrayToHex(test6.final().toByteArray()), 0587 QStringLiteral("aa4ae5e15272d00e95705637ce8a3b55ed402112")); 0588 0589 test6.clear(); // this should reuse the same key 0590 QCA::SecureArray data7("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"); 0591 test6.update(data7); 0592 QCOMPARE(QCA::arrayToHex(test6.final().toByteArray()), 0593 QStringLiteral("e8e99d0f45237d786d6bbaa7965c7808bbff1a91")); 0594 } 0595 } 0596 } 0597 0598 void MACUnitTest::HMACRMD160() 0599 { 0600 QStringList providersToTest; 0601 providersToTest.append(QStringLiteral("qca-ossl")); 0602 providersToTest.append(QStringLiteral("qca-gcrypt")); 0603 providersToTest.append(QStringLiteral("qca-botan")); 0604 providersToTest.append(QStringLiteral("qca-nss")); 0605 0606 foreach (const QString provider, providersToTest) { 0607 if (!QCA::isSupported("hmac(ripemd160)", provider)) 0608 QWARN((QStringLiteral("HMAC(RIPEMD160) not supported for ") + provider).toLocal8Bit().constData()); 0609 else { 0610 QCA::MessageAuthenticationCode ripemd160hmacLenTest( 0611 QStringLiteral("hmac(ripemd160)"), QCA::SymmetricKey(), provider); 0612 QCOMPARE(ripemd160hmacLenTest.validKeyLength(0), true); 0613 QCOMPARE(ripemd160hmacLenTest.validKeyLength(1), true); 0614 QCOMPARE(ripemd160hmacLenTest.validKeyLength(848888), true); 0615 QCOMPARE(ripemd160hmacLenTest.validKeyLength(-2), false); 0616 0617 QCA::MessageAuthenticationCode copy = ripemd160hmacLenTest; 0618 copy.context(); // detach 0619 0620 // These tests are from RFC2286, Section 2. 0621 QCA::MessageAuthenticationCode test1(QStringLiteral("hmac(ripemd160)"), QCA::SymmetricKey(), provider); 0622 QCA::SymmetricKey key1(QCA::hexToArray(QStringLiteral("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))); 0623 test1.setup(key1); 0624 QCA::SecureArray data1("Hi There"); 0625 test1.update(data1); 0626 QCOMPARE(QCA::arrayToHex(test1.final().toByteArray()), 0627 QStringLiteral("24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668")); 0628 0629 QCA::MessageAuthenticationCode test2(QStringLiteral("hmac(ripemd160)"), QCA::SymmetricKey(), provider); 0630 QCA::SymmetricKey key2(QCA::SecureArray("Jefe")); 0631 test2.setup(key2); 0632 QCA::SecureArray data2("what do ya want for nothing?"); 0633 test2.update(data2); 0634 QCOMPARE(QCA::arrayToHex(test2.final().toByteArray()), 0635 QStringLiteral("dda6c0213a485a9e24f4742064a7f033b43c4069")); 0636 0637 QCA::MessageAuthenticationCode test3(QStringLiteral("hmac(ripemd160)"), QCA::SymmetricKey(), provider); 0638 QCA::SymmetricKey key3(QCA::hexToArray(QStringLiteral("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); 0639 test3.setup(key3); 0640 QCA::SecureArray data3(50); 0641 for (int i = 0; i < data3.size(); i++) 0642 data3[i] = (char)0xDD; 0643 test3.update(data3); 0644 QCOMPARE(QCA::arrayToHex(test3.final().toByteArray()), 0645 QStringLiteral("b0b105360de759960ab4f35298e116e295d8e7c1")); 0646 0647 QCA::SymmetricKey key4( 0648 QCA::hexToArray(QStringLiteral("0102030405060708090a0b0c0d0e0f10111213141516171819"))); 0649 QCA::MessageAuthenticationCode test4(QStringLiteral("hmac(ripemd160)"), key4, provider); 0650 QCA::SecureArray data4(50); 0651 for (int i = 0; i < data4.size(); i++) 0652 data4[i] = (char)0xcd; 0653 test4.update(data4); 0654 QCOMPARE(QCA::arrayToHex(test4.final().toByteArray()), 0655 QStringLiteral("d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4")); 0656 0657 QCA::MessageAuthenticationCode test5(QStringLiteral("hmac(ripemd160)"), QCA::SymmetricKey(), provider); 0658 QCA::SymmetricKey key5(QCA::hexToArray(QStringLiteral("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"))); 0659 test5.setup(key5); 0660 QCA::SecureArray data5("Test With Truncation"); 0661 test5.update(data5); 0662 QCOMPARE(QCA::arrayToHex(test5.final().toByteArray()), 0663 QStringLiteral("7619693978f91d90539ae786500ff3d8e0518e39")); 0664 0665 QCA::MessageAuthenticationCode test6(QStringLiteral("hmac(ripemd160)"), QCA::SymmetricKey(), provider); 0666 QCA::SymmetricKey key6(80); 0667 for (int i = 0; i < key6.size(); i++) 0668 key6[i] = (char)0xAA; 0669 test6.setup(key6); 0670 QCA::SecureArray data6("Test Using Larger Than Block-Size Key - Hash Key First"); 0671 test6.update(data6); 0672 QCOMPARE(QCA::arrayToHex(test6.final().toByteArray()), 0673 QStringLiteral("6466ca07ac5eac29e1bd523e5ada7605b791fd8b")); 0674 0675 test6.clear(); // reuse the key 0676 QCA::SecureArray data7("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"); 0677 test6.update(data7); 0678 QCOMPARE(QCA::arrayToHex(test6.final().toByteArray()), 0679 QStringLiteral("69ea60798d71616cce5fd0871e23754cd75d5a0a")); 0680 } 0681 } 0682 } 0683 0684 QTEST_MAIN(MACUnitTest) 0685 0686 #include "macunittest.moc"