From 29591502e2ab22a262063e5552438b23380b3e55 Mon Sep 17 00:00:00 2001 From: Benedict Date: Sat, 17 Dec 2016 17:40:31 +0100 Subject: set6: challenge 43: completed --- set6/task43.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 set6/task43.c (limited to 'set6') diff --git a/set6/task43.c b/set6/task43.c new file mode 100644 index 0000000..b1386fa --- /dev/null +++ b/set6/task43.c @@ -0,0 +1,98 @@ +#include "../lib/lib.h" +#include "../lib/lib2.h" +#include "../lib/lib3.h" +#include "../lib/lib4.h" +#include "../lib/lib5.h" +#include "../lib/lib6.h" +#include + +int main() +{ + int i; + struct dsa_public_params dsa_pub; + struct dsa_per_user_param dsa_user; + struct dsa_per_user_param dsa_user_org; + BIGNUM *hash_bn = BN_new(); + BIGNUM *k = BN_new(); + char *message = "For those that envy a MC it can be hazardous to your health\nSo be friendly, a matter of life and death, just like a etch-a-sketch\n"; + + out = BIO_new(BIO_s_file()); + BIO_set_fp(out, stdout, BIO_NOCLOSE); + ctx = BN_CTX_new(); + + printf("message is:\n%s\n", message); + SHA_CTX sha1; + char sha1_hash[20]; + char hex[40]; + dsa_user.public = BN_new(); + dsa_user.private = BN_new(); + dsa_user.r= BN_new(); + dsa_user.s = BN_new(); + + dsa_user_org.public = BN_new(); + dsa_user_org.private = BN_new(); + dsa_user_org.r= BN_new(); + dsa_user_org.s = BN_new(); + + SHA1_Init(&sha1); + SHA1_Update(&sha1, message, strlen(message)); + SHA1_Final(sha1_hash, &sha1); + hex_binary_to_string(sha1_hash, hex, 20); + printf("hash of message is: %s\n", hex); + BN_bin2bn(sha1_hash, 20, hash_bn); + BN_print(out, hash_bn); + + init_dsa_pub_param(&dsa_pub); + + // set public key + char * y = "84ad4719d044495496a3201c8ff484feb45b962e7302e56a392aee4" + "abab3e4bdebf2955b4736012f21a08084056b19bcd7fee56048e004" + "e44984e2f411788efdc837a0d2e5abb7b555039fd243ac01f0fb2ed" + "1dec568280ce678e931868d23eb095fde9d3779191b8c0299d6e07b" + "bb283e6633451e535c45513b2d33c99ea17"; + char *r = "548099063082341131477253921760299949438196259240"; + char *s = "857042759984254168557880549501802188789837994940"; + + BN_hex2bn(&dsa_user.public, y); + // well this is decimal .... + BN_dec2bn(&dsa_user.r, r); + BN_dec2bn(&dsa_user.s, s); + + BN_copy(dsa_user_org.public, dsa_user.public); + BN_copy(dsa_user_org.r, dsa_user.r); + BN_copy(dsa_user_org.s, dsa_user.s); + + // can i verfiy that? + // brute force + BIGNUM *tmp = BN_new(); + for(i=0;i<65536;i++) { + BN_set_word(k, i); + //BN_copy(dsa_user.r, dsa_user_org.r); + //BN_copy(dsa_user.s, dsa_user_org.s); + //BN_copy(dsa_user.public, dsa_user_org.public); + dsa_recover_x_from_known_k(&dsa_pub, k, &dsa_user, hash_bn); + // compute signture with this x,k and compare + printf("\nprivate key for k = %i\n", i); + BN_print(out, dsa_user.private); + //dsa_sign(message, &dsa_pub, &dsa_user, k); + BN_mod_exp(tmp, dsa_pub.g, dsa_user.private, dsa_pub.p, ctx); + if(!BN_cmp(tmp, dsa_user.public)) + break; + } + printf("\nk is: %i, private key:\n", i); + BN_print(out, dsa_user.private); + char *str_priv = malloc(BN_num_bytes(dsa_user.private)); + char *str_priv_hex = malloc(BN_num_bytes(dsa_user.private)*2+1); + BN_bn2bin(dsa_user.private, str_priv); + hex_binary_to_string(str_priv, str_priv_hex, BN_num_bytes(dsa_user.private)); + + SHA1_Init(&sha1); + SHA1_Update(&sha1, str_priv_hex, BN_num_bytes(dsa_user.private)*2); + SHA1_Final(sha1_hash, &sha1); + + char *given_hash = "0954edd5e0afe5542a4adf012611a91912a3ec16"; + hex_binary_to_string(sha1_hash, hex, 20); + printf("\nhash of private key is: %s\n", hex); + printf("given one: %s\n", given_hash); + printf("equal?: %i", !memcmp(hex, given_hash, 20)); +} -- cgit v1.2.3-70-g09d2