File: qca_publickey.h

package info (click to toggle)
qca2 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,884 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (1562 lines) | stat: -rw-r--r-- 44,212 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
/*
 * qca_publickey.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
 *
 */

/**
   \file qca_publickey.h

   Header file for PublicKey and PrivateKey related classes

   \note You should not use this header directly from an
   application. You should just use <tt> \#include \<QtCrypto>
   </tt> instead.
*/

#ifndef QCA_PUBLICKEY_H
#define QCA_PUBLICKEY_H

#include "qca_core.h"
#include <QObject>

namespace QCA {

class PublicKey;
class PrivateKey;
class KeyGenerator;
class RSAPublicKey;
class RSAPrivateKey;
class DSAPublicKey;
class DSAPrivateKey;
class DHPublicKey;
class DHPrivateKey;

/**
   Encryption algorithms
*/
enum EncryptionAlgorithm
{
    EME_PKCS1v15,     ///< Block type 2 (PKCS#1, Version 1.5)
    EME_PKCS1_OAEP,   ///< Optimal asymmetric encryption padding (PKCS#1, Version 2.0)
    EME_PKCS1v15_SSL, ///< PKCS#1, Version 1.5 with an SSL-specific modification
    EME_NO_PADDING    ///< Raw RSA encryption
};

/**
   Signature algorithm variants

   Note that most signature algorithms follow a process of first hashing the
   plaintext data to be signed, creating a payload format that wraps the hash
   value (among other things), and then signing the payload with the private
   key.  So, for example, an EMSA3(SHA1) signature outputted by QCA cannot be
   verified by merely performing RSA and SHA1 operations (e.g.
   "openssl rsautl -verify" and comparing with sha1sum), because that would not
   take the EMSA3 payload format into consideration.
*/
enum SignatureAlgorithm
{
    SignatureUnknown, ///< Unknown signing algorithm
    EMSA1_SHA1,       ///< SHA1, with EMSA1 (IEEE1363-2000) encoding (this is the usual DSA algorithm - FIPS186)
    EMSA3_SHA1,       ///< SHA1, with EMSA3 (ie PKCS#1 Version 1.5) encoding
    EMSA3_MD5,        ///< MD5, with EMSA3 (ie PKCS#1 Version 1.5) encoding (this is the usual RSA algorithm)
    EMSA3_MD2,        ///< MD2, with EMSA3 (ie PKCS#1 Version 1.5) encoding
    EMSA3_RIPEMD160,  ///< RIPEMD160, with EMSA3 (ie PKCS#1 Version 1.5) encoding
    EMSA3_Raw,        ///< EMSA3 without computing a message digest or a DigestInfo encoding (identical to PKCS#11's
                      ///< CKM_RSA_PKCS mechanism)
    EMSA3_SHA224,     ///< SHA224, with EMSA3 (ie PKCS#1 Version 1.5) encoding
    EMSA3_SHA256,     ///< SHA256, with EMSA3 (ie PKCS#1 Version 1.5) encoding
    EMSA3_SHA384,     ///< SHA384, with EMSA3 (ie PKCS#1 Version 1.5) encoding
    EMSA3_SHA512      ///< SHA512, with EMSA3 (ie PKCS#1 Version 1.5) encoding
};

/**
   Signature formats (DSA only)
*/
enum SignatureFormat
{
    DefaultFormat, ///< For DSA, this is the same as IEEE_1363
    IEEE_1363,     ///< 40-byte format from IEEE 1363 (Botan/.NET)
    DERSequence    ///< Signature wrapped in DER formatting (OpenSSL/Java)
};

/**
   Password-based encryption
*/
enum PBEAlgorithm
{
    PBEDefault,           ///< Use modern default (same as PBES2_TripleDES_SHA1)
    PBES2_DES_SHA1,       ///< PKCS#5 v2.0 DES/CBC,SHA1
    PBES2_TripleDES_SHA1, ///< PKCS#5 v2.0 TripleDES/CBC,SHA1
    PBES2_AES128_SHA1,    ///< PKCS#5 v2.0 AES-128/CBC,SHA1
    PBES2_AES192_SHA1,    ///< PKCS#5 v2.0 AES-192/CBC,SHA1
    PBES2_AES256_SHA1     ///< PKCS#5 v2.0 AES-256/CBC,SHA1
};

/**
   Return value from a format conversion

   Note that if you are checking for any result other than ConvertGood,
   then you may be introducing a provider specific dependency.
*/
enum ConvertResult
{
    ConvertGood,     ///< Conversion succeeded, results should be valid
    ErrorDecode,     ///< General failure in the decode stage
    ErrorPassphrase, ///< Failure because of incorrect passphrase
    ErrorFile        ///< Failure because of incorrect file
};

/**
   Well known discrete logarithm group sets

   These sets are derived from three main sources:
   Java Cryptographic Extensions,
 <a href="http://www.ietf.org/rfc/rfc2412.txt">RFC2412</a> and
 <a href="http://www.ietf.org/rfc/rfc3526.txt">RFC3526</a>.
*/
enum DLGroupSet
{
    DSA_512,   ///< 512 bit group, for compatibility with JCE
    DSA_768,   ///< 768 bit group, for compatibility with JCE
    DSA_1024,  ///< 1024 bit group, for compatibility with JCE
    IETF_768,  ///< Group 1 from RFC 2412, Section E.1
    IETF_1024, ///< Group 2 from RFC 2412, Section E.2
    IETF_1536, ///< 1536-bit MODP Group ("group 5") from RFC3526 Section 2.
    IETF_2048, ///< 2048-bit MODP Group ("group 14") from RFC3526 Section 3.
    IETF_3072, ///< 3072-bit MODP Group ("group 15") from RFC3526 Section 4.
    IETF_4096, ///< 4096-bit MODP Group ("group 16") from RFC3526 Section 5.
    IETF_6144, ///< 6144-bit MODP Group ("group 17") from RFC3526 Section 6.
    IETF_8192  ///< 8192-bit MODP Group ("group 18") from RFC3526 Section 7.

};

/**
   Encode a hash result in EMSA3 (PKCS#1) format

   This is a convenience function for providers that only have access
   to raw RSA signing (mainly smartcard providers).  This is a built-in
   function of QCA and does not utilize a provider.  SHA1, MD5, MD2,
   and RIPEMD160 are supported.

   \param hashName the hash type used to create the digest
   \param digest the digest to encode in EMSA3 format
   \param size the desired size of the encoding output (-1 for automatic size)
*/
QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);

