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