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