humblebeast
humblebeast

Reputation: 303

Tracing Mhash Files (Hashing)

I am trying to trace how this open source program, mhash computes it's hashing

I can run the program successfully by using using the following commands:

gcc -o example example.c -lmhash

(also, mhash is currently installed, and I am running Ubuntu Linux)

Mhash can be found here: http://mhash.sourceforge.net/

and the example that I have tried is here:

#include <mhash.h>
#include <stdio.h>

int main()
{
    char password[] = "Jefe";
    int keylen = 4;
    char data[] = "what do ya want for nothing?";
    int datalen = 28;
    MHASH td;
    unsigned char *mac;
    int j;

    td = mhash_hmac_init(MHASH_MD5, password, keylen,
                            mhash_get_hash_pblock(MHASH_MD5));
    mhash(td, data, datalen);
    mac = mhash_hmac_end(td);
    /* 
     * The output should be 0x750c783e6ab0b503eaa86e310a5db738
     * according to RFC 2104.
     */
    printf("0x");
    for (j = 0; j < mhash_get_block_size(MHASH_MD5); j++) {
        printf("%.2x", mac[j]);
    }
    printf("\n");

    exit(0);
}

I have read the API's, it has very well documentations, but there are soo many files, I do not know from which areas it inherits it's algorithms from?

Thanks for your time and help in advance

Upvotes: 1

Views: 175

Answers (1)

dragosht
dragosht

Reputation: 3275

Your question seems a little vague to me ... I'm not sure I fully understand it. I'll adventure myself into an answer though.

If you simply don't know what gets executed for crunching that MD5 hash the easiest way to get into it is probably to attach yourself with a debugger on this example program of yours. Make sure you have the debug flags enabled on your mhash library (which seem to be on by default), then step in mhash and see where that gets you. You cannot miss anything this way.

