-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/PRNG.js | 4 | ||||
-rw-r--r-- | frontend/delta/js/Clipperz/Crypto/PRNG.js | 4 | ||||
-rw-r--r-- | frontend/gamma/js/Clipperz/Crypto/PRNG.js | 4 |
3 files changed, 6 insertions, 6 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/PRNG.js b/frontend/beta/js/Clipperz/Crypto/PRNG.js index 92966d0..6fdeca4 100644 --- a/frontend/beta/js/Clipperz/Crypto/PRNG.js +++ b/frontend/beta/js/Clipperz/Crypto/PRNG.js | |||
@@ -1,705 +1,705 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2013 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz, the online password manager. | 5 | This file is part of Clipperz, the online password manager. |
6 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
7 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
8 | 8 | ||
9 | * Clipperz is free software: you can redistribute it and/or modify it | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
10 | under the terms of the GNU Affero General Public License as published | 10 | under the terms of the GNU Affero General Public License as published |
11 | by the Free Software Foundation, either version 3 of the License, or | 11 | by the Free Software Foundation, either version 3 of the License, or |
12 | (at your option) any later version. | 12 | (at your option) any later version. |
13 | 13 | ||
14 | * Clipperz is distributed in the hope that it will be useful, but | 14 | * Clipperz is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
18 | 18 | ||
19 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
21 | 21 | ||
22 | */ | 22 | */ |
23 | 23 | ||
24 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | 24 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { |
25 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; |
26 | } | 26 | } |
27 | 27 | ||
28 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { | 28 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { |
29 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; | 29 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; |
30 | } | 30 | } |
31 | 31 | ||
32 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { | 32 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { |
33 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; | 33 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; |
34 | } | 34 | } |
35 | 35 | ||
36 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } | 36 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } |
37 | 37 | ||
38 | //############################################################################# | 38 | //############################################################################# |
39 | 39 | ||
40 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { | 40 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { |
41 | args = args || {}; | 41 | args = args || {}; |
42 | //MochiKit.Base.bindMethods(this); | 42 | //MochiKit.Base.bindMethods(this); |
43 | 43 | ||
44 | this._stack = new Clipperz.ByteArray(); | 44 | this._stack = new Clipperz.ByteArray(); |
45 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; | 45 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; |
46 | return this; | 46 | return this; |
47 | } | 47 | } |
48 | 48 | ||
49 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { | 49 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { |
50 | 50 | ||
51 | 'toString': function() { | 51 | 'toString': function() { |
52 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; | 52 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; |
53 | }, | 53 | }, |
54 | 54 | ||
55 | //------------------------------------------------------------------------- | 55 | //------------------------------------------------------------------------- |
56 | 56 | ||
57 | 'stack': function() { | 57 | 'stack': function() { |
58 | return this._stack; | 58 | return this._stack; |
59 | }, | 59 | }, |
60 | 60 | ||
61 | 'setStack': function(aValue) { | 61 | 'setStack': function(aValue) { |
62 | this._stack = aValue; | 62 | this._stack = aValue; |
63 | }, | 63 | }, |
64 | 64 | ||
65 | 'resetStack': function() { | 65 | 'resetStack': function() { |
66 | this.stack().reset(); | 66 | this.stack().reset(); |
67 | }, | 67 | }, |
68 | 68 | ||
69 | 'maxStackLengthBeforeHashing': function() { | 69 | 'maxStackLengthBeforeHashing': function() { |
70 | return this._maxStackLengthBeforeHashing; | 70 | return this._maxStackLengthBeforeHashing; |
71 | }, | 71 | }, |
72 | 72 | ||
73 | //------------------------------------------------------------------------- | 73 | //------------------------------------------------------------------------- |
74 | 74 | ||
75 | 'addRandomByte': function(aValue) { | 75 | 'addRandomByte': function(aValue) { |
76 | this.stack().appendByte(aValue); | 76 | this.stack().appendByte(aValue); |
77 | 77 | ||
78 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { | 78 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { |
79 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); | 79 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); |
80 | } | 80 | } |
81 | }, | 81 | }, |
82 | 82 | ||
83 | //------------------------------------------------------------------------- | 83 | //------------------------------------------------------------------------- |
84 | __syntaxFix__: "syntax fix" | 84 | __syntaxFix__: "syntax fix" |
85 | }); | 85 | }); |
86 | 86 | ||
87 | //############################################################################# | 87 | //############################################################################# |
88 | 88 | ||
89 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { | 89 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { |
90 | args = args || {}; | 90 | args = args || {}; |
91 | MochiKit.Base.bindMethods(this); | 91 | MochiKit.Base.bindMethods(this); |
92 | 92 | ||
93 | this._generator = args.generator || null; | 93 | this._generator = args.generator || null; |
94 | this._sourceId = args.sourceId || null; | 94 | this._sourceId = args.sourceId || null; |
95 | this._boostMode = args.boostMode || false; | 95 | this._boostMode = args.boostMode || false; |
96 | 96 | ||
97 | this._nextPoolIndex = 0; | 97 | this._nextPoolIndex = 0; |
98 | 98 | ||
99 | return this; | 99 | return this; |
100 | } | 100 | } |
101 | 101 | ||
102 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { | 102 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { |
103 | 103 | ||
104 | 'generator': function() { | 104 | 'generator': function() { |
105 | return this._generator; | 105 | return this._generator; |
106 | }, | 106 | }, |
107 | 107 | ||
108 | 'setGenerator': function(aValue) { | 108 | 'setGenerator': function(aValue) { |
109 | this._generator = aValue; | 109 | this._generator = aValue; |
110 | }, | 110 | }, |
111 | 111 | ||
112 | //------------------------------------------------------------------------- | 112 | //------------------------------------------------------------------------- |
113 | 113 | ||
114 | 'boostMode': function() { | 114 | 'boostMode': function() { |
115 | return this._boostMode; | 115 | return this._boostMode; |
116 | }, | 116 | }, |
117 | 117 | ||
118 | 'setBoostMode': function(aValue) { | 118 | 'setBoostMode': function(aValue) { |
119 | this._boostMode = aValue; | 119 | this._boostMode = aValue; |
120 | }, | 120 | }, |
121 | 121 | ||
122 | //------------------------------------------------------------------------- | 122 | //------------------------------------------------------------------------- |
123 | 123 | ||
124 | 'sourceId': function() { | 124 | 'sourceId': function() { |
125 | return this._sourceId; | 125 | return this._sourceId; |
126 | }, | 126 | }, |
127 | 127 | ||
128 | 'setSourceId': function(aValue) { | 128 | 'setSourceId': function(aValue) { |
129 | this._sourceId = aValue; | 129 | this._sourceId = aValue; |
130 | }, | 130 | }, |
131 | 131 | ||
132 | //------------------------------------------------------------------------- | 132 | //------------------------------------------------------------------------- |
133 | 133 | ||
134 | 'nextPoolIndex': function() { | 134 | 'nextPoolIndex': function() { |
135 | return this._nextPoolIndex; | 135 | return this._nextPoolIndex; |
136 | }, | 136 | }, |
137 | 137 | ||
138 | 'incrementNextPoolIndex': function() { | 138 | 'incrementNextPoolIndex': function() { |
139 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); | 139 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); |
140 | }, | 140 | }, |
141 | 141 | ||
142 | //------------------------------------------------------------------------- | 142 | //------------------------------------------------------------------------- |
143 | 143 | ||
144 | 'updateGeneratorWithValue': function(aRandomValue) { | 144 | 'updateGeneratorWithValue': function(aRandomValue) { |
145 | if (this.generator() != null) { | 145 | if (this.generator() != null) { |
146 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); | 146 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); |
147 | this.incrementNextPoolIndex(); | 147 | this.incrementNextPoolIndex(); |
148 | } | 148 | } |
149 | }, | 149 | }, |
150 | 150 | ||
151 | //------------------------------------------------------------------------- | 151 | //------------------------------------------------------------------------- |
152 | __syntaxFix__: "syntax fix" | 152 | __syntaxFix__: "syntax fix" |
153 | }); | 153 | }); |
154 | 154 | ||
155 | //############################################################################# | 155 | //############################################################################# |
156 | 156 | ||
157 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { | 157 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { |
158 | args = args || {}; | 158 | args = args || {}; |
159 | //MochiKit.Base.bindMethods(this); | 159 | //MochiKit.Base.bindMethods(this); |
160 | 160 | ||
161 | this._intervalTime = args.intervalTime || 1000; | 161 | this._intervalTime = args.intervalTime || 1000; |
162 | 162 | ||
163 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 163 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
164 | 164 | ||
165 | this.collectEntropy(); | 165 | this.collectEntropy(); |
166 | return this; | 166 | return this; |
167 | } | 167 | } |
168 | 168 | ||
169 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 169 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
170 | 170 | ||
171 | 'intervalTime': function() { | 171 | 'intervalTime': function() { |
172 | return this._intervalTime; | 172 | return this._intervalTime; |
173 | }, | 173 | }, |
174 | 174 | ||
175 | //------------------------------------------------------------------------- | 175 | //------------------------------------------------------------------------- |
176 | 176 | ||
177 | 'collectEntropy': function() { | 177 | 'collectEntropy': function() { |
178 | varnow; | 178 | varnow; |
179 | varentropyByte; | 179 | varentropyByte; |
180 | var intervalTime; | 180 | var intervalTime; |
181 | now = new Date(); | 181 | now = new Date(); |
182 | entropyByte = (now.getTime() & 0xff); | 182 | entropyByte = (now.getTime() & 0xff); |
183 | 183 | ||
184 | intervalTime = this.intervalTime(); | 184 | intervalTime = this.intervalTime(); |
185 | if (this.boostMode() == true) { | 185 | if (this.boostMode() == true) { |
186 | intervalTime = intervalTime / 9; | 186 | intervalTime = intervalTime / 9; |
187 | } | 187 | } |
188 | 188 | ||
189 | this.updateGeneratorWithValue(entropyByte); | 189 | this.updateGeneratorWithValue(entropyByte); |
190 | setTimeout(this.collectEntropy, intervalTime); | 190 | setTimeout(this.collectEntropy, intervalTime); |
191 | }, | 191 | }, |
192 | 192 | ||
193 | //------------------------------------------------------------------------- | 193 | //------------------------------------------------------------------------- |
194 | 194 | ||
195 | 'numberOfRandomBits': function() { | 195 | 'numberOfRandomBits': function() { |
196 | return 5; | 196 | return 5; |
197 | }, | 197 | }, |
198 | 198 | ||
199 | //------------------------------------------------------------------------- | 199 | //------------------------------------------------------------------------- |
200 | __syntaxFix__: "syntax fix" | 200 | __syntaxFix__: "syntax fix" |
201 | }); | 201 | }); |
202 | 202 | ||
203 | //***************************************************************************** | 203 | //***************************************************************************** |
204 | 204 | ||
205 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { | 205 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { |
206 | args = args || {}; | 206 | args = args || {}; |
207 | 207 | ||
208 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 208 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
209 | 209 | ||
210 | this._numberOfBitsToCollectAtEachEvent = 4; | 210 | this._numberOfBitsToCollectAtEachEvent = 4; |
211 | this._randomBitsCollector = 0; | 211 | this._randomBitsCollector = 0; |
212 | this._numberOfRandomBitsCollected = 0; | 212 | this._numberOfRandomBitsCollected = 0; |
213 | 213 | ||
214 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); | 214 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); |
215 | 215 | ||
216 | return this; | 216 | return this; |
217 | } | 217 | } |
218 | 218 | ||
219 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 219 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
220 | 220 | ||
221 | //------------------------------------------------------------------------- | 221 | //------------------------------------------------------------------------- |
222 | 222 | ||
223 | 'numberOfBitsToCollectAtEachEvent': function() { | 223 | 'numberOfBitsToCollectAtEachEvent': function() { |
224 | return this._numberOfBitsToCollectAtEachEvent; | 224 | return this._numberOfBitsToCollectAtEachEvent; |
225 | }, | 225 | }, |
226 | 226 | ||
227 | //------------------------------------------------------------------------- | 227 | //------------------------------------------------------------------------- |
228 | 228 | ||
229 | 'randomBitsCollector': function() { | 229 | 'randomBitsCollector': function() { |
230 | return this._randomBitsCollector; | 230 | return this._randomBitsCollector; |
231 | }, | 231 | }, |
232 | 232 | ||
233 | 'setRandomBitsCollector': function(aValue) { | 233 | 'setRandomBitsCollector': function(aValue) { |
234 | this._randomBitsCollector = aValue; | 234 | this._randomBitsCollector = aValue; |
235 | }, | 235 | }, |
236 | 236 | ||
237 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { | 237 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { |
238 | var collectedBits; | 238 | var collectedBits; |
239 | var numberOfRandomBitsCollected; | 239 | var numberOfRandomBitsCollected; |
240 | 240 | ||
241 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | 241 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); |
242 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | 242 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); |
243 | this.setRandomBitsCollector(collectedBits); | 243 | this.setRandomBitsCollector(collectedBits); |
244 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); | 244 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); |
245 | 245 | ||
246 | if (numberOfRandomBitsCollected == 8) { | 246 | if (numberOfRandomBitsCollected == 8) { |
247 | this.updateGeneratorWithValue(collectedBits); | 247 | this.updateGeneratorWithValue(collectedBits); |
248 | numberOfRandomBitsCollected = 0; | 248 | numberOfRandomBitsCollected = 0; |
249 | this.setRandomBitsCollector(0); | 249 | this.setRandomBitsCollector(0); |
250 | } | 250 | } |
251 | 251 | ||
252 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | 252 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) |
253 | }, | 253 | }, |
254 | 254 | ||
255 | //------------------------------------------------------------------------- | 255 | //------------------------------------------------------------------------- |
256 | 256 | ||
257 | 'numberOfRandomBitsCollected': function() { | 257 | 'numberOfRandomBitsCollected': function() { |
258 | return this._numberOfRandomBitsCollected; | 258 | return this._numberOfRandomBitsCollected; |
259 | }, | 259 | }, |
260 | 260 | ||
261 | 'setNumberOfRandomBitsCollected': function(aValue) { | 261 | 'setNumberOfRandomBitsCollected': function(aValue) { |
262 | this._numberOfRandomBitsCollected = aValue; | 262 | this._numberOfRandomBitsCollected = aValue; |
263 | }, | 263 | }, |
264 | 264 | ||
265 | //------------------------------------------------------------------------- | 265 | //------------------------------------------------------------------------- |
266 | 266 | ||
267 | 'collectEntropy': function(anEvent) { | 267 | 'collectEntropy': function(anEvent) { |
268 | var mouseLocation; | 268 | var mouseLocation; |
269 | var randomBit; | 269 | var randomBit; |
270 | var mask; | 270 | var mask; |
271 | 271 | ||
272 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); | 272 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); |
273 | 273 | ||
274 | mouseLocation = anEvent.mouse().client; | 274 | mouseLocation = anEvent.mouse().client; |
275 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); | 275 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); |
276 | this.appendRandomBitsToRandomBitsCollector(randomBit) | 276 | this.appendRandomBitsToRandomBitsCollector(randomBit) |
277 | }, | 277 | }, |
278 | 278 | ||
279 | //------------------------------------------------------------------------- | 279 | //------------------------------------------------------------------------- |
280 | 280 | ||
281 | 'numberOfRandomBits': function() { | 281 | 'numberOfRandomBits': function() { |
282 | return 1; | 282 | return 1; |
283 | }, | 283 | }, |
284 | 284 | ||
285 | //------------------------------------------------------------------------- | 285 | //------------------------------------------------------------------------- |
286 | __syntaxFix__: "syntax fix" | 286 | __syntaxFix__: "syntax fix" |
287 | }); | 287 | }); |
288 | 288 | ||
289 | //***************************************************************************** | 289 | //***************************************************************************** |
290 | 290 | ||
291 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { | 291 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { |
292 | args = args || {}; | 292 | args = args || {}; |
293 | 293 | ||
294 | this._intervalTime = args.intervalTime || 1000; | 294 | this._intervalTime = args.intervalTime || 1000; |
295 | this._browserCrypto = args.browserCrypto; | 295 | this._browserCrypto = args.browserCrypto; |
296 | 296 | ||
297 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 297 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
298 | 298 | ||
299 | this.collectEntropy(); | 299 | this.collectEntropy(); |
300 | return this; | 300 | return this; |
301 | } | 301 | } |
302 | 302 | ||
303 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 303 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
304 | 304 | ||
305 | 'intervalTime': function() { | 305 | 'intervalTime': function() { |
306 | return this._intervalTime; | 306 | return this._intervalTime; |
307 | }, | 307 | }, |
308 | 308 | ||
309 | 'browserCrypto': function () { | 309 | 'browserCrypto': function () { |
310 | return this._browserCrypto; | 310 | return this._browserCrypto; |
311 | }, | 311 | }, |
312 | 312 | ||
313 | //------------------------------------------------------------------------- | 313 | //------------------------------------------------------------------------- |
314 | 314 | ||
315 | 'collectEntropy': function() { | 315 | 'collectEntropy': function() { |
316 | varbytesToCollect; | 316 | varbytesToCollect; |
317 | 317 | ||
318 | if (this.boostMode() == true) { | 318 | if (this.boostMode() == true) { |
319 | bytesToCollect = 8; | 319 | bytesToCollect = 64; |
320 | } else { | 320 | } else { |
321 | bytesToCollect = 32; | 321 | bytesToCollect = 8; |
322 | } | 322 | } |
323 | 323 | ||
324 | var randomValuesArray = new Uint8Array(bytesToCollect); | 324 | var randomValuesArray = new Uint8Array(bytesToCollect); |
325 | this.browserCrypto().getRandomValues(randomValuesArray); | 325 | this.browserCrypto().getRandomValues(randomValuesArray); |
326 | for (var i = 0; i < randomValuesArray.length; i++) { | 326 | for (var i = 0; i < randomValuesArray.length; i++) { |
327 | this.updateGeneratorWithValue(randomValuesArray[i]); | 327 | this.updateGeneratorWithValue(randomValuesArray[i]); |
328 | } | 328 | } |
329 | 329 | ||
330 | setTimeout(this.collectEntropy, this.intervalTime()); | 330 | setTimeout(this.collectEntropy, this.intervalTime()); |
331 | }, | 331 | }, |
332 | 332 | ||
333 | //------------------------------------------------------------------------- | 333 | //------------------------------------------------------------------------- |
334 | __syntaxFix__: "syntax fix" | 334 | __syntaxFix__: "syntax fix" |
335 | }); | 335 | }); |
336 | 336 | ||
337 | //############################################################################# | 337 | //############################################################################# |
338 | 338 | ||
339 | Clipperz.Crypto.PRNG.Fortuna = function(args) { | 339 | Clipperz.Crypto.PRNG.Fortuna = function(args) { |
340 | vari,c; | 340 | vari,c; |
341 | 341 | ||
342 | args = args || {}; | 342 | args = args || {}; |
343 | 343 | ||
344 | this._key = args.seed || null; | 344 | this._key = args.seed || null; |
345 | if (this._key == null) { | 345 | if (this._key == null) { |
346 | this._counter = 0; | 346 | this._counter = 0; |
347 | this._key = new Clipperz.ByteArray(); | 347 | this._key = new Clipperz.ByteArray(); |
348 | } else { | 348 | } else { |
349 | this._counter = 1; | 349 | this._counter = 1; |
350 | } | 350 | } |
351 | 351 | ||
352 | this._aesKey = null; | 352 | this._aesKey = null; |
353 | 353 | ||
354 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; | 354 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; |
355 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; | 355 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; |
356 | 356 | ||
357 | this._accumulators = []; | 357 | this._accumulators = []; |
358 | c = this.numberOfEntropyAccumulators(); | 358 | c = this.numberOfEntropyAccumulators(); |
359 | for (i=0; i<c; i++) { | 359 | for (i=0; i<c; i++) { |
360 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); | 360 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); |
361 | } | 361 | } |
362 | 362 | ||
363 | this._randomnessSources = []; | 363 | this._randomnessSources = []; |
364 | this._reseedCounter = 0; | 364 | this._reseedCounter = 0; |
365 | 365 | ||
366 | return this; | 366 | return this; |
367 | } | 367 | } |
368 | 368 | ||
369 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { | 369 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { |
370 | 370 | ||
371 | 'toString': function() { | 371 | 'toString': function() { |
372 | return "Clipperz.Crypto.PRNG.Fortuna"; | 372 | return "Clipperz.Crypto.PRNG.Fortuna"; |
373 | }, | 373 | }, |
374 | 374 | ||
375 | //------------------------------------------------------------------------- | 375 | //------------------------------------------------------------------------- |
376 | 376 | ||
377 | 'key': function() { | 377 | 'key': function() { |
378 | return this._key; | 378 | return this._key; |
379 | }, | 379 | }, |
380 | 380 | ||
381 | 'setKey': function(aValue) { | 381 | 'setKey': function(aValue) { |
382 | this._key = aValue; | 382 | this._key = aValue; |
383 | this._aesKey = null; | 383 | this._aesKey = null; |
384 | }, | 384 | }, |
385 | 385 | ||
386 | 'aesKey': function() { | 386 | 'aesKey': function() { |
387 | if (this._aesKey == null) { | 387 | if (this._aesKey == null) { |
388 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); | 388 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); |
389 | } | 389 | } |
390 | 390 | ||
391 | return this._aesKey; | 391 | return this._aesKey; |
392 | }, | 392 | }, |
393 | 393 | ||
394 | 'accumulators': function() { | 394 | 'accumulators': function() { |
395 | return this._accumulators; | 395 | return this._accumulators; |
396 | }, | 396 | }, |
397 | 397 | ||
398 | 'firstPoolReseedLevel': function() { | 398 | 'firstPoolReseedLevel': function() { |
399 | return this._firstPoolReseedLevel; | 399 | return this._firstPoolReseedLevel; |
400 | }, | 400 | }, |
401 | 401 | ||
402 | //------------------------------------------------------------------------- | 402 | //------------------------------------------------------------------------- |
403 | 403 | ||
404 | 'reseedCounter': function() { | 404 | 'reseedCounter': function() { |
405 | return this._reseedCounter; | 405 | return this._reseedCounter; |
406 | }, | 406 | }, |
407 | 407 | ||
408 | 'incrementReseedCounter': function() { | 408 | 'incrementReseedCounter': function() { |
409 | this._reseedCounter = this._reseedCounter +1; | 409 | this._reseedCounter = this._reseedCounter +1; |
410 | }, | 410 | }, |
411 | 411 | ||
412 | //------------------------------------------------------------------------- | 412 | //------------------------------------------------------------------------- |
413 | 413 | ||
414 | 'reseed': function() { | 414 | 'reseed': function() { |
415 | varnewKeySeed; | 415 | varnewKeySeed; |
416 | var reseedCounter; | 416 | var reseedCounter; |
417 | varreseedCounterMask; | 417 | varreseedCounterMask; |
418 | var i, c; | 418 | var i, c; |
419 | 419 | ||
420 | newKeySeed = this.key(); | 420 | newKeySeed = this.key(); |
421 | this.incrementReseedCounter(); | 421 | this.incrementReseedCounter(); |
422 | reseedCounter = this.reseedCounter(); | 422 | reseedCounter = this.reseedCounter(); |
423 | 423 | ||
424 | c = this.numberOfEntropyAccumulators(); | 424 | c = this.numberOfEntropyAccumulators(); |
425 | reseedCounterMask = 0xffffffff >>> (32 - c); | 425 | reseedCounterMask = 0xffffffff >>> (32 - c); |
426 | for (i=0; i<c; i++) { | 426 | for (i=0; i<c; i++) { |
427 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { | 427 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { |
428 | newKeySeed.appendBlock(this.accumulators()[i].stack()); | 428 | newKeySeed.appendBlock(this.accumulators()[i].stack()); |
429 | this.accumulators()[i].resetStack(); | 429 | this.accumulators()[i].resetStack(); |
430 | } | 430 | } |
431 | } | 431 | } |
432 | 432 | ||
433 | if (reseedCounter == 1) { | 433 | if (reseedCounter == 1) { |
434 | c = this.randomnessSources().length; | 434 | c = this.randomnessSources().length; |
435 | for (i=0; i<c; i++) { | 435 | for (i=0; i<c; i++) { |
436 | this.randomnessSources()[i].setBoostMode(false); | 436 | this.randomnessSources()[i].setBoostMode(false); |
437 | } | 437 | } |
438 | } | 438 | } |
439 | 439 | ||
440 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); | 440 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); |
441 | if (reseedCounter == 1) { | 441 | if (reseedCounter == 1) { |
442 | MochiKit.Logging.logDebug("### PRNG.readyToGenerateRandomBytes"); | 442 | MochiKit.Logging.logDebug("### PRNG.readyToGenerateRandomBytes"); |
443 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); | 443 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); |
444 | } | 444 | } |
445 | MochiKit.Signal.signal(this, 'reseeded'); | 445 | MochiKit.Signal.signal(this, 'reseeded'); |
446 | }, | 446 | }, |
447 | 447 | ||
448 | //------------------------------------------------------------------------- | 448 | //------------------------------------------------------------------------- |
449 | 449 | ||
450 | 'isReadyToGenerateRandomValues': function() { | 450 | 'isReadyToGenerateRandomValues': function() { |
451 | return this.reseedCounter() != 0; | 451 | return this.reseedCounter() != 0; |
452 | }, | 452 | }, |
453 | 453 | ||
454 | //------------------------------------------------------------------------- | 454 | //------------------------------------------------------------------------- |
455 | 455 | ||
456 | 'entropyLevel': function() { | 456 | 'entropyLevel': function() { |
457 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); | 457 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); |
458 | }, | 458 | }, |
459 | 459 | ||
460 | //------------------------------------------------------------------------- | 460 | //------------------------------------------------------------------------- |
461 | 461 | ||
462 | 'counter': function() { | 462 | 'counter': function() { |
463 | return this._counter; | 463 | return this._counter; |
464 | }, | 464 | }, |
465 | 465 | ||
466 | 'incrementCounter': function() { | 466 | 'incrementCounter': function() { |
467 | this._counter += 1; | 467 | this._counter += 1; |
468 | }, | 468 | }, |
469 | 469 | ||
470 | 'counterBlock': function() { | 470 | 'counterBlock': function() { |
471 | var result; | 471 | var result; |
472 | 472 | ||
473 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); | 473 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); |
474 | 474 | ||
475 | return result; | 475 | return result; |
476 | }, | 476 | }, |
477 | 477 | ||
478 | //------------------------------------------------------------------------- | 478 | //------------------------------------------------------------------------- |
479 | 479 | ||
480 | 'getRandomBlock': function() { | 480 | 'getRandomBlock': function() { |
481 | var result; | 481 | var result; |
482 | 482 | ||
483 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); | 483 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); |
484 | this.incrementCounter(); | 484 | this.incrementCounter(); |
485 | 485 | ||
486 | return result; | 486 | return result; |
487 | }, | 487 | }, |
488 | 488 | ||
489 | //------------------------------------------------------------------------- | 489 | //------------------------------------------------------------------------- |
490 | 490 | ||
491 | 'getRandomBytes': function(aSize) { | 491 | 'getRandomBytes': function(aSize) { |
492 | var result; | 492 | var result; |
493 | 493 | ||
494 | if (this.isReadyToGenerateRandomValues()) { | 494 | if (this.isReadyToGenerateRandomValues()) { |
495 | var i,c; | 495 | var i,c; |
496 | var newKey; | 496 | var newKey; |
497 | 497 | ||
498 | result = new Clipperz.ByteArray(); | 498 | result = new Clipperz.ByteArray(); |
499 | 499 | ||
500 | c = Math.ceil(aSize / (128 / 8)); | 500 | c = Math.ceil(aSize / (128 / 8)); |
501 | for (i=0; i<c; i++) { | 501 | for (i=0; i<c; i++) { |
502 | result.appendBlock(this.getRandomBlock()); | 502 | result.appendBlock(this.getRandomBlock()); |
503 | } | 503 | } |
504 | 504 | ||
505 | if (result.length() != aSize) { | 505 | if (result.length() != aSize) { |
506 | result = result.split(0, aSize); | 506 | result = result.split(0, aSize); |
507 | } | 507 | } |
508 | 508 | ||
509 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); | 509 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); |
510 | this.setKey(newKey); | 510 | this.setKey(newKey); |
511 | } else { | 511 | } else { |
512 | MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | 512 | MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); |
513 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; | 513 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; |
514 | } | 514 | } |
515 | 515 | ||
516 | return result; | 516 | return result; |
517 | }, | 517 | }, |
518 | 518 | ||
519 | //------------------------------------------------------------------------- | 519 | //------------------------------------------------------------------------- |
520 | 520 | ||
521 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { | 521 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { |
522 | varselectedAccumulator; | 522 | varselectedAccumulator; |
523 | 523 | ||
524 | selectedAccumulator = this.accumulators()[aPoolId]; | 524 | selectedAccumulator = this.accumulators()[aPoolId]; |
525 | selectedAccumulator.addRandomByte(aRandomValue); | 525 | selectedAccumulator.addRandomByte(aRandomValue); |
526 | 526 | ||
527 | if (aPoolId == 0) { | 527 | if (aPoolId == 0) { |
528 | MochiKit.Signal.signal(this, 'addedRandomByte') | 528 | MochiKit.Signal.signal(this, 'addedRandomByte') |
529 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { | 529 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { |
530 | this.reseed(); | 530 | this.reseed(); |
531 | } | 531 | } |
532 | } | 532 | } |
533 | }, | 533 | }, |
534 | 534 | ||
535 | //------------------------------------------------------------------------- | 535 | //------------------------------------------------------------------------- |
536 | 536 | ||
537 | 'numberOfEntropyAccumulators': function() { | 537 | 'numberOfEntropyAccumulators': function() { |
538 | return this._numberOfEntropyAccumulators; | 538 | return this._numberOfEntropyAccumulators; |
539 | }, | 539 | }, |
540 | 540 | ||
541 | //------------------------------------------------------------------------- | 541 | //------------------------------------------------------------------------- |
542 | 542 | ||
543 | 'randomnessSources': function() { | 543 | 'randomnessSources': function() { |
544 | return this._randomnessSources; | 544 | return this._randomnessSources; |
545 | }, | 545 | }, |
546 | 546 | ||
547 | 'addRandomnessSource': function(aRandomnessSource) { | 547 | 'addRandomnessSource': function(aRandomnessSource) { |
548 | aRandomnessSource.setGenerator(this); | 548 | aRandomnessSource.setGenerator(this); |
549 | aRandomnessSource.setSourceId(this.randomnessSources().length); | 549 | aRandomnessSource.setSourceId(this.randomnessSources().length); |
550 | this.randomnessSources().push(aRandomnessSource); | 550 | this.randomnessSources().push(aRandomnessSource); |
551 | 551 | ||
552 | if (this.isReadyToGenerateRandomValues() == false) { | 552 | if (this.isReadyToGenerateRandomValues() == false) { |
553 | aRandomnessSource.setBoostMode(true); | 553 | aRandomnessSource.setBoostMode(true); |
554 | } | 554 | } |
555 | }, | 555 | }, |
556 | 556 | ||
557 | //------------------------------------------------------------------------- | 557 | //------------------------------------------------------------------------- |
558 | 558 | ||
559 | 'deferredEntropyCollection': function(aValue) { | 559 | 'deferredEntropyCollection': function(aValue) { |
560 | var result; | 560 | var result; |
561 | 561 | ||
562 | 562 | ||
563 | if (this.isReadyToGenerateRandomValues()) { | 563 | if (this.isReadyToGenerateRandomValues()) { |
564 | result = aValue; | 564 | result = aValue; |
565 | } else { | 565 | } else { |
566 | var deferredResult; | 566 | var deferredResult; |
567 | 567 | ||
568 | Clipperz.NotificationCenter.notify(this, 'updatedProgressState', 'collectingEntropy', true); | 568 | Clipperz.NotificationCenter.notify(this, 'updatedProgressState', 'collectingEntropy', true); |
569 | 569 | ||
570 | deferredResult = new MochiKit.Async.Deferred(); | 570 | deferredResult = new MochiKit.Async.Deferred(); |
571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); | 571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); |
572 | MochiKit.Signal.connect(this, | 572 | MochiKit.Signal.connect(this, |
573 | 'readyToGenerateRandomBytes', | 573 | 'readyToGenerateRandomBytes', |
574 | deferredResult, | 574 | deferredResult, |
575 | 'callback'); | 575 | 'callback'); |
576 | 576 | ||
577 | result = deferredResult; | 577 | result = deferredResult; |
578 | } | 578 | } |
579 | 579 | ||
580 | return result; | 580 | return result; |
581 | }, | 581 | }, |
582 | 582 | ||
583 | //------------------------------------------------------------------------- | 583 | //------------------------------------------------------------------------- |
584 | 584 | ||
585 | 'fastEntropyAccumulationForTestingPurpose': function() { | 585 | 'fastEntropyAccumulationForTestingPurpose': function() { |
586 | while (! this.isReadyToGenerateRandomValues()) { | 586 | while (! this.isReadyToGenerateRandomValues()) { |
587 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); | 587 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); |
588 | } | 588 | } |
589 | }, | 589 | }, |
590 | 590 | ||
591 | //------------------------------------------------------------------------- | 591 | //------------------------------------------------------------------------- |
592 | /* | 592 | /* |
593 | 'dump': function(appendToDoc) { | 593 | 'dump': function(appendToDoc) { |
594 | var tbl; | 594 | var tbl; |
595 | var i,c; | 595 | var i,c; |
596 | 596 | ||
597 | tbl = document.createElement("table"); | 597 | tbl = document.createElement("table"); |
598 | tbl.border = 0; | 598 | tbl.border = 0; |
599 | with (tbl.style) { | 599 | with (tbl.style) { |
600 | border = "1px solid lightgrey"; | 600 | border = "1px solid lightgrey"; |
601 | fontFamily = 'Helvetica, Arial, sans-serif'; | 601 | fontFamily = 'Helvetica, Arial, sans-serif'; |
602 | fontSize = '8pt'; | 602 | fontSize = '8pt'; |
603 | //borderCollapse = "collapse"; | 603 | //borderCollapse = "collapse"; |
604 | } | 604 | } |
605 | var hdr = tbl.createTHead(); | 605 | var hdr = tbl.createTHead(); |
606 | var hdrtr = hdr.insertRow(0); | 606 | var hdrtr = hdr.insertRow(0); |
607 | // document.createElement("tr"); | 607 | // document.createElement("tr"); |
608 | { | 608 | { |
609 | var ntd; | 609 | var ntd; |
610 | 610 | ||
611 | ntd = hdrtr.insertCell(0); | 611 | ntd = hdrtr.insertCell(0); |
612 | ntd.style.borderBottom = "1px solid lightgrey"; | 612 | ntd.style.borderBottom = "1px solid lightgrey"; |
613 | ntd.style.borderRight = "1px solid lightgrey"; | 613 | ntd.style.borderRight = "1px solid lightgrey"; |
614 | ntd.appendChild(document.createTextNode("#")); | 614 | ntd.appendChild(document.createTextNode("#")); |
615 | 615 | ||
616 | ntd = hdrtr.insertCell(1); | 616 | ntd = hdrtr.insertCell(1); |
617 | ntd.style.borderBottom = "1px solid lightgrey"; | 617 | ntd.style.borderBottom = "1px solid lightgrey"; |
618 | ntd.style.borderRight = "1px solid lightgrey"; | 618 | ntd.style.borderRight = "1px solid lightgrey"; |
619 | ntd.appendChild(document.createTextNode("s")); | 619 | ntd.appendChild(document.createTextNode("s")); |
620 | 620 | ||
621 | ntd = hdrtr.insertCell(2); | 621 | ntd = hdrtr.insertCell(2); |
622 | ntd.colSpan = this.firstPoolReseedLevel(); | 622 | ntd.colSpan = this.firstPoolReseedLevel(); |
623 | ntd.style.borderBottom = "1px solid lightgrey"; | 623 | ntd.style.borderBottom = "1px solid lightgrey"; |
624 | ntd.style.borderRight = "1px solid lightgrey"; | 624 | ntd.style.borderRight = "1px solid lightgrey"; |
625 | ntd.appendChild(document.createTextNode("base values")); | 625 | ntd.appendChild(document.createTextNode("base values")); |
626 | 626 | ||
627 | ntd = hdrtr.insertCell(3); | 627 | ntd = hdrtr.insertCell(3); |
628 | ntd.colSpan = 20; | 628 | ntd.colSpan = 20; |
629 | ntd.style.borderBottom = "1px solid lightgrey"; | 629 | ntd.style.borderBottom = "1px solid lightgrey"; |
630 | ntd.appendChild(document.createTextNode("extra values")); | 630 | ntd.appendChild(document.createTextNode("extra values")); |
631 | 631 | ||
632 | } | 632 | } |
633 | 633 | ||
634 | c = this.accumulators().length; | 634 | c = this.accumulators().length; |
635 | for (i=0; i<c ; i++) { | 635 | for (i=0; i<c ; i++) { |
636 | varcurrentAccumulator; | 636 | varcurrentAccumulator; |
637 | var bdytr; | 637 | var bdytr; |
638 | var bdytd; | 638 | var bdytd; |
639 | var ii, cc; | 639 | var ii, cc; |
640 | 640 | ||
641 | currentAccumulator = this.accumulators()[i] | 641 | currentAccumulator = this.accumulators()[i] |
642 | 642 | ||
643 | bdytr = tbl.insertRow(true); | 643 | bdytr = tbl.insertRow(true); |
644 | 644 | ||
645 | bdytd = bdytr.insertCell(0); | 645 | bdytd = bdytr.insertCell(0); |
646 | bdytd.style.borderRight = "1px solid lightgrey"; | 646 | bdytd.style.borderRight = "1px solid lightgrey"; |
647 | bdytd.style.color = "lightgrey"; | 647 | bdytd.style.color = "lightgrey"; |
648 | bdytd.appendChild(document.createTextNode("" + i)); | 648 | bdytd.appendChild(document.createTextNode("" + i)); |
649 | 649 | ||
650 | bdytd = bdytr.insertCell(1); | 650 | bdytd = bdytr.insertCell(1); |
651 | bdytd.style.borderRight = "1px solid lightgrey"; | 651 | bdytd.style.borderRight = "1px solid lightgrey"; |
652 | bdytd.style.color = "gray"; | 652 | bdytd.style.color = "gray"; |
653 | bdytd.appendChild(document.createTextNode("" + currentAccumulator.stack().length())); | 653 | bdytd.appendChild(document.createTextNode("" + currentAccumulator.stack().length())); |
654 | 654 | ||
655 | 655 | ||
656 | cc = Math.max(currentAccumulator.stack().length(), this.firstPoolReseedLevel()); | 656 | cc = Math.max(currentAccumulator.stack().length(), this.firstPoolReseedLevel()); |
657 | for (ii=0; ii<cc; ii++) { | 657 | for (ii=0; ii<cc; ii++) { |
658 | var cellText; | 658 | var cellText; |
659 | 659 | ||
660 | bdytd = bdytr.insertCell(ii + 2); | 660 | bdytd = bdytr.insertCell(ii + 2); |
661 | 661 | ||
662 | if (ii < currentAccumulator.stack().length()) { | 662 | if (ii < currentAccumulator.stack().length()) { |
663 | cellText = Clipperz.ByteArray.byteToHex(currentAccumulator.stack().byteAtIndex(ii)); | 663 | cellText = Clipperz.ByteArray.byteToHex(currentAccumulator.stack().byteAtIndex(ii)); |
664 | } else { | 664 | } else { |
665 | cellText = "_"; | 665 | cellText = "_"; |
666 | } | 666 | } |
667 | 667 | ||
668 | if (ii == (this.firstPoolReseedLevel() - 1)) { | 668 | if (ii == (this.firstPoolReseedLevel() - 1)) { |
669 | bdytd.style.borderRight = "1px solid lightgrey"; | 669 | bdytd.style.borderRight = "1px solid lightgrey"; |
670 | } | 670 | } |
671 | 671 | ||
672 | bdytd.appendChild(document.createTextNode(cellText)); | 672 | bdytd.appendChild(document.createTextNode(cellText)); |
673 | } | 673 | } |
674 | 674 | ||
675 | } | 675 | } |
676 | 676 | ||
677 | 677 | ||
678 | if (appendToDoc) { | 678 | if (appendToDoc) { |
679 | var ne = document.createElement("div"); | 679 | var ne = document.createElement("div"); |
680 | ne.id = "entropyGeneratorStatus"; | 680 | ne.id = "entropyGeneratorStatus"; |
681 | with (ne.style) { | 681 | with (ne.style) { |
682 | fontFamily = "Courier New, monospace"; | 682 | fontFamily = "Courier New, monospace"; |
683 | fontSize = "12px"; | 683 | fontSize = "12px"; |
684 | lineHeight = "16px"; | 684 | lineHeight = "16px"; |
685 | borderTop = "1px solid black"; | 685 | borderTop = "1px solid black"; |
686 | padding = "10px"; | 686 | padding = "10px"; |
687 | } | 687 | } |
688 | if (document.getElementById(ne.id)) { | 688 | if (document.getElementById(ne.id)) { |
689 | MochiKit.DOM.swapDOM(ne.id, ne); | 689 | MochiKit.DOM.swapDOM(ne.id, ne); |
690 | } else { | 690 | } else { |
691 | document.body.appendChild(ne); | 691 | document.body.appendChild(ne); |
692 | } | 692 | } |
693 | ne.appendChild(tbl); | 693 | ne.appendChild(tbl); |
694 | } | 694 | } |
695 | 695 | ||
696 | return tbl; | 696 | return tbl; |
697 | }, | 697 | }, |
698 | */ | 698 | */ |
699 | //----------------------------------------------------------------------------- | 699 | //----------------------------------------------------------------------------- |
700 | __syntaxFix__: "syntax fix" | 700 | __syntaxFix__: "syntax fix" |
701 | }); | 701 | }); |
702 | 702 | ||
703 | //############################################################################# | 703 | //############################################################################# |
704 | 704 | ||
705 | Clipperz.Crypto.PRNG.Random = function(args) { | 705 | Clipperz.Crypto.PRNG.Random = function(args) { |
diff --git a/frontend/delta/js/Clipperz/Crypto/PRNG.js b/frontend/delta/js/Clipperz/Crypto/PRNG.js index 7885429..80d972f 100644 --- a/frontend/delta/js/Clipperz/Crypto/PRNG.js +++ b/frontend/delta/js/Clipperz/Crypto/PRNG.js | |||
@@ -1,707 +1,707 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2013 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz, the online password manager. | 5 | This file is part of Clipperz, the online password manager. |
6 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
7 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
8 | 8 | ||
9 | * Clipperz is free software: you can redistribute it and/or modify it | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
10 | under the terms of the GNU Affero General Public License as published | 10 | under the terms of the GNU Affero General Public License as published |
11 | by the Free Software Foundation, either version 3 of the License, or | 11 | by the Free Software Foundation, either version 3 of the License, or |
12 | (at your option) any later version. | 12 | (at your option) any later version. |
13 | 13 | ||
14 | * Clipperz is distributed in the hope that it will be useful, but | 14 | * Clipperz is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
18 | 18 | ||
19 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
21 | 21 | ||
22 | */ | 22 | */ |
23 | 23 | ||
24 | "use strict"; | 24 | "use strict"; |
25 | 25 | ||
26 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | 26 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { |
27 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; | 27 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; |
28 | } | 28 | } |
29 | 29 | ||
30 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { | 30 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { |
31 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; | 31 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; |
32 | } | 32 | } |
33 | 33 | ||
34 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { | 34 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { |
35 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; | 35 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; |
36 | } | 36 | } |
37 | 37 | ||
38 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } | 38 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } |
39 | 39 | ||
40 | //############################################################################# | 40 | //############################################################################# |
41 | 41 | ||
42 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { | 42 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { |
43 | args = args || {}; | 43 | args = args || {}; |
44 | //MochiKit.Base.bindMethods(this); | 44 | //MochiKit.Base.bindMethods(this); |
45 | 45 | ||
46 | this._stack = new Clipperz.ByteArray(); | 46 | this._stack = new Clipperz.ByteArray(); |
47 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; | 47 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; |
48 | return this; | 48 | return this; |
49 | } | 49 | } |
50 | 50 | ||
51 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { | 51 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { |
52 | 52 | ||
53 | 'toString': function() { | 53 | 'toString': function() { |
54 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; | 54 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; |
55 | }, | 55 | }, |
56 | 56 | ||
57 | //------------------------------------------------------------------------- | 57 | //------------------------------------------------------------------------- |
58 | 58 | ||
59 | 'stack': function() { | 59 | 'stack': function() { |
60 | return this._stack; | 60 | return this._stack; |
61 | }, | 61 | }, |
62 | 62 | ||
63 | 'setStack': function(aValue) { | 63 | 'setStack': function(aValue) { |
64 | this._stack = aValue; | 64 | this._stack = aValue; |
65 | }, | 65 | }, |
66 | 66 | ||
67 | 'resetStack': function() { | 67 | 'resetStack': function() { |
68 | this.stack().reset(); | 68 | this.stack().reset(); |
69 | }, | 69 | }, |
70 | 70 | ||
71 | 'maxStackLengthBeforeHashing': function() { | 71 | 'maxStackLengthBeforeHashing': function() { |
72 | return this._maxStackLengthBeforeHashing; | 72 | return this._maxStackLengthBeforeHashing; |
73 | }, | 73 | }, |
74 | 74 | ||
75 | //------------------------------------------------------------------------- | 75 | //------------------------------------------------------------------------- |
76 | 76 | ||
77 | 'addRandomByte': function(aValue) { | 77 | 'addRandomByte': function(aValue) { |
78 | this.stack().appendByte(aValue); | 78 | this.stack().appendByte(aValue); |
79 | 79 | ||
80 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { | 80 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { |
81 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); | 81 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); |
82 | } | 82 | } |
83 | }, | 83 | }, |
84 | 84 | ||
85 | //------------------------------------------------------------------------- | 85 | //------------------------------------------------------------------------- |
86 | __syntaxFix__: "syntax fix" | 86 | __syntaxFix__: "syntax fix" |
87 | }); | 87 | }); |
88 | 88 | ||
89 | //############################################################################# | 89 | //############################################################################# |
90 | 90 | ||
91 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { | 91 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { |
92 | args = args || {}; | 92 | args = args || {}; |
93 | MochiKit.Base.bindMethods(this); | 93 | MochiKit.Base.bindMethods(this); |
94 | 94 | ||
95 | this._generator = args.generator || null; | 95 | this._generator = args.generator || null; |
96 | this._sourceId = args.sourceId || null; | 96 | this._sourceId = args.sourceId || null; |
97 | this._boostMode = args.boostMode || false; | 97 | this._boostMode = args.boostMode || false; |
98 | 98 | ||
99 | this._nextPoolIndex = 0; | 99 | this._nextPoolIndex = 0; |
100 | 100 | ||
101 | return this; | 101 | return this; |
102 | } | 102 | } |
103 | 103 | ||
104 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { | 104 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { |
105 | 105 | ||
106 | 'generator': function() { | 106 | 'generator': function() { |
107 | return this._generator; | 107 | return this._generator; |
108 | }, | 108 | }, |
109 | 109 | ||
110 | 'setGenerator': function(aValue) { | 110 | 'setGenerator': function(aValue) { |
111 | this._generator = aValue; | 111 | this._generator = aValue; |
112 | }, | 112 | }, |
113 | 113 | ||
114 | //------------------------------------------------------------------------- | 114 | //------------------------------------------------------------------------- |
115 | 115 | ||
116 | 'boostMode': function() { | 116 | 'boostMode': function() { |
117 | return this._boostMode; | 117 | return this._boostMode; |
118 | }, | 118 | }, |
119 | 119 | ||
120 | 'setBoostMode': function(aValue) { | 120 | 'setBoostMode': function(aValue) { |
121 | this._boostMode = aValue; | 121 | this._boostMode = aValue; |
122 | }, | 122 | }, |
123 | 123 | ||
124 | //------------------------------------------------------------------------- | 124 | //------------------------------------------------------------------------- |
125 | 125 | ||
126 | 'sourceId': function() { | 126 | 'sourceId': function() { |
127 | return this._sourceId; | 127 | return this._sourceId; |
128 | }, | 128 | }, |
129 | 129 | ||
130 | 'setSourceId': function(aValue) { | 130 | 'setSourceId': function(aValue) { |
131 | this._sourceId = aValue; | 131 | this._sourceId = aValue; |
132 | }, | 132 | }, |
133 | 133 | ||
134 | //------------------------------------------------------------------------- | 134 | //------------------------------------------------------------------------- |
135 | 135 | ||
136 | 'nextPoolIndex': function() { | 136 | 'nextPoolIndex': function() { |
137 | return this._nextPoolIndex; | 137 | return this._nextPoolIndex; |
138 | }, | 138 | }, |
139 | 139 | ||
140 | 'incrementNextPoolIndex': function() { | 140 | 'incrementNextPoolIndex': function() { |
141 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); | 141 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); |
142 | }, | 142 | }, |
143 | 143 | ||
144 | //------------------------------------------------------------------------- | 144 | //------------------------------------------------------------------------- |
145 | 145 | ||
146 | 'updateGeneratorWithValue': function(aRandomValue) { | 146 | 'updateGeneratorWithValue': function(aRandomValue) { |
147 | if (this.generator() != null) { | 147 | if (this.generator() != null) { |
148 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); | 148 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); |
149 | this.incrementNextPoolIndex(); | 149 | this.incrementNextPoolIndex(); |
150 | } | 150 | } |
151 | }, | 151 | }, |
152 | 152 | ||
153 | //------------------------------------------------------------------------- | 153 | //------------------------------------------------------------------------- |
154 | __syntaxFix__: "syntax fix" | 154 | __syntaxFix__: "syntax fix" |
155 | }); | 155 | }); |
156 | 156 | ||
157 | //############################################################################# | 157 | //############################################################################# |
158 | 158 | ||
159 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { | 159 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { |
160 | args = args || {}; | 160 | args = args || {}; |
161 | //MochiKit.Base.bindMethods(this); | 161 | //MochiKit.Base.bindMethods(this); |
162 | 162 | ||
163 | this._intervalTime = args.intervalTime || 1000; | 163 | this._intervalTime = args.intervalTime || 1000; |
164 | 164 | ||
165 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 165 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
166 | 166 | ||
167 | this.collectEntropy(); | 167 | this.collectEntropy(); |
168 | return this; | 168 | return this; |
169 | } | 169 | } |
170 | 170 | ||
171 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 171 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
172 | 172 | ||
173 | 'intervalTime': function() { | 173 | 'intervalTime': function() { |
174 | return this._intervalTime; | 174 | return this._intervalTime; |
175 | }, | 175 | }, |
176 | 176 | ||
177 | //------------------------------------------------------------------------- | 177 | //------------------------------------------------------------------------- |
178 | 178 | ||
179 | 'collectEntropy': function() { | 179 | 'collectEntropy': function() { |
180 | varnow; | 180 | varnow; |
181 | varentropyByte; | 181 | varentropyByte; |
182 | var intervalTime; | 182 | var intervalTime; |
183 | now = new Date(); | 183 | now = new Date(); |
184 | entropyByte = (now.getTime() & 0xff); | 184 | entropyByte = (now.getTime() & 0xff); |
185 | 185 | ||
186 | intervalTime = this.intervalTime(); | 186 | intervalTime = this.intervalTime(); |
187 | if (this.boostMode() == true) { | 187 | if (this.boostMode() == true) { |
188 | intervalTime = intervalTime / 9; | 188 | intervalTime = intervalTime / 9; |
189 | } | 189 | } |
190 | 190 | ||
191 | this.updateGeneratorWithValue(entropyByte); | 191 | this.updateGeneratorWithValue(entropyByte); |
192 | setTimeout(this.collectEntropy, intervalTime); | 192 | setTimeout(this.collectEntropy, intervalTime); |
193 | }, | 193 | }, |
194 | 194 | ||
195 | //------------------------------------------------------------------------- | 195 | //------------------------------------------------------------------------- |
196 | 196 | ||
197 | 'numberOfRandomBits': function() { | 197 | 'numberOfRandomBits': function() { |
198 | return 5; | 198 | return 5; |
199 | }, | 199 | }, |
200 | 200 | ||
201 | //------------------------------------------------------------------------- | 201 | //------------------------------------------------------------------------- |
202 | __syntaxFix__: "syntax fix" | 202 | __syntaxFix__: "syntax fix" |
203 | }); | 203 | }); |
204 | 204 | ||
205 | //***************************************************************************** | 205 | //***************************************************************************** |
206 | 206 | ||
207 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { | 207 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { |
208 | args = args || {}; | 208 | args = args || {}; |
209 | 209 | ||
210 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 210 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
211 | 211 | ||
212 | this._numberOfBitsToCollectAtEachEvent = 4; | 212 | this._numberOfBitsToCollectAtEachEvent = 4; |
213 | this._randomBitsCollector = 0; | 213 | this._randomBitsCollector = 0; |
214 | this._numberOfRandomBitsCollected = 0; | 214 | this._numberOfRandomBitsCollected = 0; |
215 | 215 | ||
216 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); | 216 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); |
217 | 217 | ||
218 | return this; | 218 | return this; |
219 | } | 219 | } |
220 | 220 | ||
221 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 221 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
222 | 222 | ||
223 | //------------------------------------------------------------------------- | 223 | //------------------------------------------------------------------------- |
224 | 224 | ||
225 | 'numberOfBitsToCollectAtEachEvent': function() { | 225 | 'numberOfBitsToCollectAtEachEvent': function() { |
226 | return this._numberOfBitsToCollectAtEachEvent; | 226 | return this._numberOfBitsToCollectAtEachEvent; |
227 | }, | 227 | }, |
228 | 228 | ||
229 | //------------------------------------------------------------------------- | 229 | //------------------------------------------------------------------------- |
230 | 230 | ||
231 | 'randomBitsCollector': function() { | 231 | 'randomBitsCollector': function() { |
232 | return this._randomBitsCollector; | 232 | return this._randomBitsCollector; |
233 | }, | 233 | }, |
234 | 234 | ||
235 | 'setRandomBitsCollector': function(aValue) { | 235 | 'setRandomBitsCollector': function(aValue) { |
236 | this._randomBitsCollector = aValue; | 236 | this._randomBitsCollector = aValue; |
237 | }, | 237 | }, |
238 | 238 | ||
239 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { | 239 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { |
240 | var collectedBits; | 240 | var collectedBits; |
241 | var numberOfRandomBitsCollected; | 241 | var numberOfRandomBitsCollected; |
242 | 242 | ||
243 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | 243 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); |
244 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | 244 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); |
245 | this.setRandomBitsCollector(collectedBits); | 245 | this.setRandomBitsCollector(collectedBits); |
246 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); | 246 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); |
247 | 247 | ||
248 | if (numberOfRandomBitsCollected == 8) { | 248 | if (numberOfRandomBitsCollected == 8) { |
249 | this.updateGeneratorWithValue(collectedBits); | 249 | this.updateGeneratorWithValue(collectedBits); |
250 | numberOfRandomBitsCollected = 0; | 250 | numberOfRandomBitsCollected = 0; |
251 | this.setRandomBitsCollector(0); | 251 | this.setRandomBitsCollector(0); |
252 | } | 252 | } |
253 | 253 | ||
254 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | 254 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) |
255 | }, | 255 | }, |
256 | 256 | ||
257 | //------------------------------------------------------------------------- | 257 | //------------------------------------------------------------------------- |
258 | 258 | ||
259 | 'numberOfRandomBitsCollected': function() { | 259 | 'numberOfRandomBitsCollected': function() { |
260 | return this._numberOfRandomBitsCollected; | 260 | return this._numberOfRandomBitsCollected; |
261 | }, | 261 | }, |
262 | 262 | ||
263 | 'setNumberOfRandomBitsCollected': function(aValue) { | 263 | 'setNumberOfRandomBitsCollected': function(aValue) { |
264 | this._numberOfRandomBitsCollected = aValue; | 264 | this._numberOfRandomBitsCollected = aValue; |
265 | }, | 265 | }, |
266 | 266 | ||
267 | //------------------------------------------------------------------------- | 267 | //------------------------------------------------------------------------- |
268 | 268 | ||
269 | 'collectEntropy': function(anEvent) { | 269 | 'collectEntropy': function(anEvent) { |
270 | var mouseLocation; | 270 | var mouseLocation; |
271 | var randomBit; | 271 | var randomBit; |
272 | var mask; | 272 | var mask; |
273 | 273 | ||
274 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); | 274 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); |
275 | 275 | ||
276 | mouseLocation = anEvent.mouse().client; | 276 | mouseLocation = anEvent.mouse().client; |
277 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); | 277 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); |
278 | this.appendRandomBitsToRandomBitsCollector(randomBit) | 278 | this.appendRandomBitsToRandomBitsCollector(randomBit) |
279 | }, | 279 | }, |
280 | 280 | ||
281 | //------------------------------------------------------------------------- | 281 | //------------------------------------------------------------------------- |
282 | 282 | ||
283 | 'numberOfRandomBits': function() { | 283 | 'numberOfRandomBits': function() { |
284 | return 1; | 284 | return 1; |
285 | }, | 285 | }, |
286 | 286 | ||
287 | //------------------------------------------------------------------------- | 287 | //------------------------------------------------------------------------- |
288 | __syntaxFix__: "syntax fix" | 288 | __syntaxFix__: "syntax fix" |
289 | }); | 289 | }); |
290 | 290 | ||
291 | //***************************************************************************** | 291 | //***************************************************************************** |
292 | 292 | ||
293 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { | 293 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { |
294 | args = args || {}; | 294 | args = args || {}; |
295 | 295 | ||
296 | this._intervalTime = args.intervalTime || 1000; | 296 | this._intervalTime = args.intervalTime || 1000; |
297 | this._browserCrypto = args.browserCrypto; | 297 | this._browserCrypto = args.browserCrypto; |
298 | 298 | ||
299 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 299 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
300 | 300 | ||
301 | this.collectEntropy(); | 301 | this.collectEntropy(); |
302 | return this; | 302 | return this; |
303 | } | 303 | } |
304 | 304 | ||
305 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 305 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
306 | 306 | ||
307 | 'intervalTime': function() { | 307 | 'intervalTime': function() { |
308 | return this._intervalTime; | 308 | return this._intervalTime; |
309 | }, | 309 | }, |
310 | 310 | ||
311 | 'browserCrypto': function () { | 311 | 'browserCrypto': function () { |
312 | return this._browserCrypto; | 312 | return this._browserCrypto; |
313 | }, | 313 | }, |
314 | 314 | ||
315 | //------------------------------------------------------------------------- | 315 | //------------------------------------------------------------------------- |
316 | 316 | ||
317 | 'collectEntropy': function() { | 317 | 'collectEntropy': function() { |
318 | varbytesToCollect; | 318 | varbytesToCollect; |
319 | 319 | ||
320 | if (this.boostMode() == true) { | 320 | if (this.boostMode() == true) { |
321 | bytesToCollect = 8; | 321 | bytesToCollect = 64; |
322 | } else { | 322 | } else { |
323 | bytesToCollect = 32; | 323 | bytesToCollect = 8; |
324 | } | 324 | } |
325 | 325 | ||
326 | var randomValuesArray = new Uint8Array(bytesToCollect); | 326 | var randomValuesArray = new Uint8Array(bytesToCollect); |
327 | this.browserCrypto().getRandomValues(randomValuesArray); | 327 | this.browserCrypto().getRandomValues(randomValuesArray); |
328 | for (var i = 0; i < randomValuesArray.length; i++) { | 328 | for (var i = 0; i < randomValuesArray.length; i++) { |
329 | this.updateGeneratorWithValue(randomValuesArray[i]); | 329 | this.updateGeneratorWithValue(randomValuesArray[i]); |
330 | } | 330 | } |
331 | 331 | ||
332 | setTimeout(this.collectEntropy, this.intervalTime()); | 332 | setTimeout(this.collectEntropy, this.intervalTime()); |
333 | }, | 333 | }, |
334 | 334 | ||
335 | //------------------------------------------------------------------------- | 335 | //------------------------------------------------------------------------- |
336 | __syntaxFix__: "syntax fix" | 336 | __syntaxFix__: "syntax fix" |
337 | }); | 337 | }); |
338 | 338 | ||
339 | //############################################################################# | 339 | //############################################################################# |
340 | 340 | ||
341 | Clipperz.Crypto.PRNG.Fortuna = function(args) { | 341 | Clipperz.Crypto.PRNG.Fortuna = function(args) { |
342 | vari,c; | 342 | vari,c; |
343 | 343 | ||
344 | args = args || {}; | 344 | args = args || {}; |
345 | 345 | ||
346 | this._key = args.seed || null; | 346 | this._key = args.seed || null; |
347 | if (this._key == null) { | 347 | if (this._key == null) { |
348 | this._counter = 0; | 348 | this._counter = 0; |
349 | this._key = new Clipperz.ByteArray(); | 349 | this._key = new Clipperz.ByteArray(); |
350 | } else { | 350 | } else { |
351 | this._counter = 1; | 351 | this._counter = 1; |
352 | } | 352 | } |
353 | 353 | ||
354 | this._aesKey = null; | 354 | this._aesKey = null; |
355 | 355 | ||
356 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; | 356 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; |
357 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; | 357 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; |
358 | 358 | ||
359 | this._accumulators = []; | 359 | this._accumulators = []; |
360 | c = this.numberOfEntropyAccumulators(); | 360 | c = this.numberOfEntropyAccumulators(); |
361 | for (i=0; i<c; i++) { | 361 | for (i=0; i<c; i++) { |
362 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); | 362 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); |
363 | } | 363 | } |
364 | 364 | ||
365 | this._randomnessSources = []; | 365 | this._randomnessSources = []; |
366 | this._reseedCounter = 0; | 366 | this._reseedCounter = 0; |
367 | 367 | ||
368 | return this; | 368 | return this; |
369 | } | 369 | } |
370 | 370 | ||
371 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { | 371 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { |
372 | 372 | ||
373 | 'toString': function() { | 373 | 'toString': function() { |
374 | return "Clipperz.Crypto.PRNG.Fortuna"; | 374 | return "Clipperz.Crypto.PRNG.Fortuna"; |
375 | }, | 375 | }, |
376 | 376 | ||
377 | //------------------------------------------------------------------------- | 377 | //------------------------------------------------------------------------- |
378 | 378 | ||
379 | 'key': function() { | 379 | 'key': function() { |
380 | return this._key; | 380 | return this._key; |
381 | }, | 381 | }, |
382 | 382 | ||
383 | 'setKey': function(aValue) { | 383 | 'setKey': function(aValue) { |
384 | this._key = aValue; | 384 | this._key = aValue; |
385 | this._aesKey = null; | 385 | this._aesKey = null; |
386 | }, | 386 | }, |
387 | 387 | ||
388 | 'aesKey': function() { | 388 | 'aesKey': function() { |
389 | if (this._aesKey == null) { | 389 | if (this._aesKey == null) { |
390 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); | 390 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); |
391 | } | 391 | } |
392 | 392 | ||
393 | return this._aesKey; | 393 | return this._aesKey; |
394 | }, | 394 | }, |
395 | 395 | ||
396 | 'accumulators': function() { | 396 | 'accumulators': function() { |
397 | return this._accumulators; | 397 | return this._accumulators; |
398 | }, | 398 | }, |
399 | 399 | ||
400 | 'firstPoolReseedLevel': function() { | 400 | 'firstPoolReseedLevel': function() { |
401 | return this._firstPoolReseedLevel; | 401 | return this._firstPoolReseedLevel; |
402 | }, | 402 | }, |
403 | 403 | ||
404 | //------------------------------------------------------------------------- | 404 | //------------------------------------------------------------------------- |
405 | 405 | ||
406 | 'reseedCounter': function() { | 406 | 'reseedCounter': function() { |
407 | return this._reseedCounter; | 407 | return this._reseedCounter; |
408 | }, | 408 | }, |
409 | 409 | ||
410 | 'incrementReseedCounter': function() { | 410 | 'incrementReseedCounter': function() { |
411 | this._reseedCounter = this._reseedCounter +1; | 411 | this._reseedCounter = this._reseedCounter +1; |
412 | }, | 412 | }, |
413 | 413 | ||
414 | //------------------------------------------------------------------------- | 414 | //------------------------------------------------------------------------- |
415 | 415 | ||
416 | 'reseed': function() { | 416 | 'reseed': function() { |
417 | varnewKeySeed; | 417 | varnewKeySeed; |
418 | var reseedCounter; | 418 | var reseedCounter; |
419 | varreseedCounterMask; | 419 | varreseedCounterMask; |
420 | var i, c; | 420 | var i, c; |
421 | 421 | ||
422 | newKeySeed = this.key(); | 422 | newKeySeed = this.key(); |
423 | this.incrementReseedCounter(); | 423 | this.incrementReseedCounter(); |
424 | reseedCounter = this.reseedCounter(); | 424 | reseedCounter = this.reseedCounter(); |
425 | 425 | ||
426 | c = this.numberOfEntropyAccumulators(); | 426 | c = this.numberOfEntropyAccumulators(); |
427 | reseedCounterMask = 0xffffffff >>> (32 - c); | 427 | reseedCounterMask = 0xffffffff >>> (32 - c); |
428 | for (i=0; i<c; i++) { | 428 | for (i=0; i<c; i++) { |
429 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { | 429 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { |
430 | newKeySeed.appendBlock(this.accumulators()[i].stack()); | 430 | newKeySeed.appendBlock(this.accumulators()[i].stack()); |
431 | this.accumulators()[i].resetStack(); | 431 | this.accumulators()[i].resetStack(); |
432 | } | 432 | } |
433 | } | 433 | } |
434 | 434 | ||
435 | if (reseedCounter == 1) { | 435 | if (reseedCounter == 1) { |
436 | c = this.randomnessSources().length; | 436 | c = this.randomnessSources().length; |
437 | for (i=0; i<c; i++) { | 437 | for (i=0; i<c; i++) { |
438 | this.randomnessSources()[i].setBoostMode(false); | 438 | this.randomnessSources()[i].setBoostMode(false); |
439 | } | 439 | } |
440 | } | 440 | } |
441 | 441 | ||
442 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); | 442 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); |
443 | if (reseedCounter == 1) { | 443 | if (reseedCounter == 1) { |
444 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); | 444 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); |
445 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); | 445 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); |
446 | } | 446 | } |
447 | MochiKit.Signal.signal(this, 'reseeded'); | 447 | MochiKit.Signal.signal(this, 'reseeded'); |
448 | }, | 448 | }, |
449 | 449 | ||
450 | //------------------------------------------------------------------------- | 450 | //------------------------------------------------------------------------- |
451 | 451 | ||
452 | 'isReadyToGenerateRandomValues': function() { | 452 | 'isReadyToGenerateRandomValues': function() { |
453 | return this.reseedCounter() != 0; | 453 | return this.reseedCounter() != 0; |
454 | }, | 454 | }, |
455 | 455 | ||
456 | //------------------------------------------------------------------------- | 456 | //------------------------------------------------------------------------- |
457 | 457 | ||
458 | 'entropyLevel': function() { | 458 | 'entropyLevel': function() { |
459 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); | 459 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); |
460 | }, | 460 | }, |
461 | 461 | ||
462 | //------------------------------------------------------------------------- | 462 | //------------------------------------------------------------------------- |
463 | 463 | ||
464 | 'counter': function() { | 464 | 'counter': function() { |
465 | return this._counter; | 465 | return this._counter; |
466 | }, | 466 | }, |
467 | 467 | ||
468 | 'incrementCounter': function() { | 468 | 'incrementCounter': function() { |
469 | this._counter += 1; | 469 | this._counter += 1; |
470 | }, | 470 | }, |
471 | 471 | ||
472 | 'counterBlock': function() { | 472 | 'counterBlock': function() { |
473 | var result; | 473 | var result; |
474 | 474 | ||
475 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); | 475 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); |
476 | 476 | ||
477 | return result; | 477 | return result; |
478 | }, | 478 | }, |
479 | 479 | ||
480 | //------------------------------------------------------------------------- | 480 | //------------------------------------------------------------------------- |
481 | 481 | ||
482 | 'getRandomBlock': function() { | 482 | 'getRandomBlock': function() { |
483 | var result; | 483 | var result; |
484 | 484 | ||
485 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); | 485 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); |
486 | this.incrementCounter(); | 486 | this.incrementCounter(); |
487 | 487 | ||
488 | return result; | 488 | return result; |
489 | }, | 489 | }, |
490 | 490 | ||
491 | //------------------------------------------------------------------------- | 491 | //------------------------------------------------------------------------- |
492 | 492 | ||
493 | 'getRandomBytes': function(aSize) { | 493 | 'getRandomBytes': function(aSize) { |
494 | var result; | 494 | var result; |
495 | 495 | ||
496 | if (this.isReadyToGenerateRandomValues()) { | 496 | if (this.isReadyToGenerateRandomValues()) { |
497 | var i,c; | 497 | var i,c; |
498 | var newKey; | 498 | var newKey; |
499 | 499 | ||
500 | result = new Clipperz.ByteArray(); | 500 | result = new Clipperz.ByteArray(); |
501 | 501 | ||
502 | c = Math.ceil(aSize / (128 / 8)); | 502 | c = Math.ceil(aSize / (128 / 8)); |
503 | for (i=0; i<c; i++) { | 503 | for (i=0; i<c; i++) { |
504 | result.appendBlock(this.getRandomBlock()); | 504 | result.appendBlock(this.getRandomBlock()); |
505 | } | 505 | } |
506 | 506 | ||
507 | if (result.length() != aSize) { | 507 | if (result.length() != aSize) { |
508 | result = result.split(0, aSize); | 508 | result = result.split(0, aSize); |
509 | } | 509 | } |
510 | 510 | ||
511 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); | 511 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); |
512 | this.setKey(newKey); | 512 | this.setKey(newKey); |
513 | } else { | 513 | } else { |
514 | Clipperz.logWarning("Fortuna generator has not enough entropy, yet!"); | 514 | Clipperz.logWarning("Fortuna generator has not enough entropy, yet!"); |
515 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; | 515 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; |
516 | } | 516 | } |
517 | 517 | ||
518 | return result; | 518 | return result; |
519 | }, | 519 | }, |
520 | 520 | ||
521 | //------------------------------------------------------------------------- | 521 | //------------------------------------------------------------------------- |
522 | 522 | ||
523 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { | 523 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { |
524 | varselectedAccumulator; | 524 | varselectedAccumulator; |
525 | 525 | ||
526 | selectedAccumulator = this.accumulators()[aPoolId]; | 526 | selectedAccumulator = this.accumulators()[aPoolId]; |
527 | selectedAccumulator.addRandomByte(aRandomValue); | 527 | selectedAccumulator.addRandomByte(aRandomValue); |
528 | 528 | ||
529 | if (aPoolId == 0) { | 529 | if (aPoolId == 0) { |
530 | MochiKit.Signal.signal(this, 'addedRandomByte') | 530 | MochiKit.Signal.signal(this, 'addedRandomByte') |
531 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { | 531 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { |
532 | this.reseed(); | 532 | this.reseed(); |
533 | } | 533 | } |
534 | } | 534 | } |
535 | }, | 535 | }, |
536 | 536 | ||
537 | //------------------------------------------------------------------------- | 537 | //------------------------------------------------------------------------- |
538 | 538 | ||
539 | 'numberOfEntropyAccumulators': function() { | 539 | 'numberOfEntropyAccumulators': function() { |
540 | return this._numberOfEntropyAccumulators; | 540 | return this._numberOfEntropyAccumulators; |
541 | }, | 541 | }, |
542 | 542 | ||
543 | //------------------------------------------------------------------------- | 543 | //------------------------------------------------------------------------- |
544 | 544 | ||
545 | 'randomnessSources': function() { | 545 | 'randomnessSources': function() { |
546 | return this._randomnessSources; | 546 | return this._randomnessSources; |
547 | }, | 547 | }, |
548 | 548 | ||
549 | 'addRandomnessSource': function(aRandomnessSource) { | 549 | 'addRandomnessSource': function(aRandomnessSource) { |
550 | aRandomnessSource.setGenerator(this); | 550 | aRandomnessSource.setGenerator(this); |
551 | aRandomnessSource.setSourceId(this.randomnessSources().length); | 551 | aRandomnessSource.setSourceId(this.randomnessSources().length); |
552 | this.randomnessSources().push(aRandomnessSource); | 552 | this.randomnessSources().push(aRandomnessSource); |
553 | 553 | ||
554 | if (this.isReadyToGenerateRandomValues() == false) { | 554 | if (this.isReadyToGenerateRandomValues() == false) { |
555 | aRandomnessSource.setBoostMode(true); | 555 | aRandomnessSource.setBoostMode(true); |
556 | } | 556 | } |
557 | }, | 557 | }, |
558 | 558 | ||
559 | //------------------------------------------------------------------------- | 559 | //------------------------------------------------------------------------- |
560 | 560 | ||
561 | 'deferredEntropyCollection': function(aValue) { | 561 | 'deferredEntropyCollection': function(aValue) { |
562 | var result; | 562 | var result; |
563 | 563 | ||
564 | 564 | ||
565 | if (this.isReadyToGenerateRandomValues()) { | 565 | if (this.isReadyToGenerateRandomValues()) { |
566 | result = aValue; | 566 | result = aValue; |
567 | } else { | 567 | } else { |
568 | var deferredResult; | 568 | var deferredResult; |
569 | 569 | ||
570 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); | 570 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); |
571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); | 571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); |
572 | MochiKit.Signal.connect(this, | 572 | MochiKit.Signal.connect(this, |
573 | 'readyToGenerateRandomBytes', | 573 | 'readyToGenerateRandomBytes', |
574 | deferredResult, | 574 | deferredResult, |
575 | 'callback'); | 575 | 'callback'); |
576 | 576 | ||
577 | result = deferredResult; | 577 | result = deferredResult; |
578 | } | 578 | } |
579 | 579 | ||
580 | return result; | 580 | return result; |
581 | }, | 581 | }, |
582 | 582 | ||
583 | //------------------------------------------------------------------------- | 583 | //------------------------------------------------------------------------- |
584 | 584 | ||
585 | 'fastEntropyAccumulationForTestingPurpose': function() { | 585 | 'fastEntropyAccumulationForTestingPurpose': function() { |
586 | while (! this.isReadyToGenerateRandomValues()) { | 586 | while (! this.isReadyToGenerateRandomValues()) { |
587 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); | 587 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); |
588 | } | 588 | } |
589 | }, | 589 | }, |
590 | 590 | ||
591 | //------------------------------------------------------------------------- | 591 | //------------------------------------------------------------------------- |
592 | /* | 592 | /* |
593 | 'dump': function(appendToDoc) { | 593 | 'dump': function(appendToDoc) { |
594 | var tbl; | 594 | var tbl; |
595 | var i,c; | 595 | var i,c; |
596 | 596 | ||
597 | tbl = document.createElement("table"); | 597 | tbl = document.createElement("table"); |
598 | tbl.border = 0; | 598 | tbl.border = 0; |
599 | with (tbl.style) { | 599 | with (tbl.style) { |
600 | border = "1px solid lightgrey"; | 600 | border = "1px solid lightgrey"; |
601 | fontFamily = 'Helvetica, Arial, sans-serif'; | 601 | fontFamily = 'Helvetica, Arial, sans-serif'; |
602 | fontSize = '8pt'; | 602 | fontSize = '8pt'; |
603 | //borderCollapse = "collapse"; | 603 | //borderCollapse = "collapse"; |
604 | } | 604 | } |
605 | var hdr = tbl.createTHead(); | 605 | var hdr = tbl.createTHead(); |
606 | var hdrtr = hdr.insertRow(0); | 606 | var hdrtr = hdr.insertRow(0); |
607 | // document.createElement("tr"); | 607 | // document.createElement("tr"); |
608 | { | 608 | { |
609 | var ntd; | 609 | var ntd; |
610 | 610 | ||
611 | ntd = hdrtr.insertCell(0); | 611 | ntd = hdrtr.insertCell(0); |
612 | ntd.style.borderBottom = "1px solid lightgrey"; | 612 | ntd.style.borderBottom = "1px solid lightgrey"; |
613 | ntd.style.borderRight = "1px solid lightgrey"; | 613 | ntd.style.borderRight = "1px solid lightgrey"; |
614 | ntd.appendChild(document.createTextNode("#")); | 614 | ntd.appendChild(document.createTextNode("#")); |
615 | 615 | ||
616 | ntd = hdrtr.insertCell(1); | 616 | ntd = hdrtr.insertCell(1); |
617 | ntd.style.borderBottom = "1px solid lightgrey"; | 617 | ntd.style.borderBottom = "1px solid lightgrey"; |
618 | ntd.style.borderRight = "1px solid lightgrey"; | 618 | ntd.style.borderRight = "1px solid lightgrey"; |
619 | ntd.appendChild(document.createTextNode("s")); | 619 | ntd.appendChild(document.createTextNode("s")); |
620 | 620 | ||
621 | ntd = hdrtr.insertCell(2); | 621 | ntd = hdrtr.insertCell(2); |
622 | ntd.colSpan = this.firstPoolReseedLevel(); | 622 | ntd.colSpan = this.firstPoolReseedLevel(); |
623 | ntd.style.borderBottom = "1px solid lightgrey"; | 623 | ntd.style.borderBottom = "1px solid lightgrey"; |
624 | ntd.style.borderRight = "1px solid lightgrey"; | 624 | ntd.style.borderRight = "1px solid lightgrey"; |
625 | ntd.appendChild(document.createTextNode("base values")); | 625 | ntd.appendChild(document.createTextNode("base values")); |
626 | 626 | ||
627 | ntd = hdrtr.insertCell(3); | 627 | ntd = hdrtr.insertCell(3); |
628 | ntd.colSpan = 20; | 628 | ntd.colSpan = 20; |
629 | ntd.style.borderBottom = "1px solid lightgrey"; | 629 | ntd.style.borderBottom = "1px solid lightgrey"; |
630 | ntd.appendChild(document.createTextNode("extra values")); | 630 | ntd.appendChild(document.createTextNode("extra values")); |
631 | 631 | ||
632 | } | 632 | } |
633 | 633 | ||
634 | c = this.accumulators().length; | 634 | c = this.accumulators().length; |
635 | for (i=0; i<c ; i++) { | 635 | for (i=0; i<c ; i++) { |
636 | varcurrentAccumulator; | 636 | varcurrentAccumulator; |
637 | var bdytr; | 637 | var bdytr; |
638 | var bdytd; | 638 | var bdytd; |
639 | var ii, cc; | 639 | var ii, cc; |
640 | 640 | ||
641 | currentAccumulator = this.accumulators()[i] | 641 | currentAccumulator = this.accumulators()[i] |
642 | 642 | ||
643 | bdytr = tbl.insertRow(true); | 643 | bdytr = tbl.insertRow(true); |
644 | 644 | ||
645 | bdytd = bdytr.insertCell(0); | 645 | bdytd = bdytr.insertCell(0); |
646 | bdytd.style.borderRight = "1px solid lightgrey"; | 646 | bdytd.style.borderRight = "1px solid lightgrey"; |
647 | bdytd.style.color = "lightgrey"; | 647 | bdytd.style.color = "lightgrey"; |
648 | bdytd.appendChild(document.createTextNode("" + i)); | 648 | bdytd.appendChild(document.createTextNode("" + i)); |
649 | 649 | ||
650 | bdytd = bdytr.insertCell(1); | 650 | bdytd = bdytr.insertCell(1); |
651 | bdytd.style.borderRight = "1px solid lightgrey"; | 651 | bdytd.style.borderRight = "1px solid lightgrey"; |
652 | bdytd.style.color = "gray"; | 652 | bdytd.style.color = "gray"; |
653 | bdytd.appendChild(document.createTextNode("" + currentAccumulator.stack().length())); | 653 | bdytd.appendChild(document.createTextNode("" + currentAccumulator.stack().length())); |
654 | 654 | ||
655 | 655 | ||
656 | cc = Math.max(currentAccumulator.stack().length(), this.firstPoolReseedLevel()); | 656 | cc = Math.max(currentAccumulator.stack().length(), this.firstPoolReseedLevel()); |
657 | for (ii=0; ii<cc; ii++) { | 657 | for (ii=0; ii<cc; ii++) { |
658 | var cellText; | 658 | var cellText; |
659 | 659 | ||
660 | bdytd = bdytr.insertCell(ii + 2); | 660 | bdytd = bdytr.insertCell(ii + 2); |
661 | 661 | ||
662 | if (ii < currentAccumulator.stack().length()) { | 662 | if (ii < currentAccumulator.stack().length()) { |
663 | cellText = Clipperz.ByteArray.byteToHex(currentAccumulator.stack().byteAtIndex(ii)); | 663 | cellText = Clipperz.ByteArray.byteToHex(currentAccumulator.stack().byteAtIndex(ii)); |
664 | } else { | 664 | } else { |
665 | cellText = "_"; | 665 | cellText = "_"; |
666 | } | 666 | } |
667 | 667 | ||
668 | if (ii == (this.firstPoolReseedLevel() - 1)) { | 668 | if (ii == (this.firstPoolReseedLevel() - 1)) { |
669 | bdytd.style.borderRight = "1px solid lightgrey"; | 669 | bdytd.style.borderRight = "1px solid lightgrey"; |
670 | } | 670 | } |
671 | 671 | ||
672 | bdytd.appendChild(document.createTextNode(cellText)); | 672 | bdytd.appendChild(document.createTextNode(cellText)); |
673 | } | 673 | } |
674 | 674 | ||
675 | } | 675 | } |
676 | 676 | ||
677 | 677 | ||
678 | if (appendToDoc) { | 678 | if (appendToDoc) { |
679 | var ne = document.createElement("div"); | 679 | var ne = document.createElement("div"); |
680 | ne.id = "entropyGeneratorStatus"; | 680 | ne.id = "entropyGeneratorStatus"; |
681 | with (ne.style) { | 681 | with (ne.style) { |
682 | fontFamily = "Courier New, monospace"; | 682 | fontFamily = "Courier New, monospace"; |
683 | fontSize = "12px"; | 683 | fontSize = "12px"; |
684 | lineHeight = "16px"; | 684 | lineHeight = "16px"; |
685 | borderTop = "1px solid black"; | 685 | borderTop = "1px solid black"; |
686 | padding = "10px"; | 686 | padding = "10px"; |
687 | } | 687 | } |
688 | if (document.getElementById(ne.id)) { | 688 | if (document.getElementById(ne.id)) { |
689 | MochiKit.DOM.swapDOM(ne.id, ne); | 689 | MochiKit.DOM.swapDOM(ne.id, ne); |
690 | } else { | 690 | } else { |
691 | document.body.appendChild(ne); | 691 | document.body.appendChild(ne); |
692 | } | 692 | } |
693 | ne.appendChild(tbl); | 693 | ne.appendChild(tbl); |
694 | } | 694 | } |
695 | 695 | ||
696 | return tbl; | 696 | return tbl; |
697 | }, | 697 | }, |
698 | */ | 698 | */ |
699 | //----------------------------------------------------------------------------- | 699 | //----------------------------------------------------------------------------- |
700 | __syntaxFix__: "syntax fix" | 700 | __syntaxFix__: "syntax fix" |
701 | }); | 701 | }); |
702 | 702 | ||
703 | //############################################################################# | 703 | //############################################################################# |
704 | 704 | ||
705 | Clipperz.Crypto.PRNG.Random = function(args) { | 705 | Clipperz.Crypto.PRNG.Random = function(args) { |
706 | args = args || {}; | 706 | args = args || {}; |
707 | //MochiKit.Base.bindMethods(this); | 707 | //MochiKit.Base.bindMethods(this); |
diff --git a/frontend/gamma/js/Clipperz/Crypto/PRNG.js b/frontend/gamma/js/Clipperz/Crypto/PRNG.js index 7885429..80d972f 100644 --- a/frontend/gamma/js/Clipperz/Crypto/PRNG.js +++ b/frontend/gamma/js/Clipperz/Crypto/PRNG.js | |||
@@ -1,707 +1,707 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2013 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz, the online password manager. | 5 | This file is part of Clipperz, the online password manager. |
6 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
7 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
8 | 8 | ||
9 | * Clipperz is free software: you can redistribute it and/or modify it | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
10 | under the terms of the GNU Affero General Public License as published | 10 | under the terms of the GNU Affero General Public License as published |
11 | by the Free Software Foundation, either version 3 of the License, or | 11 | by the Free Software Foundation, either version 3 of the License, or |
12 | (at your option) any later version. | 12 | (at your option) any later version. |
13 | 13 | ||
14 | * Clipperz is distributed in the hope that it will be useful, but | 14 | * Clipperz is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
18 | 18 | ||
19 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
21 | 21 | ||
22 | */ | 22 | */ |
23 | 23 | ||
24 | "use strict"; | 24 | "use strict"; |
25 | 25 | ||
26 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | 26 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { |
27 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; | 27 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; |
28 | } | 28 | } |
29 | 29 | ||
30 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { | 30 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { |
31 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; | 31 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; |
32 | } | 32 | } |
33 | 33 | ||
34 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { | 34 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { |
35 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; | 35 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; |
36 | } | 36 | } |
37 | 37 | ||
38 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } | 38 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } |
39 | 39 | ||
40 | //############################################################################# | 40 | //############################################################################# |
41 | 41 | ||
42 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { | 42 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { |
43 | args = args || {}; | 43 | args = args || {}; |
44 | //MochiKit.Base.bindMethods(this); | 44 | //MochiKit.Base.bindMethods(this); |
45 | 45 | ||
46 | this._stack = new Clipperz.ByteArray(); | 46 | this._stack = new Clipperz.ByteArray(); |
47 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; | 47 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; |
48 | return this; | 48 | return this; |
49 | } | 49 | } |
50 | 50 | ||
51 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { | 51 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { |
52 | 52 | ||
53 | 'toString': function() { | 53 | 'toString': function() { |
54 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; | 54 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; |
55 | }, | 55 | }, |
56 | 56 | ||
57 | //------------------------------------------------------------------------- | 57 | //------------------------------------------------------------------------- |
58 | 58 | ||
59 | 'stack': function() { | 59 | 'stack': function() { |
60 | return this._stack; | 60 | return this._stack; |
61 | }, | 61 | }, |
62 | 62 | ||
63 | 'setStack': function(aValue) { | 63 | 'setStack': function(aValue) { |
64 | this._stack = aValue; | 64 | this._stack = aValue; |
65 | }, | 65 | }, |
66 | 66 | ||
67 | 'resetStack': function() { | 67 | 'resetStack': function() { |
68 | this.stack().reset(); | 68 | this.stack().reset(); |
69 | }, | 69 | }, |
70 | 70 | ||
71 | 'maxStackLengthBeforeHashing': function() { | 71 | 'maxStackLengthBeforeHashing': function() { |
72 | return this._maxStackLengthBeforeHashing; | 72 | return this._maxStackLengthBeforeHashing; |
73 | }, | 73 | }, |
74 | 74 | ||
75 | //------------------------------------------------------------------------- | 75 | //------------------------------------------------------------------------- |
76 | 76 | ||
77 | 'addRandomByte': function(aValue) { | 77 | 'addRandomByte': function(aValue) { |
78 | this.stack().appendByte(aValue); | 78 | this.stack().appendByte(aValue); |
79 | 79 | ||
80 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { | 80 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { |
81 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); | 81 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); |
82 | } | 82 | } |
83 | }, | 83 | }, |
84 | 84 | ||
85 | //------------------------------------------------------------------------- | 85 | //------------------------------------------------------------------------- |
86 | __syntaxFix__: "syntax fix" | 86 | __syntaxFix__: "syntax fix" |
87 | }); | 87 | }); |
88 | 88 | ||
89 | //############################################################################# | 89 | //############################################################################# |
90 | 90 | ||
91 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { | 91 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { |
92 | args = args || {}; | 92 | args = args || {}; |
93 | MochiKit.Base.bindMethods(this); | 93 | MochiKit.Base.bindMethods(this); |
94 | 94 | ||
95 | this._generator = args.generator || null; | 95 | this._generator = args.generator || null; |
96 | this._sourceId = args.sourceId || null; | 96 | this._sourceId = args.sourceId || null; |
97 | this._boostMode = args.boostMode || false; | 97 | this._boostMode = args.boostMode || false; |
98 | 98 | ||
99 | this._nextPoolIndex = 0; | 99 | this._nextPoolIndex = 0; |
100 | 100 | ||
101 | return this; | 101 | return this; |
102 | } | 102 | } |
103 | 103 | ||
104 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { | 104 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { |
105 | 105 | ||
106 | 'generator': function() { | 106 | 'generator': function() { |
107 | return this._generator; | 107 | return this._generator; |
108 | }, | 108 | }, |
109 | 109 | ||
110 | 'setGenerator': function(aValue) { | 110 | 'setGenerator': function(aValue) { |
111 | this._generator = aValue; | 111 | this._generator = aValue; |
112 | }, | 112 | }, |
113 | 113 | ||
114 | //------------------------------------------------------------------------- | 114 | //------------------------------------------------------------------------- |
115 | 115 | ||
116 | 'boostMode': function() { | 116 | 'boostMode': function() { |
117 | return this._boostMode; | 117 | return this._boostMode; |
118 | }, | 118 | }, |
119 | 119 | ||
120 | 'setBoostMode': function(aValue) { | 120 | 'setBoostMode': function(aValue) { |
121 | this._boostMode = aValue; | 121 | this._boostMode = aValue; |
122 | }, | 122 | }, |
123 | 123 | ||
124 | //------------------------------------------------------------------------- | 124 | //------------------------------------------------------------------------- |
125 | 125 | ||
126 | 'sourceId': function() { | 126 | 'sourceId': function() { |
127 | return this._sourceId; | 127 | return this._sourceId; |
128 | }, | 128 | }, |
129 | 129 | ||
130 | 'setSourceId': function(aValue) { | 130 | 'setSourceId': function(aValue) { |
131 | this._sourceId = aValue; | 131 | this._sourceId = aValue; |
132 | }, | 132 | }, |
133 | 133 | ||
134 | //------------------------------------------------------------------------- | 134 | //------------------------------------------------------------------------- |
135 | 135 | ||
136 | 'nextPoolIndex': function() { | 136 | 'nextPoolIndex': function() { |
137 | return this._nextPoolIndex; | 137 | return this._nextPoolIndex; |
138 | }, | 138 | }, |
139 | 139 | ||
140 | 'incrementNextPoolIndex': function() { | 140 | 'incrementNextPoolIndex': function() { |
141 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); | 141 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); |
142 | }, | 142 | }, |
143 | 143 | ||
144 | //------------------------------------------------------------------------- | 144 | //------------------------------------------------------------------------- |
145 | 145 | ||
146 | 'updateGeneratorWithValue': function(aRandomValue) { | 146 | 'updateGeneratorWithValue': function(aRandomValue) { |
147 | if (this.generator() != null) { | 147 | if (this.generator() != null) { |
148 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); | 148 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); |
149 | this.incrementNextPoolIndex(); | 149 | this.incrementNextPoolIndex(); |
150 | } | 150 | } |
151 | }, | 151 | }, |
152 | 152 | ||
153 | //------------------------------------------------------------------------- | 153 | //------------------------------------------------------------------------- |
154 | __syntaxFix__: "syntax fix" | 154 | __syntaxFix__: "syntax fix" |
155 | }); | 155 | }); |
156 | 156 | ||
157 | //############################################################################# | 157 | //############################################################################# |
158 | 158 | ||
159 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { | 159 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { |
160 | args = args || {}; | 160 | args = args || {}; |
161 | //MochiKit.Base.bindMethods(this); | 161 | //MochiKit.Base.bindMethods(this); |
162 | 162 | ||
163 | this._intervalTime = args.intervalTime || 1000; | 163 | this._intervalTime = args.intervalTime || 1000; |
164 | 164 | ||
165 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 165 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
166 | 166 | ||
167 | this.collectEntropy(); | 167 | this.collectEntropy(); |
168 | return this; | 168 | return this; |
169 | } | 169 | } |
170 | 170 | ||
171 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 171 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
172 | 172 | ||
173 | 'intervalTime': function() { | 173 | 'intervalTime': function() { |
174 | return this._intervalTime; | 174 | return this._intervalTime; |
175 | }, | 175 | }, |
176 | 176 | ||
177 | //------------------------------------------------------------------------- | 177 | //------------------------------------------------------------------------- |
178 | 178 | ||
179 | 'collectEntropy': function() { | 179 | 'collectEntropy': function() { |
180 | varnow; | 180 | varnow; |
181 | varentropyByte; | 181 | varentropyByte; |
182 | var intervalTime; | 182 | var intervalTime; |
183 | now = new Date(); | 183 | now = new Date(); |
184 | entropyByte = (now.getTime() & 0xff); | 184 | entropyByte = (now.getTime() & 0xff); |
185 | 185 | ||
186 | intervalTime = this.intervalTime(); | 186 | intervalTime = this.intervalTime(); |
187 | if (this.boostMode() == true) { | 187 | if (this.boostMode() == true) { |
188 | intervalTime = intervalTime / 9; | 188 | intervalTime = intervalTime / 9; |
189 | } | 189 | } |
190 | 190 | ||
191 | this.updateGeneratorWithValue(entropyByte); | 191 | this.updateGeneratorWithValue(entropyByte); |
192 | setTimeout(this.collectEntropy, intervalTime); | 192 | setTimeout(this.collectEntropy, intervalTime); |
193 | }, | 193 | }, |
194 | 194 | ||
195 | //------------------------------------------------------------------------- | 195 | //------------------------------------------------------------------------- |
196 | 196 | ||
197 | 'numberOfRandomBits': function() { | 197 | 'numberOfRandomBits': function() { |
198 | return 5; | 198 | return 5; |
199 | }, | 199 | }, |
200 | 200 | ||
201 | //------------------------------------------------------------------------- | 201 | //------------------------------------------------------------------------- |
202 | __syntaxFix__: "syntax fix" | 202 | __syntaxFix__: "syntax fix" |
203 | }); | 203 | }); |
204 | 204 | ||
205 | //***************************************************************************** | 205 | //***************************************************************************** |
206 | 206 | ||
207 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { | 207 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { |
208 | args = args || {}; | 208 | args = args || {}; |
209 | 209 | ||
210 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 210 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
211 | 211 | ||
212 | this._numberOfBitsToCollectAtEachEvent = 4; | 212 | this._numberOfBitsToCollectAtEachEvent = 4; |
213 | this._randomBitsCollector = 0; | 213 | this._randomBitsCollector = 0; |
214 | this._numberOfRandomBitsCollected = 0; | 214 | this._numberOfRandomBitsCollected = 0; |
215 | 215 | ||
216 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); | 216 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); |
217 | 217 | ||
218 | return this; | 218 | return this; |
219 | } | 219 | } |
220 | 220 | ||
221 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 221 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
222 | 222 | ||
223 | //------------------------------------------------------------------------- | 223 | //------------------------------------------------------------------------- |
224 | 224 | ||
225 | 'numberOfBitsToCollectAtEachEvent': function() { | 225 | 'numberOfBitsToCollectAtEachEvent': function() { |
226 | return this._numberOfBitsToCollectAtEachEvent; | 226 | return this._numberOfBitsToCollectAtEachEvent; |
227 | }, | 227 | }, |
228 | 228 | ||
229 | //------------------------------------------------------------------------- | 229 | //------------------------------------------------------------------------- |
230 | 230 | ||
231 | 'randomBitsCollector': function() { | 231 | 'randomBitsCollector': function() { |
232 | return this._randomBitsCollector; | 232 | return this._randomBitsCollector; |
233 | }, | 233 | }, |
234 | 234 | ||
235 | 'setRandomBitsCollector': function(aValue) { | 235 | 'setRandomBitsCollector': function(aValue) { |
236 | this._randomBitsCollector = aValue; | 236 | this._randomBitsCollector = aValue; |
237 | }, | 237 | }, |
238 | 238 | ||
239 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { | 239 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { |
240 | var collectedBits; | 240 | var collectedBits; |
241 | var numberOfRandomBitsCollected; | 241 | var numberOfRandomBitsCollected; |
242 | 242 | ||
243 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | 243 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); |
244 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | 244 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); |
245 | this.setRandomBitsCollector(collectedBits); | 245 | this.setRandomBitsCollector(collectedBits); |
246 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); | 246 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); |
247 | 247 | ||
248 | if (numberOfRandomBitsCollected == 8) { | 248 | if (numberOfRandomBitsCollected == 8) { |
249 | this.updateGeneratorWithValue(collectedBits); | 249 | this.updateGeneratorWithValue(collectedBits); |
250 | numberOfRandomBitsCollected = 0; | 250 | numberOfRandomBitsCollected = 0; |
251 | this.setRandomBitsCollector(0); | 251 | this.setRandomBitsCollector(0); |
252 | } | 252 | } |
253 | 253 | ||
254 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | 254 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) |
255 | }, | 255 | }, |
256 | 256 | ||
257 | //------------------------------------------------------------------------- | 257 | //------------------------------------------------------------------------- |
258 | 258 | ||
259 | 'numberOfRandomBitsCollected': function() { | 259 | 'numberOfRandomBitsCollected': function() { |
260 | return this._numberOfRandomBitsCollected; | 260 | return this._numberOfRandomBitsCollected; |
261 | }, | 261 | }, |
262 | 262 | ||
263 | 'setNumberOfRandomBitsCollected': function(aValue) { | 263 | 'setNumberOfRandomBitsCollected': function(aValue) { |
264 | this._numberOfRandomBitsCollected = aValue; | 264 | this._numberOfRandomBitsCollected = aValue; |
265 | }, | 265 | }, |
266 | 266 | ||
267 | //------------------------------------------------------------------------- | 267 | //------------------------------------------------------------------------- |
268 | 268 | ||
269 | 'collectEntropy': function(anEvent) { | 269 | 'collectEntropy': function(anEvent) { |
270 | var mouseLocation; | 270 | var mouseLocation; |
271 | var randomBit; | 271 | var randomBit; |
272 | var mask; | 272 | var mask; |
273 | 273 | ||
274 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); | 274 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); |
275 | 275 | ||
276 | mouseLocation = anEvent.mouse().client; | 276 | mouseLocation = anEvent.mouse().client; |
277 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); | 277 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); |
278 | this.appendRandomBitsToRandomBitsCollector(randomBit) | 278 | this.appendRandomBitsToRandomBitsCollector(randomBit) |
279 | }, | 279 | }, |
280 | 280 | ||
281 | //------------------------------------------------------------------------- | 281 | //------------------------------------------------------------------------- |
282 | 282 | ||
283 | 'numberOfRandomBits': function() { | 283 | 'numberOfRandomBits': function() { |
284 | return 1; | 284 | return 1; |
285 | }, | 285 | }, |
286 | 286 | ||
287 | //------------------------------------------------------------------------- | 287 | //------------------------------------------------------------------------- |
288 | __syntaxFix__: "syntax fix" | 288 | __syntaxFix__: "syntax fix" |
289 | }); | 289 | }); |
290 | 290 | ||
291 | //***************************************************************************** | 291 | //***************************************************************************** |
292 | 292 | ||
293 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { | 293 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { |
294 | args = args || {}; | 294 | args = args || {}; |
295 | 295 | ||
296 | this._intervalTime = args.intervalTime || 1000; | 296 | this._intervalTime = args.intervalTime || 1000; |
297 | this._browserCrypto = args.browserCrypto; | 297 | this._browserCrypto = args.browserCrypto; |
298 | 298 | ||
299 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 299 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
300 | 300 | ||
301 | this.collectEntropy(); | 301 | this.collectEntropy(); |
302 | return this; | 302 | return this; |
303 | } | 303 | } |
304 | 304 | ||
305 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 305 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
306 | 306 | ||
307 | 'intervalTime': function() { | 307 | 'intervalTime': function() { |
308 | return this._intervalTime; | 308 | return this._intervalTime; |
309 | }, | 309 | }, |
310 | 310 | ||
311 | 'browserCrypto': function () { | 311 | 'browserCrypto': function () { |
312 | return this._browserCrypto; | 312 | return this._browserCrypto; |
313 | }, | 313 | }, |
314 | 314 | ||
315 | //------------------------------------------------------------------------- | 315 | //------------------------------------------------------------------------- |
316 | 316 | ||
317 | 'collectEntropy': function() { | 317 | 'collectEntropy': function() { |
318 | varbytesToCollect; | 318 | varbytesToCollect; |
319 | 319 | ||
320 | if (this.boostMode() == true) { | 320 | if (this.boostMode() == true) { |
321 | bytesToCollect = 8; | 321 | bytesToCollect = 64; |
322 | } else { | 322 | } else { |
323 | bytesToCollect = 32; | 323 | bytesToCollect = 8; |
324 | } | 324 | } |
325 | 325 | ||
326 | var randomValuesArray = new Uint8Array(bytesToCollect); | 326 | var randomValuesArray = new Uint8Array(bytesToCollect); |
327 | this.browserCrypto().getRandomValues(randomValuesArray); | 327 | this.browserCrypto().getRandomValues(randomValuesArray); |
328 | for (var i = 0; i < randomValuesArray.length; i++) { | 328 | for (var i = 0; i < randomValuesArray.length; i++) { |
329 | this.updateGeneratorWithValue(randomValuesArray[i]); | 329 | this.updateGeneratorWithValue(randomValuesArray[i]); |
330 | } | 330 | } |
331 | 331 | ||
332 | setTimeout(this.collectEntropy, this.intervalTime()); | 332 | setTimeout(this.collectEntropy, this.intervalTime()); |
333 | }, | 333 | }, |
334 | 334 | ||
335 | //------------------------------------------------------------------------- | 335 | //------------------------------------------------------------------------- |
336 | __syntaxFix__: "syntax fix" | 336 | __syntaxFix__: "syntax fix" |
337 | }); | 337 | }); |
338 | 338 | ||
339 | //############################################################################# | 339 | //############################################################################# |
340 | 340 | ||
341 | Clipperz.Crypto.PRNG.Fortuna = function(args) { | 341 | Clipperz.Crypto.PRNG.Fortuna = function(args) { |
342 | vari,c; | 342 | vari,c; |
343 | 343 | ||
344 | args = args || {}; | 344 | args = args || {}; |
345 | 345 | ||
346 | this._key = args.seed || null; | 346 | this._key = args.seed || null; |
347 | if (this._key == null) { | 347 | if (this._key == null) { |
348 | this._counter = 0; | 348 | this._counter = 0; |
349 | this._key = new Clipperz.ByteArray(); | 349 | this._key = new Clipperz.ByteArray(); |
350 | } else { | 350 | } else { |
351 | this._counter = 1; | 351 | this._counter = 1; |
352 | } | 352 | } |
353 | 353 | ||
354 | this._aesKey = null; | 354 | this._aesKey = null; |
355 | 355 | ||
356 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; | 356 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; |
357 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; | 357 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; |
358 | 358 | ||
359 | this._accumulators = []; | 359 | this._accumulators = []; |
360 | c = this.numberOfEntropyAccumulators(); | 360 | c = this.numberOfEntropyAccumulators(); |
361 | for (i=0; i<c; i++) { | 361 | for (i=0; i<c; i++) { |
362 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); | 362 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); |
363 | } | 363 | } |
364 | 364 | ||
365 | this._randomnessSources = []; | 365 | this._randomnessSources = []; |
366 | this._reseedCounter = 0; | 366 | this._reseedCounter = 0; |
367 | 367 | ||
368 | return this; | 368 | return this; |
369 | } | 369 | } |
370 | 370 | ||
371 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { | 371 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { |
372 | 372 | ||
373 | 'toString': function() { | 373 | 'toString': function() { |
374 | return "Clipperz.Crypto.PRNG.Fortuna"; | 374 | return "Clipperz.Crypto.PRNG.Fortuna"; |
375 | }, | 375 | }, |
376 | 376 | ||
377 | //------------------------------------------------------------------------- | 377 | //------------------------------------------------------------------------- |
378 | 378 | ||
379 | 'key': function() { | 379 | 'key': function() { |
380 | return this._key; | 380 | return this._key; |
381 | }, | 381 | }, |
382 | 382 | ||
383 | 'setKey': function(aValue) { | 383 | 'setKey': function(aValue) { |
384 | this._key = aValue; | 384 | this._key = aValue; |
385 | this._aesKey = null; | 385 | this._aesKey = null; |
386 | }, | 386 | }, |
387 | 387 | ||
388 | 'aesKey': function() { | 388 | 'aesKey': function() { |
389 | if (this._aesKey == null) { | 389 | if (this._aesKey == null) { |
390 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); | 390 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); |
391 | } | 391 | } |
392 | 392 | ||
393 | return this._aesKey; | 393 | return this._aesKey; |
394 | }, | 394 | }, |
395 | 395 | ||
396 | 'accumulators': function() { | 396 | 'accumulators': function() { |
397 | return this._accumulators; | 397 | return this._accumulators; |
398 | }, | 398 | }, |
399 | 399 | ||
400 | 'firstPoolReseedLevel': function() { | 400 | 'firstPoolReseedLevel': function() { |
401 | return this._firstPoolReseedLevel; | 401 | return this._firstPoolReseedLevel; |
402 | }, | 402 | }, |
403 | 403 | ||
404 | //------------------------------------------------------------------------- | 404 | //------------------------------------------------------------------------- |
405 | 405 | ||
406 | 'reseedCounter': function() { | 406 | 'reseedCounter': function() { |
407 | return this._reseedCounter; | 407 | return this._reseedCounter; |
408 | }, | 408 | }, |
409 | 409 | ||
410 | 'incrementReseedCounter': function() { | 410 | 'incrementReseedCounter': function() { |
411 | this._reseedCounter = this._reseedCounter +1; | 411 | this._reseedCounter = this._reseedCounter +1; |
412 | }, | 412 | }, |
413 | 413 | ||
414 | //------------------------------------------------------------------------- | 414 | //------------------------------------------------------------------------- |
415 | 415 | ||
416 | 'reseed': function() { | 416 | 'reseed': function() { |
417 | varnewKeySeed; | 417 | varnewKeySeed; |
418 | var reseedCounter; | 418 | var reseedCounter; |
419 | varreseedCounterMask; | 419 | varreseedCounterMask; |
420 | var i, c; | 420 | var i, c; |
421 | 421 | ||
422 | newKeySeed = this.key(); | 422 | newKeySeed = this.key(); |
423 | this.incrementReseedCounter(); | 423 | this.incrementReseedCounter(); |
424 | reseedCounter = this.reseedCounter(); | 424 | reseedCounter = this.reseedCounter(); |
425 | 425 | ||
426 | c = this.numberOfEntropyAccumulators(); | 426 | c = this.numberOfEntropyAccumulators(); |
427 | reseedCounterMask = 0xffffffff >>> (32 - c); | 427 | reseedCounterMask = 0xffffffff >>> (32 - c); |
428 | for (i=0; i<c; i++) { | 428 | for (i=0; i<c; i++) { |
429 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { | 429 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { |
430 | newKeySeed.appendBlock(this.accumulators()[i].stack()); | 430 | newKeySeed.appendBlock(this.accumulators()[i].stack()); |
431 | this.accumulators()[i].resetStack(); | 431 | this.accumulators()[i].resetStack(); |
432 | } | 432 | } |
433 | } | 433 | } |
434 | 434 | ||
435 | if (reseedCounter == 1) { | 435 | if (reseedCounter == 1) { |
436 | c = this.randomnessSources().length; | 436 | c = this.randomnessSources().length; |
437 | for (i=0; i<c; i++) { | 437 | for (i=0; i<c; i++) { |
438 | this.randomnessSources()[i].setBoostMode(false); | 438 | this.randomnessSources()[i].setBoostMode(false); |
439 | } | 439 | } |
440 | } | 440 | } |
441 | 441 | ||
442 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); | 442 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); |
443 | if (reseedCounter == 1) { | 443 | if (reseedCounter == 1) { |
444 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); | 444 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); |
445 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); | 445 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); |
446 | } | 446 | } |
447 | MochiKit.Signal.signal(this, 'reseeded'); | 447 | MochiKit.Signal.signal(this, 'reseeded'); |
448 | }, | 448 | }, |
449 | 449 | ||
450 | //------------------------------------------------------------------------- | 450 | //------------------------------------------------------------------------- |
451 | 451 | ||
452 | 'isReadyToGenerateRandomValues': function() { | 452 | 'isReadyToGenerateRandomValues': function() { |
453 | return this.reseedCounter() != 0; | 453 | return this.reseedCounter() != 0; |
454 | }, | 454 | }, |
455 | 455 | ||
456 | //------------------------------------------------------------------------- | 456 | //------------------------------------------------------------------------- |
457 | 457 | ||
458 | 'entropyLevel': function() { | 458 | 'entropyLevel': function() { |
459 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); | 459 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); |
460 | }, | 460 | }, |
461 | 461 | ||
462 | //------------------------------------------------------------------------- | 462 | //------------------------------------------------------------------------- |
463 | 463 | ||
464 | 'counter': function() { | 464 | 'counter': function() { |
465 | return this._counter; | 465 | return this._counter; |
466 | }, | 466 | }, |
467 | 467 | ||
468 | 'incrementCounter': function() { | 468 | 'incrementCounter': function() { |
469 | this._counter += 1; | 469 | this._counter += 1; |
470 | }, | 470 | }, |
471 | 471 | ||
472 | 'counterBlock': function() { | 472 | 'counterBlock': function() { |
473 | var result; | 473 | var result; |
474 | 474 | ||
475 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); | 475 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); |
476 | 476 | ||
477 | return result; | 477 | return result; |
478 | }, | 478 | }, |
479 | 479 | ||
480 | //------------------------------------------------------------------------- | 480 | //------------------------------------------------------------------------- |
481 | 481 | ||
482 | 'getRandomBlock': function() { | 482 | 'getRandomBlock': function() { |
483 | var result; | 483 | var result; |
484 | 484 | ||
485 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); | 485 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); |
486 | this.incrementCounter(); | 486 | this.incrementCounter(); |
487 | 487 | ||
488 | return result; | 488 | return result; |
489 | }, | 489 | }, |
490 | 490 | ||
491 | //------------------------------------------------------------------------- | 491 | //------------------------------------------------------------------------- |
492 | 492 | ||
493 | 'getRandomBytes': function(aSize) { | 493 | 'getRandomBytes': function(aSize) { |
494 | var result; | 494 | var result; |
495 | 495 | ||
496 | if (this.isReadyToGenerateRandomValues()) { | 496 | if (this.isReadyToGenerateRandomValues()) { |
497 | var i,c; | 497 | var i,c; |
498 | var newKey; | 498 | var newKey; |
499 | 499 | ||
500 | result = new Clipperz.ByteArray(); | 500 | result = new Clipperz.ByteArray(); |
501 | 501 | ||
502 | c = Math.ceil(aSize / (128 / 8)); | 502 | c = Math.ceil(aSize / (128 / 8)); |
503 | for (i=0; i<c; i++) { | 503 | for (i=0; i<c; i++) { |
504 | result.appendBlock(this.getRandomBlock()); | 504 | result.appendBlock(this.getRandomBlock()); |
505 | } | 505 | } |
506 | 506 | ||
507 | if (result.length() != aSize) { | 507 | if (result.length() != aSize) { |
508 | result = result.split(0, aSize); | 508 | result = result.split(0, aSize); |
509 | } | 509 | } |
510 | 510 | ||
511 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); | 511 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); |
512 | this.setKey(newKey); | 512 | this.setKey(newKey); |
513 | } else { | 513 | } else { |
514 | Clipperz.logWarning("Fortuna generator has not enough entropy, yet!"); | 514 | Clipperz.logWarning("Fortuna generator has not enough entropy, yet!"); |
515 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; | 515 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; |
516 | } | 516 | } |
517 | 517 | ||
518 | return result; | 518 | return result; |
519 | }, | 519 | }, |
520 | 520 | ||
521 | //------------------------------------------------------------------------- | 521 | //------------------------------------------------------------------------- |
522 | 522 | ||
523 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { | 523 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { |
524 | varselectedAccumulator; | 524 | varselectedAccumulator; |
525 | 525 | ||
526 | selectedAccumulator = this.accumulators()[aPoolId]; | 526 | selectedAccumulator = this.accumulators()[aPoolId]; |
527 | selectedAccumulator.addRandomByte(aRandomValue); | 527 | selectedAccumulator.addRandomByte(aRandomValue); |
528 | 528 | ||
529 | if (aPoolId == 0) { | 529 | if (aPoolId == 0) { |
530 | MochiKit.Signal.signal(this, 'addedRandomByte') | 530 | MochiKit.Signal.signal(this, 'addedRandomByte') |
531 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { | 531 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { |
532 | this.reseed(); | 532 | this.reseed(); |
533 | } | 533 | } |
534 | } | 534 | } |
535 | }, | 535 | }, |
536 | 536 | ||
537 | //------------------------------------------------------------------------- | 537 | //------------------------------------------------------------------------- |
538 | 538 | ||
539 | 'numberOfEntropyAccumulators': function() { | 539 | 'numberOfEntropyAccumulators': function() { |
540 | return this._numberOfEntropyAccumulators; | 540 | return this._numberOfEntropyAccumulators; |
541 | }, | 541 | }, |
542 | 542 | ||
543 | //------------------------------------------------------------------------- | 543 | //------------------------------------------------------------------------- |
544 | 544 | ||
545 | 'randomnessSources': function() { | 545 | 'randomnessSources': function() { |
546 | return this._randomnessSources; | 546 | return this._randomnessSources; |
547 | }, | 547 | }, |
548 | 548 | ||
549 | 'addRandomnessSource': function(aRandomnessSource) { | 549 | 'addRandomnessSource': function(aRandomnessSource) { |
550 | aRandomnessSource.setGenerator(this); | 550 | aRandomnessSource.setGenerator(this); |
551 | aRandomnessSource.setSourceId(this.randomnessSources().length); | 551 | aRandomnessSource.setSourceId(this.randomnessSources().length); |
552 | this.randomnessSources().push(aRandomnessSource); | 552 | this.randomnessSources().push(aRandomnessSource); |
553 | 553 | ||
554 | if (this.isReadyToGenerateRandomValues() == false) { | 554 | if (this.isReadyToGenerateRandomValues() == false) { |
555 | aRandomnessSource.setBoostMode(true); | 555 | aRandomnessSource.setBoostMode(true); |
556 | } | 556 | } |
557 | }, | 557 | }, |
558 | 558 | ||
559 | //------------------------------------------------------------------------- | 559 | //------------------------------------------------------------------------- |
560 | 560 | ||
561 | 'deferredEntropyCollection': function(aValue) { | 561 | 'deferredEntropyCollection': function(aValue) { |
562 | var result; | 562 | var result; |
563 | 563 | ||
564 | 564 | ||
565 | if (this.isReadyToGenerateRandomValues()) { | 565 | if (this.isReadyToGenerateRandomValues()) { |
566 | result = aValue; | 566 | result = aValue; |
567 | } else { | 567 | } else { |
568 | var deferredResult; | 568 | var deferredResult; |
569 | 569 | ||
570 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); | 570 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); |
571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); | 571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); |
572 | MochiKit.Signal.connect(this, | 572 | MochiKit.Signal.connect(this, |
573 | 'readyToGenerateRandomBytes', | 573 | 'readyToGenerateRandomBytes', |
574 | deferredResult, | 574 | deferredResult, |
575 | 'callback'); | 575 | 'callback'); |
576 | 576 | ||
577 | result = deferredResult; | 577 | result = deferredResult; |
578 | } | 578 | } |
579 | 579 | ||
580 | return result; | 580 | return result; |
581 | }, | 581 | }, |
582 | 582 | ||
583 | //------------------------------------------------------------------------- | 583 | //------------------------------------------------------------------------- |
584 | 584 | ||
585 | 'fastEntropyAccumulationForTestingPurpose': function() { | 585 | 'fastEntropyAccumulationForTestingPurpose': function() { |
586 | while (! this.isReadyToGenerateRandomValues()) { | 586 | while (! this.isReadyToGenerateRandomValues()) { |
587 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); | 587 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); |
588 | } | 588 | } |
589 | }, | 589 | }, |
590 | 590 | ||
591 | //------------------------------------------------------------------------- | 591 | //------------------------------------------------------------------------- |
592 | /* | 592 | /* |
593 | 'dump': function(appendToDoc) { | 593 | 'dump': function(appendToDoc) { |
594 | var tbl; | 594 | var tbl; |
595 | var i,c; | 595 | var i,c; |
596 | 596 | ||
597 | tbl = document.createElement("table"); | 597 | tbl = document.createElement("table"); |
598 | tbl.border = 0; | 598 | tbl.border = 0; |
599 | with (tbl.style) { | 599 | with (tbl.style) { |
600 | border = "1px solid lightgrey"; | 600 | border = "1px solid lightgrey"; |
601 | fontFamily = 'Helvetica, Arial, sans-serif'; | 601 | fontFamily = 'Helvetica, Arial, sans-serif'; |
602 | fontSize = '8pt'; | 602 | fontSize = '8pt'; |
603 | //borderCollapse = "collapse"; | 603 | //borderCollapse = "collapse"; |
604 | } | 604 | } |
605 | var hdr = tbl.createTHead(); | 605 | var hdr = tbl.createTHead(); |
606 | var hdrtr = hdr.insertRow(0); | 606 | var hdrtr = hdr.insertRow(0); |
607 | // document.createElement("tr"); | 607 | // document.createElement("tr"); |
608 | { | 608 | { |
609 | var ntd; | 609 | var ntd; |
610 | 610 | ||
611 | ntd = hdrtr.insertCell(0); | 611 | ntd = hdrtr.insertCell(0); |
612 | ntd.style.borderBottom = "1px solid lightgrey"; | 612 | ntd.style.borderBottom = "1px solid lightgrey"; |
613 | ntd.style.borderRight = "1px solid lightgrey"; | 613 | ntd.style.borderRight = "1px solid lightgrey"; |
614 | ntd.appendChild(document.createTextNode("#")); | 614 | ntd.appendChild(document.createTextNode("#")); |
615 | 615 | ||
616 | ntd = hdrtr.insertCell(1); | 616 | ntd = hdrtr.insertCell(1); |
617 | ntd.style.borderBottom = "1px solid lightgrey"; | 617 | ntd.style.borderBottom = "1px solid lightgrey"; |
618 | ntd.style.borderRight = "1px solid lightgrey"; | 618 | ntd.style.borderRight = "1px solid lightgrey"; |
619 | ntd.appendChild(document.createTextNode("s")); | 619 | ntd.appendChild(document.createTextNode("s")); |
620 | 620 | ||
621 | ntd = hdrtr.insertCell(2); | 621 | ntd = hdrtr.insertCell(2); |
622 | ntd.colSpan = this.firstPoolReseedLevel(); | 622 | ntd.colSpan = this.firstPoolReseedLevel(); |
623 | ntd.style.borderBottom = "1px solid lightgrey"; | 623 | ntd.style.borderBottom = "1px solid lightgrey"; |
624 | ntd.style.borderRight = "1px solid lightgrey"; | 624 | ntd.style.borderRight = "1px solid lightgrey"; |
625 | ntd.appendChild(document.createTextNode("base values")); | 625 | ntd.appendChild(document.createTextNode("base values")); |
626 | 626 | ||
627 | ntd = hdrtr.insertCell(3); | 627 | ntd = hdrtr.insertCell(3); |
628 | ntd.colSpan = 20; | 628 | ntd.colSpan = 20; |
629 | ntd.style.borderBottom = "1px solid lightgrey"; | 629 | ntd.style.borderBottom = "1px solid lightgrey"; |
630 | ntd.appendChild(document.createTextNode("extra values")); | 630 | ntd.appendChild(document.createTextNode("extra values")); |
631 | 631 | ||
632 | } | 632 | } |
633 | 633 | ||
634 | c = this.accumulators().length; | 634 | c = this.accumulators().length; |
635 | for (i=0; i<c ; i++) { | 635 | for (i=0; i<c ; i++) { |
636 | varcurrentAccumulator; | 636 | varcurrentAccumulator; |
637 | var bdytr; | 637 | var bdytr; |
638 | var bdytd; | 638 | var bdytd; |
639 | var ii, cc; | 639 | var ii, cc; |
640 | 640 | ||
641 | currentAccumulator = this.accumulators()[i] | 641 | currentAccumulator = this.accumulators()[i] |
642 | 642 | ||
643 | bdytr = tbl.insertRow(true); | 643 | bdytr = tbl.insertRow(true); |
644 | 644 | ||
645 | bdytd = bdytr.insertCell(0); | 645 | bdytd = bdytr.insertCell(0); |
646 | bdytd.style.borderRight = "1px solid lightgrey"; | 646 | bdytd.style.borderRight = "1px solid lightgrey"; |
647 | bdytd.style.color = "lightgrey"; | 647 | bdytd.style.color = "lightgrey"; |
648 | bdytd.appendChild(document.createTextNode("" + i)); | 648 | bdytd.appendChild(document.createTextNode("" + i)); |
649 | 649 | ||
650 | bdytd = bdytr.insertCell(1); | 650 | bdytd = bdytr.insertCell(1); |
651 | bdytd.style.borderRight = "1px solid lightgrey"; | 651 | bdytd.style.borderRight = "1px solid lightgrey"; |
652 | bdytd.style.color = "gray"; | 652 | bdytd.style.color = "gray"; |
653 | bdytd.appendChild(document.createTextNode("" + currentAccumulator.stack().length())); | 653 | bdytd.appendChild(document.createTextNode("" + currentAccumulator.stack().length())); |
654 | 654 | ||
655 | 655 | ||
656 | cc = Math.max(currentAccumulator.stack().length(), this.firstPoolReseedLevel()); | 656 | cc = Math.max(currentAccumulator.stack().length(), this.firstPoolReseedLevel()); |
657 | for (ii=0; ii<cc; ii++) { | 657 | for (ii=0; ii<cc; ii++) { |
658 | var cellText; | 658 | var cellText; |
659 | 659 | ||
660 | bdytd = bdytr.insertCell(ii + 2); | 660 | bdytd = bdytr.insertCell(ii + 2); |
661 | 661 | ||
662 | if (ii < currentAccumulator.stack().length()) { | 662 | if (ii < currentAccumulator.stack().length()) { |
663 | cellText = Clipperz.ByteArray.byteToHex(currentAccumulator.stack().byteAtIndex(ii)); | 663 | cellText = Clipperz.ByteArray.byteToHex(currentAccumulator.stack().byteAtIndex(ii)); |
664 | } else { | 664 | } else { |
665 | cellText = "_"; | 665 | cellText = "_"; |
666 | } | 666 | } |
667 | 667 | ||
668 | if (ii == (this.firstPoolReseedLevel() - 1)) { | 668 | if (ii == (this.firstPoolReseedLevel() - 1)) { |
669 | bdytd.style.borderRight = "1px solid lightgrey"; | 669 | bdytd.style.borderRight = "1px solid lightgrey"; |
670 | } | 670 | } |
671 | 671 | ||
672 | bdytd.appendChild(document.createTextNode(cellText)); | 672 | bdytd.appendChild(document.createTextNode(cellText)); |
673 | } | 673 | } |
674 | 674 | ||
675 | } | 675 | } |
676 | 676 | ||
677 | 677 | ||
678 | if (appendToDoc) { | 678 | if (appendToDoc) { |
679 | var ne = document.createElement("div"); | 679 | var ne = document.createElement("div"); |
680 | ne.id = "entropyGeneratorStatus"; | 680 | ne.id = "entropyGeneratorStatus"; |
681 | with (ne.style) { | 681 | with (ne.style) { |
682 | fontFamily = "Courier New, monospace"; | 682 | fontFamily = "Courier New, monospace"; |
683 | fontSize = "12px"; | 683 | fontSize = "12px"; |
684 | lineHeight = "16px"; | 684 | lineHeight = "16px"; |
685 | borderTop = "1px solid black"; | 685 | borderTop = "1px solid black"; |
686 | padding = "10px"; | 686 | padding = "10px"; |
687 | } | 687 | } |
688 | if (document.getElementById(ne.id)) { | 688 | if (document.getElementById(ne.id)) { |
689 | MochiKit.DOM.swapDOM(ne.id, ne); | 689 | MochiKit.DOM.swapDOM(ne.id, ne); |
690 | } else { | 690 | } else { |
691 | document.body.appendChild(ne); | 691 | document.body.appendChild(ne); |
692 | } | 692 | } |
693 | ne.appendChild(tbl); | 693 | ne.appendChild(tbl); |
694 | } | 694 | } |
695 | 695 | ||
696 | return tbl; | 696 | return tbl; |
697 | }, | 697 | }, |
698 | */ | 698 | */ |
699 | //----------------------------------------------------------------------------- | 699 | //----------------------------------------------------------------------------- |
700 | __syntaxFix__: "syntax fix" | 700 | __syntaxFix__: "syntax fix" |
701 | }); | 701 | }); |
702 | 702 | ||
703 | //############################################################################# | 703 | //############################################################################# |
704 | 704 | ||
705 | Clipperz.Crypto.PRNG.Random = function(args) { | 705 | Clipperz.Crypto.PRNG.Random = function(args) { |
706 | args = args || {}; | 706 | args = args || {}; |
707 | //MochiKit.Base.bindMethods(this); | 707 | //MochiKit.Base.bindMethods(this); |