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 FSecCryptoDhKeyExchange.h
19 * @brief This is the header file for the %DhKeyExchange class.
21 * This header file contains the declarations of the %DhKeyExchange class.
23 #ifndef _FSEC_CRYPTO_DH_KEYEXCHANGE_H_
24 #define _FSEC_CRYPTO_DH_KEYEXCHANGE_H_
26 #include <FSecCryptoIKeyExchange.h>
29 namespace Tizen { namespace Security { namespace Crypto
33 * @class DhKeyExchange
34 * @brief This class provides methods for a key exchange mechanism using the Diffie-Hellman (DH) algorithm.
38 * The %DhKeyExchange class provides a DH key exchange between two communicating users. @n
40 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/key_exchange_algorithm.htm">Key exchanging</a>.
43 * @see KeyPairGenerator
45 * The following example demonstrates how to use the %DhKeyExchange class.
48 * void DhGenerateSecretExample(void)
50 * result r = E_SUCCESS;
51 * KeyPair *pKeyPair = null;
52 * KeyPair *pKeyPair1 = null;
53 * IPrivateKey *pPriKey = null;
54 * IPublicKey *pPubKey = null;
55 * IPrivateKey *pPriKey1 = null;
56 * IPublicKey *pPubKey1 = null;
57 * KeyPairGenerator *pKeyPairGen = null;
58 * IKeyParameters *pKeyParams = null;
60 * DhKeyExchange *pDhKeyExchangeAlice = null;
61 * DhKeyExchange *pDhKeyExchangeBob = null;
62 * ByteBuffer *pBuffer = null;
63 * ByteBuffer *pBuffer1 = null;
65 * SetLastResult(E_SUCCESS);
66 * // Generates the key.
67 * pKeyPairGen = new KeyPairGenerator();
68 * if (pKeyPairGen == null)
74 * r = pKeyPairGen->Construct(size, "DH");
80 * pKeyParams = pKeyPairGen->GenerateKeyParametersN();
81 * if (pKeyParams == null)
87 * pKeyPair = pKeyPairGen->GenerateKeyPairN(pKeyParams);
88 * if (pKeyPair == null)
93 * pKeyPair1 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
94 * if (pKeyPair1 == null)
100 * pPubKey = pKeyPair->GetPublicKey();
101 * if (pPubKey == null)
107 * pPriKey = pKeyPair->GetPrivateKey();
108 * if (pPriKey == null)
114 * pPubKey1 = pKeyPair1->GetPublicKey();
115 * if (pPubKey1 == null)
121 * pPriKey1 = pKeyPair1->GetPrivateKey();
122 * if (pPriKey1 == null)
128 * pDhKeyExchangeAlice = new DhKeyExchange ();
129 * if (pDhKeyExchangeAlice == null)
134 * pDhKeyExchangeBob = new DhKeyExchange ();
135 * if (pDhKeyExchangeBob == null)
140 * // Calling the Construct API.
141 * r = pDhKeyExchangeAlice->Construct(*pKeyParams);
147 * r = pDhKeyExchangeBob->Construct(*pKeyParams);
153 * pBuffer = pDhKeyExchangeAlice->GenerateSecretN(*pPriKey, *pPubKey1);
154 * if (pBuffer == null)
159 * pBuffer1 = pDhKeyExchangeBob->GenerateSecretN(*pPriKey1, *pPubKey);
160 * if (pBuffer1 == null)
165 * if (*pBuffer == *pBuffer1)
167 * AppLog("The secret is generated SuccessFully");
175 * delete pKeyPairGen;
180 * delete pDhKeyExchangeAlice;
181 * delete pDhKeyExchangeBob;
190 class _OSP_EXPORT_ DhKeyExchange
191 : public virtual IKeyExchange
192 , public Tizen::Base::Object
197 * The object is not fully constructed after this constructor is called. For full construction, @n
198 * the Construct() method must be called right after calling this constructor.
205 * This destructor overrides Tizen::Base::Object::~Object().
209 virtual ~DhKeyExchange(void);
212 * Initializes this instance of %DhKeyExchange with the specified key parameters.
216 * @return An error code
217 * @param[in] keyParameters The domain parameters ('p' prime number and 'g' generator) of DH
218 * algorithm that needs to instantiate
219 * @exception E_SUCCESS The method is successful.
220 * @exception E_OUT_OF_MEMORY The memory is insufficient.
221 * @exception E_INVALID_ARG The specified input parameter is invalid, or the specified @c keyParameters does not contain a valid value. */
222 virtual result Construct(const Tizen::Security::IKeyParameters& keyParameters);
225 * Generates the final shared secret between two parties.
229 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the generated secret key, @n
230 * else @c null if the method fails to generate the secret key
231 * @param[in] privateKey The private key component of the first party that needs to instantiate
232 * @param[in] publicKey The public key component of the second party that needs to instantiate
233 * @exception E_SUCCESS The method is successful.
234 * @exception E_INVALID_ARG A specified input parameter is invalid.
235 * @exception E_OUT_OF_MEMORY The memory is insufficient.
236 * @exception E_SYSTEM A system error has occurred. @n
237 * The method has failed to operate with the openssl library, or
238 * the Tizen::Base::ByteBuffer operation has failed.
239 * @remarks The specific error code can be accessed using the GetLastResult() method.
241 virtual Tizen::Base::ByteBuffer* GenerateSecretN(Tizen::Security::IPrivateKey& privateKey, Tizen::Security::IPublicKey& publicKey);
246 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
250 DhKeyExchange(const DhKeyExchange& rhs);
253 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
257 DhKeyExchange& operator =(const DhKeyExchange& rhs);
260 Tizen::Base::ByteBuffer* __pParamsP;
261 Tizen::Base::ByteBuffer* __pParamsG;
263 class _DhKeyExchangeImpl* __pDhKeyExchangeImpl;
264 friend class _DhKeyExchangeImpl;
268 } } } //Tizen::Security::Crypto
270 #endif //_FSEC_CRYPTO_DH_KEYEXCHANGE_H_