summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBenedict <benedict@0xb8000.de>2016-12-15 21:19:27 +0100
committerBenedict <benedict@0xb8000.de>2017-02-21 13:00:26 +0100
commit1fe002d0b46b3e5b3559dd629f3d56b16bb12c0b (patch)
tree9aa8ddebc071eeb41b6acb4159c15bb59d0a4739 /lib
parentcfe5cbfd43b5c3e18737fc2707d351919ddf4aa2 (diff)
set6: task42 completed
Diffstat (limited to 'lib')
-rw-r--r--lib/lib6.c98
-rw-r--r--lib/lib6.h16
2 files changed, 114 insertions, 0 deletions
diff --git a/lib/lib6.c b/lib/lib6.c
index e69de29..fe2cd60 100644
--- a/lib/lib6.c
+++ b/lib/lib6.c
@@ -0,0 +1,98 @@
+#include "lib6.h"
+#include "lib5.h"
+#include "lib4.h"
+#include "lib3.h"
+#include "lib2.h"
+#include "lib.h"
+
+
+int rsa_sign_bignum(BIGNUM *message, BIGNUM *signed_message, struct rsa_key_bignum *private)
+{
+ rsa_encrypt_bignum(message, signed_message, private);
+}
+
+int rsa_verify_bignum(BIGNUM *signed_message, BIGNUM *org_message, struct rsa_key_bignum *public)
+{
+ BIGNUM *res = BN_new();
+ int ret = -1;
+
+ rsa_decrypt_bignum(signed_message, res, public);
+ ret = BN_cmp(res, org_message);
+ printf("\nverfied mess ret: %i, message:\n", ret);
+ BN_print(out, res);
+ printf("\n");
+ BN_free(res);
+
+ return ret == 0;
+}
+/**
+ * construct a VALID pkcs_padding
+ **/
+void pkcs1_5_padding(char *message, char *result, unsigned int target_length_byte)
+{
+ SHA1Context sha1;
+ char sha1_hash[20];
+ int i;
+
+ memset(result, 0xff, target_length_byte);
+ result[0] = 0x00;
+ result[1] = 0x01;
+ result[target_length_byte-21] = 0x00;
+
+ // TODO ASN.1 things
+
+ SHA1Reset(&sha1);
+ SHA1Input(&sha1, message, strlen(message));
+ SHA1Result(&sha1);
+ memcpy(sha1_hash, &(sha1.Message_Digest), 20);
+
+ for(i = 20;i>0;i--)
+ result[target_length_byte-i] = sha1_hash[20-i];
+}
+
+int pkcs1_5_padding_verify(char *to_verify, int len, char *message)
+{
+ char result[1024/8];
+ int i;
+
+ // construct the padding how the expect it and than compare
+ pkcs1_5_padding(message, result, 1024/8);
+ // printf both paddings
+ char buf[(1024/8)*2];
+ hex_binary_to_string(result, buf, 1024/8);
+ printf("expected padding:\n%s\n", buf);
+ hex_binary_to_string(to_verify, buf, len);
+ printf("got:\n%s\n", buf);
+
+ return memcmp(to_verify, result, 128) == 0;
+}
+
+int shitty_pkcs1_5_padding_verify(char *to_verify, int len, char *message)
+{
+ int i = 2;
+ SHA1Context sha1;
+ char sha1_hash[20];
+
+ if (len < 2 && to_verify[0] != 0x00 && to_verify[1] != 0x01)
+ return 0;
+
+ // search for the next 0x00 no matter what's in between
+ while(to_verify[i] != 0x00)
+ i++;
+
+ i++;
+ // TODO check asn.1 things
+ // verfiy the hash
+ SHA1Reset(&sha1);
+ SHA1Input(&sha1, message, strlen(message));
+ SHA1Result(&sha1);
+ memcpy(sha1_hash, &(sha1.Message_Digest), 20);
+
+ int j;
+ for(j=0;j<20;j++, i++) {
+ if (to_verify[i] != sha1_hash[j])
+ return 0;
+ }
+
+ return 1;
+}
diff --git a/lib/lib6.h b/lib/lib6.h
index e69de29..23741b0 100644
--- a/lib/lib6.h
+++ b/lib/lib6.h
@@ -0,0 +1,16 @@
+#ifndef __LIB_6_H__
+#define __LIB_6_H__
+#include "lib6.h"
+#include "lib5.h"
+#include "lib4.h"
+#include "lib3.h"
+#include "lib2.h"
+#include "lib.h"
+
+
+int rsa_sign_bignum(BIGNUM *message, BIGNUM *signed_message, struct rsa_key_bignum *private);
+int rsa_verify_bignum(BIGNUM *signed_message, BIGNUM *org_message, struct rsa_key_bignum *public);
+int shitty_pkcs1_5_padding_verify(char *to_verify, int len, char *message);
+int pkcs1_5_padding_verify(char *to_verify, int len, char *message);
+
+#endif /* __LIB_6_H__ */