update header for Doxygen
[platform/framework/native/appfw.git] / inc / FSecCryptoIAsymmetricCipher.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSecCryptoIAsymmetricCipher.h
20  * @brief               This is the header file for the %IAsymmetricCipher interface.
21  *
22  * This header file contains the declarations of the %IAsymmetricCipher interface.
23  */
24 #ifndef _FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_
25 #define _FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_
26
27 #include <FBaseString.h>
28 #include <FBaseByteBuffer.h>
29 #include <FSecIKey.h>
30 #include <FSecIPublicKey.h>
31 #include <FSecIPrivateKey.h>
32 #include <FSecCryptoTypes.h>
33
34
35 namespace Tizen { namespace Security { namespace Crypto
36 {
37
38 /**
39  *      @interface      IAsymmetricCipher
40  *      @brief          This interface provides the functionality of an asymmetric cryptographic cipher for encryption and decryption.
41  *
42  *      @since          2.0
43  *
44  *      The %IAsymmetricCipher interface provides the functionality of an asymmetric cryptographic cipher for encryption and decryption. @n
45  *
46  *      For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/ciphers.htm">Ciphers</a>. @n
47  *
48  *      The following example demonstrates how to use the %IAsymmetricCipher interface.
49  *
50  *      @code
51  *
52  *      void
53  *      MyClass::TestAsymmetricCipherSample(void)
54  *      {
55  *              result r = E_FAILURE;
56  *              IAsymmetricCipher *pCipher = null;
57  *              IKeyPairGenerator *pKeyPairGen = null;
58  *              KeyPair *pKeyPair = null;
59  *              IPrivateKey *pPriKey = null;
60  *              IPublicKey *pPubKey = null;
61  *              ByteBuffer input;
62  *              ByteBuffer *pOutput = null;
63  *              ByteBuffer *pDecOutput = null;
64  *              ByteBuffer keyBytes;
65  *              ByteBuffer iv;
66  *              byte pArray[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'};
67  *              int size = 1024;
68  *
69  *              r = input.Construct(16);
70  *              r = input.SetArray(pArray, 0, 16);
71  *              input.Flip();
72  *
73  *              pCipher = new RsaCipher();
74  *              if (pCipher == null)
75  *              {
76  *                      goto CATCH;
77  *              }
78  *
79  *              // Generates the key.
80  *              pKeyPairGen = new KeyPairGenerator();
81  *              if (pKeyPairGen == null)
82  *              {
83  *                      goto CATCH;
84  *              }
85  *
86  *              r = pKeyPairGen->Construct(size);
87  *              if (IsFailed(r))
88  *              {
89  *                      goto CATCH;
90  *              }
91  *
92  *              pKeyPair = pKeyPairGen->GenerateKeyPairN();
93  *              if (pKeyPair == null)
94  *              {
95  *                      goto CATCH;
96  *              }
97  *
98  *              pPubKey = pKeyPair->GetPublicKey();
99  *              if (pPubKey == null)
100  *              {
101  *                      goto CATCH;
102  *              }
103  *
104  *              pPriKey = pKeyPair->GetPrivateKey();
105  *              if (pPriKey == null)
106  *              {
107  *                      goto CATCH;
108  *              }
109  *
110  *              r = pCipher->SetPublicKey(*pPubKey);
111  *              if (IsFailed(r))
112  *              {
113  *                      goto CATCH;
114  *              }
115  *
116  *              pOutput = pCipher->EncryptN(input);
117  *              if (pOutput == null)
118  *              {
119  *                      r = GetLastResult();
120  *                      goto CATCH;
121  *              }
122  *
123  *              r = pCipher->SetPrivateKey(*pPriKey);
124  *              if (IsFailed(r))
125  *              {
126  *                      goto CATCH;
127  *              }
128  *
129  *              pDecOutput = pCipher->DecryptN(*pOutput);
130  *              if (pDecOutput == null)
131  *              {
132  *                      r = GetLastResult();
133  *                      goto CATCH;
134  *              }
135  *
136  *              if (memcmp(pDecOutput->GetPointer(), input.GetPointer(), input.GetRemaining()) != 0)
137  *              {
138  *                      goto CATCH;
139  *              }
140  *
141  *              r       = E_SUCCESS;
142  *
143  *      CATCH:
144  *              delete pCipher;
145  *              delete pOutput;
146  *              delete pDecOutput;
147  *              delete pKeyPairGen;
148  *              delete pKeyPair;
149  *      }
150  *
151  *      @endcode
152  */
153
154 class _OSP_EXPORT_ IAsymmetricCipher
155 {
156
157 public:
158         /**
159          * This polymorphic destructor should be overridden if required. This way, the destructors of the derived classes @n
160          * are called when the destructor of this interface is called.
161          *
162          * @since               2.0
163          */
164         virtual ~IAsymmetricCipher(void) {}
165
166         /**
167          *      Sets an asymmetric private key for encryption or decryption.
168          *
169          *      @since          2.0
170          *
171          *      @return         An error code
172          *      @param[in]      key                                                     An instance of IKey
173          *      @exception      E_SUCCESS                                       The method is successful.
174          *      @exception      E_INVALID_ARG                           The specified key is invalid.
175          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
176          */
177         virtual result SetPrivateKey(const Tizen::Security::IKey& key) = 0;
178
179         /**
180          *      Sets an asymmetric public key for encryption or decryption.
181          *
182          *      @since          2.0
183          *
184          *      @return         An error code
185          *      @param[in]      key                                                     An instance of IKey
186          *      @exception      E_SUCCESS                                       The method is successful.
187          *      @exception      E_INVALID_ARG                           The specified key is invalid.
188          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
189          */
190         virtual result SetPublicKey(const Tizen::Security::IKey& key) = 0;
191
192         /**
193          *      Encrypts the data (single-part).
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          *      @param[in]      input                                           An instance of Tizen::Base::ByteBuffer
200          *      @exception      E_SUCCESS                                       The method is successful.
201          *      @exception      E_INVALID_ARG                           The input Tizen::Base::ByteBuffer is empty or contains invalid data.
202          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
203          *      @exception      E_UNSUPPORTED_ALGORITHM         The algorithm is not supported.
204          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
205          *      @exception      E_SYSTEM                                        A system error has occurred. @n
206          *                                                                                      The method has failed to operate with the openssl library, or
207          *                                                                                      the Tizen::Base::ByteBuffer operation has failed.
208          */
209         virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input) = 0;
210
211         /**
212          *      Decrypts the data (single-part).
213          *
214          *      @since          2.0
215          *
216          *      @return         A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
217          *                              else @c null if an error occurs
218          *      @param[in]      input                                           An instance of Tizen::Base::ByteBuffer
219          *      @exception      E_SUCCESS                                       The method is successful.
220          *      @exception      E_INVALID_ARG                           The input Tizen::Base::ByteBuffer is empty or contains invalid data.
221          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
222          *      @exception      E_UNSUPPORTED_ALGORITHM         The algorithm is not supported.
223          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
224          *      @exception      E_SYSTEM                                        A system error has occurred. @n
225          *                                                                                      The method has failed to operate with the openssl library, or
226          *                                                                                      the Tizen::Base::ByteBuffer operation has failed.
227          */
228         virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input) = 0;
229
230 protected:
231         //
232         // This method is for internal use only. Using this method can cause behavioral, security-related,
233         // and consistency-related issues in the application.
234         //
235         // This method is reserved and may change its name at any time without prior notice.
236         //
237         // @since 2.0
238         //
239         virtual void IAsymmetricCipher_Reserved1(void) {}
240
241 }; //IAsymmetricCipher
242
243 } } } //Tizen::Security::Crypto
244
245 #endif //_FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_