File indexing completed on 2024-04-28 04:44:09

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 KeyGenUnitTest : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 private Q_SLOTS:
0038     void initTestCase();
0039     void cleanupTestCase();
0040     void testRSA();
0041     void testDSA();
0042     void testDH();
0043 
0044 private:
0045     QCA::Initializer *m_init;
0046 };
0047 
0048 void KeyGenUnitTest::initTestCase()
0049 {
0050     m_init = new QCA::Initializer;
0051 }
0052 
0053 void KeyGenUnitTest::cleanupTestCase()
0054 {
0055     delete m_init;
0056 }
0057 
0058 void KeyGenUnitTest::testRSA()
0059 {
0060     QCA::KeyGenerator keygen;
0061     QCOMPARE(keygen.isBusy(), false);
0062     QCOMPARE(keygen.blockingEnabled(), true);
0063 
0064     if (!QCA::isSupported("pkey") || !QCA::PKey::supportedTypes().contains(QCA::PKey::RSA) ||
0065         !QCA::PKey::supportedIOTypes().contains(QCA::PKey::RSA))
0066         QSKIP("RSA not supported!");
0067 
0068     QCA::PrivateKey    priv1 = keygen.createRSA(1024, 65537);
0069     QCA::RSAPrivateKey rsa1  = priv1.toRSA();
0070     QCOMPARE(rsa1.isNull(), false);
0071     QCOMPARE(rsa1.e(), QCA::BigInteger(65537));
0072     QCOMPARE(rsa1.bitSize(), 1024);
0073 
0074     priv1 = keygen.createRSA(512, 17);
0075     rsa1  = priv1.toRSA();
0076     QCOMPARE(rsa1.isNull(), false);
0077     QCOMPARE(rsa1.e(), QCA::BigInteger(17));
0078     QCOMPARE(rsa1.bitSize(), 512);
0079 
0080     priv1 = keygen.createRSA(512, 3);
0081     rsa1  = priv1.toRSA();
0082     QCOMPARE(rsa1.isNull(), false);
0083     QCOMPARE(rsa1.e(), QCA::BigInteger(3));
0084     QCOMPARE(rsa1.bitSize(), 512);
0085 }
0086 
0087 void KeyGenUnitTest::testDSA()
0088 {
0089     QCA::KeyGenerator keygen;
0090     QCOMPARE(keygen.isBusy(), false);
0091     QCOMPARE(keygen.blockingEnabled(), true);
0092 
0093     if (!QCA::isSupported("pkey") || !QCA::PKey::supportedTypes().contains(QCA::PKey::DSA) ||
0094         !QCA::PKey::supportedIOTypes().contains(QCA::PKey::DSA))
0095         QSKIP("DSA not supported!");
0096 
0097     QCA::DLGroup       group;
0098     QCA::PrivateKey    priv2;
0099     QCA::DSAPrivateKey dsa1;
0100 
0101     if (QCA::DLGroup::supportedGroupSets().contains(QCA::DSA_512)) {
0102         group = keygen.createDLGroup(QCA::DSA_512);
0103         priv2 = keygen.createDSA(group);
0104         dsa1  = priv2.toDSA();
0105         QCOMPARE(dsa1.isNull(), false);
0106         QCOMPARE(dsa1.bitSize(), 512);
0107     }
0108 
0109     if (QCA::DLGroup::supportedGroupSets().contains(QCA::DSA_768)) {
0110         group = keygen.createDLGroup(QCA::DSA_768);
0111         priv2 = keygen.createDSA(group);
0112         dsa1  = priv2.toDSA();
0113         QCOMPARE(dsa1.isNull(), false);
0114         QCOMPARE(dsa1.bitSize(), 768);
0115     }
0116 
0117     if (QCA::DLGroup::supportedGroupSets().contains(QCA::DSA_1024)) {
0118         group = keygen.createDLGroup(QCA::DSA_1024);
0119         priv2 = keygen.createDSA(group);
0120         dsa1  = priv2.toDSA();
0121         QCOMPARE(dsa1.isNull(), false);
0122         QCOMPARE(dsa1.bitSize(), 1024);
0123     }
0124 }
0125 
0126 void KeyGenUnitTest::testDH()
0127 {
0128     QCA::KeyGenerator keygen;
0129     QCOMPARE(keygen.isBusy(), false);
0130     QCOMPARE(keygen.blockingEnabled(), true);
0131 
0132     if (!QCA::isSupported("pkey") || !QCA::PKey::supportedTypes().contains(QCA::PKey::DH) ||
0133         !QCA::PKey::supportedIOTypes().contains(QCA::PKey::DH))
0134         QSKIP("DH not supported!");
0135 
0136     QCA::DLGroup      group = keygen.createDLGroup(QCA::IETF_1024);
0137     QCA::PrivateKey   priv3 = keygen.createDH(group);
0138     QCA::DHPrivateKey dh1   = priv3.toDH();
0139     QCOMPARE(dh1.isNull(), false);
0140     QCOMPARE(dh1.bitSize(), 1024);
0141 
0142     group = keygen.createDLGroup(QCA::IETF_2048);
0143     priv3 = keygen.createDH(group);
0144     dh1   = priv3.toDH();
0145     QCOMPARE(dh1.isNull(), false);
0146     QCOMPARE(dh1.bitSize(), 2048);
0147 }
0148 
0149 QTEST_MAIN(KeyGenUnitTest)
0150 
0151 #include "keygenunittest.moc"