/**
   \class DLGroup qca_publickey.h QtCrypto

   A discrete logarithm group

   \ingroup UserAPI
*/
class QCA_EXPORT DLGroup
{
public:
    DLGroup();

    /**
       Construct a discrete logarithm group from raw parameters

       \param p the P parameter
       \param q the Q parameter
       \param g the G parameter
    */
    DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);

    /**
       Construct a discrete logarithm group from raw parameters

       \param p the P parameter
       \param g the G parameter
    */
    DLGroup(const BigInteger &p, const BigInteger &g);

    /**
       Standard copy constructor

       \param from the group to copy from
    */
    DLGroup(const DLGroup &from);
    ~DLGroup();

    /**
       Standard assignment operator

       \param from the DLGroup to copy from
    */
    DLGroup &operator=(const DLGroup &from);

    /**
       Provide a list of the supported group sets

       \param provider the provider to report which group sets are
       available. If not specified, all providers will be checked
    */
    static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());

    /**
       Test if the group is empty
    */
    bool isNull() const;

    /**
       Provide the p component of the group
    */
    BigInteger p() const;

    /**
       Provide the q component of the group
    */
    BigInteger q() const;

    /**
       Provide the g component of the group
    */
    BigInteger g() const;

private:
    class Private;
    Private *d;
};

/**
   \class PKey qca_publickey.h QtCrypto

   General superclass for public (PublicKey) and private (PrivateKey) keys
   used with asymmetric encryption techniques.

   \ingroup UserAPI

*/
class QCA_EXPORT PKey : public Algorithm
{
public:
    /**
       Types of public key cryptography keys supported by QCA
    */
    enum Type
    {
        RSA, ///< RSA key
        DSA, ///< DSA key
        DH   ///< Diffie Hellman key
    };

    /**
       Standard constructor
    */
    PKey();

    /**
       Standard copy constructor

       \param from the key to copy from
    */
    PKey(const PKey &from);

    ~PKey() override;

    /**
       Standard assignment operator

       \param from the PKey to copy from
    */
    PKey &operator=(const PKey &from);

    /**
       Test what types of keys are supported.

       Normally you would just test if the capability is present, however
       for PKey, you also need to test which types of keys are available.
       So if you want to figure out if RSA keys are supported, you need to
       do something like:
       \code
if(!QCA::isSupported("pkey") ||
    !QCA::PKey::supportedTypes().contains(QCA::PKey::RSA))
{
    // then there is no RSA key support
}
else
{
    // there is RSA key support
}
       \endcode

       To make things a bit more complex, supportedTypes() only
       checks for basic functionality. If you want to check that
       you can do operations with PEM or DER (eg toPEM(), fromPEM(), and
       the equivalent DER and PEMfile operations, plus anything else
       that uses them, including the constructor form that takes a
       fileName), then you need to check for supportedIOTypes() instead.

       \param provider the name of the provider to use, if a particular
       provider is required.

       \sa supportedIOTypes()
    */
    static QList<Type> supportedTypes(const QString &provider = QString());

