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 // Qt5 comes with QStringLiteral for wrapping string literals, which Qt4 does
39 // not have. It is needed if the headers are built with QT_NO_CAST_FROM_ASCII.
40 // Defining it here as QString::fromUtf8 for convenience.
41 #ifndef QStringLiteral
42 #define QStringLiteral(str) QString::fromUtf8(str)
43 #endif
44 
45 namespace QCA {
46 
69 class QCA_EXPORT Random : public Algorithm
70 {
71 public:
78  Random(const QString &provider = QString());
79 
85  Random(const Random &from);
86 
87  ~Random();
88 
94  Random & operator=(const Random &from);
95 
104  uchar nextByte();
105 
116  SecureArray nextBytes(int size);
117 
129  static uchar randomChar();
130 
140  static int randomInt();
141 
152  static SecureArray randomArray(int size);
153 
154 private:
155  class Private;
156  Private *d;
157 };
158 
212 class QCA_EXPORT Hash : public Algorithm, public BufferedComputation
213 {
214 public:
223  explicit Hash(const QString &type, const QString &provider = QString());
224 
230  Hash(const Hash &from);
231 
232  ~Hash();
233 
239  Hash & operator=(const Hash &from);
240 
248  static QStringList supportedTypes(const QString &provider = QString());
249 
253  QString type() const;
254 
265  virtual void clear();
266 
278  virtual void update(const MemoryRegion &a);
279 
285  void update(const QByteArray &a);
286 
301  void update(const char *data, int len = -1);
302 
325  void update(QIODevice *file);
326 
340  virtual MemoryRegion final();
341 
362  MemoryRegion hash(const MemoryRegion &array);
363 
378  QString hashToString(const MemoryRegion &array);
379 
380 private:
381  class Private;
382  Private *d;
383 };
384 
582 class QCA_EXPORT Cipher : public Algorithm, public Filter
583 {
584 public:
592  enum Mode
593  {
594  CBC,
595  CFB,
596  ECB,
597  OFB,
598  CTR,
599  };
600 
607  enum Padding
608  {
611  PKCS7
612  };
613 
630  Cipher(const QString &type, Mode mode, Padding pad = DefaultPadding,
631  Direction dir = Encode, const SymmetricKey &key = SymmetricKey(),
633  const QString &provider = QString());
634 
640  Cipher(const Cipher &from);
641 
642  ~Cipher();
643 
649  Cipher & operator=(const Cipher &from);
650 
658  static QStringList supportedTypes(const QString &provider = QString());
659 
663  QString type() const;
664 
668  Mode mode() const;
669 
673  Padding padding() const;
674 
678  Direction direction() const;
679 
683  KeyLength keyLength() const;
684 
691  bool validKeyLength(int n) const;
692 
696  int blockSize() const;
697 
701  virtual void clear();
702 
710  virtual MemoryRegion update(const MemoryRegion &a);
711 
716  virtual MemoryRegion final();
717 
723  virtual bool ok() const;
724 
738  void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv = InitializationVector());
739 
749  static QString withAlgorithms(const QString &cipherType, Mode modeType, Padding paddingType);
750 
751 private:
752  class Private;
753  Private *d;
754 };
755 
776 class QCA_EXPORT MessageAuthenticationCode : public Algorithm, public BufferedComputation
777 {
778 public:
788  MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider = QString());
789 
799 
801 
810  MessageAuthenticationCode & operator=(const MessageAuthenticationCode &from);
811 
820  static QStringList supportedTypes(const QString &provider = QString());
821 
825  QString type() const;
826 
830  KeyLength keyLength() const;
831 
838  bool validKeyLength(int n) const;
839 
852  virtual void clear();
853 
861  virtual void update(const MemoryRegion &array);
862 
874  virtual MemoryRegion final();
875 
881  void setup(const SymmetricKey &key);
882 
883 private:
884  class Private;
885  Private *d;
886 };
887 
902 class QCA_EXPORT KeyDerivationFunction : public Algorithm
903 {
904 public:
911 
913 
922  KeyDerivationFunction & operator=(const KeyDerivationFunction &from);
923 
936  SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, unsigned int iterationCount);
937 
951  SymmetricKey makeKey(const SecureArray &secret,
952  const InitializationVector &salt,
953  unsigned int keyLength,
954  int msecInterval,
955  unsigned int *iterationCount);
956 
969  static QString withAlgorithm(const QString &kdfType, const QString &algType);
970 
971 protected:
978  KeyDerivationFunction(const QString &type, const QString &provider);
979 
980 private:
981  class Private;
982  Private *d;
983 };
984 
995 class QCA_EXPORT PBKDF1 : public KeyDerivationFunction
996 {
997 public:
1004  explicit PBKDF1(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1005  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf1"), algorithm), provider) {}
1006 };
1007 
1018 class QCA_EXPORT PBKDF2 : public KeyDerivationFunction
1019 {
1020 public:
1027  explicit PBKDF2(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1028  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf2"), algorithm), provider) {}
1029 };
1030 
1031 }
1032 
1033 #endif
PBKDF2(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1027
General superclass for an algorithm.
Definition: qca_core.h:1121
Padding
Padding variations for cipher algorithms.
Definition: qca_basic.h:607
General class for cipher (encryption / decryption) algorithms.
Definition: qca_basic.h:582
Mode
Mode settings for cipher algorithms.
Definition: qca_basic.h:592
Source of random numbers.
Definition: qca_basic.h:69
General superclass for buffered computation algorithms.
Definition: qca_core.h:1009
operate in Output FeedBack Mode
Definition: qca_basic.h:597
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1221
General class for message authentication code (MAC) algorithms.
Definition: qca_basic.h:776
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:1004
Header file for core QCA infrastructure.
Default for cipher-mode.
Definition: qca_basic.h:609
operate in Electronic Code Book mode
Definition: qca_basic.h:596
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:45
operate in Cipher Block Chaining mode
Definition: qca_basic.h:594
Do not use padding.
Definition: qca_basic.h:610
General class for hashing algorithms.
Definition: qca_basic.h:212
Secure array of bytes.
Definition: qca_tools.h:316
Password based key derivation function version 1.
Definition: qca_basic.h:995
operate in CounTer Mode
Definition: qca_basic.h:598
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:902
operate in Cipher FeedBack mode
Definition: qca_basic.h:595
Password based key derivation function version 2.
Definition: qca_basic.h:1018
Array of bytes that may be optionally secured.
Definition: qca_tools.h:90