50059b2f190a7741c4e159a991935d0fc34a6160
[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  *      Set appropriate values for the requested mode/key bit/padding scheme and cipher operation mode (::CIPHER_ENCRYPT, ::CIPHER_DECRYPT, ::CIPHER_WRAP, 
43  *      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. For full construction, @n
59          *      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 name of 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                   A specified input parameter is invalid, or the specified @c opMode does not contain a valid value for the cipher operation.
85          *      @remarks        If @c opMode is not matching the actual operation, the result of the operation is @c null and an exception is returned. @n
86          *                              For example, if @c opMode is set to @c CIPHER_ENCRYPT, @c CIPHER_WRAP, or @c CIPHER_UNWRAP and the DecryptN() method is used, then the result obtained is @c null and an exception is returned.
87          */
88         virtual result Construct(const Tizen::Base::String& transformation, enum CipherOperation opMode);
89
90         /**
91          *      Sets a symmetric key for encryption or decryption.
92          *
93          *      @since          2.0
94          *
95          *      @return         An error code
96          *      @param[in]      key                                             An instance of ISecretKey
97          *      @exception      E_SUCCESS                               The method is successful.
98          *      @exception      E_INVALID_ARG                   The specified @c key is invalid.
99          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
100          */
101         virtual result SetKey(const Tizen::Security::ISecretKey& key);
102
103         /**
104          *      Sets the initial vector.
105          *
106          *      @since          2.0
107          *
108          *      @return         An error code
109          *      @param[in]      initialVector                   The initial vector
110          *      @exception      E_SUCCESS                               The method is successful.
111          *      @exception      E_INVALID_ARG                   The specified input parameter is invalid.
112          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
113          */
114         virtual result SetInitialVector(const Tizen::Base::ByteBuffer& initialVector);
115
116         /**
117          *      Encrypts the data (single-part).
118          *
119          *      @since          2.0
120          *      @pre                    Before calling this method, a secret key and an initial vector should be set using SetKey() and SetInitialVector().
121          *
122          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
123          *                              else @c null if an error occurs
124          *  @param[in]  input                                   An instance of Tizen::Base::ByteBuffer
125          *      @exception      E_SUCCESS                               The method is successful.
126          *      @exception      E_INVALID_ARG                   The specified instance of Tizen::Base::ByteBuffer is invalid or empty.
127          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
128          *      @exception      E_KEY_NOT_FOUND                 The specified key is not found.
129          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
130          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
131          *      @exception      E_SYSTEM                                A system error has occurred. @n
132          *                                                                              The method has failed to operate with the openssl library, or
133          *                                                                              the Tizen::Base::ByteBuffer operation has failed.
134          *  @remarks    The specific error code can be accessed using the GetLastResult() method.
135          */
136         virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input);
137
138         /**
139          *      Decrypts the data (single-part).
140          *
141          *      @since          2.0
142          *      @pre                    Before calling this method, a secret key and an initial vector should be set using SetKey() and SetInitialVector().
143          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
144          *                              else @c null if an error occurs
145          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
146          *      @exception      E_SUCCESS                               The method is successful.
147          *      @exception      E_INVALID_ARG                   The specified instance of Tizen::Base::ByteBuffer is invalid or empty.
148          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
149          *      @exception      E_KEY_NOT_FOUND                 The specified key is not found.
150          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
151          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
152          *      @exception      E_SYSTEM                                A system error has occurred. @n
153          *                                                                              The method has failed to operate with the openssl library, or
154          *                                                                              the Tizen::Base::ByteBuffer operation has failed.
155          *      @remarks                The specific error code can be accessed using the GetLastResult() method.
156          */
157         virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input);
158
159         /**
160          *      Initializes a multiple-part encryption or decryption operation.
161          *
162          *      @since          2.0
163          *
164          *      @return         An error code
165          *      @exception      E_SUCCESS                               The method is successful.
166          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
167          *      @exception      E_KEY_NOT_FOUND                 The specified key is not found.
168          *      @exception      E_INVALID_OPERATION             The specified cipher operation mode for this method is invalid.
169          *      @exception      E_SYSTEM                                A system error has occurred. @n
170          *                                                                              The method has failed to operate with the openssl library.
171          */
172         virtual result Initialize(void);
173
174         /**
175          *      Updates a multiple-part encryption or decryption operation.
176          *
177          *      @since          2.0
178          *
179          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
180          *                              else @c null if an error occurs
181          *      @param[in]      input                                   An instance of Tizen::Base::ByteBuffer
182          *      @exception      E_SUCCESS                               The method is successful.
183          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
184          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
185          *      @exception      E_INVALID_ARG                   The specified instance of Tizen::Base::ByteBuffer is invalid or empty.
186          *      @exception      E_SYSTEM                                A system error has occurred. @n
187          *                                                                              The method has failed to operate with the openssl library, or
188          *                                                                              the Tizen::Base::ByteBuffer operation has failed.
189          *      @remarks        The specific error code can be accessed using the GetLastResult() method.
190          */
191         virtual Tizen::Base::ByteBuffer* UpdateN(const Tizen::Base::ByteBuffer& input);
192
193         /**
194          *      Finalizes a multiple-part encryption or decryption operation.
195          *
196          *      @since          2.0
197          *
198          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
199          *                              else @c null if an error occurs
200          *      @exception      E_SUCCESS                               The method is successful.
201          *      @exception      E_OUT_OF_MEMORY                 The memory is insufficient.
202          *      @exception      E_OVERFLOW                              This operation has caused the memory to overflow.
203          *      @exception      E_SYSTEM                                A system error has occurred. @n
204          *                                                                              The method has failed to operate with the openssl library, or
205          *                                                                              the Tizen::Base::ByteBuffer operation has failed.
206          *      @remarks        The specific error code can be accessed using the GetLastResult() method.
207          */
208         virtual Tizen::Base::ByteBuffer* FinalizeN(void);
209
210         /**
211          *      Wraps a key.
212          *
213          *      @since          2.0
214          *      @pre                    Before calling this method, a secret key should be set using SetKey().
215          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
216          *                              else @c null if an error occurs
217          *      @param[in]      secretKey                                       The secret key to wrap
218          *      @exception      E_SUCCESS                                       The method is successful.
219          *      @exception      E_INVALID_ARG                           The specified instance of Tizen::Base::ByteBuffer is invalid or empty.
220          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
221          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
222          *      @exception      E_INVALID_OPERATION                     The specified cipher operation mode for this method is invalid.
223          *      @exception      E_UNSUPPORTED_ALGORITHM         The specified cipher algorithm for this method is invalid.
224          *      @exception      E_SYSTEM                                        A system error has occurred. @n
225          *                                                                                      The method has failed to operate with the openssl library.
226          *      @remarks                The specific error code can be accessed using the GetLastResult() method.
227          */
228         virtual Tizen::Base::ByteBuffer* WrapN(const Tizen::Base::ByteBuffer& secretKey);
229
230         /**
231          *      Unwraps a previously wrapped key.
232          *
233          *      @since          2.0
234          *      @pre                    Before calling this method, a secret key should be set using SetKey().
235          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
236          *                              else @c null if an error occurs
237          *      @param[in]      wrappedKey                                      The wrapped key to unwrap
238          *      @exception      E_SUCCESS                                       The method is successful.
239          *      @exception      E_INVALID_ARG                           The specified instance of Tizen::Base::ByteBuffer is invalid.
240          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
241          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
242          *      @exception      E_INVALID_OPERATION                     The specified cipher operation mode for this method is invalid.
243          *      @exception      E_UNSUPPORTED_ALGORITHM         The specified cipher algorithm for this method is invalid.
244          *      @exception      E_SYSTEM                                        A system error has occurred. @n
245          *                                                                                      The method has failed to operate with the openssl library.
246          *      @remarks                The specific error code can be accessed using the GetLastResult() method.
247          */
248         virtual Tizen::Base::ByteBuffer* UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey);
249
250 private:
251
252         //
253         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
254         //
255         // @since 2.0
256         //
257         AesCipher(const AesCipher& rhs);
258
259         //
260         // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
261         //
262         // @since 2.0
263         //
264         AesCipher& operator =(const AesCipher& rhs);
265
266 private:
267         _SymmetricCipher* __pSymmetricCipher;
268         const evp_cipher_st* __pCipherAlgorithm;
269         Tizen::Base::String __cipherAlgorithm;
270
271         class _AesCipherImpl* __pAesCipherImpl;
272         friend class _AesCipherImpl;
273
274 }; //AesCipher
275
276 } } } //Tizen::Security:Crypto
277
278 #endif //_FSEC_CRYPTO_AES_CIPHER_H_