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 FSecCryptoKeaKeyExchange.h
19 * @brief This is the header file for the %KeaKeyExchange class.
21 * This header file contains the declarations of the %KeaKeyExchange class.
23 #ifndef _FSEC_CRYPTO_KEA_KEY_EXCHANGE_H_
24 #define _FSEC_CRYPTO_KEA_KEY_EXCHANGE_H_
26 #include <FSecCryptoIKeyExchange.h>
29 namespace Tizen { namespace Security { namespace Crypto
33 * @class KeaKeyExchange
34 * @brief This class provides methods for performing the key exchange mechanism using Key Exchange Algorithm (KEA).
38 * The %KeaKeyExchange class provides a KEA 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 %KeaKeyExchange class.
48 * void KeaGenerateSecretExample(void)
51 * result r = E_SUCCESS;
52 * KeyPair *pKeyPairAlice1 = null;
53 * IPrivateKey *pPriKeyAlice1 = null;
54 * IPrivateKey *pPriKeyAlice2 = null;
55 * KeyPair *pKeyPairAlice2 = null;
56 * IPublicKey *pPubKeyAlice1 = null;
57 * IPublicKey *pPubKeyAlice2 = null;
59 * KeyPair *pKeyPairBob1 = null;
60 * IPrivateKey *pPriKeyBob1 = null;
61 * IPrivateKey *pPriKeyBob2 = null;
62 * KeyPair *pKeyPairBob2 = null;
63 * IPublicKey *pPubKeyBob1 = null;
64 * IPublicKey *pPubKeyBob2 = null;
67 * KeyPairGenerator *pKeyPairGen = null;
68 * IKeyParameters *pKeyParams = null;
71 * KeaKeyExchange *pKeaKeyExchangeAlice = null;
72 * KeaKeyExchange *pKeaKeyExchangeBob = null;
73 * ByteBuffer *pBuffer = null;
74 * ByteBuffer *pBuffer1 = null;
77 * // Generates the key.
78 * pKeyPairGen = new KeyPairGenerator();
79 * if (pKeyPairGen == null)
85 * r = pKeyPairGen->Construct(size, L"KEA");
92 * pKeyParams = pKeyPairGen->GenerateKeyParametersN();
93 * if (pKeyParams == null)
99 * pKeyPairAlice1 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
100 * if (pKeyPairAlice1 == null)
105 * pKeyPairAlice2 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
106 * if (pKeyPairAlice2 == null)
111 * pPriKeyAlice1 = pKeyPairAlice1->GetPrivateKey();
112 * if (pPriKeyAlice1 == null)
117 * pPubKeyAlice1 = pKeyPairAlice1->GetPublicKey();
118 * if (pPubKeyAlice1 == null)
123 * pPriKeyAlice2 = pKeyPairAlice2->GetPrivateKey();
124 * if (pPriKeyAlice2 == null)
129 * pPubKeyAlice2 = pKeyPairAlice2->GetPublicKey();
130 * if (pPubKeyAlice2 == null)
135 * pKeyPairBob1 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
136 * if (pKeyPairBob1 == null)
141 * pKeyPairBob2 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
142 * if (pKeyPairBob2 == null)
147 * pPriKeyBob1 = pKeyPairBob1->GetPrivateKey();
148 * if (pPriKeyBob1 == null)
153 * pPubKeyBob1 = pKeyPairBob1->GetPublicKey();
154 * if (pPubKeyBob1 == null)
159 * pPriKeyBob2 = pKeyPairBob2->GetPrivateKey();
160 * if (pPriKeyBob2 == null)
165 * pPubKeyBob2 = pKeyPairBob2->GetPublicKey();
166 * if (pPubKeyBob2 == null)
172 * pKeaKeyExchangeAlice = new KeaKeyExchange();
173 * if (pKeaKeyExchangeAlice == null)
178 * pKeaKeyExchangeBob = new KeaKeyExchange();
179 * if (pKeaKeyExchangeBob == null)
184 * r = pKeaKeyExchangeAlice->Construct(*pKeyParams);
190 * r = pKeaKeyExchangeAlice->DoPhase(*pPriKeyAlice1, *pPubKeyBob1);
196 * r = pKeaKeyExchangeBob->Construct(*pKeyParams);
202 * r = pKeaKeyExchangeBob->DoPhase(*pPriKeyBob1, *pPubKeyAlice1);
208 * pBuffer = pKeaKeyExchangeAlice->GenerateSecretN(*pPriKeyAlice2, *pPubKeyBob2);
209 * if (pBuffer == null)
214 * pBuffer1 = pKeaKeyExchangeBob->GenerateSecretN(*pPriKeyBob2, *pPubKeyAlice2);
215 * if (pBuffer1 == null)
220 * if (*pBuffer == *pBuffer1)
222 * AppLog("Secret is Generated Successfully");
231 * delete pKeyPairGen;
232 * delete pKeyPairAlice1;
233 * delete pKeyPairAlice2;
234 * delete pKeyPairBob1;
235 * delete pKeyPairBob2;
238 * delete pKeaKeyExchangeAlice;
239 * delete pKeaKeyExchangeBob;
246 class _OSP_EXPORT_ KeaKeyExchange
247 : public virtual IKeyExchange
248 , public Tizen::Base::Object
253 * The object is not fully constructed after this constructor is called. For full construction, @n
254 * the Construct() method must be called right after calling this constructor.
258 KeaKeyExchange(void);
261 * This destructor overrides Tizen::Base::Object::~Object().
265 virtual ~KeaKeyExchange(void);
268 * Computes the shared secret in a phase-wise manner. @n
269 * The %DoPhase() method requires the first party's private key(s) and the second party's public key(s)
270 * to generate the shared secret.
271 * This method is used in algorithms such as the KEA algorithm and also for a multi-party key exchange.
275 * @return An error code
276 * @param[in] privateKey The private key component of the first party to instantiate
277 * @param[in] publicKey The public key component of the second party to instantiate
278 * @exception E_SUCCESS The method is successful.
279 * @exception E_INVALID_ARG A specified input parameter is invalid.
280 * @exception E_OUT_OF_MEMORY The memory is insufficient.
283 virtual result DoPhase(Tizen::Security::IPrivateKey& privateKey, Tizen::Security::IPublicKey& publicKey);
286 * Initializes this instance of %IKeyExchange with the specified key parameters.
290 * @return An error code
291 * @param[in] keyParameters The domain parameters of the key exchange algorithm @n
292 * This parameter needs to be instantiated.
293 * @exception E_SUCCESS The method is successful.
294 * @exception E_INVALID_ARG The specified input parameter is invalid.
295 * @exception E_OUT_OF_MEMORY The memory is insufficient.
297 virtual result Construct(const Tizen::Security::IKeyParameters& keyParameters);
300 * Generates the final shared secret among two parties.
304 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the generated secret key, @n
305 * else @c null if the method fails to generate the secret key
306 * @param[in] privateKey The private key component of the first party to instantiate
307 * @param[in] publicKey The public key component of the second party to instantiate
308 * @exception E_SUCCESS The method is successful.
309 * @exception E_INVALID_ARG A specified input parameter is invalid.
310 * @exception E_OUT_OF_MEMORY The memory is insufficient.
311 * @exception E_SYSTEM A system error has occurred. @n
312 * The method has failed to operate with the openssl library, or
313 * the Tizen::Base::ByteBuffer operation has failed.
314 * @remarks The specific error code can be accessed using the GetLastResult() method.
316 virtual Tizen::Base::ByteBuffer* GenerateSecretN(Tizen::Security::IPrivateKey& privateKey, Tizen::Security::IPublicKey& publicKey);
321 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
325 KeaKeyExchange(const KeaKeyExchange& rhs);
328 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
332 KeaKeyExchange& operator =(const KeaKeyExchange& rhs);
335 Tizen::Base::ByteBuffer* __pParamsP;
336 Tizen::Base::ByteBuffer* __pParamsG;
338 Tizen::Base::ByteBuffer* __pPrivateComponent;
339 Tizen::Base::ByteBuffer* __pPublicComponent;
341 class _KeaKeyExchangeImpl* __pKeaKeyExchangeImpl;
342 friend class _KeaKeyExchangeImpl;
346 } } } //Tizen::Security::Crypto
348 #endif //_FSEC_CRYPTO_KEA_KEY_EXCHANGE_H_