Flush app registry before releasing file lock
[platform/framework/native/appfw.git] / inc / FSecCryptoISymmetricCipher.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  *      @file   FSecCryptoISymmetricCipher.h
19  *      @brief  This is the header file for the %ISymmetricCipher interface.
20  *
21  *      This header file contains the declarations of the %ISymmetricCipher interface.
22  */
23 #ifndef _FSEC_CRYPTO_ISYMMETRIC_CIPHER_H_
24 #define _FSEC_CRYPTO_ISYMMETRIC_CIPHER_H_
25
26 #include <FBaseString.h>
27 #include <FBaseByteBuffer.h>
28 #include <FSecISecretKey.h>
29 #include <FSecCryptoTypes.h>
30
31
32 namespace Tizen { namespace Security { namespace Crypto
33 {
34 /**
35  *      @interface      ISymmetricCipher
36  *      @brief          This interface provides the functionalities of a symmetric cryptographic cipher for encryption and decryption.
37  *
38  *      @since          2.0
39  *
40  *      The %ISymmetricCipher interface provides the functionalities of a symmetric cryptographic cipher for encryption and decryption. @n
41  *
42  *      For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/ciphers.htm">Ciphers</a>. @n
43  *
44  *      The following example demonstrates how to use the %ISymmetricCipher interface.
45  *      @code
46  *
47  *      void
48  *      MyClass::TestSymmetricCipherSample(void)
49  *      {
50  *              // #####AES CBC Test Vector(128-bits key)#####
51  *              // PlainText: 48 Bytes
52  *              const int messageLen    = 48;
53  *              static const byte message[messageLen] = {
54  *                      0x87, 0x3C, 0x66, 0x1D, 0x2C, 0x0D, 0x49, 0x2D,
55  *                      0x6C, 0x76, 0xBE, 0x44, 0x57, 0x39, 0xB8, 0x28,
56  *                      0x84, 0x5E, 0x2A, 0x15, 0x18, 0x3B, 0x1D, 0x00,
57  *                      0xA7, 0x6E, 0x80, 0x4D, 0x22, 0xF1, 0x2A, 0x6B,
58  *                      0xBA, 0xFE, 0xA8, 0x02, 0x2B, 0xC2, 0x97, 0x01,
59  *                      0x59, 0x0F, 0x3C, 0x2A, 0x67, 0x8B, 0x98, 0x69
60  *              };
61  *
62  *              // CipherText: 48 Bytes
63  *              const int cipherLen     = 48;
64  *              static const byte cipher[cipherLen] = {
65  *                      0x10, 0x84, 0x9D, 0x24, 0xEB, 0x22, 0xE0, 0x7F,
66  *                      0xA8, 0x57, 0xE9, 0xA0, 0x4F, 0xE2, 0x3D, 0xE5,
67  *                      0xC1, 0x51, 0x7E, 0xEB, 0xF8, 0xB3, 0x3A, 0xA2,
68  *                      0xDC, 0xF4, 0x8B, 0xDC, 0x14, 0x0A, 0xC7, 0x58,
69  *                      0x85, 0x6B, 0x0D, 0xE9, 0x30, 0x8B, 0xA1, 0x71,
70  *                      0xD5, 0x0B, 0x14, 0x97, 0xEF, 0xAD, 0x22, 0x8D
71  *              };
72  *
73  *              // KEY: 16Bytes
74  *              const int secretKeyLen  = 16;
75  *              static const byte secretKey[secretKeyLen] = {
76  *                      0x62, 0x5C, 0xC7, 0x7E, 0xEA, 0x7B, 0xA5, 0x4D,
77  *                      0x47, 0xCE, 0xAF, 0x26, 0x9E, 0xB1, 0x6C, 0x2D
78  *              };
79  *
80  *              // IV: 16Bytes
81  *              const int ivectorLen    = 16;
82  *              static const byte ivector[ivectorLen] = {
83  *                              0x3E, 0xB5, 0x01, 0x45, 0xE4, 0xF8, 0x75, 0x3F,
84  *                      0x08, 0x9D, 0x9F, 0x57, 0x3B, 0x63, 0xEF, 0x4B
85  *              };
86  *
87  *              result r = E_FAILURE;
88  *              String transformation;
89  *              ISymmetricCipher        *pCipherEnc     = null;
90  *              ISymmetricCipher        *pCipherDec     = null;
91  *              SecretKeyGenerator      *pKeyGen        = null;
92  *              ISecretKey                      *pKey           = null;
93  *              ByteBuffer                      input;
94  *              ByteBuffer                      *pOutput        = null;
95  *              ByteBuffer                      *pOutput2       = null;
96  *              ByteBuffer                      keyBytes;
97  *              ByteBuffer                      iv;
98  *
99  *              // Input
100  *              input.Construct(messageLen);
101  *              input.SetArray(message, 0, messageLen);
102  *              input.Flip();
103  *
104  *              // Key
105  *              keyBytes.Construct(secretKeyLen);
106  *              keyBytes.SetArray(secretKey, 0, secretKeyLen);
107  *              keyBytes.Flip();
108  *
109  *              // IV setting
110  *              iv.Construct(ivectorLen);
111  *              iv.SetArray(ivector, 0, ivectorLen);
112  *              iv.Flip();
113  *
114  *              pCipherEnc = new AesCipher();
115  *              if (pCipherEnc == null)
116  *              {
117  *                      goto CATCH;
118  *              }
119  *
120  *              transformation  = "CBC/128/NOPADDING";
121  *
122  *              r = pCipherEnc->Construct(transformation, CIPHER_ENCRYPT);
123  *              if (IsFailed(r))
124  *              {
125  *                      goto CATCH;
126  *              }
127  *
128  *              // Generates the key.
129  *              pKeyGen = new SecretKeyGenerator();
130  *              if (pKeyGen == null)
131  *              {
132  *                      goto CATCH;
133  *              }
134  *
135  *              r = pKeyGen->Construct(keyBytes);
136  *              if (IsFailed(r))
137  *              {
138  *                      goto CATCH;
139  *              }
140  *
141  *              pKey = pKeyGen->GenerateKeyN();
142  *              if (pKey == null)
143  *              {
144  *                      goto CATCH;
145  *              }
146  *
147  *              r = pCipherEnc->SetKey(*pKey);
148  *              if (IsFailed(r))
149  *              {
150  *                      goto CATCH;
151  *              }
152  *
153  *              r = pCipherEnc->SetInitialVector(iv);
154  *              if (IsFailed(r))
155  *              {
156  *                      goto CATCH;
157  *              }
158  *
159  *              pOutput = pCipherEnc->EncryptN(input);
160  *              if (pOutput == null)
161  *              {
162  *                      r = GetLastResult();
163  *                      goto CATCH;
164  *              }
165  *
166  *              pCipherDec = new AesCipher();
167  *              if (pCipherDec == null)
168  *              {
169  *                      goto CATCH;
170  *              }
171  *
172  *              r = pCipherDec->Construct(transformation, CIPHER_DECRYPT);
173  *              if (IsFailed(r))
174  *              {
175  *                      goto CATCH;
176  *              }
177  *
178  *              r = pCipherDec->SetKey(*pKey);
179  *              if (IsFailed(r))
180  *              {
181  *                      goto CATCH;
182  *              }
183  *
184  *              r = pCipherDec->SetInitialVector(iv);
185  *              if (IsFailed(r))
186  *              {
187  *                      goto CATCH;
188  *              }
189  *
190  *              pOutput2 = pCipherDec->DecryptN(*pOutput);
191  *              if (pOutput2 == null)
192  *              {
193  *                      r = GetLastResult();
194  *                      goto CATCH;
195  *              }
196  *
197  *              if (memcmp(pOutput->GetPointer(), cipher, cipherLen) != 0)
198  *              {
199  *                      goto CATCH;
200  *              }
201  *
202  *              if (memcmp(pOutput2->GetPointer(), input.GetPointer(), input.GetRemaining()) != 0)
203  *              {
204  *                      goto CATCH;
205  *              }
206  *
207  *      CATCH:
208  *              delete pCipherEnc;
209  *              delete pCipherDec;
210  *              delete pKeyGen;
211  *              delete pOutput;
212  *              delete pOutput2;
213  *              delete pKey;
214  *      }
215  *
216  *       @endcode
217  *
218  *
219  */
220
221 class _OSP_EXPORT_ ISymmetricCipher
222 {
223
224 public:
225         /**
226          * This polymorphic destructor should be overridden if required. @n 
227          * This way, the destructors of the derived classes are called when the destructor of this interface is called.
228          *
229          *      @since          2.0
230          */
231         virtual ~ISymmetricCipher(void) {}
232
233         /**
234          *      Initializes this instance of %ISymmetricCipher with the specified operation mode and padding scheme.
235          *
236          *      @since          2.0
237          *
238          *      @return         An error code
239          *      @param[in]      transformation                  The requested mode or key bit or padding scheme
240          *      @param[in]      opMode                                  The cipher operation mode @n
241          *                                                                              For example, @c CIPHER_ENCRYPT, @c CIPHER_DECRYPT, @c CIPHER_WRAP, or @c CIPHER_UNWRAP.
242          *      @exception      E_SUCCESS                               The method is successful.
243          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
244          *      @exception      E_INVALID_ARG                   The specified @c opMode does not contain a valid value for the cipher operation.
245          *      @exception      E_UNSUPPORTED_ALGORITHM The algorithm is not supported.
246          */
247         virtual result Construct(const Tizen::Base::String& transformation, enum CipherOperation opMode = Tizen::Security::Crypto::CIPHER_ENCRYPT) = 0;
248
249         /**
250          *      Sets the symmetric key for encryption or decryption.
251          *
252          *      @since          2.0
253          *
254          *      @return         An error code
255          *      @param[in]      key                                             An instance of ISecretKey to set
256          *      @exception      E_SUCCESS                               The method is successful.
257          *      @exception      E_INVALID_ARG                   The specified @c key is invalid.
258          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
259          */
260         virtual result SetKey(const Tizen::Security::ISecretKey& key) = 0;
261
262         /**
263          *      Sets the initial vector.
264          *
265          *      @since          2.0
266          *
267          *      @return         An error code
268          *      @param[in]      initialVector                   The initial vector to set
269          *      @exception      E_SUCCESS                               The method is successful.
270          *      @exception      E_INVALID_ARG                   The specified input parameter is invalid.
271          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
272          */
273         virtual result SetInitialVector(const Tizen::Base::ByteBuffer& initialVector) = 0;
274
275         /**
276          *      Encrypts the data (single-part).
277          *
278          *      @since          2.0
279          *
280          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
281          *                              else @c null if an error occurs
282          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
283          *      @exception      E_SUCCESS                               The method is successful.
284          *      @exception      E_INVALID_ARG                   The input Tizen::Base::ByteBuffer is empty or contains invalid data.
285          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
286          *      @exception      E_KEY_NOT_FOUND                 The key is not found.
287          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
288          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
289          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
290          *                                                                              - A system error has occurred.
291          *                                                                              - The method has failed to operate with the openssl library.
292          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
293          */
294         virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input) = 0;
295
296         /**
297          *      Decrypts the data (single-part).
298          *
299          *      @since          2.0
300          *
301          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
302          *                              else @c null if an error occurs
303          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
304          *      @exception      E_SUCCESS                               The method is successful.
305          *      @exception      E_INVALID_ARG                   The input Tizen::Base::ByteBuffer is empty or contains invalid data.
306          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
307          *      @exception      E_KEY_NOT_FOUND                 The key is not found.
308          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
309          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
310          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
311          *                                                                              - A system error has occurred.
312          *                                                                              - The method has failed to operate with the openssl library.
313          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
314          */
315         virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input) = 0;
316
317         /**
318          * Initializes the instance of %ISymmetricCipher for the multiple-part encryption or decryption operation.
319          *
320          *
321          *      @since          2.0
322          *
323          *      @return         An error code
324          *      @exception      E_SUCCESS                               The method is successful.
325          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
326          *      @exception      E_KEY_NOT_FOUND                 The key is not found.
327          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
328          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
329          *                                                                              - A system error has occurred.
330          *                                                                              - The method has failed to operate with the openssl library.
331          */
332         virtual result Initialize(void) = 0;
333
334         /**
335          *      Updates the instance of %ISymmetricCipher for the multiple-part encryption or decryption operation.
336          *
337          *      @since          2.0
338          *
339          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
340          *                              else @c null if an error occurs
341          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
342          *      @exception      E_SUCCESS                               The method is successful.
343          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
344          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
345          *      @exception      E_INVALID_ARG                   The input Tizen::Base::ByteBuffer is empty or contains invalid data.
346          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
347          *                                                                              - A system error has occurred.
348          *                                                                              - The method has failed to operate with the openssl library.
349          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
350          *      @remarks        The specific error code can be accessed using the GetLastResult() method.
351          */
352         virtual Tizen::Base::ByteBuffer* UpdateN(const Tizen::Base::ByteBuffer& input) = 0;
353
354         /**
355          *      Finalizes the instance of %ISymmetricCipher for the multiple-part encryption or decryption operation.
356          *
357          *      @since          2.0
358          *
359          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
360          *                              else @c null if an error occurs
361          *      @exception      E_SUCCESS                               The method is successful.
362          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
363          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
364          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
365          *                                                                              - A system error has occurred.
366          *                                                                              - The method has failed to operate with the openssl library.
367          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
368          *      @remarks                The specific error code can be accessed using the GetLastResult() method.
369          */
370         virtual Tizen::Base::ByteBuffer* FinalizeN(void) = 0;
371
372         /**
373          *      Wraps a key.
374          *
375          *      @since          2.0
376          *
377          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
378          *                              else @c null if an error occurs
379          *      @param[in]      secretKey                               The secret key to wrap
380          *      @exception      E_SUCCESS                               The method is successful.
381          *      @exception      E_INVALID_ARG                   The input Tizen::Base::ByteBuffer is empty or contains invalid data.
382          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
383          *      @exception      E_KEY_NOT_FOUND                 The key is not found.
384          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
385          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
386          *                                                                              - A system error has occurred.
387          *                                                                              - The method has failed to operate with the openssl library.
388          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
389          *      @remarks                
390          *                      - The specific error code can be accessed using the GetLastResult() method.
391          *                      - This operation is only supported in AesCipher.
392          */
393         virtual Tizen::Base::ByteBuffer* WrapN(const Tizen::Base::ByteBuffer& secretKey) = 0;
394
395         /**
396          *      Unwraps a previously wrapped key.
397          *
398          *      @since          2.0
399          *
400          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
401          *                              else @c null if an error occurs
402          *      @param[in]      wrappedKey                              The wrapped key to unwrap
403          *      @exception      E_SUCCESS                               The method is successful.
404          *      @exception      E_INVALID_ARG                   The specified @c wrappedKey Tizen::Base::ByteBuffer is invalid.
405          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
406          *      @exception      E_KEY_NOT_FOUND                 The key is not found.
407          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
408          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
409          *                                                                              - A system error has occurred.
410          *                                                                              - The method has failed to operate with the openssl library.
411          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.      
412          *      @remarks                
413          *                      - The specific error code can be accessed using the GetLastResult() method.
414          *                      - This operation is only supported in AesCipher.
415          */
416         virtual Tizen::Base::ByteBuffer* UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey) = 0;
417
418 protected:
419         //
420         //      This method is for internal use only. Using this method can cause behavioral, security-related,
421         //      and consistency-related issues in the application.
422         //
423         //      This method is reserved and may change its name at any time without prior notice.
424         //
425         //      @since 2.0
426         //
427         virtual void ISymmetricCipher_Reserved1(void) {}
428
429 }; //ISymmetricCipher
430
431 } } } //Tizen::Security::Crypto
432
433 #endif //_FSEC_CRYPTO_ISYMMETRIC_CIPHER_H_