2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
17 * @file wrt_crypto_hash.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of cryptographic hasing algorithms
22 #include <vcore/CryptoHash.h>
24 #include <openssl/bio.h>
25 #include <openssl/ssl.h>
26 #include <openssl/evp.h>
29 #include <vcore/Base64.h>
31 namespace ValidationCore
37 namespace // anonymous
39 const size_t HASH_DIGEST_STREAM_FEED_SIZE = 1024;
40 } // namespace anonymous
51 void Base::Append(const char *buffer)
54 VcoreThrowMsg(Crypto::Hash::OutOfSequence,
55 "Cannot append hash after final update!");
57 HashUpdate(buffer, strlen(buffer));
60 void Base::Append(const char *buffer, size_t bufferSize)
63 VcoreThrowMsg(Crypto::Hash::OutOfSequence,
64 "Cannot append hash after final update!");
66 HashUpdate(buffer, bufferSize);
69 void Base::Append(const std::string &buffer)
72 VcoreThrowMsg(Crypto::Hash::OutOfSequence,
73 "Cannot append hash after final update!");
75 HashUpdate(buffer.c_str(), buffer.size());
78 void Base::Append(std::istream &stream)
81 VcoreThrowMsg(Crypto::Hash::OutOfSequence,
82 "Cannot append hash after final update!");
84 char buffer[HASH_DIGEST_STREAM_FEED_SIZE];
88 stream.read(buffer, HASH_DIGEST_STREAM_FEED_SIZE);
90 if (stream.gcount() > 0)
91 Append(static_cast<void *>(buffer), static_cast<size_t>(stream.gcount()));
93 } while (stream.gcount() > 0);
96 void Base::Append(const void *data, size_t dataSize)
99 VcoreThrowMsg(Crypto::Hash::OutOfSequence,
100 "Cannot append hash after final update!");
102 HashUpdate(data, dataSize);
110 // Finalize hashing algorithm
113 // Convert to base 64 string
114 Base64Encoder encoder;
116 encoder.append(std::string(m_raw.begin(), m_raw.end()));
118 m_base64StringHash = encoder.get();
123 std::string Base::ToBase64String() const
125 return m_base64StringHash;
128 Raw Base::GetHash() const
133 OpenSSL::OpenSSL(const EVP_MD *evpMd)
136 EVP_MD_CTX_init(&m_context);
138 if (EVP_DigestInit(&m_context, evpMd) != 1)
139 VcoreThrowMsg(Crypto::Hash::AppendFailed,
140 "EVP_DigestInit failed!");
147 // Just clean context
148 EVP_MD_CTX_cleanup(&m_context);
153 void OpenSSL::HashUpdate(const void *data, size_t dataSize)
156 VcoreThrowMsg(Crypto::Hash::AppendFailed,
157 "OpenSSLHash hash already finalized!");
159 if (EVP_DigestUpdate(&m_context, data, dataSize) != 1)
160 VcoreThrowMsg(Crypto::Hash::AppendFailed,
161 "EVP_DigestUpdate failed!");
164 Hash::Raw OpenSSL::HashFinal()
167 VcoreThrowMsg(Crypto::Hash::AppendFailed,
168 "OpenSSLHash hash already finalized!");
170 unsigned char hash[EVP_MAX_MD_SIZE] = {};
171 unsigned int hashLength;
173 // Also cleans context
174 if (EVP_DigestFinal(&m_context, hash, &hashLength) != 1)
175 VcoreThrowMsg(Crypto::Hash::AppendFailed,
176 "EVP_DigestFinal failed!");
179 return Raw(hash, hash + hashLength);
183 } // namespace Crypto
184 } // namespace ValidationCore