author | Michael Krelin <hacker@klever.net> | 2014-06-30 18:20:13 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2014-06-30 18:20:13 (UTC) |
commit | c392fe28606eefa0c814e5c25d641f5ffe623186 (patch) (unidiff) | |
tree | da03fe13ca09fadbebbad9b5d38750757270bae8 /frontend/beta/js/Clipperz/Crypto | |
parent | d341307d346dee62ee36b27f0f93b8f000748a96 (diff) | |
parent | 6dd16d9359e3a4dc306802588b09acd43947a606 (diff) | |
download | clipperz-c392fe28606eefa0c814e5c25d641f5ffe623186.zip clipperz-c392fe28606eefa0c814e5c25d641f5ffe623186.tar.gz clipperz-c392fe28606eefa0c814e5c25d641f5ffe623186.tar.bz2 |
Merge remote-tracking branch 'github/master' into nmaster
Diffstat (limited to 'frontend/beta/js/Clipperz/Crypto') (more/less context) (show whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/PRNG.js | 126 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/SRP.js | 57 |
2 files changed, 79 insertions, 104 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/PRNG.js b/frontend/beta/js/Clipperz/Crypto/PRNG.js index b5c3f8a..6fdeca4 100644 --- a/frontend/beta/js/Clipperz/Crypto/PRNG.js +++ b/frontend/beta/js/Clipperz/Crypto/PRNG.js | |||
@@ -198,10 +198,4 @@ Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new C | |||
198 | 198 | ||
199 | //------------------------------------------------------------------------- | 199 | //------------------------------------------------------------------------- |
200 | |||
201 | 'pollingFrequency': function() { | ||
202 | return 10; | ||
203 | }, | ||
204 | |||
205 | //------------------------------------------------------------------------- | ||
206 | __syntaxFix__: "syntax fix" | 200 | __syntaxFix__: "syntax fix" |
207 | }); | 201 | }); |
@@ -246,10 +240,10 @@ Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new | |||
246 | 240 | ||
247 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | 241 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); |
248 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | 242 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); |
249 | this.setRandomBitsCollector(collectetBits); | 243 | this.setRandomBitsCollector(collectedBits); |
250 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); | 244 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); |
251 | 245 | ||
252 | if (numberOfRandomBitsCollected == 8) { | 246 | if (numberOfRandomBitsCollected == 8) { |
253 | this.updateGeneratorWithValue(collectetBits); | 247 | this.updateGeneratorWithValue(collectedBits); |
254 | numberOfRandomBitsCollected = 0; | 248 | numberOfRandomBitsCollected = 0; |
255 | this.setRandomBitsCollector(0); | 249 | this.setRandomBitsCollector(0); |
@@ -290,10 +284,4 @@ Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new | |||
290 | 284 | ||
291 | //------------------------------------------------------------------------- | 285 | //------------------------------------------------------------------------- |
292 | |||
293 | 'pollingFrequency': function() { | ||
294 | return 10; | ||
295 | }, | ||
296 | |||
297 | //------------------------------------------------------------------------- | ||
298 | __syntaxFix__: "syntax fix" | 286 | __syntaxFix__: "syntax fix" |
299 | }); | 287 | }); |
@@ -301,80 +289,44 @@ Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new | |||
301 | //***************************************************************************** | 289 | //***************************************************************************** |
302 | 290 | ||
303 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource = function(args) { | 291 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { |
304 | args = args || {}; | 292 | args = args || {}; |
305 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | ||
306 | 293 | ||
307 | this._randomBitsCollector = 0; | 294 | this._intervalTime = args.intervalTime || 1000; |
308 | this._numberOfRandomBitsCollected = 0; | 295 | this._browserCrypto = args.browserCrypto; |
309 | 296 | ||
310 | MochiKit.Signal.connect(document, 'onkeypress', this, 'collectEntropy'); | 297 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
311 | 298 | ||
299 | this.collectEntropy(); | ||
312 | return this; | 300 | return this; |
313 | } | 301 | } |
314 | 302 | ||
315 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 303 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
316 | |||
317 | //------------------------------------------------------------------------- | ||
318 | |||
319 | 'randomBitsCollector': function() { | ||
320 | return this._randomBitsCollector; | ||
321 | }, | ||
322 | |||
323 | 'setRandomBitsCollector': function(aValue) { | ||
324 | this._randomBitsCollector = aValue; | ||
325 | }, | ||
326 | |||
327 | 'appendRandomBitToRandomBitsCollector': function(aValue) { | ||
328 | var collectedBits; | ||
329 | var numberOfRandomBitsCollected; | ||
330 | |||
331 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | ||
332 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | ||
333 | this.setRandomBitsCollector(collectetBits); | ||
334 | numberOfRandomBitsCollected ++; | ||
335 | |||
336 | if (numberOfRandomBitsCollected == 8) { | ||
337 | this.updateGeneratorWithValue(collectetBits); | ||
338 | numberOfRandomBitsCollected = 0; | ||
339 | this.setRandomBitsCollector(0); | ||
340 | } | ||
341 | |||
342 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | ||
343 | }, | ||
344 | |||
345 | //------------------------------------------------------------------------- | ||
346 | 304 | ||
347 | 'numberOfRandomBitsCollected': function() { | 305 | 'intervalTime': function() { |
348 | return this._numberOfRandomBitsCollected; | 306 | return this._intervalTime; |
349 | }, | 307 | }, |
350 | 308 | ||
351 | 'setNumberOfRandomBitsCollected': function(aValue) { | 309 | 'browserCrypto': function () { |
352 | this._numberOfRandomBitsCollected = aValue; | 310 | return this._browserCrypto; |
353 | }, | 311 | }, |
354 | 312 | ||
355 | //------------------------------------------------------------------------- | 313 | //------------------------------------------------------------------------- |
356 | 314 | ||
357 | 'collectEntropy': function(anEvent) { | 315 | 'collectEntropy': function() { |
358 | /* | 316 | varbytesToCollect; |
359 | var mouseLocation; | ||
360 | var randomBit; | ||
361 | |||
362 | mouseLocation = anEvent.mouse().client; | ||
363 | |||
364 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & 0x1); | ||
365 | this.appendRandomBitToRandomBitsCollector(randomBit); | ||
366 | */ | ||
367 | }, | ||
368 | |||
369 | //------------------------------------------------------------------------- | ||
370 | 317 | ||
371 | 'numberOfRandomBits': function() { | 318 | if (this.boostMode() == true) { |
372 | return 1; | 319 | bytesToCollect = 64; |
373 | }, | 320 | } else { |
321 | bytesToCollect = 8; | ||
322 | } | ||
374 | 323 | ||
375 | //------------------------------------------------------------------------- | 324 | var randomValuesArray = new Uint8Array(bytesToCollect); |
325 | this.browserCrypto().getRandomValues(randomValuesArray); | ||
326 | for (var i = 0; i < randomValuesArray.length; i++) { | ||
327 | this.updateGeneratorWithValue(randomValuesArray[i]); | ||
328 | } | ||
376 | 329 | ||
377 | 'pollingFrequency': function() { | 330 | setTimeout(this.collectEntropy, this.intervalTime()); |
378 | return 10; | ||
379 | }, | 331 | }, |
380 | 332 | ||
@@ -608,11 +560,8 @@ MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | |||
608 | var result; | 560 | var result; |
609 | 561 | ||
610 | //MochiKit.Logging.logDebug(">>> PRNG.deferredEntropyCollection"); | ||
611 | 562 | ||
612 | if (this.isReadyToGenerateRandomValues()) { | 563 | if (this.isReadyToGenerateRandomValues()) { |
613 | //MochiKit.Logging.logDebug("--- PRNG.deferredEntropyCollection - 1"); | ||
614 | result = aValue; | 564 | result = aValue; |
615 | } else { | 565 | } else { |
616 | //MochiKit.Logging.logDebug("--- PRNG.deferredEntropyCollection - 2"); | ||
617 | var deferredResult; | 566 | var deferredResult; |
618 | 567 | ||
@@ -620,7 +569,5 @@ MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | |||
620 | 569 | ||
621 | deferredResult = new MochiKit.Async.Deferred(); | 570 | deferredResult = new MochiKit.Async.Deferred(); |
622 | // deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("1.2.1 - PRNG.deferredEntropyCollection - 1: " + res); return res;}); | ||
623 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); | 571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); |
624 | // deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("1.2.2 - PRNG.deferredEntropyCollection - 2: " + res); return res;}); | ||
625 | MochiKit.Signal.connect(this, | 572 | MochiKit.Signal.connect(this, |
626 | 'readyToGenerateRandomBytes', | 573 | 'readyToGenerateRandomBytes', |
@@ -630,5 +577,4 @@ MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | |||
630 | result = deferredResult; | 577 | result = deferredResult; |
631 | } | 578 | } |
632 | //MochiKit.Logging.logDebug("<<< PRNG.deferredEntropyCollection - result: " + result); | ||
633 | 579 | ||
634 | return result; | 580 | return result; |
@@ -644,5 +590,5 @@ MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | |||
644 | 590 | ||
645 | //------------------------------------------------------------------------- | 591 | //------------------------------------------------------------------------- |
646 | 592 | /* | |
647 | 'dump': function(appendToDoc) { | 593 | 'dump': function(appendToDoc) { |
648 | var tbl; | 594 | var tbl; |
@@ -750,5 +696,5 @@ MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | |||
750 | return tbl; | 696 | return tbl; |
751 | }, | 697 | }, |
752 | 698 | */ | |
753 | //----------------------------------------------------------------------------- | 699 | //----------------------------------------------------------------------------- |
754 | __syntaxFix__: "syntax fix" | 700 | __syntaxFix__: "syntax fix" |
@@ -825,14 +771,24 @@ Clipperz.Crypto.PRNG.defaultRandomGenerator = function() { | |||
825 | //............................................................. | 771 | //............................................................. |
826 | // | 772 | // |
827 | // KeyboardRandomnessSource | 773 | // CryptoRandomRandomnessSource |
828 | // | 774 | // |
829 | //............................................................. | 775 | //............................................................. |
830 | { | 776 | { |
831 | varnewRandomnessSource; | 777 | varnewRandomnessSource; |
778 | varbrowserCrypto; | ||
832 | 779 | ||
833 | newRandomnessSource = new Clipperz.Crypto.PRNG.KeyboardRandomnessSource(); | 780 | if (window.crypto && window.crypto.getRandomValues) { |
834 | _clipperz_crypt_prng_defaultPRNG.addRandomnessSource(newRandomnessSource); | 781 | browserCrypto = window.crypto; |
782 | } else if (window.msCrypto && window.msCrypto.getRandomValues) { | ||
783 | browserCrypto = window.msCrypto; | ||
784 | } else { | ||
785 | browserCrypto = null; | ||
835 | } | 786 | } |
836 | 787 | ||
788 | if (browserCrypto != null) { | ||
789 | newRandomnessSource = new Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource({'browserCrypto':browserCrypto}); | ||
790 | _clipperz_crypt_prng_defaultPRNG.addRandomnessSource(newRandomnessSource); | ||
791 | } | ||
792 | } | ||
837 | } | 793 | } |
838 | 794 | ||
diff --git a/frontend/beta/js/Clipperz/Crypto/SRP.js b/frontend/beta/js/Clipperz/Crypto/SRP.js index 8cc80ba..8c522ad 100644 --- a/frontend/beta/js/Clipperz/Crypto/SRP.js +++ b/frontend/beta/js/Clipperz/Crypto/SRP.js | |||
@@ -45,4 +45,6 @@ MochiKit.Base.update(Clipperz.Crypto.SRP, { | |||
45 | '_n': null, | 45 | '_n': null, |
46 | '_g': null, | 46 | '_g': null, |
47 | '_k': null, | ||
48 | |||
47 | //------------------------------------------------------------------------- | 49 | //------------------------------------------------------------------------- |
48 | 50 | ||
@@ -65,4 +67,13 @@ MochiKit.Base.update(Clipperz.Crypto.SRP, { | |||
65 | }, | 67 | }, |
66 | 68 | ||
69 | 'k': function() { | ||
70 | if (Clipperz.Crypto.SRP._k == null) { | ||
71 | // Clipperz.Crypto.SRP._k = new Clipperz.Crypto.BigInt(this.stringHash(this.n().asString() + this.g().asString()), 16); | ||
72 | Clipperz.Crypto.SRP._k = new Clipperz.Crypto.BigInt("64398bff522814e306a97cb9bfc4364b7eed16a8c17c5208a40a2bad2933c8e", 16); | ||
73 | } | ||
74 | |||
75 | return Clipperz.Crypto.SRP._k; | ||
76 | }, | ||
77 | |||
67 | //----------------------------------------------------------------------------- | 78 | //----------------------------------------------------------------------------- |
68 | 79 | ||
@@ -130,5 +141,4 @@ Clipperz.Crypto.SRP.Connection.prototype = MochiKit.Base.update(null, { | |||
130 | this._a = new Clipperz.Crypto.BigInt(Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2), 16); | 141 | this._a = new Clipperz.Crypto.BigInt(Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2), 16); |
131 | // this._a = new Clipperz.Crypto.BigInt("37532428169486597638072888476611365392249575518156687476805936694442691012367", 10); | 142 | // this._a = new Clipperz.Crypto.BigInt("37532428169486597638072888476611365392249575518156687476805936694442691012367", 10); |
132 | //MochiKit.Logging.logDebug("SRP a: " + this._a); | ||
133 | } | 143 | } |
134 | 144 | ||
@@ -140,12 +150,10 @@ Clipperz.Crypto.SRP.Connection.prototype = MochiKit.Base.update(null, { | |||
140 | 'A': function () { | 150 | 'A': function () { |
141 | if (this._A == null) { | 151 | if (this._A == null) { |
142 | //Warning: this value should be strictly greater than zero: how should we perform this check? | 152 | //Warning: this value should be strictly greater than zero |
143 | this._A = Clipperz.Crypto.SRP.g().powerModule(this.a(), Clipperz.Crypto.SRP.n()); | 153 | this._A = Clipperz.Crypto.SRP.g().powerModule(this.a(), Clipperz.Crypto.SRP.n()); |
144 | 154 | if (this._A.equals(0) || negative(this._A)) { | |
145 | if (this._A.equals(0)) { | ||
146 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'A' to 0."); | 155 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'A' to 0."); |
147 | throw Clipperz.Crypto.SRP.exception.InvalidValue; | 156 | throw Clipperz.Crypto.SRP.exception.InvalidValue; |
148 | } | 157 | } |
149 | //MochiKit.Logging.logDebug("SRP A: " + this._A); | ||
150 | } | 158 | } |
151 | 159 | ||
@@ -157,5 +165,4 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'A' to | |||
157 | 's': function () { | 165 | 's': function () { |
158 | return this._s; | 166 | return this._s; |
159 | //MochiKit.Logging.logDebug("SRP s: " + this._S); | ||
160 | }, | 167 | }, |
161 | 168 | ||
@@ -171,9 +178,7 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'A' to | |||
171 | 178 | ||
172 | 'set_B': function(aValue) { | 179 | 'set_B': function(aValue) { |
173 | //Warning: this value should be strictly greater than zero: how should we perform this check? | 180 | //Warning: this value should be strictly greater than zero |
174 | if (! aValue.equals(0)) { | ||
175 | this._B = aValue; | 181 | this._B = aValue; |
176 | //MochiKit.Logging.logDebug("SRP B: " + this._B); | 182 | if (this._B.equals(0) || negative(this._B)) { |
177 | } else { | ||
178 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to 0."); | 183 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to 0."); |
179 | throw Clipperz.Crypto.SRP.exception.InvalidValue; | 184 | throw Clipperz.Crypto.SRP.exception.InvalidValue; |
@@ -186,5 +191,4 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to | |||
186 | if (this._x == null) { | 191 | if (this._x == null) { |
187 | this._x = new Clipperz.Crypto.BigInt(this.stringHash(this.s().asString(16, 64) + this.P()), 16); | 192 | this._x = new Clipperz.Crypto.BigInt(this.stringHash(this.s().asString(16, 64) + this.P()), 16); |
188 | //MochiKit.Logging.logDebug("SRP x: " + this._x); | ||
189 | } | 193 | } |
190 | 194 | ||
@@ -196,6 +200,5 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to | |||
196 | 'u': function () { | 200 | 'u': function () { |
197 | if (this._u == null) { | 201 | if (this._u == null) { |
198 | this._u = new Clipperz.Crypto.BigInt(this.stringHash(this.B().asString()), 16); | 202 | this._u = new Clipperz.Crypto.BigInt(this.stringHash(this.A().asString() + this.B().asString()), 16); |
199 | //MochiKit.Logging.logDebug("SRP u: " + this._u); | ||
200 | } | 203 | } |
201 | 204 | ||
@@ -214,9 +217,14 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to | |||
214 | 217 | ||
215 | this._S =bigint.powerModule( | 218 | this._S =bigint.powerModule( |
216 | bigint.subtract(this.B(), bigint.powerModule(srp.g(), this.x(), srp.n())), | 219 | bigint.subtract( |
220 | this.B(), | ||
221 | bigint.multiply( | ||
222 | Clipperz.Crypto.SRP.k(), | ||
223 | bigint.powerModule(srp.g(), this.x(), srp.n()) | ||
224 | ) | ||
225 | ), | ||
217 | bigint.add(this.a(), bigint.multiply(this.u(), this.x())), | 226 | bigint.add(this.a(), bigint.multiply(this.u(), this.x())), |
218 | srp.n() | 227 | srp.n() |
219 | ) | 228 | ) |
220 | //MochiKit.Logging.logDebug("SRP S: " + this._S); | ||
221 | } | 229 | } |
222 | 230 | ||
@@ -229,5 +237,4 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to | |||
229 | if (this._K == null) { | 237 | if (this._K == null) { |
230 | this._K = this.stringHash(this.S().asString()); | 238 | this._K = this.stringHash(this.S().asString()); |
231 | //MochiKit.Logging.logDebug("SRP K: " + this._K); | ||
232 | } | 239 | } |
233 | 240 | ||
@@ -239,6 +246,18 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to | |||
239 | 'M1': function () { | 246 | 'M1': function () { |
240 | if (this._M1 == null) { | 247 | if (this._M1 == null) { |
241 | this._M1 = this.stringHash(this.A().asString(10) + this.B().asString(10) + this.K()); | 248 | // this._M1 = this.stringHash(this.A().asString(10) + this.B().asString(10) + this.K()); |
242 | //MochiKit.Logging.logDebug("SRP M1: " + this._M1); | 249 | |
250 | //http://srp.stanford.edu/design.html | ||
251 | //User -> Host: M = H(H(N) xor H(g), H(I), s, A, B, K) | ||
252 | |||
253 | this._M1 = this.stringHash( | ||
254 | "597626870978286801440197562148588907434001483655788865609375806439877501869636875571920406529" + | ||
255 | this.stringHash(this.C()) + | ||
256 | this.s().asString() + | ||
257 | this.A().asString() + | ||
258 | this.B().asString() + | ||
259 | this.K() | ||
260 | ); | ||
261 | //console.log("M1", this._M1); | ||
243 | } | 262 | } |
244 | 263 | ||
@@ -251,5 +270,5 @@ MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to | |||
251 | if (this._M2 == null) { | 270 | if (this._M2 == null) { |
252 | this._M2 = this.stringHash(this.A().asString(10) + this.M1() + this.K()); | 271 | this._M2 = this.stringHash(this.A().asString(10) + this.M1() + this.K()); |
253 | //MochiKit.Logging.logDebug("SRP M2: " + this._M2); | 272 | //console.log("M2", this._M2); |
254 | } | 273 | } |
255 | 274 | ||