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 namespace QCA {
42 
65 class QCA_EXPORT Random : public Algorithm
66 {
67 public:
74  Random(const QString &provider = QString());
75 
81  Random(const Random &from);
82 
83  ~Random() override;
84 
90  Random & operator=(const Random &from);
91 
100  uchar nextByte();
101 
112  SecureArray nextBytes(int size);
113 
125  static uchar randomChar();
126 
136  static int randomInt();
137 
148  static SecureArray randomArray(int size);
149 
150 private:
151  class Private;
152  Private *d;
153 };
154 
208 class QCA_EXPORT Hash : public Algorithm, public BufferedComputation
209 {
210 public:
219  explicit Hash(const QString &type, const QString &provider = QString());
220 
226  Hash(const Hash &from);
227 
228  ~Hash() override;
229 
235  Hash & operator=(const Hash &from);
236 
244  static QStringList supportedTypes(const QString &provider = QString());
245 
249  QString type() const;
250 
261  void clear() override;
262 
274  void update(const MemoryRegion &a) override;
275 
281  void update(const QByteArray &a);
282 
297  void update(const char *data, int len = -1);
298 
321  void update(QIODevice *file);
322 
336  MemoryRegion final() override;
337 
358  MemoryRegion hash(const MemoryRegion &array);
359 
374  QString hashToString(const MemoryRegion &array);
375 
376 private:
377  class Private;
378  Private *d;
379 };
380 
581 class QCA_EXPORT Cipher : public Algorithm, public Filter
582 {
583 public:
591  enum Mode
592  {
593  CBC,
594  CFB,
595  ECB,
596  OFB,
597  CTR,
598  GCM,
599  CCM
600  };
601 
608  enum Padding
609  {
612  PKCS7
613  };
614 
631  Cipher(const QString &type, Mode mode, Padding pad = DefaultPadding,
632  Direction dir = Encode, const SymmetricKey &key = SymmetricKey(),
634  const QString &provider = QString());
635 
653  Cipher(const QString &type, Mode mode, Padding pad,
654  Direction dir, const SymmetricKey &key,
655  const InitializationVector &iv, const AuthTag &tag,
656  const QString &provider = QString());
657 
663  Cipher(const Cipher &from);
664 
665  ~Cipher() override;
666 
672  Cipher & operator=(const Cipher &from);
673 
681  static QStringList supportedTypes(const QString &provider = QString());
682 
686  QString type() const;
687 
691  Mode mode() const;
692 
696  Padding padding() const;
697 
701  Direction direction() const;
702 
706  KeyLength keyLength() const;
707 
714  bool validKeyLength(int n) const;
715 
719  int blockSize() const;
720 
724  AuthTag tag() const;
725 
729  void clear() override;
730 
738  MemoryRegion update(const MemoryRegion &a) override;
739 
744  MemoryRegion final() override;
745 
751  bool ok() const override;
752 
766  void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv = InitializationVector());
767 
782  void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv, const AuthTag &tag);
783 
793  static QString withAlgorithms(const QString &cipherType, Mode modeType, Padding paddingType);
794 private:
795  class Private;
796  Private *d;
797 };
798 
819 class QCA_EXPORT MessageAuthenticationCode : public Algorithm, public BufferedComputation
820 {
821 public:
831  MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider = QString());
832 
842 
843  ~MessageAuthenticationCode() override;
844 
853  MessageAuthenticationCode & operator=(const MessageAuthenticationCode &from);
854 
863  static QStringList supportedTypes(const QString &provider = QString());
864 
868  QString type() const;
869 
873  KeyLength keyLength() const;
874 
881  bool validKeyLength(int n) const;
882 
895  void clear() override;
896 
904  void update(const MemoryRegion &array) override;
905 
917  MemoryRegion final() override;
918 
924  void setup(const SymmetricKey &key);
925 
926 private:
927  class Private;
928  Private *d;
929 };
930 
945 class QCA_EXPORT KeyDerivationFunction : public Algorithm
946 {
947 public:
954 
955  ~KeyDerivationFunction() override;
956 
965  KeyDerivationFunction & operator=(const KeyDerivationFunction &from);
966 
979  SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, unsigned int iterationCount);
980 
994  SymmetricKey makeKey(const SecureArray &secret,
995  const InitializationVector &salt,
996  unsigned int keyLength,
997  int msecInterval,
998  unsigned int *iterationCount);
999 
1012  static QString withAlgorithm(const QString &kdfType, const QString &algType);
1013 
1014 protected:
1021  KeyDerivationFunction(const QString &type, const QString &provider);
1022 
1023 private:
1024  class Private;
1025  Private *d;
1026 };
1027 
1038 class QCA_EXPORT PBKDF1 : public KeyDerivationFunction
1039 {
1040 public:
1047  explicit PBKDF1(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1048  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf1"), algorithm), provider) {}
1049 };
1050 
1061 class QCA_EXPORT PBKDF2 : public KeyDerivationFunction
1062 {
1063 public:
1070  explicit PBKDF2(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1071  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf2"), algorithm), provider) {}
1072 };
1073 
1085 class QCA_EXPORT HKDF : public Algorithm
1086 {
1087 public:
1094  explicit HKDF(const QString &algorithm = QStringLiteral("sha256"), const QString &provider = QString());
1095 
1101  HKDF(const HKDF &from);
1102 
1103  ~HKDF() override;
1104 
1113  HKDF & operator=(const HKDF &from);
1114 
1127  SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt,
1128  const InitializationVector &info, unsigned int keyLength);
1129 };
1130 
1131 }
1132 
1133 #endif
PBKDF2(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1070
General superclass for an algorithm.
Definition: qca_core.h:1151
Padding
Padding variations for cipher algorithms.
Definition: qca_basic.h:608
General class for cipher (encryption / decryption) algorithms.
Definition: qca_basic.h:581
Mode
Mode settings for cipher algorithms.
Definition: qca_basic.h:591
Source of random numbers.
Definition: qca_basic.h:65
General superclass for buffered computation algorithms.
Definition: qca_core.h:1039
operate in Galois Counter Mode
Definition: qca_basic.h:598
operate in Output FeedBack Mode
Definition: qca_basic.h:596
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1251
Container for authentication tag.
Definition: qca_core.h:1334
General class for message authentication code (MAC) algorithms.
Definition: qca_basic.h:819
Simple container for acceptable key lengths
Definition: qca_core.h:700
PBKDF1(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1047
Header file for core QCA infrastructure.
Default for cipher-mode.
Definition: qca_basic.h:610
operate in Electronic Code Book mode
Definition: qca_basic.h:595
Container for initialisation vectors and nonces.
Definition: qca_core.h:1297
Direction
Direction settings for symmetric algorithms.
Definition: qca_core.h:140
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
operate in Cipher Block Chaining mode
Definition: qca_basic.h:593
Do not use padding.
Definition: qca_basic.h:611
General class for hashing algorithms.
Definition: qca_basic.h:208
Secure array of bytes.
Definition: qca_tools.h:316
Password based key derivation function version 1.
Definition: qca_basic.h:1038
operate in CounTer Mode
Definition: qca_basic.h:597
Operate in the "forward" direction; for example, encrypting.
Definition: qca_core.h:142
General superclass for filtering transformation algorithms.
Definition: qca_core.h:1095
Definition: qca_basic.h:1085
General superclass for key derivation algorithms.
Definition: qca_basic.h:945
operate in Cipher FeedBack mode
Definition: qca_basic.h:594
Password based key derivation function version 2.
Definition: qca_basic.h:1061
Array of bytes that may be optionally secured.
Definition: qca_tools.h:90