Thread: HMAC of a directory

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    27

    HMAC of a directory

    im supposed to create a program that checks the HMAC of a directory and then store it in a seperate file. The following program does it for a file. I have a feeling that i would need to use a for loop or something to that effect,but ive been known to be wrong before. If anyone can help me I would appreciate it.

    Code:
    # include <stdio .h>
    
    # include <string .h>
    
    # include <openssl /evp.h>
    
    int main (int argc , char ** argv )
    
    {
    
    char key [] = " This is the HMAC key";
    
    unsigned char result [ EVP_MAX_MD_SIZE ];
    
    char * data ;
    
    int i, size , *len ;
    
    FILE *fp;
    
    /* Open input file */
    
    if ( argc != 2)
    
    {
    
    fprintf (stderr , " Usage : %s filename \n", argv [0]);
    
    abort ();
    
    }
    
    fp = fopen ( argv [1] , "rb");
    
    if (! fp)
    
    {
    
    fprintf (stderr , " Cannot open file %s\n", argv [1]);
    
    abort ();
    
    }
    
    /* Read file into the data buffer */
    
    fseek (fp , 0, SEEK_END );
    
    size = ftell (fp );
    
    rewind (fp );
    
    data = ( char *) malloc ( size );
    
    3
    
    fread (data , 1, size , fp );
    
    fclose (fp );
    
    /* Compute HMAC */
    
    HMAC ( EVP_sha256 (), key , strlen ( key), data , size , result , len );
    
    /* Output result */
    
    printf (" The HMAC of file %s is: ", argv [1]);
    
    for (i = 0; i < *len ; i++)
    
    printf (" %02x", result [i ]);
    
    printf ("\n");
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You'll have to find out what exactly is meant by the "HMAC of a directory". Are you supposed to find the digest of each file in the directory? The digest of all files concatenated?

    If you're on a Unix-like system, you can use opendir() and readdir() to do directory access. POSIX 2008 also added the scandir() function, but many systems that predate POSIX 2008 support it, too.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    27
    Quote Originally Posted by cas View Post
    You'll have to find out what exactly is meant by the "HMAC of a directory". Are you supposed to find the digest of each file in the directory? The digest of all files concatenated?

    If you're on a Unix-like system, you can use opendir() and readdir() to do directory access. POSIX 2008 also added the scandir() function, but many systems that predate POSIX 2008 support it, too.
    yeah,its essentially each file in a directory and im using backtrack to do it. so i would just what,add it into a for loop or something?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    27
    Im now having an issue with segmentation fault. If anyone can help me id appreciate it.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<dirent.h>
    #include<openssl/evp.h>
    #include<openssl/hmac.h>
    
    int main (int argc , char ** argv )
    {
    struct dirent* dent;
    char key [] = " This is the HMAC key";
    unsigned char result [ EVP_MAX_MD_SIZE ];
    char * data ;
    int i, size , *len ;
    DIR* dir=opendir("..");
    FILE *fp;
    
    
    /* Open input file */
    
    if ( argc != 2)
    {
    fprintf (stderr , " Usage : %s filename \n", argv [0]);
    abort ();
    }
    fp = fopen ( argv [1] , "rb");
    if (! fp)
    {
    fprintf (stderr , " Cannot open file %s\n", argv [1]);
    abort ();
    }
    
    /* Read file into the data buffer */
    if(dir)
    {
    while((dent=readdir(dir)))
    {
    
    
    fseek (fp , 0, SEEK_END );
    size = ftell (fp );
    rewind (fp );
    data = ( char *) malloc ( size );
    fread (data , 1, size , fp );
    fclose (fp );
    
    /* Compute HMAC */
    
    HMAC ( EVP_sha256 (), key , strlen ( key), data , size , result , len );
    }
    closedir(dir);
    }
    
    /* Output result */
    
    printf (" The HMAC of file %s is: ", argv [1]);
    for (i = 0; i < *len ; i++)
    printf (" %02x", result [i ]);
    printf ("\n");
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Fix your indentation, your code is hard to read. http://sourceforge.net/apps/mediawik...le=Indentation

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    27
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<dirent.h>
    #include<openssl/evp.h>
    #include<openssl/hmac.h>
    
    int main (int argc , char ** argv )
    {
       struct dirent* dent;
       char key [] = " This is the HMAC key";
       unsigned char result [ EVP_MAX_MD_SIZE ];
       char * data ;
       int i, size , *len ;
       DIR* dir=opendir("..");
       FILE *fp;
    
    
    /* Open input file */
    
    if ( argc != 2)
    {
       fprintf (stderr , " Usage : %s filename \n", argv [0]);
       abort ();
    }
    fp = fopen ( argv [1] , "rb");
    if (! fp)
    {
       fprintf (stderr , " Cannot open file %s\n", argv [1]);
       abort ();
    }
    
    /* Read file into the data buffer */
    
    if(dir)
    {
    while((dent=readdir(dir)))
    {
       fseek (fp , 0, SEEK_END );
       size = ftell (fp );
       rewind (fp );
       data = ( char *) malloc ( size );
       fread (data , 1, size , fp );
       fclose (fp );
    
    /* Compute HMAC */
    
      HMAC ( EVP_sha256 (), key , strlen ( key), data , size , result , len );
    }
    closedir(dir);
    }
    
    /* Output result */
    
    printf (" The HMAC of file %s is: ", argv [1]);
    for (i = 0; i < *len ; i++)
    printf (" %02x", result [i ]);
    printf ("\n");
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Better, but still could use some work. Witness:
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<dirent.h>
    #include<openssl/evp.h>
    #include<openssl/hmac.h>
    
    
    int main (int argc , char ** argv )
    {
        struct dirent* dent;
        char key [] = " This is the HMAC key";
        unsigned char result [ EVP_MAX_MD_SIZE ];
        char * data ;
        int i, size , *len ;
        DIR* dir=opendir("..");
        FILE *fp;
    
    
    
    
        /* Open input file */
    
    
        if ( argc != 2)
        {
            fprintf (stderr , " Usage : %s filename \n", argv [0]);
            abort ();
        }
        fp = fopen ( argv [1] , "rb");
        if (! fp)
        {
            fprintf (stderr , " Cannot open file %s\n", argv [1]);
            abort ();
        }
    
    
        /* Read file into the data buffer */
    
    
        if(dir)
        {
            while((dent=readdir(dir)))
            {
                fseek (fp , 0, SEEK_END );
                size = ftell (fp );
                rewind (fp );
                data = ( char *) malloc ( size );
                fread (data , 1, size , fp );
                fclose (fp );
    
    
                /* Compute HMAC */
    
    
                HMAC ( EVP_sha256 (), key , strlen ( key), data , size , result , len );
            }
            closedir(dir);
        }
    
    
        /* Output result */
    
    
        printf (" The HMAC of file %s is: ", argv [1]);
        for (i = 0; i < *len ; i++)
            printf (" %02x", result [i ]);
        printf ("\n");
        return 0;
    }
    I don't know the HMAC function, but I would guess your problem is that len doesn't actually point anywhere. Most likely, the HMAC function wants a pointer to an integer, so it can pass back the length of something via the len parameter. You probably need to declare len as a plain integer, then pass in it's address:
    Code:
    int len;
    ...
    HMAC(..., &len);
    Also, I don't know if this matters, but does data need to be null terminated? If so, you need to malloc size+1 bytes, and set data[size] = '\0'.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    27
    noted,but out of curiouisty. Its the first time ive used the dirent library. did I implement it right?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It looks fine, but did you test it? Did you try printing some relevant info from the dent variable each time through the loop to see if it works as expected? There's absolutely no point in using the dirent library unless you make use of the dent variable somewhere. Currently, you call HMAC repeatedly, with what appears to be a bad parameter (len), which is more likely the cause of your seg fault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-30-2010, 06:13 PM
  2. directory help
    By thinice16 in forum C Programming
    Replies: 2
    Last Post: 08-09-2008, 02:29 PM
  3. Directory help.
    By mrafcho001 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2005, 06:38 PM
  4. Directory?
    By Com-41 in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 07:57 PM
  5. Directory
    By dot_com in forum Windows Programming
    Replies: 1
    Last Post: 02-27-2002, 10:27 PM