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 FSecCryptoIHash.h
19 * @brief This is the header file for the %IHash interface.
21 * This header file contains the declarations of the %IHash interface.
23 #ifndef _FSEC_CRYPTO_IHASH_H_
24 #define _FSEC_CRYPTO_IHASH_H_
26 #include <FBaseString.h>
27 #include <FBaseByteBuffer.h>
29 namespace Tizen { namespace Security { namespace Crypto
34 * @brief This interface provides the functionality of a hash algorithm.
38 * The %IHash interface provides the functionality of a hash algorithm. @n
40 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/hashing.htm">Hashing</a>. @n
42 * The following example demonstrates how to use the %IHash interface.
46 * MyClass::TestHashSample(void)
48 * const int messageLen = 49;
49 * static const byte message[messageLen] = {
50 * 0xF4, 0xA4, 0xA2, 0x40, 0xB0, 0xAB, 0x73, 0x1B, 0xC8, 0x10, 0xEA, 0x08, 0x9C, 0xD0, 0x78, 0x0D,
51 * 0x40, 0xB9, 0x94, 0x02, 0x21, 0x79, 0xFD, 0x5A, 0xA3, 0xC9, 0x17, 0x64, 0x9B, 0x27, 0xC5, 0x20,
52 * 0x03, 0x7B, 0x4D, 0x7C, 0x4D, 0xE6, 0xEE, 0x64, 0x78, 0xA2, 0xBE, 0x2C, 0x22, 0x0A, 0x8E, 0x37,
56 * const int sampleOutputLen = 20;
57 * static const byte sampleOutput[sampleOutputLen] = {
58 * 0xC4, 0x10, 0xB8, 0x6E, 0xDA, 0x00, 0xE3, 0x2C, 0x8A, 0xC4,
59 * 0xE5, 0xDC, 0xB0, 0xE0, 0xE8, 0x2C, 0x21, 0xB6, 0x4E, 0x73
62 * const int Sha1Len = 20;
64 * result r = E_FAILURE;
65 * IHash * pHash = null;
68 * ByteBuffer *pOutput = null;
70 * input.Construct(messageLen);
71 * input.SetArray(message, 0, messageLen);
74 * pHash = new Sha1Hash();
80 * pOutput = pHash->GetHashN(input);
81 * if (pOutput == null)
83 * r = GetLastResult();
87 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
102 * MyClass::TestHashSample_Multipart(void)
104 * const int messageLen = 49;
105 * static const byte message[messageLen] = {
106 * 0xF4, 0xA4, 0xA2, 0x40, 0xB0, 0xAB, 0x73, 0x1B, 0xC8, 0x10, 0xEA, 0x08, 0x9C, 0xD0, 0x78, 0x0D,
107 * 0x40, 0xB9, 0x94, 0x02, 0x21, 0x79, 0xFD, 0x5A, 0xA3, 0xC9, 0x17, 0x64, 0x9B, 0x27, 0xC5, 0x20,
108 * 0x03, 0x7B, 0x4D, 0x7C, 0x4D, 0xE6, 0xEE, 0x64, 0x78, 0xA2, 0xBE, 0x2C, 0x22, 0x0A, 0x8E, 0x37,
112 * const int sampleOutputLen = 20;
113 * static const byte sampleOutput[sampleOutputLen] = {
114 * 0xC4, 0x10, 0xB8, 0x6E, 0xDA, 0x00, 0xE3, 0x2C, 0x8A, 0xC4,
115 * 0xE5, 0xDC, 0xB0, 0xE0, 0xE8, 0x2C, 0x21, 0xB6, 0x4E, 0x73
118 * const int Sha1Len = 20;
119 * int unitLen = messageLen / 5;
122 * result r = E_FAILURE;
123 * IHash * pHash = null;
126 * ByteBuffer *pOutput = null;
128 * input.Construct(messageLen);
129 * input.SetArray(message, 0, messageLen);
132 * pHash = new Sha1Hash();
138 * r = pHash->Initialize();
139 * if (r != E_SUCCESS)
144 * for (int i = 0; i * unitLen < messageLen; i++)
146 * if (messageLen - (i * unitLen) < unitLen)
148 * dataLen = messageLen - (i * unitLen);
155 * // messageLen == 98
156 * input.Construct(dataLen);
157 * input.SetArray(message + (i * unitLen), 0, dataLen);
160 * r = pHash->Update(input);
161 * if (r != E_SUCCESS)
169 * pOutput = pHash->FinalizeN();
170 * if (pOutput == null)
172 * r = GetLastResult();
176 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
193 class _OSP_EXPORT_ IHash
198 * This polymorphic destructor should be overridden if required. This way, the destructors of the derived classes @n
199 * are called when the destructor of this interface is called.
203 virtual ~IHash(void) {}
206 * Sets the hash algorithm. @n
207 * Only supported in Secure Hash Algorithm-2 (SHA-2).
211 * @return An error code
212 * @param[in] algorithm The name of the hash algorithm @n
213 * For example, "SHA2/224", "SHA2/256", "SHA2/384", or "SHA2/512".
214 * @exception E_SUCCESS The method is successful.
215 * @exception E_UNSUPPORTED_ALGORITHM The algorithm is not supported.
217 virtual result SetAlgorithm(const Tizen::Base::String& algorithm) = 0;
220 * Gets the hashes of the data (single-part).
224 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
225 * else @c null if an error occurs
226 * @param[in] input An instance of Tizen::Base::ByteBuffer
227 * @exception E_SUCCESS The method is successful.
228 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
229 * @exception E_OUT_OF_MEMORY The memory is insufficient.
230 * @exception E_SYSTEM A system error has occurred. @n
231 * The method has failed to operate with the openssl library, or
232 * the Tizen::Base::ByteBuffer operation has failed.
234 virtual Tizen::Base::ByteBuffer* GetHashN(const Tizen::Base::ByteBuffer& input) const = 0;
237 * Initializes a multiple-part hash operation.
241 * @return An error code
242 * @exception E_SUCCESS The method is successful.
243 * @exception E_OUT_OF_MEMORY The memory is insufficient.
244 * @exception E_SYSTEM A system error has occurred. @n
245 * The method has failed to operate with the openssl library.
247 virtual result Initialize(void) = 0;
250 * Updates a multiple-part hash operation while processing another data part.
254 * @return An error code
255 * @param[in] input An instance of Tizen::Base::ByteBuffer
256 * @exception E_SUCCESS The method is successful.
257 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
258 * @exception E_OUT_OF_MEMORY The memory is insufficient.
259 * @exception E_SYSTEM A system error has occurred. @n
260 * The method has failed to operate with the openssl library, or
261 * the Tizen::Base::ByteBuffer operation has failed.
263 virtual result Update(const Tizen::Base::ByteBuffer& input) = 0;
266 * Finalizes a multiple-part hash operation.
270 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
271 * else @c null if an error occurs
272 * @exception E_SUCCESS The method is successful.
273 * @exception E_OUT_OF_MEMORY The memory is insufficient.
274 * @exception E_SYSTEM A system error has occurred. @n
275 * The method has failed to operate with the openssl library, or
276 * the Tizen::Base::ByteBuffer operation has failed.
278 virtual Tizen::Base::ByteBuffer* FinalizeN(void) = 0;
282 // This method is for internal use only. Using this method can cause behavioral, security-related,
283 // and consistency-related issues in the application.
285 // This method is reserved and may change its name at any time without prior notice.
289 virtual void IHash_Reserved1(void) {}
293 } } } //Tizen::Security::Crypto
295 #endif //_FSEC_CRYPTO_IHASH_H_