diff options
| author | Benedict <benedict@0xb8000.de> | 2016-09-23 11:36:08 +0200 |
|---|---|---|
| committer | Benedict <benedict@0xb8000.de> | 2017-02-21 13:00:26 +0100 |
| commit | 723222e769785563babdda5f78a0ce21a276cb9f (patch) | |
| tree | 660d7312f7e85206daad7de6912ce63bee5f4b36 /set4/task28.c | |
| parent | 111642369ba778d5b8d18f1a9d8acf379ab45c13 (diff) | |
completed set 4, task 31
Diffstat (limited to 'set4/task28.c')
| -rw-r--r-- | set4/task28.c | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/set4/task28.c b/set4/task28.c index 993d5a3..02ab0f0 100644 --- a/set4/task28.c +++ b/set4/task28.c @@ -9,34 +9,46 @@ 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]; + unsigned int hmac[5]; + unsigned int hmac2[5]; int i; - char *text = "comment1=cooking%20MCs;userdata=foo;comment2=%20like%20a%20pound%20of%20bacon"; - char *append = ";admin=true"; - char *key = argv[1]; - + unsigned char *text = "comment1=cooking%20MCs;userdata=foo;comment2=%20like%20a%20pound%20of%20bacon"; + unsigned char *append = ";admin=true"; + unsigned char *key = argv[1]; printf("Using secret key: %s\n", key); - char *padded; - int padding_len = sha1_padding(strlen(text), &padded); + unsigned char *padded; + int padding_len = sha1_padding(strlen(key)+strlen(text), &padded); - sha1_hmac(hex, text, strlen(text), key, strlen(key)); + sha1_hmac(hmac, text, strlen(text), key, strlen(key)); printf("MAC of original message:\n"); for(i=0;i<5;i++) - printf("%02x", hex[i]); + printf("%02x", hmac[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. + * key. Actually we don't know the message here, just the hash of the orginal + * message. We have to append the right padding here, e.g. the size of the + * *complete* message, not only append */ - sha1_hmac_forge(hex2, append, strlen(append), hex); + unsigned int new_msg_len = strlen(text)+strlen(append)+padding_len; + unsigned 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)); + + unsigned char *padding2; + // mesage + padding + append + padding + int padding2_len = sha1_padding(new_msg_len+strlen(key), &padding2); + unsigned char *tmp2 = malloc(strlen(append)+padding2_len); + memcpy(tmp2, append, strlen(append)); + memcpy(&tmp2[strlen(append)], padding2, padding2_len); + sha1_hmac_forge(hmac2, tmp2, (strlen(append)+padding2_len), hmac); printf("MAC of forged message:\n"); for(i=0;i<5;i++) - printf("%02x", hex2[i]); + printf("%02x", hmac2[i]); printf("\n"); @@ -45,12 +57,7 @@ int main(int argc, char **argv) * 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("Verifying...\n"); + if(sha1_hmac_verify(hmac2, new_msg, new_msg_len, key, strlen(key))) printf("Forged MAC got accepted!\n"); } |
