diff options
| author | Benedict <benedict@0xb8000.de> | 2016-09-03 00:12:20 +0200 |
|---|---|---|
| committer | Benedict <benedict@0xb8000.de> | 2017-02-21 13:00:26 +0100 |
| commit | 111642369ba778d5b8d18f1a9d8acf379ab45c13 (patch) | |
| tree | 4cfba465f09616d46350c2b1be625d601e318e40 /lib | |
| parent | fe73b0589483a11389fd68b1b0910229039bfcca (diff) | |
set4, completed challend 28
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/lib4.c | 42 | ||||
| -rw-r--r-- | lib/lib4.h | 3 |
2 files changed, 43 insertions, 2 deletions
@@ -21,8 +21,9 @@ int aes_ctr_edit(char *ciphertext, int ciphertext_length, int offset, char *newt return 0; } -void sha1_hmac(unsigned int *mac, unsigned char *message, unsigned int msg_len, unsigned char *key, - unsigned int key_len) + +void sha1_hmac(unsigned int *mac, unsigned char *message, unsigned int msg_len, + unsigned char *key, unsigned int key_len) { char *res = malloc(msg_len + key_len); @@ -32,6 +33,7 @@ void sha1_hmac(unsigned int *mac, unsigned char *message, unsigned int msg_len, SHA1Context sh; SHA1Reset(&sh); + SHA1Input(&sh, res, (msg_len + key_len)); SHA1Result(&sh); @@ -48,3 +50,39 @@ int sha1_hmac_verify(unsigned int *mac, unsigned char *msg, unsigned int msg_len return !memcmp(com_mac, mac, 20); } + +int sha1_padding(unsigned long msg_len, char **result) +{ + int i; + unsigned int padding_len = 64 - (msg_len % 64); + // enough sapce for the length of the message? + padding_len = padding_len < 9 ? padding_len+64 : padding_len; + + (*result) = malloc(padding_len); + + memset((*result), 0x00, padding_len); + // append 1 + memset(&(*result)[0], 0x80, 1); + //(*result)[0] = 0x80; + + // write the 8 byte msg_len at the end bytewise + for(i=0;i<8;i++) { + (*result)[padding_len-i-1] = (msg_len*8 >> (i*8)) & 0xFF; + } + + return padding_len; +} + +void sha1_hmac_forge(unsigned int *mac, unsigned char *text, unsigned int text_len, + unsigned int *sha1_registers) +{ + SHA1Context sh; + SHA1Reset(&sh); + + sha1_set_magic_nr(&sh, sha1_registers); + + SHA1Input(&sh, text, text_len); + SHA1Result(&sh); + + memcpy(mac, &(sh.Message_Digest), 20); +} @@ -10,4 +10,7 @@ void sha1_hmac(unsigned int *mac, unsigned char *message, unsigned int msg_len, unsigned int key_len); int sha1_hmac_verify(unsigned int *mac, unsigned char *msg, unsigned int msg_len, unsigned char *key, unsigned int key_len); +int sha1_padding(unsigned long msg_len, char **result); +void sha1_hmac_forge(unsigned int *mac, unsigned char *text, unsigned int text_len, + unsigned int *sha1_registers); #endif |
