qca_tools.h
Go to the documentation of this file.
/*
* qca_tools.h - Qt Cryptographic Architecture
* Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com>
* Copyright (C) 2004,2005 Brad Hards <bradh@frogmouth.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef QCA_TOOLS_H
#define QCA_TOOLS_H
#include "qca_export.h"
#include <QMetaType>
#include <QSharedData>
#include <QSharedDataPointer>
class QString;
class QByteArray;
class QTextStream;
QCA_EXPORT void *qca_secure_alloc(int bytes);
QCA_EXPORT void qca_secure_free(void *p);
QCA_EXPORT void *qca_secure_realloc(void *p, int bytes);
namespace QCA {
class QCA_EXPORT MemoryRegion
{
public:
MemoryRegion();
MemoryRegion(const char *str);
MemoryRegion(const QByteArray &from);
MemoryRegion(const MemoryRegion &from);
~MemoryRegion();
MemoryRegion &operator=(const MemoryRegion &from);
MemoryRegion &operator=(const QByteArray &from);
bool isNull() const;
bool isSecure() const;
QByteArray toByteArray() const;
bool isEmpty() const;
int size() const;
const char *data() const;
const char *constData() const;
const char &at(int index) const;
protected:
MemoryRegion(bool secure);
MemoryRegion(int size, bool secure);
MemoryRegion(const QByteArray &from, bool secure);
char *data();
char &at(int index);
bool resize(int size);
void set(const QByteArray &from, bool secure);
void setSecure(bool secure);
private:
bool _secure;
class Private;
QSharedDataPointer<Private> d;
};
class QCA_EXPORT SecureArray : public MemoryRegion
{
public:
SecureArray();
explicit SecureArray(int size, char ch = 0);
SecureArray(const char *str);
SecureArray(const QByteArray &a);
SecureArray(const MemoryRegion &a);
SecureArray(const SecureArray &from);
~SecureArray();
SecureArray &operator=(const SecureArray &from);
SecureArray &operator=(const QByteArray &a);
void clear();
char &operator[](int index);
const char &operator[](int index) const;
char *data();
const char *data() const;
const char *constData() const;
char &at(int index);
const char &at(int index) const;
int size() const;
bool isEmpty() const;
bool resize(int size);
void fill(char fillChar, int fillToPosition = -1);
QByteArray toByteArray() const;
SecureArray &append(const SecureArray &a);
bool operator==(const MemoryRegion &other) const;
inline bool operator!=(const MemoryRegion &other) const
{
return !(*this == other);
}
SecureArray &operator+=(const SecureArray &a);
protected:
void set(const SecureArray &from);
void set(const QByteArray &from);
};
QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b);
class QCA_EXPORT BigInteger
{
public:
BigInteger();
BigInteger(int n);
BigInteger(const char *c);
BigInteger(const QString &s);
BigInteger(const QCA::SecureArray &a);
BigInteger(const BigInteger &from);
~BigInteger();
BigInteger &operator=(const BigInteger &from);
BigInteger &operator=(const QString &s);
BigInteger &operator+=(const BigInteger &b);
BigInteger &operator-=(const BigInteger &b);
BigInteger &operator*=(const BigInteger &b);
BigInteger &operator/=(const BigInteger &b);
BigInteger &operator%=(const BigInteger &b);
QCA::SecureArray toArray() const;
void fromArray(const QCA::SecureArray &a);
QString toString() const;
bool fromString(const QString &s);
int compare(const BigInteger &n) const;
inline bool operator==(const BigInteger &other) const
{
return (compare(other) == 0);
}
inline bool operator!=(const BigInteger &other) const
{
return !(*this == other);
}
inline bool operator<=(const BigInteger &other) const
{
return (compare(other) <= 0);
}
inline bool operator>=(const BigInteger &other) const
{
return (compare(other) >= 0);
}
inline bool operator<(const BigInteger &other) const
{
return (compare(other) < 0);
}
inline bool operator>(const BigInteger &other) const
{
return (compare(other) > 0);
}
private:
class Private;
QSharedDataPointer<Private> d;
};
QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b);
}
#endif
QCA::BigInteger::BigInteger
BigInteger(int n)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::BigInteger::operator!=
bool operator!=(const BigInteger &other) const
Inequality operator.
Definition qca_tools.h:780
QCA::BigInteger::fromArray
void fromArray(const QCA::SecureArray &a)
Assign from an array.
QCA::BigInteger::fromString
bool fromString(const QString &s)
Assign from a QString.
QCA::BigInteger::operator<=
bool operator<=(const BigInteger &other) const
Less than or equal operator.
Definition qca_tools.h:792
QCA::BigInteger::BigInteger
BigInteger(const char *c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::BigInteger::operator=
BigInteger & operator=(const BigInteger &from)
Assignment operator.
QCA::BigInteger::operator-=
BigInteger & operator-=(const BigInteger &b)
Decrement in place operator.
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
Greater than or equal operator.
Definition qca_tools.h:804
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::toArray
QCA::SecureArray toArray() const
Output BigInteger as a byte array, useful for storage or transmission.
QCA::BigInteger::BigInteger
BigInteger(const BigInteger &from)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::BigInteger::BigInteger
BigInteger(const QString &s)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QCA::BigInteger::toString
QString toString() const
Convert BigInteger to a QString.
QCA::BigInteger::operator<<
QCA_EXPORT QTextStream & operator<<(QTextStream &stream, const BigInteger &b)
Stream operator.
QCA::BigInteger::compare
int compare(const BigInteger &n) const
Compare this value with another BigInteger.
QCA::BigInteger::BigInteger
BigInteger()
Constructor.
QCA::BigInteger::operator==
bool operator==(const BigInteger &other) const
Equality operator.
Definition qca_tools.h:769
QCA::BigInteger::operator<
bool operator<(const BigInteger &other) const
Less than operator.
Definition qca_tools.h:816
QCA::BigInteger::operator/=
BigInteger & operator/=(const BigInteger &b)
Divide in place operator.
QCA::BigInteger::operator*=
BigInteger & operator*=(const BigInteger &b)
Multiply in place operator.
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::BigInteger::operator+=
BigInteger & operator+=(const BigInteger &b)
Increment in place operator.
QCA::MemoryRegion::resize
bool resize(int size)
Resize the memory region to the specified size.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const MemoryRegion &from)
Standard copy constructor.
QCA::MemoryRegion::operator=
MemoryRegion & operator=(const MemoryRegion &from)
Standard assignment operator.
QCA::MemoryRegion::operator=
MemoryRegion & operator=(const QByteArray &from)
Standard assignment operator.
QCA::MemoryRegion::isNull
bool isNull() const
Test if the MemoryRegion is null (i.e.
QCA::MemoryRegion::set
void set(const QByteArray &from, bool secure)
Modify the memory region to match a specified byte array.
QCA::MemoryRegion::data
const char * data() const
Convert the contents of the memory region to a C-compatible character array.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(int size, bool secure)
Create a memory region, optionally using secure storage.
QCA::MemoryRegion::isEmpty
bool isEmpty() const
Returns true if the size of the memory region is zero.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const QByteArray &from, bool secure)
Create a memory region, optionally using secure storage.
QCA::MemoryRegion::isSecure
bool isSecure() const
Test if the MemoryRegion is using secure memory, or not.
QCA::MemoryRegion::size
int size() const
Returns the number of bytes in the memory region.
QCA::MemoryRegion::setSecure
void setSecure(bool secure)
Convert the memory region to use the specified memory type.
QCA::MemoryRegion::at
const char & at(int index) const
Obtain the value of the memory location at the specified position.
QCA::MemoryRegion::constData
const char * constData() const
Convert the contents of the memory region to a C-compatible character array.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const QByteArray &from)
Constructs a new MemoryRegion from the data in a byte array.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(const char *str)
Constructs a new Memory Region from a null terminated character array.
QCA::MemoryRegion::toByteArray
QByteArray toByteArray() const
Convert this memory region to a byte array.
QCA::MemoryRegion::at
char & at(int index)
Obtain the value of the memory location at the specified position.
QCA::MemoryRegion::MemoryRegion
MemoryRegion(bool secure)
Create a memory region, optionally using secure storage.
QCA::MemoryRegion::data
char * data()
Convert the contents of the memory region to a C-compatible character array.
QCA::SecureArray
Secure array of bytes.
Definition qca_tools.h:317
QCA::SecureArray::operator=
SecureArray & operator=(const SecureArray &from)
Creates a reference, rather than a deep copy.
QCA::SecureArray::set
void set(const SecureArray &from)
Assign the contents of a provided byte array to this object.
QCA::SecureArray::data
const char * data() const
Pointer to the data in the secure array.
QCA::SecureArray::size
int size() const
Returns the number of bytes in the array.
QCA::SecureArray::SecureArray
SecureArray(const MemoryRegion &a)
Construct a secure byte array from a MemoryRegion.
QCA::SecureArray::operator[]
const char & operator[](int index) const
Returns a reference to the byte at the index position.
QCA::SecureArray::operator=
SecureArray & operator=(const QByteArray &a)
Creates a copy, rather than references.
QCA::SecureArray::operator[]
char & operator[](int index)
Returns a reference to the byte at the index position.
QCA::SecureArray::operator!=
bool operator!=(const MemoryRegion &other) const
Inequality operator.
Definition qca_tools.h:516
QCA::SecureArray::SecureArray
SecureArray(const QByteArray &a)
Construct a secure byte array from a QByteArray.
QCA::SecureArray::SecureArray
SecureArray()
Construct a secure byte array, zero length.
QCA::SecureArray::at
char & at(int index)
Returns a reference to the byte at the index position.
QCA::SecureArray::data
char * data()
Pointer to the data in the secure array.
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::SecureArray::SecureArray
SecureArray(const SecureArray &from)
Construct a (shallow) copy of another secure byte array.
QCA::SecureArray::SecureArray
SecureArray(int size, char ch=0)
Construct a secure byte array of the specified length.
QCA::SecureArray::SecureArray
SecureArray(const char *str)
Construct a secure byte array from a string.
QCA::SecureArray::toByteArray
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
QCA::SecureArray::fill
void fill(char fillChar, int fillToPosition=-1)
Fill the data array with a specified character.
QCA::SecureArray::isEmpty
bool isEmpty() const
Test if the array contains any bytes.
QCA::SecureArray::constData
const char * constData() const
Pointer to the data in the secure array.
QCA::SecureArray::at
const char & at(int index) const
Returns a reference to the byte at the index position.
QCA::SecureArray::set
void set(const QByteArray &from)
Assign the contents of a provided byte array to this object.
QCA::SecureArray::clear
void clear()
Clears the contents of the array and makes it empty.
QCA::SecureArray::operator+=
SecureArray & operator+=(const SecureArray &a)
Append a secure byte array to the end of this array.
QCA::SecureArray::operator==
bool operator==(const MemoryRegion &other) const
Equality operator.
QCA::SecureArray::append
SecureArray & append(const SecureArray &a)
Append a secure byte array to the end of this array.
QCA
QCA - the Qt Cryptographic Architecture.
Definition qca_basic.h:41
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_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_secure_alloc
QCA_EXPORT void * qca_secure_alloc(int bytes)
Allocate a block of memory from the secure memory pool.
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.