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 FSecCryptoKeaKeyExchange.h
20 * @brief This is the header file for the %KeaKeyExchange class.
22 * This header file contains the declarations of the %KeaKeyExchange class.
24 #ifndef _FSEC_CRYPTO_KEA_KEY_EXCHANGE_H_
25 #define _FSEC_CRYPTO_KEA_KEY_EXCHANGE_H_
27 #include <FSecCryptoIKeyExchange.h>
30 namespace Tizen { namespace Security { namespace Crypto
34 * @class KeaKeyExchange
35 * @brief This class provides methods for performing the key exchange mechanism using Key Exchange Algorithm (KEA).
39 * The %KeaKeyExchange class provides a KEA 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 %KeaKeyExchange class.
49 * void KeaGenerateSecretExample(void)
52 * result r = E_SUCCESS;
53 * KeyPair *pKeyPairAlice1 = null;
54 * IPrivateKey *pPriKeyAlice1 = null;
55 * IPrivateKey *pPriKeyAlice2 = null;
56 * KeyPair *pKeyPairAlice2 = null;
57 * IPublicKey *pPubKeyAlice1 = null;
58 * IPublicKey *pPubKeyAlice2 = null;
60 * KeyPair *pKeyPairBob1 = null;
61 * IPrivateKey *pPriKeyBob1 = null;
62 * IPrivateKey *pPriKeyBob2 = null;
63 * KeyPair *pKeyPairBob2 = null;
64 * IPublicKey *pPubKeyBob1 = null;
65 * IPublicKey *pPubKeyBob2 = null;
68 * KeyPairGenerator *pKeyPairGen = null;
69 * IKeyParameters *pKeyParams = null;
72 * KeaKeyExchange *pKeaKeyExchangeAlice = null;
73 * KeaKeyExchange *pKeaKeyExchangeBob = null;
74 * ByteBuffer *pBuffer = null;
75 * ByteBuffer *pBuffer1 = null;
78 * // Generates the key.
79 * pKeyPairGen = new KeyPairGenerator();
80 * if (pKeyPairGen == null)
86 * r = pKeyPairGen->Construct(size, L"KEA");
93 * pKeyParams = pKeyPairGen->GenerateKeyParametersN();
94 * if (pKeyParams == null)
100 * pKeyPairAlice1 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
101 * if (pKeyPairAlice1 == null)
106 * pKeyPairAlice2 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
107 * if (pKeyPairAlice2 == null)
112 * pPriKeyAlice1 = pKeyPairAlice1->GetPrivateKey();
113 * if (pPriKeyAlice1 == null)
118 * pPubKeyAlice1 = pKeyPairAlice1->GetPublicKey();
119 * if (pPubKeyAlice1 == null)
124 * pPriKeyAlice2 = pKeyPairAlice2->GetPrivateKey();
125 * if (pPriKeyAlice2 == null)
130 * pPubKeyAlice2 = pKeyPairAlice2->GetPublicKey();
131 * if (pPubKeyAlice2 == null)
136 * pKeyPairBob1 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
137 * if (pKeyPairBob1 == null)
142 * pKeyPairBob2 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
143 * if (pKeyPairBob2 == null)
148 * pPriKeyBob1 = pKeyPairBob1->GetPrivateKey();
149 * if (pPriKeyBob1 == null)
154 * pPubKeyBob1 = pKeyPairBob1->GetPublicKey();
155 * if (pPubKeyBob1 == null)
160 * pPriKeyBob2 = pKeyPairBob2->GetPrivateKey();
161 * if (pPriKeyBob2 == null)
166 * pPubKeyBob2 = pKeyPairBob2->GetPublicKey();
167 * if (pPubKeyBob2 == null)
173 * pKeaKeyExchangeAlice = new KeaKeyExchange();
174 * if (pKeaKeyExchangeAlice == null)
179 * pKeaKeyExchangeBob = new KeaKeyExchange();
180 * if (pKeaKeyExchangeBob == null)
185 * r = pKeaKeyExchangeAlice->Construct(*pKeyParams);
191 * r = pKeaKeyExchangeAlice->DoPhase(*pPriKeyAlice1, *pPubKeyBob1);
197 * r = pKeaKeyExchangeBob->Construct(*pKeyParams);
203 * r = pKeaKeyExchangeBob->DoPhase(*pPriKeyBob1, *pPubKeyAlice1);
209 * pBuffer = pKeaKeyExchangeAlice->GenerateSecretN(*pPriKeyAlice2, *pPubKeyBob2);
210 * if (pBuffer == null)
215 * pBuffer1 = pKeaKeyExchangeBob->GenerateSecretN(*pPriKeyBob2, *pPubKeyAlice2);
216 * if (pBuffer1 == null)
221 * if (*pBuffer == *pBuffer1)
223 * AppLog("Secret is Generated Successfully");
232 * delete pKeyPairGen;
233 * delete pKeyPairAlice1;
234 * delete pKeyPairAlice2;
235 * delete pKeyPairBob1;
236 * delete pKeyPairBob2;
239 * delete pKeaKeyExchangeAlice;
240 * delete pKeaKeyExchangeBob;
247 class _OSP_EXPORT_ KeaKeyExchange
248 : public virtual IKeyExchange
249 , public Tizen::Base::Object
254 * The object is not fully constructed after this constructor is called. For full construction, @n
255 * the Construct() method must be called right after calling this constructor.
259 KeaKeyExchange(void);
262 * This destructor overrides Tizen::Base::Object::~Object().
266 virtual ~KeaKeyExchange(void);
269 * Computes the shared secret in a phase-wise manner. @n
270 * The %DoPhase() method requires the first party's private key(s) and the second party's public key(s)
271 * to generate the shared secret.
272 * This method is used in algorithms such as the KEA algorithm and also for a multi-party key exchange.
276 * @return An error code
277 * @param[in] privateKey The private key component of the first party to instantiate
278 * @param[in] publicKey The public key component of the second party to instantiate
279 * @exception E_SUCCESS The method is successful.
280 * @exception E_INVALID_ARG A specified input parameter is invalid.
281 * @exception E_OUT_OF_MEMORY The memory is insufficient.
284 virtual result DoPhase(Tizen::Security::IPrivateKey& privateKey, Tizen::Security::IPublicKey& publicKey);
287 * Initializes this instance of %IKeyExchange with the specified key parameters.
291 * @return An error code
292 * @param[in] keyParameters The domain parameters of the key exchange algorithm @n
293 * This parameter needs to be instantiated.
294 * @exception E_SUCCESS The method is successful.
295 * @exception E_INVALID_ARG The specified input parameter is invalid.
296 * @exception E_OUT_OF_MEMORY The memory is insufficient.
298 virtual result Construct(const Tizen::Security::IKeyParameters& keyParameters);
301 * Generates the final shared secret among two parties.
305 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the generated secret key, @n
306 * else @c null if the method fails to generate the secret key
307 * @param[in] privateKey The private key component of the first party to instantiate
308 * @param[in] publicKey The public key component of the second party to instantiate
309 * @exception E_SUCCESS The method is successful.
310 * @exception E_INVALID_ARG A specified input parameter is invalid.
311 * @exception E_OUT_OF_MEMORY The memory is insufficient.
312 * @exception E_SYSTEM A system error has occurred. @n
313 * The method has failed to operate with the openssl library, or
314 * the Tizen::Base::ByteBuffer operation has failed.
315 * @remarks The specific error code can be accessed using the GetLastResult() method.
317 virtual Tizen::Base::ByteBuffer* GenerateSecretN(Tizen::Security::IPrivateKey& privateKey, Tizen::Security::IPublicKey& publicKey);
322 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
326 KeaKeyExchange(const KeaKeyExchange& rhs);
329 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
333 KeaKeyExchange& operator =(const KeaKeyExchange& rhs);
336 Tizen::Base::ByteBuffer* __pParamsP;
337 Tizen::Base::ByteBuffer* __pParamsG;
339 Tizen::Base::ByteBuffer* __pPrivateComponent;
340 Tizen::Base::ByteBuffer* __pPublicComponent;
342 class _KeaKeyExchangeImpl* __pKeaKeyExchangeImpl;
343 friend class _KeaKeyExchangeImpl;
347 } } } //Tizen::Security::Crypto
349 #endif //_FSEC_CRYPTO_KEA_KEY_EXCHANGE_H_