Skip to content

Commit

Permalink
Added HMAC_MD5 hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
napsterback committed Nov 28, 2024
1 parent c3d4fd3 commit 9ef9fe3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 14 deletions.
1 change: 0 additions & 1 deletion aes/tiny-AES-c
Submodule tiny-AES-c deleted from ca85e0
9 changes: 4 additions & 5 deletions md5/Makefile → hashing/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
CC=gcc
DEPS = global.h md5.h
OBJ = md5c.o mddriver.o
OBJ = md5c.o mddriver.o hmac.o main.o

%.o: %.c $(DEPS)
$(CC) -c -g -o $@ $< $(CFLAGS)

md5: $(OBJ)
hash: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS)
rm *.o

clean:
# rm *.o
rm md5
rm *.o
rm hash
7 changes: 7 additions & 0 deletions md5/global.h → hashing/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* been defined with C compiler flags.
*/

#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#ifndef PROTOTYPES
#define PROTOTYPES 0
#endif
Expand All @@ -29,3 +32,7 @@ typedef unsigned long int UINT4;
#else
#define PROTO_LIST(list) ()
#endif

int main_md5();
int main_hmac();
#endif // _GLOBAL_H_
89 changes: 89 additions & 0 deletions hashing/hmac.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include "md5.h"

void hexdump(void *data, size_t size)
{ unsigned char *byteData = (unsigned char *)data; for (size_t i = 0; i < size; i++) { printf("%02x ", byteData[i]); if ((i + 1) % 16 == 0) printf("\n"); } printf("\n"); }

/// @brief the implementation of HMAC-MD5 as well as some corresponding test vectors
/// @param text pointer to data stream
/// @param text_len length of data stream
/// @param key pointer to authentication key
/// @param key_len length of authentication key
/// @param digest aller digest to be filled in

void hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, void *digest)
{
MD5_CTX context;
unsigned char k_ipad[65]; /* inner padding - key XORd with ipad*/
unsigned char k_opad[65]; /* outer padding - key XORd with opad*/

unsigned char tk[16];
int i;
/* if key is longer than 64 bytes reset it to key=MD5(key) */
if (key_len > 64)
{
MD5_CTX tctx;
MD5Init(&tctx);
MD5Update(&tctx, key, key_len);
MD5Final(tk, &tctx);

key = tk;
key_len = 16;
}
/* the HMAC_MD5 tansform look like
* MD5 (K XOR opad, MD5(K XOR ipad, text))
* where K is an n byte key
* ipad is the byte 0x36 repeated 64 times
* opad is the byte 0x5c repeated 64 times
* and text is the data being protected.
*/

/* start out by storing key in pads*/
bzero(k_ipad, sizeof k_ipad);
bzero(k_opad, sizeof k_opad);
bcopy(key, k_ipad, key_len);
bcopy(key, k_opad, key_len);

/* XOR key with ipad and opad values*/
for (i = 0; i < 64; i++)
{
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}

/* perform inner MD5 */
MD5Init(&context); /* init context for 1st
* pass */
MD5Update(&context, k_ipad, 64); /* start with inner pad */
MD5Update(&context, text, text_len); /* then text of datagram */
MD5Final(digest, &context); /* finish up 1st pass */
/*
* perform outer MD5
*/
MD5Init(&context); /* init context for 2nd pass */
MD5Update(&context, k_opad, 64); /* start with outer pad */
MD5Update(&context, digest, 16); /* then results of 1st hash */
MD5Final(digest, &context); /* finish up 2nd pass */
}

int main_hmac(int argc, char *argv[])
{
unsigned char key[255];
unsigned char data[255];
unsigned char digest[255];

memset(key, 0 , sizeof(key));
memset(data, 0, sizeof(data));
memset(digest, 0, sizeof(digest));

printf("Enter data: ");
scanf("%s", key);
printf("\nEnter key: ");
scanf("%s", data);

hmac_md5(data, strlen(data), key, strlen(key), digest);
hexdump(digest, sizeof(digest));
return 0;
}
16 changes: 11 additions & 5 deletions md5/md5.h → hashing/md5.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include <stdint.h>

#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif

typedef struct {
UINT4 state[4];
UINT4 count[2];
uint8_t state[4];
uint8_t count[2];
unsigned char buffer[64];
}MD5_CTX;

void MD5Init PROTO_LIST((MAD5_CTX *));
void MD5Init PROTO_LIST((MD5_CTX *));
void MD5Update PROTO_LIST ((MD5_CTX *, unsigned char *, unsigned int));
void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));


1 change: 1 addition & 0 deletions md5/md5c.c → hashing/md5c.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
#include "global.h"
#include "md5.h"

Expand Down
5 changes: 2 additions & 3 deletions md5/mddriver.c → hashing/mddriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void help()
* \n");
}

int main(argc, argv)
int main_md5(argc, argv)
int argc;
char *argv[];
{
Expand All @@ -51,8 +51,7 @@ int main(argc, argv)
MDFile(argv[i]);
}
}else{
help();
MDFilter();
help();
}
return 0;
}
Expand Down

0 comments on commit 9ef9fe3

Please sign in to comment.