forked from gadial/ECC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
elgamal.h
149 lines (123 loc) · 3.79 KB
/
elgamal.h
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* File: elgamal.h
* Author: gadial
*
* Created on January 27, 2010, 2:51 PM
*/
#ifndef _ELGAMAL_H
#define _ELGAMAL_H
#include "ellipticcurve.h"
static vector<string> StringSplit(string str, string delim) {
int cutAt;
vector<string> results;
while ((cutAt = str.find_first_of(delim)) != str.npos) {
if (cutAt > 0) {
results.push_back(str.substr(0, cutAt));
}
str = str.substr(cutAt + 1);
}
if (str.length() > 0) {
results.push_back(str);
}
return results;
}
class ECC_ElGamal_Ciphertext{
public:
Coordinate C1, C2;
ECC_ElGamal_Ciphertext(Coordinate _C1, Coordinate _C2): C1(_C1), C2(_C2){}
static ECC_ElGamal_Ciphertext from_string(string str, Ellipticcurve* el) {
vector<string> v = StringSplit(str, ",");
return ECC_ElGamal_Ciphertext(el->getPointCompressedForm(v[0]),
el->getPointCompressedForm(v[1]));
}
string to_string(Ellipticcurve* el) {
return el->toCompressedForm(C1) + "," + el->toCompressedForm(C2);
}
};
class ECC_ElGamal_Plaintext {
public:
Coordinate P;
ECC_ElGamal_Plaintext(Coordinate _P) : P(_P) {}
};
class ECC_ElGamal{
public:
ECC_ElGamal(Ellipticcurve* E);
Coordinate get_public_key() const{return Q;}
mpz_class get_private_key() const{return d;}
void set_keys(Coordinate _Q, mpz_class _d){Q = _Q; d = _d;}
void set_public_key(Coordinate _Q) {Q = _Q;}
void set_private_key(mpz_class _d) {d = _d;}
void set_keys_from_private_key(mpz_class _d){d = _d; Q = ell->pointMultiplication(ell->point, d);}
/*
* Generates a random pk/sk key pair according to Algorithm 1.12 in
* "Guide to Elliptic Curve Cryptography"
*/
void generate_random_keypair();
ECC_ElGamal_Ciphertext encrypt_element(ECC_ElGamal_Plaintext m);
//ECC_ElGamal_Ciphertext encrypt_element(string m);
ECC_ElGamal_Plaintext decrypt_element (ECC_ElGamal_Ciphertext ciphertext);
/*
* Encrypts a given string 'm'
* 'm' must be no longer than 'get_max_point_length()'
*/
string encrypt(string m);
/*
* decrypts 'c'
* 'c' is a ciphertext in the following format: C1,C2
* Where C1 and C2 are in compressed format
*/
string decrypt(string c);
/*
* Splits the message string into coordinates on the EC
* integers, with 1 byte random padding (p)
* str_1 p str_2 p
* [|---------|-|],[---------|-|],...
*
*
*
vector<ECC_ElGamal_Plaintext> split_msg(string msg);
*/
/*
* Converts a string of length 'max_point_length'-1 to an gmp integer
*
* More specifically:
*
* Encodes a string to a point on the Elliptic Curve, including
* one byte random padding. In the following way:
*
* The string corresponds to the X-coordinate of the point,
* random padding is choosen in such a way that a Y coordinate exists
*
* --------------------------
* Format of the X-Coordinate
* --------------------------
*
* ASCII(char) means the ascii code of a character (in binary format)
* rand() is a random (one byte) character
*
* Example: "abc" <-> ASCII(c)|ASCII(b)|ASCII(a)|ASCII(rand())
*/
Coordinate to_point(string str);
/*
* Returns a point on the EC, with an appropriate padding.
*/
Coordinate get_point_with_padding(mpz_class str, int padding_length);
void print_keypair() {
cout << "private key: " << get_private_key() << endl;
cout << "public key: " << get_public_key().X << "," << get_public_key().Y << endl;
}
string remove_padding(ECC_ElGamal_Plaintext& ep);
int get_max_point_length();
Ellipticcurve* ell;
bool validate_curve();
private:
Coordinate Q;
mpz_class d;
RandomNumberGenerator rand;
string to_string(mpz_class mpz);
/*
* Max. length of a point in bytes
*/
int max_point_length;
};
#endif /* _ELGAMAL_H */