summaryrefslogtreecommitdiff
path: root/set5/task39.c
blob: 40c00ef1ee3086737c00ef6612903472ff4d0344 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "../lib/lib.h"
#include "../lib/lib2.h"
#include "../lib/lib3.h"
#include "../lib/lib4.h"
#include "../lib/lib5.h"
#include <time.h>

#include<openssl/bn.h>
#include<openssl/bio.h>

int modular_multiplicative_inverse(int number, int _modulo)
{
	struct extended_euclid tmp;
	extended_euclid_algo(number, _modulo, &tmp);
	// only has a inverse iff gcd = 1
	if ( tmp.d != 1)
		return INT_MIN;

	// mod works not fine for negytive numbers in c
	return modulo(tmp.s, _modulo);
}
/*
 * TODO do it iterative, maybe stack it not big enough
void extended_euclid_algo_bignum(BIGNUM *a, BIGNUM *b, struct extended_euclid_bignum *e)
{
	struct extended_euclid_bignum tmp;
	tmp.d = BN_new();
	tmp.s = BN_new();
	tmp.t = BN_new();

	if (BN_is_zero(b)) {
		e->d=a;
		BN_one(e->s);
		BN_zero(e->t);
	}
	BIGNUM *mod = BN_new();
	BN_mod(mod, a, b, ctx);

	extended_euclid_algo_bignum(b, mod, &tmp);
	BN_copy(e->d, tmp.d);
	BN_copy(e->s, tmp.t);
	BN_div(mod, NULL, a, b, ctx);
	BN_mul(mod, mod, tmp.s, ctx);
	BN_sub(e->t, tmp.s, mod);
	//BN_copy(e->t, );
	
	BN_free(mod);
	BN_free(tmp.d);
	BN_free(tmp.s);
	BN_free(tmp.t);
	printf("durchlauf von extended_euclid durch\n");
	return;
}
int modular_multiplicative_inverse_bignum(BIGNUM *res, BIGNUM *number, BIGNUM *modulo)
{
//	
	struct extended_euclid_bignum tmp;
	tmp.d = BN_new();
	tmp.s = BN_new();
	tmp.t = BN_new();
	extended_euclid_algo_bignum(number, modulo, &tmp);
	// only has a invese iff gcd = 1
	if (BN_is_one(tmp.d))
		return -1;

	return BN_mod(res, tmp.s, modulo, ctx);
}
*/

#define BN_DEBUG

int main()
{
	struct rsa_key_bignum private, public;
	// debugging: printing BN's
	BIO *out = BIO_new(BIO_s_file());
	BIO_set_fp(out, stdout, BIO_NOCLOSE);

	ctx = BN_CTX_new();

	rsa_generate_key_bignum(&private, &public);

	printf("message:\n");
	BIGNUM *message = BN_new();
	BIGNUM *encrypted = BN_new();
	BIGNUM *decrypted = BN_new();
	BN_set_word(message, 4234667);
	BN_print(out, message);

	if(!rsa_encrypt_bignum(message, encrypted, &public))
		die("could not rsa encrypt message");

	printf("\nencrypted rsa message\n");
	BN_print(out, encrypted);

	if(!rsa_decrypt_bignum(encrypted, decrypted, &private))
		die("could not rsa decrypt");

	printf("\ndecrypted message:\n");
	BN_print(out, decrypted);

	BN_CTX_free(ctx);
	free_rsa_key_bignum(&private);
	free(public.exponent);
}

int main_littlenum()
{
	int message = 65;
	int p = 5, q = 11;
	int n = p * q;
	int et = (p-1) * (q-1);
	int e = 3;

	// does not work, nums are above INT_MAX
	int d = modular_multiplicative_inverse(e, et);
	// public key is [e, n], private key is [d, n]
	struct rsa_key public = { .exponent = e, .modulo = n };
	struct rsa_key private = { .exponent = d, .modulo = n };
	printf("public key is: %i, %i\n", public.exponent, public.modulo);
	printf("private key is: %i, %i\n", private.exponent, private.modulo);
	int ciphertext = rsa_encrypt(message, &public);
	printf("encrpyt %i: %i\n", message, ciphertext);
	int dec_message = rsa_decrpyt(ciphertext, &private);
	printf("decrypt %i: %i\n", ciphertext, dec_message);
	return 0;
}