    /**
       Test what types of keys are supported for IO operations

       If you are using PKey DER or PEM operations, then you need
       to check for appropriate support using this method. For example,
       if you want to check if you can export or import an RSA key, then
       you need to do something like:
       \code
if(!QCA::isSupported("pkey") ||
    !QCA::PKey::supportedIOTypes().contains(QCA::PKey::RSA))
{
    // then there is no RSA key IO support
}
else
{
    // there is RSA key IO support
}
       \endcode

       Note that if you only want to check for basic functionality
       (ie not PEM or DER import/export), then you can use
       supportedTypes().  There is no need to use both - if the key type
       is supported for IO, then is also supported for basic operations.

       \param provider the name of the provider to use, if a particular
       provider is required.

       \sa supportedTypes()
    */
    static QList<Type> supportedIOTypes(const QString &provider = QString());

    /**
       Test if the key is null (empty)

       \return true if the key is null
    */
    bool isNull() const;

    /**
       Report the Type of key (eg RSA, DSA or Diffie Hellman)

       \sa isRSA, isDSA and isDH for boolean tests.
    */
    Type type() const;

    /**
       Report the number of bits in the key
    */
    int bitSize() const;

    /**
       Test if the key is an RSA key
    */
    bool isRSA() const;

    /**
       Test if the key is a DSA key
    */
    bool isDSA() const;

    /**
       Test if the key is a Diffie Hellman key
    */
    bool isDH() const;

    /**
       Test if the key is a public key
    */
    bool isPublic() const;

    /**
       Test if the key is a private key
    */
    bool isPrivate() const;

    /**
       Test if the key data can be exported.  If the key resides on a
       smart card or other such device, this will likely return false.
    */
    bool canExport() const;

    /**
       Test if the key can be used for key agreement
    */
    bool canKeyAgree() const;

    /**
       Interpret this key as a PublicKey

       \sa toRSAPublicKey(), toDSAPublicKey() and toDHPublicKey()
       for protected forms of this call.
    */
    PublicKey toPublicKey() const;

    /**
       Interpret this key as a PrivateKey
    */
    PrivateKey toPrivateKey() const;

    /**
       test if two keys are equal

       \param a the key to compare with this key
    */
    bool operator==(const PKey &a) const;

    /**
       test if two keys are not equal

       \param a the key to compare with this key
    */
    bool operator!=(const PKey &a) const;

protected:
    /**
       Create a key of the specified type

       \param type the name of the type of key to create
       \param provider the name of the provider to create the key in
    */
    PKey(const QString &type, const QString &provider);

    /**
       Set the key

       \param k the key to assign from
    */
    void set(const PKey &k);

    /**
       Interpret this key as an RSAPublicKey

       \note This function is essentially a convenience cast - if the
       key was created as a DSA key, this function cannot turn it into
       an RSA key.

       \sa toPublicKey() for the public version of this method
    */
    RSAPublicKey toRSAPublicKey() const;

    /**
       Interpret this key as an  RSAPrivateKey

       \note This function is essentially a convenience cast - if the
       key was created as a DSA key, this function cannot turn it into
       a RSA key.

       \sa toPrivateKey() for the public version of this method
    */
    RSAPrivateKey toRSAPrivateKey() const;

    /**
       Interpret this key as an DSAPublicKey

       \note This function is essentially a convenience cast - if the
       key was created as an RSA key, this function cannot turn it into
       a DSA key.

       \sa toPublicKey() for the public version of this method
    */
    DSAPublicKey toDSAPublicKey() const;

    /**
       Interpret this key as a DSAPrivateKey

       \note This function is essentially a convenience cast - if the
       key was created as an RSA key, this function cannot turn it into
       a DSA key.

       \sa toPrivateKey() for the public version of this method
    */
    DSAPrivateKey toDSAPrivateKey() const;

    /**
       Interpret this key as an DHPublicKey

       \note This function is essentially a convenience cast - if the
       key was created as a DSA key, this function cannot turn it into
       a DH key.

       \sa toPublicKey() for the public version of this method
    */
    DHPublicKey toDHPublicKey() const;

