|
1 /* |
|
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the |
|
16 * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted. |
|
17 * Asymmetric keys implementation |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 /** |
|
25 @file |
|
26 @internalAll |
|
27 */ |
|
28 |
|
29 #ifndef __ASYMMETRICKEYS_H__ |
|
30 #define __ASYMMETRICKEYS_H__ |
|
31 |
|
32 #include <e32base.h> |
|
33 #include <random.h> |
|
34 #include <bigint.h> |
|
35 |
|
36 /** |
|
37 * Defines the various ways of representing supported RSA private keys. |
|
38 * |
|
39 * @publishedPartner |
|
40 * @released |
|
41 */ |
|
42 enum TRSAPrivateKeyType |
|
43 { |
|
44 /** |
|
45 * Standard type of RSA private key |
|
46 * |
|
47 * This consists of the modulus (n) and decryption exponent (d). |
|
48 */ |
|
49 EStandard, |
|
50 /** |
|
51 * CRT (Chinese Remainder Theorem) type of RSA private key |
|
52 * |
|
53 * This consists of the the first factor (p), the second factor (q), |
|
54 * the first factor's CRT exponent (dP), the second factor's CRT exponent (dQ), |
|
55 * and the (first) CRT coefficient (qInv). The two factors, p and q, are the |
|
56 * first two prime factors of the RSA modulus, n. |
|
57 */ |
|
58 EStandardCRT |
|
59 //We may support types like this in the future (currently these are a patent |
|
60 //minefield): |
|
61 //EMulti, //multi prime version of EStandard |
|
62 //EMultiCRT //multi prime version of EStandardCRT |
|
63 }; |
|
64 |
|
65 /** |
|
66 * Concrete class representing the parameters common to both an RSA public and |
|
67 * private key. |
|
68 * |
|
69 * See ANSI X9.31 and RSA PKCS#1 |
|
70 * |
|
71 * @publishedPartner |
|
72 * @released |
|
73 */ |
|
74 class CRSAParameters : public CBase |
|
75 { |
|
76 public: |
|
77 /** |
|
78 * Gets the RSA parameter, n (the modulus) |
|
79 * |
|
80 * @return The RSA parameter, n |
|
81 */ |
|
82 IMPORT_C const TInteger& N(void) const; |
|
83 |
|
84 /** Destructor */ |
|
85 IMPORT_C virtual ~CRSAParameters(void); |
|
86 protected: |
|
87 /** |
|
88 * Constructor |
|
89 * |
|
90 * @param aN The RSA parameter, n (the modulus) |
|
91 */ |
|
92 IMPORT_C CRSAParameters(RInteger& aN); |
|
93 |
|
94 /** Default constructor */ |
|
95 IMPORT_C CRSAParameters(void); |
|
96 protected: |
|
97 /** The RSA modulus, n, a positive integer */ |
|
98 RInteger iN; |
|
99 private: |
|
100 CRSAParameters(const CRSAParameters&); |
|
101 CRSAParameters& operator=(const CRSAParameters&); |
|
102 }; |
|
103 |
|
104 /** |
|
105 * Representation of an RSA public key. |
|
106 * |
|
107 * An RSA public key is identified by its modulus (n) and its encryption exponent |
|
108 * (e). |
|
109 * |
|
110 * @publishedPartner |
|
111 * @released |
|
112 */ |
|
113 class CRSAPublicKey : public CRSAParameters |
|
114 { |
|
115 public: |
|
116 /** |
|
117 * Creates a new CRSAPublicKey object from a specified |
|
118 * modulus and encryption exponent. |
|
119 * |
|
120 * @param aN The RSA parameter, n (the modulus) |
|
121 * @param aE The RSA parameter, e (the encryption exponent) |
|
122 * @return A pointer to a new CRSAPublicKey object |
|
123 * |
|
124 * @leave KErrArgument If either aN or aE are not positive integers, |
|
125 * and releases ownership. |
|
126 */ |
|
127 IMPORT_C static CRSAPublicKey* NewL(RInteger& aN, RInteger& aE); |
|
128 |
|
129 /** |
|
130 * Creates a new CRSAPublicKey object from a specified |
|
131 * modulus and encryption exponent. |
|
132 * |
|
133 * The returned pointer is put onto the cleanup stack. |
|
134 * |
|
135 * @param aN The RSA parameter, n (the modulus) |
|
136 * @param aE The RSA parameter, e (the encryption exponent) |
|
137 * @return A pointer to a new CRSAPublicKey object |
|
138 * |
|
139 * @leave KErrArgument If either aN or aE are not positive integers, |
|
140 * and releases ownership. |
|
141 */ |
|
142 IMPORT_C static CRSAPublicKey* NewLC(RInteger& aN, RInteger& aE); |
|
143 |
|
144 /** |
|
145 * Gets the RSA parameter, e (the encryption exponent) |
|
146 * |
|
147 * @return The RSA parameter, e |
|
148 */ |
|
149 IMPORT_C const TInteger& E(void) const; |
|
150 |
|
151 /** Destructor */ |
|
152 IMPORT_C virtual ~CRSAPublicKey(void); |
|
153 protected: |
|
154 /** |
|
155 * Constructor |
|
156 * |
|
157 * @param aN The RSA parameter, n (the modulus) |
|
158 * @param aE The RSA parameter, e (the encryption exponent) |
|
159 */ |
|
160 IMPORT_C CRSAPublicKey(RInteger& aN, RInteger& aE); |
|
161 |
|
162 /** Default constructor */ |
|
163 IMPORT_C CRSAPublicKey(void); |
|
164 protected: |
|
165 /** The RSA encryption exponent, e */ |
|
166 RInteger iE; |
|
167 private: |
|
168 CRSAPublicKey(const CRSAPublicKey&); |
|
169 CRSAPublicKey& operator=(const CRSAPublicKey&); |
|
170 void ConstructL(); |
|
171 }; |
|
172 |
|
173 /** |
|
174 * Non-exported container class for the various ways of representing an RSA |
|
175 * private key. |
|
176 * |
|
177 * To instantiate a representation of an RSA private key, find a |
|
178 * subclass of this appropriate to your key type. |
|
179 * |
|
180 * @publishedPartner |
|
181 * @released |
|
182 */ |
|
183 class CRSAPrivateKey : public CRSAParameters |
|
184 { |
|
185 public: |
|
186 /** |
|
187 * Constructor |
|
188 * |
|
189 * @param aKeyType The type of the RSA private key |
|
190 * @param aN The RSA parameter, n (the modulus) |
|
191 * @internalAll |
|
192 */ |
|
193 CRSAPrivateKey(const TRSAPrivateKeyType aKeyType, RInteger& aN); |
|
194 public: |
|
195 /** |
|
196 * Gets the type of RSA private key |
|
197 * |
|
198 * @return The RSA private key type |
|
199 */ |
|
200 inline const TRSAPrivateKeyType PrivateKeyType() const {return (iKeyType);}; |
|
201 protected: |
|
202 /** The type of the RSA private key */ |
|
203 const TRSAPrivateKeyType iKeyType; |
|
204 private: |
|
205 CRSAPrivateKey(const CRSAPrivateKey&); |
|
206 CRSAPrivateKey& operator=(const CRSAPrivateKey&); |
|
207 }; |
|
208 |
|
209 /** |
|
210 * The 'classical' representation of a RSA private key. |
|
211 * |
|
212 * Such a private key is composed of a modulus (n) and a decryption exponent (d). |
|
213 * |
|
214 * @publishedPartner |
|
215 * @released |
|
216 */ |
|
217 class CRSAPrivateKeyStandard : public CRSAPrivateKey |
|
218 { |
|
219 public: |
|
220 /** |
|
221 * Creates a new CRSAPrivateKeyStandard object from a specified |
|
222 * modulus and decryption exponent. |
|
223 * |
|
224 * @param aN The RSA parameter, n (the modulus) |
|
225 * @param aD The RSA parameter, d (the decryption exponent) |
|
226 * @return A pointer to a new CRSAPrivateKeyStandard object |
|
227 * |
|
228 * @leave KErrArgument If either aN or aD are not positive integers, |
|
229 * and releases ownership. |
|
230 */ |
|
231 IMPORT_C static CRSAPrivateKeyStandard* NewL(RInteger& aN, RInteger& aD); |
|
232 |
|
233 /** |
|
234 * Creates a new CRSAPrivateKeyStandard object from a specified |
|
235 * modulus and decryption exponent. |
|
236 * |
|
237 * The returned pointer is put onto the cleanup stack. |
|
238 * |
|
239 * @param aN The RSA parameter, n (the modulus) |
|
240 * @param aD The RSA parameter, d (the decryption exponent) |
|
241 * @return A pointer to a new CRSAPrivateKeyStandard object |
|
242 * |
|
243 * @leave KErrArgument If either aN or aD are not positive integers, |
|
244 * and releases ownership. |
|
245 */ |
|
246 IMPORT_C static CRSAPrivateKeyStandard* NewLC(RInteger& aN, RInteger& aD); |
|
247 |
|
248 /** |
|
249 * Gets the RSA parameter, d (the decryption exponent) |
|
250 * |
|
251 * @return The RSA parameter, d |
|
252 */ |
|
253 IMPORT_C const TInteger& D(void) const; |
|
254 |
|
255 /** Destructor */ |
|
256 IMPORT_C virtual ~CRSAPrivateKeyStandard(void); |
|
257 protected: |
|
258 /** |
|
259 * Constructor |
|
260 * |
|
261 * @param aN The RSA parameter, n (the modulus) |
|
262 * @param aD The RSA parameter, d (the decryption exponent) |
|
263 */ |
|
264 IMPORT_C CRSAPrivateKeyStandard(RInteger& aN, RInteger& aD); |
|
265 protected: |
|
266 /** The RSA decryption exponent, d */ |
|
267 RInteger iD; |
|
268 private: |
|
269 CRSAPrivateKeyStandard(const CRSAPrivateKeyStandard&); |
|
270 CRSAPrivateKeyStandard& operator=(const CRSAPrivateKeyStandard&); |
|
271 void ConstructL(); |
|
272 }; |
|
273 |
|
274 /** |
|
275 * An alternate representation of an RSA private key providing significant |
|
276 * speed enhancements through its use of the Chinese Remainder Theorem (CRT). |
|
277 * |
|
278 * Here, a private key is represented by a modulus (n), the two prime factors of |
|
279 * the modulus (p, q), p's CRT exponent (dP), q's CRT exponent (dQ), and the CRT |
|
280 * coefficient (qInv). See PKCS#1 at http://www.rsasecurity.com/rsalabs/pkcs/ |
|
281 * for more information. |
|
282 * |
|
283 * @publishedPartner |
|
284 * @released |
|
285 */ |
|
286 class CRSAPrivateKeyCRT : public CRSAPrivateKey |
|
287 { |
|
288 public: |
|
289 /** |
|
290 * Creates a new CRSAPrivateKeyCRT object from a specified |
|
291 * modulus and decryption exponent. |
|
292 * |
|
293 * @param iN The RSA parameter, n (the modulus) |
|
294 * @param aP The RSA parameter, p (the first factor) |
|
295 * @param aQ The RSA parameter, q (the second factor) |
|
296 * @param aDP The RSA parameter, dP (the first factor's CRT exponent) |
|
297 * @param aDQ The RSA parameter, dQ (the second factor's CRT exponent) |
|
298 * @param aQInv The RSA parameter, qInv (the CRT coefficient) |
|
299 * @return A pointer to a new CRSAPrivateKeyCRT object |
|
300 * |
|
301 * @leave KErrArgument If any of the parameters are not positive integers, |
|
302 * and releases ownership. |
|
303 */ |
|
304 IMPORT_C static CRSAPrivateKeyCRT* NewL(RInteger& iN, RInteger& aP, |
|
305 RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv); |
|
306 |
|
307 /** |
|
308 * Creates a new CRSAPrivateKeyCRT object from a specified |
|
309 * modulus and decryption exponent. |
|
310 * |
|
311 * The returned pointer is put onto the cleanup stack. |
|
312 * |
|
313 * @param iN The RSA parameter, n (the modulus) |
|
314 * @param aP The RSA parameter, p (the first factor) |
|
315 * @param aQ The RSA parameter, q (the second factor) |
|
316 * @param aDP The RSA parameter, dP (the first factor's CRT exponent) |
|
317 * @param aDQ The RSA parameter, dQ (the second factor's CRT exponent) |
|
318 * @param aQInv The RSA parameter, qInv (the CRT coefficient) |
|
319 * @return A pointer to a new CRSAPrivateKeyCRT object |
|
320 * |
|
321 * @leave KErrArgument If any of the parameters are not positive integers, |
|
322 * and releases ownership. |
|
323 */ |
|
324 IMPORT_C static CRSAPrivateKeyCRT* NewLC(RInteger& iN, RInteger& aP, |
|
325 RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv); |
|
326 |
|
327 /** Destructor */ |
|
328 IMPORT_C virtual ~CRSAPrivateKeyCRT(void); |
|
329 |
|
330 /** |
|
331 * Gets the RSA parameter, p (the first factor) |
|
332 * |
|
333 * @return The first factor |
|
334 */ |
|
335 IMPORT_C const TInteger& P(void) const; |
|
336 |
|
337 /** |
|
338 * Gets the RSA parameter, q (the second factor) |
|
339 * |
|
340 * @return The second factor |
|
341 */ |
|
342 IMPORT_C const TInteger& Q(void) const; |
|
343 |
|
344 /** |
|
345 * Gets the RSA parameter, dP (the first factor's CRT exponent) |
|
346 * |
|
347 * @return The first factor's CRT exponent |
|
348 */ |
|
349 IMPORT_C const TInteger& DP(void) const; |
|
350 |
|
351 /** |
|
352 * Gets the RSA parameter, dQ (the second factor's CRT exponent) |
|
353 * |
|
354 * @return The second factor's CRT exponent |
|
355 */ |
|
356 IMPORT_C const TInteger& DQ(void) const; |
|
357 |
|
358 /** |
|
359 * Gets the RSA parameter, qInv (the CRT coefficient) |
|
360 * |
|
361 * @return The CRT coefficient |
|
362 */ |
|
363 IMPORT_C const TInteger& QInv(void) const; |
|
364 protected: |
|
365 /** |
|
366 * Constructor |
|
367 * |
|
368 * @param aN The RSA parameter, n (the modulus) |
|
369 * @param aP The RSA parameter, p (the first factor) |
|
370 * @param aQ The RSA parameter, q (the second factor) |
|
371 * @param aDP The RSA parameter, dP (the first factor's CRT exponent) |
|
372 * @param aDQ The RSA parameter, dQ (the second factor's CRT exponent) |
|
373 * @param aQInv The RSA parameter, qInv (the CRT coefficient) |
|
374 */ |
|
375 IMPORT_C CRSAPrivateKeyCRT(RInteger& aN, RInteger& aP, RInteger& aQ, |
|
376 RInteger& aDP, RInteger& aDQ, RInteger& aQInv); |
|
377 protected: |
|
378 /** The RSA parameter, p, which is the first factor */ |
|
379 RInteger iP; |
|
380 /** The RSA parameter, q, which is the second factor */ |
|
381 RInteger iQ; |
|
382 /** The RSA parameter, dP, which is the first factor's CRT exponent */ |
|
383 RInteger iDP; |
|
384 /** The RSA parameter, dQ, which is the second factor's CRT exponent */ |
|
385 RInteger iDQ; |
|
386 /** The RSA parameter, qInv, which is the CRT coefficient */ |
|
387 RInteger iQInv; |
|
388 private: |
|
389 CRSAPrivateKeyCRT(const CRSAPrivateKeyCRT&); |
|
390 CRSAPrivateKeyCRT& operator=(const CRSAPrivateKeyCRT&); |
|
391 void ConstructL(); |
|
392 }; |
|
393 |
|
394 /** |
|
395 * This class is capable of generating an RSA public/private key pair. |
|
396 * |
|
397 * By default, it generates 2 prime (standard) CRT private keys. |
|
398 * |
|
399 * @publishedPartner |
|
400 * @released |
|
401 */ |
|
402 class CRSAKeyPair : public CBase |
|
403 { |
|
404 public: |
|
405 /** |
|
406 * Creates a new RSA key pair |
|
407 * |
|
408 * @param aModulusBits The length of the modulus, n (in bits) |
|
409 * @param aKeyType The type of the RSA key |
|
410 * @return A pointer to a new CRSAKeyPair object |
|
411 * |
|
412 * @leave KErrNotSupported If the type of RSA key is not supported |
|
413 */ |
|
414 IMPORT_C static CRSAKeyPair* NewL(TUint aModulusBits, |
|
415 TRSAPrivateKeyType aKeyType = EStandardCRT); |
|
416 |
|
417 /** |
|
418 * Creates a new RSA key pair |
|
419 * |
|
420 * The returned pointer is put onto the cleanup stack. |
|
421 * |
|
422 * @param aModulusBits The length of the modulus, n (in bits) |
|
423 * @param aKeyType The type of the RSA key |
|
424 * @return A pointer to a new CRSAKeyPair object |
|
425 * |
|
426 * @leave KErrNotSupported If the type of RSA key is not supported |
|
427 */ |
|
428 IMPORT_C static CRSAKeyPair* NewLC(TUint aModulusBits, |
|
429 TRSAPrivateKeyType aKeyType = EStandardCRT); |
|
430 |
|
431 /** |
|
432 * Gets the RSA public key |
|
433 * |
|
434 * @return A CRSAPublicKey object |
|
435 */ |
|
436 IMPORT_C const CRSAPublicKey& PublicKey(void) const; |
|
437 |
|
438 /** |
|
439 * Gets the RSA private key |
|
440 * |
|
441 * @return A CRSAPrivateKey object |
|
442 */ |
|
443 IMPORT_C const CRSAPrivateKey& PrivateKey(void) const; |
|
444 |
|
445 /** The destructor frees all resources owned by the object, prior to its destruction. */ |
|
446 IMPORT_C virtual ~CRSAKeyPair(void); |
|
447 protected: |
|
448 /** Default destructor */ |
|
449 IMPORT_C CRSAKeyPair(void); |
|
450 protected: |
|
451 /** The RSA public key */ |
|
452 CRSAPublicKey* iPublic; |
|
453 /** The RSA private key */ |
|
454 CRSAPrivateKey* iPrivate; |
|
455 private: |
|
456 void ConstructL(TUint aModulusBits, TRSAPrivateKeyType aKeyType, |
|
457 TUint aPublicExponent); |
|
458 CRSAKeyPair(const CRSAKeyPair&); |
|
459 CRSAKeyPair& operator=(const CRSAKeyPair&); |
|
460 }; |
|
461 |
|
462 /** |
|
463 * Representation of the parameters used to generate the primes in a |
|
464 * CDSAParameters object. |
|
465 * |
|
466 * Given such a certificate, one can ensure that the DSA |
|
467 * primes contained in CDSAParameters were generated correctly. |
|
468 * |
|
469 * @see CDSAParameters::ValidatePrimesL() |
|
470 * |
|
471 * @publishedPartner |
|
472 * @released |
|
473 */ |
|
474 class CDSAPrimeCertificate : public CBase |
|
475 { |
|
476 public: |
|
477 /** |
|
478 * Creates a new DSA prime certificate from a specified |
|
479 * seed and counter value. |
|
480 * |
|
481 * @param aSeed The seed from a DSA key generation process |
|
482 * @param aCounter The counter value from a DSA key generation process |
|
483 * @return A pointer to a new CDSAPrimeCertificate object |
|
484 */ |
|
485 IMPORT_C static CDSAPrimeCertificate* NewL(const TDesC8& aSeed, |
|
486 TUint aCounter); |
|
487 |
|
488 /** |
|
489 * Creates a new DSA prime certificate from a specified |
|
490 * seed and counter value. |
|
491 * |
|
492 * The returned pointer is put onto the cleanup stack. |
|
493 * |
|
494 * @param aSeed The seed from a DSA key generation process |
|
495 * @param aCounter The counter value from a DSA key generation process |
|
496 * @return A pointer to a new CDSAPrimeCertificate object |
|
497 */ |
|
498 IMPORT_C static CDSAPrimeCertificate* NewLC(const TDesC8& aSeed, |
|
499 TUint aCounter); |
|
500 |
|
501 /** |
|
502 * Gets the seed of the DSA prime certificate |
|
503 * |
|
504 * @return The seed |
|
505 */ |
|
506 IMPORT_C const TDesC8& Seed(void) const; |
|
507 |
|
508 /** |
|
509 * Gets the counter value of the DSA prime certificate |
|
510 * |
|
511 * @return The counter's value |
|
512 */ |
|
513 IMPORT_C TUint Counter(void) const; |
|
514 |
|
515 /** Destructor */ |
|
516 IMPORT_C virtual ~CDSAPrimeCertificate(void); |
|
517 protected: |
|
518 /** |
|
519 * Constructor |
|
520 * |
|
521 * @param aCounter The DSA key generation counter |
|
522 */ |
|
523 IMPORT_C CDSAPrimeCertificate(TUint aCounter); |
|
524 |
|
525 /** Default constructor */ |
|
526 IMPORT_C CDSAPrimeCertificate(void); |
|
527 /** @internalAll */ |
|
528 void ConstructL(const TDesC8& aSeed); |
|
529 protected: |
|
530 /** The DSA key generation seed */ |
|
531 const HBufC8* iSeed; |
|
532 /** The DSA key generation counter */ |
|
533 TUint iCounter; |
|
534 private: |
|
535 CDSAPrimeCertificate(const CDSAPrimeCertificate&); |
|
536 CDSAPrimeCertificate& operator=(const CDSAPrimeCertificate&); |
|
537 }; |
|
538 |
|
539 /** |
|
540 * Concrete class representing the parameters common to both a DSA public and |
|
541 * private key. |
|
542 * |
|
543 * See FIPS 186-2, Digital Signature Standard |
|
544 * |
|
545 * @publishedPartner |
|
546 * @released |
|
547 */ |
|
548 class CDSAParameters : public CBase |
|
549 { |
|
550 public: |
|
551 /** |
|
552 * Gets the DSA parameter, p (the prime) |
|
553 * |
|
554 * @return The DSA parameter, p |
|
555 */ |
|
556 IMPORT_C const TInteger& P(void) const; |
|
557 |
|
558 /** |
|
559 * Gets the DSA parameter, q (the subprime) |
|
560 * |
|
561 * @return The DSA parameter, q |
|
562 */ |
|
563 IMPORT_C const TInteger& Q(void) const; |
|
564 |
|
565 /** |
|
566 * Gets the DSA parameter, g (the base) |
|
567 * |
|
568 * @return The DSA parameter, g |
|
569 */ |
|
570 IMPORT_C const TInteger& G(void) const; |
|
571 |
|
572 /** |
|
573 * Validates the primes regenerated from a DSA prime certificate |
|
574 * |
|
575 * @param aCert The DSA prime certificate that contains the seed and |
|
576 * counter value from a DSA key generation process |
|
577 * @return Whether or not the primes are valid |
|
578 */ |
|
579 IMPORT_C TBool ValidatePrimesL(const CDSAPrimeCertificate& aCert) const; |
|
580 |
|
581 /** |
|
582 * Whether or not the prime is of a valid length |
|
583 * |
|
584 * It is valid if the length of the prime modulus is between KMinPrimeLength |
|
585 * and KMaxPrimeLength bits, and the prime is a multiple of KPrimeLengthMultiple. |
|
586 * |
|
587 * @param aPrimeBits The prime modulus |
|
588 * @return ETrue, if within the constraints; EFalse, otherwise. |
|
589 */ |
|
590 IMPORT_C static TBool ValidPrimeLength(TUint aPrimeBits); |
|
591 |
|
592 /** Destructor */ |
|
593 IMPORT_C virtual ~CDSAParameters(void); |
|
594 |
|
595 /** |
|
596 * Creates a new DSA parameters object from a specified |
|
597 * prime, subprime, and base. |
|
598 * |
|
599 * @param aP The DSA parameter, p (the prime) |
|
600 * @param aQ The DSA parameter, g (the subprime) |
|
601 * @param aG The DSA parameter, g (the base) |
|
602 * @return A pointer to a new CDSAParameters object |
|
603 */ |
|
604 IMPORT_C static CDSAParameters* NewL(RInteger& aP, RInteger& aQ, |
|
605 RInteger& aG); |
|
606 public: |
|
607 /** @internalAll */ |
|
608 static TBool GeneratePrimesL(const TDesC8& aSeed, TUint& aCounter, |
|
609 RInteger& aP, TUint aL, RInteger& aQ, TBool aUseInputCounter=EFalse); |
|
610 protected: |
|
611 /** |
|
612 * Constructor |
|
613 * |
|
614 * @param aP The DSA parameter, p (the prime) |
|
615 * @param aQ The DSA parameter, g (the subprime) |
|
616 * @param aG The DSA parameter, g (the base) |
|
617 */ |
|
618 IMPORT_C CDSAParameters(RInteger& aP, RInteger& aQ, RInteger& aG); |
|
619 |
|
620 /** Default constructor */ |
|
621 IMPORT_C CDSAParameters(void); |
|
622 protected: |
|
623 /** |
|
624 * The DSA parameter, p (the prime). |
|
625 * |
|
626 * A prime modulus whose length is between KMinPrimeLength and KMaxPrimeLength bits, |
|
627 * and is a multiple of KPrimeLengthMultiple. |
|
628 */ |
|
629 RInteger iP; |
|
630 |
|
631 /** |
|
632 * The DSA parameter, q (the subprime) |
|
633 * |
|
634 * This is a 160-bit prime divisor of <code>p-1</code>. |
|
635 */ |
|
636 RInteger iQ; |
|
637 |
|
638 /** |
|
639 * The DSA parameter, g (the base) |
|
640 * |
|
641 * <code>g = h^((p-1)/q) mod p</code>, |
|
642 * |
|
643 * where h is any integer less than <code>p-1</code> such that <code>g > 1</code> |
|
644 */ |
|
645 RInteger iG; |
|
646 private: |
|
647 CDSAParameters(const CDSAParameters&); |
|
648 CDSAParameters& operator=(const CDSAParameters&); |
|
649 }; |
|
650 |
|
651 /** |
|
652 * Representation of a DSA public key. |
|
653 * |
|
654 * @publishedPartner |
|
655 * @released |
|
656 */ |
|
657 class CDSAPublicKey : public CDSAParameters |
|
658 { |
|
659 public: |
|
660 /** |
|
661 * Creates a new DSA public key object from a specified |
|
662 * primes, base, and public key. |
|
663 * |
|
664 * @param aP The DSA parameter, p (the prime) |
|
665 * @param aQ The DSA parameter, q (the subprime) |
|
666 * @param aG The DSA parameter, g (the base) |
|
667 * @param aY The DSA parameter, y (the public key) |
|
668 * @return A pointer to a new CDSAPublicKey object |
|
669 */ |
|
670 IMPORT_C static CDSAPublicKey* NewL(RInteger& aP, RInteger& aQ, |
|
671 RInteger& aG, RInteger& aY); |
|
672 |
|
673 /** |
|
674 * Creates a new DSA public key object from a specified |
|
675 * primes, base, and public key. |
|
676 * |
|
677 * The returned pointer is put onto the cleanup stack. |
|
678 * |
|
679 * @param aP The DSA parameter, p (the prime) |
|
680 * @param aQ The DSA parameter, q (the subprime) |
|
681 * @param aG The DSA parameter, g (the base) |
|
682 * @param aY The DSA parameter, y (the public key) |
|
683 * @return A pointer to a new CDSAPublicKey object |
|
684 */ |
|
685 IMPORT_C static CDSAPublicKey* NewLC(RInteger& aP, RInteger& aQ, |
|
686 RInteger& aG, RInteger& aY); |
|
687 |
|
688 /** |
|
689 * Gets the DSA parameter, y (the public key) |
|
690 * |
|
691 * @return The DSA parameter, y |
|
692 */ |
|
693 IMPORT_C const TInteger& Y(void) const; |
|
694 |
|
695 /** Destructor */ |
|
696 IMPORT_C virtual ~CDSAPublicKey(void); |
|
697 protected: |
|
698 /** |
|
699 * Constructor |
|
700 * |
|
701 * @param aP The DSA parameter, p (the prime) |
|
702 * @param aQ The DSA parameter, q (the subprime) |
|
703 * @param aG The DSA parameter, g (the base) |
|
704 * @param aY The DSA parameter, y (the public key) |
|
705 */ |
|
706 IMPORT_C CDSAPublicKey(RInteger& aP, RInteger& aQ, RInteger& aG, |
|
707 RInteger& aY); |
|
708 |
|
709 /** Default constructor */ |
|
710 IMPORT_C CDSAPublicKey(void); |
|
711 protected: |
|
712 /** |
|
713 * The DSA parameter, y, which is the public key |
|
714 * |
|
715 * <code>y = g^x mod p</code> |
|
716 */ |
|
717 RInteger iY; |
|
718 private: |
|
719 CDSAPublicKey(const CDSAPublicKey&); |
|
720 CDSAPublicKey& operator=(const CDSAPublicKey&); |
|
721 }; |
|
722 |
|
723 /** |
|
724 * Representation of a DSA private key. |
|
725 * |
|
726 * @publishedPartner |
|
727 * @released |
|
728 */ |
|
729 class CDSAPrivateKey : public CDSAParameters |
|
730 { |
|
731 public: |
|
732 /** |
|
733 * Creates a new DSA private key object from a specified |
|
734 * primes, base, and private key. |
|
735 * |
|
736 * @param aP The DSA parameter, p (the prime) |
|
737 * @param aQ The DSA parameter, q (the subprime) |
|
738 * @param aG The DSA parameter, g (the base) |
|
739 * @param aX The DSA parameter, x (the private key) |
|
740 * @return A pointer to a new CDSAPrivateKey object |
|
741 */ |
|
742 IMPORT_C static CDSAPrivateKey* NewL(RInteger& aP, RInteger& aQ, |
|
743 RInteger& aG, RInteger& aX); |
|
744 |
|
745 /** |
|
746 * Creates a new DSA private key object from a specified |
|
747 * primes, base, and private key. |
|
748 * |
|
749 * The returned pointer is put onto the cleanup stack. |
|
750 * |
|
751 * @param aP The DSA parameter, p (the prime) |
|
752 * @param aQ The DSA parameter, q (the subprime) |
|
753 * @param aG The DSA parameter, g (the base) |
|
754 * @param aX The DSA parameter, x (the private key) |
|
755 * @return A pointer to a new CDSAPrivateKey object |
|
756 */ |
|
757 IMPORT_C static CDSAPrivateKey* NewLC(RInteger& aP, RInteger& aQ, |
|
758 RInteger& aG, RInteger& aX); |
|
759 |
|
760 /** |
|
761 * Gets the DSA parameter, x (the private key) |
|
762 * |
|
763 * @return The DSA parameter, x |
|
764 */ |
|
765 IMPORT_C const TInteger& X(void) const; |
|
766 |
|
767 /** Destructor */ |
|
768 IMPORT_C virtual ~CDSAPrivateKey(void); |
|
769 protected: |
|
770 /** |
|
771 * Constructor |
|
772 * |
|
773 * @param aP The DSA parameter, p (the prime) |
|
774 * @param aQ The DSA parameter, q (the subprime) |
|
775 * @param aG The DSA parameter, g (the base) |
|
776 * @param aX The DSA parameter, x (the private key) |
|
777 */ |
|
778 IMPORT_C CDSAPrivateKey(RInteger& aP, RInteger& aQ, RInteger& aG, |
|
779 RInteger& aX); |
|
780 |
|
781 /** Default constructor */ |
|
782 IMPORT_C CDSAPrivateKey(void); |
|
783 protected: |
|
784 /** |
|
785 * The DSA parameter, x, which is the private key |
|
786 * |
|
787 * A pseudorandomly generated integer whose value is between 0 and q. |
|
788 */ |
|
789 RInteger iX; |
|
790 private: |
|
791 CDSAPrivateKey(const CDSAPrivateKey&); |
|
792 CDSAPrivateKey& operator=(const CDSAPrivateKey&); |
|
793 }; |
|
794 |
|
795 /** |
|
796 * This class is capable of generating a DSA public/private key pair. |
|
797 * |
|
798 * @publishedPartner |
|
799 * @released |
|
800 */ |
|
801 class CDSAKeyPair : public CBase |
|
802 { |
|
803 public: |
|
804 /** |
|
805 * Creates a new DSA key pair and also a DSA prime certificate |
|
806 * |
|
807 * @param aSize The length (in bits) of the DSA parameter, p (the prime) |
|
808 * @return A pointer to a new CDSAKeyPair object |
|
809 */ |
|
810 IMPORT_C static CDSAKeyPair* NewL(TUint aSize); |
|
811 |
|
812 /** |
|
813 * Creates a new DSA key pair and also a DSA prime certificate |
|
814 * |
|
815 * The returned pointer is put onto the cleanup stack. |
|
816 * |
|
817 * @param aSize The length (in bits) of the DSA parameter, p (the prime) |
|
818 * @return A pointer to a new CDSAKeyPair object |
|
819 */ |
|
820 IMPORT_C static CDSAKeyPair* NewLC(TUint aSize); |
|
821 |
|
822 /** |
|
823 * Gets the DSA public key |
|
824 * |
|
825 * @return The DSA public key object |
|
826 */ |
|
827 IMPORT_C const CDSAPublicKey& PublicKey(void) const; |
|
828 |
|
829 /** |
|
830 * Gets the DSA private key |
|
831 * |
|
832 * @return The DSA private key object |
|
833 */ |
|
834 IMPORT_C const CDSAPrivateKey& PrivateKey(void) const; |
|
835 |
|
836 /** |
|
837 * Gets the DSA prime certificate (i.e., the seed and counter) |
|
838 * |
|
839 * @return The DSA prime certificate object |
|
840 */ |
|
841 IMPORT_C const CDSAPrimeCertificate& PrimeCertificate(void) const; |
|
842 |
|
843 /** The destructor frees all resources owned by the object, prior to its destruction. */ |
|
844 IMPORT_C virtual ~CDSAKeyPair(void); |
|
845 protected: |
|
846 /** Default constructor */ |
|
847 IMPORT_C CDSAKeyPair(void); |
|
848 protected: |
|
849 /** The DSA public key */ |
|
850 CDSAPublicKey* iPublic; |
|
851 /** The DSA private key */ |
|
852 CDSAPrivateKey* iPrivate; |
|
853 /** The DSA prime certificate */ |
|
854 CDSAPrimeCertificate* iPrimeCertificate; |
|
855 private: |
|
856 void ConstructL(TUint aSize); |
|
857 CDSAKeyPair(const CDSAKeyPair&); |
|
858 CDSAKeyPair& operator=(const CDSAKeyPair&); |
|
859 }; |
|
860 |
|
861 /** |
|
862 * Concrete class representing the parameters common to both |
|
863 * a Diffie-Hellman (DH) public and private key. |
|
864 * |
|
865 * @publishedPartner |
|
866 * @released |
|
867 */ |
|
868 class CDHParameters : public CBase |
|
869 { |
|
870 public: |
|
871 /** |
|
872 * Gets the DH parameter, n |
|
873 * |
|
874 * @return An integer representing the DH parameter, n |
|
875 */ |
|
876 IMPORT_C const TInteger& N(void) const; |
|
877 |
|
878 /** |
|
879 * Gets the DH parameter, g |
|
880 * |
|
881 * @return An integer representing the DH parameter, g |
|
882 */ |
|
883 IMPORT_C const TInteger& G(void) const; |
|
884 |
|
885 /** Destructor */ |
|
886 IMPORT_C virtual ~CDHParameters(void); |
|
887 protected: |
|
888 /** |
|
889 * Constructor |
|
890 * |
|
891 * @param aN The DH parameter, n |
|
892 * @param aG The DH parameter, g |
|
893 */ |
|
894 IMPORT_C CDHParameters(RInteger& aN, RInteger& aG); |
|
895 |
|
896 /** Default constructor */ |
|
897 IMPORT_C CDHParameters(void); |
|
898 protected: |
|
899 /** |
|
900 * The DH parameter, n (a prime number) |
|
901 * |
|
902 * <code>X = g^x mod n</code> (note the case sensitivity) |
|
903 */ |
|
904 RInteger iN; |
|
905 /** |
|
906 * The DH parameter, g (the generator) |
|
907 * |
|
908 * <code>X = g^x mod n</code> (note the case sensitivity) |
|
909 */ |
|
910 RInteger iG; |
|
911 private: |
|
912 CDHParameters(const CDHParameters&); |
|
913 CDHParameters& operator=(const CDHParameters&); |
|
914 }; |
|
915 |
|
916 /** |
|
917 * Representation of a Diffie-Hellman (DH) public key. |
|
918 * |
|
919 * @publishedPartner |
|
920 * @released |
|
921 */ |
|
922 class CDHPublicKey : public CDHParameters |
|
923 { |
|
924 public: |
|
925 /** |
|
926 * Creates a new DH public key from a specified |
|
927 * large prime, generator, and random large integer. |
|
928 * |
|
929 * @param aN The DH parameter, n (a large prime) |
|
930 * @param aG The DH parameter, g (the generator) |
|
931 * @param aX The DH value, X |
|
932 * @return A pointer to a new CDHPublicKey object |
|
933 */ |
|
934 IMPORT_C static CDHPublicKey* NewL(RInteger& aN, RInteger& aG, |
|
935 RInteger& aX); |
|
936 |
|
937 /** |
|
938 * Creates a new DH public key from a specified |
|
939 * large prime, generator, and random large integer. |
|
940 * |
|
941 * The returned pointer is put onto the cleanup stack. |
|
942 * |
|
943 * @param aN The DH parameter, n (a large prime) |
|
944 * @param aG The DH parameter, g (the generator) |
|
945 * @param aX The DH value, X |
|
946 * @return A pointer to a new CDHPublicKey object |
|
947 */ |
|
948 IMPORT_C static CDHPublicKey* NewLC(RInteger& aN, RInteger& aG, |
|
949 RInteger& aX); |
|
950 |
|
951 /** |
|
952 * Gets the DH value, X |
|
953 * |
|
954 * @return The DH value, X |
|
955 */ |
|
956 IMPORT_C const TInteger& X(void) const; |
|
957 |
|
958 /** Destructor */ |
|
959 IMPORT_C virtual ~CDHPublicKey(void); |
|
960 protected: |
|
961 /** |
|
962 * Constructor |
|
963 * |
|
964 * @param aN The DH parameter, n (a large prime) |
|
965 * @param aG The DH parameter, g (the generator) |
|
966 * @param aX The DH value, X |
|
967 */ |
|
968 IMPORT_C CDHPublicKey(RInteger& aN, RInteger& aG, RInteger& aX); |
|
969 |
|
970 /** Constructor */ |
|
971 IMPORT_C CDHPublicKey(void); |
|
972 protected: |
|
973 /** |
|
974 * The DH value, X |
|
975 * |
|
976 * <code>X = g^x mod n</code> (note the case sensitivity) |
|
977 */ |
|
978 RInteger iX; |
|
979 private: |
|
980 CDHPublicKey(const CDHPublicKey&); |
|
981 CDHPublicKey& operator=(const CDHPublicKey&); |
|
982 }; |
|
983 |
|
984 /** |
|
985 * Representation of a Diffie-Hellman (DH) private key. |
|
986 * |
|
987 * @publishedPartner |
|
988 * @released |
|
989 */ |
|
990 class CDHPrivateKey : public CDHParameters |
|
991 { |
|
992 public: |
|
993 /** |
|
994 * Creates a new DH private key from a specified |
|
995 * large prime, generator, and random large integer. |
|
996 * |
|
997 * @param aN The DH parameter, n (a large prime) |
|
998 * @param aG The DH parameter, g (the generator) |
|
999 * @param ax The DH value, x (a random large integer) |
|
1000 * @return A pointer to a new CDHPrivateKey object |
|
1001 */ |
|
1002 IMPORT_C static CDHPrivateKey* NewL(RInteger& aN, RInteger& aG, |
|
1003 RInteger& ax); |
|
1004 |
|
1005 /** |
|
1006 * Creates a new DH private key from a specified |
|
1007 * large prime, generator, and random large integer. |
|
1008 * |
|
1009 * The returned pointer is put onto the cleanup stack. |
|
1010 * |
|
1011 * @param aN The DH parameter, n (a large prime) |
|
1012 * @param aG The DH parameter, g (the generator) |
|
1013 * @param ax The DH value, x (a random large integer) |
|
1014 * @return A pointer to a new CDHPrivateKey object |
|
1015 */ |
|
1016 IMPORT_C static CDHPrivateKey* NewLC(RInteger& aN, RInteger& aG, |
|
1017 RInteger& ax); |
|
1018 |
|
1019 /** |
|
1020 * Gets the DH value, x, which is a random large integer. |
|
1021 * |
|
1022 * @return The DH value, x |
|
1023 */ |
|
1024 IMPORT_C const TInteger& x(void) const; |
|
1025 |
|
1026 /** Destructor */ |
|
1027 IMPORT_C virtual ~CDHPrivateKey(void); |
|
1028 protected: |
|
1029 /** |
|
1030 * Constructor |
|
1031 * |
|
1032 * @param aN The DH parameter, n (a large prime) |
|
1033 * @param aG The DH parameter, g (the generator) |
|
1034 * @param ax The DH value, x (a random large integer) |
|
1035 */ |
|
1036 IMPORT_C CDHPrivateKey(RInteger& aN, RInteger& aG, RInteger& ax); |
|
1037 |
|
1038 /** Constructor */ |
|
1039 IMPORT_C CDHPrivateKey(void); |
|
1040 protected: |
|
1041 /** |
|
1042 * The DH value, x, which is a random large integer. |
|
1043 * |
|
1044 * <code>X = g^x mod n</code> (note the case sensitivity) |
|
1045 */ |
|
1046 RInteger ix; |
|
1047 private: |
|
1048 CDHPrivateKey(const CDHPrivateKey&); |
|
1049 CDHPrivateKey& operator=(const CDHPrivateKey&); |
|
1050 }; |
|
1051 |
|
1052 /** |
|
1053 * This class is capable of generating a Diffie-Hellman (DH) public/private key pair. |
|
1054 * |
|
1055 * @publishedPartner |
|
1056 * @released |
|
1057 */ |
|
1058 class CDHKeyPair : public CBase |
|
1059 { |
|
1060 public: |
|
1061 /** |
|
1062 * Creates a new DH key pair from a random large integer, |
|
1063 * and a specified large prime and generator. |
|
1064 * |
|
1065 * @param aN The DH parameter, n (a large prime) |
|
1066 * @param aG The DH parameter, g (the generator) |
|
1067 * @return A pointer to a new CDHKeyPair object |
|
1068 * |
|
1069 * @leave KErrArgument If aG is out of bounds |
|
1070 */ |
|
1071 IMPORT_C static CDHKeyPair* NewL(RInteger& aN, RInteger& aG); |
|
1072 |
|
1073 /** |
|
1074 * Creates a new DH key pair from a random large integer, |
|
1075 * and a specified large prime and generator. |
|
1076 * |
|
1077 * The returned pointer is put onto the cleanup stack. |
|
1078 * |
|
1079 * @param aN The DH parameter, n (a large prime) |
|
1080 * @param aG The DH parameter, g (the generator) |
|
1081 * @return A pointer to a new CDHKeyPair object |
|
1082 * |
|
1083 * @leave KErrArgument If aG is out of bounds |
|
1084 */ |
|
1085 IMPORT_C static CDHKeyPair* NewLC(RInteger& aN, RInteger& aG); |
|
1086 |
|
1087 /** |
|
1088 * Creates a new DH key pair from a specified |
|
1089 * large prime, generator, and random large integer. |
|
1090 * |
|
1091 * @param aN The DH parameter, n (a large prime) |
|
1092 * @param aG The DH parameter, g (the generator) |
|
1093 * @param ax The DH value, x (a random large integer) |
|
1094 * @return A pointer to a new CDHKeyPair object |
|
1095 * |
|
1096 * @leave KErrArgument If either aG or ax are out of bounds |
|
1097 */ |
|
1098 IMPORT_C static CDHKeyPair* NewL(RInteger& aN, RInteger& aG, RInteger& ax); |
|
1099 |
|
1100 /** |
|
1101 * Creates a new DH key pair from a specified |
|
1102 * large prime, generator, and random large integer. |
|
1103 * |
|
1104 * The returned pointer is put onto the cleanup stack. |
|
1105 * |
|
1106 * @param aN The DH parameter, n (a large prime) |
|
1107 * @param aG The DH parameter, g (the generator) |
|
1108 * @param ax The DH value, x (a random large integer) |
|
1109 * @return A pointer to a new CDHKeyPair object |
|
1110 * |
|
1111 * @leave KErrArgument If either aG or ax are out of bounds |
|
1112 */ |
|
1113 IMPORT_C static CDHKeyPair* NewLC(RInteger& aN, RInteger& aG, RInteger& ax); |
|
1114 |
|
1115 /** |
|
1116 * Gets the DH public key |
|
1117 * |
|
1118 * @return The DH public key |
|
1119 */ |
|
1120 IMPORT_C const CDHPublicKey& PublicKey(void) const; |
|
1121 |
|
1122 /** |
|
1123 * Gets the DH private key |
|
1124 * |
|
1125 * @return The DH private key |
|
1126 */ |
|
1127 IMPORT_C const CDHPrivateKey& PrivateKey(void) const; |
|
1128 |
|
1129 /** The destructor frees all resources owned by the object, prior to its destruction. */ |
|
1130 IMPORT_C virtual ~CDHKeyPair(void); |
|
1131 protected: |
|
1132 /** Default constructor */ |
|
1133 IMPORT_C CDHKeyPair(void); |
|
1134 |
|
1135 /** |
|
1136 * Constructor |
|
1137 * |
|
1138 * @param aN The DH parameter, n (a large prime) |
|
1139 * @param aG The DH parameter, g (the generator) |
|
1140 */ |
|
1141 IMPORT_C void ConstructL(RInteger& aN, RInteger& aG); |
|
1142 |
|
1143 /** |
|
1144 * Constructor |
|
1145 * |
|
1146 * @param aN The DH parameter, n (a large prime) |
|
1147 * @param aG The DH parameter, g (the generator) |
|
1148 * @param ax The DH value, x (a random large integer) |
|
1149 */ |
|
1150 IMPORT_C void ConstructL(RInteger& aN, RInteger& aG, RInteger& ax); |
|
1151 protected: |
|
1152 /** The DH public key */ |
|
1153 CDHPublicKey* iPublic; |
|
1154 /** The DH private key */ |
|
1155 CDHPrivateKey* iPrivate; |
|
1156 private: |
|
1157 CDHKeyPair(const CDHKeyPair&); |
|
1158 CDHKeyPair& operator=(const CDHKeyPair&); |
|
1159 }; |
|
1160 #endif // __ASYMMETRICKEYS_H__ |