diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2012-01-16 23:24:40 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2012-01-16 23:24:40 -0500 |
| commit | 2b679d318b65d090fb75668d26d6e85ea250b771 (patch) | |
| tree | 5053a7f40c105f1883e31bc93994cf40a52bcbc5 /webclient/lib/sjcl.js | |
| parent | 18ac8babb1d40b291468058d9125a1598434f543 (diff) | |
| parent | 73b2c72961544b42229dd334fc75a20d52acff9b (diff) | |
| download | alias-2b679d318b65d090fb75668d26d6e85ea250b771.tar.gz | |
Merge branch 'master' of alias.fr.nf:alias
Diffstat (limited to 'webclient/lib/sjcl.js')
| -rw-r--r-- | webclient/lib/sjcl.js | 94 |
1 files changed, 52 insertions, 42 deletions
diff --git a/webclient/lib/sjcl.js b/webclient/lib/sjcl.js index bb7262a..24d3e3b 100644 --- a/webclient/lib/sjcl.js +++ b/webclient/lib/sjcl.js @@ -19,6 +19,9 @@ var sjcl = { /** @namespace Hash functions. Right now only SHA256 is implemented. */ hash: {}, + + /** @namespace Key exchange functions. Right now only SRP is implemented. */ + keyexchange: {}, /** @namespace Block cipher modes of operation. */ mode: {}, @@ -55,6 +58,12 @@ var sjcl = { bug: function(message) { this.toString = function() { return "BUG: "+this.message; }; this.message = message; + }, + + /** @class Something isn't ready. */ + notReady: function(message) { + this.toString = function() { return "NOT READY: "+this.message; }; + this.message = message; } } }; @@ -311,6 +320,27 @@ sjcl.bitArray = { }, /** + * Extract a number packed into a bit array. + * @param {bitArray} a The array to slice. + * @param {Number} bstart The offset to the start of the slice, in bits. + * @param {Number} length The length of the number to extract. + * @return {Number} The requested slice. + */ + extract: function(a, bstart, blength) { + // FIXME: this Math.floor is not necessary at all, but for some reason + // seems to suppress a bug in the Chromium JIT. + var x, sh = Math.floor((-bstart-blength) & 31); + if ((bstart + blength - 1 ^ bstart) & -32) { + // it crosses a boundary + x = (a[bstart/32|0] << (32 - sh)) ^ (a[bstart/32+1|0] >>> sh); + } else { + // within a single word + x = a[bstart/32|0] >>> sh; + } + return x & ((1<<blength) - 1); + }, + + /** * Concatenate two bit arrays. * @param {bitArray} a1 The first array. * @param {bitArray} a2 The second array. @@ -516,8 +546,9 @@ sjcl.codec.base64 = { _chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", /** Convert from a bitArray to a base64 string. */ - fromBits: function (arr, _noEquals) { + fromBits: function (arr, _noEquals, _url) { var out = "", i, bits=0, c = sjcl.codec.base64._chars, ta=0, bl = sjcl.bitArray.bitLength(arr); + if (_url) c = c.substr(0,62) + '-_'; for (i=0; out.length * 6 < bl; ) { out += c.charAt((ta ^ arr[i]>>>bits) >>> 26); if (bits < 6) { @@ -534,9 +565,10 @@ sjcl.codec.base64 = { }, /** Convert from a base64 string to a bitArray */ - toBits: function(str) { + toBits: function(str, _url) { str = str.replace(/\s|=/g,''); var out = [], i, bits=0, c = sjcl.codec.base64._chars, ta=0, x; + if (_url) c = c.substr(0,62) + '-_'; for (i=0; i<str.length; i++) { x = c.indexOf(str.charAt(i)); if (x < 0) { @@ -557,42 +589,10 @@ sjcl.codec.base64 = { return out; } }; -/** @fileOverview Bit array codec implementations. - * - * @author Emily Stark - * @author Mike Hamburg - * @author Dan Boneh - */ -/** @namespace Arrays of bytes */ -sjcl.codec.bytes = { - /** Convert from a bitArray to an array of bytes. */ - fromBits: function (arr) { - var out = [], bl = sjcl.bitArray.bitLength(arr), i, tmp; - for (i=0; i<bl/8; i++) { - if ((i&3) === 0) { - tmp = arr[i/4]; - } - out.push(tmp >>> 24); - tmp <<= 8; - } - return out; - }, - /** Convert from an array of bytes to a bitArray. */ - toBits: function (bytes) { - var out = [], i, tmp=0; - for (i=0; i<bytes.length; i++) { - tmp = tmp << 8 | bytes[i]; - if ((i&3) === 3) { - out.push(tmp); - tmp = 0; - } - } - if (i&3) { - out.push(sjcl.bitArray.partial(8*(i&3), tmp)); - } - return out; - } +sjcl.codec.base64url = { + fromBits: function (arr) { return sjcl.codec.base64.fromBits(arr,1,1); }, + toBits: function (str) { return sjcl.codec.base64.toBits(str,1); } }; /** @fileOverview Javascript SHA-256 implementation. * @@ -1309,7 +1309,7 @@ sjcl.random = { var out = [], i, readiness = this.isReady(paranoia), g; if (readiness === this._NOT_READY) { - throw new sjcl.exception.notready("generator isn't seeded"); + throw new sjcl.exception.notReady("generator isn't seeded"); } else if (readiness & this._REQUIRES_RESEED) { this._reseedFromPools(!(readiness & this._READY)); } @@ -1455,8 +1455,8 @@ sjcl.random = { if (!this._collectorsStarted) { return; } if (window.removeEventListener) { - window.removeEventListener("load", this._loadTimeCollector); - window.removeEventListener("mousemove", this._mouseCollector); + window.removeEventListener("load", this._loadTimeCollector, false); + window.removeEventListener("mousemove", this._mouseCollector, false); } else if (window.detachEvent) { window.detachEvent("onload", this._loadTimeCollector); window.detachEvent("onmousemove", this._mouseCollector); @@ -1628,6 +1628,16 @@ sjcl.random = { } }; +(function(){ + try { + // get cryptographically strong entropy in Webkit + var ab = new Uint32Array(32); + crypto.getRandomValues(ab); + sjcl.random.addEntropy(ab, 1024, "crypto.getRandomValues"); + } catch (e) { + // no getRandomValues :-( + } +})(); /** @fileOverview Convenince functions centered around JSON encapsulation. * * @author Emily Stark @@ -1686,7 +1696,7 @@ sjcl.random = { rp.key = password; /* do the encryption */ - p.ct = sjcl.mode[p.mode].encrypt(prp, plaintext, p.iv, p.adata, p.tag); + p.ct = sjcl.mode[p.mode].encrypt(prp, plaintext, p.iv, p.adata, p.ts); return j.encode(j._subtract(p, j.defaults)); }, @@ -1730,7 +1740,7 @@ sjcl.random = { prp = new sjcl.cipher[p.cipher](password); /* do the decryption */ - ct = sjcl.mode[p.mode].decrypt(prp, p.ct, p.iv, p.adata, p.tag); + ct = sjcl.mode[p.mode].decrypt(prp, p.ct, p.iv, p.adata, p.ts); /* return the json data */ j._add(rp, p); |
