2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FSecCryptoIAsymmetricCipher.h
19 * @brief This is the header file for the %IAsymmetricCipher interface.
21 * This header file contains the declarations of the %IAsymmetricCipher interface.
23 #ifndef _FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_
24 #define _FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_
26 #include <FBaseString.h>
27 #include <FBaseByteBuffer.h>
29 #include <FSecIPublicKey.h>
30 #include <FSecIPrivateKey.h>
31 #include <FSecCryptoTypes.h>
34 namespace Tizen { namespace Security { namespace Crypto
38 * @interface IAsymmetricCipher
39 * @brief This interface provides the functionality of an asymmetric cryptographic cipher for encryption and decryption.
43 * The %IAsymmetricCipher interface provides the functionality of an asymmetric cryptographic cipher for encryption and decryption. @n
45 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/ciphers.htm">Ciphers</a>. @n
47 * The following example demonstrates how to use the %IAsymmetricCipher interface.
52 * MyClass::TestAsymmetricCipherSample(void)
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;
61 * ByteBuffer *pOutput = null;
62 * ByteBuffer *pDecOutput = null;
63 * ByteBuffer keyBytes;
65 * byte pArray[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'};
68 * r = input.Construct(16);
69 * r = input.SetArray(pArray, 0, 16);
72 * pCipher = new RsaCipher();
73 * if (pCipher == null)
78 * // Generates the key.
79 * pKeyPairGen = new KeyPairGenerator();
80 * if (pKeyPairGen == null)
85 * r = pKeyPairGen->Construct(size);
91 * pKeyPair = pKeyPairGen->GenerateKeyPairN();
92 * if (pKeyPair == null)
97 * pPubKey = pKeyPair->GetPublicKey();
98 * if (pPubKey == null)
103 * pPriKey = pKeyPair->GetPrivateKey();
104 * if (pPriKey == null)
109 * r = pCipher->SetPublicKey(*pPubKey);
115 * pOutput = pCipher->EncryptN(input);
116 * if (pOutput == null)
118 * r = GetLastResult();
122 * r = pCipher->SetPrivateKey(*pPriKey);
128 * pDecOutput = pCipher->DecryptN(*pOutput);
129 * if (pDecOutput == null)
131 * r = GetLastResult();
135 * if (memcmp(pDecOutput->GetPointer(), input.GetPointer(), input.GetRemaining()) != 0)
146 * delete pKeyPairGen;
153 class _OSP_EXPORT_ IAsymmetricCipher
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.
163 virtual ~IAsymmetricCipher(void) {}
166 * Sets an asymmetric private key for encryption or decryption.
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.
176 virtual result SetPrivateKey(const Tizen::Security::IKey& key) = 0;
179 * Sets an asymmetric public key for encryption or decryption.
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.
189 virtual result SetPublicKey(const Tizen::Security::IKey& key) = 0;
192 * Encrypts the data (single-part).
196 * @return A pointer to the Tizen::Base::ByteBuffer class 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 A system error has occurred. @n
205 * The method has failed to operate with the openssl library, or
206 * the Tizen::Base::ByteBuffer operation has failed.
208 virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input) = 0;
211 * Decrypts the data (single-part).
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] input An instance of Tizen::Base::ByteBuffer
218 * @exception E_SUCCESS The method is successful.
219 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
220 * @exception E_OUT_OF_MEMORY The memory is insufficient.
221 * @exception E_UNSUPPORTED_ALGORITHM The algorithm is not supported.
222 * @exception E_KEY_NOT_FOUND The specified key is not found.
223 * @exception E_SYSTEM A system error has occurred. @n
224 * The method has failed to operate with the openssl library, or
225 * the Tizen::Base::ByteBuffer operation has failed.
227 virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input) = 0;
231 // This method is for internal use only. Using this method can cause behavioral, security-related,
232 // and consistency-related issues in the application.
234 // This method is reserved and may change its name at any time without prior notice.
238 virtual void IAsymmetricCipher_Reserved1(void) {}
240 }; //IAsymmetricCipher
242 } } } //Tizen::Security::Crypto
244 #endif //_FSEC_CRYPTO_IASYMMETRIC_CIPHER_H_