    /**
       Interpret this key as a DHPrivateKey

       \note This function is essentially a convenience cast - if the
       key was created as a DSA key, this function cannot turn it into
       a DH key.

       \sa toPrivateKey() for the public version of this method
    */
    DHPrivateKey toDHPrivateKey() const;

private:
    void assignToPublic(PKey *dest) const;
    void assignToPrivate(PKey *dest) const;

    class Private;
    Private *d;
};

/**
   \class PublicKey qca_publickey.h QtCrypto

   Generic public key

   \ingroup UserAPI

*/
class QCA_EXPORT PublicKey : public PKey
{
public:
    /**
       Create an empty (null) public key
    */
    PublicKey();

    /**
       Create a public key based on a specified private key

       \param k the private key to extract the public key parts from
    */
    PublicKey(const PrivateKey &k);

    /**
       Import a public key from a PEM representation in a file

       \param fileName the name of the file containing the public key

       \sa fromPEMFile for an alternative method
    */
    PublicKey(const QString &fileName);

    /**
       Copy constructor

       \param from the PublicKey to copy from
    */
    PublicKey(const PublicKey &from);

    ~PublicKey() override;

    /**
       Assignment operator

       \param from the PublicKey to copy from
    */
    PublicKey &operator=(const PublicKey &from);

    /**
       Convenience method to convert this key to an RSAPublicKey

       Note that if the key is not an RSA key (eg it is DSA or DH),
       then this will produce a null key.
    */
    RSAPublicKey toRSA() const;

    /**
       Convenience method to convert this key to a DSAPublicKey

       Note that if the key is not an DSA key (eg it is RSA or DH),
       then this will produce a null key.
    */
    DSAPublicKey toDSA() const;

    /**
       Convenience method to convert this key to a DHPublicKey

       Note that if the key is not an DH key (eg it is DSA or RSA),
       then this will produce a null key.
    */
    DHPublicKey toDH() const;

    /**
       Test if this key can be used for encryption

       \return true if the key can be used for encryption
    */
    bool canEncrypt() const;

    /**
       Test if this key can be used for decryption

       \return true if the key can be used for decryption
    */
    bool canDecrypt() const;

    /**
       Test if the key can be used for verifying signatures

       \return true of the key can be used for verification
    */
    bool canVerify() const;

    /**
       The maximum message size that can be encrypted with a specified
       algorithm

       \param alg the algorithm to check
    */
    int maximumEncryptSize(EncryptionAlgorithm alg) const;

