summaryrefslogtreecommitdiffabout
path: root/pwmanager
authorulf69 <ulf69>2004-10-15 22:47:12 (UTC)
committer ulf69 <ulf69>2004-10-15 22:47:12 (UTC)
commit1821dcc08c8f6f2bc0fdb991c335ed03262d1443 (patch) (unidiff)
tree93249cb2cc3fbb57cc8cf6688afbc252629b62a4 /pwmanager
parent71ee1be533cdbe7ce50c4f617254011fc640e88e (diff)
downloadkdepimpi-1821dcc08c8f6f2bc0fdb991c335ed03262d1443.zip
kdepimpi-1821dcc08c8f6f2bc0fdb991c335ed03262d1443.tar.gz
kdepimpi-1821dcc08c8f6f2bc0fdb991c335ed03262d1443.tar.bz2
*** empty log message ***
Diffstat (limited to 'pwmanager') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/libgcryptif.cpp420
-rw-r--r--pwmanager/pwmanager/libgcryptif.h5
-rw-r--r--pwmanager/pwmanager/pwmanagerE.pro10
-rw-r--r--pwmanager/pwmanager/pwmdoc.cpp77
4 files changed, 446 insertions, 66 deletions
diff --git a/pwmanager/pwmanager/libgcryptif.cpp b/pwmanager/pwmanager/libgcryptif.cpp
index 8e55144..6f3a994 100644
--- a/pwmanager/pwmanager/libgcryptif.cpp
+++ b/pwmanager/pwmanager/libgcryptif.cpp
@@ -433,3 +433,423 @@ void LibGCryptIf::unpadData(const unsigned char *buf,
433} 433}
434 434
435#endif // CONFIG_PWMANAGER_GCRY 435#endif // CONFIG_PWMANAGER_GCRY
436
437#ifdef CONFIG_PWMANAGER_CRYPTO
438
439#include "pwmdoc.h"
440#include "randomizer.h"
441
442#include <openssl/crypto.h>
443
444PwMerror LibGCryptIf::encrypt(unsigned char **outBuf,
445 size_t *outBufLen,
446 unsigned char *inBuf,
447 size_t inBufLen,
448 const unsigned char *key,
449 size_t keylen,
450 char _algo)
451{
452 PwMerror ret = e_success;
453 gcry_error_t err;
454 gcry_cipher_hd_t handle;
455 size_t blklen;
456 size_t unpaddedLen = inBufLen;
457 size_t cipherKeylen;
458 unsigned char *hashedKey;
459 unsigned char salt[STRING2KEY_SALTLEN];
460 int algo = mapCipherId(_algo);
461
462 if (!inBufLen || !keylen)
463 return e_invalidArg;
464
465 // test if algo is ready for encryption
466 err = gcry_cipher_algo_info(algo,
467 GCRYCTL_TEST_ALGO,
468 0, 0);
469 if (err != GPG_ERR_NO_ERROR) {
470 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_TEST_ALGO failed: ")
471 + gcry_strerror(err));
472 ret = e_cryptNotImpl;
473 goto out;
474 }
475 // get the algo block length
476 err = gcry_cipher_algo_info(algo,
477 GCRYCTL_GET_BLKLEN,
478 0,
479 &blklen);
480 if (err != GPG_ERR_NO_ERROR) {
481 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_BLKLEN failed: ")
482 + gcry_strerror(err));
483 ret = e_cryptNotImpl;
484 goto out;
485 }
486 /* double check if we have enough space.
487 * We have only 1024 extra bytes for padding and salt.
488 */
489 BUG_ON(blklen > 1024 - STRING2KEY_SALTLEN);
490 // get the algo key length
491 err = gcry_cipher_algo_info(algo,
492 GCRYCTL_GET_KEYLEN,
493 0,
494 &cipherKeylen);
495 if (err != GPG_ERR_NO_ERROR) {
496 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_KEYLEN failed: ")
497 + gcry_strerror(err));
498 ret = e_cryptNotImpl;
499 goto out;
500 }
501 // now open the algo and get a handle
502 err = gcry_cipher_open(&handle,
503 algo,
504 GCRY_CIPHER_MODE_CBC,
505 0);
506 if (err != GPG_ERR_NO_ERROR) {
507 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_open() failed: ")
508 + gcry_strerror(err));
509 ret = e_cryptNotImpl;
510 goto out;
511 }
512 // hash the "key" to a fixed size hash matching "cipherKeylen"
513 hashedKey = new unsigned char[cipherKeylen];
514 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, true);
515 // so now set the hashed key
516 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen);
517 if (err != GPG_ERR_NO_ERROR) {
518 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_setkey() failed: ")
519 + gcry_strerror(err));
520 ret = e_cryptNotImpl;
521 delete [] hashedKey;
522 goto out_close;
523 }
524 delete [] hashedKey;
525 /* allocate a buffer for the encrypted data.
526 * The size of the buffer is the inBuf length, but blklen
527 * aligned and plus the length of the salt, that is appended.
528 */
529 *outBufLen = getBufLen(unpaddedLen, blklen) + STRING2KEY_SALTLEN;
530 *outBuf = new unsigned char[*outBufLen];
531 padData(inBuf, unpaddedLen, blklen);
532 // encrypt the padded data
533 err = gcry_cipher_encrypt(handle,
534 *outBuf,
535 *outBufLen - STRING2KEY_SALTLEN,
536 inBuf,
537 *outBufLen - STRING2KEY_SALTLEN);
538 if (err != GPG_ERR_NO_ERROR) {
539 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ")
540 + gcry_strerror(err));
541 ret = e_cryptNotImpl;
542 goto out_delete;
543 }
544 // append the salt to the encrypted data
545 memcpy(*outBuf + *outBufLen - STRING2KEY_SALTLEN, salt, STRING2KEY_SALTLEN);
546 goto out_close;
547out_delete:
548 delete [] *outBuf;
549out_close:
550 gcry_cipher_close(handle);
551out:
552 return ret;
553}
554
555PwMerror LibGCryptIf::decrypt(unsigned char **outBuf,
556 size_t *outBufLen,
557 const unsigned char *inBuf,
558 size_t inBufLen,
559 const unsigned char *key,
560 size_t keylen,
561 char _algo)
562{
563 PwMerror ret = e_success;
564 gcry_error_t err;
565 gcry_cipher_hd_t handle;
566 size_t cipherKeylen;
567 unsigned char *hashedKey;
568 unsigned char salt[STRING2KEY_SALTLEN];
569 int algo = mapCipherId(_algo);
570
571 if (!inBufLen || !keylen)
572 return e_invalidArg;
573
574 // test if algo is ready for encryption
575 err = gcry_cipher_algo_info(algo,
576 GCRYCTL_TEST_ALGO,
577 0, 0);
578 if (err != GPG_ERR_NO_ERROR) {
579 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_TEST_ALGO failed: ")
580 + gcry_strerror(err));
581 ret = e_cryptNotImpl;
582 goto out;
583 }
584 // get algo key length
585 err = gcry_cipher_algo_info(algo,
586 GCRYCTL_GET_KEYLEN,
587 0,
588 &cipherKeylen);
589 if (err != GPG_ERR_NO_ERROR) {
590 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_GET_KEYLEN failed: ")
591 + gcry_strerror(err));
592 ret = e_cryptNotImpl;
593 goto out;
594 }
595 // extract the salt of the encrypted data buffer
596 memcpy(salt, inBuf + inBufLen - STRING2KEY_SALTLEN, STRING2KEY_SALTLEN);
597 // open the algo and get a handle
598 err = gcry_cipher_open(&handle,
599 algo,
600 GCRY_CIPHER_MODE_CBC,
601 0);
602 if (err != GPG_ERR_NO_ERROR) {
603 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_open() failed: ")
604 + gcry_strerror(err));
605 ret = e_cryptNotImpl;
606 goto out;
607 }
608 // hash the "key" to a fixed size hash matching "cipherKeylen"
609 hashedKey = new unsigned char[cipherKeylen];
610 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, false);
611 // so now set the hashed key
612 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen);
613 if (err != GPG_ERR_NO_ERROR) {
614 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_setkey() failed: ")
615 + gcry_strerror(err));
616 ret = e_cryptNotImpl;
617 delete [] hashedKey;
618 goto out_close;
619 }
620 delete [] hashedKey;
621 *outBufLen = inBufLen - STRING2KEY_SALTLEN;
622 *outBuf = new unsigned char[*outBufLen];
623 // decrypt the data
624 err = gcry_cipher_decrypt(handle,
625 *outBuf,
626 *outBufLen,
627 inBuf,
628 *outBufLen);
629 if (err != GPG_ERR_NO_ERROR) {
630 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ")
631 + gcry_strerror(err));
632 ret = e_cryptNotImpl;
633 goto out_delete;
634 }
635 // remove all random padding
636 unpadData(*outBuf, outBufLen);
637 goto out_close;
638out_delete:
639 delete [] *outBuf;
640out_close:
641 gcry_cipher_close(handle);
642out:
643 return ret;
644}
645
646PwMerror LibGCryptIf::hash(unsigned char **outBuf,
647 size_t *outBufLen,
648 const unsigned char *inBuf,
649 size_t inBufLen,
650 char _algo)
651{
652 PwMerror ret = e_success;
653 unsigned int hashLen;
654 int algo = mapHashId(_algo);
655
656 hashLen = gcry_md_get_algo_dlen(algo);
657 *outBufLen = hashLen;
658 *outBuf = new unsigned char[*outBufLen];
659 gcry_md_hash_buffer(algo,
660 *outBuf,
661 inBuf,
662 inBufLen);
663 return ret;
664}
665
666unsigned int LibGCryptIf::hashLength(char _algo)
667{
668 unsigned int ret;
669 int algo = mapHashId(_algo);
670 ret = gcry_md_get_algo_dlen(algo);
671 return ret;
672}
673
674int LibGCryptIf::mapCipherId(char algo)
675{
676 switch (algo) {
677 case PWM_CRYPT_AES128:
678 return GCRY_CIPHER_AES;
679 case PWM_CRYPT_AES192:
680 return GCRY_CIPHER_AES192;
681 case PWM_CRYPT_AES256:
682 return GCRY_CIPHER_AES256;
683 case PWM_CRYPT_3DES:
684 return GCRY_CIPHER_3DES;
685 case PWM_CRYPT_TWOFISH:
686 return GCRY_CIPHER_TWOFISH;
687 case PWM_CRYPT_TWOFISH128:
688 return GCRY_CIPHER_TWOFISH128;
689 default:
690 BUG();
691 }
692 return GCRY_CIPHER_NONE;
693}
694
695int LibGCryptIf::mapHashId(char algo)
696{
697 switch (algo) {
698 case PWM_HASH_SHA1:
699 return GCRY_MD_SHA1;
700 case PWM_HASH_SHA256:
701 return GCRY_MD_SHA256;
702 case PWM_HASH_SHA384:
703 return GCRY_MD_SHA384;
704 case PWM_HASH_SHA512:
705 return GCRY_MD_SHA512;
706 case PWM_HASH_MD5:
707 return GCRY_MD_MD5;
708 case PWM_HASH_RMD160:
709 return GCRY_MD_RMD160;
710 case PWM_HASH_TIGER:
711 return GCRY_MD_TIGER;
712 default:
713 BUG();
714 }
715 return GCRY_MD_NONE;
716}
717
718bool LibGCryptIf::hashPassphrase(const unsigned char *pw,
719 size_t pwlen,
720 unsigned char *salt,
721 unsigned char *key,
722 size_t keylen,
723 bool create)
724{
725 DEK dek;
726 STRING2KEY s2k;
727 bool ret;
728
729 dek.keylen = keylen;
730 s2k.mode = 1;
731 s2k.hash_algo = mapHashId(conf()->confGlobHashAlgo());
732 s2k.count = 0;
733 if (!create)
734 memcpy(s2k.salt, salt, STRING2KEY_SALTLEN);
735 ret = doHashPassphrase(&dek,
736 pw,
737 pwlen,
738 &s2k,
739 create);
740 if (!ret)
741 goto out;
742 memcpy(key, dek.key, dek.keylen);
743 if (create)
744 memcpy(salt, s2k.salt, STRING2KEY_SALTLEN);
745out:
746 return ret;
747}
748
749
750bool LibGCryptIf::doHashPassphrase(DEK *dek,
751 const unsigned char *pw,
752 size_t pwlen,
753 STRING2KEY *s2k,
754 bool create)
755{
756 // This function is derived from GnuPG-1.2.5-rc2
757 gcry_md_hd_t md;
758 gcry_error_t err;
759 bool ret = true;
760 size_t pass, i;
761 size_t used = 0;
762
763 PWM_ASSERT(s2k->hash_algo);
764 BUG_ON(!(dek->keylen > 0 && dek->keylen <= array_size(dek->key)));
765
766 err = gcry_md_open(&md, s2k->hash_algo, 0);
767 if (err != GPG_ERR_NO_ERROR) {
768 ret = false;
769 goto out;
770 }
771 for (pass = 0; used < dek->keylen; pass++) {
772 if (pass) {
773 gcry_md_reset(md);
774 for (i = 0; i < pass; i++) // preset the hash context
775 gcry_md_putc(md, 0);
776 }
777 if (s2k->mode == 1 || s2k->mode == 3) {
778 size_t len2 = pwlen + 8;
779 size_t count = len2;
780
781 if (create && !pass) {
782 Randomizer *rnd = Randomizer::obj();
783 const unsigned int salt_len = 8;
784 string rndBuf(rnd->genRndBuf(salt_len));
785 memcpy(s2k->salt, rndBuf.c_str(), salt_len);
786 if (s2k->mode == 3)
787 s2k->count = 96; // 65536 iterations
788 }
789 if (s2k->mode == 3) {
790 count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6);
791 if (count < len2)
792 count = len2;
793 }
794 // a little bit complicated because we need a ulong for count
795 while (count > len2) { // maybe iterated+salted
796 gcry_md_write(md, s2k->salt, 8);
797 gcry_md_write(md, pw, pwlen);
798 count -= len2;
799 }
800 if (count < 8) {
801 gcry_md_write(md, s2k->salt, count);
802 } else {
803 gcry_md_write(md, s2k->salt, 8);
804 count -= 8;
805 gcry_md_write(md, pw, count);
806 }
807 } else
808 gcry_md_write(md, pw, pwlen);
809 gcry_md_final(md);
810 i = gcry_md_get_algo_dlen(s2k->hash_algo);
811 if (i > dek->keylen - used)
812 i = dek->keylen - used;
813 memcpy(dek->key+used, gcry_md_read(md, s2k->hash_algo), i);
814 used += i;
815 }
816 gcry_md_close(md);
817out:
818 return ret;
819}
820
821void LibGCryptIf::padData(unsigned char *buf,
822 size_t bufLen,
823 size_t boundary)
824{
825 size_t numPadBytes = boundary - ((bufLen + 1) % boundary);
826 buf[bufLen] = static_cast<char>(0x01);
827 size_t i = 0;
828 Randomizer *rnd = Randomizer::obj();
829 char c;
830 unsigned char *b;
831 while (i < numPadBytes) {
832 c = rnd->genRndChar();
833 if (c == static_cast<char>(0x01))
834 continue;
835 b = buf + bufLen + 1 + i;
836 *b = c;
837 ++i;
838 }
839}
840
841void LibGCryptIf::unpadData(const unsigned char *buf,
842 size_t *bufLen)
843{
844 size_t pos;
845 BUG_ON(*bufLen % 8);
846 pos = *bufLen - 1;
847 while (buf[pos] != static_cast<char>(0x01)) {
848 BUG_ON(!pos);
849 --pos;
850 }
851 *bufLen = pos;
852}
853
854#endif // CONFIG_PWMANAGER_CRYPTO
855
diff --git a/pwmanager/pwmanager/libgcryptif.h b/pwmanager/pwmanager/libgcryptif.h
index e86d638..7390827 100644
--- a/pwmanager/pwmanager/libgcryptif.h
+++ b/pwmanager/pwmanager/libgcryptif.h
@@ -18,8 +18,11 @@
18 18
19#include "pwmexception.h" 19#include "pwmexception.h"
20 20
21//US ENH: should we put this better into globalstuff.h?
22#define CONFIG_PWMANAGER_CRYPTO
23
21//#undef CONFIG_PWMANAGER_GCRY // for debugging only. 24//#undef CONFIG_PWMANAGER_GCRY // for debugging only.
22#ifdef CONFIG_PWMANAGER_GCRY 25#if defined CONFIG_PWMANAGER_GCRY || defined CONFIG_PWMANAGER_CRYPTO
23 26
24#include <stddef.h> 27#include <stddef.h>
25#include <sys/types.h> 28#include <sys/types.h>
diff --git a/pwmanager/pwmanager/pwmanagerE.pro b/pwmanager/pwmanager/pwmanagerE.pro
index 52d7586..294f549 100644
--- a/pwmanager/pwmanager/pwmanagerE.pro
+++ b/pwmanager/pwmanager/pwmanagerE.pro
@@ -10,7 +10,7 @@ DESTDIR=$(QPEDIR)/bin
10INCLUDEPATH += . ../../ ../../qtcompat ../../qtcompat/xml ../../libkdepim ../../microkde ../../microkde/kdecore ../../microkde/kdeui ../../microkde/kutils $(QPEDIR)/include 10INCLUDEPATH += . ../../ ../../qtcompat ../../qtcompat/xml ../../libkdepim ../../microkde ../../microkde/kdecore ../../microkde/kdeui ../../microkde/kutils $(QPEDIR)/include
11DEFINES += PWM_EMBEDDED 11DEFINES += PWM_EMBEDDED
12#enable this setting if you want debugoutput for pwmanager 12#enable this setting if you want debugoutput for pwmanager
13#DEFINES += CONFIG_DEBUG 13DEFINES += CONFIG_DEBUG
14 14
15LIBS += -lmicrokde 15LIBS += -lmicrokde
16LIBS += -lmicroqtcompat 16LIBS += -lmicroqtcompat
@@ -18,7 +18,7 @@ LIBS += -lmicrokdepim
18LIBS += -L$(QPEDIR)/lib 18LIBS += -L$(QPEDIR)/lib
19LIBS += -lqpe 19LIBS += -lqpe
20LIBS += -lz 20LIBS += -lz
21LIBS += -lbz2 21#LIBS += -lbz2
22LIBS += -lcrypto 22LIBS += -lcrypto
23LIBS += $(QTOPIALIB) 23LIBS += $(QTOPIALIB)
24 24
@@ -44,6 +44,7 @@ LIBS += $(QTOPIALIB)
44#selftest.h 44#selftest.h
45#subtbledit.h \ 45#subtbledit.h \
46#subtbleditimpl.h \ 46#subtbleditimpl.h \
47#compressbzip2.h \
47 48
48HEADERS = \ 49HEADERS = \
49addentrywnd_emb.h \ 50addentrywnd_emb.h \
@@ -53,7 +54,6 @@ binentrygen.h \
53blowfish.h \ 54blowfish.h \
54commentbox.h \ 55commentbox.h \
55compiler.h \ 56compiler.h \
56compressbzip2.h \
57compressgzip.h \ 57compressgzip.h \
58findwnd_emb.h \ 58findwnd_emb.h \
59findwndimpl.h \ 59findwndimpl.h \
@@ -66,6 +66,7 @@ gpasmanfile.h \
66htmlgen.h \ 66htmlgen.h \
67htmlparse.h \ 67htmlparse.h \
68ipc.h \ 68ipc.h \
69libgcryptif.h \
69listobjselectwnd.h \ 70listobjselectwnd.h \
70listviewpwm.h \ 71listviewpwm.h \
71printtext.h \ 72printtext.h \
@@ -108,6 +109,7 @@ kcmconfigs/pwmconfigwidget.h \
108#spinforsignal.cpp 109#spinforsignal.cpp
109#subtbledit.cpp \ 110#subtbledit.cpp \
110#subtbleditimpl.cpp \ 111#subtbleditimpl.cpp \
112#compressbzip2.cpp \
111 113
112SOURCES = \ 114SOURCES = \
113addentrywnd_emb.cpp \ 115addentrywnd_emb.cpp \
@@ -116,7 +118,6 @@ base64.cpp \
116binentrygen.cpp \ 118binentrygen.cpp \
117blowfish.cpp \ 119blowfish.cpp \
118commentbox.cpp \ 120commentbox.cpp \
119compressbzip2.cpp \
120compressgzip.cpp \ 121compressgzip.cpp \
121findwnd_emb.cpp \ 122findwnd_emb.cpp \
122findwndimpl.cpp \ 123findwndimpl.cpp \
@@ -128,6 +129,7 @@ globalstuff.cpp \
128gpasmanfile.cpp \ 129gpasmanfile.cpp \
129htmlgen.cpp \ 130htmlgen.cpp \
130ipc.cpp \ 131ipc.cpp \
132libgcryptif.cpp \
131listobjselectwnd.cpp \ 133listobjselectwnd.cpp \
132listviewpwm.cpp \ 134listviewpwm.cpp \
133main.cpp \ 135main.cpp \
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp
index 4ad392e..a5df8f0 100644
--- a/pwmanager/pwmanager/pwmdoc.cpp
+++ b/pwmanager/pwmanager/pwmdoc.cpp
@@ -28,9 +28,8 @@
28#include "compressbzip2.h" 28#include "compressbzip2.h"
29#include "randomizer.h" 29#include "randomizer.h"
30#include "pwminit.h" 30#include "pwminit.h"
31#ifndef PWM_EMBEDDED 31#include "libgcryptif.h"
32//US #include "libgryptif.h" 32#ifdef PWM_EMBEDDED
33#else
34#include "pwmprefs.h" 33#include "pwmprefs.h"
35#include "kglobal.h" 34#include "kglobal.h"
36#endif 35#endif
@@ -59,6 +58,18 @@
59#include <unistd.h> 58#include <unistd.h>
60#include <stdint.h> 59#include <stdint.h>
61 60
61
62#ifdef PWM_EMBEDDED
63#ifndef Q_LONG
64#define Q_LONG long
65#endif
66
67#ifndef Q_ULONG
68#define Q_ULONG unsigned long
69#endif
70#endif //PWM_EMBEDDED
71
72
62//TODO: reset to its normal value. 73//TODO: reset to its normal value.
63 #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */ 74 #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */
64 75
@@ -352,13 +363,9 @@ PwMerror PwMDoc::saveDoc(char compress, const QString *file)
352 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); 363 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD);
353 } 364 }
354 } 365 }
355#ifndef PWM_EMBEDDED 366
356 int _cryptAlgo = conf()->confGlobCryptAlgo(); 367 int _cryptAlgo = conf()->confGlobCryptAlgo();
357 int _hashAlgo = conf()->confGlobHashAlgo(); 368 int _hashAlgo = conf()->confGlobHashAlgo();
358#else
359 int _cryptAlgo = PWM_CRYPT_BLOWFISH;
360 int _hashAlgo = PWM_HASH_SHA1;
361#endif
362 369
363 // sanity check for the selected algorithms 370 // sanity check for the selected algorithms
364 if (_cryptAlgo < PWM_CRYPT_BLOWFISH || 371 if (_cryptAlgo < PWM_CRYPT_BLOWFISH ||
@@ -599,7 +606,6 @@ PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char c
599 PWM_ASSERT(pw); 606 PWM_ASSERT(pw);
600 PWM_ASSERT(f); 607 PWM_ASSERT(f);
601 PWM_ASSERT(listView); 608 PWM_ASSERT(listView);
602#ifndef PWM_EMBEDDED
603 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) != 609 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) !=
604 static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) { 610 static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) {
605 return e_writeFile; 611 return e_writeFile;
@@ -614,21 +620,6 @@ PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char c
614 return e_writeFile; 620 return e_writeFile;
615 } 621 }
616 622
617#else
618 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) !=
619 (long)(strlen(FILE_ID_HEADER))) {
620 return e_writeFile;
621 }
622 if (f->putch(PWM_FILE_VER) == -1 ||
623 f->putch(keyHash) == -1 ||
624 f->putch(dataHash) == -1 ||
625 f->putch(crypt) == -1 ||
626 f->putch(compress) == -1 ||
627 f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ?
628 ((char)(0x01)) : ((char)(0x00))) == -1) {
629 return e_writeFile;
630 }
631#endif
632 // write bytes of NUL-data. These bytes are reserved for future-use. 623 // write bytes of NUL-data. These bytes are reserved for future-use.
633 const int bufSize = 64; 624 const int bufSize = 64;
634 char tmp_buf[bufSize]; 625 char tmp_buf[bufSize];
@@ -646,7 +637,6 @@ PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char c
646 return e_writeFile; 637 return e_writeFile;
647 break; 638 break;
648 } 639 }
649#ifndef PWM_EMBEDDED
650 case PWM_HASH_SHA256: 640 case PWM_HASH_SHA256:
651 /*... fall through */ 641 /*... fall through */
652 case PWM_HASH_SHA384: 642 case PWM_HASH_SHA384:
@@ -676,7 +666,6 @@ PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char c
676 delete [] buf; 666 delete [] buf;
677 break; 667 break;
678 } 668 }
679#endif
680 default: { 669 default: {
681 return e_hashNotImpl; 670 return e_hashNotImpl;
682 } } 671 } }
@@ -762,7 +751,6 @@ PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
762 return e_wrongPw;// hash doesn't match (wrong key) 751 return e_wrongPw;// hash doesn't match (wrong key)
763 break; 752 break;
764 } 753 }
765#ifndef PWM_EMBEDDED
766 case PWM_HASH_SHA256: 754 case PWM_HASH_SHA256:
767 /*... fall through */ 755 /*... fall through */
768 case PWM_HASH_SHA384: 756 case PWM_HASH_SHA384:
@@ -795,7 +783,6 @@ PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
795 return e_wrongPw;// hash doesn't match (wrong key) 783 return e_wrongPw;// hash doesn't match (wrong key)
796 break; 784 break;
797 } 785 }
798#endif
799 default: { 786 default: {
800 return e_hashNotImpl; 787 return e_hashNotImpl;
801 } } 788 } }
@@ -805,7 +792,6 @@ PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
805 case PWM_HASH_SHA1: 792 case PWM_HASH_SHA1:
806 hashLen = SHA1_HASH_LEN_BYTE; 793 hashLen = SHA1_HASH_LEN_BYTE;
807 break; 794 break;
808#ifndef PWM_EMBEDDED
809 case PWM_HASH_SHA256: 795 case PWM_HASH_SHA256:
810 /*... fall through */ 796 /*... fall through */
811 case PWM_HASH_SHA384: 797 case PWM_HASH_SHA384:
@@ -821,7 +807,6 @@ PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
821 return e_hashNotImpl; 807 return e_hashNotImpl;
822 break; 808 break;
823 } 809 }
824#endif
825 default: 810 default:
826 return e_hashNotImpl; 811 return e_hashNotImpl;
827 } 812 }
@@ -865,7 +850,6 @@ PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f)
865 return e_writeFile; 850 return e_writeFile;
866 break; 851 break;
867 } 852 }
868 #ifndef PWM_EMBEDDED
869 case PWM_HASH_SHA256: 853 case PWM_HASH_SHA256:
870 /*... fall through */ 854 /*... fall through */
871 case PWM_HASH_SHA384: 855 case PWM_HASH_SHA384:
@@ -894,7 +878,6 @@ PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f)
894 delete [] buf; 878 delete [] buf;
895 break; 879 break;
896 } 880 }
897#endif
898 default: { 881 default: {
899 return e_hashNotImpl; 882 return e_hashNotImpl;
900 } } 883 } }
@@ -935,31 +918,18 @@ bool PwMDoc::copyFile(const QString &src, const QString &dst)
935 } 918 }
936 const int tmpBuf_size = 512; 919 const int tmpBuf_size = 512;
937 char tmpBuf[tmpBuf_size]; 920 char tmpBuf[tmpBuf_size];
938#ifndef PWM_EMBEDDED
939 Q_LONG bytesRead, bytesWritten; 921 Q_LONG bytesRead, bytesWritten;
940#else 922
941 long bytesRead, bytesWritten;
942#endif
943 while (!srcFd.atEnd()) { 923 while (!srcFd.atEnd()) {
944#ifndef PWM_EMBEDDED
945 bytesRead = srcFd.readBlock(tmpBuf, 924 bytesRead = srcFd.readBlock(tmpBuf,
946 static_cast<Q_ULONG>(tmpBuf_size)); 925 static_cast<Q_ULONG>(tmpBuf_size));
947#else
948 bytesRead = srcFd.readBlock(tmpBuf,
949 (unsigned long)(tmpBuf_size));
950#endif
951 if (bytesRead == -1) { 926 if (bytesRead == -1) {
952 srcFd.close(); 927 srcFd.close();
953 dstFd.close(); 928 dstFd.close();
954 return false; 929 return false;
955 } 930 }
956#ifndef PWM_EMBEDDED
957 bytesWritten = dstFd.writeBlock(tmpBuf, 931 bytesWritten = dstFd.writeBlock(tmpBuf,
958 static_cast<Q_ULONG>(bytesRead)); 932 static_cast<Q_ULONG>(bytesRead));
959#else
960 bytesWritten = dstFd.writeBlock(tmpBuf,
961 (unsigned long)(bytesRead));
962#endif
963 if (bytesWritten != bytesRead) { 933 if (bytesWritten != bytesRead) {
964 srcFd.close(); 934 srcFd.close();
965 dstFd.close(); 935 dstFd.close();
@@ -1300,7 +1270,6 @@ PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo)
1300 bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize); 1270 bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize);
1301 break; 1271 break;
1302 } 1272 }
1303 #ifndef PWM_EMBEDDED
1304 case PWM_CRYPT_AES128: 1273 case PWM_CRYPT_AES128:
1305 /*... fall through */ 1274 /*... fall through */
1306 case PWM_CRYPT_AES192: 1275 case PWM_CRYPT_AES192:
@@ -1326,28 +1295,18 @@ PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo)
1326 return e_cryptNotImpl; 1295 return e_cryptNotImpl;
1327 break; 1296 break;
1328 } 1297 }
1329#endif
1330 default: { 1298 default: {
1331 delete_ifnot_null_array(encrypted); 1299 delete_ifnot_null_array(encrypted);
1332 return e_cryptNotImpl; 1300 return e_cryptNotImpl;
1333 } } 1301 } }
1334 1302
1335 // write encrypted data to file 1303 // write encrypted data to file
1336#ifndef PWM_EMBEDDED
1337 if (f->writeBlock(reinterpret_cast<const char *>(encrypted), 1304 if (f->writeBlock(reinterpret_cast<const char *>(encrypted),
1338 static_cast<Q_ULONG>(encSize)) 1305 static_cast<Q_ULONG>(encSize))
1339 != static_cast<Q_LONG>(encSize)) { 1306 != static_cast<Q_LONG>(encSize)) {
1340 delete_ifnot_null_array(encrypted); 1307 delete_ifnot_null_array(encrypted);
1341 return e_writeFile; 1308 return e_writeFile;
1342 } 1309 }
1343#else
1344 if (f->writeBlock((const char *)(encrypted),
1345 (unsigned long)(encSize))
1346 != (long)(encSize)) {
1347 delete_ifnot_null_array(encrypted);
1348 return e_writeFile;
1349 }
1350#endif
1351 delete_ifnot_null_array(encrypted); 1310 delete_ifnot_null_array(encrypted);
1352 return e_success; 1311 return e_success;
1353} 1312}
@@ -1388,7 +1347,6 @@ PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw,
1388 bf.bf_decrypt(decrypted, encrypted, cryptLen); 1347 bf.bf_decrypt(decrypted, encrypted, cryptLen);
1389 break; 1348 break;
1390 } 1349 }
1391#ifndef PWM_EMBEDDED
1392 case PWM_CRYPT_AES128: 1350 case PWM_CRYPT_AES128:
1393 /*... fall through */ 1351 /*... fall through */
1394 case PWM_CRYPT_AES192: 1352 case PWM_CRYPT_AES192:
@@ -1414,7 +1372,6 @@ PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw,
1414 } 1372 }
1415 break; 1373 break;
1416 } 1374 }
1417#endif
1418 default: { 1375 default: {
1419 delete [] encrypted; 1376 delete [] encrypted;
1420 delete [] decrypted; 1377 delete [] decrypted;
@@ -1452,7 +1409,6 @@ PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash,
1452 return e_fileCorrupt; 1409 return e_fileCorrupt;
1453 break; 1410 break;
1454 } 1411 }
1455#ifndef PWM_EMBEDDED
1456 case PWM_HASH_SHA256: 1412 case PWM_HASH_SHA256:
1457 /*... fall through */ 1413 /*... fall through */
1458 case PWM_HASH_SHA384: 1414 case PWM_HASH_SHA384:
@@ -1480,7 +1436,6 @@ PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash,
1480 return e_fileCorrupt; 1436 return e_fileCorrupt;
1481 break; 1437 break;
1482 } 1438 }
1483#endif
1484 default: 1439 default:
1485 return e_hashNotImpl; 1440 return e_hashNotImpl;
1486 } 1441 }