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 FSecCryptoIHmac.h
19 * @brief This is the header file for the %IHmac interface.
21 * This header file contains the declarations of the %IHmac interface.
23 #ifndef _FSEC_CRYPTO_IHMAC_H_
24 #define _FSEC_CRYPTO_IHMAC_H_
26 #include <FBaseString.h>
27 #include <FBaseByteBuffer.h>
28 #include <FSecISecretKey.h>
31 namespace Tizen { namespace Security { namespace Crypto
36 * @brief This interface provides the functionality of a Hash Message Authentication Code (HMAC) algorithm.
40 * The %IHmac interface provides the functionality of a Hash Message Authentication Code (HMAC) algorithm. @n
42 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/hashing.htm">Hashing</a>. @n
44 * The following example demonstrates how to use the %IHmac interface.
49 * MyClass::TestHmacSample(void)
51 * const int messageLen = 64;
52 * static const byte message[messageLen] = {
53 * 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
54 * 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
55 * 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
56 * 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60,
57 * 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
58 * 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
59 * 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
60 * 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80
63 * const int macKeyLen = 20;
64 * static const byte macKey[macKeyLen] = {
65 * 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
66 * 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
67 * 0x20, 0x21, 0x22, 0x23
70 * const int sampleOutputLen = 20;
71 * static const byte sampleOutput[sampleOutputLen] = {
72 * 0x0f, 0xf4, 0xd8, 0x25, 0x33, 0xe5, 0xd8, 0x22,
73 * 0x70, 0x8c, 0x8f, 0x76, 0xda, 0x9e, 0x6c, 0xaf,
74 * 0x71, 0xea, 0x1a, 0x5b
77 * const int Sha1Len = 20;
79 * result r = E_FAILURE;
80 * IHmac *pHmac = null;
82 * ByteBuffer *pOutput = null;
83 * ByteBuffer keyBytes;
84 * SecretKeyGenerator *pKeyGen = null;
85 * ISecretKey *pKey = null;
87 * input.Construct(messageLen);
88 * input.SetArray(message, 0, messageLen);
91 * keyBytes.Construct(macKeyLen);
92 * keyBytes.SetArray(macKey, 0, macKeyLen);
96 * pHmac = new Sha1Hmac();
102 * // Generates the key.
103 * pKeyGen = new SecretKeyGenerator();
104 * if (pKeyGen == null)
109 * r = pKeyGen->Construct(keyBytes);
110 * if (r != E_SUCCESS)
115 * pKey = pKeyGen->GenerateKeyN();
121 * r = pHmac->SetKey(*pKey);
122 * if (r != E_SUCCESS)
127 * pOutput = pHmac->GetHmacN(input);
128 * if (pOutput == null)
130 * r = GetLastResult();
134 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
150 * MyClass::TestHmacSample_Multipart(void)
152 * const int messageLen = 64;
153 * static const byte message[messageLen] = {
154 * 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
155 * 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
156 * 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
157 * 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60,
158 * 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
159 * 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
160 * 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
161 * 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80
164 * // This contains the MAC key to be used.
165 * const int macKeyLen = 20;
166 * static const byte macKey[macKeyLen] = {
167 * 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
168 * 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
169 * 0x20, 0x21, 0x22, 0x23
172 * const int sampleOutputLen = 20;
173 * static const byte sampleOutput[sampleOutputLen] = {
174 * 0x0f, 0xf4, 0xd8, 0x25, 0x33, 0xe5, 0xd8, 0x22,
175 * 0x70, 0x8c, 0x8f, 0x76, 0xda, 0x9e, 0x6c, 0xaf,
176 * 0x71, 0xea, 0x1a, 0x5b
179 * const int Sha1Len = 20;
180 * int unitLen = messageLen / 5;
183 * result r = E_FAILURE;
184 * IHmac * pHmac = null;
186 * ByteBuffer *pOutput = null;
187 * ByteBuffer keyBytes;
188 * SecretKeyGenerator* pKeyGen = null;
189 * ISecretKey *pKey = null;
191 * input.Construct(messageLen);
192 * input.SetArray(message, 0, messageLen);
195 * keyBytes.Construct(macKeyLen);
196 * keyBytes.SetArray(macKey, 0, macKeyLen);
200 * pHmac = new Sha1Hmac();
206 * // Generates the key.
207 * pKeyGen = new SecretKeyGenerator();
208 * if (pKeyGen == null)
213 * r = pKeyGen->Construct(keyBytes);
214 * if (r != E_SUCCESS)
219 * pKey = pKeyGen->GenerateKeyN();
225 * r = pHmac->SetKey(*pKey);
226 * if (r != E_SUCCESS)
231 * r = pHmac->Initialize();
232 * if (r != E_SUCCESS)
237 * for (int i = 0; i * unitLen < messageLen; i++)
239 * if (messageLen - (i * unitLen) < unitLen)
241 * dataLen = messageLen - (i * unitLen);
248 * // MessageLen == 98
249 * input.Construct(dataLen);
250 * input.SetArray(message + (i * unitLen), 0, dataLen);
253 * r = pHmac->Update(input);
254 * if (r != E_SUCCESS)
262 * pOutput = pHmac->FinalizeN();
263 * if (pOutput == null)
265 * r = GetLastResult();
269 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
287 class _OSP_EXPORT_ IHmac
292 * This polymorphic destructor should be overridden if required. @n
293 * This way, the destructors of the derived classes are called when the destructor of this interface is called.
297 virtual ~IHmac(void) {}
300 * Sets the HMAC algorithm.
304 * @return An error code
305 * @param[in] algorithm The HMAC algorithm @n
306 * For example, "HMACSHA2/224", "HMACSHA2/256", "HMACSHA2/384", or "HMACSHA2/512".
307 * @exception E_SUCCESS The method is successful.
308 * @exception E_UNSUPPORTED_ALGORITHM The algorithm is not supported.
310 virtual result SetAlgorithm(const Tizen::Base::String& algorithm) = 0;
313 * Sets the secret key.
317 * @return An error code
318 * @param[in] key An instance of ISecretKey
319 * @exception E_SUCCESS The method is successful.
320 * @exception E_INVALID_ARG The specified key is invalid.
321 * @exception E_OUT_OF_MEMORY The memory is insufficient.
323 virtual result SetKey(const Tizen::Security::ISecretKey& key) = 0;
326 * Gets the HMAC for the specified input.
330 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
331 * else @c null if an error occurs
332 * @param[in] input An instance of Tizen::Base::ByteBuffer
333 * @exception E_SUCCESS The method is successful.
334 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
335 * @exception E_OUT_OF_MEMORY The memory is insufficient.
336 * @exception E_KEY_NOT_FOUND The key is not found.
337 * @exception E_SYSTEM Either of the following conditions has occurred:
338 * - A system error has occurred.
339 * - The method has failed to operate with the openssl library.
340 * - The Tizen::Base::ByteBuffer operation has failed.
342 virtual Tizen::Base::ByteBuffer* GetHmacN(const Tizen::Base::ByteBuffer& input) const = 0;
345 * Initializes the multiple-part HMAC operation.
349 * @return An error code
350 * @exception E_SUCCESS The method is successful.
351 * @exception E_OUT_OF_MEMORY The memory is insufficient.
352 * @exception E_KEY_NOT_FOUND The key is not found.
353 * @exception E_SYSTEM Either of the following conditions has occurred:
354 * - A system error has occurred.
355 * - The method has failed to operate with the openssl library.
357 virtual result Initialize(void) = 0;
360 * Updates the multiple-part HMAC operation while processing another data part.
364 * @return An error code
365 * @param[in] input An instance of Tizen::Base::ByteBuffer
366 * @exception E_SUCCESS The method is successful.
367 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
368 * @exception E_OUT_OF_MEMORY The memory is insufficient.
369 * @exception E_SYSTEM Either of the following conditions has occurred:
370 * - A system error has occurred.
371 * - The method has failed to operate with the openssl library.
372 * - The Tizen::Base::ByteBuffer operation has failed.
374 virtual result Update(const Tizen::Base::ByteBuffer& input) = 0;
377 * Finalizes the multiple-part HMAC operation.
381 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
382 * else @c null if an error occurs
383 * @exception E_SUCCESS The method is successful.
384 * @exception E_OUT_OF_MEMORY The memory is insufficient.
385 * @exception E_SYSTEM Either of the following conditions has occurred:
386 * - A system error has occurred.
387 * - The method has failed to operate with the openssl library.
388 * - The Tizen::Base::ByteBuffer operation has failed.
390 virtual Tizen::Base::ByteBuffer* FinalizeN(void) = 0;
394 // This method is for internal use only. Using this method can cause behavioral, security-related,
395 // and consistency-related issues in the application.
397 // This method is reserved and may change its name at any time without prior notice.
401 virtual void IHmac_Reserved1(void) {}
405 } } } //Tizen::Security::Crypto
407 #endif //_FSEC_CRYPTO_IHMAC_H_