00001 /* 00002 * qca_tools.h - Qt Cryptographic Architecture 00003 * Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com> 00004 * Copyright (C) 2004,2005 Brad Hards <bradh@frogmouth.net> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 * 02110-1301 USA 00020 * 00021 */ 00022 00036 #ifndef QCA_TOOLS_H 00037 #define QCA_TOOLS_H 00038 00039 #include <QSharedData> 00040 #include <QSharedDataPointer> 00041 #include <QMetaType> 00042 #include "qca_export.h" 00043 00044 class QString; 00045 class QByteArray; 00046 class QTextStream; 00047 00055 QCA_EXPORT void *qca_secure_alloc(int bytes); 00056 00065 QCA_EXPORT void qca_secure_free(void *p); 00066 00074 QCA_EXPORT void *qca_secure_realloc(void *p, int bytes); 00075 00076 namespace QCA { 00077 00090 class QCA_EXPORT MemoryRegion 00091 { 00092 public: 00093 MemoryRegion(); 00094 00101 MemoryRegion(const char *str); 00102 00109 MemoryRegion(const QByteArray &from); 00110 00116 MemoryRegion(const MemoryRegion &from); 00117 ~MemoryRegion(); 00118 00124 MemoryRegion & operator=(const MemoryRegion &from); 00125 00131 MemoryRegion & operator=(const QByteArray &from); 00132 00141 bool isNull() const; 00142 00151 bool isSecure() const; 00152 00161 QByteArray toByteArray() const; 00162 00166 bool isEmpty() const; 00167 00171 int size() const; 00172 00182 const char *data() const; 00183 00192 const char *constData() const; 00193 00204 const char & at(int index) const; 00205 00206 protected: 00218 MemoryRegion(bool secure); 00219 00229 MemoryRegion(int size, bool secure); 00230 00243 MemoryRegion(const QByteArray &from, bool secure); 00244 00250 char *data(); 00251 00262 char & at(int index); 00263 00269 bool resize(int size); 00270 00280 void set(const QByteArray &from, bool secure); 00281 00293 void setSecure(bool secure); 00294 00295 private: 00296 bool _secure; 00297 class Private; 00298 QSharedDataPointer<Private> d; 00299 }; 00300 00316 class QCA_EXPORT SecureArray : public MemoryRegion 00317 { 00318 public: 00322 SecureArray(); 00323 00330 explicit SecureArray(int size, char ch = 0); 00331 00339 SecureArray(const char *str); 00340 00350 SecureArray(const QByteArray &a); 00351 00361 SecureArray(const MemoryRegion &a); 00362 00368 SecureArray(const SecureArray &from); 00369 00370 ~SecureArray(); 00371 00377 SecureArray & operator=(const SecureArray &from); 00378 00384 SecureArray & operator=(const QByteArray &a); 00385 00389 void clear(); 00390 00396 char & operator[](int index); 00397 00403 const char & operator[](int index) const; 00404 00412 char *data(); 00413 00421 const char *data() const; 00422 00430 const char *constData() const; 00431 00437 char & at(int index); 00438 00444 const char & at(int index) const; 00445 00449 int size() const; 00450 00460 bool isEmpty() const; 00461 00470 bool resize(int size); 00471 00486 void fill(char fillChar, int fillToPosition = -1); 00487 00493 QByteArray toByteArray() const; 00494 00500 SecureArray & append(const SecureArray &a); 00501 00508 bool operator==(const MemoryRegion &other) const; 00509 00516 inline bool operator!=(const MemoryRegion &other) const 00517 { 00518 return !(*this == other); 00519 } 00520 00526 SecureArray & operator+=(const SecureArray &a); 00527 00528 protected: 00535 void set(const SecureArray &from); 00536 00543 void set(const QByteArray &from); 00544 }; 00545 00552 QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b); 00553 00570 class QCA_EXPORT BigInteger 00571 { 00572 public: 00576 BigInteger(); 00577 00583 BigInteger(int n); 00584 00594 BigInteger(const char *c); 00595 00601 BigInteger(const QString &s); 00602 00608 BigInteger(const QCA::SecureArray &a); 00609 00615 BigInteger(const BigInteger &from); 00616 00617 ~BigInteger(); 00618 00630 BigInteger & operator=(const BigInteger &from); 00631 00643 BigInteger & operator=(const QString &s); 00644 00657 BigInteger & operator+=(const BigInteger &b); 00658 00671 BigInteger & operator-=(const BigInteger &b); 00672 00678 BigInteger & operator*=(const BigInteger &b); 00679 00685 BigInteger & operator/=(const BigInteger &b); 00686 00692 BigInteger & operator%=(const BigInteger &b); 00693 00701 QCA::SecureArray toArray() const; 00702 00712 void fromArray(const QCA::SecureArray &a); 00713 00723 QString toString() const; 00724 00737 bool fromString(const QString &s); 00738 00761 int compare(const BigInteger &n) const; 00762 00769 inline bool operator==(const BigInteger &other) const 00770 { 00771 return (compare(other) == 0); 00772 } 00773 00780 inline bool operator!=(const BigInteger &other) const 00781 { 00782 return !(*this == other); 00783 } 00784 00792 inline bool operator<=(const BigInteger &other) const 00793 { 00794 return (compare(other) <= 0); 00795 } 00796 00804 inline bool operator>=(const BigInteger &other) const 00805 { 00806 return (compare(other) >= 0); 00807 } 00808 00816 inline bool operator<(const BigInteger &other) const 00817 { 00818 return (compare(other) < 0); 00819 } 00820 00828 inline bool operator>(const BigInteger &other) const 00829 { 00830 return (compare(other) > 0); 00831 } 00832 00833 private: 00834 class Private; 00835 QSharedDataPointer<Private> d; 00836 }; 00837 00838 00839 00848 QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b); 00849 00850 00851 } 00852 00853 #endif