2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FSecCryptoIHash.h
20 * @brief This is the header file for the %IHash interface.
22 * This header file contains the declarations of the %IHash interface.
24 #ifndef _FSEC_CRYPTO_IHASH_H_
25 #define _FSEC_CRYPTO_IHASH_H_
27 #include <FBaseString.h>
28 #include <FBaseByteBuffer.h>
30 namespace Tizen { namespace Security { namespace Crypto
35 * @brief This interface provides the functionality of a hash algorithm.
39 * The %IHash interface provides the functionality of a hash algorithm. @n
41 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/hashing.htm">Hashing</a>. @n
43 * The following example demonstrates how to use the %IHash interface.
47 * MyClass::TestHashSample(void)
49 * const int messageLen = 49;
50 * static const byte message[messageLen] = {
51 * 0xF4, 0xA4, 0xA2, 0x40, 0xB0, 0xAB, 0x73, 0x1B, 0xC8, 0x10, 0xEA, 0x08, 0x9C, 0xD0, 0x78, 0x0D,
52 * 0x40, 0xB9, 0x94, 0x02, 0x21, 0x79, 0xFD, 0x5A, 0xA3, 0xC9, 0x17, 0x64, 0x9B, 0x27, 0xC5, 0x20,
53 * 0x03, 0x7B, 0x4D, 0x7C, 0x4D, 0xE6, 0xEE, 0x64, 0x78, 0xA2, 0xBE, 0x2C, 0x22, 0x0A, 0x8E, 0x37,
57 * const int sampleOutputLen = 20;
58 * static const byte sampleOutput[sampleOutputLen] = {
59 * 0xC4, 0x10, 0xB8, 0x6E, 0xDA, 0x00, 0xE3, 0x2C, 0x8A, 0xC4,
60 * 0xE5, 0xDC, 0xB0, 0xE0, 0xE8, 0x2C, 0x21, 0xB6, 0x4E, 0x73
63 * const int Sha1Len = 20;
65 * result r = E_FAILURE;
66 * IHash * pHash = null;
69 * ByteBuffer *pOutput = null;
71 * input.Construct(messageLen);
72 * input.SetArray(message, 0, messageLen);
75 * pHash = new Sha1Hash();
81 * pOutput = pHash->GetHashN(input);
82 * if (pOutput == null)
84 * r = GetLastResult();
88 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
103 * MyClass::TestHashSample_Multipart(void)
105 * const int messageLen = 49;
106 * static const byte message[messageLen] = {
107 * 0xF4, 0xA4, 0xA2, 0x40, 0xB0, 0xAB, 0x73, 0x1B, 0xC8, 0x10, 0xEA, 0x08, 0x9C, 0xD0, 0x78, 0x0D,
108 * 0x40, 0xB9, 0x94, 0x02, 0x21, 0x79, 0xFD, 0x5A, 0xA3, 0xC9, 0x17, 0x64, 0x9B, 0x27, 0xC5, 0x20,
109 * 0x03, 0x7B, 0x4D, 0x7C, 0x4D, 0xE6, 0xEE, 0x64, 0x78, 0xA2, 0xBE, 0x2C, 0x22, 0x0A, 0x8E, 0x37,
113 * const int sampleOutputLen = 20;
114 * static const byte sampleOutput[sampleOutputLen] = {
115 * 0xC4, 0x10, 0xB8, 0x6E, 0xDA, 0x00, 0xE3, 0x2C, 0x8A, 0xC4,
116 * 0xE5, 0xDC, 0xB0, 0xE0, 0xE8, 0x2C, 0x21, 0xB6, 0x4E, 0x73
119 * const int Sha1Len = 20;
120 * int unitLen = messageLen / 5;
123 * result r = E_FAILURE;
124 * IHash * pHash = null;
127 * ByteBuffer *pOutput = null;
129 * input.Construct(messageLen);
130 * input.SetArray(message, 0, messageLen);
133 * pHash = new Sha1Hash();
139 * r = pHash->Initialize();
140 * if (r != E_SUCCESS)
145 * for (int i = 0; i * unitLen < messageLen; i++)
147 * if (messageLen - (i * unitLen) < unitLen)
149 * dataLen = messageLen - (i * unitLen);
156 * // messageLen == 98
157 * input.Construct(dataLen);
158 * input.SetArray(message + (i * unitLen), 0, dataLen);
161 * r = pHash->Update(input);
162 * if (r != E_SUCCESS)
170 * pOutput = pHash->FinalizeN();
171 * if (pOutput == null)
173 * r = GetLastResult();
177 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
194 class _OSP_EXPORT_ IHash
199 * This polymorphic destructor should be overridden if required. This way, the destructors of the derived classes @n
200 * are called when the destructor of this interface is called.
204 virtual ~IHash(void) {}
207 * Sets the hash algorithm. @n
208 * Only supported in Secure Hash Algorithm-2 (SHA-2).
212 * @return An error code
213 * @param[in] algorithm The name of the hash algorithm @n
214 * For example, "SHA2/224", "SHA2/256", "SHA2/384", or "SHA2/512".
215 * @exception E_SUCCESS The method is successful.
216 * @exception E_UNSUPPORTED_ALGORITHM The algorithm is not supported.
218 virtual result SetAlgorithm(const Tizen::Base::String& algorithm) = 0;
221 * Gets the hashes of the data (single-part).
225 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
226 * else @c null if an error occurs
227 * @param[in] input An instance of Tizen::Base::ByteBuffer
228 * @exception E_SUCCESS The method is successful.
229 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
230 * @exception E_OUT_OF_MEMORY The memory is insufficient.
231 * @exception E_SYSTEM A system error has occurred. @n
232 * The method has failed to operate with the openssl library, or
233 * the Tizen::Base::ByteBuffer operation has failed.
235 virtual Tizen::Base::ByteBuffer* GetHashN(const Tizen::Base::ByteBuffer& input) const = 0;
238 * Initializes a multiple-part hash operation.
242 * @return An error code
243 * @exception E_SUCCESS The method is successful.
244 * @exception E_OUT_OF_MEMORY The memory is insufficient.
245 * @exception E_SYSTEM A system error has occurred. @n
246 * The method has failed to operate with the openssl library.
248 virtual result Initialize(void) = 0;
251 * Updates a multiple-part hash operation while processing another data part.
255 * @return An error code
256 * @param[in] input An instance of Tizen::Base::ByteBuffer
257 * @exception E_SUCCESS The method is successful.
258 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
259 * @exception E_OUT_OF_MEMORY The memory is insufficient.
260 * @exception E_SYSTEM A system error has occurred. @n
261 * The method has failed to operate with the openssl library, or
262 * the Tizen::Base::ByteBuffer operation has failed.
264 virtual result Update(const Tizen::Base::ByteBuffer& input) = 0;
267 * Finalizes a multiple-part hash operation.
271 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
272 * else @c null if an error occurs
273 * @exception E_SUCCESS The method is successful.
274 * @exception E_OUT_OF_MEMORY The memory is insufficient.
275 * @exception E_SYSTEM A system error has occurred. @n
276 * The method has failed to operate with the openssl library, or
277 * the Tizen::Base::ByteBuffer operation has failed.
279 virtual Tizen::Base::ByteBuffer* FinalizeN(void) = 0;
283 // This method is for internal use only. Using this method can cause behavioral, security-related,
284 // and consistency-related issues in the application.
286 // This method is reserved and may change its name at any time without prior notice.
290 virtual void IHash_Reserved1(void) {}
294 } } } //Tizen::Security::Crypto
296 #endif //_FSEC_CRYPTO_IHASH_H_