This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 281
/
applicationui.cpp
646 lines (478 loc) · 23.1 KB
/
applicationui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
* Copyright (c) 2011-2015 BlackBerry Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "applicationui.hpp"
ApplicationUI::ApplicationUI()
: QObject()
, _sbCtx(NULL)
, _rngCtx(NULL)
, _sha256Context(NULL)
, _hmacContext(NULL)
, _sha1Context(NULL)
, _expireTimer(new QTimer(this))
, _keyBitLength(2048)
, _keyPubExp(3)
, _arc4Params(NULL)
, _arc4Key(NULL)
, _arc4Context(NULL)
{
_translator = new QTranslator(this);
_localeHandler = new LocaleHandler(this);
bool res = QObject::connect(_localeHandler, SIGNAL(systemLanguageChanged()),
this, SLOT(onSystemLanguageChanged()));
Q_ASSERT(res);
Q_UNUSED(res);
onSystemLanguageChanged();
_qml = QmlDocument::create("asset:///main.qml").parent(this);
_root = _qml->createRootObject<AbstractPane>();
_mainPage = _root->findChild<QObject*>("mainPage");
// ============== Message to be sent to page
QObject::connect( this, SIGNAL(message(QVariant)),
_mainPage, SLOT(onMessage(QVariant)));
// ============== Hook up buttons
QObject::connect( _mainPage, SIGNAL(initRandomNumber()),
this, SLOT(onInitRandomNumber()));
QObject::connect( _mainPage, SIGNAL(getRandomNumber()),
this, SLOT(onGetRandomNumber()));
QObject::connect( _mainPage, SIGNAL(endRandomNumber()),
this, SLOT(onEndRandomNumber()));
QObject::connect( _mainPage, SIGNAL(initHash()),
this, SLOT(onInitHash()));
QObject::connect( _mainPage, SIGNAL(endHash()),
this, SLOT(onEndHash()));
QObject::connect( _mainPage, SIGNAL(makeHash(int)),
this, SLOT(onMakeHash(int)));
QObject::connect( _mainPage, SIGNAL(initRsa()),
this, SLOT(onInitRsa()));
QObject::connect( _mainPage, SIGNAL(endRsa()),
this, SLOT(onEndRsa()));
QObject::connect( _mainPage, SIGNAL(doRsa()),
this, SLOT(onDoRsa()));
QObject::connect( _mainPage, SIGNAL(initKdf()),
this, SLOT(onInitKdf()));
QObject::connect( _mainPage, SIGNAL(endKdf()),
this, SLOT(onEndKdf()));
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);
}
void ApplicationUI::onInitRandomNumber()
{
emit message("onInitRandomNumber");
int rc = hu_GlobalCtxCreateDefault(&_sbCtx);
_CHECKRC( rc, "onInitRandomNumber hu_GlobalCtxCreateDefault()" );
rc = hu_RegisterSbg56(_sbCtx);
_CHECKRC( rc, "onInitRandomNumber hu_RegisterSbg56()" );
rc = hu_RegisterSystemSeed(_sbCtx);
_CHECKRC( rc, "onInitRandomNumber hu_RegisterSystemSeed()" );
rc = hu_InitSbg56(_sbCtx);
if (rc != SB_FAIL_LIBRARY_ALREADY_INIT) {
_CHECKRC( rc, "onInitRandomNumber hu_InitSbg56()" );
}
uchar mySeed[4];
size_t seedLen = 4;
rc = hu_SeedGet(&seedLen, mySeed, _sbCtx);
_CHECKRC( rc, "onInitRandomNumber hu_SeedGet()" );
rc = hu_RngCreate(seedLen, mySeed, NULL, NULL, NULL, &_rngCtx, _sbCtx);
_CHECKRC( rc, "onInitRandomNumber hu_RngCreate()" );
}
void ApplicationUI::onGetRandomNumber()
{
emit message("onGetRandomNumber");
int buffer = 0;
int rc = hu_RngGetBytes(_rngCtx, sizeof(buffer), (uchar *)&buffer, _sbCtx);
_CHECKRC( rc, "onGetRandomNumber hu_RngGetBytes()" );
emit message("Random number=" + QString::number(buffer));
}
void ApplicationUI::onEndRandomNumber()
{
emit message("onEndRandomNumber");
int rc = hu_RngDestroy(&_rngCtx, _sbCtx);
_CHECKRC( rc, "onEndRandomNumber hu_RngDestroy()" );
_rngCtx = NULL;
rc = hu_GlobalCtxDestroy(&_sbCtx);
_CHECKRC( rc, "onEndRandomNumber hu_GlobalCtxDestroy()" );
_sbCtx = NULL;
}
void ApplicationUI::onInitHash() {
emit message("onInitHash");
int rc = SB_SUCCESS; /* Return Code */
/* Create SB Contexts */
rc = hu_GlobalCtxCreateDefault(&_sbCtx);
_CHECKRC( rc, "onInitHash hu_GlobalCtxCreateDefault()" );
rc = hu_RegisterSbg56(_sbCtx);
_CHECKRC( rc, "onInitHash hu_RegisterSbg56()" );
rc = hu_InitSbg56(_sbCtx);
if (rc != SB_FAIL_LIBRARY_ALREADY_INIT) {
_CHECKRC( rc, "onInitHash hu_InitSbg56()" );
}
}
void ApplicationUI::onEndHash() {
emit message("onEndHash");
hu_GlobalCtxDestroy(&_sbCtx);
_sbCtx = NULL;
}
void ApplicationUI::onMakeHash(int hashType) {
bool doSha1 = (hashType == 0);
bool doSha256 = (hashType == 1);
bool doHmac = (hashType == 2);
qDebug() << "hashType:" << hashType << endl;
QString input_data("1234567890");
unsigned char messageDigestSha1[SB_SHA1_DIGEST_LEN];
unsigned char messageDigestSha2[SB_SHA256_DIGEST_LEN];
unsigned char messageDigestHMAC[SB_HMAC_SHA256_256_TAG_LEN];
unsigned char hmacKey[SB_HMAC_SHA256_256_TAG_LEN] = {0x1C, 0x85, 0xC9, 0xBB, 0x25, 0xBA, 0xFB, 0xE9,
0xFC, 0xE7, 0xB1, 0x3B, 0x9B, 0x53, 0x6F, 0x40,
0x79, 0x4F, 0x13, 0x3B, 0x60, 0x96, 0xF7, 0x44,
0x0B, 0x62, 0x39, 0xFB, 0xCA, 0x4A, 0xA3, 0xB6
};
/* Initialize variables */
int rc = SB_SUCCESS; /* Return Code */
QByteArray input_bytes = input_data.toUtf8();
unsigned char* hash_input = reinterpret_cast<unsigned char*>(input_bytes.data());
if (doHmac) {
emit message("onMakeHash - input data:" + input_data + " (" + QString::number(input_data.length()) + ")");
QString digest;
for (int i = 0; i < SB_HMAC_SHA256_256_TAG_LEN; ++i) {
digest.append(QString("%1").arg(QString::number((uint)hmacKey[i], 16), 2, QChar('0')));
}
emit message("HMAC Key = " + digest);
rc = hu_HMACSHA256Begin(SB_HMAC_SHA256_256_TAG_LEN, hmacKey, NULL, &_hmacContext, _sbCtx);
_CHECKRC( rc, "onMakeHash hu_HMACSHA256Begin()" );
/* Hash Message */
rc = hu_HMACSHA256Hash(_hmacContext, (size_t) input_bytes.length(), hash_input, _sbCtx);
_CHECKRC( rc, "onMakeHash hu_HMACSHA256Hash()" );
/* Complete HMAC-SHA-256 Hashing */
rc = hu_HMACSHA256End(&_hmacContext, SB_HMAC_SHA256_256_TAG_LEN, messageDigestHMAC, _sbCtx);
_CHECKRCEXT( rc,
{
QString digest;
for (int i = 0; i < SB_HMAC_SHA256_256_TAG_LEN; ++i) {
digest.append(QString("%1").arg(QString::number((uint)messageDigestHMAC[i], 16), 2, QChar('0')));
}
emit message("Result HMAC = " + digest);
},
"onMakeHash hu_HMACSHA256End()" );
} else if (doSha1) {
emit message("onMakeHash - input data:" + input_data + " (" + QString::number(input_data.length()) + ")");
rc = hu_SHA1Begin((size_t) SB_SHA1_DIGEST_LEN, NULL, &_sha1Context, _sbCtx);
_CHECKRC( rc, "onMakeHash hu_SHA1Begin()" );
/* Hash Message */
rc = hu_SHA1Hash(_sha1Context, (size_t) input_bytes.length(), hash_input, _sbCtx);
_CHECKRC( rc, "onMakeHash hu_SHA1Hash()" );
/* Complete SHA-1 Hashing */
rc = hu_SHA1End(&_sha1Context, messageDigestSha1, _sbCtx);
_CHECKRCEXT( rc,
{
QString digest;
for (int i = 0; i < SB_SHA1_DIGEST_LEN; ++i) {
digest.append(QString("%1").arg(QString::number((uint)messageDigestSha1[i], 16), 2, QChar('0')));
}
emit message("Result SHA-1 hash = " + digest);
},
"onMakeHash hu_SHA1End()" );
} else if (doSha256) {
emit message("onMakeHash - input data:" + input_data + " (" + QString::number(input_data.length()) + ")");
rc = hu_SHA256Begin((size_t) SB_SHA256_DIGEST_LEN, NULL, &_sha256Context, _sbCtx);
_CHECKRC( rc, "onMakeHash hu_SHA256Begin()" );
/* Hash Message */
rc = hu_SHA256Hash(_sha256Context, (size_t) input_bytes.length(), hash_input, _sbCtx);
_CHECKRC( rc, "onMakeHash hu_SHA256Hash()" );
/* Complete SHA-256 Hashing */
rc = hu_SHA256End(&_sha256Context, messageDigestSha2, _sbCtx);
_CHECKRCEXT( rc,
{
QString digest;
for (int i = 0; i < SB_SHA256_DIGEST_LEN; ++i) {
digest.append(QString("%1").arg(QString::number((uint)messageDigestSha2[i], 16), 2, QChar('0')));
}
emit message("Result SHA-256 hash = " + digest);
},
"onMakeHash hu_SHA256End()" );
}
}
void ApplicationUI::onInitRsa() {
emit message("onInitRsa");
void* subName = 0;
size_t subNameLen = 0;
tp_X509CertEncodeCtx encCtx = 0;
void* gname = NULL;
size_t gnameLen = 0;
tp_X509GeneralNamesEncodeCtx gnameEncCtx = 0;
tp_DateTime startDate;
tp_DateTime endDate;
startDate.year = 2015;
startDate.mon = 3;
startDate.mday = 24;
startDate.hour = 0;
startDate.min = 0;
startDate.sec = 0;
startDate.offHour = 0;
startDate.offMin = 0;
startDate.offDir = 0;
const char *RSA_CERTIFICATE_SUBJECT = "E = me@foobar.com, CN = Mock RSA User, "
"OU = GB, OU = EMEA, DC = foobar, DC = net";
const char * CERTIFICATE_EMAIL = "me@foobar.com";
const unsigned char* EXTENDED_KEY_USAGE_OID[] = { TP_OID_PKIX_KP_CLIENT_AUTH, TP_OID_PKIX_KP_EMAIL_PROTECTION };
int rc = SB_SUCCESS; /* Return Code */
/* Create SB Contexts */
onInitRandomNumber();
_keySlot.cryptoAlgorithm = SC_CRYPTO_ALGORITHM_RSA;
_keySlot.params = 0;
_keySlot.privateKey = 0;
_keySlot.publicKey = 0;
_keySlot.certificate = 0;
_keySlot.certificateLength = 0;
rc = hu_RSAParamsCreate( _keyBitLength , _rngCtx, NULL, &(_keySlot.params), _sbCtx );
_CHECKRC( rc, "onInitRsa hu_RSAParamsCreate()" );
rc = hu_RSAKeyGen( _keySlot.params, _keyPubExp, NULL, &( _keySlot.privateKey ), &( _keySlot.publicKey ), _sbCtx );
_CHECKRCEXT( rc, {
emit message("onInitRsa: key pair generated");
}, "onInitRsa hu_RSAKeyGen()" );
rc = tp_X509NameFromStringAlloc( 0, RSA_CERTIFICATE_SUBJECT, &subName, &subNameLen, _sbCtx );
_CHECKRC( rc, "onInitRsa tp_X509NameFromStringAlloc()" );
rc = tp_X509CertEncodeBegin( TP_SIGN_FLAG_NO_SPECIFIED, subName, subNameLen, CIC_ALLOC_KEEP, NULL,
_keySlot.params, _keySlot.publicKey, NULL, 0, CIC_ALLOC_KEEP,
TP_SIGN_RSA_SHA256_ID, NULL, _keySlot.params, _keySlot.privateKey, NULL, &encCtx, _sbCtx );
_CHECKRC( rc, "onInitRsa tp_X509CertEncodeBegin()" );
endDate = startDate;
endDate.year = endDate.year + 10;
rc = tp_X509CertEncodeSetValidityDates( encCtx, 0, &startDate, &endDate );
_CHECKRC( rc, "onInitRsa tp_X509CertEncodeSetValidityDates()" );
rc = tp_X509CertEncodeSetBasicConstraints( encCtx, 1, 0, 1 );
_CHECKRC( rc, "onInitRsa tp_X509CertEncodeSetBasicConstraints()" );
rc = tp_X509CertEncodeSetKeyUsage( encCtx, 1,
TP_X509_KEY_USAGE_KEY_ENCIPHERMENT | TP_X509_KEY_USAGE_DIGITAL_SIGNATURE
| TP_X509_KEY_USAGE_KEY_CERT_SIGN | TP_X509_KEY_USAGE_DATA_ENCIPHERMENT
| TP_X509_KEY_USAGE_KEY_AGREEMENT | TP_X509_KEY_USAGE_DATA_ENCIPHERMENT );
_CHECKRC( rc, "onInitRsa tp_X509CertEncodeSetKeyUsage()" );
rc = tp_X509CertEncodeSetExtKeyUsage( encCtx, 1, EXTENDED_KEY_USAGE_OID, 2 );
_CHECKRC( rc, "onInitRsa tp_X509CertEncodeSetExtKeyUsage()" );
rc = tp_X509GeneralNamesEncodeBegin( 0, &gnameEncCtx, _sbCtx );
_CHECKRC( rc, "onInitRsa tp_X509GeneralNamesEncodeBegin()" );
rc = tp_X509GeneralNamesEncodeAddRfc822Name( gnameEncCtx, CERTIFICATE_EMAIL, 0 );
_CHECKRC( rc, "onInitRsa tp_X509GeneralNamesEncodeAddRfc822Name()" );
rc = tp_X509GeneralNamesEncodeEndAlloc( &gnameEncCtx, &gname, &gnameLen );
_CHECKRC( rc, "onInitRsa tp_X509GeneralNamesEncodeEndAlloc()" );
rc = tp_X509CertEncodeSetSubjectAltName( encCtx, 0, gname, gnameLen, CIC_ALLOC_KEEP );
_CHECKRC( rc, "onInitRsa tp_X509CertEncodeSetSubjectAltName()" );
rc = tp_X509CertEncodeEndAlloc( &encCtx, &( _keySlot.certificate ), &( _keySlot.certificateLength ) );
_CHECKRCEXT( rc, {
emit message("onInitRsa: Certificate Generated from: " + QString(RSA_CERTIFICATE_SUBJECT));
}, "onInitRsa tp_X509CertEncodeEndAlloc()" );
QString certHexString;
qDebug() << "XXXX RSA Cert length:" << _keySlot.certificateLength << endl;
size_t segmentLen = 40;
size_t numSegments = _keySlot.certificateLength / segmentLen;
size_t lastSegment = _keySlot.certificateLength % segmentLen;
qDebug() << "XXXX RSA Cert:" << endl;
for (size_t j = 0; j < numSegments; ++j) {
certHexString.clear();
for (size_t i = j*segmentLen; i < (j+1)*segmentLen; ++i) {
certHexString.append(QString("%1").arg(QString::number((((uint8_t *)(_keySlot.certificate))[i]), 16), 2, QChar('0')));
}
qDebug() << certHexString << endl;
}
certHexString.clear();
for (size_t i = _keySlot.certificateLength-lastSegment; i < _keySlot.certificateLength; ++i) {
certHexString.append(QString("%1").arg(QString::number((((uint8_t *)(_keySlot.certificate))[i]), 16), 2, QChar('0')));
}
qDebug() << certHexString << endl;
}
void ApplicationUI::onEndRsa() {
emit message("onEndRsa");
sb_free( _keySlot.certificate, _sbCtx );
_keySlot.certificate = 0;
hu_RSAKeyDestroy( _keySlot.params, &( _keySlot.privateKey ), &( _keySlot.publicKey ), _sbCtx );
hu_RSAParamsDestroy( &( _keySlot.params ), _sbCtx );
onEndRandomNumber();
}
void ApplicationUI::onDoRsa() {
emit message("onDoRsa");
char textToEncrypt[] = "This is some plaintext! Hello Plaintext";
unsigned char plaintext[_keyBitLength/8];
unsigned char ciphertext[_keyBitLength/8];
memset(plaintext, 0, sizeof(plaintext));
memset(ciphertext, 0, sizeof(ciphertext));
memcpy (plaintext, textToEncrypt, strlen(textToEncrypt)+1 );
int rc = SB_SUCCESS; /* Return Code */
emit message(QString("onDoRsa plaintext: %1").arg(textToEncrypt));
rc = hu_RSAPublicEncrypt( _keySlot.params, _keySlot.publicKey, plaintext, ciphertext, _sbCtx );
_CHECKRC( rc, "onDoRsa hu_RSAPublicEncrypt()" );
QString cipherTextHex;
for (size_t i = 0; i < sizeof(ciphertext); ++i) {
cipherTextHex.append(QString("%1").arg(QString::number(((uint8_t *)ciphertext)[i], 16), 2, QChar('0')));
}
qDebug() << cipherTextHex << endl;
emit message("Ciphertext: " + cipherTextHex);
// and back again
memset(plaintext, 0, sizeof(plaintext));
rc = hu_RSAPrivateDecrypt( _keySlot.params, _keySlot.privateKey, ciphertext, plaintext, _sbCtx );
_CHECKRC( rc, "onDoRsa hu_RSAPrivateDecrypt()" );
qDebug() << (char *)plaintext << endl;
emit message(QString("onDoRsa plaintext: %1").arg((char *)plaintext));
}
void ApplicationUI::onInitKdf() {
emit message("onInitKdf - Derive a key using SHA1+KDF2");
int rc = SB_SUCCESS; /* Return Code */
/* Create SB Contexts */
rc = hu_GlobalCtxCreateDefault(&_sbCtx);
_CHECKRC( rc, "onInitKdf hu_GlobalCtxCreateDefault()" );
rc = hu_RegisterSbg56(_sbCtx);
_CHECKRC( rc, "onInitKdf hu_RegisterSbg56()" );
rc = hu_InitSbg56(_sbCtx);
if (rc != SB_FAIL_LIBRARY_ALREADY_INIT) {
_CHECKRC( rc, "onInitKdf hu_InitSbg56()" );
}
}
void ApplicationUI::onEndKdf() {
emit message("onEndKdf");
hu_GlobalCtxDestroy(&_sbCtx);
_sbCtx = NULL;
}
void ApplicationUI::onDoKdf() {
unsigned char sha1Kdf2SharedSecret[] = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ',
's', 'h', 'a', 'r', 'e', 'd', ' ', 's', 'e', 'c', 'r', 'e', 't', ' ',
'p', 'a', 's', 's', 'p', 'h', 'r', 'a', 's', 'e', '!', '\0'};
size_t sha1Kdf2SharedSecretLen = sizeof(sha1Kdf2SharedSecret)-1;
unsigned char sha1Kdf2GeneratedKey[2048/8];
emit message(QString("onDoKdf - doSha1Kdf2 shared secret: %1").arg(reinterpret_cast<const char*>(sha1Kdf2SharedSecret)));
QString generatedKeyHex;
int rc = hu_KDFDerive(
HU_KDF_IEEE_KDF2_SHA1,
sha1Kdf2SharedSecretLen, sha1Kdf2SharedSecret,
0, NULL,
sizeof(sha1Kdf2GeneratedKey), sha1Kdf2GeneratedKey, _sbCtx);
_CHECKRCEXT( rc,
{
for (size_t i = 0; i < sizeof(sha1Kdf2GeneratedKey); ++i) {
generatedKeyHex.append(QString("%1").arg(QString::number(((uint8_t *)sha1Kdf2GeneratedKey)[i], 16), 2, QChar('0')));
}
qDebug() << generatedKeyHex << endl;
emit message("Generated SHA1+KDF2 Key: " + generatedKeyHex);
},
"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);
QString locale_string = QLocale().name();
QString file_name = QString("TestRandom_%1").arg(locale_string);
if (_translator->load(file_name, "app/native/qm")) {
QCoreApplication::instance()->installTranslator(_translator);
}
}