diff --git a/cryptosample/.settings/com.rim.tad.tools.qml.core.prefs b/cryptosample/.settings/com.rim.tad.tools.qml.core.prefs index 96521d0..164517e 100644 --- a/cryptosample/.settings/com.rim.tad.tools.qml.core.prefs +++ b/cryptosample/.settings/com.rim.tad.tools.qml.core.prefs @@ -1,3 +1,3 @@ -config-pri.hash=C598770B9A21E021095B4916F1E3A3C4 +config-pri.hash=F25247CC56EBC95D03B734CAF613E130 config-pri.version=2.0 eclipse.preferences.version=1 diff --git a/cryptosample/assets/data/sample_clear_text b/cryptosample/assets/data/sample_clear_text new file mode 100644 index 0000000..34b8080 --- /dev/null +++ b/cryptosample/assets/data/sample_clear_text @@ -0,0 +1,11 @@ +Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque +laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi +architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia +voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores +eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum +quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi +tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad +minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut +aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate +velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo +voluptas nulla pariatur? \ No newline at end of file diff --git a/cryptosample/assets/main.qml b/cryptosample/assets/main.qml index 70b6cfe..c50c4d8 100644 --- a/cryptosample/assets/main.qml +++ b/cryptosample/assets/main.qml @@ -49,6 +49,7 @@ Page { property bool doingHash: false property bool doingRsa: false property bool doingKdf: false + property bool doingStream: false property int hashType: 0 // ======== SIGNAL()s ============== @@ -69,6 +70,10 @@ Page { signal endKdf() signal doKdf() + signal initStream() + signal endStream() + signal doStream() + // ======== SLOT()s ================ function onMessage(text) { @@ -104,7 +109,7 @@ Page { } Button { id: initRandomNumber - enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf + enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf && !mainPage.doingStream text: "Init Random" horizontalAlignment: HorizontalAlignment.Center layoutProperties: StackLayoutProperties { @@ -148,7 +153,7 @@ Page { } Button { id: initSecurity - enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf + enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf && !mainPage.doingStream text: "Init Hash" horizontalAlignment: HorizontalAlignment.Center layoutProperties: StackLayoutProperties { @@ -247,7 +252,7 @@ Page { } Button { id: initRsa - enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf + enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf && !mainPage.doingStream text: "Init RSA" horizontalAlignment: HorizontalAlignment.Center layoutProperties: StackLayoutProperties { @@ -291,7 +296,7 @@ Page { } Button { id: initKdf - enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf + enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf && !mainPage.doingStream text: "Init KDF" horizontalAlignment: HorizontalAlignment.Center layoutProperties: StackLayoutProperties { @@ -329,6 +334,50 @@ Page { } } + Container { + layout: StackLayout { + orientation: LayoutOrientation.LeftToRight + } + Button { + id: initStream + enabled: !mainPage.doingRandom && !mainPage.doingHash && !mainPage.doingRsa && !mainPage.doingKdf && !mainPage.doingStream + text: "Init Stream" + horizontalAlignment: HorizontalAlignment.Center + layoutProperties: StackLayoutProperties { + spaceQuota: 33 + } + onClicked: { + enabled: mainPage.doingStream = true; + mainPage.initStream(); + } + } + Button { + id: doStream + enabled: mainPage.doingStream + text: "Do Stream" + horizontalAlignment: HorizontalAlignment.Center + layoutProperties: StackLayoutProperties { + spaceQuota: 33 + } + onClicked: { + mainPage.doStream(); + } + } + Button { + id: endStream + enabled: mainPage.doingStream + text: "End Stream" + horizontalAlignment: HorizontalAlignment.Center + layoutProperties: StackLayoutProperties { + spaceQuota: 33 + } + onClicked: { + enabled: mainPage.doingStream = false; + mainPage.endStream(); + } + } + } + Logger { id: log } diff --git a/cryptosample/bar-descriptor.xml b/cryptosample/bar-descriptor.xml index 6a86b6e..b8fa56d 100644 --- a/cryptosample/bar-descriptor.xml +++ b/cryptosample/bar-descriptor.xml @@ -94,6 +94,7 @@ run_native + access_shared diff --git a/cryptosample/config.pri b/cryptosample/config.pri index c9a124e..a3efeee 100644 --- a/cryptosample/config.pri +++ b/cryptosample/config.pri @@ -66,7 +66,10 @@ lupdate_inclusion { $$quote($$BASEDIR/../src/*.cxx) \ $$quote($$BASEDIR/../assets/*.qml) \ $$quote($$BASEDIR/../assets/*.js) \ - $$quote($$BASEDIR/../assets/*.qs) + $$quote($$BASEDIR/../assets/*.qs) \ + $$quote($$BASEDIR/../assets/data/*.qml) \ + $$quote($$BASEDIR/../assets/data/*.js) \ + $$quote($$BASEDIR/../assets/data/*.qs) HEADERS += \ $$quote($$BASEDIR/../src/*.h) \ diff --git a/cryptosample/src/applicationui.cpp b/cryptosample/src/applicationui.cpp index 40b5355..c4f2a43 100644 --- a/cryptosample/src/applicationui.cpp +++ b/cryptosample/src/applicationui.cpp @@ -26,6 +26,9 @@ ApplicationUI::ApplicationUI() , _expireTimer(new QTimer(this)) , _keyBitLength(2048) , _keyPubExp(3) + , _arc4Params(NULL) + , _arc4Key(NULL) + , _arc4Context(NULL) { _translator = new QTranslator(this); _localeHandler = new LocaleHandler(this); @@ -76,6 +79,13 @@ ApplicationUI::ApplicationUI() QObject::connect( _mainPage, SIGNAL(doKdf()), this, SLOT(onDoKdf())); + QObject::connect( _mainPage, SIGNAL(initStream()), + this, SLOT(onInitStream())); + QObject::connect( _mainPage, SIGNAL(endStream()), + this, SLOT(onEndStream())); + QObject::connect( _mainPage, SIGNAL(doStream()), + this, SLOT(onDoStream())); + Application::instance()->setScene(_root); } @@ -491,6 +501,140 @@ void ApplicationUI::onDoKdf() { "onMakeHash onDoKdf()" ); } +void ApplicationUI::onInitStream() { + emit message("onInitStream()"); + + int rc = hu_GlobalCtxCreateDefault(&_sbCtx); + _CHECKRC( rc, "onInitStream hu_GlobalCtxCreateDefault()" ); + + rc = hu_RegisterSbg56(_sbCtx); + _CHECKRC( rc, "onInitStream hu_RegisterSbg56()" ); + + rc = hu_RegisterSystemSeed(_sbCtx); + _CHECKRC( rc, "onInitStream hu_RegisterSystemSeed()" ); + + rc = hu_InitSbg56(_sbCtx); + if (rc != SB_FAIL_LIBRARY_ALREADY_INIT) { + _CHECKRC( rc, "onInitStream hu_InitSbg56()" ); + } + + uchar mySeed[4]; + size_t seedLen = 4; + + rc = hu_SeedGet(&seedLen, mySeed, _sbCtx); + _CHECKRC( rc, "onInitStream hu_SeedGet()" ); + + rc = hu_RngCreate(seedLen, mySeed, NULL, NULL, NULL, &_rngCtx, _sbCtx); + _CHECKRC( rc, "onInitStream hu_RngCreate()" ); + + rc = hu_ARC4ParamsCreate(_rngCtx, NULL, &_arc4Params, _sbCtx); + _CHECKRC( rc, "onInitStream hu_ARC4ParamsCreate()" ); + + const unsigned char keyValue[] = { 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'k', 'e', 'y', ' ', + 'v', 'a', 'l', 'u', 'e', ' ', 't', 'h', 'a', 't', + 'w', 'i', 'l', 'l', ' ', 'b', 'e', ' ', 'u', 's', 'e', 'd', ' ', + 'f', 'o', 'r', ' ', 'a', 'r', 'c', '4', '!' }; + + rc = hu_ARC4KeySet(_arc4Params, sizeof(keyValue), keyValue, &_arc4Key, _sbCtx); + _CHECKRC( rc, "onInitStream hu_ARC4KeySet()" ); + + // Could also generate a random key rather than specify one + //rc = hu_ARC4KeyGen(sb_Params arc4Params, size_t keyLen, sb_Key *arc4Key, sb_GlobalCtx sbCtx) +} + +void ApplicationUI::onDoStream() { + emit message("onDoStream"); + + QDir dir; + QFile plaintextFile("app/native/assets/data/sample_clear_text"); + QFile ciphetextFile("shared/misc/sample_cipher_text"); + + int rc = hu_ARC4Begin(_arc4Params, _arc4Key, &_arc4Context, _sbCtx); + _CHECKRC( rc, "onDoStream() hu_ARC4Begin()" ); + + // Read plain text file and stream cipher it to a ciphertext file + + if (plaintextFile.open(QIODevice::ReadOnly)) { + if (ciphetextFile.open(QIODevice::WriteOnly)) { + QDataStream plainTextStream ( &plaintextFile ); + QDataStream cipherTextStream ( &ciphetextFile ); + char buffer[20]; + unsigned char ciphertext[20]; + int len; + do { + len = plainTextStream.readRawData(buffer, sizeof(ciphertext)); + + rc = hu_ARC4Encrypt(_arc4Context, len, (unsigned char *)buffer, (unsigned char *)ciphertext, _sbCtx); + _CHECKRC( rc, "onDoStream hu_ARC4Encrypt()" ); + + cipherTextStream.writeRawData((const char *)ciphertext, len); + + } while (len > 0); + } else { + qDebug() << "Error opening cipher text file" << endl; + } + ciphetextFile.close(); + } else { + qDebug() << "Error opening plain text file" << endl; + } + plaintextFile.close(); + + rc = hu_ARC4End(&_arc4Context, _sbCtx); + _CHECKRC( rc, "onDoStream() hu_ARC4End()" ); + + // Read cipher text file and stream decrypt it and display on log + + rc = hu_ARC4Begin(_arc4Params, _arc4Key, &_arc4Context, _sbCtx); + _CHECKRC( rc, "onDoStream() hu_ARC4Begin()" ); + + if (ciphetextFile.open(QIODevice::ReadOnly)) { + QDataStream dataStream ( &ciphetextFile ); + char buffer[20]; + char plaintext[20]; + int len; + do { + len = dataStream.readRawData(buffer, sizeof(buffer)); + + rc = hu_ARC4Decrypt(_arc4Context, len, (const unsigned char *)buffer, (unsigned char *)plaintext, _sbCtx); + _CHECKRC( rc, "onDoStream hu_ARC4Decrypt()" ); + + qDebug() << QByteArray(plaintext, len) << endl; + + } while (len > 0); + } else { + qDebug() << "Error opening cipher text file" << endl; + } + + ciphetextFile.close(); + + rc = hu_ARC4End(&_arc4Context, _sbCtx); + _CHECKRC( rc, "onDoStream() hu_ARC4End()" ); +} + +void ApplicationUI::onEndStream() { + emit message("onEndStream"); + + int rc = hu_ARC4KeyDestroy(_arc4Params, &_arc4Key, _sbCtx); + _CHECKRC( rc, "onEndStream() hu_ARC4KeyDestroy()" ); + + _arc4Key = NULL; + + rc = hu_RSAParamsDestroy( &_arc4Params, _sbCtx ); + _CHECKRC( rc, "onEndStream() hu_RSAParamsDestroy()" ); + + _arc4Params = NULL; + + rc = hu_RngDestroy(&_rngCtx, _sbCtx); + _CHECKRC( rc, "onEndStream() hu_RngDestroy()" ); + + _rngCtx = NULL; + + rc = hu_GlobalCtxDestroy(&_sbCtx); + _CHECKRC( rc, "onEndStream() hu_GlobalCtxDestroy()" ); + + _sbCtx = NULL; +} + void ApplicationUI::onSystemLanguageChanged() { QCoreApplication::instance()->removeTranslator(_translator); diff --git a/cryptosample/src/applicationui.hpp b/cryptosample/src/applicationui.hpp index 863a16b..a7ebd6a 100644 --- a/cryptosample/src/applicationui.hpp +++ b/cryptosample/src/applicationui.hpp @@ -46,6 +46,7 @@ #include #include #include +#include #include @@ -123,6 +124,10 @@ public slots: void onEndKdf(); void onDoKdf(); + void onInitStream(); + void onEndStream(); + void onDoStream(); + signals: void message(const QVariant &text); @@ -144,6 +149,10 @@ public slots: KeySlot _keySlot; size_t _keyBitLength; size_t _keyPubExp; + + sb_Params _arc4Params; + sb_Key _arc4Key; + sb_Context _arc4Context; }; #endif /* ApplicationUI_HPP_ */ diff --git a/cryptosample/translations/cryptosample.ts b/cryptosample/translations/cryptosample.ts index 493806c..bcbbec9 100644 --- a/cryptosample/translations/cryptosample.ts +++ b/cryptosample/translations/cryptosample.ts @@ -32,13 +32,13 @@ main - + - + Simple Crypto Stuff