In gdb it would look something like this (You'd probably want to use an IDE - eclipse perhaps, to make it a LOT prettier):

$ gdb ./test.exe
..
Reading symbols from /home/B41655/workspace/ctest/test.exe...done.
(gdb) break main
Breakpoint 1 at 0x4011af: file test.c, line 5.
(gdb) run
Starting program: /home/B41655/workspace/ctest/test.exe
[New Thread 10200.0x205c]
[New Thread 10200.0x27b0]

Breakpoint 1, main () at test.c:5
5               char password[] = "Jefe";
(gdb) s
6               int keylen = 4;
(gdb) s
7               char data[] = "what do ya want for nothing?";
(gdb) s
8               int datalen = 28;
(gdb) s
13              td = mhash_hmac_init(MHASH_MD5, password, keylen,
(gdb) s
mhash_get_hash_pblock (type=MHASH_MD5) at mhash.c:438
438     {
(gdb) s
441             MHASH_ALG_LOOP(ret = p->hash_pblock);

and so on ...

If by any chance you want to passively get some sort a call graph of your example program execution you could do that with a profiler. Using gprof on this program would issue something like this (this would require your library/program recompiled with -pg flag):

index % time    self  children    called     name
                0.00    0.00      17/17          main [81]
 [2]     0.0    0.00    0.00      17         mhash_get_block_size [2]
-----------------------------------------------
                0.00    0.00       1/9           mhash [14]
                0.00    0.00       2/9           mhash_hmac_deinit [17]
                0.00    0.00       2/9           mhash_hmac_init [20]
                0.00    0.00       2/9           MD5Update [9]
                0.00    0.00       2/9           MD5Final [10]
[3]      0.0    0.00    0.00       9         mutils_memcpy [3]
-----------------------------------------------
                0.00    0.00       1/6           mhash_deinit [15]
                0.00    0.00       1/6           mhash_hmac_init [20]
                0.00    0.00       2/6           mhash_hmac_deinit [17]
                0.00    0.00       2/6           MD5Final [10]
[4]      0.0    0.00    0.00       6         mutils_bzero [4]
-----------------------------------------------
                0.00    0.00       1/6           mhash_hmac_end_m [19]
                0.00    0.00       1/6           mhash_hmac_init [20]
                0.00    0.00       4/6           mhash_init_int [12]
[5]      0.0    0.00    0.00       6         mutils_malloc [5]
-----------------------------------------------
                0.00    0.00       2/6           MD5Update [9]
                0.00    0.00       4/6           MD5Final [10]
[6]      0.0    0.00    0.00       6         mutils_word32nswap [6]
-----------------------------------------------
                0.00    0.00       1/5           mhash_deinit [15]
                0.00    0.00       4/5           mhash_hmac_deinit [17]
[7]      0.0    0.00    0.00       5         mutils_free [7]
-----------------------------------------------
                0.00    0.00       2/4           MD5Update [9]
                0.00    0.00       2/4           MD5Final [10]
[8]      0.0    0.00    0.00       4         MD5Transform [8]
-----------------------------------------------
                0.00    0.00       1/4           mhash [14]
                0.00    0.00       1/4           mhash_hmac_init [20]
                0.00    0.00       2/4           mhash_hmac_deinit [17]
[9]      0.0    0.00    0.00       4         MD5Update [9]
                0.00    0.00       2/9           mutils_memcpy [3]
                0.00    0.00       2/6           mutils_word32nswap [6]
                0.00    0.00       2/4           MD5Transform [8]
-----------------------------------------------
                0.00    0.00       1/2           mhash_deinit [15]
                0.00    0.00       1/2           mhash_hmac_deinit [17]
[10]     0.0    0.00    0.00       2         MD5Final [10]
                0.00    0.00       4/6           mutils_word32nswap [6]
                0.00    0.00       2/4           MD5Transform [8]
                0.00    0.00       2/9           mutils_memcpy [3]
                0.00    0.00       2/6           mutils_bzero [4]
-----------------------------------------------
                0.00    0.00       2/2           mhash_init_int [12]
[11]     0.0    0.00    0.00       2         MD5Init [11]
-----------------------------------------------
                0.00    0.00       1/2           mhash_hmac_deinit [17]
                0.00    0.00       1/2           mhash_hmac_init [20]
[12]     0.0    0.00    0.00       2         mhash_init_int [12]
                0.00    0.00       4/6           mutils_malloc [5]
                0.00    0.00       2/2           mutils_memset [13]
                0.00    0.00       2/2           MD5Init [11]
-----------------------------------------------
                0.00    0.00       2/2           mhash_init_int [12]
[13]     0.0    0.00    0.00       2         mutils_memset [13]
-----------------------------------------------
                0.00    0.00       1/1           main [81]
[14]     0.0    0.00    0.00       1         mhash [14]
                0.00    0.00       1/9           mutils_memcpy [3]
                0.00    0.00       1/4           MD5Update [9]
-----------------------------------------------
                0.00    0.00       1/1           mhash_hmac_deinit [17]
[15]     0.0    0.00    0.00       1         mhash_deinit [15]
                0.00    0.00       1/6           mutils_bzero [4]
                0.00    0.00       1/2           MD5Final [10]
                0.00    0.00       1/5           mutils_free [7]
-----------------------------------------------
                0.00    0.00       1/1           main [81]
[16]     0.0    0.00    0.00       1         mhash_get_hash_pblock [16]
-----------------------------------------------
                0.00    0.00       1/1           mhash_hmac_end_m [19]
[17]     0.0    0.00    0.00       1         mhash_hmac_deinit [17]
                0.00    0.00       4/5           mutils_free [7]
                0.00    0.00       2/9           mutils_memcpy [3]
                0.00    0.00       2/4           MD5Update [9]
                0.00    0.00       2/6           mutils_bzero [4]
                0.00    0.00       1/2           mhash_init_int [12]
                0.00    0.00       1/2           MD5Final [10]
                0.00    0.00       1/1           mhash_deinit [15]
-----------------------------------------------
                0.00    0.00       1/1           main [81]
[18]     0.0    0.00    0.00       1         mhash_hmac_end [18]
                0.00    0.00       1/1           mhash_hmac_end_m [19]
-----------------------------------------------
                0.00    0.00       1/1           mhash_hmac_end [18]
[19]     0.0    0.00    0.00       1         mhash_hmac_end_m [19]
                0.00    0.00       1/6           mutils_malloc [5]
                0.00    0.00       1/1           mhash_hmac_deinit [17]
-----------------------------------------------
                0.00    0.00       1/1           main [81]
[20]     0.0    0.00    0.00       1         mhash_hmac_init [20]
                0.00    0.00       2/9           mutils_memcpy [3]
                0.00    0.00       1/2           mhash_init_int [12]
                0.00    0.00       1/6           mutils_malloc [5]
                0.00    0.00       1/6           mutils_bzero [4]
                0.00    0.00       1/4           MD5Update [9]
-----------------------------------------------

showing you which functions got executed and how they were called.

Upvotes: 2

Related Questions