Debian Codegraph @

src/lib/crypto/AsymmetricAlgorithm.h

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 AsymmetricAlgorithm.h
29
30 Base class for asymmetric algorithm classes
31 *****************************************************************************/
32
33 #ifndef _SOFTHSM_V2_ASYMMETRICALGORITHM_H
34 #define _SOFTHSM_V2_ASYMMETRICALGORITHM_H
35
36 #include "config.h"
37 #include "AsymmetricKeyPair.h"
38 #include "AsymmetricParameters.h"
39 #include "HashAlgorithm.h"
40 #include "PublicKey.h"
41 #include "PrivateKey.h"
42 #include "RNG.h"
43 #include "SymmetricKey.h"
44
45 struct AsymAlgo
46 {
47 enum Type
48 {
49 Unknown,
50 RSA,
51 DSA,
52 DH,
53 ECDH,
54 ECDSA,
55 GOST,
56 EDDSA
57 };
58 };
59
60 struct AsymMech
61 {
62 enum Type
63 {
64 Unknown,
65 RSA,
66 RSA_MD5_PKCS,
67 RSA_PKCS,
68 RSA_PKCS_OAEP,
69 RSA_SHA1_PKCS,
70 RSA_SHA224_PKCS,
71 RSA_SHA256_PKCS,
72 RSA_SHA384_PKCS,
73 RSA_SHA512_PKCS,
74 RSA_PKCS_PSS,
75 RSA_SHA1_PKCS_PSS,
76 RSA_SHA224_PKCS_PSS,
77 RSA_SHA256_PKCS_PSS,
78 RSA_SHA384_PKCS_PSS,
79 RSA_SHA512_PKCS_PSS,
80 RSA_SSL,
81 DSA,
82 DSA_SHA1,
83 DSA_SHA224,
84 DSA_SHA256,
85 DSA_SHA384,
86 DSA_SHA512,
87 ECDSA,
88 GOST,
89 GOST_GOST,
90 EDDSA
91 };
92 };
93
94 struct AsymRSAMGF
95 {
96 enum Type
97 {
98 Unknown,
99 MGF1_SHA1,
100 MGF1_SHA224,
101 MGF1_SHA256,
102 MGF1_SHA384,
103 MGF1_SHA512
104 };
105 };
106
107 struct RSA_PKCS_PSS_PARAMS
108 {
109 HashAlgo::Type hashAlg;
110 AsymRSAMGF::Type mgf;
111 size_t sLen;
112 };
113
114 class AsymmetricAlgorithm
115 {
116 public:
117 // Base constructors
118 AsymmetricAlgorithm();
119
120 // Destructor
121 virtual ~AsymmetricAlgorithm() { }
122
123 // Signing functions
124 virtual bool sign(PrivateKey* privateKey, const ByteString& dataToSign, ByteString& signature, const AsymMech::Type mechanism, const void* param = NULL, const size_t paramLen = 0);
125 virtual bool signInit(PrivateKey* privateKey, const AsymMech::Type mechanism, const void* param = NULL, const size_t paramLen = 0);
126 virtual bool signUpdate(const ByteString& dataToSign);
127 virtual bool signFinal(ByteString& signature);
128
129 // Verification functions
130 virtual bool verify(PublicKey* publicKey, const ByteString& originalData, const ByteString& signature, const AsymMech::Type mechanism, const void* param = NULL, const size_t paramLen = 0);
131 virtual bool verifyInit(PublicKey* publicKey, const AsymMech::Type mechanism, const void* param = NULL, const size_t paramLen = 0);
132 virtual bool verifyUpdate(const ByteString& originalData);
133 virtual bool verifyFinal(const ByteString& signature);
134
135 // Encryption functions
136 virtual bool encrypt(PublicKey* publicKey, const ByteString& data, ByteString& encryptedData, const AsymMech::Type padding) = 0;
137
138 // Decryption functions
139 virtual bool decrypt(PrivateKey* privateKey, const ByteString& encryptedData, ByteString& data, const AsymMech::Type padding) = 0;
140
141 // Wrap/Unwrap keys
142 bool wrapKey(PublicKey* publicKey, const ByteString& data, ByteString& encryptedData, const AsymMech::Type padding);
143 bool unwrapKey(PrivateKey* privateKey, const ByteString& encryptedData, ByteString& data, const AsymMech::Type padding);
144
145 // Key factory
146 virtual bool generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* rng = NULL) = 0;
147 virtual unsigned long getMinKeySize() = 0;
148 virtual unsigned long getMaxKeySize() = 0;
149 virtual bool generateParameters(AsymmetricParameters** ppParams, void* parameters = NULL, RNG* rng = NULL);
150 virtual bool deriveKey(SymmetricKey **ppSymmetricKey, PublicKey* publicKey, PrivateKey* privateKey);
151 virtual bool reconstructKeyPair(AsymmetricKeyPair** ppKeyPair, ByteString& serialisedData) = 0;
152 virtual bool reconstructPublicKey(PublicKey** ppPublicKey, ByteString& serialisedData) = 0;
153 virtual bool reconstructPrivateKey(PrivateKey** ppPrivateKey, ByteString& serialisedData) = 0;
154 virtual bool reconstructParameters(AsymmetricParameters** ppParams, ByteString& serialisedData);
155 virtual PublicKey* newPublicKey() = 0;
156 virtual PrivateKey* newPrivateKey() = 0;
157 virtual AsymmetricParameters* newParameters();
158
159 // Key recycling -- override these functions in a derived class if you need to perform specific cleanup
160 virtual void recycleKeyPair(AsymmetricKeyPair* toRecycle);
161 virtual void recycleParameters(AsymmetricParameters* toRecycle);
162 virtual void recyclePublicKey(PublicKey* toRecycle);
163 virtual void recyclePrivateKey(PrivateKey* toRecycle);
164 virtual void recycleSymmetricKey(SymmetricKey* toRecycle);
165
166 protected:
167 PublicKey* currentPublicKey;
168 PrivateKey* currentPrivateKey;
169
170 AsymMech::Type currentMechanism;
171 AsymMech::Type currentPadding;
172
173 private:
174 enum
175 {
176 NONE,
177 SIGN,
178 VERIFY
179 }
180 currentOperation;
181
182 bool isWrappingMech(AsymMech::Type padding);
183 };
184
185 #endif // !_SOFTHSM_V2_ASYMMETRICALGORITHM_H
186