sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* ** IMPORTANT ** API's in this file are published to 3rd party developers via the
|
sl@0
|
16 |
* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
|
sl@0
|
17 |
* Asymmetric crypto implementation
|
sl@0
|
18 |
*
|
sl@0
|
19 |
*/
|
sl@0
|
20 |
|
sl@0
|
21 |
|
sl@0
|
22 |
/**
|
sl@0
|
23 |
@file
|
sl@0
|
24 |
@publishedAll
|
sl@0
|
25 |
@released
|
sl@0
|
26 |
*/
|
sl@0
|
27 |
|
sl@0
|
28 |
#ifndef __ASYMMETRIC_H__
|
sl@0
|
29 |
#define __ASYMMETRIC_H__
|
sl@0
|
30 |
|
sl@0
|
31 |
#include <padding.h>
|
sl@0
|
32 |
#include <asymmetrickeys.h>
|
sl@0
|
33 |
#include <random.h>
|
sl@0
|
34 |
#include <hash.h>
|
sl@0
|
35 |
|
sl@0
|
36 |
// All the classes in this file have their default constructors and
|
sl@0
|
37 |
// assignment operators defined private, but not implemented, in order to
|
sl@0
|
38 |
// prevent their use.
|
sl@0
|
39 |
|
sl@0
|
40 |
/**
|
sl@0
|
41 |
* Mixin class defining common operations for public key encryption and
|
sl@0
|
42 |
* decryption classes.
|
sl@0
|
43 |
*
|
sl@0
|
44 |
*/
|
sl@0
|
45 |
class MCryptoSystem
|
sl@0
|
46 |
{
|
sl@0
|
47 |
public:
|
sl@0
|
48 |
/**
|
sl@0
|
49 |
* Gets the maximum size of input accepted by this object.
|
sl@0
|
50 |
*
|
sl@0
|
51 |
* @return The maximum input length allowed in bytes.
|
sl@0
|
52 |
*/
|
sl@0
|
53 |
virtual TInt MaxInputLength(void) const = 0;
|
sl@0
|
54 |
|
sl@0
|
55 |
/**
|
sl@0
|
56 |
* Gets the maximum size of output that can be generated by this object.
|
sl@0
|
57 |
*
|
sl@0
|
58 |
* @return The maximum output length in bytes.
|
sl@0
|
59 |
*/
|
sl@0
|
60 |
virtual TInt MaxOutputLength(void) const = 0;
|
sl@0
|
61 |
protected:
|
sl@0
|
62 |
/**
|
sl@0
|
63 |
* Constructor
|
sl@0
|
64 |
*/
|
sl@0
|
65 |
IMPORT_C MCryptoSystem(void);
|
sl@0
|
66 |
private:
|
sl@0
|
67 |
MCryptoSystem(const MCryptoSystem&);
|
sl@0
|
68 |
MCryptoSystem& operator=(const MCryptoSystem&);
|
sl@0
|
69 |
};
|
sl@0
|
70 |
|
sl@0
|
71 |
/**
|
sl@0
|
72 |
* Abstract base class for all public key encryptors.
|
sl@0
|
73 |
*
|
sl@0
|
74 |
*/
|
sl@0
|
75 |
class CEncryptor : public CBase, public MCryptoSystem
|
sl@0
|
76 |
{
|
sl@0
|
77 |
public:
|
sl@0
|
78 |
/**
|
sl@0
|
79 |
* Encrypts the specified plaintext into ciphertext.
|
sl@0
|
80 |
*
|
sl@0
|
81 |
* @param aInput The plaintext
|
sl@0
|
82 |
* @param aOutput On return, the ciphertext
|
sl@0
|
83 |
*
|
sl@0
|
84 |
* @panic KCryptoPanic If the input data is too long.
|
sl@0
|
85 |
* See ECryptoPanicInputTooLarge
|
sl@0
|
86 |
* @panic KCryptoPanic If the supplied output descriptor is not large enough to store the result.
|
sl@0
|
87 |
* See ECryptoPanicOutputDescriptorOverflow
|
sl@0
|
88 |
*/
|
sl@0
|
89 |
virtual void EncryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
|
sl@0
|
90 |
protected:
|
sl@0
|
91 |
/** Default constructor */
|
sl@0
|
92 |
IMPORT_C CEncryptor(void);
|
sl@0
|
93 |
private:
|
sl@0
|
94 |
CEncryptor(const CEncryptor&);
|
sl@0
|
95 |
CEncryptor& operator=(const CEncryptor&);
|
sl@0
|
96 |
};
|
sl@0
|
97 |
|
sl@0
|
98 |
/**
|
sl@0
|
99 |
* Abstract base class for all public key decryptors.
|
sl@0
|
100 |
*
|
sl@0
|
101 |
*/
|
sl@0
|
102 |
class CDecryptor : public CBase, public MCryptoSystem
|
sl@0
|
103 |
{
|
sl@0
|
104 |
public:
|
sl@0
|
105 |
/**
|
sl@0
|
106 |
* Decrypts the specified ciphertext into plaintext
|
sl@0
|
107 |
*
|
sl@0
|
108 |
* @param aInput The ciphertext to be decrypted
|
sl@0
|
109 |
* @param aOutput On return, the plaintext
|
sl@0
|
110 |
*
|
sl@0
|
111 |
* @panic KCryptoPanic If the input data is too long.
|
sl@0
|
112 |
* See ECryptoPanicInputTooLarge
|
sl@0
|
113 |
* @panic KCryptoPanic If the supplied output descriptor is not large enough to store the result.
|
sl@0
|
114 |
* See ECryptoPanicOutputDescriptorOverflow
|
sl@0
|
115 |
*/
|
sl@0
|
116 |
virtual void DecryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
|
sl@0
|
117 |
protected:
|
sl@0
|
118 |
/** Default constructor */
|
sl@0
|
119 |
IMPORT_C CDecryptor(void);
|
sl@0
|
120 |
private:
|
sl@0
|
121 |
CDecryptor(const CDecryptor&);
|
sl@0
|
122 |
CDecryptor& operator=(const CDecryptor&);
|
sl@0
|
123 |
};
|
sl@0
|
124 |
|
sl@0
|
125 |
/**
|
sl@0
|
126 |
* Implementation of RSA encryption as described in PKCS#1 v1.5.
|
sl@0
|
127 |
*
|
sl@0
|
128 |
*/
|
sl@0
|
129 |
class CRSAPKCS1v15Encryptor : public CEncryptor
|
sl@0
|
130 |
{
|
sl@0
|
131 |
public:
|
sl@0
|
132 |
/**
|
sl@0
|
133 |
* Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
|
sl@0
|
134 |
*
|
sl@0
|
135 |
* @param aKey The RSA encryption key
|
sl@0
|
136 |
* @return A pointer to a new CRSAPKCS1v15Encryptor object
|
sl@0
|
137 |
*
|
sl@0
|
138 |
* @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
|
sl@0
|
139 |
* cipher strength restrictions of the crypto library.
|
sl@0
|
140 |
* See TCrypto::IsAsymmetricWeakEnoughL()
|
sl@0
|
141 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
142 |
*/
|
sl@0
|
143 |
IMPORT_C static CRSAPKCS1v15Encryptor* NewL(const CRSAPublicKey& aKey);
|
sl@0
|
144 |
|
sl@0
|
145 |
/**
|
sl@0
|
146 |
* Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
|
sl@0
|
147 |
*
|
sl@0
|
148 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
149 |
*
|
sl@0
|
150 |
* @param aKey The RSA encryption key
|
sl@0
|
151 |
* @return A pointer to a new CRSAPKCS1v15Encryptor object
|
sl@0
|
152 |
*
|
sl@0
|
153 |
* @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
|
sl@0
|
154 |
* cipher strength restrictions of the crypto library.
|
sl@0
|
155 |
* See TCrypto::IsAsymmetricWeakEnoughL()
|
sl@0
|
156 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
157 |
*/
|
sl@0
|
158 |
IMPORT_C static CRSAPKCS1v15Encryptor* NewLC(const CRSAPublicKey& aKey);
|
sl@0
|
159 |
void EncryptL(const TDesC8& aInput, TDes8& aOutput) const;
|
sl@0
|
160 |
TInt MaxInputLength(void) const;
|
sl@0
|
161 |
TInt MaxOutputLength(void) const;
|
sl@0
|
162 |
/** The destructor frees all resources owned by the object, prior to its destruction. */
|
sl@0
|
163 |
virtual ~CRSAPKCS1v15Encryptor(void);
|
sl@0
|
164 |
protected:
|
sl@0
|
165 |
/** @internalAll */
|
sl@0
|
166 |
CRSAPKCS1v15Encryptor(const CRSAPublicKey& aKey);
|
sl@0
|
167 |
/** @internalAll */
|
sl@0
|
168 |
void ConstructL(void);
|
sl@0
|
169 |
protected:
|
sl@0
|
170 |
/** The RSA public key */
|
sl@0
|
171 |
const CRSAPublicKey& iPublicKey;
|
sl@0
|
172 |
/** The PKCS#1 v1.5 encryption padding */
|
sl@0
|
173 |
CPaddingPKCS1Encryption* iPadding;
|
sl@0
|
174 |
private:
|
sl@0
|
175 |
CRSAPKCS1v15Encryptor(const CRSAPKCS1v15Encryptor&);
|
sl@0
|
176 |
CRSAPKCS1v15Encryptor& operator=(const CRSAPKCS1v15Encryptor&);
|
sl@0
|
177 |
};
|
sl@0
|
178 |
|
sl@0
|
179 |
/**
|
sl@0
|
180 |
* Implementation of RSA decryption as described in PKCS#1 v1.5.
|
sl@0
|
181 |
*
|
sl@0
|
182 |
*/
|
sl@0
|
183 |
class CRSAPKCS1v15Decryptor : public CDecryptor
|
sl@0
|
184 |
{
|
sl@0
|
185 |
public:
|
sl@0
|
186 |
/**
|
sl@0
|
187 |
* Creates a new RSA decryptor object using PKCS#1 v1.5 padding.
|
sl@0
|
188 |
*
|
sl@0
|
189 |
* @param aKey The RSA private key for decryption
|
sl@0
|
190 |
*
|
sl@0
|
191 |
* @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
|
sl@0
|
192 |
* cipher strength restrictions of the crypto library.
|
sl@0
|
193 |
* See TCrypto::IsAsymmetricWeakEnoughL()
|
sl@0
|
194 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
195 |
*/
|
sl@0
|
196 |
IMPORT_C static CRSAPKCS1v15Decryptor* NewL(const CRSAPrivateKey& aKey);
|
sl@0
|
197 |
|
sl@0
|
198 |
/**
|
sl@0
|
199 |
* Creates a new RSA decryptor object using PKCS#1 v1.5 padding
|
sl@0
|
200 |
*
|
sl@0
|
201 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
202 |
*
|
sl@0
|
203 |
* @param aKey The RSA private key for decryption
|
sl@0
|
204 |
*
|
sl@0
|
205 |
* @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
|
sl@0
|
206 |
* cipher strength restrictions of the crypto library.
|
sl@0
|
207 |
* See TCrypto::IsAsymmetricWeakEnoughL()
|
sl@0
|
208 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
209 |
* @leave KErrNotSupported If the RSA private key is not a supported TRSAPrivateKeyType
|
sl@0
|
210 |
*/
|
sl@0
|
211 |
IMPORT_C static CRSAPKCS1v15Decryptor* NewLC(const CRSAPrivateKey& aKey);
|
sl@0
|
212 |
void DecryptL(const TDesC8& aInput, TDes8& aOutput) const;
|
sl@0
|
213 |
TInt MaxInputLength(void) const;
|
sl@0
|
214 |
TInt MaxOutputLength(void) const;
|
sl@0
|
215 |
/** The destructor frees all resources owned by the object, prior to its destruction. */
|
sl@0
|
216 |
virtual ~CRSAPKCS1v15Decryptor(void);
|
sl@0
|
217 |
protected:
|
sl@0
|
218 |
/** @internalAll */
|
sl@0
|
219 |
CRSAPKCS1v15Decryptor(const CRSAPrivateKey& aKey);
|
sl@0
|
220 |
/** @internalAll */
|
sl@0
|
221 |
void ConstructL(void);
|
sl@0
|
222 |
protected:
|
sl@0
|
223 |
/** The RSA private key */
|
sl@0
|
224 |
const CRSAPrivateKey& iPrivateKey;
|
sl@0
|
225 |
/** The PKCS#1 v1.5 encryption padding */
|
sl@0
|
226 |
CPaddingPKCS1Encryption* iPadding;
|
sl@0
|
227 |
private:
|
sl@0
|
228 |
CRSAPKCS1v15Decryptor(const CRSAPKCS1v15Decryptor&);
|
sl@0
|
229 |
CRSAPKCS1v15Decryptor& operator=(const CRSAPKCS1v15Decryptor&);
|
sl@0
|
230 |
};
|
sl@0
|
231 |
|
sl@0
|
232 |
/**
|
sl@0
|
233 |
* Mixin class defining operations common to all public key signature systems.
|
sl@0
|
234 |
*
|
sl@0
|
235 |
*/
|
sl@0
|
236 |
class MSignatureSystem
|
sl@0
|
237 |
{
|
sl@0
|
238 |
public:
|
sl@0
|
239 |
/**
|
sl@0
|
240 |
* Gets the maximum size of input accepted by this object.
|
sl@0
|
241 |
*
|
sl@0
|
242 |
* @return The maximum length allowed in bytes
|
sl@0
|
243 |
*/
|
sl@0
|
244 |
virtual TInt MaxInputLength(void) const = 0;
|
sl@0
|
245 |
protected:
|
sl@0
|
246 |
/** Constructor */
|
sl@0
|
247 |
IMPORT_C MSignatureSystem(void);
|
sl@0
|
248 |
private:
|
sl@0
|
249 |
MSignatureSystem(const MSignatureSystem&);
|
sl@0
|
250 |
MSignatureSystem& operator=(const MSignatureSystem&);
|
sl@0
|
251 |
};
|
sl@0
|
252 |
|
sl@0
|
253 |
/**
|
sl@0
|
254 |
* Abstract base class for all public key signers.
|
sl@0
|
255 |
*
|
sl@0
|
256 |
* The template parameter, CSignature, should be a class that encapsulates the
|
sl@0
|
257 |
* concept of a digital signature. Derived signature classes must own their
|
sl@0
|
258 |
* respective signatures (and hence be CBase derived). There are no other
|
sl@0
|
259 |
* restrictions on the formation of the signature classes.
|
sl@0
|
260 |
*
|
sl@0
|
261 |
*/
|
sl@0
|
262 |
template <class CSignature> class CSigner : public CBase, public MSignatureSystem
|
sl@0
|
263 |
{
|
sl@0
|
264 |
public:
|
sl@0
|
265 |
/**
|
sl@0
|
266 |
* Digitally signs the specified input message
|
sl@0
|
267 |
*
|
sl@0
|
268 |
* @param aInput The raw data to sign, typically a hash of the actual message
|
sl@0
|
269 |
* @return A pointer to a new CSignature object
|
sl@0
|
270 |
*
|
sl@0
|
271 |
* @panic ECryptoPanicInputTooLarge If aInput is larger than MaxInputLength(),
|
sl@0
|
272 |
* which is likely to happen if the caller
|
sl@0
|
273 |
* has passed in something that has not been
|
sl@0
|
274 |
* hashed.
|
sl@0
|
275 |
*/
|
sl@0
|
276 |
virtual CSignature* SignL(const TDesC8& aInput) const = 0;
|
sl@0
|
277 |
protected:
|
sl@0
|
278 |
/** @internalAll */
|
sl@0
|
279 |
CSigner(void);
|
sl@0
|
280 |
private:
|
sl@0
|
281 |
CSigner(const CSigner&);
|
sl@0
|
282 |
CSigner& operator=(const CSigner&);
|
sl@0
|
283 |
};
|
sl@0
|
284 |
|
sl@0
|
285 |
/**
|
sl@0
|
286 |
* Abstract class for all public key verifiers.
|
sl@0
|
287 |
*
|
sl@0
|
288 |
* The template parameter, CSignature, should be a class that encapsulates the
|
sl@0
|
289 |
* concept of a digital signature. Derived signature classes must own their
|
sl@0
|
290 |
* respective signatures (and hence be CBase derived). There are no other
|
sl@0
|
291 |
* restrictions on the formation of the signature classes.
|
sl@0
|
292 |
*
|
sl@0
|
293 |
*/
|
sl@0
|
294 |
template <class CSignature> class CVerifier : public CBase, public MSignatureSystem
|
sl@0
|
295 |
{
|
sl@0
|
296 |
public:
|
sl@0
|
297 |
/**
|
sl@0
|
298 |
* Verifies the specified digital signature
|
sl@0
|
299 |
*
|
sl@0
|
300 |
* @param aInput The message digest that was originally signed
|
sl@0
|
301 |
* @param aSignature The signature to be verified
|
sl@0
|
302 |
*
|
sl@0
|
303 |
* @return Whether the signature is the result of signing
|
sl@0
|
304 |
* aInput with the supplied key
|
sl@0
|
305 |
*/
|
sl@0
|
306 |
virtual TBool VerifyL(const TDesC8& aInput,
|
sl@0
|
307 |
const CSignature& aSignature) const = 0;
|
sl@0
|
308 |
protected:
|
sl@0
|
309 |
/** @internalAll */
|
sl@0
|
310 |
CVerifier(void);
|
sl@0
|
311 |
private:
|
sl@0
|
312 |
CVerifier(const CVerifier&);
|
sl@0
|
313 |
CVerifier& operator=(const CVerifier&);
|
sl@0
|
314 |
};
|
sl@0
|
315 |
|
sl@0
|
316 |
/* Template nastiness for CVerifier and CSigner in asymmetric.inl */
|
sl@0
|
317 |
|
sl@0
|
318 |
#include <asymmetric.inl>
|
sl@0
|
319 |
|
sl@0
|
320 |
/**
|
sl@0
|
321 |
* An encapsulation of a RSA signature.
|
sl@0
|
322 |
*
|
sl@0
|
323 |
*/
|
sl@0
|
324 |
class CRSASignature : public CBase
|
sl@0
|
325 |
{
|
sl@0
|
326 |
public:
|
sl@0
|
327 |
/**
|
sl@0
|
328 |
* Creates a new CRSASignature object from the integer value
|
sl@0
|
329 |
* output of a previous RSA signing operation.
|
sl@0
|
330 |
*
|
sl@0
|
331 |
* @param aS The integer value output from a previous RSA signing operation
|
sl@0
|
332 |
* @return A pointer to the new CRSASignature object.
|
sl@0
|
333 |
*/
|
sl@0
|
334 |
IMPORT_C static CRSASignature* NewL(RInteger& aS);
|
sl@0
|
335 |
|
sl@0
|
336 |
/**
|
sl@0
|
337 |
* Creates a new CRSASignature object from the integer value
|
sl@0
|
338 |
* output of a previous RSA signing operation.
|
sl@0
|
339 |
*
|
sl@0
|
340 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
341 |
*
|
sl@0
|
342 |
* @param aS The integer value output from a previous RSA signing operation
|
sl@0
|
343 |
* @return A pointer to the new CRSASignature object.
|
sl@0
|
344 |
*/
|
sl@0
|
345 |
IMPORT_C static CRSASignature* NewLC(RInteger& aS);
|
sl@0
|
346 |
|
sl@0
|
347 |
/**
|
sl@0
|
348 |
* Gets the integer value of the RSA signature
|
sl@0
|
349 |
*
|
sl@0
|
350 |
* @return The integer value of the RSA signature
|
sl@0
|
351 |
*/
|
sl@0
|
352 |
IMPORT_C const TInteger& S(void) const;
|
sl@0
|
353 |
|
sl@0
|
354 |
/**
|
sl@0
|
355 |
* Whether this RSASignature is identical to a specified RSASignature
|
sl@0
|
356 |
*
|
sl@0
|
357 |
* @param aSig The RSASignature for comparison
|
sl@0
|
358 |
* @return ETrue, if the two signatures are identical; EFalse, otherwise.
|
sl@0
|
359 |
*/
|
sl@0
|
360 |
IMPORT_C TBool operator== (const CRSASignature& aSig) const;
|
sl@0
|
361 |
|
sl@0
|
362 |
/** Destructor */
|
sl@0
|
363 |
/** The destructor frees all resources owned by the object, prior to its destruction. */
|
sl@0
|
364 |
IMPORT_C virtual ~CRSASignature(void);
|
sl@0
|
365 |
protected:
|
sl@0
|
366 |
/**
|
sl@0
|
367 |
* Second phase constructor
|
sl@0
|
368 |
*
|
sl@0
|
369 |
* @see CRSASignature::NewL()
|
sl@0
|
370 |
*
|
sl@0
|
371 |
* @param aS The integer value output from a previous RSA signing operation
|
sl@0
|
372 |
*/
|
sl@0
|
373 |
IMPORT_C CRSASignature(RInteger& aS);
|
sl@0
|
374 |
|
sl@0
|
375 |
/** Default constructor */
|
sl@0
|
376 |
IMPORT_C CRSASignature(void);
|
sl@0
|
377 |
protected:
|
sl@0
|
378 |
/** An integer value; the output from a previous RSA signing operation. */
|
sl@0
|
379 |
RInteger iS;
|
sl@0
|
380 |
private:
|
sl@0
|
381 |
CRSASignature(const CRSASignature&);
|
sl@0
|
382 |
CRSASignature& operator=(const CRSASignature);
|
sl@0
|
383 |
};
|
sl@0
|
384 |
|
sl@0
|
385 |
/**
|
sl@0
|
386 |
* Abstract base class for all RSA Signers.
|
sl@0
|
387 |
*
|
sl@0
|
388 |
*/
|
sl@0
|
389 |
class CRSASigner : public CSigner<CRSASignature>
|
sl@0
|
390 |
{
|
sl@0
|
391 |
public:
|
sl@0
|
392 |
/**
|
sl@0
|
393 |
* Gets the maximum size of output that can be generated by this object.
|
sl@0
|
394 |
*
|
sl@0
|
395 |
* @return The maximum output length in bytes
|
sl@0
|
396 |
*/
|
sl@0
|
397 |
virtual TInt MaxOutputLength(void) const = 0;
|
sl@0
|
398 |
protected:
|
sl@0
|
399 |
/** Default constructor */
|
sl@0
|
400 |
IMPORT_C CRSASigner(void);
|
sl@0
|
401 |
private:
|
sl@0
|
402 |
CRSASigner(const CRSASigner&);
|
sl@0
|
403 |
CRSASigner& operator=(const CRSASigner&);
|
sl@0
|
404 |
};
|
sl@0
|
405 |
|
sl@0
|
406 |
/**
|
sl@0
|
407 |
* Implementation of RSA signing as described in PKCS#1 v1.5.
|
sl@0
|
408 |
*
|
sl@0
|
409 |
* This class creates RSA signatures following the RSA PKCS#1 v1.5 standard (with
|
sl@0
|
410 |
* the one caveat noted below) and using PKCS#1 v1.5 signature padding. The only
|
sl@0
|
411 |
* exception is that the SignL() function simply performs a 'raw' PKCS#1 v1.5 sign
|
sl@0
|
412 |
* operation on whatever it is given. It does <b>not</b> hash or in any way
|
sl@0
|
413 |
* manipulate the input data before signing.
|
sl@0
|
414 |
*
|
sl@0
|
415 |
*/
|
sl@0
|
416 |
class CRSAPKCS1v15Signer : public CRSASigner
|
sl@0
|
417 |
{
|
sl@0
|
418 |
public:
|
sl@0
|
419 |
/**
|
sl@0
|
420 |
* Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
|
sl@0
|
421 |
*
|
sl@0
|
422 |
* @param aKey The RSA private key to be used for signing
|
sl@0
|
423 |
* @return A pointer to the new CRSAPKCS1v15Signer object
|
sl@0
|
424 |
*
|
sl@0
|
425 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
426 |
*/
|
sl@0
|
427 |
IMPORT_C static CRSAPKCS1v15Signer* NewL(const CRSAPrivateKey& aKey);
|
sl@0
|
428 |
|
sl@0
|
429 |
/**
|
sl@0
|
430 |
* Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
|
sl@0
|
431 |
*
|
sl@0
|
432 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
433 |
*
|
sl@0
|
434 |
* @param aKey The RSA private key to be used for signing
|
sl@0
|
435 |
* @return A pointer to the new CRSAPKCS1v15Signer object
|
sl@0
|
436 |
*
|
sl@0
|
437 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
438 |
*/
|
sl@0
|
439 |
IMPORT_C static CRSAPKCS1v15Signer* NewLC(const CRSAPrivateKey& aKey);
|
sl@0
|
440 |
/**
|
sl@0
|
441 |
* Digitally signs the specified input message
|
sl@0
|
442 |
*
|
sl@0
|
443 |
* @param aInput The raw data to sign, typically a hash of the actual message
|
sl@0
|
444 |
* @return A pointer to a new CSignature object
|
sl@0
|
445 |
*
|
sl@0
|
446 |
* @leave KErrNotSupported If the private key is not a supported TRSAPrivateKeyType
|
sl@0
|
447 |
* @panic ECryptoPanicInputTooLarge If aInput is larger than MaxInputLength(),
|
sl@0
|
448 |
* which is likely to happen if the caller
|
sl@0
|
449 |
* has passed in something that has not been hashed.
|
sl@0
|
450 |
*/
|
sl@0
|
451 |
virtual CRSASignature* SignL(const TDesC8& aInput) const;
|
sl@0
|
452 |
virtual TInt MaxInputLength(void) const;
|
sl@0
|
453 |
virtual TInt MaxOutputLength(void) const;
|
sl@0
|
454 |
/** The destructor frees all resources owned by the object, prior to its destruction.
|
sl@0
|
455 |
* @internalAll */
|
sl@0
|
456 |
~CRSAPKCS1v15Signer(void);
|
sl@0
|
457 |
protected:
|
sl@0
|
458 |
/** @internalAll */
|
sl@0
|
459 |
CRSAPKCS1v15Signer(const CRSAPrivateKey& aKey);
|
sl@0
|
460 |
/** @internalAll */
|
sl@0
|
461 |
void ConstructL(void);
|
sl@0
|
462 |
protected:
|
sl@0
|
463 |
/** The RSA private key to be used for signing */
|
sl@0
|
464 |
const CRSAPrivateKey& iPrivateKey;
|
sl@0
|
465 |
/** The PKCS#1 v1.5 signature padding */
|
sl@0
|
466 |
CPaddingPKCS1Signature* iPadding;
|
sl@0
|
467 |
private:
|
sl@0
|
468 |
CRSAPKCS1v15Signer(const CRSAPKCS1v15Signer&);
|
sl@0
|
469 |
CRSAPKCS1v15Signer& operator=(const CRSAPKCS1v15Signer&);
|
sl@0
|
470 |
};
|
sl@0
|
471 |
|
sl@0
|
472 |
/**
|
sl@0
|
473 |
* Abstract base class for all RSA Verifiers.
|
sl@0
|
474 |
*
|
sl@0
|
475 |
*/
|
sl@0
|
476 |
class CRSAVerifier : public CVerifier<CRSASignature>
|
sl@0
|
477 |
{
|
sl@0
|
478 |
public:
|
sl@0
|
479 |
/**
|
sl@0
|
480 |
* Gets the maximum size of output that can be generated by this object.
|
sl@0
|
481 |
*
|
sl@0
|
482 |
* @return The maximum output length in bytes
|
sl@0
|
483 |
*/
|
sl@0
|
484 |
virtual TInt MaxOutputLength(void) const = 0;
|
sl@0
|
485 |
|
sl@0
|
486 |
/**
|
sl@0
|
487 |
* Performs a decryption operation on a signature using the public key.
|
sl@0
|
488 |
*
|
sl@0
|
489 |
* This is the inverse of the sign operation, which performs a encryption
|
sl@0
|
490 |
* operation on its input data using the private key. Although this can be
|
sl@0
|
491 |
* used to verify signatures, CRSAVerifier::VerifyL should be used in
|
sl@0
|
492 |
* preference. This method is however required by some security protocols.
|
sl@0
|
493 |
*
|
sl@0
|
494 |
* @param aSignature The signature to be verified
|
sl@0
|
495 |
* @return A pointer to a new buffer containing the result of the
|
sl@0
|
496 |
* operation. The pointer is left on the cleanup stack.
|
sl@0
|
497 |
*/
|
sl@0
|
498 |
virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const = 0;
|
sl@0
|
499 |
|
sl@0
|
500 |
IMPORT_C virtual TBool VerifyL(const TDesC8& aInput,
|
sl@0
|
501 |
const CRSASignature& aSignature) const;
|
sl@0
|
502 |
protected:
|
sl@0
|
503 |
/** Default constructor */
|
sl@0
|
504 |
IMPORT_C CRSAVerifier(void);
|
sl@0
|
505 |
private:
|
sl@0
|
506 |
CRSAVerifier(const CRSAVerifier&);
|
sl@0
|
507 |
CRSAVerifier& operator=(const CRSAVerifier&);
|
sl@0
|
508 |
};
|
sl@0
|
509 |
|
sl@0
|
510 |
/**
|
sl@0
|
511 |
* This class verifies RSA signatures given a message and its supposed
|
sl@0
|
512 |
* signature. It follows the RSA PKCS#1 v1.5 with PKCS#1 v1.5 padding specification
|
sl@0
|
513 |
* with the following exception: the VerifyL() function does <b>not</b> hash or
|
sl@0
|
514 |
* in any way manipulate the input data before checking. Thus in order to verify
|
sl@0
|
515 |
* RSA signatures in PKCS#1 v1.5 format, the input data needs to follow PKCS#1 v1.5
|
sl@0
|
516 |
* specification, i.e. be ASN.1 encoded and prefixed by ASN.1 encoded digestId.
|
sl@0
|
517 |
*
|
sl@0
|
518 |
*/
|
sl@0
|
519 |
class CRSAPKCS1v15Verifier : public CRSAVerifier
|
sl@0
|
520 |
{
|
sl@0
|
521 |
public:
|
sl@0
|
522 |
/**
|
sl@0
|
523 |
* Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
|
sl@0
|
524 |
*
|
sl@0
|
525 |
* @param aKey The RSA public key to be used for verifying
|
sl@0
|
526 |
* @return A pointer to the new CRSAPKCS1v15Verifier object
|
sl@0
|
527 |
*
|
sl@0
|
528 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
529 |
*/
|
sl@0
|
530 |
IMPORT_C static CRSAPKCS1v15Verifier* NewL(const CRSAPublicKey& aKey);
|
sl@0
|
531 |
|
sl@0
|
532 |
/**
|
sl@0
|
533 |
* Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
|
sl@0
|
534 |
*
|
sl@0
|
535 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
536 |
*
|
sl@0
|
537 |
* @param aKey The RSA public key to be used for verifying
|
sl@0
|
538 |
* @return A pointer to the new CRSAPKCS1v15Verifier object
|
sl@0
|
539 |
*
|
sl@0
|
540 |
* @leave KErrKeySize If the key length is too small
|
sl@0
|
541 |
*/
|
sl@0
|
542 |
IMPORT_C static CRSAPKCS1v15Verifier* NewLC(const CRSAPublicKey& aKey);
|
sl@0
|
543 |
virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const;
|
sl@0
|
544 |
virtual TInt MaxInputLength(void) const;
|
sl@0
|
545 |
virtual TInt MaxOutputLength(void) const;
|
sl@0
|
546 |
/** The destructor frees all resources owned by the object, prior to its destruction. */
|
sl@0
|
547 |
virtual ~CRSAPKCS1v15Verifier(void);
|
sl@0
|
548 |
protected:
|
sl@0
|
549 |
/** @internalAll */
|
sl@0
|
550 |
CRSAPKCS1v15Verifier(const CRSAPublicKey& aKey);
|
sl@0
|
551 |
/** @internalAll */
|
sl@0
|
552 |
void ConstructL(void);
|
sl@0
|
553 |
protected:
|
sl@0
|
554 |
/** The RSA public key to be used for verification */
|
sl@0
|
555 |
const CRSAPublicKey& iPublicKey;
|
sl@0
|
556 |
/** The PKCS#1 v1.5 signature padding */
|
sl@0
|
557 |
CPaddingPKCS1Signature* iPadding;
|
sl@0
|
558 |
private:
|
sl@0
|
559 |
CRSAPKCS1v15Verifier(const CRSAPKCS1v15Verifier&);
|
sl@0
|
560 |
CRSAPKCS1v15Verifier& operator=(const CRSAPKCS1v15Verifier&);
|
sl@0
|
561 |
};
|
sl@0
|
562 |
|
sl@0
|
563 |
/**
|
sl@0
|
564 |
* An encapsulation of a DSA signature.
|
sl@0
|
565 |
*
|
sl@0
|
566 |
*/
|
sl@0
|
567 |
class CDSASignature : public CBase
|
sl@0
|
568 |
{
|
sl@0
|
569 |
public:
|
sl@0
|
570 |
/**
|
sl@0
|
571 |
* Creates a new CDSASignature object from the specified R and S values.
|
sl@0
|
572 |
*
|
sl@0
|
573 |
* @param aR The DSA signature's R value
|
sl@0
|
574 |
* @param aS The DSA signature's S value
|
sl@0
|
575 |
* @return A pointer to the new CDSASignature object
|
sl@0
|
576 |
*/
|
sl@0
|
577 |
IMPORT_C static CDSASignature* NewL(RInteger& aR, RInteger& aS);
|
sl@0
|
578 |
|
sl@0
|
579 |
/**
|
sl@0
|
580 |
* Creates a new CDSASignature object from the specified R and S values.
|
sl@0
|
581 |
*
|
sl@0
|
582 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
583 |
*
|
sl@0
|
584 |
* @param aR The DSA signature's R value
|
sl@0
|
585 |
* @param aS The DSA signature's S value
|
sl@0
|
586 |
* @return A pointer to the new CDSASignature object
|
sl@0
|
587 |
*/
|
sl@0
|
588 |
IMPORT_C static CDSASignature* NewLC(RInteger& aR, RInteger& aS);
|
sl@0
|
589 |
|
sl@0
|
590 |
/**
|
sl@0
|
591 |
* Gets the DSA signature's R value
|
sl@0
|
592 |
*
|
sl@0
|
593 |
* @return The R value
|
sl@0
|
594 |
*/
|
sl@0
|
595 |
IMPORT_C const TInteger& R(void) const;
|
sl@0
|
596 |
|
sl@0
|
597 |
/**
|
sl@0
|
598 |
* Gets the DSA signature's S value
|
sl@0
|
599 |
*
|
sl@0
|
600 |
* @return The S value
|
sl@0
|
601 |
*/
|
sl@0
|
602 |
IMPORT_C const TInteger& S(void) const;
|
sl@0
|
603 |
|
sl@0
|
604 |
/**
|
sl@0
|
605 |
* Whether this DSASignature is identical to a specified DSASignature
|
sl@0
|
606 |
*
|
sl@0
|
607 |
* @param aSig The DSASignature for comparison
|
sl@0
|
608 |
* @return ETrue, if the two signatures are identical; EFalse, otherwise.
|
sl@0
|
609 |
*/
|
sl@0
|
610 |
IMPORT_C TBool operator== (const CDSASignature& aSig) const;
|
sl@0
|
611 |
|
sl@0
|
612 |
/** The destructor frees all resources owned by the object, prior to its destruction. */
|
sl@0
|
613 |
IMPORT_C virtual ~CDSASignature(void);
|
sl@0
|
614 |
protected:
|
sl@0
|
615 |
/**
|
sl@0
|
616 |
* Protected constructor
|
sl@0
|
617 |
*
|
sl@0
|
618 |
* @param aR The DSA signature's R value
|
sl@0
|
619 |
* @param aS The DSA signature's S value
|
sl@0
|
620 |
*/
|
sl@0
|
621 |
IMPORT_C CDSASignature(RInteger& aR, RInteger& aS);
|
sl@0
|
622 |
|
sl@0
|
623 |
/** Default constructor */
|
sl@0
|
624 |
IMPORT_C CDSASignature(void);
|
sl@0
|
625 |
protected:
|
sl@0
|
626 |
/** The DSA signature's R value */
|
sl@0
|
627 |
RInteger iR;
|
sl@0
|
628 |
/** The DSA signature's S value */
|
sl@0
|
629 |
RInteger iS;
|
sl@0
|
630 |
private:
|
sl@0
|
631 |
CDSASignature(const CDSASignature&);
|
sl@0
|
632 |
CDSASignature& operator=(const CDSASignature&);
|
sl@0
|
633 |
};
|
sl@0
|
634 |
|
sl@0
|
635 |
/**
|
sl@0
|
636 |
* Implementation of DSA signing as specified in FIPS 186-2 change request 1.
|
sl@0
|
637 |
*
|
sl@0
|
638 |
*/
|
sl@0
|
639 |
class CDSASigner : public CSigner<CDSASignature>
|
sl@0
|
640 |
{
|
sl@0
|
641 |
public:
|
sl@0
|
642 |
/**
|
sl@0
|
643 |
* Creates a new CDSASigner object from a specified DSA private key.
|
sl@0
|
644 |
*
|
sl@0
|
645 |
* @param aKey The DSA private key to be used for signing
|
sl@0
|
646 |
* @return A pointer to the new CDSASigner object
|
sl@0
|
647 |
*/
|
sl@0
|
648 |
IMPORT_C static CDSASigner* NewL(const CDSAPrivateKey& aKey);
|
sl@0
|
649 |
|
sl@0
|
650 |
/**
|
sl@0
|
651 |
* Creates a new CDSASigner object from a specified DSA private key.
|
sl@0
|
652 |
*
|
sl@0
|
653 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
654 |
*
|
sl@0
|
655 |
* @param aKey The DSA private key to be used for signing
|
sl@0
|
656 |
* @return A pointer to the new CDSASigner object
|
sl@0
|
657 |
*/
|
sl@0
|
658 |
IMPORT_C static CDSASigner* NewLC(const CDSAPrivateKey& aKey);
|
sl@0
|
659 |
/**
|
sl@0
|
660 |
* Digitally signs the specified input message
|
sl@0
|
661 |
*
|
sl@0
|
662 |
* Note that in order to be interoperable and compliant with the DSS, aInput
|
sl@0
|
663 |
* must be the result of a SHA-1 hash.
|
sl@0
|
664 |
*
|
sl@0
|
665 |
* @param aInput A SHA-1 hash of the message to sign
|
sl@0
|
666 |
* @return A pointer to a new CSignature object
|
sl@0
|
667 |
*
|
sl@0
|
668 |
* @panic ECryptoPanicInputTooLarge If aInput is larger than MaxInputLength(),
|
sl@0
|
669 |
* which is likely to happen if the caller
|
sl@0
|
670 |
* has passed in something that has not been hashed.
|
sl@0
|
671 |
*/
|
sl@0
|
672 |
virtual CDSASignature* SignL(const TDesC8& aInput) const;
|
sl@0
|
673 |
virtual TInt MaxInputLength(void) const;
|
sl@0
|
674 |
protected:
|
sl@0
|
675 |
/** @internalAll */
|
sl@0
|
676 |
CDSASigner(const CDSAPrivateKey& aKey);
|
sl@0
|
677 |
protected:
|
sl@0
|
678 |
/** The DSA private key to be used for signing */
|
sl@0
|
679 |
const CDSAPrivateKey& iPrivateKey;
|
sl@0
|
680 |
private:
|
sl@0
|
681 |
CDSASigner(const CDSASigner&);
|
sl@0
|
682 |
CDSASigner& operator=(const CDSASigner&);
|
sl@0
|
683 |
};
|
sl@0
|
684 |
|
sl@0
|
685 |
/**
|
sl@0
|
686 |
* Implementation of DSA signature verification as specified in FIPS 186-2 change
|
sl@0
|
687 |
* request 1.
|
sl@0
|
688 |
*
|
sl@0
|
689 |
*/
|
sl@0
|
690 |
class CDSAVerifier : public CVerifier<CDSASignature>
|
sl@0
|
691 |
{
|
sl@0
|
692 |
public:
|
sl@0
|
693 |
/**
|
sl@0
|
694 |
* Creates a new CDSAVerifier object from a specified DSA public key.
|
sl@0
|
695 |
*
|
sl@0
|
696 |
* @param aKey The DSA public key to be used for verifying
|
sl@0
|
697 |
* @return A pointer to the new CDSAVerifier object
|
sl@0
|
698 |
*/
|
sl@0
|
699 |
IMPORT_C static CDSAVerifier* NewL(const CDSAPublicKey& aKey);
|
sl@0
|
700 |
|
sl@0
|
701 |
/**
|
sl@0
|
702 |
* Creates a new CDSAVerifier object from a specified DSA public key.
|
sl@0
|
703 |
*
|
sl@0
|
704 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
705 |
*
|
sl@0
|
706 |
* @param aKey The DSA public key to be used for verifying
|
sl@0
|
707 |
* @return A pointer to the new CDSAVerifier object
|
sl@0
|
708 |
*/
|
sl@0
|
709 |
IMPORT_C static CDSAVerifier* NewLC(const CDSAPublicKey& aKey);
|
sl@0
|
710 |
/**
|
sl@0
|
711 |
* Verifies the specified digital signature
|
sl@0
|
712 |
*
|
sl@0
|
713 |
* Note that in order to be interoperable and compliant with the DSS, aInput
|
sl@0
|
714 |
* must be the result of a SHA-1 hash.
|
sl@0
|
715 |
*
|
sl@0
|
716 |
* @param aInput A SHA-1 hash of the received message
|
sl@0
|
717 |
* @param aSignature The signature to be verified
|
sl@0
|
718 |
*
|
sl@0
|
719 |
* @return Whether the signature is the result of signing
|
sl@0
|
720 |
* aInput with the supplied key
|
sl@0
|
721 |
*/
|
sl@0
|
722 |
virtual TBool VerifyL(const TDesC8& aInput, const CDSASignature& aSignature) const;
|
sl@0
|
723 |
virtual TInt MaxInputLength(void) const;
|
sl@0
|
724 |
protected:
|
sl@0
|
725 |
/** @internalAll */
|
sl@0
|
726 |
CDSAVerifier(const CDSAPublicKey& aKey);
|
sl@0
|
727 |
protected:
|
sl@0
|
728 |
/** The DSA public key to be used for verification */
|
sl@0
|
729 |
const CDSAPublicKey& iPublicKey;
|
sl@0
|
730 |
private:
|
sl@0
|
731 |
CDSAVerifier(const CDSAVerifier&);
|
sl@0
|
732 |
CDSAVerifier& operator=(const CDSAVerifier&);
|
sl@0
|
733 |
};
|
sl@0
|
734 |
|
sl@0
|
735 |
/**
|
sl@0
|
736 |
* Implementation of Diffie-Hellman key agreement as specified in PKCS#3.
|
sl@0
|
737 |
*
|
sl@0
|
738 |
*/
|
sl@0
|
739 |
class CDH : public CBase
|
sl@0
|
740 |
{
|
sl@0
|
741 |
public:
|
sl@0
|
742 |
/**
|
sl@0
|
743 |
* Creates a new CDH object from a specified DH private key.
|
sl@0
|
744 |
*
|
sl@0
|
745 |
* @param aPrivateKey The private key of this party
|
sl@0
|
746 |
* @return A pointer to the new CDH object
|
sl@0
|
747 |
*/
|
sl@0
|
748 |
IMPORT_C static CDH* NewL(const CDHPrivateKey& aPrivateKey);
|
sl@0
|
749 |
|
sl@0
|
750 |
/**
|
sl@0
|
751 |
* Creates a new CDH object from a specified DH private key.
|
sl@0
|
752 |
*
|
sl@0
|
753 |
* The returned pointer is put onto the cleanup stack.
|
sl@0
|
754 |
*
|
sl@0
|
755 |
* @param aPrivateKey The private key of this party
|
sl@0
|
756 |
* @return A pointer to the new CDH object
|
sl@0
|
757 |
*/
|
sl@0
|
758 |
IMPORT_C static CDH* NewLC(const CDHPrivateKey& aPrivateKey);
|
sl@0
|
759 |
|
sl@0
|
760 |
/**
|
sl@0
|
761 |
* Performs the key agreement operation.
|
sl@0
|
762 |
*
|
sl@0
|
763 |
* @param aPublicKey The public key of the other party
|
sl@0
|
764 |
* @return The agreed key
|
sl@0
|
765 |
*/
|
sl@0
|
766 |
IMPORT_C HBufC8* AgreeL(const CDHPublicKey& aPublicKey) const;
|
sl@0
|
767 |
protected:
|
sl@0
|
768 |
/**
|
sl@0
|
769 |
* Constructor
|
sl@0
|
770 |
*
|
sl@0
|
771 |
* @param aPrivateKey The DH private key
|
sl@0
|
772 |
*/
|
sl@0
|
773 |
IMPORT_C CDH(const CDHPrivateKey& aPrivateKey);
|
sl@0
|
774 |
protected:
|
sl@0
|
775 |
/** The DH private key */
|
sl@0
|
776 |
const CDHPrivateKey& iPrivateKey;
|
sl@0
|
777 |
private:
|
sl@0
|
778 |
CDH(const CDH&);
|
sl@0
|
779 |
CDH& operator=(const CDH&);
|
sl@0
|
780 |
};
|
sl@0
|
781 |
|
sl@0
|
782 |
#endif // __ASYMMETRIC_H__
|