Merge "Fix accessing freed memory in X509CertificateStore::Update()" into tizen
[platform/framework/native/appfw.git] / src / security / crypto / FSecCryptoSha2Hmac.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                FSecCryptoSha2Hmac.cpp
19  * @brief               This file contains the implementation of Sha2Hmac class.
20  */
21 #include <unique_ptr.h>
22 #include <openssl/hmac.h>
23 #include <FBaseResult.h>
24 #include <FBaseErrors.h>
25 #include <FSecCryptoSha2Hmac.h>
26 #include <FSecSecretKey.h>
27 #include <FBaseSysLog.h>
28
29 using namespace Tizen::Base;
30
31
32 namespace Tizen { namespace Security { namespace Crypto
33 {
34
35 Sha2Hmac::Sha2Hmac(void)
36         : __pSha2HmacCtx(null)
37         , __pAlgorithm(EVP_sha256())
38         , __pSha2HmacImpl(null)
39 {
40 }
41
42 Sha2Hmac::~Sha2Hmac(void)
43 {
44         delete __pSha2HmacCtx;
45 }
46
47 result
48 Sha2Hmac::SetAlgorithm(const Tizen::Base::String& algorithm)
49 {
50         result r = E_SUCCESS;
51
52         if (algorithm.CompareTo(L"HMACSHA2/224") == 0)
53         {
54                 __pAlgorithm = EVP_sha224();
55         }
56         else if (algorithm.CompareTo(L"HMACSHA2/256") == 0)
57         {
58                 __pAlgorithm = EVP_sha256();
59         }
60         else if (algorithm.CompareTo(L"HMACSHA2/384") == 0)
61         {
62                 __pAlgorithm = EVP_sha384();
63         }
64         else if (algorithm.CompareTo(L"HMACSHA2/512") == 0)
65         {
66                 __pAlgorithm = EVP_sha512();
67         }
68         else
69         {
70                 r = E_UNSUPPORTED_ALGORITHM;
71         }
72
73         return r;
74 }
75
76 result
77 Sha2Hmac::SetKey(const Tizen::Security::ISecretKey& key)
78 {
79         result r = E_SUCCESS;
80         int dataLen = 0;
81         ByteBuffer* pBuffer = null;
82
83         //convert the ISecretKey to ByteBuffer
84         pBuffer = key.GetEncodedN();
85         SysTryReturn(NID_SEC_CRYPTO, pBuffer != null, GetLastResult(), GetLastResult(), "[%s] Failed to fill key buffer.", GetErrorMessage(GetLastResult()));
86
87         //converts into bytebuffer
88         dataLen = static_cast< int >(pBuffer->GetRemaining());
89         SysTryCatch(NID_SEC_CRYPTO, dataLen > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Key length should be positive.");
90
91         r = __keyBytes.Construct(*pBuffer);
92         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Input key buffer should be valid.", GetErrorMessage(r));
93
94 CATCH:
95         delete pBuffer;
96         return r;
97 }
98
99 ByteBuffer*
100 Sha2Hmac::GetHmacN(const ByteBuffer& input) const
101 {
102         result r = E_SUCCESS;
103         int dataLen = 0;
104         int keyLen = 0;
105         unsigned int mdLen = 0;
106         byte* pData = null;
107         byte* pKey = null;
108
109         ClearLastResult();
110
111         pData = const_cast< byte* >(input.GetPointer());
112         SysTryReturn(NID_SEC_CRYPTO, pData != null, null, E_INVALID_ARG, "[E_INVALID_ARG] Input data value should be valid.");
113
114         dataLen = input.GetRemaining();
115         SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] Input data length should be positive.");
116
117         pKey = const_cast< byte* >(__keyBytes.GetPointer());
118         SysTryReturn(NID_SEC_CRYPTO, pKey != null, null, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] Key value should be valid.");
119
120         keyLen = __keyBytes.GetRemaining();
121         SysTryReturn(NID_SEC_CRYPTO, keyLen > 0, null, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] Key length should be positive.");
122
123         mdLen = EVP_MD_size(__pAlgorithm);
124         SysTryReturn(NID_SEC_CRYPTO, mdLen > 0, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
125
126         std::unique_ptr<byte[]> pMd(new (std::nothrow) byte[mdLen]);
127         SysTryReturn(NID_SEC_CRYPTO, pMd != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
128
129         memset(pMd.get(), 0, mdLen);
130
131         //gets HMAC of the input data for SEC_MAC_SHA224
132         HMAC(__pAlgorithm, pKey, keyLen, pData, dataLen, pMd.get(), &mdLen);
133         SysTryReturn(NID_SEC_CRYPTO, mdLen != 0, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
134
135         //stores the HMAC output in bytebuffer
136         std::unique_ptr<ByteBuffer> pOutput(new (std::nothrow) ByteBuffer());
137         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create memory.");
138
139         r = pOutput->Construct(mdLen);
140         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_MEMORY] Failed to allocate memory.");
141
142         r = pOutput->SetArray(pMd.get(), 0, mdLen);
143         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
144
145         pOutput->Flip();
146
147         return pOutput.release();
148 }
149
150 result
151 Sha2Hmac::Initialize(void)
152 {
153         result r = E_SUCCESS;
154         int ret = 0;
155         int keyLen = 0;
156         byte* pKey = null;
157
158         SysAssertf(__pSha2HmacCtx == null, "Already constructed. Calling Initialize() twice or more on a same instance is not allowed for this class.");
159
160         __pSha2HmacCtx = new (std::nothrow) HMAC_CTX();
161         SysTryReturn(NID_SEC_CRYPTO, __pSha2HmacCtx != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
162
163         pKey = const_cast< byte* >(__keyBytes.GetPointer());
164         SysTryCatch(NID_SEC_CRYPTO, pKey != null, r = E_KEY_NOT_FOUND, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] The key value should be valid.");
165
166         keyLen = __keyBytes.GetRemaining();
167         SysTryCatch(NID_SEC_CRYPTO, keyLen > 0, r = E_KEY_NOT_FOUND, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] The key length should be positive.");
168
169         ret = HMAC_Init(__pSha2HmacCtx, pKey, keyLen, __pAlgorithm);
170         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
171
172 CATCH:
173
174         if (IsFailed(r))
175         {
176                 delete __pSha2HmacCtx;
177                 __pSha2HmacCtx = null;
178         }
179         return r;
180 }
181
182 result
183 Sha2Hmac::Update(const Tizen::Base::ByteBuffer& input)
184 {
185         result r = E_SUCCESS;
186         int ret = 0;
187         int dataLen = 0;
188         byte* pData = null;
189
190         SysAssertf(__pSha2HmacCtx != null, "Not yet constructed. Initialize() should be called before use.");
191
192         pData = const_cast< byte* >(input.GetPointer());
193         SysTryReturn(NID_SEC_CRYPTO, pData != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data value should be valid.");
194
195         dataLen = input.GetRemaining();
196         SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data length should be valid.");
197
198         //calls the HMAC_update function
199         ret = HMAC_Update(__pSha2HmacCtx, const_cast< byte* >(pData), static_cast< unsigned long >(dataLen));
200         SysTryReturn(NID_SEC_CRYPTO, ret == 1, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
201
202         return r;
203 }
204
205 Tizen::Base::ByteBuffer*
206 Sha2Hmac::FinalizeN(void)
207 {
208         result r = E_SUCCESS;
209         int ret = 0;
210         unsigned int outLen = 0;
211         std::unique_ptr<ByteBuffer> pOutput(null);
212
213         ClearLastResult();
214
215         SysAssertf(__pSha2HmacCtx != null && __pAlgorithm != null, "Not yet constructed. Initialize() and SetAlgorithm() should be called before use.");
216
217         outLen = EVP_MD_size(__pAlgorithm);
218
219         std::unique_ptr<byte[]> pTempBuf(new (std::nothrow) byte[outLen]);
220         SysTryCatch(NID_SEC_CRYPTO, pTempBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
221
222         //call the HMAC_final function based on the SHA2 algorithm and stores output in pTempBuf
223         ret = HMAC_Final(__pSha2HmacCtx, pTempBuf.get(), &outLen);
224         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
225
226         //write HMAC output in bytebuffer
227         pOutput = std::unique_ptr<ByteBuffer>(new (std::nothrow) ByteBuffer());
228         SysTryCatch(NID_SEC_CRYPTO, pOutput != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create memory.");
229
230         r = pOutput->Construct(outLen);
231         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
232
233         r = pOutput->SetArray(pTempBuf.get(), 0, outLen);
234         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
235
236         pOutput->Flip();
237
238 CATCH:
239
240         HMAC_CTX_cleanup(__pSha2HmacCtx);
241
242         if (IsFailed(r))
243         {
244                 pOutput.reset(null);
245         }
246
247         delete __pSha2HmacCtx;
248         __pSha2HmacCtx = null;
249
250         return pOutput.release();
251 }
252
253 } } } //Tizen::Security::Crypto