diff options
| -rw-r--r-- | lib/lib4.c | 42 | ||||
| -rw-r--r-- | lib/lib4.h | 3 | ||||
| -rw-r--r-- | set4/task28.c | 56 | ||||
| -rwxr-xr-x | set4/task28.sh | 3 |
4 files changed, 102 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 diff --git a/set4/task28.c b/set4/task28.c new file mode 100644 index 0000000..993d5a3 --- /dev/null +++ b/set4/task28.c @@ -0,0 +1,56 @@ +#include "../lib/lib.h" +#include "../lib/lib2.h" +#include "../lib/lib3.h" +#include "../lib/lib4.h" +#include <time.h> + + +int main(int argc, char **argv) +{ + if(argc != 2) + printf("Please provide ONE key as argument!\n"); + unsigned int hex[5]; + unsigned int hex2[5]; + int i; + char *text = "comment1=cooking%20MCs;userdata=foo;comment2=%20like%20a%20pound%20of%20bacon"; + char *append = ";admin=true"; + char *key = argv[1]; + + printf("Using secret key: %s\n", key); + + char *padded; + int padding_len = sha1_padding(strlen(text), &padded); + + sha1_hmac(hex, text, strlen(text), key, strlen(key)); + printf("MAC of original message:\n"); + for(i=0;i<5;i++) + printf("%02x", hex[i]); + + printf("\n"); + + /* + * We are appending a text to the original message without knowign the + * key. Actually we don't know the message here, just the length of + * the message. + */ + sha1_hmac_forge(hex2, append, strlen(append), hex); + printf("MAC of forged message:\n"); + for(i=0;i<5;i++) + printf("%02x", hex2[i]); + + printf("\n"); + + /* + * create the message we forged. Send this plus the hmac to the + * victim. He knows the secret and test and will think that + * this is a message from Alice + */ + unsigned int new_msg_len = strlen(text)+strlen(append)+padding_len; + char *new_msg = malloc(new_msg_len); + memcpy(new_msg, text, strlen(text)); + memcpy(&new_msg[strlen(text)], padded, padding_len); + memcpy(&new_msg[strlen(text)+padding_len], append, strlen(append)); + + if(!sha1_hmac_verify(hex2, new_msg, new_msg_len, key, strlen(key))) + printf("Forged MAC got accepted!\n"); +} diff --git a/set4/task28.sh b/set4/task28.sh new file mode 100755 index 0000000..c6078fa --- /dev/null +++ b/set4/task28.sh @@ -0,0 +1,3 @@ +#!/bin/fish + +./task28 (set RANDOM_NR (math (random) \% 54763); head -n $RANDOM_NR /usr/share/dict/cracklib-small | tail -n 1) |
