Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

Hi,

 

I have used IHash in my project to encrypt the data using HMAC-SHA1 format.

 

IHash *pIHash = NULL;            const byte  *key = NULL;            byte   *body = NULL, *resultval;            int res = 0, len = 0;            //key = key & (0x00 * (64 - SZ_LEN(key)));            ISHELL_CreateInstance(pMe->m_pIShell, AEECLSID_HMAC_SHA1, (void **)&pIHash);            key = (byte *)MALLOC(100 * sizeof(byte));            body = (byte *)MALLOC(256 * sizeof(byte));            resultval = (byte *)MALLOC(256 * sizeof(byte));            SZ_CPY((byte *)key, "anonymous");            res =IHASH_SetKey(pIHash, key, sizeof(key));            SZ_CPY((byte *)body, "a");            IHASH_Update(pIHash, body, sizeof(body));            res =IHASH_GetResult(pIHash, resultval, &len);            res =IHASH_GetResult(pIHash,  resultval, &len);            res =IHASH_GetResult(pIHash,  resultval, &len);

 

This is my code. I'm getting AEE_HASH_MORE_DATA return value for IHASH_GetResult for the first time. From the second time i'm getting SUCCESS as return key but some junk characters in "resultval" buffer like "ºB"øFÍKû¨áöÝœSÓ÷

 

Dont know what wrong i'm getting?help me out of this.

 

Thanks

Jithu       

return value of AEE_HASH_MORE_DATA means your buffer size passed is less than what is required for the output & the variable len is filled with actual length required. hence the call returns SUCCESS in second time. if the actual buffer size is less than len, the program may crash at a later point as there may be overwritten memory.
 
you are using HMAC mode for SHA1 & the output for SHA1 is 160 bits(20 bytes). you output also seems to be same length.
 
The output you are getting may be the correct output. What do u want to achieve.
read through this to understand about HMAC:-
http://en.wikipedia.org/wiki/HMAC

return value of AEE_HASH_MORE_DATA means your buffer size passed is less than what is required for the output & the variable len is filled with actual length required. hence the call returns SUCCESS in second time. if the actual buffer size is less than len, the program may crash at a later point as there may be overwritten memory.
 
you are using HMAC mode for SHA1 & the output for SHA1 is 160 bits(20 bytes). you output also seems to be same length.
 
The output you are getting may be the correct output. What do u want to achieve.
read through this to understand about HMAC:-
http://en.wikipedia.org/wiki/HMAC

Hi Shivendra,
 
I have allocated enough memory for resultvalue buffer. U can see that there. I'm getting output length as 20. But prior to that i'm allocating 256 buffer. I think this should be fine. Even i tried to allocate 30000 buffer and i tried. But i'm getting same error for the first time.
 
But from second time i'm getting junk output like ºB"øFÍKû¨áöÝœSÓ÷
 
I dont know why this difference is coming. Please clarify me.
 
Thanks
Jithendra

Hi Shivendra,
 
I have allocated enough memory for resultvalue buffer. U can see that there. I'm getting output length as 20. But prior to that i'm allocating 256 buffer. I think this should be fine. Even i tried to allocate 30000 buffer and i tried. But i'm getting same error for the first time.
 
But from second time i'm getting junk output like ºB"øFÍKû¨áöÝœSÓ÷
 
I dont know why this difference is coming. Please clarify me.
 
Thanks
Jithendra

1. Try to read the documentation carefully to understand the use of the parameters being passed to IHASH_GetResult(pHash, buf, &len).
len - in/out :  On input this should contain the length of the buff & on output it contains the required length for the output to be stored.
So as in your case, if the value of len is 0 on input, IHASH_GetResult() will consider your buffer is 0 length & returns the required length 20 in the variable len & hece on second call it is a SUCCESS.
 
2. the online convertor is giving you the value in hexadecemal.
When you try to see the value returned in buffer from IHASH_GetResult(), you see the ascii chars, hence it looks like a garbage to u.
try printing it using DBGPRINTF("%x", buff[0]); DBGPRINTF("%x", buff[1]); & so on to print & see the hex equavalant of all the values.
 

1. Try to read the documentation carefully to understand the use of the parameters being passed to IHASH_GetResult(pHash, buf, &len).
len - in/out :  On input this should contain the length of the buff & on output it contains the required length for the output to be stored.
So as in your case, if the value of len is 0 on input, IHASH_GetResult() will consider your buffer is 0 length & returns the required length 20 in the variable len & hece on second call it is a SUCCESS.
 
2. the online convertor is giving you the value in hexadecemal.
When you try to see the value returned in buffer from IHASH_GetResult(), you see the ascii chars, hence it looks like a garbage to u.
try printing it using DBGPRINTF("%x", buff[0]); DBGPRINTF("%x", buff[1]); & so on to print & see the hex equavalant of all the values.