2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FSecCryptoSha1Hmac.cpp
19 * @brief This file contains the implementation of Sha1Hmac class.
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>
29 using namespace Tizen::Base;
32 namespace Tizen { namespace Security { namespace Crypto
35 static const int _SHA1_DIGEST_LENGTH = 20;
37 Sha1Hmac::Sha1Hmac(void)
38 : __pSha1HmacCtx(null)
39 , __pSha1HmacImpl(null)
43 Sha1Hmac::~Sha1Hmac(void)
45 delete __pSha1HmacCtx;
49 Sha1Hmac::SetAlgorithm(const Tizen::Base::String& algorithm)
51 // Unsupported in Sha1Hmac
52 return E_UNSUPPORTED_ALGORITHM;
56 Sha1Hmac::SetKey(const Tizen::Security::ISecretKey& key)
60 ByteBuffer* pBuffer = null;
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()));
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.");
70 r = __keyBytes.Construct(*pBuffer);
71 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Input key buffer should be valid.", GetErrorMessage(r));
79 Sha1Hmac::GetHmacN(const ByteBuffer& input) const
84 unsigned int mdLen = 0;
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.");
93 dataLen = input.GetRemaining();
94 SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] Input data length should be positive.");
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.");
99 memset(pMd.get(), 0, _SHA1_DIGEST_LENGTH);
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.");
104 keyLen = __keyBytes.GetRemaining();
105 SysTryReturn(NID_SEC_CRYPTO, keyLen, null, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] Key length should be positive.");
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.");
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.");
115 r = pOutput->Construct(mdLen);
116 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_MEMORY] Failed to allocate memory.");
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.");
123 return pOutput.release();
127 Sha1Hmac::Initialize(void)
129 result r = E_SUCCESS;
134 SysAssertf(__pSha1HmacCtx == null, "Already constructed. Calling Initialize() twice or more on a same instance is not allowed for this class.");
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.");
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.");
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.");
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.");
153 delete __pSha1HmacCtx;
154 __pSha1HmacCtx = null;
160 Sha1Hmac::Update(const Tizen::Base::ByteBuffer& input)
162 result r = E_SUCCESS;
167 SysAssertf(__pSha1HmacCtx != null, "Not yet constructed. Initialize() should be called before use.");
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.");
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.");
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.");
182 Tizen::Base::ByteBuffer*
183 Sha1Hmac::FinalizeN(void)
185 result r = E_SUCCESS;
187 unsigned int outLen = _SHA1_DIGEST_LENGTH;
188 std::unique_ptr<ByteBuffer> pOutput(null);
192 SysAssertf(__pSha1HmacCtx != null, "Not yet constructed. Initialize() should be called before use.");
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.");
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.");
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.");
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.");
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.");
215 HMAC_CTX_cleanup(__pSha1HmacCtx);
222 delete __pSha1HmacCtx;
223 __pSha1HmacCtx = null;
225 return pOutput.release();
228 } } } //Tizen::Security::Crypto