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 
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 
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,
632  Mode mode,
633  Padding pad = DefaultPadding,
634  Direction dir = Encode,
635  const SymmetricKey &key = SymmetricKey(),
637  const QString &provider = QString());
638 
656  Cipher(const QString &type,
657  Mode mode,
658  Padding pad,
659  Direction dir,
660  const SymmetricKey &key,
661  const InitializationVector &iv,
662  const AuthTag &tag,
663  const QString &provider = QString());
664 
670  Cipher(const Cipher &from);
671 
672  ~Cipher() override;
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 
709 
714 
721  bool validKeyLength(int n) const;
722 
726  int blockSize() const;
727 
731  AuthTag tag() const;
732 
736  void clear() override;
737 
745  MemoryRegion update(const MemoryRegion &a) override;
746 
751  MemoryRegion final() override;
752 
758  bool ok() const override;
759 
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 
802 private:
803  class Private;
804  Private *d;
805 };
806 
827 class QCA_EXPORT MessageAuthenticationCode : public Algorithm, public BufferedComputation
828 {
829 public:
839  MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider = QString());
840 
850 
851  ~MessageAuthenticationCode() override;
852 
862 
871  static QStringList supportedTypes(const QString &provider = QString());
872 
876  QString type() const;
877 
882 
889  bool validKeyLength(int n) const;
890 
903  void clear() override;
904 
912  void update(const MemoryRegion &array) override;
913 
925  MemoryRegion final() override;
926 
932  void setup(const SymmetricKey &key);
933 
934 private:
935  class Private;
936  Private *d;
937 };
938 
953 class QCA_EXPORT KeyDerivationFunction : public Algorithm
954 {
955 public:
962 
963  ~KeyDerivationFunction() override;
964 
974 
988  const InitializationVector &salt,
989  unsigned int keyLength,
990  unsigned int iterationCount);
991 
1006  const InitializationVector &salt,
1007  unsigned int keyLength,
1008  int msecInterval,
1009  unsigned int *iterationCount);
1010 
1023  static QString withAlgorithm(const QString &kdfType, const QString &algType);
1024 
1025 protected:
1032  KeyDerivationFunction(const QString &type, const QString &provider);
1033 
1034 private:
1035  class Private;
1036  Private *d;
1037 };
1038 
1049 class QCA_EXPORT PBKDF1 : public KeyDerivationFunction
1050 {
1051 public:
1058  explicit PBKDF1(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1059  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf1"), algorithm), provider)
1060  {
1061  }
1062 };
1063 
1074 class QCA_EXPORT PBKDF2 : public KeyDerivationFunction
1075 {
1076 public:
1083  explicit PBKDF2(const QString &algorithm = QStringLiteral("sha1"), const QString &provider = QString())
1084  : KeyDerivationFunction(withAlgorithm(QStringLiteral("pbkdf2"), algorithm), provider)
1085  {
1086  }
1087 };
1088 
1100 class QCA_EXPORT HKDF : public Algorithm
1101 {
1102 public:
1109  explicit HKDF(const QString &algorithm = QStringLiteral("sha256"), const QString &provider = QString());
1110 
1116  HKDF(const HKDF &from);
1117 
1118  ~HKDF() override;
1119 
1128  HKDF &operator=(const HKDF &from);
1129 
1143  const InitializationVector &salt,
1144  const InitializationVector &info,
1145  unsigned int keyLength);
1146 };
1147 
1148 }
1149 
1150 #endif
General superclass for an algorithm.
Definition: qca_core.h:1164
Container for authentication tag.
Definition: qca_core.h:1347
General superclass for buffered computation algorithms.
Definition: qca_core.h:1052
General class for cipher (encryption / decryption) algorithms.
Definition: qca_basic.h:582
bool validKeyLength(int n) const
Test if a key length is valid for the cipher algorithm.
Cipher(const QString &type, Mode mode, Padding pad, Direction dir, const SymmetricKey &key, const InitializationVector &iv, const AuthTag &tag, const QString &provider=QString())
Standard constructor.
Cipher & operator=(const Cipher &from)
Assignment operator.
Direction direction() const
Return the cipher direction.
static QStringList supportedTypes(const QString &provider=QString())
Returns a list of all of the cipher types available.
Padding padding() const
Return the cipher padding type.
Mode
Mode settings for cipher algorithms.
Definition: qca_basic.h:592
@ ECB
operate in Electronic Code Book mode
Definition: qca_basic.h:595
@ OFB
operate in Output FeedBack Mode
Definition: qca_basic.h:596
@ CFB
operate in Cipher FeedBack mode
Definition: qca_basic.h:594
@ GCM
operate in Galois Counter Mode
Definition: qca_basic.h:598
@ CTR
operate in CounTer Mode
Definition: qca_basic.h:597
@ CBC
operate in Cipher Block Chaining mode
Definition: qca_basic.h:593
void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv=InitializationVector())
Reset / reconfigure the Cipher.
QString type() const
Return the cipher type.
static QString withAlgorithms(const QString &cipherType, Mode modeType, Padding paddingType)
Construct a Cipher type string.
Cipher(const QString &type, Mode mode, Padding pad=DefaultPadding, Direction dir=Encode, const SymmetricKey &key=SymmetricKey(), const InitializationVector &iv=InitializationVector(), const QString &provider=QString())
Standard constructor.
AuthTag tag() const
return the authentication tag for the cipher object
MemoryRegion update(const MemoryRegion &a) override
pass in a byte array of data, which will be encrypted or decrypted (according to the Direction that w...
void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv, const AuthTag &tag)
Reset / reconfigure the Cipher.
bool ok() const override
Test if an update() or final() call succeeded.
void clear() override
reset the cipher object, to allow re-use
Cipher(const Cipher &from)
Standard copy constructor.
Mode mode() const
Return the cipher mode.
KeyLength keyLength() const
Return acceptable key lengths.
Padding
Padding variations for cipher algorithms.
Definition: qca_basic.h:609
@ NoPadding
Do not use padding.
Definition: qca_basic.h:611
@ DefaultPadding
Default for cipher-mode.
Definition: qca_basic.h:610
int blockSize() const
return the block size for the cipher object
General superclass for filtering transformation algorithms.
Definition: qca_core.h:1108
Definition: qca_basic.h:1101
HKDF & operator=(const HKDF &from)
Assignment operator.
HKDF(const QString &algorithm=QStringLiteral("sha256"), const QString &provider=QString())
Standard constructor.
HKDF(const HKDF &from)
Standard copy constructor.
SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, const InitializationVector &info, unsigned int keyLength)
Generate the key from a specified secret, salt value, and an additional info.
General class for hashing algorithms.
Definition: qca_basic.h:209
static QStringList supportedTypes(const QString &provider=QString())
Returns a list of all of the hash types available.
void update(QIODevice *file)
void update(const MemoryRegion &a) override
Update a hash, adding more of the message contents to the digest.
MemoryRegion hash(const MemoryRegion &array)
Hash & operator=(const Hash &from)
Assignment operator.
QString type() const
Return the hash type.
void clear() override
Reset a hash, dumping all previous parts of the message.
void update(const QByteArray &a)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Hash(const Hash &from)
Copy constructor.
QString hashToString(const MemoryRegion &array)
Hash a byte array, returning it as a printable string
Hash(const QString &type, const QString &provider=QString())
Constructor.
void update(const char *data, int len=-1)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Container for initialisation vectors and nonces.
Definition: qca_core.h:1310
General superclass for key derivation algorithms.
Definition: qca_basic.h:954
KeyDerivationFunction(const KeyDerivationFunction &from)
Standard copy constructor.
static QString withAlgorithm(const QString &kdfType, const QString &algType)
Construct the name of the algorithm.
SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, int msecInterval, unsigned int *iterationCount)
Generate the key from a specified secret and salt value.
SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, unsigned int iterationCount)
Generate the key from a specified secret and salt value.
KeyDerivationFunction(const QString &type, const QString &provider)
Special constructor for subclass initialisation.
KeyDerivationFunction & operator=(const KeyDerivationFunction &from)
Assignment operator.
Simple container for acceptable key lengths.
Definition: qca_core.h:701
Array of bytes that may be optionally secured.
Definition: qca_tools.h:91
General class for message authentication code (MAC) algorithms.
Definition: qca_basic.h:828
bool validKeyLength(int n) const
Test if a key length is valid for the MAC algorithm.
MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider=QString())
Standard constructor.
static QStringList supportedTypes(const QString &provider=QString())
Returns a list of all of the message authentication code types available.
void setup(const SymmetricKey &key)
Initialise the MAC algorithm.
QString type() const
Return the MAC type.
void update(const MemoryRegion &array) override
Update the MAC, adding more of the message contents to the digest.
MessageAuthenticationCode & operator=(const MessageAuthenticationCode &from)
Assignment operator.
MessageAuthenticationCode(const MessageAuthenticationCode &from)
Standard copy constructor.
KeyLength keyLength() const
Return acceptable key lengths.
void clear() override
Reset a MessageAuthenticationCode, dumping all previous parts of the message.
Password based key derivation function version 1.
Definition: qca_basic.h:1050
PBKDF1(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1058
Password based key derivation function version 2.
Definition: qca_basic.h:1075
PBKDF2(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1083
Source of random numbers.
Definition: qca_basic.h:66
uchar nextByte()
Provide a random byte.
Random(const Random &from)
Copy constructor.
static SecureArray randomArray(int size)
static int randomInt()
Random(const QString &provider=QString())
Standard Constructor.
static uchar randomChar()
Random & operator=(const Random &from)
Assignment operator.
SecureArray nextBytes(int size)
Provide a specified number of random bytes.
Secure array of bytes.
Definition: qca_tools.h:317
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1264
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
Direction
Direction settings for symmetric algorithms.
Definition: qca_core.h:141
@ Encode
Operate in the "forward" direction; for example, encrypting.
Definition: qca_core.h:142
Header file for core QCA infrastructure.