sync with tizen_2.0
[platform/framework/native/appfw.git] / inc / FSecCryptoDhKeyExchange.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                        FSecCryptoDhKeyExchange.h
20  * @brief               This is the header file for the %DhKeyExchange class.
21  *
22  * This header file contains the declarations of the %DhKeyExchange class.
23  */
24 #ifndef _FSEC_CRYPTO_DH_KEYEXCHANGE_H_
25 #define _FSEC_CRYPTO_DH_KEYEXCHANGE_H_
26
27 #include <FSecCryptoIKeyExchange.h>
28
29
30 namespace Tizen { namespace Security { namespace Crypto
31 {
32
33 /**
34  *      @class          DhKeyExchange
35  *      @brief          This class provides methods for a key exchange mechanism using the Diffie-Hellman (DH) algorithm.
36  *
37  *      @since       2.0
38  *
39  *      The %DhKeyExchange class provides a DH key exchange between two communicating users. @n
40  *
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>.
42  *
43  *      @see    IKeyExchange
44  *      @see    KeyPairGenerator
45  *
46  *      The following example demonstrates how to use the %DhKeyExchange class.
47  *      @code
48  *
49  *      void DhGenerateSecretExample(void)
50  *      {
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;
60  *              int size = 1024;
61  *              DhKeyExchange  *pDhKeyExchangeAlice = null;
62  *              DhKeyExchange  *pDhKeyExchangeBob = null;
63  *              ByteBuffer *pBuffer = null;
64  *              ByteBuffer *pBuffer1 = null;
65  *
66  *              SetLastResult(E_SUCCESS);
67  *              // Generates the key.
68  *              pKeyPairGen = new KeyPairGenerator();
69  *              if (pKeyPairGen == null)
70  *              {
71  *                      goto CATCH;
72  *              }
73  *
74  *
75  *              r = pKeyPairGen->Construct(size, "DH");
76  *              if (IsFailed(r))
77  *              {
78  *                      goto CATCH;
79  *              }
80  *
81  *              pKeyParams = pKeyPairGen->GenerateKeyParametersN();
82  *              if (pKeyParams == null)
83  *              {
84  *                      goto CATCH;
85  *              }
86  *
87  *
88  *              pKeyPair = pKeyPairGen->GenerateKeyPairN(pKeyParams);
89  *              if (pKeyPair == null)
90  *              {
91  *                      goto CATCH;
92  *              }
93  *
94  *              pKeyPair1 = pKeyPairGen->GenerateKeyPairN(pKeyParams);
95  *              if (pKeyPair1 == null)
96  *              {
97  *                      goto CATCH;
98  *              }
99  *
100  *
101  *              pPubKey = pKeyPair->GetPublicKey();
102  *              if (pPubKey == null)
103  *              {
104  *                      goto CATCH;
105  *              }
106  *
107  *
108  *              pPriKey = pKeyPair->GetPrivateKey();
109  *              if (pPriKey == null)
110  *              {
111  *                      goto CATCH;
112  *              }
113  *
114  *
115  *              pPubKey1 = pKeyPair1->GetPublicKey();
116  *              if (pPubKey1 == null)
117  *              {
118  *                      goto CATCH;
119  *              }
120  *
121  *
122  *              pPriKey1 = pKeyPair1->GetPrivateKey();
123  *              if (pPriKey1 == null)
124  *              {
125  *                      goto CATCH;
126  *              }
127  *
128  *
129  *              pDhKeyExchangeAlice  = new DhKeyExchange ();
130  *              if (pDhKeyExchangeAlice == null)
131  *              {
132  *                      goto CATCH;
133  *              }
134  *
135  *              pDhKeyExchangeBob  = new DhKeyExchange ();
136  *              if (pDhKeyExchangeBob == null)
137  *              {
138  *                      goto CATCH;
139  *              }
140  *
141  *              // Calling the Construct API.
142  *              r = pDhKeyExchangeAlice->Construct(*pKeyParams);
143  *              if (IsFailed(r))
144  *              {
145  *                      goto CATCH;
146  *              }
147  *
148  *              r = pDhKeyExchangeBob->Construct(*pKeyParams);
149  *              if (IsFailed(r))
150  *              {
151  *                      goto CATCH;
152  *              }
153  *
154  *              pBuffer = pDhKeyExchangeAlice->GenerateSecretN(*pPriKey, *pPubKey1);
155  *              if (pBuffer == null)
156  *              {
157  *                      goto CATCH;
158  *              }
159  *
160  *              pBuffer1 = pDhKeyExchangeBob->GenerateSecretN(*pPriKey1, *pPubKey);
161  *              if (pBuffer1 == null)
162  *              {
163  *                      goto CATCH;
164  *              }
165  *
166  *              if (*pBuffer == *pBuffer1)
167  *              {
168  *                      AppLog("The secret is generated SuccessFully");
169  *              }
170  *              else
171  *              {
172  *                      goto CATCH;
173  *              }
174  *
175  *      CATCH:
176  *              delete pKeyPairGen;
177  *              delete pKeyPair;
178  *              delete pKeyPair1;
179  *              delete pBuffer;
180  *              delete pBuffer1;
181  *              delete pDhKeyExchangeAlice;
182  *              delete pDhKeyExchangeBob;
183  *              delete pKeyParams;
184  *
185  *      }
186  *
187  *      @endcode
188  *
189  */
190
191 class _OSP_EXPORT_ DhKeyExchange
192         : public virtual IKeyExchange
193         , public Tizen::Base::Object
194 {
195
196 public:
197         /**
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.
200          *
201          *      @since          2.0
202          */
203         DhKeyExchange(void);
204
205         /**
206          * This destructor overrides Tizen::Base::Object::~Object().
207          *
208          *      @since          2.0
209          */
210         virtual ~DhKeyExchange(void);
211
212         /**
213          *      Initializes this instance of %DhKeyExchange with the specified key parameters.
214          *
215          *      @since      2.0
216          *
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);
224
225         /**
226          *      Generates the final shared secret between two parties.
227          *
228          *      @since          2.0
229          *
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.
241          */
242         virtual Tizen::Base::ByteBuffer* GenerateSecretN(Tizen::Security::IPrivateKey& privateKey, Tizen::Security::IPublicKey& publicKey);
243
244 private:
245
246         //
247         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
248         //
249         // @since 2.0
250         //
251         DhKeyExchange(const DhKeyExchange& rhs);
252
253         //
254         // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
255         //
256         // @since 2.0
257         //
258         DhKeyExchange& operator =(const DhKeyExchange& rhs);
259
260 private:
261         Tizen::Base::ByteBuffer* __pParamsP;
262         Tizen::Base::ByteBuffer* __pParamsG;
263
264         class _DhKeyExchangeImpl* __pDhKeyExchangeImpl;
265         friend class _DhKeyExchangeImpl;
266
267 }; //DhKeyExchange
268
269 } } } //Tizen::Security::Crypto
270
271 #endif //_FSEC_CRYPTO_DH_KEYEXCHANGE_H_