    /**
       Encrypt a message using a specified algorithm

       \param a the message to encrypt
       \param alg the algorithm to use
    */
    SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);

    /**
       Decrypt the message

       \param in the cipher (encrypted) data
       \param out the plain text data
       \param alg the algorithm to use

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);

    /**
       Initialise the signature verification process

       \param alg the algorithm to use for signing
       \param format the specific format to use, for DSA
    */
    void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);

    /**
       Update the signature verification process with more data

       \param a the array containing the data that should be added to the signature
    */
    void update(const MemoryRegion &a);

    /**
       Check the signature is valid for the message

       The process to check that a signature is correct is shown below:
       \code
// note that pubkey is a PublicKey
if( pubkey.canVerify() )
{
    pubkey.startVerify( QCA::EMSA3_MD5 );
    pubkey.update( theMessage ); // might be called multiple times
    if ( pubkey.validSignature( theSignature ) )
    {
        // then signature is valid
    }
    else
    {
        // then signature is invalid
    }
}
       \endcode

       \param sig the signature to check

       \return true if the signature is correct
    */
    bool validSignature(const QByteArray &sig);

    /**
       Single step message verification

       If you have the whole message to be verified, then this offers a
       more convenient approach to verification.

       \param a the message to check the signature on
       \param sig the signature to be checked
       \param alg the algorithm to use
       \param format the signature format to use, for DSA

       \return true if the signature is valid for the message
    */
    bool verifyMessage(const MemoryRegion &a,
                       const QByteArray   &sig,
                       SignatureAlgorithm  alg,
                       SignatureFormat     format = DefaultFormat);

    /**
       Export the key in Distinguished Encoding Rules (DER) format
    */
    QByteArray toDER() const;

    /**
       Export the key in Privacy Enhanced Mail (PEM) format

       \sa toPEMFile provides a convenient way to save the PEM encoded key
       to a file
       \sa fromPEM provides an inverse of toPEM, converting the PEM
       encoded key back to a PublicKey
    */
    QString toPEM() const;

    /**
       Export the key in Privacy Enhanced Mail (PEM) to a file

       \param fileName the name (and path, if necessary) of the file to
       save the PEM encoded key to.

       \sa toPEM for a version that exports to a QString, which may be
       useful if you need to do more sophisticated handling
       \sa fromPEMFile provides an inverse of toPEMFile, reading a PEM
       encoded key from a file
    */
    bool toPEMFile(const QString &fileName) const;

    /**
       Import a key in Distinguished Encoding Rules (DER) format

       This function takes a binary array, which is assumed to contain a
       public key in DER encoding, and returns the key. Unless you don't
       care whether the import succeeded, you should test the result, as
       shown below.

       \code
QCA::ConvertResult conversionResult;
QCA::PublicKey publicKey = QCA::PublicKey::fromDER(keyArray, &conversionResult);
if (! QCA::ConvertGood == conversionResult)
{
    std::cout << "Public key read failed" << std::endl;
}
       \endcode

       \param a the array containing a DER encoded key
       \param result pointer to a variable, which returns whether the
       conversion succeeded (ConvertGood) or not
       \param provider the name of the provider to use for the import.
    */
    static PublicKey fromDER(const QByteArray &a, ConvertResult *result = nullptr, const QString &provider = QString());

    /**
       Import a key in Privacy Enhanced Mail (PEM) format

       This function takes a string, which is assumed to contain a public
       key in PEM encoding, and returns that key. Unless you don't care
       whether the import succeeded, you should test the result, as shown
       below.

       \code
QCA::ConvertResult conversionResult;
QCA::PublicKey publicKey = QCA::PublicKey::fromPEM(keyAsString, &conversionResult);
if (! QCA::ConvertGood == conversionResult)
{
    std::cout << "Public key read failed" << std::endl;
}
       \endcode

       \param s the string containing a PEM encoded key
       \param result pointer to a variable, which returns whether the
       conversion succeeded (ConvertGood) or not
       \param provider the name of the provider to use for the import.

       \sa toPEM, which provides an inverse of fromPEM()
       \sa fromPEMFile, which provides an import direct from a file.
    */
    static PublicKey fromPEM(const QString &s, ConvertResult *result = nullptr, const QString &provider = QString());

    /**
       Import a key in Privacy Enhanced Mail (PEM) format from a file

       This function takes the name of a file, which is assumed to contain
       a public key in PEM encoding, and returns that key. Unless you
       don't care whether the import succeeded, you should test the
       result, as shown below.

       \code
QCA::ConvertResult conversionResult;
QCA::PublicKey publicKey = QCA::PublicKey::fromPEMFile(fileName, &conversionResult);
if (! QCA::ConvertGood == conversionResult)
{
    std::cout << "Public key read failed" << std::endl;
}
       \endcode

       \param fileName a string containing the name of the file
       \param result pointer to a variable, which returns whether the
       conversion succeeded (ConvertGood) or not
       \param provider the name of the provider to use for the import.

       \sa toPEMFile, which provides an inverse of fromPEMFile()
       \sa fromPEM, which provides an import from a string

       \note there is also a constructor form that can import from a file
    */
    static PublicKey
    fromPEMFile(const QString &fileName, ConvertResult *result = nullptr, const QString &provider = QString());

protected:
    /**
       Create a new key of a specified type

       \param type the type of key to create
       \param provider the provider to use, if required
    */
    PublicKey(const QString &type, const QString &provider);

private:
    class Private;
    Private *d;
};

/**
   \class PrivateKey qca_publickey.h QtCrypto

   Generic private key

   \ingroup UserAPI

*/
class QCA_EXPORT PrivateKey : public PKey
{
public:
    /**
       Create an empty private key
    */
    PrivateKey();

    /**
       Import a private key from a PEM representation in a file

       \param fileName the name of the file containing the private key
       \param passphrase the pass phrase for the private key

       \sa fromPEMFile for an alternative method

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());

    /**
       Copy constructor

       \param from the PrivateKey to copy from
    */
    PrivateKey(const PrivateKey &from);

    ~PrivateKey() override;

    /**
       Assignment operator

       \param from the PrivateKey to copy from
    */
    PrivateKey &operator=(const PrivateKey &from);

    /**
       Interpret / convert the key to an RSA key
    */
    RSAPrivateKey toRSA() const;

    /**
       Interpret / convert the key to a DSA key
    */
    DSAPrivateKey toDSA() const;

    /**
       Interpret / convert the key to a Diffie-Hellman key
    */
    DHPrivateKey toDH() const;

    /**
       Test if this key can be used for decryption

       \return true if the key can be used for decryption
    */
    bool canDecrypt() const;

    /**
       Test if this key can be used for encryption

       \return true if the key can be used for encryption
    */
    bool canEncrypt() const;

    /**
       Test if this key can be used for signing

       \return true if the key can be used to make a signature
    */
    bool canSign() const;

