1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 <openssl/sha.h>
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));
}
|