Added new method to the NotificationManager Interface
[platform/framework/native/appfw.git] / inc / FSecCryptoAesCipher.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           FSecCryptoAesCipher.h
19  *      @brief          This is the header file for the %AesCipher class.
20  *
21  *      This header file contains the declarations of the %AesCipher class.
22  */
23 #ifndef _FSEC_CRYPTO_AES_CIPHER_H_
24 #define _FSEC_CRYPTO_AES_CIPHER_H_
25
26 #include <FSecCryptoISymmetricCipher.h>
27
28 struct evp_cipher_st;
29
30
31 namespace Tizen { namespace Security { namespace Crypto
32 {
33
34 class _SymmetricCipher;
35 /**
36  *      @class          AesCipher
37  *      @brief          This class provides methods for encryption and decryption using the Advanced Encryption Standard (AES) method.
38  *
39  *      @since          2.0
40  *
41  *      The %AesCipher class provides a symmetric cipher using the Advanced Encryption Standard (AES) method.
42  *      This class allows to set appropriate values for the requested mode/key bit/padding scheme and cipher operation mode (::CIPHER_ENCRYPT, ::CIPHER_DECRYPT, 
43  *      ::CIPHER_WRAP, or ::CIPHER_UNWRAP) parameters. @n
44  *
45  *      For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/ciphers.htm">Ciphers</a>.
46  *
47  *      @see    ISymmetricCipher
48  *      @see    DesCipher
49  *      @see    DesEdeCipher
50  */
51 class _OSP_EXPORT_ AesCipher
52         : public virtual ISymmetricCipher
53         , public Tizen::Base::Object
54 {
55
56 public:
57         /**
58          *      The object is not fully constructed after this constructor is called. @n 
59          *      For full construction, the Construct() method must be called right after calling this constructor.
60          *
61          *      @since          2.0
62          */
63         AesCipher(void);
64
65         /**
66          *      This destructor overrides Tizen::Base::Object::~Object().
67          *
68          *      @since          2.0
69          */
70         virtual ~AesCipher(void);
71
72         /**
73          *      Initializes this instance of %AesCipher with the specified parameters.
74          *
75          *      @since          2.0
76          *
77          *      @return         An error code
78          *      @param[in]      transformation                  The requested mode/key bit/padding scheme @n
79          *                                                                              For example, "CBC/128/NOPADDING" or "CBC/192/NOPADDING".
80          *      @param[in]      opMode                                  The cipher operation mode @n
81          *                                                                              For example, @c CIPHER_ENCRYPT, @c CIPHER_DECRYPT, @c CIPHER_WRAP, or @c CIPHER_UNWRAP.
82          *      @exception      E_SUCCESS                               The method is successful.
83          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
84          *      @exception      E_INVALID_ARG                   Either of the following conditions has occurred:
85          *                                                                              - A specified input parameter is invalid.
86          *                                                                              - The specified @c opMode does not contain a valid value for the cipher operation.
87          *      @remarks                If @c opMode is not matching the actual operation, the result of the operation is @c null and an exception is returned. @n
88          *                              For example, if @c opMode is set to @c CIPHER_ENCRYPT, @c CIPHER_WRAP, or @c CIPHER_UNWRAP and the DecryptN() 
89          *                              method is used, then the result obtained is @c null and an exception is returned.
90          */
91         virtual result Construct(const Tizen::Base::String& transformation, enum CipherOperation opMode);
92
93         /**
94          *      Sets the symmetric key for encryption or decryption.
95          *
96          *      @since          2.0
97          *
98          *      @return         An error code
99          *      @param[in]      key                                             An instance of ISecretKey
100          *      @exception      E_SUCCESS                               The method is successful.
101          *      @exception      E_INVALID_ARG                   The specified @c key is invalid.
102          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
103          */
104         virtual result SetKey(const Tizen::Security::ISecretKey& key);
105
106         /**
107          *      Sets the specified initial vector.
108          *
109          *      @since          2.0
110          *
111          *      @return         An error code
112          *      @param[in]      initialVector                   The initial vector
113          *      @exception      E_SUCCESS                               The method is successful.
114          *      @exception      E_INVALID_ARG                   The specified input parameter is invalid.
115          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
116          */
117         virtual result SetInitialVector(const Tizen::Base::ByteBuffer& initialVector);
118
119         /**
120          *      Encrypts the specified data (single-part).
121          *
122          *      @since          2.0
123          *      @pre            Before calling this method, set a secret key and an initial vector using SetKey() and SetInitialVector().
124          *
125          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
126          *                              else @c null if an error occurs
127          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
128          *      @exception      E_SUCCESS                               The method is successful.
129          *      @exception      E_INVALID_ARG                   The specified Tizen::Base::ByteBuffer instance is invalid or empty.
130          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
131          *      @exception      E_KEY_NOT_FOUND                 The specified key is not found.
132          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
133          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
134          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
135          *                                                                              - A system error has occurred.
136          *                                                                              - The method has failed to operate with the openssl library.
137          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
138          *      @remarks        The specific error code can be accessed using the GetLastResult() method.
139          */
140         virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input);
141
142         /**
143          *      Decrypts the data (single-part).
144          *
145          *      @since          2.0
146          *      @pre            Before calling this method, set a secret key and an initial vector using SetKey() and SetInitialVector().
147          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
148          *                              else @c null if an error occurs
149          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
150          *      @exception      E_SUCCESS                               The method is successful.
151          *      @exception      E_INVALID_ARG                   The specified Tizen::Base::ByteBuffer instance is invalid or empty.
152          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
153          *      @exception      E_KEY_NOT_FOUND                 The specified key is not found.
154          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
155          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
156          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
157          *                                                                              - A system error has occurred.
158          *                                                                              - The method has failed to operate with the openssl library.
159          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
160          *      @remarks        The specific error code can be accessed using the GetLastResult() method.
161          */
162         virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input);
163
164         /**
165          *      Initializes the instance of %AesCipher for the multiple-part encryption or decryption.
166          *
167          *      @since          2.0
168          *
169          *      @return         An error code
170          *      @exception      E_SUCCESS                               The method is successful.
171          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
172          *      @exception      E_KEY_NOT_FOUND                 The specified key is not found.
173          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
174          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
175          *                                                                              - A system error has occurred.
176          *                                                                              - The method has failed to operate with the openssl library.
177          */
178         virtual result Initialize(void);
179
180         /**
181          *      Updates the multiple-part encryption or decryption operation.
182          *
183          *      @since          2.0
184          *
185          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
186          *                              else @c null if an error occurs
187          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
188          *      @exception      E_SUCCESS                               The method is successful.
189          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
190          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
191          *      @exception      E_INVALID_ARG                   The specified instance of Tizen::Base::ByteBuffer is invalid or empty.
192          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
193          *                                                                              - A system error has occurred.
194          *                                                                              - The method has failed to operate with the openssl library.
195          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.
196          *      @remarks        The specific error code can be accessed using the GetLastResult() method.
197          */
198         virtual Tizen::Base::ByteBuffer* UpdateN(const Tizen::Base::ByteBuffer& input);
199
200         /**
201          *      Finalizes the multiple-part encryption or decryption operation.
202          *
203          *      @since          2.0
204          *
205          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
206          *                              else @c null if an error occurs
207          *      @exception      E_SUCCESS                               The method is successful.
208          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
209          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
210          *      @exception      E_SYSTEM                                Either of the following conditions has occurred:
211          *                                                                              - A system error has occurred.
212          *                                                                              - The method has failed to operate with the openssl library.
213          *                                                                              - The Tizen::Base::ByteBuffer operation has failed.      
214          *      @remarks        The specific error code can be accessed using the GetLastResult() method.
215          */
216         virtual Tizen::Base::ByteBuffer* FinalizeN(void);
217
218         /**
219          *      Wraps a key.
220          *
221          *      @since          2.0
222          *      @pre            Before calling this method, set a secret key using SetKey().
223          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
224          *                              else @c null if an error occurs
225          *      @param[in]      secretKey                                       The secret key to wrap
226          *      @exception      E_SUCCESS                                       The method is successful.
227          *      @exception      E_INVALID_ARG                           The specified instance of Tizen::Base::ByteBuffer is invalid or empty.
228          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
229          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
230          *      @exception      E_INVALID_OPERATION                     The specified cipher operation mode for this method is invalid.
231          *      @exception      E_UNSUPPORTED_ALGORITHM         The specified cipher algorithm for this method is invalid.
232          *      @exception      E_SYSTEM                                        Either of the following conditions has occurred:
233          *                                                                                      - A system error has occurred.
234          *                                                                                      - The method has failed to operate with the openssl library.
235          *      @remarks                The specific error code can be accessed using the GetLastResult() method.
236          */
237         virtual Tizen::Base::ByteBuffer* WrapN(const Tizen::Base::ByteBuffer& secretKey);
238
239         /**
240          *      Unwraps a previously wrapped key.
241          *
242          *      @since          2.0
243          *      @pre            Before calling this method, set a secret key using SetKey().
244          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
245          *                              else @c null if an error occurs
246          *      @param[in]      wrappedKey                                      The wrapped key to unwrap
247          *      @exception      E_SUCCESS                                       The method is successful.
248          *      @exception      E_INVALID_ARG                           The specified instance of Tizen::Base::ByteBuffer is invalid.
249          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
250          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
251          *      @exception      E_INVALID_OPERATION                     The specified cipher operation mode for this method is invalid.
252          *      @exception      E_UNSUPPORTED_ALGORITHM         The specified cipher algorithm for this method is invalid.
253          *      @exception      E_SYSTEM                                        Either of the following conditions has occurred:
254          *                                                                                      - A system error has occurred.
255          *                                                                                      - The method has failed to operate with the openssl library.
256          *      @remarks                The specific error code can be accessed using the GetLastResult() method.
257          */
258         virtual Tizen::Base::ByteBuffer* UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey);
259
260 private:
261
262         //
263         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
264         //
265         // @since 2.0
266         //
267         AesCipher(const AesCipher& rhs);
268
269         //
270         // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
271         //
272         // @since 2.0
273         //
274         AesCipher& operator =(const AesCipher& rhs);
275
276 private:
277         _SymmetricCipher* __pSymmetricCipher;
278         const evp_cipher_st* __pCipherAlgorithm;
279         Tizen::Base::String __cipherAlgorithm;
280
281         class _AesCipherImpl* __pAesCipherImpl;
282         friend class _AesCipherImpl;
283
284 }; //AesCipher
285
286 } } } //Tizen::Security:Crypto
287
288 #endif //_FSEC_CRYPTO_AES_CIPHER_H_