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 FSecCryptoIHmac.h
20 * @brief This is the header file for the %IHmac interface.
22 * This header file contains the declarations of the %IHmac interface.
24 #ifndef _FSEC_CRYPTO_IHMAC_H_
25 #define _FSEC_CRYPTO_IHMAC_H_
27 #include <FBaseString.h>
28 #include <FBaseByteBuffer.h>
29 #include <FSecISecretKey.h>
32 namespace Tizen { namespace Security { namespace Crypto
37 * @brief This interface provides the functionality of a Hash Message Authentication Code (HMAC) algorithm.
41 * The %IHmac interface provides the functionality of a Hash Message Authentication Code (HMAC) algorithm. @n
43 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/hashing.htm">Hashing</a>. @n
45 * The following example demonstrates how to use the %IHmac interface.
50 * MyClass::TestHmacSample(void)
52 * const int messageLen = 64;
53 * static const byte message[messageLen] = {
54 * 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
55 * 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
56 * 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
57 * 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60,
58 * 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
59 * 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
60 * 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
61 * 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80
64 * const int macKeyLen = 20;
65 * static const byte macKey[macKeyLen] = {
66 * 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
67 * 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
68 * 0x20, 0x21, 0x22, 0x23
71 * const int sampleOutputLen = 20;
72 * static const byte sampleOutput[sampleOutputLen] = {
73 * 0x0f, 0xf4, 0xd8, 0x25, 0x33, 0xe5, 0xd8, 0x22,
74 * 0x70, 0x8c, 0x8f, 0x76, 0xda, 0x9e, 0x6c, 0xaf,
75 * 0x71, 0xea, 0x1a, 0x5b
78 * const int Sha1Len = 20;
80 * result r = E_FAILURE;
81 * IHmac *pHmac = null;
83 * ByteBuffer *pOutput = null;
84 * ByteBuffer keyBytes;
85 * SecretKeyGenerator *pKeyGen = null;
86 * ISecretKey *pKey = null;
88 * input.Construct(messageLen);
89 * input.SetArray(message, 0, messageLen);
92 * keyBytes.Construct(macKeyLen);
93 * keyBytes.SetArray(macKey, 0, macKeyLen);
97 * pHmac = new Sha1Hmac();
103 * // Generates the key.
104 * pKeyGen = new SecretKeyGenerator();
105 * if (pKeyGen == null)
110 * r = pKeyGen->Construct(keyBytes);
111 * if (r != E_SUCCESS)
116 * pKey = pKeyGen->GenerateKeyN();
122 * r = pHmac->SetKey(*pKey);
123 * if (r != E_SUCCESS)
128 * pOutput = pHmac->GetHmacN(input);
129 * if (pOutput == null)
131 * r = GetLastResult();
135 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
151 * MyClass::TestHmacSample_Multipart(void)
153 * const int messageLen = 64;
154 * static const byte message[messageLen] = {
155 * 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
156 * 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
157 * 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
158 * 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60,
159 * 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
160 * 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
161 * 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
162 * 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80
165 * // This contains the MAC key to be used.
166 * const int macKeyLen = 20;
167 * static const byte macKey[macKeyLen] = {
168 * 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
169 * 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
170 * 0x20, 0x21, 0x22, 0x23
173 * const int sampleOutputLen = 20;
174 * static const byte sampleOutput[sampleOutputLen] = {
175 * 0x0f, 0xf4, 0xd8, 0x25, 0x33, 0xe5, 0xd8, 0x22,
176 * 0x70, 0x8c, 0x8f, 0x76, 0xda, 0x9e, 0x6c, 0xaf,
177 * 0x71, 0xea, 0x1a, 0x5b
180 * const int Sha1Len = 20;
181 * int unitLen = messageLen / 5;
184 * result r = E_FAILURE;
185 * IHmac * pHmac = null;
187 * ByteBuffer *pOutput = null;
188 * ByteBuffer keyBytes;
189 * SecretKeyGenerator* pKeyGen = null;
190 * ISecretKey *pKey = null;
192 * input.Construct(messageLen);
193 * input.SetArray(message, 0, messageLen);
196 * keyBytes.Construct(macKeyLen);
197 * keyBytes.SetArray(macKey, 0, macKeyLen);
201 * pHmac = new Sha1Hmac();
207 * // Generates the key.
208 * pKeyGen = new SecretKeyGenerator();
209 * if (pKeyGen == null)
214 * r = pKeyGen->Construct(keyBytes);
215 * if (r != E_SUCCESS)
220 * pKey = pKeyGen->GenerateKeyN();
226 * r = pHmac->SetKey(*pKey);
227 * if (r != E_SUCCESS)
232 * r = pHmac->Initialize();
233 * if (r != E_SUCCESS)
238 * for (int i = 0; i * unitLen < messageLen; i++)
240 * if (messageLen - (i * unitLen) < unitLen)
242 * dataLen = messageLen - (i * unitLen);
249 * // MessageLen == 98
250 * input.Construct(dataLen);
251 * input.SetArray(message + (i * unitLen), 0, dataLen);
254 * r = pHmac->Update(input);
255 * if (r != E_SUCCESS)
263 * pOutput = pHmac->FinalizeN();
264 * if (pOutput == null)
266 * r = GetLastResult();
270 * if (memcmp(pOutput->GetPointer(), sampleOutput, Sha1Len) != 0)
288 class _OSP_EXPORT_ IHmac
293 * This polymorphic destructor should be overridden if required. This way, the destructors of the derived classes @n
294 * are called when the destructor of this interface is called.
298 virtual ~IHmac(void) {}
301 * Sets the HMAC algorithm.
305 * @return An error code
306 * @param[in] algorithm The name of the HMAC algorithm @n
307 * For example, "HMACSHA2/224", "HMACSHA2/256", "HMACSHA2/384", or "HMACSHA2/512".
308 * @exception E_SUCCESS The method is successful.
309 * @exception E_UNSUPPORTED_ALGORITHM The algorithm is not supported.
311 virtual result SetAlgorithm(const Tizen::Base::String& algorithm) = 0;
314 * Sets the secret key.
318 * @return An error code
319 * @param[in] key An instance of ISecretKey
320 * @exception E_SUCCESS The method is successful.
321 * @exception E_INVALID_ARG The specified key is invalid.
322 * @exception E_OUT_OF_MEMORY The memory is insufficient.
324 virtual result SetKey(const Tizen::Security::ISecretKey& key) = 0;
327 * Gets the HMAC with the given input.
331 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
332 * else @c null if an error occurs
333 * @param[in] input An instance of Tizen::Base::ByteBuffer
334 * @exception E_SUCCESS The method is successful.
335 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
336 * @exception E_OUT_OF_MEMORY The memory is insufficient.
337 * @exception E_KEY_NOT_FOUND The key is not found.
338 * @exception E_SYSTEM A system error has occurred. @n
339 * The method has failed to operate with the openssl library, or
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 A system error has occurred. @n
354 * The method has failed to operate with the openssl library.
356 virtual result Initialize(void) = 0;
359 * Updates a multiple-part HMAC operation while processing another data part.
363 * @return An error code
364 * @param[in] input An instance of Tizen::Base::ByteBuffer
365 * @exception E_SUCCESS The method is successful.
366 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
367 * @exception E_OUT_OF_MEMORY The memory is insufficient.
368 * @exception E_SYSTEM A system error has occurred. @n
369 * The method has failed to operate with the openssl library, or
370 * the Tizen::Base::ByteBuffer operation has failed.
372 virtual result Update(const Tizen::Base::ByteBuffer& input) = 0;
375 * Finalizes a multiple-part HMAC operation.
379 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
380 * else @c null if an error occurs
381 * @exception E_SUCCESS The method is successful.
382 * @exception E_OUT_OF_MEMORY The memory is insufficient.
383 * @exception E_SYSTEM A system error has occurred. @n
384 * The method has failed to operate with the openssl library, or
385 * the Tizen::Base::ByteBuffer operation has failed.
387 virtual Tizen::Base::ByteBuffer* FinalizeN(void) = 0;
391 // This method is for internal use only. Using this method can cause behavioral, security-related,
392 // and consistency-related issues in the application.
394 // This method is reserved and may change its name at any time without prior notice.
398 virtual void IHmac_Reserved1(void) {}
402 } } } //Tizen::Security::Crypto
404 #endif //_FSEC_CRYPTO_IHMAC_H_