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