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, 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 
702 
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 
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 
854 
863  static QStringList supportedTypes(const QString &provider = QString());
864 
868  QString type() const;
869 
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 
966 
979  SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, unsigned int iterationCount);
980 
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 
1128  const InitializationVector &info, unsigned int keyLength);
1129 };
1130 
1131 }
1132 
1133 #endif
QCA::Cipher::NoPadding
@ NoPadding
Do not use padding.
Definition: qca_basic.h:611
QCA::PBKDF1
Password based key derivation function version 1.
Definition: qca_basic.h:1039
QCA::Cipher::CBC
@ CBC
operate in Cipher Block Chaining mode
Definition: qca_basic.h:593
QCA::Cipher::direction
Direction direction() const
Return the cipher direction.
QCA::Random
Source of random numbers.
Definition: qca_basic.h:66
QCA::Hash::update
void update(const char *data, int len=-1)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::Cipher::ECB
@ ECB
operate in Electronic Code Book mode
Definition: qca_basic.h:595
QCA::Direction
Direction
Direction settings for symmetric algorithms.
Definition: qca_core.h:141
QCA::Cipher::update
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...
QCA::Cipher
General class for cipher (encryption / decryption) algorithms.
Definition: qca_basic.h:582
QCA::Cipher::padding
Padding padding() const
Return the cipher padding type.
QCA::Random::operator=
Random & operator=(const Random &from)
Assignment operator.
QCA::Hash::Hash
Hash(const Hash &from)
Copy constructor.
QCA::Random::randomInt
static int randomInt()
QCA::Cipher::CTR
@ CTR
operate in CounTer Mode
Definition: qca_basic.h:597
QCA::Random::nextByte
uchar nextByte()
Provide a random byte.
QCA::HKDF
Definition: qca_basic.h:1086
QCA::MessageAuthenticationCode::validKeyLength
bool validKeyLength(int n) const
Test if a key length is valid for the MAC algorithm.
QCA::Hash::hash
MemoryRegion hash(const MemoryRegion &array)
QCA::Cipher::DefaultPadding
@ DefaultPadding
Default for cipher-mode.
Definition: qca_basic.h:610
QCA::Random::Random
Random(const QString &provider=QString())
Standard Constructor.
QCA::Cipher::keyLength
KeyLength keyLength() const
Return acceptable key lengths.
QCA::Hash::update
void update(const QByteArray &a)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::Cipher::mode
Mode mode() const
Return the cipher mode.
QCA
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
QCA::Encode
@ Encode
Operate in the "forward" direction; for example, encrypting.
Definition: qca_core.h:142
QCA::Algorithm
General superclass for an algorithm.
Definition: qca_core.h:1152
QCA::HKDF::HKDF
HKDF(const QString &algorithm=QStringLiteral("sha256"), const QString &provider=QString())
Standard constructor.
QCA::MessageAuthenticationCode::keyLength
KeyLength keyLength() const
Return acceptable key lengths.
QCA::Cipher::ok
bool ok() const override
Test if an update() or final() call succeeded.
QCA::Hash::hashToString
QString hashToString(const MemoryRegion &array)
Hash a byte array, returning it as a printable string
QCA::MessageAuthenticationCode::MessageAuthenticationCode
MessageAuthenticationCode(const MessageAuthenticationCode &from)
Standard copy constructor.
QCA::MessageAuthenticationCode::operator=
MessageAuthenticationCode & operator=(const MessageAuthenticationCode &from)
Assignment operator.
QCA::KeyDerivationFunction::makeKey
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.
QCA::SymmetricKey
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1252
QCA::MessageAuthenticationCode::clear
void clear() override
Reset a MessageAuthenticationCode, dumping all previous parts of the message.
QCA::Cipher::Cipher
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.
QCA::KeyDerivationFunction::withAlgorithm
static QString withAlgorithm(const QString &kdfType, const QString &algType)
Construct the name of the algorithm.
QCA::MessageAuthenticationCode::setup
void setup(const SymmetricKey &key)
Initialise the MAC algorithm.
QCA::Random::randomArray
static SecureArray randomArray(int size)
QCA::Hash::type
QString type() const
Return the hash type.
QCA::MessageAuthenticationCode::update
void update(const MemoryRegion &array) override
Update the MAC, adding more of the message contents to the digest.
QCA::KeyDerivationFunction::KeyDerivationFunction
KeyDerivationFunction(const KeyDerivationFunction &from)
Standard copy constructor.
QCA::InitializationVector
Container for initialisation vectors and nonces.
Definition: qca_core.h:1298
QCA::Cipher::Cipher
Cipher(const Cipher &from)
Standard copy constructor.
QCA::Cipher::Mode
Mode
Mode settings for cipher algorithms.
Definition: qca_basic.h:592
QCA::PBKDF2::PBKDF2
PBKDF2(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1070
QCA::Hash::update
void update(const MemoryRegion &a) override
Update a hash, adding more of the message contents to the digest.
QCA::Hash::operator=
Hash & operator=(const Hash &from)
Assignment operator.
QCA::Cipher::setup
void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv, const AuthTag &tag)
Reset / reconfigure the Cipher.
QCA::MessageAuthenticationCode::supportedTypes
static QStringList supportedTypes(const QString &provider=QString())
Returns a list of all of the message authentication code types available.
QCA::Cipher::tag
AuthTag tag() const
return the authentication tag for the cipher object
QCA::Cipher::blockSize
int blockSize() const
return the block size for the cipher object
QCA::KeyDerivationFunction::operator=
KeyDerivationFunction & operator=(const KeyDerivationFunction &from)
Assignment operator.
QCA::Hash::update
void update(QIODevice *file)
QCA::Cipher::CFB
@ CFB
operate in Cipher FeedBack mode
Definition: qca_basic.h:594
QCA::Cipher::validKeyLength
bool validKeyLength(int n) const
Test if a key length is valid for the cipher algorithm.
QCA::HKDF::operator=
HKDF & operator=(const HKDF &from)
Assignment operator.
QCA::HKDF::makeKey
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.
QCA::MessageAuthenticationCode
General class for message authentication code (MAC) algorithms.
Definition: qca_basic.h:820
QCA::SecureArray
Secure array of bytes.
Definition: qca_tools.h:317
QCA::Cipher::Padding
Padding
Padding variations for cipher algorithms.
Definition: qca_basic.h:609
QCA::KeyDerivationFunction::KeyDerivationFunction
KeyDerivationFunction(const QString &type, const QString &provider)
Special constructor for subclass initialisation.
QCA::KeyDerivationFunction
General superclass for key derivation algorithms.
Definition: qca_basic.h:946
QCA::Hash::supportedTypes
static QStringList supportedTypes(const QString &provider=QString())
Returns a list of all of the hash types available.
QCA::Cipher::operator=
Cipher & operator=(const Cipher &from)
Assignment operator.
QCA::Random::nextBytes
SecureArray nextBytes(int size)
Provide a specified number of random bytes.
QCA::Filter
General superclass for filtering transformation algorithms.
Definition: qca_core.h:1096
qca_core.h
Header file for core QCA infrastructure.
QCA::Cipher::clear
void clear() override
reset the cipher object, to allow re-use
QCA::Cipher::supportedTypes
static QStringList supportedTypes(const QString &provider=QString())
Returns a list of all of the cipher types available.
QCA::MemoryRegion
Array of bytes that may be optionally secured.
Definition: qca_tools.h:91
QCA::PBKDF2
Password based key derivation function version 2.
Definition: qca_basic.h:1062
QCA::Cipher::setup
void setup(Direction dir, const SymmetricKey &key, const InitializationVector &iv=InitializationVector())
Reset / reconfigure the Cipher.
QCA::Cipher::OFB
@ OFB
operate in Output FeedBack Mode
Definition: qca_basic.h:596
QCA::Cipher::withAlgorithms
static QString withAlgorithms(const QString &cipherType, Mode modeType, Padding paddingType)
Construct a Cipher type string.
QCA::KeyDerivationFunction::makeKey
SymmetricKey makeKey(const SecureArray &secret, const InitializationVector &salt, unsigned int keyLength, unsigned int iterationCount)
Generate the key from a specified secret and salt value.
QCA::AuthTag
Container for authentication tag.
Definition: qca_core.h:1335
QCA::Hash::clear
void clear() override
Reset a hash, dumping all previous parts of the message.
QCA::HKDF::HKDF
HKDF(const HKDF &from)
Standard copy constructor.
QCA::KeyLength
Simple container for acceptable key lengths.
Definition: qca_core.h:701
QCA::BufferedComputation
General superclass for buffered computation algorithms.
Definition: qca_core.h:1040
QCA::MessageAuthenticationCode::MessageAuthenticationCode
MessageAuthenticationCode(const QString &type, const SymmetricKey &key, const QString &provider=QString())
Standard constructor.
QCA::MessageAuthenticationCode::type
QString type() const
Return the MAC type.
QCA::PBKDF1::PBKDF1
PBKDF1(const QString &algorithm=QStringLiteral("sha1"), const QString &provider=QString())
Standard constructor.
Definition: qca_basic.h:1047
QCA::Hash
General class for hashing algorithms.
Definition: qca_basic.h:209
QCA::Random::randomChar
static uchar randomChar()
QCA::Hash::Hash
Hash(const QString &type, const QString &provider=QString())
Constructor.
QCA::Cipher::Cipher
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.
QCA::Random::Random
Random(const Random &from)
Copy constructor.
QCA::Cipher::GCM
@ GCM
operate in Galois Counter Mode
Definition: qca_basic.h:598
QCA::Cipher::type
QString type() const
Return the cipher type.