File indexing completed on 2024-04-28 04:43:41
0001 /* 0002 * qca_tools.h - Qt Cryptographic Architecture 0003 * Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com> 0004 * Copyright (C) 2004,2005 Brad Hards <bradh@frogmouth.net> 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Lesser General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2.1 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Lesser General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Lesser General Public 0017 * License along with this library; if not, write to the Free Software 0018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0019 * 02110-1301 USA 0020 * 0021 */ 0022 0023 /** 0024 \file qca_tools.h 0025 0026 Header file for "tool" classes used in %QCA 0027 0028 These classes differ from those in qca_support.h, in that they have 0029 some cryptographic relationship, and require secure memory. 0030 0031 \note You should not use this header directly from an 0032 application. You should just use <tt> \#include \<QtCrypto> 0033 </tt> instead. 0034 */ 0035 0036 #ifndef QCA_TOOLS_H 0037 #define QCA_TOOLS_H 0038 0039 #include "qca_export.h" 0040 #include <QMetaType> 0041 #include <QSharedData> 0042 #include <QSharedDataPointer> 0043 0044 class QString; 0045 class QByteArray; 0046 class QTextStream; 0047 0048 /** 0049 Allocate a block of memory from the secure memory pool. 0050 0051 This is intended to be used when working with C libraries. 0052 0053 \param bytes the number of bytes to allocate 0054 */ 0055 QCA_EXPORT void *qca_secure_alloc(int bytes); 0056 0057 /** 0058 Free (de-allocate) a block of memory that has been previously 0059 allocated from the secure memory pool. 0060 0061 This is intended to be used when working with C libraries. 0062 0063 \param p pointer to the block of memory to be free'd 0064 */ 0065 QCA_EXPORT void qca_secure_free(void *p); 0066 0067 /** 0068 Resize (re-allocate) a block of memory that has been previously 0069 allocated from the secure memory pool. 0070 0071 \param p pointer to the block of memory to be resized. 0072 \param bytes the new size that is required. 0073 */ 0074 QCA_EXPORT void *qca_secure_realloc(void *p, int bytes); 0075 0076 namespace QCA { 0077 0078 /** 0079 \class MemoryRegion qca_tools.h QtCrypto 0080 0081 Array of bytes that may be optionally secured 0082 0083 This class is mostly unusable on its own. Either use it as a SecureArray 0084 subclass or call toByteArray() to convert to QByteArray. 0085 0086 Note that this class is implicitly shared (that is, copy on write). 0087 0088 \ingroup UserAPI 0089 */ 0090 class QCA_EXPORT MemoryRegion 0091 { 0092 public: 0093 MemoryRegion(); 0094 0095 /** 0096 Constructs a new Memory Region from a null terminated 0097 character array 0098 0099 \param str pointer to the array of data to copy 0100 */ 0101 MemoryRegion(const char *str); 0102 0103 /** 0104 Constructs a new MemoryRegion from the data in a 0105 byte array 0106 0107 \param from the QByteArray to copy from 0108 */ 0109 MemoryRegion(const QByteArray &from); 0110 0111 /** 0112 Standard copy constructor 0113 0114 \param from the MemoryRegion to copy from 0115 */ 0116 MemoryRegion(const MemoryRegion &from); 0117 ~MemoryRegion(); 0118 0119 /** 0120 Standard assignment operator 0121 0122 \param from the MemoryRegion to copy from 0123 */ 0124 MemoryRegion &operator=(const MemoryRegion &from); 0125 0126 /** 0127 Standard assignment operator 0128 0129 \param from the QByteArray to copy from 0130 */ 0131 MemoryRegion &operator=(const QByteArray &from); 0132 0133 /** 0134 Test if the MemoryRegion is null (i.e. was created 0135 as a null array, and hasn't been resized). 0136 0137 This is probably not what you are trying to do. If 0138 you are trying to determine whether there are any 0139 bytes in the array, use isEmpty() instead. 0140 */ 0141 bool isNull() const; 0142 0143 /** 0144 Test if the MemoryRegion is using secure memory, or not. 0145 0146 In this context, memory is secure if it will not be paged 0147 out to disk. 0148 0149 \return true if the memory region is secure 0150 */ 0151 bool isSecure() const; 0152 0153 /** 0154 Convert this memory region to a byte array. 0155 0156 \note For secure data, this will make it insecure 0157 0158 \sa data() and constData() for other ways to convert 0159 to an "accessible" format. 0160 */ 0161 QByteArray toByteArray() const; 0162 0163 /** 0164 Returns true if the size of the memory region is zero. 0165 */ 0166 bool isEmpty() const; 0167 0168 /** 0169 Returns the number of bytes in the memory region. 0170 */ 0171 int size() const; 0172 0173 /** 0174 Convert the contents of the memory region to 0175 a C-compatible character array. This consists 0176 of size() bytes, followed by a null terminator. 0177 0178 \sa toByteArray for an alternative approach. 0179 \sa constData, which is equivalent to this method, but avoids 0180 the possibility that the compiler picks the wrong version. 0181 */ 0182 const char *data() const; 0183 0184 /** 0185 Convert the contents of the memory region to 0186 a C-compatible character array. This consists 0187 of size() bytes, followed by a null terminator. 0188 0189 \sa toByteArray for an alternative approach. 0190 \sa data which is equivalent to this method 0191 */ 0192 const char *constData() const; 0193 0194 /** 0195 Obtain the value of the memory location at the specified 0196 position. 0197 0198 \param index the offset into the memory region. 0199 0200 \note The contents of a memory region are between 0201 0 and size()-1. The content at position size() is 0202 always a null terminator. 0203 */ 0204 const char &at(int index) const; 0205 0206 protected: 0207 /** 0208 Create a memory region, optionally using secure 0209 storage. 0210 0211 \param secure if this is true, the memory region 0212 will use secure storage. 0213 0214 \note This will create a memory region without 0215 any content (i.e. both isNull() and isEmpty() will 0216 return true. 0217 */ 0218 MemoryRegion(bool secure); 0219 0220 /** 0221 Create a memory region, optionally using secure 0222 storage. 0223 0224 \param size the number of bytes in the memory 0225 region. 0226 \param secure if this is true, the memory region 0227 will use secure storage. 0228 */ 0229 MemoryRegion(int size, bool secure); 0230 0231 /** 0232 Create a memory region, optionally using secure 0233 storage. 0234 0235 This constructor variant allows you to 0236 initialize the memory region from an existing 0237 array. 0238 0239 \param from the byte array to copy from. 0240 \param secure if this is true, the memory region 0241 will use secure storage. 0242 */ 0243 MemoryRegion(const QByteArray &from, bool secure); 0244 0245 /** 0246 Convert the contents of the memory region to 0247 a C-compatible character array. This consists 0248 of size() bytes, followed by a null terminator. 0249 */ 0250 char *data(); 0251 0252 /** 0253 Obtain the value of the memory location at the specified 0254 position. 0255 0256 \param index the offset into the memory region. 0257 0258 \note The contents of a memory region are between 0259 0 and size()-1. The content at position size() is 0260 always a null terminator. 0261 */ 0262 char &at(int index); 0263 0264 /** 0265 Resize the memory region to the specified size. 0266 0267 \param size the new size of the region. 0268 */ 0269 bool resize(int size); 0270 0271 /** 0272 Modify the memory region to match a specified 0273 byte array. This resizes the memory region 0274 as required to match the byte array size. 0275 0276 \param from the byte array to copy from. 0277 \param secure if this is true, the memory region 0278 will use secure storage. 0279 */ 0280 void set(const QByteArray &from, bool secure); 0281 0282 /** 0283 Convert the memory region to use the specified 0284 memory type. 0285 0286 This may involve copying data from secure to 0287 insecure storage, or from insecure to secure 0288 storage. 0289 0290 \param secure if true, use secure memory; otherwise 0291 use insecure memory. 0292 */ 0293 void setSecure(bool secure); 0294 0295 private: 0296 bool _secure; 0297 class Private; 0298 QSharedDataPointer<Private> d; 0299 }; 0300 0301 /** 0302 \class SecureArray qca_tools.h QtCrypto 0303 0304 Secure array of bytes 0305 0306 The %SecureArray provides an array of memory from a pool that is, 0307 at least partly, secure. In this sense, secure means that the contents 0308 of the memory should not be made available to other applications. By 0309 comparison, a QByteArray or QString may be held in pages that might be 0310 swapped to disk or free'd without being cleared first. 0311 0312 Note that this class is implicitly shared (that is, copy on write). 0313 0314 \ingroup UserAPI 0315 */ 0316 class QCA_EXPORT SecureArray : public MemoryRegion 0317 { 0318 public: 0319 /** 0320 Construct a secure byte array, zero length 0321 */ 0322 SecureArray(); 0323 0324 /** 0325 Construct a secure byte array of the specified length 0326 0327 \param size the number of bytes in the array 0328 \param ch the value every byte should be set to 0329 */ 0330 explicit SecureArray(int size, char ch = 0); 0331 0332 /** 0333 Construct a secure byte array from a string 0334 0335 Note that this copies, rather than references the source array. 0336 0337 \param str the source of the data (as a null terminated string). 0338 */ 0339 SecureArray(const char *str); 0340 0341 /** 0342 Construct a secure byte array from a QByteArray 0343 0344 Note that this copies, rather than references the source array. 0345 0346 \param a the source of the data. 0347 0348 \sa operator=() 0349 */ 0350 SecureArray(const QByteArray &a); 0351 0352 /** 0353 Construct a secure byte array from a MemoryRegion 0354 0355 Note that this copies, rather than references the source array 0356 0357 \param a the source of the data. 0358 0359 \sa operator=() 0360 */ 0361 SecureArray(const MemoryRegion &a); 0362 0363 /** 0364 Construct a (shallow) copy of another secure byte array 0365 0366 \param from the source of the data and length. 0367 */ 0368 SecureArray(const SecureArray &from); 0369 0370 ~SecureArray(); 0371 0372 /** 0373 Creates a reference, rather than a deep copy. 0374 0375 \param from the array to reference 0376 */ 0377 SecureArray &operator=(const SecureArray &from); 0378 0379 /** 0380 Creates a copy, rather than references 0381 0382 \param a the array to copy from 0383 */ 0384 SecureArray &operator=(const QByteArray &a); 0385 0386 /** 0387 Clears the contents of the array and makes it empty 0388 */ 0389 void clear(); 0390 0391 /** 0392 Returns a reference to the byte at the index position 0393 0394 \param index the zero-based offset to obtain 0395 */ 0396 char &operator[](int index); 0397 0398 /** 0399 Returns a reference to the byte at the index position 0400 0401 \param index the zero-based offset to obtain 0402 */ 0403 const char &operator[](int index) const; 0404 0405 /** 0406 Pointer to the data in the secure array 0407 0408 You can use this for memcpy and similar functions. If you are trying 0409 to obtain data at a particular offset, you might be better off using 0410 at() or operator[] 0411 */ 0412 char *data(); 0413 0414 /** 0415 Pointer to the data in the secure array 0416 0417 You can use this for memcpy and similar functions. If you are trying 0418 to obtain data at a particular offset, you might be better off using 0419 at() or operator[] 0420 */ 0421 const char *data() const; 0422 0423 /** 0424 Pointer to the data in the secure array 0425 0426 You can use this for memcpy and similar functions. If you are trying 0427 to obtain data at a particular offset, you might be better off using 0428 at() or operator[] 0429 */ 0430 const char *constData() const; 0431 0432 /** 0433 Returns a reference to the byte at the index position 0434 0435 \param index the zero-based offset to obtain 0436 */ 0437 char &at(int index); 0438 0439 /** 0440 Returns a reference to the byte at the index position 0441 0442 \param index the zero-based offset to obtain 0443 */ 0444 const char &at(int index) const; 0445 0446 /** 0447 Returns the number of bytes in the array 0448 */ 0449 int size() const; 0450 0451 /** 0452 Test if the array contains any bytes. 0453 0454 This is equivalent to testing (size() != 0). Note that if 0455 the array is allocated, isEmpty() is false (even if no data 0456 has been added) 0457 0458 \return true if the array has zero length, otherwise false 0459 */ 0460 bool isEmpty() const; 0461 0462 /** 0463 Change the length of this array 0464 If the new length is less than the old length, the extra information 0465 is (safely) discarded. If the new length is equal to or greater than 0466 the old length, the existing data is copied into the array. 0467 0468 \param size the new length 0469 */ 0470 bool resize(int size); 0471 0472 /** 0473 Fill the data array with a specified character 0474 0475 \param fillChar the character to use as the fill 0476 \param fillToPosition the number of characters to fill 0477 to. If not specified (or -1), fills array to 0478 current length. 0479 0480 \note This function does not extend the array - if 0481 you ask for fill beyond the current length, only 0482 the current length will be used. 0483 \note The number of characters is 1 based, so if 0484 you ask for fill('x', 10), it will fill from 0485 */ 0486 void fill(char fillChar, int fillToPosition = -1); 0487 0488 /** 0489 Copy the contents of the secure array out to a 0490 standard QByteArray. Note that this performs a deep copy 0491 of the data. 0492 */ 0493 QByteArray toByteArray() const; 0494 0495 /** 0496 Append a secure byte array to the end of this array 0497 0498 \param a the array to append to this array 0499 */ 0500 SecureArray &append(const SecureArray &a); 0501 0502 /** 0503 Equality operator. Returns true if both arrays have the same 0504 data (and the same length, of course). 0505 0506 \param other the MemoryRegion to compare to 0507 */ 0508 bool operator==(const MemoryRegion &other) const; 0509 0510 /** 0511 Inequality operator. Returns true if both arrays have different 0512 length, or the same length but different data. 0513 0514 \param other the MemoryRegion to compare to 0515 */ 0516 inline bool operator!=(const MemoryRegion &other) const 0517 { 0518 return !(*this == other); 0519 } 0520 0521 /** 0522 Append a secure byte array to the end of this array 0523 0524 \param a the array to append to this array 0525 */ 0526 SecureArray &operator+=(const SecureArray &a); 0527 0528 protected: 0529 /** 0530 Assign the contents of a provided byte array to this 0531 object. 0532 0533 \param from the byte array to copy 0534 */ 0535 void set(const SecureArray &from); 0536 0537 /** 0538 Assign the contents of a provided byte array to this 0539 object. 0540 0541 \param from the byte array to copy 0542 */ 0543 void set(const QByteArray &from); 0544 }; 0545 0546 /** 0547 Returns an array that is the result of concatenating a and b 0548 0549 \param a the string to put at the start of the result 0550 \param b the string to put at the end of the result 0551 */ 0552 QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b); 0553 0554 /** 0555 \class BigInteger qca_tools.h QtCrypto 0556 0557 Arbitrary precision integer 0558 0559 BigInteger provides arbitrary precision integers. 0560 \code 0561 if ( BigInteger("3499543804349") == 0562 BigInteger("38493290803248") + BigInteger( 343 ) ) 0563 { 0564 // do something 0565 } 0566 \endcode 0567 0568 \ingroup UserAPI 0569 */ 0570 class QCA_EXPORT BigInteger 0571 { 0572 public: 0573 /** 0574 Constructor. Creates a new BigInteger, initialised to zero. 0575 */ 0576 BigInteger(); 0577 0578 /** 0579 \overload 0580 0581 \param n an alternative integer initialisation value. 0582 */ 0583 BigInteger(int n); 0584 0585 /** 0586 \overload 0587 0588 \param c an alternative initialisation value, encoded as a character array 0589 0590 \code 0591 BigInteger b ( "9890343" ); 0592 \endcode 0593 */ 0594 BigInteger(const char *c); 0595 0596 /** 0597 \overload 0598 0599 \param s an alternative initialisation value, encoded as a string 0600 */ 0601 BigInteger(const QString &s); 0602 0603 /** 0604 \overload 0605 0606 \param a an alternative initialisation value, encoded as SecureArray 0607 */ 0608 BigInteger(const QCA::SecureArray &a); 0609 0610 /** 0611 \overload 0612 0613 \param from an alternative initialisation value, encoded as a %BigInteger 0614 */ 0615 BigInteger(const BigInteger &from); 0616 0617 ~BigInteger(); 0618 0619 /** 0620 Assignment operator 0621 0622 \param from the BigInteger to copy from 0623 0624 \code 0625 BigInteger a; // a is zero 0626 BigInteger b( 500 ); 0627 a = b; // a is now 500 0628 \endcode 0629 */ 0630 BigInteger &operator=(const BigInteger &from); 0631 0632 /** 0633 \overload 0634 0635 \param s the QString containing an integer representation 0636 0637 \sa bool fromString(const QString &s) 0638 0639 \note it is the application's responsibility to make sure 0640 that the QString represents a valid integer (ie it only 0641 contains numbers and an optional minus sign at the start) 0642 */ 0643 BigInteger &operator=(const QString &s); 0644 0645 /** 0646 Increment in place operator 0647 0648 \param b the amount to increment by 0649 0650 \code 0651 BigInteger a; // a is zero 0652 BigInteger b( 500 ); 0653 a += b; // a is now 500 0654 a += b; // a is now 1000 0655 \endcode 0656 */ 0657 BigInteger &operator+=(const BigInteger &b); 0658 0659 /** 0660 Decrement in place operator 0661 0662 \param b the amount to decrement by 0663 0664 \code 0665 BigInteger a; // a is zero 0666 BigInteger b( 500 ); 0667 a -= b; // a is now -500 0668 a -= b; // a is now -1000 0669 \endcode 0670 */ 0671 BigInteger &operator-=(const BigInteger &b); 0672 0673 /** 0674 Multiply in place operator 0675 0676 \param b the amount to multiply by 0677 */ 0678 BigInteger &operator*=(const BigInteger &b); 0679 0680 /** 0681 Divide in place operator 0682 0683 \param b the amount to divide by 0684 */ 0685 BigInteger &operator/=(const BigInteger &b); 0686 0687 /** 0688 Modulo in place operator 0689 0690 \param b the amount to divide by 0691 */ 0692 BigInteger &operator%=(const BigInteger &b); 0693 0694 /** 0695 Output %BigInteger as a byte array, useful for storage or 0696 transmission. The format is a binary integer in sign-extended 0697 network-byte-order. 0698 0699 \sa void fromArray(const SecureArray &a); 0700 */ 0701 QCA::SecureArray toArray() const; 0702 0703 /** 0704 Assign from an array. The input is expected to be a binary integer 0705 in sign-extended network-byte-order. 0706 0707 \param a a SecureArray that represents an integer 0708 0709 \sa BigInteger(const SecureArray &a); 0710 \sa SecureArray toArray() const; 0711 */ 0712 void fromArray(const QCA::SecureArray &a); 0713 0714 /** 0715 Convert %BigInteger to a QString 0716 0717 \code 0718 QString aString; 0719 BigInteger aBiggishInteger( 5878990 ); 0720 aString = aBiggishInteger.toString(); // aString is now "5878990" 0721 \endcode 0722 */ 0723 QString toString() const; 0724 0725 /** 0726 Assign from a QString 0727 0728 \param s a QString that represents an integer 0729 0730 \note it is the application's responsibility to make sure 0731 that the QString represents a valid integer (ie it only 0732 contains numbers and an optional minus sign at the start) 0733 0734 \sa BigInteger(const QString &s) 0735 \sa BigInteger & operator=(const QString &s) 0736 */ 0737 bool fromString(const QString &s); 0738 0739 /** 0740 Compare this value with another %BigInteger 0741 0742 Normally it is more readable to use one of the operator overloads, 0743 so you don't need to use this method directly. 0744 0745 \param n the BigInteger to compare with 0746 0747 \return zero if the values are the same, negative if the argument 0748 is less than the value of this BigInteger, and positive if the 0749 argument value is greater than this BigInteger 0750 0751 \code 0752 BigInteger a( "400" ); 0753 BigInteger b( "-400" ); 0754 BigInteger c( " 200 " ); 0755 int result; 0756 result = a.compare( b ); // return positive 400 > -400 0757 result = a.compare( c ); // return positive, 400 > 200 0758 result = b.compare( c ); // return negative, -400 < 200 0759 \endcode 0760 */ 0761 int compare(const BigInteger &n) const; 0762 0763 /** 0764 Equality operator. Returns true if the two BigInteger values 0765 are the same, including having the same sign. 0766 0767 \param other the BigInteger to compare to 0768 */ 0769 inline bool operator==(const BigInteger &other) const 0770 { 0771 return (compare(other) == 0); 0772 } 0773 0774 /** 0775 Inequality operator. Returns true if the two BigInteger values 0776 are different in magnitude, sign or both. 0777 0778 \param other the BigInteger to compare to 0779 */ 0780 inline bool operator!=(const BigInteger &other) const 0781 { 0782 return !(*this == other); 0783 } 0784 0785 /** 0786 Less than or equal operator. Returns true if the BigInteger value 0787 on the left hand side is equal to or less than the BigInteger 0788 value on the right hand side. 0789 0790 \param other the BigInteger to compare to 0791 */ 0792 inline bool operator<=(const BigInteger &other) const 0793 { 0794 return (compare(other) <= 0); 0795 } 0796 0797 /** 0798 Greater than or equal operator. Returns true if the BigInteger 0799 value on the left hand side is equal to or greater than the 0800 BigInteger value on the right hand side. 0801 0802 \param other the BigInteger to compare to 0803 */ 0804 inline bool operator>=(const BigInteger &other) const 0805 { 0806 return (compare(other) >= 0); 0807 } 0808 0809 /** 0810 Less than operator. Returns true if the BigInteger value 0811 on the left hand side is less than the BigInteger value 0812 on the right hand side. 0813 0814 \param other the BigInteger to compare to 0815 */ 0816 inline bool operator<(const BigInteger &other) const 0817 { 0818 return (compare(other) < 0); 0819 } 0820 0821 /** 0822 Greater than operator. Returns true if the BigInteger value 0823 on the left hand side is greater than the BigInteger value 0824 on the right hand side. 0825 0826 \param other the BigInteger to compare to 0827 */ 0828 inline bool operator>(const BigInteger &other) const 0829 { 0830 return (compare(other) > 0); 0831 } 0832 0833 private: 0834 class Private; 0835 QSharedDataPointer<Private> d; 0836 }; 0837 0838 /** 0839 Stream operator 0840 0841 \param stream the stream to write to 0842 \param b the integer to write to the stream 0843 0844 \relates BigInteger 0845 */ 0846 QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b); 0847 0848 } 0849 0850 #endif