2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FSecCryptoIAsymmetricCipher.h
20 * @brief This is the header file for the %IAsymmetricCipher interface.
22 * This header file contains the declarations of the %IAsymmetricCipher interface.
24 #ifndef _FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_
25 #define _FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_
27 #include <FBaseString.h>
28 #include <FBaseByteBuffer.h>
30 #include <FSecIPublicKey.h>
31 #include <FSecIPrivateKey.h>
32 #include <FSecCryptoTypes.h>
35 namespace Tizen { namespace Security { namespace Crypto
39 * @interface IAsymmetricCipher
40 * @brief This interface provides the functionality of an asymmetric cryptographic cipher for encryption and decryption.
44 * The %IAsymmetricCipher interface provides the functionality of an asymmetric cryptographic cipher for encryption and decryption. @n
46 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/ciphers.htm">Ciphers</a>. @n
48 * The following example demonstrates how to use the %IAsymmetricCipher interface.
53 * MyClass::TestAsymmetricCipherSample(void)
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;
62 * ByteBuffer *pOutput = null;
63 * ByteBuffer *pDecOutput = null;
64 * ByteBuffer keyBytes;
66 * byte pArray[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'};
69 * r = input.Construct(16);
70 * r = input.SetArray(pArray, 0, 16);
73 * pCipher = new RsaCipher();
74 * if (pCipher == null)
79 * // Generates the key.
80 * pKeyPairGen = new KeyPairGenerator();
81 * if (pKeyPairGen == null)
86 * r = pKeyPairGen->Construct(size);
92 * pKeyPair = pKeyPairGen->GenerateKeyPairN();
93 * if (pKeyPair == null)
98 * pPubKey = pKeyPair->GetPublicKey();
99 * if (pPubKey == null)
104 * pPriKey = pKeyPair->GetPrivateKey();
105 * if (pPriKey == null)
110 * r = pCipher->SetPublicKey(*pPubKey);
116 * pOutput = pCipher->EncryptN(input);
117 * if (pOutput == null)
119 * r = GetLastResult();
123 * r = pCipher->SetPrivateKey(*pPriKey);
129 * pDecOutput = pCipher->DecryptN(*pOutput);
130 * if (pDecOutput == null)
132 * r = GetLastResult();
136 * if (memcmp(pDecOutput->GetPointer(), input.GetPointer(), input.GetRemaining()) != 0)
147 * delete pKeyPairGen;
154 class _OSP_EXPORT_ IAsymmetricCipher
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.
164 virtual ~IAsymmetricCipher(void) {}
167 * Sets an asymmetric private key for encryption or decryption.
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.
177 virtual result SetPrivateKey(const Tizen::Security::IKey& key) = 0;
180 * Sets an asymmetric public key for encryption or decryption.
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.
190 virtual result SetPublicKey(const Tizen::Security::IKey& key) = 0;
193 * Encrypts the data (single-part).
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.
209 virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input) = 0;
212 * Decrypts the data (single-part).
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.
228 virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input) = 0;
232 // This method is for internal use only. Using this method can cause behavioral, security-related,
233 // and consistency-related issues in the application.
235 // This method is reserved and may change its name at any time without prior notice.
239 virtual void IAsymmetricCipher_Reserved1(void) {}
241 }; //IAsymmetricCipher
243 } } } //Tizen::Security::Crypto
245 #endif //_FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_