Merge "Fix accessing freed memory in X509CertificateStore::Update()" into tizen
[platform/framework/native/appfw.git] / src / security / crypto / FSecCrypto_KeaCore.cpp
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        FSecCrypto_KeaCore.cpp
19  * @brief       This file contains implementation of Kea Key Exchange algorithms and Shared Secret Generation.
20  */
21 #include <new>
22 #include <openssl/bn.h>
23 #include <unique_ptr.h>
24 #include <FBaseResult.h>
25 #include <FBaseErrors.h>
26 #include <FBaseSysLog.h>
27 #include "FSecCrypto_KeaCore.h"
28 #include "FSecCrypto_SkipJackCore.h"
29
30 namespace Tizen { namespace Security { namespace Crypto
31 {
32
33 static const int _DATA_ARRAY_SIZE = 2;
34 static const int _KEA_KEY_LOOP_VAR_1 = 5;
35 static const int _KEA_KEY_LOOP_VAR_2 = 7;
36 static const int _ONE_BIT_RESET_VAL = 0x00;
37 static const int _KEA_LSB_VAL_1 = 2;
38 static const int _KEA_LSB_VAL_2 = 3;
39 static const int _KEA_MSB_VAL_1 = 9;
40 static const int _KEA_MSB_VAL_2 = 8;
41 static const int _TWO_BYTE_VAL = 16;
42 static const int _CRYPTO_VARIABLE_SIGNIFICANT_BITS_ = 20;
43 static const int _PUBLIC_KEY_LENGTH = 1024;
44
45 Kea*
46 _KeaCore::CreateKeaN(void)
47 {
48         ClearLastResult();
49
50         std::unique_ptr <Kea> pRet (new (std::nothrow) Kea());
51         SysTryReturn(NID_SEC_CRYPTO, pRet != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new Kea object failed.");
52
53         pRet->version = 0;
54         pRet->pP = null;
55         pRet->pG = null;
56         pRet->length = 0;
57         pRet->pPubKey1 = null;
58         pRet->pPrivKey1 = null;
59         pRet->pPubKey2 = null;
60         pRet->pPrivKey2 = null;
61
62         return pRet.release();
63 }
64
65 void
66 _KeaCore::DeleteKea(Kea* pKea)
67 {
68         if (pKea->pP != null)
69         {
70                 BN_clear_free(pKea->pP);
71         }
72
73         if (pKea->pG != null)
74         {
75                 BN_clear_free(pKea->pG);
76         }
77
78         if (pKea->pPubKey1 != null)
79         {
80                 BN_clear_free(pKea->pPubKey1);
81         }
82
83         if (pKea->pPrivKey1 != null)
84         {
85                 BN_clear_free(pKea->pPrivKey1);
86         }
87
88         if (pKea->pPubKey2 != null)
89         {
90                 BN_clear_free(pKea->pPubKey2);
91         }
92
93         if (pKea->pPrivKey2 != null)
94         {
95                 BN_clear_free(pKea->pPrivKey2);
96         }
97
98         delete pKea;
99 }
100
101 result
102 _KeaCore::ComputeKeaKey(byte** ppKey, BIGNUM* pPublicKey1, BIGNUM* pPublicKey2, Kea& keaVar)
103 {
104         result r = E_SUCCESS;
105         int ret = 0;
106         byte secret[_PUBLIC_KEY_LENGTH] = {0, };
107         BN_CTX* pCtx = null;
108         BIGNUM* pTmp = null;
109         BIGNUM* pTmp1 = null;
110         BIGNUM* pRem = null;
111
112         SysTryReturn(NID_SEC_CRYPTO, ppKey != null && pPublicKey1 != null && pPublicKey2 != null
113                            , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Input key data and public keys should be valid.");
114
115         pCtx = BN_CTX_new();
116         SysTryReturn(NID_SEC_CRYPTO, pCtx != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
117
118         // Managing 1st set of public and private key
119         BN_CTX_start(pCtx);
120         pTmp = BN_CTX_get(pCtx);
121         SysTryCatch(NID_SEC_CRYPTO, pTmp != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
122
123         SysTryCatch(NID_SEC_CRYPTO, keaVar.pPrivKey1 != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Private key is not available.");
124
125         ret = BN_mod_exp(pTmp, pPublicKey1, keaVar.pPrivKey1, keaVar.pP, pCtx);
126         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
127
128         // Managing 2nd set of public and private key
129         pTmp1 = BN_CTX_get(pCtx);
130         SysTryCatch(NID_SEC_CRYPTO, pTmp1 != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
131
132         SysTryCatch(NID_SEC_CRYPTO, keaVar.pPrivKey2 != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Private key is not available.");
133
134         ret = BN_mod_exp(pTmp1, pPublicKey2, keaVar.pPrivKey2, keaVar.pP, pCtx);
135         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
136
137         // Performing operation pRem  = (pTmp + pTmp1) mod p
138         pRem = BN_CTX_get(pCtx);
139         SysTryCatch(NID_SEC_CRYPTO, pRem != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
140
141         ret = BN_add(pRem, pTmp, pTmp1);
142         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
143
144         ret = BN_mod(pTmp, pRem, keaVar.pP, pCtx);
145         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
146
147         ret = BN_bn2bin(pTmp, secret);
148         SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
149
150         keaVar.length = ret;
151
152         *ppKey = ComputeKeaSecret(secret, ret);
153         SysTryCatch(NID_SEC_CRYPTO, *ppKey != null, r = GetLastResult(), GetLastResult(), "[%s] Kea key secret should be valid.", GetErrorMessage(GetLastResult()));
154
155 CATCH:
156         if (pCtx != null)
157         {
158                 BN_CTX_end(pCtx);
159                 BN_CTX_free(pCtx);
160         }
161
162         return r;
163 }
164
165 byte*
166 _KeaCore::ComputeKeaSecret(byte* pKey, int len)
167 {
168         result r = E_SUCCESS;
169         int index = 0;
170         int ret = 0;
171         byte pad[_SKIPJACK_KEY_LENGTH] = {0x72, 0xf1, 0xa8, 0x7e, 0x92, 0x82, 0x41, 0x98, 0xab, 0x0b};
172         byte vector3[_DATA_ARRAY_SIZE] = {0, };
173         SkipJackKey sjVar = {{0, }, };
174         BIGNUM* pBn1 = null;
175         BIGNUM* pBn2 = null;
176         BIGNUM* pBn3 = null;
177         BN_CTX* pCtx = null;
178
179         ClearLastResult();
180
181         std::unique_ptr <byte[]> pVector1 (new (std::nothrow) byte[_SKIPJACK_KEY_LENGTH]);
182         SysTryReturn(NID_SEC_CRYPTO, pVector1 != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
183
184         std::unique_ptr <byte[]> pVector2 (new (std::nothrow) byte[_SKIPJACK_KEY_LENGTH]);
185         SysTryCatch(NID_SEC_CRYPTO, pVector2 != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
186
187         pCtx = BN_CTX_new();
188         SysTryCatch(NID_SEC_CRYPTO, pCtx != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
189
190         memset(&sjVar, 0, sizeof(sjVar));
191
192         memcpy(pVector1.get(), (pKey + len - _SKIPJACK_KEY_LENGTH), _SKIPJACK_KEY_LENGTH);
193
194         memcpy(pVector2.get(), (pKey + len - _CRYPTO_VARIABLE_SIGNIFICANT_BITS_), _SKIPJACK_KEY_LENGTH);
195
196         for (index = 0; index < _KEA_KEY_LOOP_VAR_1; index++)
197         {
198                 pVector1.get()[index] = pVector1.get()[index] + pVector1.get()[_KEA_MSB_VAL_1 - index];
199                 pVector1.get()[_KEA_MSB_VAL_1 - index] = pVector1.get()[index] - pVector1.get()[_KEA_MSB_VAL_1 - index];
200                 pVector1.get()[index] = pVector1.get()[index] - pVector1.get()[_KEA_MSB_VAL_1 - index];
201
202                 pVector2.get()[index] = pVector2.get()[index] + pVector2.get()[_KEA_MSB_VAL_1 - index];
203                 pVector2.get()[_KEA_MSB_VAL_1 - index] = pVector2.get()[index] - pVector2.get()[_KEA_MSB_VAL_1 - index];
204                 pVector2.get()[index] = pVector2.get()[index] - pVector2.get()[_KEA_MSB_VAL_1 - index];
205         }
206
207         //for creating key pVector1 ^ pad
208         for (index = 0; index < _SKIPJACK_KEY_LENGTH; index++)
209         {
210                 pVector1.get()[index] = pVector1.get()[index] ^ pad[index];
211         }
212
213         //for doing (pVector2 / 2^16) mod 2 ^64
214         //1st Step: we perform left shift on pVector2 by 2 bytes to perform (pVector2 / 2^16)
215         for (index = _KEA_KEY_LOOP_VAR_2; index >= 0; index--)
216         {
217                 pVector2.get()[_DATA_ARRAY_SIZE + index] = pVector2.get()[index];
218         }
219
220         //Setting 1st 2 bytes as 0 in continuation from the above operation
221         // no need for explicitly doing mod 2^64 opeartion since number will be the same
222         // as 2^64 -1 = 0xFFFFFFFFFFFFFFFF and that is the biggest 64 bit number
223         // and x is also a 64 bit number
224         // therefore x mod 2^64 = x
225         for (index = 0; index < _DATA_ARRAY_SIZE; index++)
226         {
227                 pVector2.get()[index] = _ONE_BIT_RESET_VAL;
228         }
229
230         // making modified pVector1 as key for doing skipjack operation
231         for (index = 0; index < _SKIPJACK_KEY_LENGTH; index++)
232         {
233                 sjVar.keyVal[index] = pVector1.get()[index];
234         }
235
236         BN_CTX_start(pCtx);
237
238         pBn2 = BN_CTX_get(pCtx);
239         SysTryCatch(NID_SEC_CRYPTO, pBn2 != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
240
241         pBn3 = BN_CTX_get(pCtx);
242         SysTryCatch(NID_SEC_CRYPTO, pBn3 != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
243
244         //Peforming pVector2 mod 2exp16 and storing it in vector3
245         //pVector2 in to big number
246         pBn1 = BN_bin2bn(pVector2.get(), _SKIPJACK_KEY_LENGTH, null);
247         SysTryCatch(NID_SEC_CRYPTO, pBn1 != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
248
249         //2^16 into big number
250         ret = BN_lshift(pBn2, BN_value_one(), _TWO_BYTE_VAL);
251         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
252
253         // performing pVector2 mod 2^16
254         ret = BN_mod(pBn3, pBn1, pBn2, pCtx);
255         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
256
257         // storing it in array
258         ret = BN_bn2bin(pBn3, vector3);
259         SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
260
261         //Performing encryption over (pVector2/2^16) mod 2 ^ 64
262         _SkipJackCore::DoCipherEcb(static_cast< const byte* >(pVector2.get() + _DATA_ARRAY_SIZE), pVector1.get(), sjVar, static_cast< bool >(1));
263
264         //Performing second encryption on the output data from previous encryption
265         for (index = _KEA_KEY_LOOP_VAR_2; index >= 0; index--)
266         {
267                 pVector1.get()[_DATA_ARRAY_SIZE + index] = pVector1.get()[index];
268                 //printf("%c\t",pVector2[index]);
269         }
270
271         for (index = 0; index < _DATA_ARRAY_SIZE; index++)
272         {
273                 pVector1.get()[index] = _ONE_BIT_RESET_VAL;
274                 //printf("%c \t",pVector2[index]);
275         }
276
277         _SkipJackCore::DoCipherEcb(static_cast< const byte* >(pVector1.get() + _DATA_ARRAY_SIZE), pVector2.get(), sjVar, static_cast< bool >(1));
278
279         // xoring pVector1 msb with vector3
280         pVector1.get()[0] = pVector1.get()[0] ^ vector3[0];
281         pVector1.get()[1] = pVector1.get()[1] ^ vector3[1];
282
283         //adding the xored bits as lsb in pVector1
284         pVector2.get()[_KEA_MSB_VAL_2] = pVector1.get()[_KEA_LSB_VAL_1];
285         pVector2.get()[_KEA_MSB_VAL_1] = pVector1.get()[_KEA_LSB_VAL_2];
286
287 CATCH:
288
289         if (pCtx != null)
290         {
291                 BN_CTX_end(pCtx);
292                 BN_CTX_free(pCtx);
293         }
294
295         return pVector2.release();
296 }
297
298 } } } //Tizen::Security::Crypto