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 FSecCryptoSha2Hmac.cpp
19 * @brief This file contains the implementation of Sha2Hmac class.
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>
29 using namespace Tizen::Base;
32 namespace Tizen { namespace Security { namespace Crypto
35 Sha2Hmac::Sha2Hmac(void)
36 : __pSha2HmacCtx(null)
37 , __pAlgorithm(EVP_sha256())
38 , __pSha2HmacImpl(null)
42 Sha2Hmac::~Sha2Hmac(void)
44 delete __pSha2HmacCtx;
48 Sha2Hmac::SetAlgorithm(const Tizen::Base::String& algorithm)
52 if (algorithm.CompareTo(L"HMACSHA2/224") == 0)
54 __pAlgorithm = EVP_sha224();
56 else if (algorithm.CompareTo(L"HMACSHA2/256") == 0)
58 __pAlgorithm = EVP_sha256();
60 else if (algorithm.CompareTo(L"HMACSHA2/384") == 0)
62 __pAlgorithm = EVP_sha384();
64 else if (algorithm.CompareTo(L"HMACSHA2/512") == 0)
66 __pAlgorithm = EVP_sha512();
70 r = E_UNSUPPORTED_ALGORITHM;
77 Sha2Hmac::SetKey(const Tizen::Security::ISecretKey& key)
81 ByteBuffer* pBuffer = null;
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()));
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.");
91 r = __keyBytes.Construct(*pBuffer);
92 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Input key buffer should be valid.", GetErrorMessage(r));
100 Sha2Hmac::GetHmacN(const ByteBuffer& input) const
102 result r = E_SUCCESS;
105 unsigned int mdLen = 0;
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.");
114 dataLen = input.GetRemaining();
115 SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] Input data length should be positive.");
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.");
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.");
123 mdLen = EVP_MD_size(__pAlgorithm);
124 SysTryReturn(NID_SEC_CRYPTO, mdLen > 0, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
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.");
129 memset(pMd.get(), 0, mdLen);
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.");
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.");
139 r = pOutput->Construct(mdLen);
140 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_MEMORY] Failed to allocate memory.");
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.");
147 return pOutput.release();
151 Sha2Hmac::Initialize(void)
153 result r = E_SUCCESS;
158 SysAssertf(__pSha2HmacCtx == null, "Already constructed. Calling Initialize() twice or more on a same instance is not allowed for this class.");
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.");
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.");
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.");
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.");
176 delete __pSha2HmacCtx;
177 __pSha2HmacCtx = null;
183 Sha2Hmac::Update(const Tizen::Base::ByteBuffer& input)
185 result r = E_SUCCESS;
190 SysAssertf(__pSha2HmacCtx != null, "Not yet constructed. Initialize() should be called before use.");
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.");
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.");
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.");
205 Tizen::Base::ByteBuffer*
206 Sha2Hmac::FinalizeN(void)
208 result r = E_SUCCESS;
210 unsigned int outLen = 0;
211 std::unique_ptr<ByteBuffer> pOutput(null);
215 SysAssertf(__pSha2HmacCtx != null && __pAlgorithm != null, "Not yet constructed. Initialize() and SetAlgorithm() should be called before use.");
217 outLen = EVP_MD_size(__pAlgorithm);
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.");
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.");
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.");
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.");
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.");
240 HMAC_CTX_cleanup(__pSha2HmacCtx);
247 delete __pSha2HmacCtx;
248 __pSha2HmacCtx = null;
250 return pOutput.release();
253 } } } //Tizen::Security::Crypto