Qt Cryptographic Architecture
qca_publickey.h
Go to the documentation of this file.
1 /*
2  * qca_publickey.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 
33 #ifndef QCA_PUBLICKEY_H
34 #define QCA_PUBLICKEY_H
35 
36 #include <QObject>
37 #include "qca_core.h"
38 
39 namespace QCA {
40 
41 class PublicKey;
42 class PrivateKey;
43 class KeyGenerator;
44 class RSAPublicKey;
45 class RSAPrivateKey;
46 class DSAPublicKey;
47 class DSAPrivateKey;
48 class DHPublicKey;
49 class DHPrivateKey;
50 
55 {
60 };
61 
74 {
86 };
87 
92 {
96 };
97 
102 {
109 };
110 
118 {
123 };
124 
134 {
146 
147 };
148 
161 QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);
162 
170 class QCA_EXPORT DLGroup
171 {
172 public:
173  DLGroup();
174 
182  DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
183 
190  DLGroup(const BigInteger &p, const BigInteger &g);
191 
197  DLGroup(const DLGroup &from);
198  ~DLGroup();
199 
205  DLGroup & operator=(const DLGroup &from);
206 
213  static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
214 
218  bool isNull() const;
219 
223  BigInteger p() const;
224 
228  BigInteger q() const;
229 
233  BigInteger g() const;
234 
235 private:
236  class Private;
237  Private *d;
238 };
239 
249 class QCA_EXPORT PKey : public Algorithm
250 {
251 public:
255  enum Type {
256  RSA,
257  DSA,
258  DH
259  };
260 
264  PKey();
265 
271  PKey(const PKey &from);
272 
273  ~PKey();
274 
280  PKey & operator=(const PKey &from);
281 
313  static QList<Type> supportedTypes(const QString &provider = QString());
314 
344  static QList<Type> supportedIOTypes(const QString &provider = QString());
345 
351  bool isNull() const;
352 
358  Type type() const;
359 
363  int bitSize() const;
364 
368  bool isRSA() const;
369 
373  bool isDSA() const;
374 
378  bool isDH() const;
379 
383  bool isPublic() const;
384 
388  bool isPrivate() const;
389 
394  bool canExport() const;
395 
399  bool canKeyAgree() const;
400 
407  PublicKey toPublicKey() const;
408 
412  PrivateKey toPrivateKey() const;
413 
419  bool operator==(const PKey &a) const;
420 
426  bool operator!=(const PKey &a) const;
427 
428 protected:
435  PKey(const QString &type, const QString &provider);
436 
442  void set(const PKey &k);
443 
453  RSAPublicKey toRSAPublicKey() const;
454 
464  RSAPrivateKey toRSAPrivateKey() const;
465 
475  DSAPublicKey toDSAPublicKey() const;
476 
486  DSAPrivateKey toDSAPrivateKey() const;
487 
497  DHPublicKey toDHPublicKey() const;
498 
508  DHPrivateKey toDHPrivateKey() const;
509 
510 private:
511  void assignToPublic(PKey *dest) const;
512  void assignToPrivate(PKey *dest) const;
513 
514  class Private;
515  Private *d;
516 };
517 
526 class QCA_EXPORT PublicKey : public PKey
527 {
528 public:
532  PublicKey();
533 
539  PublicKey(const PrivateKey &k);
540 
548  PublicKey(const QString &fileName);
549 
555  PublicKey(const PublicKey &from);
556 
557  ~PublicKey();
558 
564  PublicKey & operator=(const PublicKey &from);
565 
572  RSAPublicKey toRSA() const;
573 
580  DSAPublicKey toDSA() const;
581 
588  DHPublicKey toDH() const;
589 
595  bool canEncrypt() const;
596 
602  bool canDecrypt() const;
603 
609  bool canVerify() const;
610 
617  int maximumEncryptSize(EncryptionAlgorithm alg) const;
618 
625  SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
626 
637  bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
638 
645  void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
646 
652  void update(const MemoryRegion &a);
653 
679  bool validSignature(const QByteArray &sig);
680 
694  bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
695 
699  QByteArray toDER() const;
700 
709  QString toPEM() const;
710 
722  bool toPEMFile(const QString &fileName) const;
723 
746  static PublicKey fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
747 
773  static PublicKey fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
774 
802  static PublicKey fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
803 
804 protected:
811  PublicKey(const QString &type, const QString &provider);
812 
813 private:
814  class Private;
815  Private *d;
816 };
817 
826 class QCA_EXPORT PrivateKey : public PKey
827 {
828 public:
832  PrivateKey();
833 
845  explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
846 
852  PrivateKey(const PrivateKey &from);
853 
854  ~PrivateKey();
855 
861  PrivateKey & operator=(const PrivateKey &from);
862 
866  RSAPrivateKey toRSA() const;
867 
871  DSAPrivateKey toDSA() const;
872 
876  DHPrivateKey toDH() const;
877 
883  bool canDecrypt() const;
884 
890  bool canEncrypt() const;
891 
897  bool canSign() const;
898 
905  int maximumEncryptSize(EncryptionAlgorithm alg) const;
906 
917  bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
918 
925  SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
926 
936  void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
937 
946  void update(const MemoryRegion &a);
947 
954  QByteArray signature();
955 
968  QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
969 
975  SymmetricKey deriveKey(const PublicKey &theirs);
976 
984  static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
985 
996  SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
997 
1010  QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
1011 
1028  bool toPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
1029 
1048  static PrivateKey fromDER(const SecureArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
1049 
1068  static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
1069 
1092  static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
1093 
1094 protected:
1102  PrivateKey(const QString &type, const QString &provider);
1103 
1104 private:
1105  class Private;
1106  Private *d;
1107 };
1108 
1120 class QCA_EXPORT KeyGenerator : public QObject
1121 {
1122  Q_OBJECT
1123 public:
1129  KeyGenerator(QObject *parent = 0);
1130 
1131  ~KeyGenerator();
1132 
1141  bool blockingEnabled() const;
1142 
1151  void setBlockingEnabled(bool b);
1152 
1158  bool isBusy() const;
1159 
1176  PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
1177 
1193  PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
1194 
1209  PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
1210 
1217  PrivateKey key() const;
1218 
1227  DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
1228 
1232  DLGroup dlGroup() const;
1233 
1234 Q_SIGNALS:
1240  void finished();
1241 
1242 private:
1243  Q_DISABLE_COPY(KeyGenerator)
1244 
1245  class Private;
1246  friend class Private;
1247  Private *d;
1248 };
1249 
1258 class QCA_EXPORT RSAPublicKey : public PublicKey
1259 {
1260 public:
1264  RSAPublicKey();
1265 
1274  RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
1275 
1281  RSAPublicKey(const RSAPrivateKey &k);
1282 
1290  BigInteger n() const;
1291 
1298  BigInteger e() const;
1299 };
1300 
1309 class QCA_EXPORT RSAPrivateKey : public PrivateKey
1310 {
1311 public:
1315  RSAPrivateKey();
1316 
1328  RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider = QString());
1329 
1337  BigInteger n() const;
1338 
1345  BigInteger e() const;
1346 
1350  BigInteger p() const;
1351 
1356  BigInteger q() const;
1357 
1361  BigInteger d() const;
1362 };
1363 
1372 class QCA_EXPORT DSAPublicKey : public PublicKey
1373 {
1374 public:
1378  DSAPublicKey();
1379 
1388  DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
1389 
1395  DSAPublicKey(const DSAPrivateKey &k);
1396 
1400  DLGroup domain() const;
1401 
1405  BigInteger y() const;
1406 };
1407 
1416 class QCA_EXPORT DSAPrivateKey : public PrivateKey
1417 {
1418 public:
1422  DSAPrivateKey();
1423 
1433  DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
1434 
1438  DLGroup domain() const;
1439 
1443  BigInteger y() const;
1444 
1448  BigInteger x() const;
1449 };
1450 
1459 class QCA_EXPORT DHPublicKey : public PublicKey
1460 {
1461 public:
1465  DHPublicKey();
1466 
1475  DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
1476 
1482  DHPublicKey(const DHPrivateKey &k);
1483 
1487  DLGroup domain() const;
1488 
1492  BigInteger y() const;
1493 };
1494 
1503 class QCA_EXPORT DHPrivateKey : public PrivateKey
1504 {
1505 public:
1509  DHPrivateKey();
1510 
1520  DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
1521 
1525  DLGroup domain() const;
1526 
1530  BigInteger y() const;
1531 
1535  BigInteger x() const;
1536 };
1538 }
1539 
1540 #endif
ConvertResult
Return value from a format conversion.
Definition: qca_publickey.h:117
For DSA, this is the same as IEEE_1363.
Definition: qca_publickey.h:93
PBEAlgorithm
Password-based encryption.
Definition: qca_publickey.h:101
General superclass for an algorithm.
Definition: qca_core.h:1121
4096-bit MODP Group ("group 16") from RFC3526 Section 5.
Definition: qca_publickey.h:143
RSA Public Key.
Definition: qca_publickey.h:1258
Generic private key.
Definition: qca_publickey.h:826
SHA384, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:84
General failure in the decode stage.
Definition: qca_publickey.h:120
Group 1 from RFC 2412, Section E.1.
Definition: qca_publickey.h:138
SHA256, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:83
RSA key.
Definition: qca_publickey.h:256
PKCS#5 v2.0 AES-256/CBC,SHA1.
Definition: qca_publickey.h:108
1024 bit group, for compatibility with JCE
Definition: qca_publickey.h:137
Unknown signing algorithm.
Definition: qca_publickey.h:75
QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size=-1)
Encode a hash result in EMSA3 (PKCS#1) format.
Digital Signature Algorithm Private Key.
Definition: qca_publickey.h:1416
Raw RSA encryption.
Definition: qca_publickey.h:59
RSA Private Key.
Definition: qca_publickey.h:1309
Conversion succeeded, results should be valid.
Definition: qca_publickey.h:119
Block type 2 (PKCS#1, Version 1.5)
Definition: qca_publickey.h:56
Diffie-Hellman Private Key.
Definition: qca_publickey.h:1503
768 bit group, for compatibility with JCE
Definition: qca_publickey.h:136
SHA1, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:77
Digital Signature Algorithm Public Key.
Definition: qca_publickey.h:1372
3072-bit MODP Group ("group 15") from RFC3526 Section 4.
Definition: qca_publickey.h:142
SHA512, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:85
2048-bit MODP Group ("group 14") from RFC3526 Section 3.
Definition: qca_publickey.h:141
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1221
Signature wrapped in DER formatting (OpenSSL/Java)
Definition: qca_publickey.h:95
PKCS#5 v2.0 AES-128/CBC,SHA1.
Definition: qca_publickey.h:106
40-byte format from IEEE 1363 (Botan/.NET)
Definition: qca_publickey.h:94
Optimal asymmetric encryption padding (PKCS#1, Version 2.0)
Definition: qca_publickey.h:57
Header file for core QCA infrastructure.
Failure because of incorrect passphrase.
Definition: qca_publickey.h:121
SignatureAlgorithm
Signature algorithm variants.
Definition: qca_publickey.h:73
A discrete logarithm group.
Definition: qca_publickey.h:170
DLGroupSet
Well known discrete logarithm group sets.
Definition: qca_publickey.h:133
MD5, with EMSA3 (ie PKCS#1 Version 1.5) encoding (this is the usual RSA algorithm) ...
Definition: qca_publickey.h:78
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:47
EMSA3 without computing a message digest or a DigestInfo encoding (identical to PKCS#11's CKM_RSA_PKC...
Definition: qca_publickey.h:81
Failure because of incorrect file.
Definition: qca_publickey.h:122
SHA1, with EMSA1 (IEEE1363-2000) encoding (this is the usual DSA algorithm - FIPS186) ...
Definition: qca_publickey.h:76
SignatureFormat
Signature formats (DSA only)
Definition: qca_publickey.h:91
Type
Types of public key cryptography keys supported by QCA.
Definition: qca_publickey.h:255
Secure array of bytes.
Definition: qca_tools.h:316
8192-bit MODP Group ("group 18") from RFC3526 Section 7.
Definition: qca_publickey.h:145
Group 2 from RFC 2412, Section E.2.
Definition: qca_publickey.h:139
PKCS#5 v2.0 DES/CBC,SHA1.
Definition: qca_publickey.h:104
512 bit group, for compatibility with JCE
Definition: qca_publickey.h:135
DSA key.
Definition: qca_publickey.h:257
EncryptionAlgorithm
Encryption algorithms.
Definition: qca_publickey.h:54
Class for generating asymmetric key pairs.
Definition: qca_publickey.h:1120
General superclass for public (PublicKey) and private (PrivateKey) keys used with asymmetric encrypti...
Definition: qca_publickey.h:249
SHA224, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:82
1536-bit MODP Group ("group 5") from RFC3526 Section 2.
Definition: qca_publickey.h:140
Arbitrary precision integer.
Definition: qca_tools.h:570
PKCS#1, Version 1.5 with an SSL-specific modification.
Definition: qca_publickey.h:58
Generic public key.
Definition: qca_publickey.h:526
MD2, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:79
Diffie-Hellman Public Key.
Definition: qca_publickey.h:1459
PKCS#5 v2.0 AES-192/CBC,SHA1.
Definition: qca_publickey.h:107
RIPEMD160, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:80
PKCS#5 v2.0 TripleDES/CBC,SHA1.
Definition: qca_publickey.h:105
Array of bytes that may be optionally secured.
Definition: qca_tools.h:90
Use modern default (same as PBES2_TripleDES_SHA1)
Definition: qca_publickey.h:103
6144-bit MODP Group ("group 17") from RFC3526 Section 6.
Definition: qca_publickey.h:144