This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ntrutest.html
222 lines (194 loc) · 8.93 KB
/
ntrutest.html
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html lang="hu">
<head>
<!--
/*
* Anna Dorottya Simon, Márk Szabó
* Neptun-ID: R48G73, EMX74N
* Applied cryptography project - a postquantum messenger application
* January 2017
* This solution was submitted and prepared by Anna Dorottya Simon(R48G73), Márk Szabó(EMX74N) for the project assignment of the Applied cryptography project seminar course.
* We declare that this solution is our own work.
* We have put the necessary references wherever we have used bigger and/or complicated external codes in our project. For shorter code snippets (usually from Stack Overflow) we have put the reference there in most cases.
* Given the uniqueness of the project (no other student had, have or will have the same project) we have published our code on GitHub with the permission of our professors.
* Students’ regulation of Eötvös Loránd University (ELTE Regulations Vol. II. 74/C. § ) states that as long as a student presents another student’s work - or at least the significant part of it - as his/her own performance, it will count as a disciplinary fault. The most serious consequence of a disciplinary fault can be dismissal of the student from the University.
*/
-->
<title>PostQ</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Ubuntu">
<!-- Basic external JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!-- Additional external JS -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/2.1.0/jquery.scrollTo.min.js"></script>
<script src="external/scrypt.js" type="text/javascript"></script>
<!-- Dependencies of scrypt-->
<script src="external/setImmediate.js" type="text/javascript"></script>
<script src="external/buffer.js" type="text/javascript"></script>
<script src="external/unorm.js" type="text/javascript"></script>
<script src="external/aes.js" type="text/javascript"></script>
<script src="external/secure-random.js" type="text/javascript"></script>
<script src="external/jquery.csv.min.js" type="text/javascript"></script>
<script src="external/sha512.min.js" type="text/javascript"></script>
<script src="polynomial.js" type="text/javascript"></script>
<!-- Custom JS -->
<script src="postq.js" type="text/javascript"></script>
<script src="ntru.js" type="text/javascript"></script>
<script src="signin.js" type="text/javascript"></script>
<script src="aes.js" type="text/javascript"></script>
<script src="addFriend.js" type="text/javascript"></script>
<script src="handleFriendRequests.js" type="text/javascript"></script>
<script src="getFriendList.js" type="text/javascript"></script>
<script src="show.js" type="text/javascript"></script>
<script type="text/javascript">
var privatekey;
var publickey;
function runTests() {
polyTest();
keygenTest();
prepare();
}
function testKeyExchange() {
for(var i=0; i<10; i++)
keyExchange();
}
function polyTest() {
logToScreen("<h2>The test of the underlying truncated polynomial rings started</h2>");
Polynomial.setField("Z2");
var a = new Polynomial("2-x+3x^2");
var b = new Polynomial("1+2x-x^2");
if(a.add(b).toString() != new Polynomial("1+x").toString())
logToScreen("Polytest 1 failed!");
else
logToScreen("Polytest 1 passed");
Polynomial.setField("Z11");
var a = new Polynomial("2-x+3x^2");
var b = new Polynomial("1+2x-x^2");
if(a.add(b).toString() != new Polynomial("3+x+2x^2").toString())
logToScreen("Polytest 2 failed!");
else
logToScreen("Polytest 2 passed");
Polynomial.setField("Z503/x^3-1");
var a = new Polynomial("2-x+3x^2");
var b = new Polynomial("1+2x-x^2");
if(a.add(b).toString() != new Polynomial("3+x+2x^2").toString())
logToScreen("Polytest 3.1 failed!");
else
logToScreen("Polytest 3.1 passed");
if(a.mul(b).toString() != new Polynomial("9-x^2").add("0").toString()) //add('0') is needed to clac the mod (to remove the negative sign)
logToScreen("Polytest 3.2 failed!");
else
logToScreen("Polytest 3.2 passed");
Polynomial.setField("Z503/x^7-1");
var a = new Polynomial("3+2x^2-3x^4+x^6");
var b = new Polynomial("1-3x+x^2+2x^5-x^6");
if(a.add(b).toString() != new Polynomial("4-3x+3x^2-3x^4+2x^5").add("0").toString())
logToScreen("Polytest 4.1 failed!");
else
logToScreen("Polytest 4.1 passed");
if(a.mul(b).toString() != new Polynomial("4-10x-x^2-3x^3+x^4+14x^5-5x^6").add("0").toString()) //add('0') is needed to clac the mod (to remove the negative sign)
logToScreen("Polytest 4.2 failed!");
else
logToScreen("Polytest 4.2 passed");
Polynomial.setField("Z11");
if(polyModInverse(new Polynomial("3+2x^2-3x^4+x^6"), new Polynomial("x^7-1")).toString() != new Polynomial("-2+4x+2x^2+4x^3-4x^4+2x^5-2x^6").add("0").toString())
logToScreen("Polytest 5 failed!");
else
logToScreen("Polytest 5 passed");
Polynomial.setField("Z3");
if(polyModInverse(new Polynomial("-1+x+x^2-x^4+x^6+x^9-x^10"), new Polynomial("x^11-1")).toString() != new Polynomial("2x^9+x^8+2x^7+x^5+2x^4+2x^3+2x+1").add("0").toString())
logToScreen("Polytest 6 failed!");
else
logToScreen("Polytest 6 passed");
Polynomial.setField("Z3");
if(polyModInverse(new Polynomial("x^2+1"), new Polynomial("x^3+2x+1")).toString() != new Polynomial("2x^2+x+2").add("0").toString())
logToScreen("Polytest 7 failed!");
else
logToScreen("Polytest 7 passed");
/* Polynomial.setField("Z32");
if(polyModInverse(new Polynomial("-1+x+x^2-x^4+x^6+x^9-x^10"), new Polynomial("x^11-1")).toString() != new Polynomial("5+9x+6x^2+16x^3+4x^4+15x^5+16x^6+22x^7+20x^8+18x^9+30x^10").add("0").toString())
logToScreen("Polytest x failed!"); // polyModInverse fails, because 32 is not a prime */
Polynomial.setField("Z9829"); //NTRU Prime
var originl = new Polynomial("x^2+1");
var invrs = polyModInverse(originl, new Polynomial("x^739-x-1"));
Polynomial.setField("Z9829/x^739-x-1");
if(invrs.mul(originl).toString() != new Polynomial("1").add("0").toString())
logToScreen("Polytest 8 failed!");
else
logToScreen("Polytest 8 passed");
Polynomial.setField("Z9829"); //NTRU Prime
var originl = new Polynomial("x^532+354*x^325+62*x^289+1");
var invrs = polyModInverse(originl, new Polynomial("x^739-x-1"));
Polynomial.setField("Z9829/x^739-x-1");
if(invrs.mul(originl).toString() != new Polynomial("1").add("0").toString())
logToScreen("Polytest 9 failed!");
else
logToScreen("Polytest 9 passed");
}
function keygenTest() {
logToScreen("<h2>Key generation tests started</h2>");
logToScreen("<h3>Testing g</h3>");
var gg = genG();
var originl = new Polynomial(gg[0]);
var invrs = new Polynomial(gg[1]);
Polynomial.setField(R3);
if(invrs.mul(originl).toString() != new Polynomial("1").add("0").toString())
logToScreen("KeygenTest - genG test failed!");
else
logToScreen("KeygenTest - genG test passed");
}
function prepare() {
logToScreen("<h2>Prepare started</h2>");
var decryptionkey = secureRandom(16); //originally the first 16 character of the hash of the password, but it will do here
generateNTRUKeys(decryptionkey, function(keys) {
privatekey = keys[0];
var encprivate = AESencryptCBC_txt(privatekey, decryptionkey)
publickey = keys[1];
logToScreen("<b>The following keys have been generated:</b>");
logToScreen("<b>Decryptionkey: </b>" + decryptionkey.toString());
logToScreen("<b>Encrypted privatekey: </b>" + encprivate);
logToScreen("<b>Decrypted privatekey: </b>" + privatekey);
logToScreen("<b>Publickey: </b>" + publickey);
testKeyExchange();
});
}
function keyExchange() {
logToScreen("<h2>Keyexchange test started</h2>");
var encps = NTRUEncapsulate(publickey);
var sessionKey1 = encps[0];
var cipher = encps[1];
var sessionKey2 = NTRUDecapsulate(cipher, privatekey);
if(!arraysEqual(sessionKey1, sessionKey2)) {
logToScreen("<b>Test failed!</b> " + sessionKey1 + " != " + sessionKey2);
} else {
logToScreen("Test passed.")
}
}
function logToScreen(text) {
$('body').append(text + "<br>");
}
function randomString(length) { // Source: http://stackoverflow.com/a/1349426
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function arraysEqual(arr1, arr2) { //Source: http://stackoverflow.com/a/4025958
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;) {
if(arr1[i] !== arr2[i])
return false;
}
return true;
}
</script>
</head>
<body onload="runTests()">
</body>
</html>