    /**
       The maximum message size that can be encrypted with a specified
       algorithm

       \param alg the algorithm to check
    */
    int maximumEncryptSize(EncryptionAlgorithm alg) const;

    /**
       Decrypt the message

       \param in the cipher (encrypted) data
       \param out the plain text data
       \param alg the algorithm to use

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);

    /**
       Encrypt a message using a specified algorithm

       \param a the message to encrypt
       \param alg the algorithm to use
    */
    SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);

    /**
       Initialise the message signature process

       \param alg the algorithm to use for the message signature process
       \param format the signature format to use, for DSA

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);

    /**
       Update the signature process

       \param a the message to use to update the signature

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    void update(const MemoryRegion &a);

    /**
       The resulting signature

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    QByteArray signature();

    /**
       One step signature process

       \param a the message to sign
       \param alg the algorithm to use for the signature
       \param format the signature format to use, for DSA

       \return the signature

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);

    /**
       Derive a shared secret key from a public key

       \param theirs the public key to derive from
    */
    SymmetricKey deriveKey(const PublicKey &theirs);

    /**
       List the supported Password Based Encryption Algorithms that can be
       used to protect the key.

       \param provider the provider to use, if a particular provider is
       required
    */
    static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());

    /**
       Export the key in Distinguished Encoding Rules (DER) format

       \param passphrase the pass phrase to use to protect the key
       \param pbe the symmetric encryption algorithm to use to protect the
       key

       \sa fromDER provides an inverse of toDER, converting the DER
       encoded key back to a PrivateKey
    */
    SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;

    /**
       Export the key in Privacy Enhanced Mail (PEM) format

       \param passphrase the pass phrase to use to protect the key
       \param pbe the symmetric encryption algorithm to use to protect the
       key

       \sa toPEMFile provides a convenient way to save the PEM encoded key
       to a file
       \sa fromPEM provides an inverse of toPEM, converting the PEM
       encoded key back to a PrivateKey
    */
    QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;

    /**
       Export the key in Privacy Enhanced Mail (PEM) format to a file

       \param fileName the name (and path, if required) that the key
       should be exported to.
       \param passphrase the pass phrase to use to protect the key
       \param pbe the symmetric encryption algorithm to use to protect the
       key

       \return true if the export succeeds

       \sa toPEM provides a convenient way to save the PEM encoded key to
       a file
       \sa fromPEM provides an inverse of toPEM, converting the PEM
       encoded key back to a PrivateKey
    */
    bool toPEMFile(const QString     &fileName,
                   const SecureArray &passphrase = SecureArray(),
                   PBEAlgorithm       pbe        = PBEDefault) const;

    /**
       Import the key from Distinguished Encoding Rules (DER) format

       \param a the array containing the DER representation of the key
       \param passphrase the pass phrase that is used to protect the key
       \param result a pointer to a ConvertResult, that if specified, will
       be set to reflect the result of the import
       \param provider the provider to use, if a particular provider is
       required

       \sa toDER provides an inverse of fromDER, exporting the key to an
       array

       \sa QCA::KeyLoader for an asynchronous loader approach.

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    static PrivateKey fromDER(const SecureArray &a,
                              const SecureArray &passphrase = SecureArray(),
                              ConvertResult     *result     = nullptr,
                              const QString     &provider   = QString());

    /**
       Import the key from Privacy Enhanced Mail (PEM) format

       \param s the string containing the PEM representation of the key
       \param passphrase the pass phrase that is used to protect the key
       \param result a pointer to a ConvertResult, that if specified, will
       be set to reflect the result of the import
       \param provider the provider to use, if a particular provider is
       required

       \sa toPEM provides an inverse of fromPEM, exporting the key to a
       string in PEM encoding.

       \sa QCA::KeyLoader for an asynchronous loader approach.

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    static PrivateKey fromPEM(const QString     &s,
                              const SecureArray &passphrase = SecureArray(),
                              ConvertResult     *result     = nullptr,
                              const QString     &provider   = QString());

    /**
       Import the key in Privacy Enhanced Mail (PEM) format from a file

       \param fileName the name (and path, if required) of the file
       containing the PEM representation of the key
       \param passphrase the pass phrase that is used to protect the key
       \param result a pointer to a ConvertResult, that if specified, will
       be set to reflect the result of the import
       \param provider the provider to use, if a particular provider is
       required

       \sa toPEMFile provides an inverse of fromPEMFile
       \sa fromPEM which allows import from a string

       \sa QCA::KeyLoader for an asynchronous loader approach.

       \note there is also a constructor form, that allows you to create
       the key directly

       \note This synchronous operation may require event handling, and so
       it must not be called from the same thread as an EventHandler.
    */
    static PrivateKey fromPEMFile(const QString     &fileName,
                                  const SecureArray &passphrase = SecureArray(),
                                  ConvertResult     *result     = nullptr,
                                  const QString     &provider   = QString());

