Qt Cryptographic Architecture
qca_tools.h
Go to the documentation of this file.
1 /*
2  * qca_tools.h - Qt Cryptographic Architecture
3  * Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com>
4  * Copyright (C) 2004,2005 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 
36 #ifndef QCA_TOOLS_H
37 #define QCA_TOOLS_H
38 
39 #include <QSharedData>
40 #include <QSharedDataPointer>
41 #include <QMetaType>
42 #include "qca_export.h"
43 
44 class QString;
45 class QByteArray;
46 class QTextStream;
47 
55 QCA_EXPORT void *qca_secure_alloc(int bytes);
56 
65 QCA_EXPORT void qca_secure_free(void *p);
66 
74 QCA_EXPORT void *qca_secure_realloc(void *p, int bytes);
75 
76 namespace QCA {
77 
90 class QCA_EXPORT MemoryRegion
91 {
92 public:
93  MemoryRegion();
94 
101  MemoryRegion(const char *str);
102 
109  MemoryRegion(const QByteArray &from);
110 
117  ~MemoryRegion();
118 
125 
131  MemoryRegion & operator=(const QByteArray &from);
132 
141  bool isNull() const;
142 
151  bool isSecure() const;
152 
161  QByteArray toByteArray() const;
162 
166  bool isEmpty() const;
167 
171  int size() const;
172 
182  const char *data() const;
183 
192  const char *constData() const;
193 
204  const char & at(int index) const;
205 
206 protected:
218  MemoryRegion(bool secure);
219 
229  MemoryRegion(int size, bool secure);
230 
243  MemoryRegion(const QByteArray &from, bool secure);
244 
250  char *data();
251 
262  char & at(int index);
263 
269  bool resize(int size);
270 
280  void set(const QByteArray &from, bool secure);
281 
293  void setSecure(bool secure);
294 
295 private:
296  bool _secure;
297  class Private;
298  QSharedDataPointer<Private> d;
299 };
300 
316 class QCA_EXPORT SecureArray : public MemoryRegion
317 {
318 public:
323 
330  explicit SecureArray(int size, char ch = 0);
331 
339  SecureArray(const char *str);
340 
350  SecureArray(const QByteArray &a);
351 
362 
368  SecureArray(const SecureArray &from);
369 
370  ~SecureArray();
371 
378 
384  SecureArray & operator=(const QByteArray &a);
385 
389  void clear();
390 
396  char & operator[](int index);
397 
403  const char & operator[](int index) const;
404 
412  char *data();
413 
421  const char *data() const;
422 
430  const char *constData() const;
431 
437  char & at(int index);
438 
444  const char & at(int index) const;
445 
449  int size() const;
450 
460  bool isEmpty() const;
461 
470  bool resize(int size);
471 
486  void fill(char fillChar, int fillToPosition = -1);
487 
493  QByteArray toByteArray() const;
494 
501 
508  bool operator==(const MemoryRegion &other) const;
509 
516  inline bool operator!=(const MemoryRegion &other) const
517  {
518  return !(*this == other);
519  }
520 
527 
528 protected:
535  void set(const SecureArray &from);
536 
543  void set(const QByteArray &from);
544 };
545 
552 QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b);
553 
570 class QCA_EXPORT BigInteger
571 {
572 public:
577 
583  BigInteger(int n);
584 
594  BigInteger(const char *c);
595 
601  BigInteger(const QString &s);
602 
609 
615  BigInteger(const BigInteger &from);
616 
617  ~BigInteger();
618 
631 
643  BigInteger & operator=(const QString &s);
644 
658 
672 
679 
686 
693 
702 
712  void fromArray(const QCA::SecureArray &a);
713 
723  QString toString() const;
724 
737  bool fromString(const QString &s);
738 
761  int compare(const BigInteger &n) const;
762 
769  inline bool operator==(const BigInteger &other) const
770  {
771  return (compare(other) == 0);
772  }
773 
780  inline bool operator!=(const BigInteger &other) const
781  {
782  return !(*this == other);
783  }
784 
792  inline bool operator<=(const BigInteger &other) const
793  {
794  return (compare(other) <= 0);
795  }
796 
804  inline bool operator>=(const BigInteger &other) const
805  {
806  return (compare(other) >= 0);
807  }
808 
816  inline bool operator<(const BigInteger &other) const
817  {
818  return (compare(other) < 0);
819  }
820 
828  inline bool operator>(const BigInteger &other) const
829  {
830  return (compare(other) > 0);
831  }
832 
833 private:
834  class Private;
835  QSharedDataPointer<Private> d;
836 };
837 
838 
839 
848 QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b);
849 
850 
851 }
852 
853 #endif
QCA::BigInteger::operator>=
bool operator>=(const BigInteger &other) const
Greater than or equal operator.
Definition: qca_tools.h:804
QCA::SecureArray::operator==
bool operator==(const MemoryRegion &other) const
Equality operator.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const QByteArray &from, bool secure)
Create a memory region, optionally using secure storage.
QCA::SecureArray::operator!=
bool operator!=(const MemoryRegion &other) const
Inequality operator.
Definition: qca_tools.h:516
QCA::BigInteger::operator==
bool operator==(const BigInteger &other) const
Equality operator.
Definition: qca_tools.h:769
QCA::SecureArray::operator=
SecureArray & operator=(const QByteArray &a)
Creates a copy, rather than references.
QCA::MemoryRegion::resize
bool resize(int size)
Resize the memory region to the specified size.
QCA::SecureArray::clear
void clear()
Clears the contents of the array and makes it empty.
QCA::MemoryRegion::setSecure
void setSecure(bool secure)
Convert the memory region to use the specified memory type.
QCA::SecureArray::operator[]
const char & operator[](int index) const
Returns a reference to the byte at the index position.
qca_secure_alloc
QCA_EXPORT void * qca_secure_alloc(int bytes)
Allocate a block of memory from the secure memory pool.
QCA::operator+
QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b)
Returns an array that is the result of concatenating a and b.
QCA::BigInteger::toString
QString toString() const
QCA::MemoryRegion::MemoryRegion
MemoryRegion(int size, bool secure)
Create a memory region, optionally using secure storage.
QCA::BigInteger::operator/=
BigInteger & operator/=(const BigInteger &b)
Divide in place operator.
QCA::MemoryRegion::data
char * data()
Convert the contents of the memory region to a C-compatible character array.
QCA::MemoryRegion::isSecure
bool isSecure() const
Test if the MemoryRegion is using secure memory, or not.
QCA::BigInteger::operator<=
bool operator<=(const BigInteger &other) const
Less than or equal operator.
Definition: qca_tools.h:792
QCA::BigInteger::operator%=
BigInteger & operator%=(const BigInteger &b)
Modulo in place operator.
QCA::BigInteger::operator>
bool operator>(const BigInteger &other) const
Greater than operator.
Definition: qca_tools.h:828
QCA::BigInteger::operator<
bool operator<(const BigInteger &other) const
Less than operator.
Definition: qca_tools.h:816
QCA::MemoryRegion::MemoryRegion
MemoryRegion(bool secure)
Create a memory region, optionally using secure storage.
QCA
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
QCA::SecureArray::fill
void fill(char fillChar, int fillToPosition=-1)
Fill the data array with a specified character.
QCA::MemoryRegion::operator=
MemoryRegion & operator=(const MemoryRegion &from)
Standard assignment operator.
QCA::BigInteger::toArray
QCA::SecureArray toArray() const
Output BigInteger as a byte array, useful for storage or transmission.
QCA::SecureArray::constData
const char * constData() const
Pointer to the data in the secure array.
QCA::SecureArray::operator+=
SecureArray & operator+=(const SecureArray &a)
Append a secure byte array to the end of this array.
QCA::BigInteger::operator+=
BigInteger & operator+=(const BigInteger &b)
QCA::BigInteger::fromArray
void fromArray(const QCA::SecureArray &a)
Assign from an array.
QCA::MemoryRegion::constData
const char * constData() const
Convert the contents of the memory region to a C-compatible character array.
qca_export.h
QCA::SecureArray::toByteArray
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const char *str)
Constructs a new Memory Region from a null terminated character array.
QCA::BigInteger::BigInteger
BigInteger(const QCA::SecureArray &a)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::BigInteger::BigInteger
BigInteger()
Constructor.
QCA::SecureArray::SecureArray
SecureArray(int size, char ch=0)
Construct a secure byte array of the specified length.
QCA::SecureArray::operator=
SecureArray & operator=(const SecureArray &from)
Creates a reference, rather than a deep copy.
QCA::MemoryRegion::isEmpty
bool isEmpty() const
Returns true if the size of the memory region is zero.
QCA::SecureArray::SecureArray
SecureArray(const MemoryRegion &a)
Construct a secure byte array from a MemoryRegion.
qca_secure_realloc
QCA_EXPORT void * qca_secure_realloc(void *p, int bytes)
Resize (re-allocate) a block of memory that has been previously allocated from the secure memory pool...
QCA::SecureArray::SecureArray
SecureArray(const SecureArray &from)
Construct a (shallow) copy of another secure byte array.
qca_secure_free
QCA_EXPORT void qca_secure_free(void *p)
Free (de-allocate) a block of memory that has been previously allocated from the secure memory pool.
QCA::BigInteger::BigInteger
BigInteger(const BigInteger &from)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::SecureArray::resize
bool resize(int size)
Change the length of this array If the new length is less than the old length, the extra information ...
QCA::BigInteger::operator-=
BigInteger & operator-=(const BigInteger &b)
QCA::SecureArray::operator[]
char & operator[](int index)
Returns a reference to the byte at the index position.
QCA::SecureArray
Definition: qca_tools.h:317
QCA::BigInteger::BigInteger
BigInteger(int n)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::BigInteger
Definition: qca_tools.h:571
QCA::BigInteger::operator=
BigInteger & operator=(const BigInteger &from)
QCA::BigInteger::compare
int compare(const BigInteger &n) const
QCA::BigInteger::operator=
BigInteger & operator=(const QString &s)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::MemoryRegion::at
const char & at(int index) const
Obtain the value of the memory location at the specified position.
QCA::BigInteger::operator*=
BigInteger & operator*=(const BigInteger &b)
Multiply in place operator.
QCA::MemoryRegion::set
void set(const QByteArray &from, bool secure)
Modify the memory region to match a specified byte array.
QCA::BigInteger::BigInteger
BigInteger(const char *c)
QCA::SecureArray::set
void set(const QByteArray &from)
Assign the contents of a provided byte array to this object.
QCA::SecureArray::set
void set(const SecureArray &from)
Assign the contents of a provided byte array to this object.
QCA::SecureArray::SecureArray
SecureArray(const char *str)
Construct a secure byte array from a string.
QCA::SecureArray::data
const char * data() const
Pointer to the data in the secure array.
QCA::MemoryRegion::toByteArray
QByteArray toByteArray() const
Convert this memory region to a byte array.
QCA::MemoryRegion
Definition: qca_tools.h:91
QCA::SecureArray::SecureArray
SecureArray(const QByteArray &a)
Construct a secure byte array from a QByteArray.
QCA::SecureArray::at
const char & at(int index) const
Returns a reference to the byte at the index position.
QCA::SecureArray::at
char & at(int index)
Returns a reference to the byte at the index position.
QCA::SecureArray::append
SecureArray & append(const SecureArray &a)
Append a secure byte array to the end of this array.
QCA::BigInteger::operator!=
bool operator!=(const BigInteger &other) const
Inequality operator.
Definition: qca_tools.h:780
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const QByteArray &from)
Constructs a new MemoryRegion from the data in a byte array.
QCA::BigInteger::fromString
bool fromString(const QString &s)
Assign from a QString.
QCA::MemoryRegion::operator=
MemoryRegion & operator=(const QByteArray &from)
Standard assignment operator.
QCA::SecureArray::isEmpty
bool isEmpty() const
Test if the array contains any bytes.
QCA::MemoryRegion::isNull
bool isNull() const
Test if the MemoryRegion is null (i.e.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const MemoryRegion &from)
Standard copy constructor.
QCA::SecureArray::SecureArray
SecureArray()
Construct a secure byte array, zero length.
QCA::BigInteger::BigInteger
BigInteger(const QString &s)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::SecureArray::size
int size() const
Returns the number of bytes in the array.
QCA::SecureArray::data
char * data()
Pointer to the data in the secure array.
QCA::MemoryRegion::at
char & at(int index)
Obtain the value of the memory location at the specified position.
QCA::MemoryRegion::size
int size() const
Returns the number of bytes in the memory region.
QCA::MemoryRegion::data
const char * data() const
Convert the contents of the memory region to a C-compatible character array.