|
1 /* |
|
2 * Copyright (c) 2000, 2002, 2004 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 "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: This file contains the header of CCrCrypto class. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef CR_CRYPTO_H |
|
20 #define CR_CRYPTO_H |
|
21 |
|
22 // INCLUDES |
|
23 #include <e32base.h> // CBase. |
|
24 |
|
25 #include "cralginfo.h" // CCrAlgInfo |
|
26 #include <crdata.h> |
|
27 |
|
28 // CONSTANTS |
|
29 const TInt NO_PROCESS = -1; |
|
30 const TInt KLenFileBuffer = 1000; |
|
31 |
|
32 // DATA TYPES |
|
33 // Padding rules. |
|
34 enum TPaddingRule |
|
35 { |
|
36 ECrNone, |
|
37 ECrPKCS1, |
|
38 ECrSSLv3 |
|
39 }; |
|
40 |
|
41 // Algorithms. |
|
42 enum TCrAsymAlgorithm |
|
43 { |
|
44 ECrRSA, |
|
45 ECrDSA, |
|
46 ECrLAST_ASYMM_CRYPTO |
|
47 }; |
|
48 |
|
49 |
|
50 // CLASS DECLARATION |
|
51 |
|
52 /** |
|
53 * class CCrCrypto |
|
54 * CCrCrypto crypto API, which is used in CrPKCS12. |
|
55 * |
|
56 * @lib crpkcs12.lib |
|
57 * @since Series 60 3.0 |
|
58 */ |
|
59 NONSHARABLE_CLASS( CCrCrypto ): public CBase |
|
60 { |
|
61 // Constructors and destructors. |
|
62 private: |
|
63 CCrCrypto(); |
|
64 |
|
65 void ConstructL(); |
|
66 |
|
67 public: |
|
68 ~CCrCrypto(); |
|
69 static CCrCrypto* NewLC(); |
|
70 static CCrCrypto* NewL(); |
|
71 |
|
72 public: // Functions. |
|
73 |
|
74 /** |
|
75 * Initialize encryption or decryption with Triple DES algorithm |
|
76 * using three different keys. |
|
77 * @param aKey1 1. key. |
|
78 * @param aKey2 2. key. |
|
79 * @param aKey3 3. key. |
|
80 * @param aIV Initialization vector. |
|
81 * @param aEncrypt Encrypt if true. |
|
82 * @param aMode Algorithm mode. |
|
83 * @param aPadRule Padding rule. |
|
84 * @return KCrCrypto | KCrUnknownMode |
|
85 * KCrCrypto | KCrUnknownLibrary |
|
86 * KCrOK |
|
87 */ |
|
88 TCrStatus InitCrypt3DESL( |
|
89 const TDesC8& aKey1, |
|
90 const TDesC8& aKey2, |
|
91 const TDesC8& aKey3, |
|
92 const TDesC8& aIV, |
|
93 TBool aEncrypt = ETrue, |
|
94 TCrSymmMode aMode = ECrCBC, |
|
95 TPaddingRule aPadRule = ECrPKCS1); |
|
96 |
|
97 /** |
|
98 * Initialize encryption or decryption with RC2 algorithm. |
|
99 * @param aKey Key. |
|
100 * @param aIV Initialization vector. |
|
101 * @param aEncrypt Encrypt if true. |
|
102 * @param aEffectiveKeyLen Effective key length in bits |
|
103 * @param aMode Algorithm mode. |
|
104 * @param aPadRule Padding rule. |
|
105 * @return KCrCrypto | KCrUnknownMode |
|
106 * KCrOK |
|
107 */ |
|
108 TCrStatus InitCryptRC2L( |
|
109 const TDesC8& aKey, |
|
110 const TDesC8& aIV, |
|
111 TBool aEncrypt = ETrue, |
|
112 |
|
113 TInt aEffectiveKeyLen = 0, |
|
114 |
|
115 TCrSymmMode aMode = ECrCBC, |
|
116 TPaddingRule aPadRule = ECrPKCS1); |
|
117 |
|
118 /** |
|
119 * Initialize HMAC message digest algorithm. |
|
120 * @param aKey Key. |
|
121 * @param aDigestAlg Message digest algorithm that HMAC uses. |
|
122 * @return KCrCrypto | KCrUnknownMode |
|
123 * KCrOK |
|
124 */ |
|
125 TCrStatus InitDigestHMACL( |
|
126 const TDesC8& aKey, |
|
127 TCrAlgorithm aDigestAlg); |
|
128 |
|
129 |
|
130 /** |
|
131 * Initialize message digest with MD2 algorithm. |
|
132 * @param aAlgorithm Digest algorithm |
|
133 * @return KCrOK or |
|
134 * KCrCrypto | KCrNotSupportedAlg |
|
135 */ |
|
136 TCrStatus InitDigestL(TCrAlgorithm aAlgorithm); |
|
137 |
|
138 |
|
139 |
|
140 /** |
|
141 * Process given source data with initialized crypto operations. |
|
142 * If symmetric crypto is initialized appends to aTrg encrypted |
|
143 * or decrypted data without last portion. If only digest algorithm |
|
144 * is initialized, aTrg is not used. |
|
145 * @param aSrc Source buffer. |
|
146 * @param aTrg Target buffer. |
|
147 * @return KCrOK |
|
148 * KCrNotSupportedAlg |
|
149 * KCrUndefinedLibrary |
|
150 * KCrUnknownLibrary |
|
151 * KCrUnknownMode |
|
152 */ |
|
153 TCrStatus ProcessL(const TDesC8& aSrc, TDes8& aTrg); |
|
154 |
|
155 |
|
156 // Finalize symmetric algorithms. Function appends to aTrg |
|
157 // encrypted or decrypted last portion. |
|
158 TCrStatus FinalCryptL(TDes8& aTrg); |
|
159 |
|
160 // Finalize message digest algorithms. |
|
161 // aTrg contains message digest of the data. |
|
162 TCrStatus FinalDigest(TDes8& aTrg); |
|
163 |
|
164 // Derive key(s) or IV vector from password, salt and iteration count. |
|
165 TCrStatus DeriveKeyPKCS12L( |
|
166 const TDesC8& aPassword, |
|
167 const TDesC8& aSalt, |
|
168 const TInt aIterationCount, |
|
169 TCrAlgorithm aHashFunc, |
|
170 const TUint8 aID, |
|
171 const TInt aNumberOfBytes, |
|
172 TDes8& aTrg); |
|
173 |
|
174 private: // Functions |
|
175 // Removes the last portion of the data when encrypting or decrypting. |
|
176 TInt RemoveLastBlock( |
|
177 TDesC8& aSrc, |
|
178 const TInt aBlockSize, |
|
179 CCrAlgInfo* algInfo); |
|
180 |
|
181 TInt MesDigestInputSize(TCrAlgorithm aDigestAlg); |
|
182 TInt MesDigestOutputSize(TCrAlgorithm aDigestAlg); |
|
183 void Reset(); |
|
184 |
|
185 private: // Data. |
|
186 // Infos about initialized algorithms are collected in this array. |
|
187 CArrayPtrFlat<CCrAlgInfo>* iAlgorithmInfos; |
|
188 }; |
|
189 |
|
190 #endif // CR_CRYPTO_H |
|
191 |
|
192 // End of File |