protected:
    /**
       Create a new private key

       \param type the type of key to create
       \param provider the provider to use, if a specific provider is
       required.
    */
    PrivateKey(const QString &type, const QString &provider);

private:
    class Private;
    Private *d;
};

/**
   \class KeyGenerator qca_publickey.h QtCrypto

   Class for generating asymmetric key pairs

   This class is used for generating asymmetric keys (public/private key
   pairs).

   \ingroup UserAPI

*/
class QCA_EXPORT KeyGenerator : public QObject
{
    Q_OBJECT
public:
    /**
       Create a new key generator

       \param parent the parent object, if applicable
    */
    KeyGenerator(QObject *parent = nullptr);

    ~KeyGenerator() override;

    /**
       Test whether the key generator is set to operate in blocking mode,
       or not

       \return true if the key generator is in blocking mode

       \sa setBlockingEnabled
    */
    bool blockingEnabled() const;

    /**
       Set whether the key generator is in blocking mode, nor not

       \param b if true, the key generator will be set to operate in
       blocking mode, otherwise it will operate in non-blocking mode

       \sa blockingEnabled()
    */
    void setBlockingEnabled(bool b);

    /**
       Test if the key generator is currently busy, or not

       \return true if the key generator is busy generating a key already
    */
    bool isBusy() const;

    /**
       Generate an RSA key of the specified length

       This method creates both the public key and corresponding private
       key. You almost certainly want to extract the public key part out -
       see PKey::toPublicKey for an easy way.

       Key length is a tricky judgment - using less than 2048 is probably
       being too liberal for long term use. Don't use less than 1024
       without serious analysis.

       \param bits the length of key that is required
       \param exp the exponent - typically 3, 17 or 65537
       \param provider the name of the provider to use, if a particular
       provider is required
    */
    PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());

    /**
       Generate a DSA key

       This method creates both the public key and corresponding private
       key. You almost certainly want to extract the public key part out -
       see PKey::toPublicKey for an easy way.

       \param domain the discrete logarithm group that this key should be
       generated from
       \param provider the name of the provider to use, if a particular
       provider is required

       \note Not every DLGroup makes sense for DSA. You should use one of
       DSA_512, DSA_768 and DSA_1024.
    */
    PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());

    /**
       Generate a Diffie-Hellman key

       This method creates both the public key and corresponding private
       key. You almost certainly want to extract the public key part out -
       see PKey::toPublicKey for an easy way.

       \param domain the discrete logarithm group that this key should be
       generated from
       \param provider the name of the provider to use, if a particular
       provider is required
       \note For compatibility, you should use one of the IETF_ groupsets
       as the domain argument.
    */
    PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());

    /**
       Return the last generated key

       This is really only useful when you are working with non-blocking
       key generation
    */
    PrivateKey key() const;

    /**
       Create a new discrete logarithm group

       \param set the set of discrete logarithm parameters to generate
       from
       \param provider the name of the provider to use, if a particular
       provider is required.
    */
    DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());

    /**
       The current discrete logarithm group
    */
    DLGroup dlGroup() const;

Q_SIGNALS:
    /**
       Emitted when the key generation is complete.

       This is only used in non-blocking mode
    */
    void finished();

private:
    Q_DISABLE_COPY(KeyGenerator)

    class Private;
    friend class Private;
    Private *d;
};

/**
   \class RSAPublicKey qca_publickey.h QtCrypto

   RSA Public Key

   \ingroup UserAPI

*/
class QCA_EXPORT RSAPublicKey : public PublicKey
{
public:
    /**
       Generate an empty RSA public key
    */
    RSAPublicKey();

    /**
       Generate an RSA public key from specified parameters

       \param n the public key value
       \param e the public key exponent
       \param provider the provider to use, if a particular provider is
       required
    */
    RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());

    /**
       Extract the public key components from an RSA private key

       \param k the private key to use as the basis for the public key
    */
    RSAPublicKey(const RSAPrivateKey &k);

    /**
       The public key value

       This value is the actual public key value (the product of p and q,
       the random prime numbers used to generate the RSA key), also known
       as the public modulus.
    */
    BigInteger n() const;

    /**
       The public key exponent

       This value is the exponent chosen in the original key generator
       step
    */
    BigInteger e() const;
};

