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
|
#include "../lib/lib.h"
#include "../lib/lib2.h"
#include "../lib/lib3.h"
#include "../lib/lib4.h"
#include "../lib/lib5.h"
#include <time.h>
int DH_smallnums()
{
srand(time(NULL));
// public prime numbers; everbody can know them, parameters of the
// expoential function
unsigned int p = 37;
unsigned int g = 5;
// exponents of the dicrect exponential function, should be kept secret
unsigned int b;
unsigned int a;
generate_random_bytes((char *)&a,4);
a = a % p;
generate_random_bytes((char *)&b,4);
b = b % p;
// A and B are the public keys, sent to each other
// one cannot infer a/b from A/B
unsigned A = g ^ a % p;
unsigned B = g ^ b % p;
// both compute these number, s1 = s2; (g^a)^b = (g^b)^a
unsigned int s1 = B ^ a % p;
unsigned int s2 = A ^ b % p;
printf("session key A: %i\n", s1);
printf("session key B: %i\n", s2);
// generate a key, 128 bit by hashing the session key
}
void DH_bignums()
{
struct dh_param_bignum dh;
do_dh_key_exchange_bignum(&dh);
printf("bignums: A: ");
BN_print(out, dh.A);
printf("\nbignums: session key 1: ");
BN_print(out, dh.s1);
printf("\nbignums: session key 2: ");
BN_print(out, dh.s2);
if(BN_cmp(dh.s1, dh.s2) == 0)
printf("\nboth session keys are equal!\n");
}
int main()
{
out = BIO_new(BIO_s_file());
BIO_set_fp(out, stdout, BIO_NOCLOSE);
ctx = BN_CTX_new();
DH_smallnums();
DH_bignums();
}
|