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