Qt Cryptographic Architecture
qca_basic.h
Go to the documentation of this file.
1 /*
2  * qca_basic.h - Qt Cryptographic Architecture
3  * Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com>
4  * Copyright (C) 2004-2007 Brad Hards <bradh@frogmouth.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22 
33 #ifndef QCA_BASIC_H
34 #define QCA_BASIC_H
35 
36 #include "qca_core.h"
37 
38 #include <QIODevice>
39 
40 // Qt5 comes with QStringLiteral for wrapping string literals, which Qt4 does
41 // not have. It is needed if the headers are built with QT_NO_CAST_FROM_ASCII.
42 // Defining it here as QString::fromUtf8 for convenience.
43 #ifndef QStringLiteral
44 #define QStringLiteral(str) QString::fromUtf8(str)
45 #endif
46 
47 namespace QCA {
48 
71 class QCA_EXPORT Random : public Algorithm
72 {
73 public:
80  Random(const QString &provider = QString());
81 
87  Random(const Random &from);
88 
89  ~Random();
90 
96  Random & operator=(const Random &from);
97 
106  uchar nextByte();
107 
118  SecureArray nextBytes(int size);
119 
131  static uchar randomChar();
132 
142  static int randomInt();
143 
154  static SecureArray randomArray(int size);
155 
156 private:
157  class Private;
158  Private *d;
159 };
160 
214 class QCA_EXPORT Hash : public Algorithm, public BufferedComputation
215 {
216 public:
225  explicit Hash(const QString &type, const QString &provider = QString());
226 
232  Hash(const Hash &from);
233 
234  ~Hash();
235 
241  Hash & operator=(const Hash &from);
242 
250  static QStringList supportedTypes(const QString &provider = QString());
251 
255  QString type() const;
256 
267  virtual void clear();
268 
280  virtual void update(const MemoryRegion &a);
281 
287  void update(const QByteArray &a);
288 
303  void update(const char *data, int len = -1);
304 
327  void update(QIODevice *file);
328 
342  virtual MemoryRegion final();
343 
364  MemoryRegion hash(const MemoryRegion &array);
365 
380  QString hashToString(const MemoryRegion &array);
381 
382 private:
383  class Private;
384  Private *d;
385 };
386 
586 class QCA_EXPORT Cipher : public Algorithm, public Filter
587 {
588 public:
596  enum Mode
597  {
598  CBC,
599  CFB,
600  ECB,
601  OFB,
602  CTR
603  };
604 
611  enum Padding
612  {
615  PKCS7
616  };
617 
634  Cipher(const QString &type, Mode mode, Padding pad = DefaultPadding,
635  Direction dir = Encode, const SymmetricKey &key = SymmetricKey(),
637  const QString &provider = QString());
638 
644  Cipher(const Cipher &from);
645 
646  ~Cipher();
647 
653  Cipher & operator=(const Cipher &from);
654 
662  static QStringList supportedTypes(const QString &provider = QString());
663 
667  QString type() const;
668 
672  Mode mode() const;
673 
677  Padding padding() const;
678 
682  Direction direction() const;
683 
687  KeyLength keyLength() const;
688 
695  bool validKeyLength(int n) const;
696 
700  int blockSize() const;
701 
705  virtual void clear();
706 
714  virtual MemoryRegion update(const MemoryRegion &a);
715 
720  virtual MemoryRegion final();
721 
727  virtual bool ok() const;
728 
742  void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv = InitializationVector());
743 
753  static QString withAlgorithms(const QString &cipherType, Mode modeType, Padding paddingType);
754 
755 private:
756  class Private;
757  Private *d;
758 };
759 
780 class QCA_EXPORT MessageAuthenticationCode : public Algorithm, public BufferedComputation
781 {
782 public:
792  MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider = QString());
793 
803 
805 
814  MessageAuthenticationCode & operator=(const MessageAuthenticationCode &from);
815 
824  static QStringList supportedTypes(const QString &provider = QString());
825 
829  QString type() const;
830 
834  KeyLength keyLength() const;
835 
842  bool validKeyLength(int n) const;
843 
856  virtual void clear();
857 
865  virtual void update(const MemoryRegion &array);
866 
878  virtual MemoryRegion final();
879 
885  void setup(const SymmetricKey &key);
886 
887 private:
888  class Private;
889  Private *d;
890 };
891 
906 class QCA_EXPORT KeyDerivationFunction : public Algorithm
907 {
908 public:
915 
917 
926  KeyDerivationFunction & operator=(const KeyDerivationFunction &from);
927 
940  SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, unsigned int iterationCount);
941 
955  SymmetricKey makeKey(const SecureArray &secret,
956  const InitializationVector &salt,
957  unsigned int keyLength,
958  int msecInterval,
959  unsigned int *iterationCount);
960 
973  static QString withAlgorithm(const QString &kdfType, const QString &algType);
974 
975 protected:
982  KeyDerivationFunction(const QString &type, const QString &provider);
983 
984 private:
985  class Private;
986  Private *d;
987 };
988 
999 class QCA_EXPORT PBKDF1 : public KeyDerivationFunction
1000 {
1001 public:
1008  explicit PBKDF1(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1009  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf1"), algorithm), provider) {}
1010 };
1011 
1022 class QCA_EXPORT PBKDF2 : public KeyDerivationFunction
1023 {
1024 public:
1031  explicit PBKDF2(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1032  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf2"), algorithm), provider) {}
1033 };
1034 
1035 }
1036 
1037 #endif
PBKDF2(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1031
General superclass for an algorithm.
Definition: qca_core.h:1121
Padding
Padding variations for cipher algorithms.
Definition: qca_basic.h:611
General class for cipher (encryption / decryption) algorithms.
Definition: qca_basic.h:586
Mode
Mode settings for cipher algorithms.
Definition: qca_basic.h:596
Source of random numbers.
Definition: qca_basic.h:71
General superclass for buffered computation algorithms.
Definition: qca_core.h:1009
operate in Output FeedBack Mode
Definition: qca_basic.h:601
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1221
General class for message authentication code (MAC) algorithms.
Definition: qca_basic.h:780
Simple container for acceptable key lengths.
Definition: qca_core.h:670
PBKDF1(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1008
Header file for core QCA infrastructure.
Default for cipher-mode.
Definition: qca_basic.h:613
operate in Electronic Code Book mode
Definition: qca_basic.h:600
Container for initialisation vectors and nonces.
Definition: qca_core.h:1267
Direction
Direction settings for symmetric algorithms.
Definition: qca_core.h:139
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:47
operate in Cipher Block Chaining mode
Definition: qca_basic.h:598
Do not use padding.
Definition: qca_basic.h:614
General class for hashing algorithms.
Definition: qca_basic.h:214
Secure array of bytes.
Definition: qca_tools.h:316
Password based key derivation function version 1.
Definition: qca_basic.h:999
Operate in the "forward" direction; for example, encrypting.
Definition: qca_core.h:141
General superclass for filtering transformation algorithms.
Definition: qca_core.h:1065
General superclass for key derivation algorithms.
Definition: qca_basic.h:906
operate in Cipher FeedBack mode
Definition: qca_basic.h:599
Password based key derivation function version 2.
Definition: qca_basic.h:1022
Array of bytes that may be optionally secured.
Definition: qca_tools.h:90