File indexing completed on 2024-10-06 07:22:34

0001 /*
0002  Copyright (C) 2003 Justin Karneges <justin@affinix.com>
0003  Copyright (C) 2005-2006 Brad Hards <bradh@frogmouth.net>
0004 
0005  Permission is hereby granted, free of charge, to any person obtaining a copy
0006  of this software and associated documentation files (the "Software"), to deal
0007  in the Software without restriction, including without limitation the rights
0008  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  copies of the Software, and to permit persons to whom the Software is
0010  furnished to do so, subject to the following conditions:
0011 
0012  The above copyright notice and this permission notice shall be included in
0013  all copies or substantial portions of the Software.
0014 
0015  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0018  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0019  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0020  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0021 */
0022 
0023 // QtCrypto has the declarations for all of QCA
0024 #include <QtCrypto>
0025 #include <cstdio>
0026 
0027 #include <QCoreApplication>
0028 
0029 #ifdef QT_STATICPLUGIN
0030 #include "import_plugins.h"
0031 #endif
0032 
0033 int main(int argc, char **argv)
0034 {
0035     // the Initializer object sets things up, and
0036     // also does cleanup when it goes out of scope
0037     QCA::Initializer init;
0038 
0039     QCoreApplication app(argc, argv);
0040 
0041     // we use the first argument if provided, or
0042     // use "hello" if no arguments
0043     QCA::SecureArray arg = (argc >= 2) ? argv[1] : "hello";
0044 
0045     // AES128 testing
0046     if (!QCA::isSupported("aes128-cbc-pkcs7"))
0047         printf("AES128-CBC not supported!\n");
0048     else {
0049         // Create a random key - you'd probably use one from another
0050         // source in a real application
0051         QCA::SymmetricKey key(16);
0052 
0053         // Create a random initialisation vector - you need this
0054         // value to decrypt the resulting cipher text, but it
0055         // need not be kept secret (unlike the key).
0056         QCA::InitializationVector iv(16);
0057 
0058         // create a 128 bit AES cipher object using Cipher Block Chaining (CBC) mode
0059         QCA::Cipher cipher(QStringLiteral("aes128"),
0060                            QCA::Cipher::CBC,
0061                            // use Default padding, which is equivalent to PKCS7 for CBC
0062                            QCA::Cipher::DefaultPadding,
0063                            // this object will encrypt
0064                            QCA::Encode,
0065                            key,
0066                            iv);
0067 
0068         // we use the cipher object to encrypt the argument we passed in
0069         // the result of that is returned - note that if there is less than
0070         // 16 bytes (1 block), then nothing will be returned - it is buffered
0071         // update() can be called as many times as required.
0072         QCA::SecureArray u = cipher.update(arg);
0073 
0074         // We need to check if that update() call worked.
0075         if (!cipher.ok()) {
0076             printf("Update failed\n");
0077         }
0078         // output the results of that stage
0079         printf("AES128 encryption of %s is [%s]\n", arg.data(), qPrintable(QCA::arrayToHex(u.toByteArray())));
0080 
0081         // Because we are using PKCS7 padding, we need to output the final (padded) block
0082         // Note that we should always call final() even with no padding, to clean up
0083         QCA::SecureArray f = cipher.final();
0084 
0085         // Check if the final() call worked
0086         if (!cipher.ok()) {
0087             printf("Final failed\n");
0088         }
0089         // and output the resulting block. The ciphertext is the results of update()
0090         // and the result of final()
0091         printf("Final block for AES128 encryption is [0x%s]\n", qPrintable(QCA::arrayToHex(f.toByteArray())));
0092 
0093         // re-use the Cipher t decrypt. We need to use the same key and
0094         // initialisation vector as in the encryption.
0095         cipher.setup(QCA::Decode, key, iv);
0096 
0097         // Build a single cipher text array. You could also call update() with
0098         // each block as you receive it, if that is more useful.
0099         QCA::SecureArray cipherText = u.append(f);
0100 
0101         // take that cipher text, and decrypt it
0102         QCA::SecureArray plainText = cipher.update(cipherText);
0103 
0104         // check if the update() call worked
0105         if (!cipher.ok()) {
0106             printf("Update failed\n");
0107         }
0108 
0109         // output results
0110         printf("Decryption using AES128 of [0x%s] is %s\n",
0111                qPrintable(QCA::arrayToHex(cipherText.toByteArray())),
0112                plainText.data());
0113 
0114         // Again we need to call final(), to get the last block (with its padding removed)
0115         plainText = cipher.final();
0116 
0117         // check if the final() call worked
0118         if (!cipher.ok()) {
0119             printf("Final failed\n");
0120         }
0121 
0122         // output results
0123         printf("Final decryption block using AES128 is %s\n", plainText.data());
0124         // instead of update() and final(), you can do the whole thing
0125         // in one step, using process()
0126         printf("One step decryption using AES128: %s\n", QCA::SecureArray(cipher.process(cipherText)).data());
0127     }
0128 
0129     return 0;
0130 }