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 FSecCryptoDhKeyExchange.h
20 * @brief This is the header file for the %DhKeyExchange class.
22 * This header file contains the declarations of the %DhKeyExchange class.
24 #ifndef _FSEC_CRYPTO_DH_KEYEXCHANGE_H_
25 #define _FSEC_CRYPTO_DH_KEYEXCHANGE_H_
27 #include <FSecCryptoIKeyExchange.h>
30 namespace Tizen { namespace Security { namespace Crypto
34 * @class DhKeyExchange
35 * @brief This class provides methods for a key exchange mechanism using the Diffie-Hellman (DH) algorithm.
39 * The %DhKeyExchange class provides a DH key exchange between two communicating users. @n
41 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/key_exchange_algorithm.htm">Key exchanging</a>.
44 * @see KeyPairGenerator
46 * The following example demonstrates how to use the %DhKeyExchange class.
49 * void DhGenerateSecretExample(void)
51 * result r = E_SUCCESS;
52 * KeyPair *pKeyPair = null;
53 * KeyPair *pKeyPair1 = null;
54 * IPrivateKey *pPriKey = null;
55 * IPublicKey *pPubKey = null;
56 * IPrivateKey *pPriKey1 = null;
57 * IPublicKey *pPubKey1 = null;
58 * KeyPairGenerator *pKeyPairGen = null;
59 * IKeyParameters *pKeyParams = null;
61 * DhKeyExchange *pDhKeyExchangeAlice = null;
62 * DhKeyExchange *pDhKeyExchangeBob = null;
63 * ByteBuffer *pBuffer = null;
64 * ByteBuffer *pBuffer1 = null;
66 * SetLastResult(E_SUCCESS);
67 * // Generates the key.
68 * pKeyPairGen = new KeyPairGenerator();
69 * if (pKeyPairGen == null)
75 * r = pKeyPairGen->Construct(size, "DH");
81 * pKeyParams = pKeyPairGen->GenerateKeyParametersN();
82 * if (pKeyParams == null)
88 * pKeyPair = pKeyPairGen->GenerateKeyPairN(pKeyParams);
89 * if (pKeyPair == null)
94 * pKeyPair1 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
95 * if (pKeyPair1 == null)
101 * pPubKey = pKeyPair->GetPublicKey();
102 * if (pPubKey == null)
108 * pPriKey = pKeyPair->GetPrivateKey();
109 * if (pPriKey == null)
115 * pPubKey1 = pKeyPair1->GetPublicKey();
116 * if (pPubKey1 == null)
122 * pPriKey1 = pKeyPair1->GetPrivateKey();
123 * if (pPriKey1 == null)
129 * pDhKeyExchangeAlice = new DhKeyExchange ();
130 * if (pDhKeyExchangeAlice == null)
135 * pDhKeyExchangeBob = new DhKeyExchange ();
136 * if (pDhKeyExchangeBob == null)
141 * // Calling the Construct API.
142 * r = pDhKeyExchangeAlice->Construct(*pKeyParams);
148 * r = pDhKeyExchangeBob->Construct(*pKeyParams);
154 * pBuffer = pDhKeyExchangeAlice->GenerateSecretN(*pPriKey, *pPubKey1);
155 * if (pBuffer == null)
160 * pBuffer1 = pDhKeyExchangeBob->GenerateSecretN(*pPriKey1, *pPubKey);
161 * if (pBuffer1 == null)
166 * if (*pBuffer == *pBuffer1)
168 * AppLog("The secret is generated SuccessFully");
176 * delete pKeyPairGen;
181 * delete pDhKeyExchangeAlice;
182 * delete pDhKeyExchangeBob;
191 class _OSP_EXPORT_ DhKeyExchange
192 : public virtual IKeyExchange
193 , public Tizen::Base::Object
198 * The object is not fully constructed after this constructor is called. For full construction, @n
199 * the Construct() method must be called right after calling this constructor.
206 * This destructor overrides Tizen::Base::Object::~Object().
210 virtual ~DhKeyExchange(void);
213 * Initializes this instance of %DhKeyExchange with the specified key parameters.
217 * @return An error code
218 * @param[in] keyParameters The domain parameters ('p' prime number and 'g' generator) of DH
219 * algorithm that needs to instantiate
220 * @exception E_SUCCESS The method is successful.
221 * @exception E_OUT_OF_MEMORY The memory is insufficient.
222 * @exception E_INVALID_ARG The specified input parameter is invalid, or the specified @c keyParameters does not contain a valid value. */
223 virtual result Construct(const Tizen::Security::IKeyParameters& keyParameters);
226 * Generates the final shared secret between two parties.
230 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the generated secret key, @n
231 * else @c null if the method fails to generate the secret key
232 * @param[in] privateKey The private key component of the first party that needs to instantiate
233 * @param[in] publicKey The public key component of the second party that needs to instantiate
234 * @exception E_SUCCESS The method is successful.
235 * @exception E_INVALID_ARG A specified input parameter is invalid.
236 * @exception E_OUT_OF_MEMORY The memory is insufficient.
237 * @exception E_SYSTEM A system error has occurred. @n
238 * The method has failed to operate with the openssl library, or
239 * the Tizen::Base::ByteBuffer operation has failed.
240 * @remarks The specific error code can be accessed using the GetLastResult() method.
242 virtual Tizen::Base::ByteBuffer* GenerateSecretN(Tizen::Security::IPrivateKey& privateKey, Tizen::Security::IPublicKey& publicKey);
247 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
251 DhKeyExchange(const DhKeyExchange& rhs);
254 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
258 DhKeyExchange& operator =(const DhKeyExchange& rhs);
261 Tizen::Base::ByteBuffer* __pParamsP;
262 Tizen::Base::ByteBuffer* __pParamsG;
264 class _DhKeyExchangeImpl* __pDhKeyExchangeImpl;
265 friend class _DhKeyExchangeImpl;
269 } } } //Tizen::Security::Crypto
271 #endif //_FSEC_CRYPTO_DH_KEYEXCHANGE_H_