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 FSecCryptoSha1Hash.cpp
19 * @brief This file contains the implementation of Sha1Hash class.
21 #include <unique_ptr.h>
22 #include <openssl/sha.h>
23 #include <openssl/evp.h>
24 #include <FBaseResult.h>
25 #include <FBaseErrors.h>
26 #include <FSecCryptoSha1Hash.h>
27 #include <FBaseSysLog.h>
29 using namespace Tizen::Base;
32 namespace Tizen { namespace Security { namespace Crypto
35 Sha1Hash::Sha1Hash(void)
37 , __pSha1HashImpl(null)
41 Sha1Hash::~Sha1Hash(void)
47 Sha1Hash::SetAlgorithm(const Tizen::Base::String& algorithm)
49 // Unsupported in Sha1Hash
50 return E_UNSUPPORTED_ALGORITHM;
54 Sha1Hash::GetHashN(const ByteBuffer& input) const
58 unsigned int outLen = 0;
60 byte hashOut[SHA_DIGEST_LENGTH] = {'\0'};
64 pData = const_cast< byte* >(input.GetPointer());
65 SysTryReturn(NID_SEC_CRYPTO, pData != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The input data value should be valid.");
67 dataLen = input.GetRemaining();
68 SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The input data length should be positive.");
70 //this function returns the SHA1 hash hashOut
71 EVP_Digest(pData, dataLen, hashOut, &outLen, EVP_sha1(), null);
72 SysTryReturn(NID_SEC_CRYPTO, outLen > 0, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
74 //stores the hash in bytebuffer
75 std::unique_ptr<ByteBuffer> pOutput(new (std::nothrow) ByteBuffer());
76 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
78 r = pOutput->Construct(outLen);
79 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
81 r = pOutput->SetArray(hashOut, 0, outLen);
82 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
86 return pOutput.release();
90 Sha1Hash::Initialize(void)
95 SysAssertf(__pEvpMdCtx == null, "Already constructed. Calling Initialize() twice or more on a same instance is not allowed for this class.");
97 __pEvpMdCtx = new (std::nothrow) EVP_MD_CTX();
98 SysTryReturn(NID_SEC_CRYPTO, __pEvpMdCtx != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
100 //SHA1 init is called
101 ret = EVP_DigestInit(__pEvpMdCtx, EVP_sha1());
102 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
115 Sha1Hash::Update(const ByteBuffer& input)
117 result r = E_SUCCESS;
122 SysAssertf(__pEvpMdCtx != null, "Not yet constructed. Initialize() should be called before use.");
124 pData = const_cast< byte* >(input.GetPointer());
125 SysTryReturn(NID_SEC_CRYPTO, pData != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data value should be valid.");
127 dataLen = input.GetRemaining();
128 SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data length should be positive.");
130 //SHA1_update is called
131 ret = EVP_DigestUpdate(__pEvpMdCtx, pData, dataLen);
132 SysTryReturn(NID_SEC_CRYPTO, ret == 1, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
138 Sha1Hash::FinalizeN(void)
140 result r = E_SUCCESS;
142 int outLen = SHA_DIGEST_LENGTH;
143 std::unique_ptr<ByteBuffer> pOutput(null);
147 SysAssertf(__pEvpMdCtx != null, "Not yet constructed. Initialize() should be called before use.");
149 std::unique_ptr<byte[]> pTempBuf(new (std::nothrow) byte[outLen]);
150 SysTryCatch(NID_SEC_CRYPTO, pTempBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
152 //SHA1_final is called and is stored in pTempBuf
153 ret = EVP_DigestFinal(__pEvpMdCtx, pTempBuf.get(), null);
154 SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
156 //hash is stored in bytebuffer
157 pOutput = std::unique_ptr<ByteBuffer>(new (std::nothrow) ByteBuffer());
158 SysTryCatch(NID_SEC_CRYPTO, pOutput != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
160 r = pOutput->Construct(outLen);
161 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
163 r = pOutput->SetArray(pTempBuf.get(), 0, outLen);
164 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
170 EVP_MD_CTX_cleanup(__pEvpMdCtx);
180 return pOutput.release();
183 } } } //Tizen::Security::Crypto