Topic: HMAC MD5

I just load wolfSSL source code for the first time. Trying to use HMAC with MD5, in the Windows Visual Studio 2017 environment.

Found example for HMAC SHA256 on this page
https://www.wolfssl.com/docs/wolfssl-manual/ch10/

Modifyed to MD5

#include "wolfssl/wolfcrypt/hmac.h"

void printHex(const char* label, Uint8* data, Uint32 size)
{
    printf("%s", label);
    for (Uint8 i = 0; i < size; i++)
        printf("%c%02x", i % 16 ? ' ' : '\n', data[i]);
    printf("\n");
}

int main(void)
{
    Uint8    key[MD4_DIGEST_SIZE];
    wchar_t    tmp[256] = L"Test string";

    Uint8    digest[MD5_DIGEST_SIZE * 5];
    Hmac     hmac;

    memset(key, 0x33, sizeof(key));
    memset(digest, 0xAA, sizeof(digest));

    printHex("digest 0 ", digest, sizeof(digest));

    wc_HmacSetKey(&hmac, MD5, key, sizeof(key));
    wc_HmacUpdate(&hmac, (const byte*)tmp, wcslen(tmp) * 2);
    wc_HmacFinal(&hmac, digest + MD5_DIGEST_SIZE * 2);

    printHex("digest 1", digest, sizeof(digest));

    return 0;
}

Can you please give me a hint what i'm doing wrong, why does wc_HmacSetKey corrupting 'digest' variable? See bottom line in output bellow.

digest 0
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
digest 1
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
1b 20 7b b8 31 dd 2e 4e e3 bc 4a f2 83 94 ad 61
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
6f 6f 6f 6f 6f 6f 6f 6f 6f 6f 6f 6f 6f 6f 6f 6f

Share

Re: HMAC MD5

Hi Addax,

Can you tell us about your project and what it is you are working on!

Thanks for using the wolfSSL forums to seek an answer. Often times if the application does not have all the same settings as the wolfSSL library this can result in undefined behavior. Could you send us the contents of the user_settings.h and any C Pre Processor macros you have set in your project that are wolfSSL specific?

If you are not using a user_settings.h and you do NOT have WOLFSSL_USER_SETTINGS set in your project please try adding that define to your Preprocessor flags and then make sure the path to wolfssl-x.xx.x/IDE/WIN is in your project include path so that wolfssl-x.xx.x/IDE/WIN/user_settings.h gets pulled in!

Warm Regards,

K

Re: HMAC MD5

Hi Kaleb,

Thank you for the fast reply.

Can you tell us about your project and what it is you are working on!

I'm just experimenting and need few simple crypto functions: RC4, MD4, MD5, SHA256, HMAC. Maybe AES later.

Here is a steps what I did.

  • Download wolfSSL from Git and unzip it to C:\Projects\wolfssl

  • Open wolfssl64.sln solution and press F7 to build it. Checked that compilation succesful. Close solution.

  • Create new empty C++ solution. Place one file with code I placed in initial post.

  • In project properties:

    • in C/C++ options add C:\Projects\wolfssl into Additional Include Directories

    • in Linker options add C:\Projects\wolfssl\Debug into Additional Library Directories

    • in Linker options add wolfssl.lib into Additional Dependencies

Thanks for using the wolfSSL forums to seek an answer. Often times if the application does not have all the same settings as the wolfSSL library this can result in undefined behavior. Could you send us the contents of the user_settings.h and any C Pre Processor macros you have set in your project that are wolfSSL specific?

I have not touch user_settings.h nither add anything to C++ Preprocessor Definitions.
C++ Preprocessor Definitions has only: _MBCS

If you are not using a user_settings.h and you do NOT have WOLFSSL_USER_SETTINGS set in your project please try adding that define to your Preprocessor flags and then make sure the path to wolfssl-x.xx.x/IDE/WIN is in your project include path so that wolfssl-x.xx.x/IDE/WIN/user_settings.h gets pulled in!

I did those two more modifications which you have suggested.

  • add WOLFSSL_USER_SETTINGS to C++ Preprocessor Definitions.
    Resulting value: WOLFSSL_USER_SETTINGS;_MBCS

  • add C:\Projects\wolfssl\IDE\WIN into Additional Include Directories
    Resulting value: C:\Projects\wolfssl\;C:\Projects\wolfssl\IDE\WIN\

Above changes did not help:

digest 0
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
digest 1
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
1b 20 7b b8 31 dd 2e 4e e3 bc 4a f2 83 94 ad 61
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
f8 a8 c0 1c 39 4c e1 96 ea de 12 bb 9f 53 84 0e

I also found that the "key" value passed to wc_HmacSetKey also becomes corrupted.

I did not notice any problems with MD4, MD5 and RC4.

Thanks

Share

Re: HMAC MD5

Addax:

Were you able to solve the issue you were having? Something that was never clear was which version of wolfSSL are you testing with? I'm running your code locally (without the wide character strings) and I'm not getting any data corruption.

--John