summaryrefslogtreecommitdiff
path: root/frontend/beta/js/Clipperz/Crypto/SRP.js
Unidiff
Diffstat (limited to 'frontend/beta/js/Clipperz/Crypto/SRP.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/beta/js/Clipperz/Crypto/SRP.js331
1 files changed, 331 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/SRP.js b/frontend/beta/js/Clipperz/Crypto/SRP.js
new file mode 100644
index 0000000..0eef6ec
--- a/dev/null
+++ b/frontend/beta/js/Clipperz/Crypto/SRP.js
@@ -0,0 +1,331 @@
1/*
2
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz's Javascript Crypto Library.
6Javascript Crypto Library provides web developers with an extensive
7and efficient set of cryptographic functions. The library aims to
8obtain maximum execution speed while preserving modularity and
9reusability.
10For further information about its features and functionalities please
11refer to http://www.clipperz.com
12
13* Javascript Crypto Library is free software: you can redistribute
14 it and/or modify it under the terms of the GNU Affero General Public
15 License as published by the Free Software Foundation, either version
16 3 of the License, or (at your option) any later version.
17
18* Javascript Crypto Library is distributed in the hope that it will
19 be useful, but WITHOUT ANY WARRANTY; without even the implied
20 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU Affero General Public License for more details.
22
23* You should have received a copy of the GNU Affero General Public
24 License along with Javascript Crypto Library. If not, see
25 <http://www.gnu.org/licenses/>.
26
27*/
28
29try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) {
30 throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!";
31}
32
33try { if (typeof(Clipperz.Crypto.BigInt) == 'undefined') { throw ""; }} catch (e) {
34 throw "Clipperz.Crypto.SRP depends on Clipperz.Crypto.BigInt!";
35}
36
37try { if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { throw ""; }} catch (e) {
38 throw "Clipperz.Crypto.SRP depends on Clipperz.Crypto.PRNG!";
39}
40
41if (typeof(Clipperz.Crypto.SRP) == 'undefined') { Clipperz.Crypto.SRP = {}; }
42
43Clipperz.Crypto.SRP.VERSION = "0.1";
44Clipperz.Crypto.SRP.NAME = "Clipperz.Crypto.SRP";
45
46//#############################################################################
47
48MochiKit.Base.update(Clipperz.Crypto.SRP, {
49
50 '_n': null,
51 '_g': null,
52 //-------------------------------------------------------------------------
53
54 'n': function() {
55 if (Clipperz.Crypto.SRP._n == null) {
56 Clipperz.Crypto.SRP._n = new Clipperz.Crypto.BigInt("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3", 16);
57 }
58
59 return Clipperz.Crypto.SRP._n;
60 },
61
62 //-------------------------------------------------------------------------
63
64 'g': function() {
65 if (Clipperz.Crypto.SRP._g == null) {
66 Clipperz.Crypto.SRP._g = new Clipperz.Crypto.BigInt(2); //eventually 5 (as suggested on the Diffi-Helmann documentation)
67 }
68
69 return Clipperz.Crypto.SRP._g;
70 },
71
72 //-----------------------------------------------------------------------------
73
74 'exception': {
75 'InvalidValue': new MochiKit.Base.NamedError("Clipperz.Crypto.SRP.exception.InvalidValue")
76 },
77
78 //-------------------------------------------------------------------------
79 __syntaxFix__: "syntax fix"
80
81});
82
83//#############################################################################
84//
85 // S R P C o n n e c t i o n version 1.0
86//
87//=============================================================================
88Clipperz.Crypto.SRP.Connection = function (args) {
89 args = args || {};
90
91 this._C = args.C;
92 this._P = args.P;
93 this.hash = args.hash;
94
95 this._a = null;
96 this._A = null;
97
98 this._s = null;
99 this._B = null;
100
101 this._x = null;
102
103 this._u = null;
104 this._K = null;
105 this._M1 = null;
106 this._M2 = null;
107
108 this._sessionKey = null;
109
110 return this;
111}
112
113Clipperz.Crypto.SRP.Connection.prototype = MochiKit.Base.update(null, {
114
115 'toString': function () {
116 return "Clipperz.Crypto.SRP.Connection (username: " + this.username() + "). Status: " + this.statusDescription();
117 },
118
119 //-------------------------------------------------------------------------
120
121 'C': function () {
122 return this._C;
123 },
124
125 //-------------------------------------------------------------------------
126
127 'P': function () {
128 return this._P;
129 },
130
131 //-------------------------------------------------------------------------
132
133 'a': function () {
134 if (this._a == null) {
135 this._a = new Clipperz.Crypto.BigInt(Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2), 16);
136 // this._a = new Clipperz.Crypto.BigInt("37532428169486597638072888476611365392249575518156687476805936694442691012367", 10);
137//MochiKit.Logging.logDebug("SRP a: " + this._a);
138 }
139
140 return this._a;
141 },
142
143 //-------------------------------------------------------------------------
144
145 'A': function () {
146 if (this._A == null) {
147 //Warning: this value should be strictly greater than zero: how should we perform this check?
148 this._A = Clipperz.Crypto.SRP.g().powerModule(this.a(), Clipperz.Crypto.SRP.n());
149
150 if (this._A.equals(0)) {
151MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'A' to 0.");
152 throw Clipperz.Crypto.SRP.exception.InvalidValue;
153 }
154//MochiKit.Logging.logDebug("SRP A: " + this._A);
155 }
156
157 return this._A;
158 },
159
160 //-------------------------------------------------------------------------
161
162 's': function () {
163 return this._s;
164//MochiKit.Logging.logDebug("SRP s: " + this._S);
165 },
166
167 'set_s': function(aValue) {
168 this._s = aValue;
169 },
170
171 //-------------------------------------------------------------------------
172
173 'B': function () {
174 return this._B;
175 },
176
177 'set_B': function(aValue) {
178 //Warning: this value should be strictly greater than zero: how should we perform this check?
179 if (! aValue.equals(0)) {
180 this._B = aValue;
181//MochiKit.Logging.logDebug("SRP B: " + this._B);
182 } else {
183MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to 0.");
184 throw Clipperz.Crypto.SRP.exception.InvalidValue;
185 }
186 },
187
188 //-------------------------------------------------------------------------
189
190 'x': function () {
191 if (this._x == null) {
192 this._x = new Clipperz.Crypto.BigInt(this.stringHash(this.s().asString(16, 64) + this.P()), 16);
193//MochiKit.Logging.logDebug("SRP x: " + this._x);
194 }
195
196 return this._x;
197 },
198
199 //-------------------------------------------------------------------------
200
201 'u': function () {
202 if (this._u == null) {
203 this._u = new Clipperz.Crypto.BigInt(this.stringHash(this.B().asString()), 16);
204//MochiKit.Logging.logDebug("SRP u: " + this._u);
205 }
206
207 return this._u;
208 },
209
210 //-------------------------------------------------------------------------
211
212 'S': function () {
213 if (this._S == null) {
214 var bigint;
215 varsrp;
216
217 bigint = Clipperz.Crypto.BigInt;
218 srp = Clipperz.Crypto.SRP;
219
220 this._S =bigint.powerModule(
221 bigint.subtract(this.B(), bigint.powerModule(srp.g(), this.x(), srp.n())),
222 bigint.add(this.a(), bigint.multiply(this.u(), this.x())),
223 srp.n()
224 )
225//MochiKit.Logging.logDebug("SRP S: " + this._S);
226 }
227
228 return this._S;
229 },
230
231 //-------------------------------------------------------------------------
232
233 'K': function () {
234 if (this._K == null) {
235 this._K = this.stringHash(this.S().asString());
236//MochiKit.Logging.logDebug("SRP K: " + this._K);
237 }
238
239 return this._K;
240 },
241
242 //-------------------------------------------------------------------------
243
244 'M1': function () {
245 if (this._M1 == null) {
246 this._M1 = this.stringHash(this.A().asString(10) + this.B().asString(10) + this.K());
247//MochiKit.Logging.logDebug("SRP M1: " + this._M1);
248 }
249
250 return this._M1;
251 },
252
253 //-------------------------------------------------------------------------
254
255 'M2': function () {
256 if (this._M2 == null) {
257 this._M2 = this.stringHash(this.A().asString(10) + this.M1() + this.K());
258//MochiKit.Logging.logDebug("SRP M2: " + this._M2);
259 }
260
261 return this._M2;
262 },
263
264 //=========================================================================
265
266 'serverSideCredentialsWithSalt': function(aSalt) {
267 var result;
268 var s, x, v;
269
270 s = aSalt;
271 x = this.stringHash(s + this.P());
272 v = Clipperz.Crypto.SRP.g().powerModule(new Clipperz.Crypto.BigInt(x, 16), Clipperz.Crypto.SRP.n());
273
274 result = {};
275 result['C'] = this.C();
276 result['s'] = s;
277 result['v'] = v.asString(16);
278
279 return result;
280 },
281
282 'serverSideCredentials': function() {
283 var result;
284 var s;
285
286 s = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2);
287
288 result = this.serverSideCredentialsWithSalt(s);
289
290 return result;
291 },
292
293 //=========================================================================
294/*
295 'computeServerSide_S': function(b) {
296 var result;
297 var v;
298 var bigint;
299 varsrp;
300
301 bigint = Clipperz.Crypto.BigInt;
302 srp = Clipperz.Crypto.SRP;
303
304 v = new Clipperz.Crypto.BigInt(srpConnection.serverSideCredentialsWithSalt(this.s().asString(16, 64)).v, 16);
305 // _S = (this.A().multiply(this.v().modPow(this.u(), this.n()))).modPow(this.b(), this.n());
306 result = bigint.powerModule(
307 bigint.multiply(
308 this.A(),
309 bigint.powerModule(v, this.u(), srp.n())
310 ), new Clipperz.Crypto.BigInt(b, 10), srp.n()
311 );
312
313 return result;
314 },
315*/
316 //=========================================================================
317
318 'stringHash': function(aValue) {
319 varresult;
320
321 result = this.hash(new Clipperz.ByteArray(aValue)).toHexString().substring(2);
322
323 return result;
324 },
325
326 //=========================================================================
327 __syntaxFix__: "syntax fix"
328
329});
330
331//#############################################################################