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  * Copyright (C) 2013-2016 Ivan Romanov <drizt@land.ru>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
34 #ifndef QCA_BASIC_H
35 #define QCA_BASIC_H
36 
37 #include "qca_core.h"
38 
39 #include <QIODevice>
40 
41 // Qt5 comes with QStringLiteral for wrapping string literals, which Qt4 does
42 // not have. It is needed if the headers are built with QT_NO_CAST_FROM_ASCII.
43 // Defining it here as QString::fromUtf8 for convenience.
44 #ifndef QStringLiteral
45 #define QStringLiteral(str) QString::fromUtf8(str)
46 #endif
47 
48 namespace QCA {
49 
72 class QCA_EXPORT Random : public Algorithm
73 {
74 public:
81  Random(const QString &provider = QString());
82 
88  Random(const Random &from);
89 
90  ~Random();
91 
97  Random & operator=(const Random &from);
98 
107  uchar nextByte();
108 
119  SecureArray nextBytes(int size);
120 
132  static uchar randomChar();
133 
143  static int randomInt();
144 
155  static SecureArray randomArray(int size);
156 
157 private:
158  class Private;
159  Private *d;
160 };
161 
215 class QCA_EXPORT Hash : public Algorithm, public BufferedComputation
216 {
217 public:
226  explicit Hash(const QString &type, const QString &provider = QString());
227 
233  Hash(const Hash &from);
234 
235  ~Hash();
236 
242  Hash & operator=(const Hash &from);
243 
251  static QStringList supportedTypes(const QString &provider = QString());
252 
256  QString type() const;
257 
268  virtual void clear();
269 
281  virtual void update(const MemoryRegion &a);
282 
288  void update(const QByteArray &a);
289 
304  void update(const char *data, int len = -1);
305 
328  void update(QIODevice *file);
329 
343  virtual MemoryRegion final();
344 
365  MemoryRegion hash(const MemoryRegion &array);
366 
381  QString hashToString(const MemoryRegion &array);
382 
383 private:
384  class Private;
385  Private *d;
386 };
387 
588 class QCA_EXPORT Cipher : public Algorithm, public Filter
589 {
590 public:
598  enum Mode
599  {
600  CBC,
601  CFB,
602  ECB,
603  OFB,
604  CTR,
605  GCM,
606  CCM
607  };
608 
615  enum Padding
616  {
619  PKCS7
620  };
621 
638  Cipher(const QString &type, Mode mode, Padding pad = DefaultPadding,
639  Direction dir = Encode, const SymmetricKey &key = SymmetricKey(),
641  const QString &provider = QString());
642 
660  Cipher(const QString &type, Mode mode, Padding pad,
661  Direction dir, const SymmetricKey &key,
662  const InitializationVector &iv, const AuthTag &tag,
663  const QString &provider = QString());
664 
670  Cipher(const Cipher &from);
671 
672  ~Cipher();
673 
679  Cipher & operator=(const Cipher &from);
680 
688  static QStringList supportedTypes(const QString &provider = QString());
689 
693  QString type() const;
694 
698  Mode mode() const;
699 
703  Padding padding() const;
704 
708  Direction direction() const;
709 
713  KeyLength keyLength() const;
714 
721  bool validKeyLength(int n) const;
722 
726  int blockSize() const;
727 
731  AuthTag tag() const;
732 
736  virtual void clear();
737 
745  virtual MemoryRegion update(const MemoryRegion &a);
746 
751  virtual MemoryRegion final();
752 
758  virtual bool ok() const;
759 
773  void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv = InitializationVector());
774 
789  void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv, const AuthTag &tag);
790 
800  static QString withAlgorithms(const QString &cipherType, Mode modeType, Padding paddingType);
801 private:
802  class Private;
803  Private *d;
804 };
805 
826 class QCA_EXPORT MessageAuthenticationCode : public Algorithm, public BufferedComputation
827 {
828 public:
838  MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider = QString());
839 
849 
851 
860  MessageAuthenticationCode & operator=(const MessageAuthenticationCode &from);
861 
870  static QStringList supportedTypes(const QString &provider = QString());
871 
875  QString type() const;
876 
880  KeyLength keyLength() const;
881 
888  bool validKeyLength(int n) const;
889 
902  virtual void clear();
903 
911  virtual void update(const MemoryRegion &array);
912 
924  virtual MemoryRegion final();
925 
931  void setup(const SymmetricKey &key);
932 
933 private:
934  class Private;
935  Private *d;
936 };
937 
952 class QCA_EXPORT KeyDerivationFunction : public Algorithm
953 {
954 public:
961 
963 
972  KeyDerivationFunction & operator=(const KeyDerivationFunction &from);
973 
986  SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, unsigned int iterationCount);
987 
1001  SymmetricKey makeKey(const SecureArray &secret,
1002  const InitializationVector &salt,
1003  unsigned int keyLength,
1004  int msecInterval,
1005  unsigned int *iterationCount);
1006 
1019  static QString withAlgorithm(const QString &kdfType, const QString &algType);
1020 
1021 protected:
1028  KeyDerivationFunction(const QString &type, const QString &provider);
1029 
1030 private:
1031  class Private;
1032  Private *d;
1033 };
1034 
1045 class QCA_EXPORT PBKDF1 : public KeyDerivationFunction
1046 {
1047 public:
1054  explicit PBKDF1(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1055  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf1"), algorithm), provider) {}
1056 };
1057 
1068 class QCA_EXPORT PBKDF2 : public KeyDerivationFunction
1069 {
1070 public:
1077  explicit PBKDF2(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1078  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf2"), algorithm), provider) {}
1079 };
1080 
1092 class QCA_EXPORT HKDF : public Algorithm
1093 {
1094 public:
1101  explicit HKDF(const QString &algorithm = QStringLiteral("sha256"), const QString &provider = QString());
1102 
1108  HKDF(const HKDF &from);
1109 
1110  ~HKDF();
1111 
1120  HKDF & operator=(const HKDF &from);
1121 
1134  SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt,
1135  const InitializationVector &info, unsigned int keyLength);
1136 };
1137 
1138 }
1139 
1140 #endif
PBKDF2(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1077
General superclass for an algorithm.
Definition: qca_core.h:1148
Padding
Padding variations for cipher algorithms.
Definition: qca_basic.h:615
General class for cipher (encryption / decryption) algorithms.
Definition: qca_basic.h:588
Mode
Mode settings for cipher algorithms.
Definition: qca_basic.h:598
Source of random numbers.
Definition: qca_basic.h:72
General superclass for buffered computation algorithms.
Definition: qca_core.h:1036
operate in Galois Counter Mode
Definition: qca_basic.h:605
operate in Output FeedBack Mode
Definition: qca_basic.h:603
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1248
Container for authentication tag.
Definition: qca_core.h:1331
General class for message authentication code (MAC) algorithms.
Definition: qca_basic.h:826
Simple container for acceptable key lengths
Definition: qca_core.h:697
PBKDF1(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1054
Header file for core QCA infrastructure.
Default for cipher-mode.
Definition: qca_basic.h:617
operate in Electronic Code Book mode
Definition: qca_basic.h:602
Container for initialisation vectors and nonces.
Definition: qca_core.h:1294
Direction
Direction settings for symmetric algorithms.
Definition: qca_core.h:140
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:48
operate in Cipher Block Chaining mode
Definition: qca_basic.h:600
Do not use padding.
Definition: qca_basic.h:618
General class for hashing algorithms.
Definition: qca_basic.h:215
Secure array of bytes.
Definition: qca_tools.h:316
Password based key derivation function version 1.
Definition: qca_basic.h:1045
operate in CounTer Mode
Definition: qca_basic.h:604
Operate in the "forward" direction; for example, encrypting.
Definition: qca_core.h:142
General superclass for filtering transformation algorithms.
Definition: qca_core.h:1092
Definition: qca_basic.h:1092
General superclass for key derivation algorithms.
Definition: qca_basic.h:952
operate in Cipher FeedBack mode
Definition: qca_basic.h:601
Password based key derivation function version 2.
Definition: qca_basic.h:1068
Array of bytes that may be optionally secured.
Definition: qca_tools.h:90