/**
   \class RSAPrivateKey qca_publickey.h QtCrypto

   RSA Private Key

   \ingroup UserAPI

*/
class QCA_EXPORT RSAPrivateKey : public PrivateKey
{
public:
    /**
       Generate an empty RSA private key
    */
    RSAPrivateKey();

    /**
       Generate an RSA private key from specified parameters

       \param n the public key value
       \param e the public key exponent
       \param p one of the two chosen primes
       \param q the other of the two chosen primes
       \param d inverse of the exponent, modulo (p-1)(q-1)
       \param provider the provider to use, if a particular provider is
       required
    */
    RSAPrivateKey(const BigInteger &n,
                  const BigInteger &e,
                  const BigInteger &p,
                  const BigInteger &q,
                  const BigInteger &d,
                  const QString    &provider = QString());

    /**
       The public key value

       This value is the actual public key value (the product of p and q,
       the random prime numbers used to generate the RSA key), also known
       as the public modulus.
    */
    BigInteger n() const;

    /**
       The public key exponent

       This value is the exponent chosen in the original key generator
       step
    */
    BigInteger e() const;

    /**
       One of the two random primes used to generate the private key
    */
    BigInteger p() const;

    /**
       The second of the two random primes used to generate the private
       key
    */
    BigInteger q() const;

    /**
       The inverse of the exponent, module (p-1)(q-1)
    */
    BigInteger d() const;
};

/**
   \class DSAPublicKey qca_publickey.h QtCrypto

   Digital Signature %Algorithm Public Key

   \ingroup UserAPI

*/
class QCA_EXPORT DSAPublicKey : public PublicKey
{
public:
    /**
       Create an empty DSA public key
    */
    DSAPublicKey();

    /**
       Create a DSA public key

       \param domain the discrete logarithm group to use
       \param y the public random value
       \param provider the provider to use, if a specific provider is
       required
    */
    DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());

    /**
       Create a DSA public key from a specified private key

       \param k the DSA private key to use as the source
    */
    DSAPublicKey(const DSAPrivateKey &k);

    /**
       The discrete logarithm group that is being used
    */
    DLGroup domain() const;

    /**
       The public random value associated with this key
    */
    BigInteger y() const;
};

/**
   \class DSAPrivateKey qca_publickey.h QtCrypto

   Digital Signature %Algorithm Private Key

   \ingroup UserAPI

*/
class QCA_EXPORT DSAPrivateKey : public PrivateKey
{
public:
    /**
       Create an empty DSA private key
    */
    DSAPrivateKey();

    /**
       Create a DSA public key

       \param domain the discrete logarithm group to use
       \param y the public random value
       \param x the private random value
       \param provider the provider to use, if a specific provider is
       required
    */
    DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());

    /**
       The discrete logarithm group that is being used
    */
    DLGroup domain() const;

    /**
       the public random value
    */
    BigInteger y() const;

    /**
       the private random value
    */
    BigInteger x() const;
};

/**
   \class DHPublicKey qca_publickey.h QtCrypto

   Diffie-Hellman Public Key

   \ingroup UserAPI

*/
class QCA_EXPORT DHPublicKey : public PublicKey
{
public:
    /**
        Create an empty Diffie-Hellman public key
    */
    DHPublicKey();

    /**
       Create a Diffie-Hellman public key

       \param domain the discrete logarithm group to use
       \param y the public random value
       \param provider the provider to use, if a specific provider is
       required
    */
    DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());

    /**
       Create a Diffie-Hellman public key from a specified private key

       \param k the Diffie-Hellman private key to use as the source
    */
    DHPublicKey(const DHPrivateKey &k);

    /**
       The discrete logarithm group that is being used
    */
    DLGroup domain() const;

    /**
       The public random value associated with this key
    */
    BigInteger y() const;
};

/**
   \class DHPrivateKey qca_publickey.h QtCrypto

   Diffie-Hellman Private Key

   \ingroup UserAPI

*/
class QCA_EXPORT DHPrivateKey : public PrivateKey
{
public:
    /**
       Create an empty Diffie-Hellman private key
    */
    DHPrivateKey();

    /**
       Create a Diffie-Hellman private key

       \param domain the discrete logarithm group to use
       \param y the public random value
       \param x the private random value
       \param provider the provider to use, if a particular provider is
       required
    */
    DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());

    /**
       The discrete logarithm group that is being used
    */
    DLGroup domain() const;

    /**
       The public random value associated with this key
    */
    BigInteger y() const;

    /**
       The private random value associated with this key
    */
    BigInteger x() const;
};
/*@}*/
}

#endif