summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib4.c28
-rw-r--r--lib/lib4.h8
-rw-r--r--set4/task27.c29
3 files changed, 64 insertions, 1 deletions
diff --git a/lib/lib4.c b/lib/lib4.c
index 475efe9..5b7ec7d 100644
--- a/lib/lib4.c
+++ b/lib/lib4.c
@@ -20,3 +20,31 @@ int aes_ctr_edit(char *ciphertext, int ciphertext_length, int offset, char *newt
aes_ctr(plaintext, ciphertext_length, ciphertext, key, nonce);
return 0;
}
+
+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);
+
+ memcpy(res, key, key_len);
+ memcpy(&res[key_len], message, msg_len);
+
+ SHA1Context sh;
+
+ SHA1Reset(&sh);
+ SHA1Input(&sh, res, (msg_len + key_len));
+ SHA1Result(&sh);
+
+ memcpy(mac, &(sh.Message_Digest), 20);
+}
+
+int sha1_hmac_verify(unsigned int *mac, unsigned char *msg, unsigned int msg_len,
+ unsigned char *key, unsigned int key_len)
+{
+ unsigned int com_mac[5];
+
+ sha1_hmac(com_mac, msg, msg_len, key, key_len);
+
+ return !memcmp(com_mac, mac, 20);
+
+}
diff --git a/lib/lib4.h b/lib/lib4.h
index dee52a7..1d7bd36 100644
--- a/lib/lib4.h
+++ b/lib/lib4.h
@@ -1,7 +1,13 @@
#ifndef __LIB_4__
#define __LIB_4__
+#include "sha1.h"
+// sha1 constants
-int aes_ctr_edit(char *ciphertext, int ciphertext_length, int offset, char *newtext);
+int aes_ctr_edit(char *ciphertext, int ciphertext_length, int offset, char *newtext);
+void sha1_hmac(unsigned int *mac, unsigned char *message, unsigned int msg_len, unsigned char *key,
+ 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);
#endif
diff --git a/set4/task27.c b/set4/task27.c
new file mode 100644
index 0000000..ae279cd
--- /dev/null
+++ b/set4/task27.c
@@ -0,0 +1,29 @@
+#include "../lib/lib.h"
+#include "../lib/lib2.h"
+#include "../lib/lib3.h"
+#include "../lib/lib4.h"
+#include <time.h>
+
+
+int main()
+{
+ unsigned int hex[5];
+ int i;
+ char text[] = "Rolling on the floor lauthing";
+ char *key = "ROFL";
+
+ sha1_hmac(hex, text, strlen(text), key, strlen(key));
+ for(i=0; i<5; i++) {
+ printf("%08x", hex[i]);
+ }
+ printf("\n");
+
+ if(sha1_hmac_verify(hex, text, strlen(text), key, strlen(key)))
+ printf("MACs are equal\n");
+
+ // tamper message change R to r
+ text[0] = 'r';
+
+ if(!sha1_hmac_verify(hex, text, strlen(text), key, strlen(key)))
+ printf("MACs of tampered message are NOT equal\n");
+}