Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #231 from jcmurray/master
Browse files Browse the repository at this point in the history
Add example of ARC4 Stream Cipher
  • Loading branch information
John Murray committed Aug 11, 2015
2 parents fa30d80 + d1bbc85 commit 502b8f7
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 8 deletions.
2 changes: 1 addition & 1 deletion 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
11 changes: 11 additions & 0 deletions 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?
57 changes: 53 additions & 4 deletions cryptosample/assets/main.qml
Expand Up @@ -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 ==============
Expand All @@ -69,6 +70,10 @@ Page {
signal endKdf()
signal doKdf()

signal initStream()
signal endStream()
signal doStream()

// ======== SLOT()s ================

function onMessage(text) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions cryptosample/bar-descriptor.xml
Expand Up @@ -94,6 +94,7 @@

<!-- Request permission to execute native code. Required for native applications. -->
<permission system="true">run_native</permission>
<permission>access_shared</permission>
<env var="LD_LIBRARY_PATH" value="app/native/lib:/usr/lib/qt4/lib"/>

</qnx>
5 changes: 4 additions & 1 deletion cryptosample/config.pri
Expand Up @@ -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) \
Expand Down
144 changes: 144 additions & 0 deletions cryptosample/src/applicationui.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions cryptosample/src/applicationui.hpp
Expand Up @@ -46,6 +46,7 @@
#include <tpkpukie.h>
#include <tpp8d.h>
#include <hukdf.h>
#include <huarc4.h>

#include <smartcard/sc_data_types.h>

Expand Down Expand Up @@ -123,6 +124,10 @@ public slots:
void onEndKdf();
void onDoKdf();

void onInitStream();
void onEndStream();
void onDoStream();

signals:
void message(const QVariant &text);

Expand All @@ -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_ */
4 changes: 2 additions & 2 deletions cryptosample/translations/cryptosample.ts
Expand Up @@ -32,13 +32,13 @@
<context>
<name>main</name>
<message>
<location filename="../assets/main.qml" line="81"/>
<location filename="../assets/main.qml" line="86"/>
<source>
</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../assets/main.qml" line="92"/>
<location filename="../assets/main.qml" line="97"/>
<source>Simple Crypto Stuff</source>
<translation type="unfinished"></translation>
</message>
Expand Down

0 comments on commit 502b8f7

Please sign in to comment.