summaryrefslogtreecommitdiff
path: root/lib/lib6.c
blob: fe2cd6030eb0cad96505fd6a8a69f64175f46aa2 (plain)
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 "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;
}