-
Notifications
You must be signed in to change notification settings - Fork 0
/
DH_KeyEstablishment_hash.cpp
131 lines (98 loc) · 3.69 KB
/
DH_KeyEstablishment_hash.cpp
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
128
129
130
/*
* Code for Diffie-Hellman Key Establishment using 2048 bit prime
*
* Requires: big.cpp ecn.cpp
*/
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "ecn.h"
#include "big.h"
#include <ctime>
extern "C"
{
#include "miracl.h"
}
using namespace std;
extern "C" { FILE _iob[3] = {__iob_func()[0], __iob_func()[1], __iob_func()[2]}; }
/* large 2048 bit prime p for which (p-1)/2 is also prime, we found it from RFC 3526.
* Check the link below to see RFC 3526
* <http://tools.ietf.org/html/rfc3526#page-3>
*/
char *primetext=(char *)
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF";
#ifndef MR_NOFULLWIDTH
Miracl precision(500,0);
#else
Miracl precision(500,MAXBASE);
#endif
// If MR_STATIC is defined in mirdef.h, it is assumed to be 100
//Miracl precision(120,(1<<26));
//char *DH_KeyEstablishment()
int main()
{
time_t seed;
Big a,b,p,q,pa,pb,key;
miracl *mip=&precision;
time(&seed);
irand((long)seed); /* change parameter for different values */
cout << "First Diffie-Hellman Key exchange .... " << endl;
mip->IOBASE=16;
p=primetext;
// int primetext_len=strlen(primetext);
// cout<<"primetext_len= "<<primetext_len<<endl;
/* offline calculations could be done quicker using Comb method
- See brick.cpp. Note use of "truncated exponent" of 160 bits -
could be output from hash function SHA (see mrshs.c) */
cout << "\nClient's offline calculation" << endl;
a=rand(160,2);
/* 2 generates the prime sub-group of size (p-1)/2 */
pa=pow(2,a,p); // pa =2^a mod p
cout << "Server's offline calculation" << endl;
b=rand(160,2);
pb=pow(2,b,p);
cout << "Client calculates Key=" << endl;
key=pow(pb,a,p);
cout << key << endl;
cout<<"key[0]= "<<key[0]<<endl;
/*
int key_len;
key_len=to_binary(key,strlen(mip->IOBUFF),mip->IOBUFF,FALSE);
cout<<"pure binary key string is "<<endl;
for(int i=0;i<key_len;i++)
printf("%02x",(unsigned char)mip->IOBUFF[i]);
printf("\n");
cout<<"key_len= "<<key_len<<endl;
*/
mip->IOBASE=16;
mip->IOBUFF << key;
cout<<"hex display of key is: "<<endl<<mip->IOBUFF<<endl;
cout<<"len of hex string of key is "<<strlen(mip->IOBUFF)<<endl;
//Here, client begins to hash the long session key, to get 32bytes new session key for AES encryption
char hash_c[32];
int i;
sha256 sh;
shs256_init(&sh);
for (i=0;mip->IOBUFF[i]!=0;i++) shs256_process(&sh,mip->IOBUFF[i]);
shs256_hash(&sh,hash_c);
for (i=0;i<32;i++) printf("%02x",(unsigned char)hash_c[i]);
printf("\n");
//server calculates his key
cout << "Server calculates Key=" << endl;
key=pow(pa,b,p);
cout << key << endl;
//Here, server begins to hash the long session key, to get 32bytes new session key for AES encryption
char hash_s[32];
// int i;
// sha256 sh;
shs256_init(&sh);
for (i=0;mip->IOBUFF[i]!=0;i++) shs256_process(&sh,mip->IOBUFF[i]);
shs256_hash(&sh,hash_s);
for (i=0;i<32;i++) printf("%02x",(unsigned char)hash_s[i]);
printf("\n");
//////--------------------Client and Server finished to create key using 2048 bit prime--------------//////
cout << "Alice and Bob's keys should be the same!" << endl;
system("pause");
return 0;
}