From 080df573cb9b49220467e11f1e734dab7e96703e Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 01:12:47 -0700 Subject: [PATCH 01/27] add SHA-1 implementation --- core/sha1.js | 165 ++++++++++++++ test/sha1_test.js | 14 ++ test/sha1_vectors.js | 516 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 695 insertions(+) create mode 100644 core/sha1.js create mode 100644 test/sha1_test.js create mode 100644 test/sha1_vectors.js diff --git a/core/sha1.js b/core/sha1.js new file mode 100644 index 00000000..73d05b18 --- /dev/null +++ b/core/sha1.js @@ -0,0 +1,165 @@ +/** @fileOverview Javascript SHA-1 implementation. + * + * Based on the implementation in RFC 3174, method 1, and on the SJCL + * SHA-256 implementation. + * + * @author Quinn Slack + */ + +/** + * Context for a SHA-1 operation in progress. + * @constructor + * @class Secure Hash Algorithm, 160 bits. + */ +sjcl.hash.sha1 = function (hash) { + if (hash) { + this._h = hash._h.slice(0); + this._buffer = hash._buffer.slice(0); + this._length = hash._length; + } else { + this.reset(); + } +}; + +/** + * Hash a string or an array of words. + * @static + * @param {bitArray|String} data the data to hash. + * @return {bitArray} The hash value, an array of 5 big-endian words. + */ +sjcl.hash.sha1.hash = function (data) { + return (new sjcl.hash.sha1()).update(data).finalize(); +}; + +sjcl.hash.sha1.prototype = { + /** + * The hash's block size, in bits. + * @constant + */ + blockSize: 512, + + /** + * Reset the hash state. + * @return this + */ + reset:function () { + this._h = this._init.slice(0); + this._buffer = []; + this._length = 0; + return this; + }, + + /** + * Input several words to the hash. + * @param {bitArray|String} data the data to hash. + * @return this + */ + update: function (data) { + if (typeof data === "string") { + data = sjcl.codec.utf8String.toBits(data); + } + var i, b = this._buffer = sjcl.bitArray.concat(this._buffer, data), + ol = this._length, + nl = this._length = ol + sjcl.bitArray.bitLength(data); + for (i = this.blockSize+ol & -this.blockSize; i <= nl; + i+= this.blockSize) { + this._block(b.splice(0,16)); + } + return this; + }, + + /** + * Complete hashing and output the hash value. + * @return {bitArray} The hash value, an array of 5 big-endian words. TODO + */ + finalize:function () { + var i, b = this._buffer, h = this._h; + + // Round out and push the buffer + b = sjcl.bitArray.concat(b, [sjcl.bitArray.partial(1,1)]); + // Round out the buffer to a multiple of 16 words, less the 2 length words. + for (i = b.length + 2; i & 15; i++) { + b.push(0); + } + + // append the length + b.push(Math.floor(this._length / 0x100000000)); + b.push(this._length | 0); + + while (b.length) { + this._block(b.splice(0,16)); + } + + this.reset(); + return h; + }, + + /** + * The SHA-1 initialization vector. + * @private + */ + _init:[0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], + + /** + * The SHA-1 hash key. + * @private + */ + _key:[0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6], + + /** + * The SHA-1 logical functions f(0), f(1), ..., f(79). + * @private + */ + _f:function(t, b, c, d) { + if (t <= 19) { + return (b & c) | (~b & d); + } else if (t <= 39) { + return b ^ c ^ d; + } else if (t <= 59) { + return (b & c) | (b & d) | (c & d); + } else if (t <= 79) { + return b ^ c ^ d; + } + }, + + /** + * Circular left-shift operator. + * @private + */ + _S:function(n, x) { + return (x << n) | (x >>> 32-n); + }, + + /** + * Perform one cycle of SHA-1. + * @param {bitArray} words one block of words. + * @private + */ + _block:function (words) { + var t, tmp, a, b, c, d, e, + w = words.slice(0), + h = this._h, + k = this._key; + + a = h[0]; b = h[1]; c = h[2]; d = h[3]; e = h[4]; + + for (t=0; t<=79; t++) { + if (t >= 16) { + w[t] = this._S(1, w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16]); + } + tmp = (this._S(5, a) + this._f(t, b, c, d) + e + w[t] + + this._key[Math.floor(t/20)]) | 0; + e = d; + d = c; + c = this._S(30, b); + b = a; + a = tmp; + } + + h[0] = (h[0]+a) |0; + h[1] = (h[1]+b) |0; + h[2] = (h[2]+c) |0; + h[3] = (h[3]+d) |0; + h[4] = (h[4]+e) |0; + } +}; diff --git a/test/sha1_test.js b/test/sha1_test.js new file mode 100644 index 00000000..8ec38cde --- /dev/null +++ b/test/sha1_test.js @@ -0,0 +1,14 @@ +new sjcl.test.TestCase("SHA-1 from sha1sum", function (cb) { + if (!sjcl.hash.sha1) { + this.unimplemented(); + cb && cb(); + return; + } + + var i, kat=sjcl.test.vector.sha1, p=0, f=0; + for (i=0; i Date: Tue, 19 Apr 2011 01:16:58 -0700 Subject: [PATCH 02/27] add keyexchange namespace --- core/sjcl.js | 3 +++ sjcl.js | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core/sjcl.js b/core/sjcl.js index e3644217..2cc50908 100644 --- a/core/sjcl.js +++ b/core/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: {}, diff --git a/sjcl.js b/sjcl.js index 8939f6a6..c6e599e2 100644 --- a/sjcl.js +++ b/sjcl.js @@ -1,4 +1,4 @@ -"use strict";var sjcl={cipher:{},hash:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a}}}; +"use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a}}}; sjcl.cipher.aes=function(a){this.h[0][0][0]||this.w();var b,c,d,e,f=this.h[0][4],g=this.h[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.a=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ g[3][f[c&255]]}}; sjcl.cipher.aes.prototype={encrypt:function(a){return this.H(a,0)},decrypt:function(a){return this.H(a,1)},h:[[[],[],[],[],[]],[[],[],[],[],[]]],w:function(){var a=this.h[0],b=this.h[1],c=a[4],d=b[4],e,f,g,h=[],i=[],k,j,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=k||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;j=h[e=h[k=h[f]]];m=j*0x1010101^e*0x10001^k*0x101^f*0x1010100;j=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=j=j<<24^j>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= @@ -28,7 +28,7 @@ sjcl.random={randomWords:function(a,b){var c=[];b=this.isReady(b);var d;if(b===0 (this.q[c]+1)%this.b.length;switch(typeof a){case "number":break;case "object":if(b===undefined)for(c=b=0;c0;){b++;e>>>=1}this.b[g].update([d,this.J++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.b[g].update([d,this.J++,3,b,f,a.length]);this.b[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.j[g]+=b;this.f+=b;if(h===0){this.isReady()!==0&&this.K("seeded",Math.max(this.g, this.f));this.K("progress",this.getProgress())}},isReady:function(a){a=this.B[a!==undefined?a:this.t];return this.g&&this.g>=a?this.j[0]>80&&(new Date).valueOf()>this.O?3:1:this.f>=a?2:0},getProgress:function(a){a=this.B[a?a:this.t];return this.g>=a?1["0"]:this.f>a?1["0"]:this.f/a},startCollectors:function(){if(!this.m){if(window.addEventListener){window.addEventListener("load",this.o,false);window.addEventListener("mousemove",this.p,false)}else if(document.attachEvent){document.attachEvent("onload", this.o);document.attachEvent("onmousemove",this.p)}else throw new sjcl.exception.bug("can't attach event");this.m=true}},stopCollectors:function(){if(this.m){if(window.removeEventListener){window.removeEventListener("load",this.o);window.removeEventListener("mousemove",this.p)}else if(window.detachEvent){window.detachEvent("onload",this.o);window.detachEvent("onmousemove",this.p)}this.m=false}},addEventListener:function(a,b){this.r[a][this.Q++]=b},removeEventListener:function(a,b){var c;a=this.r[a]; -var d=[];for(c in a)a.hasOwnProperty[c]&&a[c]===b&&d.push(c);for(b=0;b=1<this.g)this.g=c;this.z++;this.T(b)},p:function(a){sjcl.random.addEntropy([a.x|| a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},o:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},K:function(a,b){var c;a=sjcl.random.r[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c From 8b379cce99af1eaa15842baec04ea098f2825a7e Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 01:38:31 -0700 Subject: [PATCH 03/27] update minified sjcl.js after adding sha1 --- sjcl.js | 65 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/sjcl.js b/sjcl.js index 8939f6a6..9f8c5a0f 100644 --- a/sjcl.js +++ b/sjcl.js @@ -1,40 +1,43 @@ "use strict";var sjcl={cipher:{},hash:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a}}}; -sjcl.cipher.aes=function(a){this.h[0][0][0]||this.w();var b,c,d,e,f=this.h[0][4],g=this.h[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.a=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ +sjcl.cipher.aes=function(a){this.j[0][0][0]||this.B();var b,c,d,e,f=this.j[0][4],g=this.j[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.a=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ g[3][f[c&255]]}}; -sjcl.cipher.aes.prototype={encrypt:function(a){return this.H(a,0)},decrypt:function(a){return this.H(a,1)},h:[[[],[],[],[],[]],[[],[],[],[],[]]],w:function(){var a=this.h[0],b=this.h[1],c=a[4],d=b[4],e,f,g,h=[],i=[],k,j,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=k||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;j=h[e=h[k=h[f]]];m=j*0x1010101^e*0x10001^k*0x101^f*0x1010100;j=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=j=j<<24^j>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= -0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},H:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.a[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,i,k=c.length/4-2,j,l=4,m=[0,0,0,0];g=this.h[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(j=0;j>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[l];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[l+1];i=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[l+2];a=n[a>>>24]^o[d>>16& +sjcl.cipher.aes.prototype={encrypt:function(a){return this.J(a,0)},decrypt:function(a){return this.J(a,1)},j:[[[],[],[],[],[]],[[],[],[],[],[]]],B:function(){var a=this.j[0],b=this.j[1],c=a[4],d=b[4],e,f,g,h=[],i=[],k,j,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=k||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;j=h[e=h[k=h[f]]];m=j*0x1010101^e*0x10001^k*0x101^f*0x1010100;j=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=j=j<<24^j>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= +0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},J:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.a[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,i,k=c.length/4-2,j,l=4,m=[0,0,0,0];g=this.j[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(j=0;j>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[l];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[l+1];i=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[l+2];a=n[a>>>24]^o[d>>16& 255]^p[e>>8&255]^q[f&255]^c[l+3];l+=4;d=g;e=h;f=i}for(j=0;j<4;j++){m[b?3&-j:j]=r[d>>>24]<<24^r[e>>16&255]<<16^r[f>>8&255]<<8^r[a&255]^c[l++];g=d;d=e;e=f;f=a;a=g}return m}}; -sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.P(a.slice(b/32),32-(b&31)).slice(1);return c===undefined?a:sjcl.bitArray.clamp(a,c-b)},concat:function(a,b){if(a.length===0||b.length===0)return a.concat(b);var c=a[a.length-1],d=sjcl.bitArray.getPartial(c);return d===32?a.concat(b):sjcl.bitArray.P(b,d,c|0,a.slice(0,a.length-1))},bitLength:function(a){var b=a.length;if(b===0)return 0;return(b-1)*32+sjcl.bitArray.getPartial(a[b-1])},clamp:function(a,b){if(a.length*320&&b)a[c-1]=sjcl.bitArray.partial(b,a[c-1]&2147483648>>b-1,1);return a},partial:function(a,b,c){if(a===32)return b;return(c?b|0:b<<32-a)+a*0x10000000000},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return false;var c=0,d;for(d=0;d=32;b-=32){d.push(c);c=0}if(b===0)return d.concat(a); -for(e=0;e>>b);c=a[e]<<32-b}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,b+a>32?c:d.pop(),1));return d},k:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}}; +sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.Q(a.slice(b/32),32-(b&31)).slice(1);return c===undefined?a:sjcl.bitArray.clamp(a,c-b)},concat:function(a,b){if(a.length===0||b.length===0)return a.concat(b);var c=a[a.length-1],d=sjcl.bitArray.getPartial(c);return d===32?a.concat(b):sjcl.bitArray.Q(b,d,c|0,a.slice(0,a.length-1))},bitLength:function(a){var b=a.length;if(b===0)return 0;return(b-1)*32+sjcl.bitArray.getPartial(a[b-1])},clamp:function(a,b){if(a.length*320&&b)a[c-1]=sjcl.bitArray.partial(b,a[c-1]&2147483648>>b-1,1);return a},partial:function(a,b,c){if(a===32)return b;return(c?b|0:b<<32-a)+a*0x10000000000},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return false;var c=0,d;for(d=0;d=32;b-=32){d.push(c);c=0}if(b===0)return d.concat(a); +for(e=0;e>>b);c=a[e]<<32-b}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,b+a>32?c:d.pop(),1));return d},m:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}}; sjcl.codec.utf8String={fromBits:function(a){var b="",c=sjcl.bitArray.bitLength(a),d,e;for(d=0;d>>24);e<<=8}return decodeURIComponent(escape(b))},toBits:function(a){a=unescape(encodeURIComponent(a));var b=[],c,d=0;for(c=0;c>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.D,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha256=function(a){this.a[0]||this.w();if(a){this.n=a.n.slice(0);this.i=a.i.slice(0);this.e=a.e}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; -sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.n=this.N.slice(0);this.i=[];this.e=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.i=sjcl.bitArray.concat(this.i,a);b=this.e;a=this.e=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.C(c.splice(0,16));return this},finalize:function(){var a,b=this.i,c=this.n;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.e/ -4294967296));for(b.push(this.e|0);b.length;)this.C(b.splice(0,16));this.reset();return c},N:[],a:[],w:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.N[b]=a(Math.pow(c,0.5));this.a[b]=a(Math.pow(c,1/3));b++}},C:function(a){var b,c,d=a.slice(0),e=this.n,f=this.a,g=e[0],h=e[1],i=e[2],k=e[3],j=e[4],l=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ +sjcl.codec.base64={G:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",fromBits:function(a,b){var c="",d,e=0,f=sjcl.codec.base64.G,g=0,h=sjcl.bitArray.bitLength(a);for(d=0;c.length*6>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.G,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha1=function(a){if(a){this.d=a.d.slice(0);this.c=a.c.slice(0);this.b=a.b}else this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()}; +sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.d=this.p.slice(0);this.c=[];this.b=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.c=sjcl.bitArray.concat(this.c,a);b=this.b;a=this.b=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)this.k(c.splice(0,16));return this},finalize:function(){var a,b=this.c,c=this.d;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0); +b.push(Math.floor(this.b/0x100000000));for(b.push(this.b|0);b.length;)this.k(b.splice(0,16));this.reset();return c},p:[1732584193,4023233417,2562383102,271733878,3285377520],a:[1518500249,1859775393,2400959708,3395469782],T:function(a,b,c,d){if(a<=19)return b&c|~b&d;else if(a<=39)return b^c^d;else if(a<=59)return b&c|b&d|c&d;else if(a<=79)return b^c^d},t:function(a,b){return b<>>32-a},k:function(a){var b,c,d,e,f,g,h=a.slice(0),i=this.d;c=i[0];d=i[1];e=i[2];f=i[3];g=i[4];for(a=0;a<=79;a++){if(a>= +16)h[a]=this.t(1,h[a-3]^h[a-8]^h[a-14]^h[a-16]);b=this.t(5,c)+this.T(a,d,e,f)+g+h[a]+this.a[Math.floor(a/20)]|0;g=f;f=e;e=this.t(30,d);d=c;c=b}i[0]=i[0]+c|0;i[1]=i[1]+d|0;i[2]=i[2]+e|0;i[3]=i[3]+f|0;i[4]=i[4]+g|0}};sjcl.hash.sha256=function(a){this.a[0]||this.B();if(a){this.d=a.d.slice(0);this.c=a.c.slice(0);this.b=a.b}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; +sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.d=this.p.slice(0);this.c=[];this.b=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.c=sjcl.bitArray.concat(this.c,a);b=this.b;a=this.b=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.k(c.splice(0,16));return this},finalize:function(){var a,b=this.c,c=this.d;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.b/ +4294967296));for(b.push(this.b|0);b.length;)this.k(b.splice(0,16));this.reset();return c},p:[],a:[],B:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.p[b]=a(Math.pow(c,0.5));this.a[b]=a(Math.pow(c,1/3));b++}},k:function(a){var b,c,d=a.slice(0),e=this.d,f=this.a,g=e[0],h=e[1],i=e[2],k=e[3],j=e[4],l=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(j>>>6^j>>>11^j>>>25^j<<26^j<<21^j<<7)+(m^j&(l^m))+f[a];n=m;m=l;l=j;j=k+b|0;k=i;i=h;h=g;g=b+(h&i^k&(h^i))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+i|0;e[3]=e[3]+k|0;e[4]=e[4]+j|0;e[5]=e[5]+l|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}}; -sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,i=h.bitLength(c)/8,k=h.bitLength(g)/8;e=e||64;d=d||[];if(i<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(f=2;f<4&&k>>>8*f;f++);if(f<15-i)f=15-i;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.G(a,b,c,d,e,f);g=sjcl.mode.ccm.I(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),i=f.clamp(b,h-e),k=f.bitSlice(b, -h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));i=sjcl.mode.ccm.I(a,i,c,k,e,b);a=sjcl.mode.ccm.G(a,i.data,c,d,e,b);if(!f.equal(i.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return i.data},G:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,i=h.k;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); -f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>>8*f;f++);if(f<15-i)f=15-i;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.I(a,b,c,d,e,f);g=sjcl.mode.ccm.K(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),i=f.clamp(b,h-e),k=f.bitSlice(b, +h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));i=sjcl.mode.ccm.K(a,i,c,k,e,b);a=sjcl.mode.ccm.I(a,i.data,c,d,e,b);if(!f.equal(i.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return i.data},I:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,i=h.m;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); +f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.M=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.l=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.O=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.n=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b0;){b++;e>>>=1}this.b[g].update([d,this.J++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.b[g].update([d,this.J++,3,b,f,a.length]);this.b[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.j[g]+=b;this.f+=b;if(h===0){this.isReady()!==0&&this.K("seeded",Math.max(this.g, -this.f));this.K("progress",this.getProgress())}},isReady:function(a){a=this.B[a!==undefined?a:this.t];return this.g&&this.g>=a?this.j[0]>80&&(new Date).valueOf()>this.O?3:1:this.f>=a?2:0},getProgress:function(a){a=this.B[a?a:this.t];return this.g>=a?1["0"]:this.f>a?1["0"]:this.f/a},startCollectors:function(){if(!this.m){if(window.addEventListener){window.addEventListener("load",this.o,false);window.addEventListener("mousemove",this.p,false)}else if(document.attachEvent){document.attachEvent("onload", -this.o);document.attachEvent("onmousemove",this.p)}else throw new sjcl.exception.bug("can't attach event");this.m=true}},stopCollectors:function(){if(this.m){if(window.removeEventListener){window.removeEventListener("load",this.o);window.removeEventListener("mousemove",this.p)}else if(window.detachEvent){window.detachEvent("onload",this.o);window.detachEvent("onmousemove",this.p)}this.m=false}},addEventListener:function(a,b){this.r[a][this.Q++]=b},removeEventListener:function(a,b){var c;a=this.r[a]; -var d=[];for(c in a)a.hasOwnProperty[c]&&a[c]===b&&d.push(c);for(b=0;b=1<this.g)this.g=c;this.z++;this.T(b)},p:function(a){sjcl.random.addEntropy([a.x|| -a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},o:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},K:function(a,b){var c;a=sjcl.random.r[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c -4)throw new sjcl.exception.invalid("json encrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,f);a=c.key.slice(0,f.ks/32);f.salt=c.salt}if(typeof b==="string")b=sjcl.codec.utf8String.toBits(b);c=new sjcl.cipher[f.cipher](a);e.c(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(c,b,f.iv,f.adata,f.tag);return e.encode(e.V(f,e.defaults))},decrypt:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.c(e.c(e.c({},e.defaults),e.decode(b)),c,true);if(typeof b.salt==="string")b.salt= +sjcl.random={randomWords:function(a,b){var c=[];b=this.isReady(b);var d;if(b===0)throw new sjcl.exception.notready("generator isn't seeded");else b&2&&this.W(!(b&1));for(b=0;b0;){b++;e>>>=1}this.e[g].update([d,this.L++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.e[g].update([d,this.L++,3,b,f,a.length]);this.e[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.l[g]+=b;this.h+=b;if(h===0){this.isReady()!==0&&this.M("seeded",Math.max(this.i, +this.h));this.M("progress",this.getProgress())}},isReady:function(a){a=this.F[a!==undefined?a:this.z];return this.i&&this.i>=a?this.l[0]>80&&(new Date).valueOf()>this.P?3:1:this.h>=a?2:0},getProgress:function(a){a=this.F[a?a:this.z];return this.i>=a?1["0"]:this.h>a?1["0"]:this.h/a},startCollectors:function(){if(!this.o){if(window.addEventListener){window.addEventListener("load",this.q,false);window.addEventListener("mousemove",this.r,false)}else if(document.attachEvent){document.attachEvent("onload", +this.q);document.attachEvent("onmousemove",this.r)}else throw new sjcl.exception.bug("can't attach event");this.o=true}},stopCollectors:function(){if(this.o){if(window.removeEventListener){window.removeEventListener("load",this.q);window.removeEventListener("mousemove",this.r)}else if(window.detachEvent){window.detachEvent("onload",this.q);window.detachEvent("onmousemove",this.r)}this.o=false}},addEventListener:function(a,b){this.u[a][this.R++]=b},removeEventListener:function(a,b){var c;a=this.u[a]; +var d=[];for(c in a)a.hasOwnProperty(c)&&a[c]===b&&d.push(c);for(b=0;b=1<this.i)this.i=c;this.C++;this.V(b)},r:function(a){sjcl.random.addEntropy([a.x|| +a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},q:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},M:function(a,b){var c;a=sjcl.random.u[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c +4)throw new sjcl.exception.invalid("json encrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,f);a=c.key.slice(0,f.ks/32);f.salt=c.salt}if(typeof b==="string")b=sjcl.codec.utf8String.toBits(b);c=new sjcl.cipher[f.cipher](a);e.f(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(c,b,f.iv,f.adata,f.tag);return e.encode(e.X(f,e.defaults))},decrypt:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.f(e.f(e.f({},e.defaults),e.decode(b)),c,true);if(typeof b.salt==="string")b.salt= sjcl.codec.base64.toBits(b.salt);if(typeof b.iv==="string")b.iv=sjcl.codec.base64.toBits(b.iv);if(!sjcl.mode[b.mode]||!sjcl.cipher[b.cipher]||typeof a==="string"&&b.iter<=100||b.ts!==64&&b.ts!==96&&b.ts!==128||b.ks!==128&&b.ks!==192&&b.ks!==0x100||!b.iv||b.iv.length<2||b.iv.length>4)throw new sjcl.exception.invalid("json decrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,b);a=c.key.slice(0,b.ks/32);b.salt=c.salt}c=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(c, -b.ct,b.iv,b.adata,b.tag);e.c(d,b);d.key=a;return sjcl.codec.utf8String.fromBits(c)},encode:function(a){var b,c="{",d="";for(b in a)if(a.hasOwnProperty(b)){if(!b.match(/^[a-z0-9]+$/i))throw new sjcl.exception.invalid("json encode: invalid property name");c+=d+b+":";d=",";switch(typeof a[b]){case "number":case "boolean":c+=a[b];break;case "string":c+='"'+escape(a[b])+'"';break;case "object":c+='"'+sjcl.codec.base64.fromBits(a[b],1)+'"';break;default:throw new sjcl.exception.bug("json encode: unsupported type"); -}}return c+"}"},decode:function(a){a=a.replace(/\s/g,"");if(!a.match(/^\{.*\}$/))throw new sjcl.exception.invalid("json decode: this isn't json!");a=a.replace(/^\{|\}$/g,"").split(/,/);var b={},c,d;for(c=0;c Date: Tue, 19 Apr 2011 01:43:21 -0700 Subject: [PATCH 04/27] SRP tests --- test/srp_test.js | 19 +++++++++++++++++++ test/srp_vectors.js | 9 +++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/srp_test.js create mode 100644 test/srp_vectors.js diff --git a/test/srp_test.js b/test/srp_test.js new file mode 100644 index 00000000..bbbd9281 --- /dev/null +++ b/test/srp_test.js @@ -0,0 +1,19 @@ +new sjcl.test.TestCase("SRP known-answer (RFC 5054) tests", function (cb) { + if (!sjcl.keyexchange.srp) { + this.unimplemented(); + cb && cb(); + return; + } + + var i, kat = sjcl.test.vector.srp, tv, N, g, v; + + for (i=0; i v = " + v); + this.require(sjcl.bitArray.equal(v, tv.v), "srpv #"+i); + } + cb && cb(); +}); diff --git a/test/srp_vectors.js b/test/srp_vectors.js new file mode 100644 index 00000000..9759ecde --- /dev/null +++ b/test/srp_vectors.js @@ -0,0 +1,9 @@ +/* Official SRP test vectors. */ +sjcl.test.vector.srp = [ +{ I: "alice", + P: "password123", + s: "BEB25379D1A8581EB5A727673A2441EE", + v: "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB", + known_group_size: 1024 +} +] From 8fe21ec52fe9f8e0a6e77c99acae2b8e1078cb9f Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 05:12:00 -0700 Subject: [PATCH 05/27] ws --- test/srp_vectors.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/srp_vectors.js b/test/srp_vectors.js index 9759ecde..589c7c07 100644 --- a/test/srp_vectors.js +++ b/test/srp_vectors.js @@ -1,6 +1,7 @@ /* Official SRP test vectors. */ sjcl.test.vector.srp = [ -{ I: "alice", +{ + I: "alice", P: "password123", s: "BEB25379D1A8581EB5A727673A2441EE", v: "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB", From 89905acb59db4bbb08a87ecc2b0e4e4092e775fd Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 05:12:06 -0700 Subject: [PATCH 06/27] add srp X --- test/srp_vectors.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/srp_vectors.js b/test/srp_vectors.js index 589c7c07..35625ce7 100644 --- a/test/srp_vectors.js +++ b/test/srp_vectors.js @@ -4,6 +4,7 @@ sjcl.test.vector.srp = [ I: "alice", P: "password123", s: "BEB25379D1A8581EB5A727673A2441EE", + x: "94B7555AABE9127CC58CCF4993DB6CF84D16C124", v: "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB", known_group_size: 1024 } From 2bbda850b123a9bf4a7cdeb4b8177392ef8bd946 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 05:13:45 -0700 Subject: [PATCH 07/27] add bn powermod and mulmod --- core/bn.js | 15 +++++++++++++ test/bn_test.js | 22 ++++++++++++++++++ test/bn_vectors.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 test/bn_test.js create mode 100644 test/bn_vectors.js diff --git a/core/bn.js b/core/bn.js index 16978e79..55ecdba4 100644 --- a/core/bn.js +++ b/core/bn.js @@ -275,6 +275,21 @@ sjcl.bn.prototype = { return out; }, + mulmod: function(x, N) { + return this.mod(N).mul(x.mod(N)).mod(N); + }, + + powermod: function(x, N) { + var result = new sjcl.bn(1), a = new sjcl.bn(this), k = new sjcl.bn(x); + while (true) { + if (k.limbs[0] & 1) { result = result.mulmod(a, N); } + k.halveM(); + if (k.equals(0)) { break; } + a = a.mulmod(a, N); + } + return result.normalize().reduce(); + }, + trim: function() { var l = this.limbs, p; do { diff --git a/test/bn_test.js b/test/bn_test.js new file mode 100644 index 00000000..4a6c1408 --- /dev/null +++ b/test/bn_test.js @@ -0,0 +1,22 @@ +new sjcl.test.TestCase("Bignum modular exponentiation test", function (cb) { + if (!sjcl.bn) { + this.unimplemented(); + cb && cb(); + return; + } + + var i, tv, g, x, N, v; + for (i=0; i < sjcl.test.vector.bn_powermod.length; i++) { + tv = sjcl.test.vector.bn_powermod[i]; + try { + g = new sjcl.bn(tv.g); + x = new sjcl.bn(tv.x); + N = new sjcl.bn(tv.N); + v = g.powermod(x, N); + this.require(v.equals(new sjcl.bn(tv.v))); + } catch(e) { + this.fail(e); + } + } + cb && cb(); +}); diff --git a/test/bn_vectors.js b/test/bn_vectors.js new file mode 100644 index 00000000..053f68a7 --- /dev/null +++ b/test/bn_vectors.js @@ -0,0 +1,56 @@ +sjcl.test.vector.bn_powermod = [ + { + g: 2, + x: 3, + N: 3, + v: 2 + }, + { + g: 2, + x: "10000000000000000000000000", + N: 1337, + v: 1206 + }, + { + g: 17, + x: 90, + N: 34717861147, + v: 28445204336 + }, + { + g: 2, + x: "0x844A000000000000000000000", + N: 13, + v: 9 + }, + { + g: 2, + x: 0x1010, + N: 131, + v: 59 + }, + { + g: 2, + x: "43207437777777877617151", + N: 13, + v: 2 + }, + { + g: 2, + x: "389274238947216444871600001871964319565192765874149", + N: 117, + v: 44 + }, + { + g: 2, + x: "89457115510016156219817846189181057618965150496979174671534084187", + N: "1897166415676096761", + v: "16840615e646a4c5c8d" + }, + { + g: 2, + x: "94B7555AABE9127CC58CCF4993DB6CF84D16C124", + N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", + v: "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB" + } +] From 44ddce8d5fc67e3cb0726aa0707502d82c117122 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 05:14:28 -0700 Subject: [PATCH 08/27] test srp x and v --- test/srp_test.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/srp_test.js b/test/srp_test.js index bbbd9281..d5219e72 100644 --- a/test/srp_test.js +++ b/test/srp_test.js @@ -5,15 +5,18 @@ new sjcl.test.TestCase("SRP known-answer (RFC 5054) tests", function (cb) { return; } - var i, kat = sjcl.test.vector.srp, tv, N, g, v; + var i, kat = sjcl.test.vector.srp, tv, N, g, v, x; for (i=0; i v = " + v); - this.require(sjcl.bitArray.equal(v, tv.v), "srpv #"+i); + N = sjcl.keyexchange.srp.knownGroup(tv.known_group_size).N; + g = sjcl.keyexchange.srp.knownGroup(tv.known_group_size).g; + tv.s = sjcl.codec.hex.toBits(tv.s); + x = sjcl.keyexchange.srp.makeX(tv.I, tv.P, tv.s); + this.require(sjcl.codec.hex.fromBits(x).toUpperCase() === tv.x, "srpx #"+i); + + v = sjcl.keyexchange.srp.makeVerifier(tv.I, tv.P, tv.s, N, g); + this.require(v.equals(new sjcl.bn(tv.v)), "srpv #"+i); } cb && cb(); }); From b55ed280ddadcf9245af56658e28de0112bfe939 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 05:14:44 -0700 Subject: [PATCH 09/27] srp implementation --- core/srp.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 core/srp.js diff --git a/core/srp.js b/core/srp.js new file mode 100644 index 00000000..417cd5ed --- /dev/null +++ b/core/srp.js @@ -0,0 +1,93 @@ +/** @fileOverview Javascript SRP implementation. + * + * @author Quinn Slack + */ + +/** + * Compute the SRP verifier from the username, password, salt, and group. + * @class SRP + */ +sjcl.keyexchange.srp = { + makeVerifier: function(I, P, s, N, g) { + var x; + // From RFC 5054: + // v = g^x mod N + x = this.makeX(I, P, s); + x = sjcl.bn.fromBits(x); + return g.powermod(x, N); + }, + + makeX: function(I, P, s) { + var inner; + // From RFC 2945: + // x = SHA1( | SHA( | ":" | )) + inner = sjcl.hash.sha1.hash(I + ':' + P); + return sjcl.hash.sha1.hash(sjcl.bitArray.concat(s, inner)); + }, + + /** + * Returns the known SRP group with the given size (in bits). + * @param {String} i The size of the known SRP group. + * @return {Object} An object with "N" and "g" properties. + */ + knownGroup:function(i) { + if (typeof i !== "string") { i = i.toString(); } + if (!this._didInitKnownGroups) { this._initKnownGroups(); } + return this._knownGroups[i]; + }, + + /** + * Initializes bignum objects for known group parameters. + * @private + */ + _didInitKnownGroups: false, + _initKnownGroups:function() { + var i, size, group; + for (i=0; i < this._knownGroupSizes.length; i++) { + size = this._knownGroupSizes[i].toString(); + group = this._knownGroups[size]; + group.N = new sjcl.bn(group.N); + group.g = new sjcl.bn(group.g); + } + this._didInitKnownGroups = true; + }, + + _knownGroupSizes: [1024, 1536, 2048], + _knownGroups: { + 1024: { + N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C" + + "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4" + + "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29" + + "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A" + + "FD5138FE8376435B9FC61D2FC0EB06E3", + g:2 + }, + + 1536: { + N: "9DEF3CAFB939277AB1F12A8617A47BBBDBA51DF499AC4C80BEEEA961" + + "4B19CC4D5F4F5F556E27CBDE51C6A94BE4607A291558903BA0D0F843" + + "80B655BB9A22E8DCDF028A7CEC67F0D08134B1C8B97989149B609E0B" + + "E3BAB63D47548381DBC5B1FC764E3F4B53DD9DA1158BFD3E2B9C8CF5" + + "6EDF019539349627DB2FD53D24B7C48665772E437D6C7F8CE442734A" + + "F7CCB7AE837C264AE3A9BEB87F8A2FE9B8B5292E5A021FFF5E91479E" + + "8CE7A28C2442C6F315180F93499A234DCF76E3FED135F9BB", + g: 2 + }, + + 2048: { + N: "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294" + + "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D" + + "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB" + + "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74" + + "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A" + + "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D" + + "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73" + + "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6" + + "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F" + + "9E4AFF73", + g: 2 + } + } + +}; + From d5c014a06b2dc543cac72fba498ecfe56efa01cd Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 05:16:47 -0700 Subject: [PATCH 10/27] update minified after srp changes --- sjcl.js | 94 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/sjcl.js b/sjcl.js index 534cc0d6..7a65e280 100644 --- a/sjcl.js +++ b/sjcl.js @@ -1,65 +1,69 @@ "use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"GENERATOR NOT READY: "+this.message};this.message=a}}}; -sjcl.cipher.aes=function(a){this.o[0][0][0]||this.L();var b,c,d,e,f=this.o[0][4],g=this.o[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.c=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ +sjcl.cipher.aes=function(a){this.p[0][0][0]||this.M();var b,c,d,e,f=this.p[0][4],g=this.p[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.c=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ g[3][f[c&255]]}}; -sjcl.cipher.aes.prototype={encrypt:function(a){return this.S(a,0)},decrypt:function(a){return this.S(a,1)},o:[[[],[],[],[],[]],[[],[],[],[],[]]],L:function(){var a=this.o[0],b=this.o[1],c=a[4],d=b[4],e,f,g,h=[],i=[],j,k,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=j||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;k=h[e=h[j=h[f]]];m=k*0x1010101^e*0x10001^j*0x101^f*0x1010100;k=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=k=k<<24^k>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= -0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},S:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.c[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,i,j=c.length/4-2,k,l=4,m=[0,0,0,0];g=this.o[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(k=0;k>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[l];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[l+1];i=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[l+2];a=n[a>>>24]^o[d>>16& +sjcl.cipher.aes.prototype={encrypt:function(a){return this.U(a,0)},decrypt:function(a){return this.U(a,1)},p:[[[],[],[],[],[]],[[],[],[],[],[]]],M:function(){var a=this.p[0],b=this.p[1],c=a[4],d=b[4],e,f,g,h=[],i=[],j,k,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=j||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;k=h[e=h[j=h[f]]];m=k*0x1010101^e*0x10001^j*0x101^f*0x1010100;k=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=k=k<<24^k>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= +0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},U:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.c[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,i,j=c.length/4-2,k,l=4,m=[0,0,0,0];g=this.p[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(k=0;k>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[l];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[l+1];i=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[l+2];a=n[a>>>24]^o[d>>16& 255]^p[e>>8&255]^q[f&255]^c[l+3];l+=4;d=g;e=h;f=i}for(k=0;k<4;k++){m[b?3&-k:k]=r[d>>>24]<<24^r[e>>16&255]<<16^r[f>>8&255]<<8^r[a&255]^c[l++];g=d;d=e;e=f;f=a;a=g}return m}}; -sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.aa(a.slice(b/32),32-(b&31)).slice(1);return c===undefined?a:sjcl.bitArray.clamp(a,c-b)},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a[b/32|0]<<32-d^a[b/32+1|0]>>>d:a[b/32|0]>>>d)&(1<>>d:a[b/32|0]>>>d)&(1<0&&b)a[c-1]=sjcl.bitArray.partial(b,a[c-1]&2147483648>>b-1,1);return a},partial:function(a,b,c){if(a===32)return b;return(c?b|0:b<<32-a)+a*0x10000000000},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return false;var c=0,d;for(d=0;d=32;b-=32){d.push(c);c=0}if(b===0)return d.concat(a);for(e=0;e>>b);c=a[e]<<32-b}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,b+a>32?c:d.pop(),1));return d},l:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}}; +a[d]^b[d];return c===0},fa:function(a,b,c,d){var e;e=0;if(d===undefined)d=[];for(;b>=32;b-=32){d.push(c);c=0}if(b===0)return d.concat(a);for(e=0;e>>b);c=a[e]<<32-b}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,b+a>32?c:d.pop(),1));return d},m:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}}; sjcl.codec.utf8String={fromBits:function(a){var b="",c=sjcl.bitArray.bitLength(a),d,e;for(d=0;d>>24);e<<=8}return decodeURIComponent(escape(b))},toBits:function(a){a=unescape(encodeURIComponent(a));var b=[],c,d=0;for(c=0;c>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.P,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha1=function(a){if(a){this.g=a.g.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()}; -sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.g=this.u.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)this.p(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.g;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0); -b.push(Math.floor(this.d/0x100000000));for(b.push(this.d|0);b.length;)this.p(b.splice(0,16));this.reset();return c},u:[1732584193,4023233417,2562383102,271733878,3285377520],c:[1518500249,1859775393,2400959708,3395469782],ea:function(a,b,c,d){if(a<=19)return b&c|~b&d;else if(a<=39)return b^c^d;else if(a<=59)return b&c|b&d|c&d;else if(a<=79)return b^c^d},C:function(a,b){return b<>>32-a},p:function(a){var b,c,d,e,f,g,h=a.slice(0),i=this.g;c=i[0];d=i[1];e=i[2];f=i[3];g=i[4];for(a=0;a<=79;a++){if(a>= -16)h[a]=this.C(1,h[a-3]^h[a-8]^h[a-14]^h[a-16]);b=this.C(5,c)+this.ea(a,d,e,f)+g+h[a]+this.c[Math.floor(a/20)]|0;g=f;f=e;e=this.C(30,d);d=c;c=b}i[0]=i[0]+c|0;i[1]=i[1]+d|0;i[2]=i[2]+e|0;i[3]=i[3]+f|0;i[4]=i[4]+g|0}};sjcl.hash.sha256=function(a){this.c[0]||this.L();if(a){this.g=a.g.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; -sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.g=this.u.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.p(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.g;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.d/ -4294967296));for(b.push(this.d|0);b.length;)this.p(b.splice(0,16));this.reset();return c},u:[],c:[],L:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.u[b]=a(Math.pow(c,0.5));this.c[b]=a(Math.pow(c,1/3));b++}},p:function(a){var b,c,d=a.slice(0),e=this.g,f=this.c,g=e[0],h=e[1],i=e[2],j=e[3],k=e[4],l=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ +sjcl.codec.base64={R:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",fromBits:function(a,b){var c="",d,e=0,f=sjcl.codec.base64.R,g=0,h=sjcl.bitArray.bitLength(a);for(d=0;c.length*6>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.R,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha1=function(a){if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()}; +sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.h=this.w.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)this.q(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.h;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0); +b.push(Math.floor(this.d/0x100000000));for(b.push(this.d|0);b.length;)this.q(b.splice(0,16));this.reset();return c},w:[1732584193,4023233417,2562383102,271733878,3285377520],c:[1518500249,1859775393,2400959708,3395469782],ja:function(a,b,c,d){if(a<=19)return b&c|~b&d;else if(a<=39)return b^c^d;else if(a<=59)return b&c|b&d|c&d;else if(a<=79)return b^c^d},D:function(a,b){return b<>>32-a},q:function(a){var b,c,d,e,f,g,h=a.slice(0),i=this.h;c=i[0];d=i[1];e=i[2];f=i[3];g=i[4];for(a=0;a<=79;a++){if(a>= +16)h[a]=this.D(1,h[a-3]^h[a-8]^h[a-14]^h[a-16]);b=this.D(5,c)+this.ja(a,d,e,f)+g+h[a]+this.c[Math.floor(a/20)]|0;g=f;f=e;e=this.D(30,d);d=c;c=b}i[0]=i[0]+c|0;i[1]=i[1]+d|0;i[2]=i[2]+e|0;i[3]=i[3]+f|0;i[4]=i[4]+g|0}};sjcl.hash.sha256=function(a){this.c[0]||this.M();if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; +sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.h=this.w.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.q(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.h;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.d/ +4294967296));for(b.push(this.d|0);b.length;)this.q(b.splice(0,16));this.reset();return c},w:[],c:[],M:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.w[b]=a(Math.pow(c,0.5));this.c[b]=a(Math.pow(c,1/3));b++}},q:function(a){var b,c,d=a.slice(0),e=this.h,f=this.c,g=e[0],h=e[1],i=e[2],j=e[3],k=e[4],l=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(k>>>6^k>>>11^k>>>25^k<<26^k<<21^k<<7)+(m^k&(l^m))+f[a];n=m;m=l;l=k;k=j+b|0;j=i;i=h;h=g;g=b+(h&i^j&(h^i))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+i|0;e[3]=e[3]+j|0;e[4]=e[4]+k|0;e[5]=e[5]+l|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}}; -sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,i=h.bitLength(c)/8,j=h.bitLength(g)/8;e=e||64;d=d||[];if(i<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(f=2;f<4&&j>>>8*f;f++);if(f<15-i)f=15-i;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.R(a,b,c,d,e,f);g=sjcl.mode.ccm.T(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),i=f.clamp(b,h-e),j=f.bitSlice(b, -h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));i=sjcl.mode.ccm.T(a,i,c,j,e,b);a=sjcl.mode.ccm.R(a,i.data,c,d,e,b);if(!f.equal(i.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return i.data},R:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,i=h.l;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); -f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>>8*f;f++);if(f<15-i)f=15-i;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.T(a,b,c,d,e,f);g=sjcl.mode.ccm.V(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),i=f.clamp(b,h-e),j=f.bitSlice(b, +h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));i=sjcl.mode.ccm.V(a,i,c,j,e,b);a=sjcl.mode.ccm.T(a,i.data,c,d,e,b);if(!f.equal(i.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return i.data},T:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,i=h.m;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); +f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>3&15))*0x1010101;c=a.encrypt(f(c,e.concat(b,[g,g,g,g]).slice(d,d+4)));i.splice(d,0,c[0],c[1],c[2],c[3]);return i},decrypt:function(a,b,c,d){if(d&&d.length)throw new sjcl.exception.invalid("cbc can't authenticate data");if(sjcl.bitArray.bitLength(c)!==128)throw new sjcl.exception.invalid("cbc iv must be 128 bits");if(sjcl.bitArray.bitLength(b)&127||!b.length)throw new sjcl.exception.corrupt("cbc ciphertext must be a positive multiple of the block size"); -var e=sjcl.bitArray,f=e.l,g,h=[];for(d=0;d16)throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");c=g*0x1010101;if(!e.equal(e.bitSlice([c,c,c,c],0,g*8),e.bitSlice(h,h.length*32-g*8,h.length*32)))throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");return e.bitSlice(h,0,h.length*32-g*8)}}}; -sjcl.mode.ocb2={name:"ocb2",encrypt:function(a,b,c,d,e,f){if(sjcl.bitArray.bitLength(c)!==128)throw new sjcl.exception.invalid("ocb iv must be 128 bits");var g,h=sjcl.mode.ocb2.N,i=sjcl.bitArray,j=i.l,k=[0,0,0,0];c=h(a.encrypt(c));var l,m=[];d=d||[];e=e||64;for(g=0;g+4>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.Y=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.s=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b16)throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");c=g*0x1010101;if(!e.equal(e.bitSlice([c,c,c,c],0,g*8),e.bitSlice(h,h.length*32-g*8,h.length*32)))throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");return e.bitSlice(h,0,h.length*32-g*8)}}}; +sjcl.mode.ocb2={name:"ocb2",encrypt:function(a,b,c,d,e,f){if(sjcl.bitArray.bitLength(c)!==128)throw new sjcl.exception.invalid("ocb iv must be 128 bits");var g,h=sjcl.mode.ocb2.P,i=sjcl.bitArray,j=i.m,k=[0,0,0,0];c=h(a.encrypt(c));var l,m=[];d=d||[];e=e||64;for(g=0;g+4>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.aa=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.t=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b0;){b++;e>>>=1}this.i[g].update([d,this.V++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.i[g].update([d,this.V++,3,b,f,a.length]);this.i[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.q[g]+=b;this.m+=b;if(h===0){this.isReady()!==0&&this.W("seeded", -Math.max(this.n,this.m));this.W("progress",this.getProgress())}},isReady:function(a){a=this.O[a!==undefined?a:this.H];return this.n&&this.n>=a?this.q[0]>80&&(new Date).valueOf()>this.$?3:1:this.m>=a?2:0},getProgress:function(a){a=this.O[a?a:this.H];return this.n>=a?1["0"]:this.m>a?1["0"]:this.m/a},startCollectors:function(){if(!this.t){if(window.addEventListener){window.addEventListener("load",this.w,false);window.addEventListener("mousemove",this.A,false)}else if(document.attachEvent){document.attachEvent("onload", -this.w);document.attachEvent("onmousemove",this.A)}else throw new sjcl.exception.bug("can't attach event");this.t=true}},stopCollectors:function(){if(this.t){if(window.removeEventListener){window.removeEventListener("load",this.w);window.removeEventListener("mousemove",this.A)}else if(window.detachEvent){window.detachEvent("onload",this.w);window.detachEvent("onmousemove",this.A)}this.t=false}},addEventListener:function(a,b){this.D[a][this.ca++]=b},removeEventListener:function(a,b){var c;a=this.D[a]; -var d=[];for(c in a)a.hasOwnProperty(c)&&a[c]===b&&d.push(c);for(b=0;b=1<this.n)this.n=c;this.M++;this.ga(b)},A:function(a){sjcl.random.addEntropy([a.x|| -a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},w:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},W:function(a,b){var c;a=sjcl.random.D[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c -4)throw new sjcl.exception.invalid("json encrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,f);a=c.key.slice(0,f.ks/32);f.salt=c.salt}if(typeof b==="string")b=sjcl.codec.utf8String.toBits(b);c=new sjcl.cipher[f.cipher](a);e.j(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(c,b,f.iv,f.adata,f.tag);return e.encode(f)},decrypt:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.j(e.j(e.j({},e.defaults),e.decode(b)),c,true);if(typeof b.salt==="string")b.salt=sjcl.codec.base64.toBits(b.salt); -if(typeof b.iv==="string")b.iv=sjcl.codec.base64.toBits(b.iv);if(!sjcl.mode[b.mode]||!sjcl.cipher[b.cipher]||typeof a==="string"&&b.iter<=100||b.ts!==64&&b.ts!==96&&b.ts!==128||b.ks!==128&&b.ks!==192&&b.ks!==0x100||!b.iv||b.iv.length<2||b.iv.length>4)throw new sjcl.exception.invalid("json decrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,b);a=c.key.slice(0,b.ks/32);b.salt=c.salt}c=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(c,b.ct,b.iv,b.adata,b.tag);e.j(d, +sjcl.keyexchange.srp={makeVerifier:function(a,b,c,d,e){a=this.makeX(a,b,c);a=sjcl.bn.fromBits(a);return e.powermod(a,d)},makeX:function(a,b,c){a=sjcl.hash.sha1.hash(a+":"+b);return sjcl.hash.sha1.hash(sjcl.bitArray.concat(c,a))},knownGroup:function(a){if(typeof a!=="string")a=a.toString();this.X||this.ka();return this.ca[a]},X:false,ka:function(){var a,b;for(a=0;a0;){b++;e>>>=1}this.j[g].update([d,this.Y++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.j[g].update([d,this.Y++,3,b,f,a.length]);this.j[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.s[g]+=b;this.n+=b;if(h===0){this.isReady()!==0&&this.Z("seeded", +Math.max(this.o,this.n));this.Z("progress",this.getProgress())}},isReady:function(a){a=this.Q[a!==undefined?a:this.I];return this.o&&this.o>=a?this.s[0]>80&&(new Date).valueOf()>this.ea?3:1:this.n>=a?2:0},getProgress:function(a){a=this.Q[a?a:this.I];return this.o>=a?1["0"]:this.n>a?1["0"]:this.n/a},startCollectors:function(){if(!this.u){if(window.addEventListener){window.addEventListener("load",this.A,false);window.addEventListener("mousemove",this.B,false)}else if(document.attachEvent){document.attachEvent("onload", +this.A);document.attachEvent("onmousemove",this.B)}else throw new sjcl.exception.bug("can't attach event");this.u=true}},stopCollectors:function(){if(this.u){if(window.removeEventListener){window.removeEventListener("load",this.A);window.removeEventListener("mousemove",this.B)}else if(window.detachEvent){window.detachEvent("onload",this.A);window.detachEvent("onmousemove",this.B)}this.u=false}},addEventListener:function(a,b){this.F[a][this.ha++]=b},removeEventListener:function(a,b){var c;a=this.F[a]; +var d=[];for(c in a)a.hasOwnProperty(c)&&a[c]===b&&d.push(c);for(b=0;b=1<this.o)this.o=c;this.O++;this.ma(b)},B:function(a){sjcl.random.addEntropy([a.x|| +a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},A:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},Z:function(a,b){var c;a=sjcl.random.F[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c +4)throw new sjcl.exception.invalid("json encrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,f);a=c.key.slice(0,f.ks/32);f.salt=c.salt}if(typeof b==="string")b=sjcl.codec.utf8String.toBits(b);c=new sjcl.cipher[f.cipher](a);e.k(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(c,b,f.iv,f.adata,f.tag);return e.encode(f)},decrypt:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.k(e.k(e.k({},e.defaults),e.decode(b)),c,true);if(typeof b.salt==="string")b.salt=sjcl.codec.base64.toBits(b.salt); +if(typeof b.iv==="string")b.iv=sjcl.codec.base64.toBits(b.iv);if(!sjcl.mode[b.mode]||!sjcl.cipher[b.cipher]||typeof a==="string"&&b.iter<=100||b.ts!==64&&b.ts!==96&&b.ts!==128||b.ks!==128&&b.ks!==192&&b.ks!==0x100||!b.iv||b.iv.length<2||b.iv.length>4)throw new sjcl.exception.invalid("json decrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,b);a=c.key.slice(0,b.ks/32);b.salt=c.salt}c=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(c,b.ct,b.iv,b.adata,b.tag);e.k(d, b);d.key=a;return sjcl.codec.utf8String.fromBits(c)},encode:function(a){var b,c="{",d="";for(b in a)if(a.hasOwnProperty(b)){if(!b.match(/^[a-z0-9]+$/i))throw new sjcl.exception.invalid("json encode: invalid property name");c+=d+'"'+b+'":';d=",";switch(typeof a[b]){case "number":case "boolean":c+=a[b];break;case "string":c+='"'+escape(a[b])+'"';break;case "object":c+='"'+sjcl.codec.base64.fromBits(a[b],1)+'"';break;default:throw new sjcl.exception.bug("json encode: unsupported type");}}return c+"}"}, -decode:function(a){a=a.replace(/\s/g,"");if(!a.match(/^\{.*\}$/))throw new sjcl.exception.invalid("json decode: this isn't json!");a=a.replace(/^\{|\}$/g,"").split(/,/);var b={},c,d;for(c=0;c=this.limbs.length?0:this.limbs[a]},greaterEquals:function(a){if(typeof a==="number")a=new this.f(a);var b=0,c=0,d,e,f;for(d=Math.max(this.limbs.length,a.limbs.length)-1;d>=0;d--){e=this.getLimb(d);f=a.getLimb(d);c|=f-e&~b;b|=e-f&~c}return(c|~b)>>>31},toString:function(){this.fullReduce();var a="",b,c,d=this.limbs;for(b=0;b>d}b&&f.push(b);return this},halveM:function(){var a,b=0,c,d=this.radix,e=this.limbs;for(a=e.length-1;a>=0;a--){c=e[a];e[a]=c+b>> 1;b=(c&1)<0;c--){a.halveM();b.greaterEquals(a)&&b.subM(a).normalize()}return b.trim()},inverseMod:function(a){var b=new sjcl.bn(1),c=new sjcl.bn(0),d=new sjcl.bn(this), e=new sjcl.bn(a),f,g=1;if(!(a.limbs[0]&1))throw new sjcl.exception.invalid("inverseMod: p must be odd");do{if(d.limbs[0]&1){if(!d.greaterEquals(e)){f=d;d=e;e=f;f=b;b=c;c=f}d.subM(e);d.normalize();b.greaterEquals(c)||b.addM(a);b.subM(c)}d.halveM();b.limbs[0]&1&&b.addM(a);b.normalize();b.halveM();for(f=g=0;f=0;b--)a=c.concat(a,[c.partial(this.radix,this.getLimb(b))]);return a},bitLength:function(){this.fullReduce();for(var a=this.radix*(this.limbs.length-1),b=this.limbs[this.limbs.length- -1];b;b>>=1)a++;return a+7&-8}};sjcl.bn.fromBits=function(a){var b=new this,c=[],d=sjcl.bitArray,e=this.prototype,f=Math.min(this.bitLength||0x100000000,d.bitLength(a)),g=f%e.radix||e.radix;for(c[0]=d.extract(a,0,g);g=0;b--)a=c.concat(a,[c.partial(this.radix,this.getLimb(b))]);return a},bitLength:function(){this.fullReduce();for(var a=this.radix*(this.limbs.length-1),b=this.limbs[this.limbs.length-1];b;b>>=1)a++;return a+7&-8}}; +sjcl.bn.fromBits=function(a){var b=new this,c=[],d=sjcl.bitArray,e=this.prototype,f=Math.min(this.bitLength||0x100000000,d.bitLength(a)),g=f%e.radix||e.radix;for(c[0]=d.extract(a,0,g);gj;){i=k.pop();o=k.length;for(h=0;hj;){i=k.pop();o=k.length;for(h=0;h Date: Tue, 19 Apr 2011 13:15:23 -0700 Subject: [PATCH 11/27] notready -> notReady --- core/random.js | 2 +- core/sjcl.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/random.js b/core/random.js index 2080cd32..a32eaa3a 100644 --- a/core/random.js +++ b/core/random.js @@ -47,7 +47,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)); } diff --git a/core/sjcl.js b/core/sjcl.js index 90c91b51..4c751cd8 100644 --- a/core/sjcl.js +++ b/core/sjcl.js @@ -58,7 +58,7 @@ var sjcl = { }, /** @class Something isn't ready. */ - notready: function(message) { + notReady: function(message) { this.toString = function() { return "NOT READY: "+this.message; }; this.message = message; } From e6cc1dd6a181ad55805cee020c0c1cfcb2b716ca Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 15:08:56 -0700 Subject: [PATCH 12/27] add srp, sha1, and bn tests to browser test list --- test/run_tests_browser.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/run_tests_browser.js b/test/run_tests_browser.js index 6e73758f..40733e82 100644 --- a/test/run_tests_browser.js +++ b/test/run_tests_browser.js @@ -13,9 +13,15 @@ function testCore(coreName, cb) { "sha256_test.js", "sha256_vectors.js", "sha256_test_brute_force.js", + "sha1_test.js", + "sha1_vectors.js", "hmac_test.js", "hmac_vectors.js", "pbkdf2_test.js", + "srp_test.js", + "srp_vectors.js", + "bn_test.js", + "bn_vectors.js", "ecdh_test.js", "ecdsa_test.js" ], i; From de23a4016cb093fcc4321845d07ae2efcd2a66ba Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 15:09:25 -0700 Subject: [PATCH 13/27] bn_mod and bn_mulmod tests --- test/bn_test.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/bn_vectors.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/test/bn_test.js b/test/bn_test.js index 4a6c1408..32861acc 100644 --- a/test/bn_test.js +++ b/test/bn_test.js @@ -1,3 +1,48 @@ +new sjcl.test.TestCase("Bignum modular reduction test", function (cb) { + if (!sjcl.bn) { + this.unimplemented(); + cb && cb(); + return; + } + + var a, N, r; + for (i=0; i < sjcl.test.vector.bn_mod.length; i++) { + tv = sjcl.test.vector.bn_mod[i]; + try { + a = new sjcl.bn(tv.a); + N = new sjcl.bn(tv.N); + r = a.mod(N); + this.require(r.equals(new sjcl.bn(tv.r))); + } catch(e) { + this.fail(e); + } + } + cb && cb(); +}); + +new sjcl.test.TestCase("Bignum modular multiplication test", function (cb) { + if (!sjcl.bn) { + this.unimplemented(); + cb && cb(); + return; + } + + var a, b, N, r; + for(var j=0;j<10;j++)for (i=0; i < sjcl.test.vector.bn_mulmod.length; i++) { + tv = sjcl.test.vector.bn_mulmod[i]; + try { + a = new sjcl.bn(tv.a); + b = new sjcl.bn(tv.b); + N = new sjcl.bn(tv.N); + r = a.mulmod(b, N); + this.require(r.equals(new sjcl.bn(tv.r))); + } catch(e) { + this.fail(e); + } + } + cb && cb(); +}); + new sjcl.test.TestCase("Bignum modular exponentiation test", function (cb) { if (!sjcl.bn) { this.unimplemented(); diff --git a/test/bn_vectors.js b/test/bn_vectors.js index 053f68a7..c6d72387 100644 --- a/test/bn_vectors.js +++ b/test/bn_vectors.js @@ -1,3 +1,32 @@ +sjcl.test.vector.bn_mod = [ + { + a: "cfb9caac51a13eb13592d47863e463b306547683070424a7c7a41302e30453c2f5f6f2c432a267d2d72746c534d6c233c5c6740776e5c473592d4786377d745c534f2d502427612249266a45382a6f512e527d475577484f277e7a4ce62c7919c0b34b9f125124c574bac9738edb0998bfa8f5b8076c5266ae06e1b9121303d7ff8f0380a24526474d592a7d5e69f125124c574bac9738ed77d745c534f2d502427612249266a45382a6f512e527d4755560144ced0a078454a727d24db5d77484f27b0998bfa8f5b8076c5266ae06e1b9121303d7ff8f0380a24526474d592a7d5e682b2358377d745c534f2d502427612249266a45382a6f512e527d4755560144ced0a078454a727d24db5d77484f277e7b3d28256d71482d2a287d666b3ac7053b02c6543592d47863b6a0541cfa603219b694bec483592d478630", + N: "c3219b694b6a0541cfa60c46a0541cfa60c4c574bac9738edb0998bfa8f5b8076c5266ae06e1b9121303d7ff8f0380a2f51de2fdc93bba83b4c4f49e2D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D498254b6afa60c4835920541cfa60c4c4f49e2D3383B4813D692C855c8a7d64e9", + r: "3c3312d60b052287f92b61bc1e890a3f43f46f302086fc19d7555059f957607981f89d7\ +25b775c70c47e940a874e143efec01558c9cbe5f15df716d812c61cef1a2c6561ee999\ +0b5db8e54827a16018fee6c111c9a8e66897f849ccd9114639ab086f8ecfa558b58555\ +47b1eb110e245f51598bc98486af1813423877189e66a84cd7ead55" + } +] + +sjcl.test.vector.bn_mulmod = [ + { + a: "94B7555AABE9127CC58CCF4993DB6CF84D16C1244021612e464d6e2f4c724f2b3e275a43385a5a6d4c7441284648362a4526474d592a7d5e682b2358377d745c534f2d502427612249266a45382a6f512e527d475577484f277e7b3d28256d71482d2a287d666b52247027", + b: "70424648242f4273612a524e4a44717b655b487b395d2f407d4c7141503e33442a657637257968345237677d6e736f5f24546b642a23283e463b306547683070424a7c7a41302e30453c2f5f6f2c432a267d2d72746c534d6c233c5c6740776e5c4725562029623d7a673d", + N: "EEAF0AB9ADB38D8775FF3C0B9EA27496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", + r: "266bd21de6da4ce62c7919c0b34b9f125124c574bac9738edb0998bfa8f5b8076c5266ae06e1b9121303d7ff8f0380a2f51de2fdc93bba83b4c4f49e2ddc65c24a8e2ecbac374e49181792aeeada8fc5438073187b2cf3d63f93d560144ced0a078454a727d24db5d09e56" + }, + { + a: "94B7555AABE9127CC58CCF4993DB6CF84D16C1244021612e464d6e2f4c724f2b3e275a43385a5a6d4c7441284648362a4526474d592a7d5e682b2358377d745c534f2d502427612249266a45382a6f512e527d475577484f277e7b3d28256d71482d2a287d666b52247027", + b: "70424648242f42364d5635424967654d212a2e4532656123234b3822524f58762e75374d22252f324865406f43386a5d3d435f66315a7e6b6b20277978244f5856626a4f5c796f374c3373612a524e4a44717b655b487b395d2f407d4c7141503e33442a657637257968345237677d6e736f5f24546b642a23283e463b306547683070424a7c7a41302e30453c2f5f6f2c432a267d2d72746c534d6c233c5c6740776e5c4725562029623d7a673d", + N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", + r: "4701ff9bffd27e43a071e0a4c09b22945af95c8d81fda91e1fcbf2c6a54fc9d3f824ec\ +2dc88b88ba455c206965c03dbcb19c5e1826435de032abeac7a576f176a3a037455f0e\ +7f9681755eb6e7ee91a4d58fc75b6db59563e6b4fb69efec1ccc53b7a0003b6c0be278\ +cb9f8075d4bb6eef64015f09749db233e27bf3a1b48ba7" + } +] + sjcl.test.vector.bn_powermod = [ { g: 2, From 945d6c4996945d2e0e581198ec80e299cfe8af91 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 15:09:44 -0700 Subject: [PATCH 14/27] smaller bn test vectors --- test/bn_vectors.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/bn_vectors.js b/test/bn_vectors.js index c6d72387..34cba08f 100644 --- a/test/bn_vectors.js +++ b/test/bn_vectors.js @@ -78,8 +78,8 @@ sjcl.test.vector.bn_powermod = [ }, { g: 2, - x: "94B7555AABE9127CC58CCF4993DB6CF84D16C124", - N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", - v: "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB" - } + x: "eeaf0ab9adb3008dd6c314c9c25600057674df692c0006e0d5d8e2050b98be48e4", + N: "b48130d6e07674df740e1d33b4816e0d5d8e20e2050b98be48e457674df74096ea", + v: "9c3219b694befb9caac51a13eb1ac7053b02c654b6a0541cfa60c483592d478630" + }, ] From 57da29902f1358e6f1c4d57403af4077becaa363 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 16:26:44 -0700 Subject: [PATCH 15/27] SRP group as object --- core/srp.js | 4 ++-- test/srp_test.js | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/srp.js b/core/srp.js index 417cd5ed..814f3245 100644 --- a/core/srp.js +++ b/core/srp.js @@ -8,13 +8,13 @@ * @class SRP */ sjcl.keyexchange.srp = { - makeVerifier: function(I, P, s, N, g) { + makeVerifier: function(I, P, s, group) { var x; // From RFC 5054: // v = g^x mod N x = this.makeX(I, P, s); x = sjcl.bn.fromBits(x); - return g.powermod(x, N); + return group.g.powermod(x, group.N); }, makeX: function(I, P, s) { diff --git a/test/srp_test.js b/test/srp_test.js index d5219e72..8a40b871 100644 --- a/test/srp_test.js +++ b/test/srp_test.js @@ -5,17 +5,16 @@ new sjcl.test.TestCase("SRP known-answer (RFC 5054) tests", function (cb) { return; } - var i, kat = sjcl.test.vector.srp, tv, N, g, v, x; + var i, kat = sjcl.test.vector.srp, tv, group, v, x; for (i=0; i Date: Tue, 19 Apr 2011 16:26:59 -0700 Subject: [PATCH 16/27] docs for makeX --- core/srp.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/core/srp.js b/core/srp.js index 814f3245..6b0dd248 100644 --- a/core/srp.js +++ b/core/srp.js @@ -17,11 +17,16 @@ sjcl.keyexchange.srp = { return group.g.powermod(x, group.N); }, + /** + * Calculates SRP x. + * x = SHA1( | SHA( | ":" | )) [RFC 2945] + * @param {String} I The username. + * @param {String} P The password. + * @param {Object} s A bitArray of the salt. + * @return {Object} A bitArray of SRP x. + */ makeX: function(I, P, s) { - var inner; - // From RFC 2945: - // x = SHA1( | SHA( | ":" | )) - inner = sjcl.hash.sha1.hash(I + ':' + P); + var inner = sjcl.hash.sha1.hash(I + ':' + P); return sjcl.hash.sha1.hash(sjcl.bitArray.concat(s, inner)); }, From cf4ffd762c05d5c1798d5ea464b4a164e1e2081f Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 16:27:04 -0700 Subject: [PATCH 17/27] docs for makeVerifier --- core/srp.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/srp.js b/core/srp.js index 6b0dd248..68d76f44 100644 --- a/core/srp.js +++ b/core/srp.js @@ -8,6 +8,15 @@ * @class SRP */ sjcl.keyexchange.srp = { + /** + * Calculates SRP v, the verifier. + * v = g^x mod N [RFC 5054] + * @param {String} I The username. + * @param {String} P The password. + * @param {Object} s A bitArray of the salt. + * @param {Object} group The SRP group. Use sjcl.keyexchange.srp.knownGroup to obtain this object. + * @return {Object} A bitArray of SRP v. + */ makeVerifier: function(I, P, s, group) { var x; // From RFC 5054: From 5acaf988be4979c57b354f0cd6e9fbe852db39d3 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Tue, 19 Apr 2011 16:28:23 -0700 Subject: [PATCH 18/27] sample vals from RFC 5054 --- test/bn_vectors.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/bn_vectors.js b/test/bn_vectors.js index 34cba08f..b01ba408 100644 --- a/test/bn_vectors.js +++ b/test/bn_vectors.js @@ -82,4 +82,11 @@ sjcl.test.vector.bn_powermod = [ N: "b48130d6e07674df740e1d33b4816e0d5d8e20e2050b98be48e457674df74096ea", v: "9c3219b694befb9caac51a13eb1ac7053b02c654b6a0541cfa60c483592d478630" }, +// // The following values are from TLS-SRP RFC 5054. +// { +// g: 2, +// x: "94B7555AABE9127CC58CCF4993DB6CF84D16C124", +// N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", +// v: "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB" +// } ] From d6ea18a72aa94e0915798ddec658169bc0480b26 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Wed, 20 Apr 2011 17:50:23 -0700 Subject: [PATCH 19/27] merge changes made while preparing to push ecc --- core/bn.js | 6 ++++-- core/sjcl.js | 4 ++-- sjcl.js | 17 ++++++++-------- test/bn_test.js | 6 +++--- test/bn_vectors.js | 41 ++++++++++++++++----------------------- test/run_tests_browser.js | 8 ++++---- 6 files changed, 39 insertions(+), 43 deletions(-) diff --git a/core/bn.js b/core/bn.js index 55ecdba4..672fd085 100644 --- a/core/bn.js +++ b/core/bn.js @@ -275,10 +275,12 @@ sjcl.bn.prototype = { return out; }, - mulmod: function(x, N) { - return this.mod(N).mul(x.mod(N)).mod(N); + /** this * that mod N */ + mulmod: function(that, N) { + return this.mod(N).mul(that.mod(N)).mod(N); }, + /** this ^ x mod N */ powermod: function(x, N) { var result = new sjcl.bn(1), a = new sjcl.bn(this), k = new sjcl.bn(x); while (true) { diff --git a/core/sjcl.js b/core/sjcl.js index 6f1337ee..0c6a17c2 100644 --- a/core/sjcl.js +++ b/core/sjcl.js @@ -60,9 +60,9 @@ var sjcl = { this.message = message; }, - /** @class Bug or missing feature in SJCL. */ + /** @class Something isn't ready. */ notReady: function(message) { - this.toString = function() { return "GENERATOR NOT READY: "+this.message; }; + this.toString = function() { return "NOT READY: "+this.message; }; this.message = message; } } diff --git a/sjcl.js b/sjcl.js index 7a65e280..107d002a 100644 --- a/sjcl.js +++ b/sjcl.js @@ -1,4 +1,4 @@ -"use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"GENERATOR NOT READY: "+this.message};this.message=a}}}; +"use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message};this.message=a}}}; sjcl.cipher.aes=function(a){this.p[0][0][0]||this.M();var b,c,d,e,f=this.p[0][4],g=this.p[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.c=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ g[3][f[c&255]]}}; sjcl.cipher.aes.prototype={encrypt:function(a){return this.U(a,0)},decrypt:function(a){return this.U(a,1)},p:[[[],[],[],[],[]],[[],[],[],[],[]]],M:function(){var a=this.p[0],b=this.p[1],c=a[4],d=b[4],e,f,g,h=[],i=[],j,k,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=j||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;k=h[e=h[j=h[f]]];m=k*0x1010101^e*0x10001^j*0x101^f*0x1010100;k=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=k=k<<24^k>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= @@ -10,13 +10,13 @@ a[d]^b[d];return c===0},fa:function(a,b,c,d){var e;e=0;if(d===undefined)d=[];for sjcl.codec.utf8String={fromBits:function(a){var b="",c=sjcl.bitArray.bitLength(a),d,e;for(d=0;d>>24);e<<=8}return decodeURIComponent(escape(b))},toBits:function(a){a=unescape(encodeURIComponent(a));var b=[],c,d=0;for(c=0;c>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.R,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha1=function(a){if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()}; -sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.h=this.w.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)this.q(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.h;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0); -b.push(Math.floor(this.d/0x100000000));for(b.push(this.d|0);b.length;)this.q(b.splice(0,16));this.reset();return c},w:[1732584193,4023233417,2562383102,271733878,3285377520],c:[1518500249,1859775393,2400959708,3395469782],ja:function(a,b,c,d){if(a<=19)return b&c|~b&d;else if(a<=39)return b^c^d;else if(a<=59)return b&c|b&d|c&d;else if(a<=79)return b^c^d},D:function(a,b){return b<>>32-a},q:function(a){var b,c,d,e,f,g,h=a.slice(0),i=this.h;c=i[0];d=i[1];e=i[2];f=i[3];g=i[4];for(a=0;a<=79;a++){if(a>= -16)h[a]=this.D(1,h[a-3]^h[a-8]^h[a-14]^h[a-16]);b=this.D(5,c)+this.ja(a,d,e,f)+g+h[a]+this.c[Math.floor(a/20)]|0;g=f;f=e;e=this.D(30,d);d=c;c=b}i[0]=i[0]+c|0;i[1]=i[1]+d|0;i[2]=i[2]+e|0;i[3]=i[3]+f|0;i[4]=i[4]+g|0}};sjcl.hash.sha256=function(a){this.c[0]||this.M();if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; +if(d>26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha256=function(a){this.c[0]||this.M();if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.h=this.w.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.q(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.h;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.d/ 4294967296));for(b.push(this.d|0);b.length;)this.q(b.splice(0,16));this.reset();return c},w:[],c:[],M:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.w[b]=a(Math.pow(c,0.5));this.c[b]=a(Math.pow(c,1/3));b++}},q:function(a){var b,c,d=a.slice(0),e=this.h,f=this.c,g=e[0],h=e[1],i=e[2],j=e[3],k=e[4],l=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ -b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(k>>>6^k>>>11^k>>>25^k<<26^k<<21^k<<7)+(m^k&(l^m))+f[a];n=m;m=l;l=k;k=j+b|0;j=i;i=h;h=g;g=b+(h&i^j&(h^i))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+i|0;e[3]=e[3]+j|0;e[4]=e[4]+k|0;e[5]=e[5]+l|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}}; +b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(k>>>6^k>>>11^k>>>25^k<<26^k<<21^k<<7)+(m^k&(l^m))+f[a];n=m;m=l;l=k;k=j+b|0;j=i;i=h;h=g;g=b+(h&i^j&(h^i))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+i|0;e[3]=e[3]+j|0;e[4]=e[4]+k|0;e[5]=e[5]+l|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}};sjcl.hash.sha1=function(a){if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()}; +sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.h=this.w.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)this.q(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.h;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0); +b.push(Math.floor(this.d/0x100000000));for(b.push(this.d|0);b.length;)this.q(b.splice(0,16));this.reset();return c},w:[1732584193,4023233417,2562383102,271733878,3285377520],c:[1518500249,1859775393,2400959708,3395469782],ja:function(a,b,c,d){if(a<=19)return b&c|~b&d;else if(a<=39)return b^c^d;else if(a<=59)return b&c|b&d|c&d;else if(a<=79)return b^c^d},D:function(a,b){return b<>>32-a},q:function(a){var b,c,d,e,f,g,h=a.slice(0),i=this.h;c=i[0];d=i[1];e=i[2];f=i[3];g=i[4];for(a=0;a<=79;a++){if(a>= +16)h[a]=this.D(1,h[a-3]^h[a-8]^h[a-14]^h[a-16]);b=this.D(5,c)+this.ja(a,d,e,f)+g+h[a]+this.c[Math.floor(a/20)]|0;g=f;f=e;e=this.D(30,d);d=c;c=b}i[0]=i[0]+c|0;i[1]=i[1]+d|0;i[2]=i[2]+e|0;i[3]=i[3]+f|0;i[4]=i[4]+g|0}}; sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,i=h.bitLength(c)/8,j=h.bitLength(g)/8;e=e||64;d=d||[];if(i<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(f=2;f<4&&j>>>8*f;f++);if(f<15-i)f=15-i;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.T(a,b,c,d,e,f);g=sjcl.mode.ccm.V(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),i=f.clamp(b,h-e),j=f.bitSlice(b, h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));i=sjcl.mode.ccm.V(a,i,c,j,e,b);a=sjcl.mode.ccm.T(a,i.data,c,d,e,b);if(!f.equal(i.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return i.data},T:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,i=h.m;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.aa=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.t=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b0;){b++;e>>>=1}this.j[g].update([d,this.Y++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.j[g].update([d,this.Y++,3,b,f,a.length]);this.j[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.s[g]+=b;this.n+=b;if(h===0){this.isReady()!==0&&this.Z("seeded", diff --git a/test/bn_test.js b/test/bn_test.js index 32861acc..7756b302 100644 --- a/test/bn_test.js +++ b/test/bn_test.js @@ -4,7 +4,7 @@ new sjcl.test.TestCase("Bignum modular reduction test", function (cb) { cb && cb(); return; } - + var a, N, r; for (i=0; i < sjcl.test.vector.bn_mod.length; i++) { tv = sjcl.test.vector.bn_mod[i]; @@ -26,7 +26,7 @@ new sjcl.test.TestCase("Bignum modular multiplication test", function (cb) { cb && cb(); return; } - + var a, b, N, r; for(var j=0;j<10;j++)for (i=0; i < sjcl.test.vector.bn_mulmod.length; i++) { tv = sjcl.test.vector.bn_mulmod[i]; @@ -49,7 +49,7 @@ new sjcl.test.TestCase("Bignum modular exponentiation test", function (cb) { cb && cb(); return; } - + var i, tv, g, x, N, v; for (i=0; i < sjcl.test.vector.bn_powermod.length; i++) { tv = sjcl.test.vector.bn_powermod[i]; diff --git a/test/bn_vectors.js b/test/bn_vectors.js index b01ba408..3dd5a3ba 100644 --- a/test/bn_vectors.js +++ b/test/bn_vectors.js @@ -1,3 +1,6 @@ +// Verify with Mathematica: +// BaseForm[Mod[16^^a, 16^^N], 16] +// should return "16^^r". sjcl.test.vector.bn_mod = [ { a: "cfb9caac51a13eb13592d47863e463b306547683070424a7c7a41302e30453c2f5f6f2c432a267d2d72746c534d6c233c5c6740776e5c473592d4786377d745c534f2d502427612249266a45382a6f512e527d475577484f277e7a4ce62c7919c0b34b9f125124c574bac9738edb0998bfa8f5b8076c5266ae06e1b9121303d7ff8f0380a24526474d592a7d5e69f125124c574bac9738ed77d745c534f2d502427612249266a45382a6f512e527d4755560144ced0a078454a727d24db5d77484f27b0998bfa8f5b8076c5266ae06e1b9121303d7ff8f0380a24526474d592a7d5e682b2358377d745c534f2d502427612249266a45382a6f512e527d4755560144ced0a078454a727d24db5d77484f277e7b3d28256d71482d2a287d666b3ac7053b02c6543592d47863b6a0541cfa603219b694bec483592d478630", @@ -9,21 +12,15 @@ sjcl.test.vector.bn_mod = [ } ] +// Verify with Mathematica: +// BaseForm[Mod[16^^a * 16^^b, 16^^N], 16] +// should return "16^^r". sjcl.test.vector.bn_mulmod = [ { a: "94B7555AABE9127CC58CCF4993DB6CF84D16C1244021612e464d6e2f4c724f2b3e275a43385a5a6d4c7441284648362a4526474d592a7d5e682b2358377d745c534f2d502427612249266a45382a6f512e527d475577484f277e7b3d28256d71482d2a287d666b52247027", b: "70424648242f4273612a524e4a44717b655b487b395d2f407d4c7141503e33442a657637257968345237677d6e736f5f24546b642a23283e463b306547683070424a7c7a41302e30453c2f5f6f2c432a267d2d72746c534d6c233c5c6740776e5c4725562029623d7a673d", N: "EEAF0AB9ADB38D8775FF3C0B9EA27496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", r: "266bd21de6da4ce62c7919c0b34b9f125124c574bac9738edb0998bfa8f5b8076c5266ae06e1b9121303d7ff8f0380a2f51de2fdc93bba83b4c4f49e2ddc65c24a8e2ecbac374e49181792aeeada8fc5438073187b2cf3d63f93d560144ced0a078454a727d24db5d09e56" - }, - { - a: "94B7555AABE9127CC58CCF4993DB6CF84D16C1244021612e464d6e2f4c724f2b3e275a43385a5a6d4c7441284648362a4526474d592a7d5e682b2358377d745c534f2d502427612249266a45382a6f512e527d475577484f277e7b3d28256d71482d2a287d666b52247027", - b: "70424648242f42364d5635424967654d212a2e4532656123234b3822524f58762e75374d22252f324865406f43386a5d3d435f66315a7e6b6b20277978244f5856626a4f5c796f374c3373612a524e4a44717b655b487b395d2f407d4c7141503e33442a657637257968345237677d6e736f5f24546b642a23283e463b306547683070424a7c7a41302e30453c2f5f6f2c432a267d2d72746c534d6c233c5c6740776e5c4725562029623d7a673d", - N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", - r: "4701ff9bffd27e43a071e0a4c09b22945af95c8d81fda91e1fcbf2c6a54fc9d3f824ec\ -2dc88b88ba455c206965c03dbcb19c5e1826435de032abeac7a576f176a3a037455f0e\ -7f9681755eb6e7ee91a4d58fc75b6db59563e6b4fb69efec1ccc53b7a0003b6c0be278\ -cb9f8075d4bb6eef64015f09749db233e27bf3a1b48ba7" } ] @@ -57,7 +54,7 @@ sjcl.test.vector.bn_powermod = [ x: 0x1010, N: 131, v: 59 - }, + }, { g: 2, x: "43207437777777877617151", @@ -75,18 +72,14 @@ sjcl.test.vector.bn_powermod = [ x: "89457115510016156219817846189181057618965150496979174671534084187", N: "1897166415676096761", v: "16840615e646a4c5c8d" - }, - { - g: 2, - x: "eeaf0ab9adb3008dd6c314c9c25600057674df692c0006e0d5d8e2050b98be48e4", - N: "b48130d6e07674df740e1d33b4816e0d5d8e20e2050b98be48e457674df74096ea", - v: "9c3219b694befb9caac51a13eb1ac7053b02c654b6a0541cfa60c483592d478630" - }, -// // The following values are from TLS-SRP RFC 5054. -// { -// g: 2, -// x: "94B7555AABE9127CC58CCF4993DB6CF84D16C124", -// N: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3", -// v: "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB" -// } + }//, + // // This was disabled because it's slow (~2.5s with `rhino -O 9`). Once + // // a better powermod algorithm is implemented (e.g., using Montgomery + // // reduction), this can be re-enabled. + // { + // g: 2, + // x: "eeaf0ab9adb3008dd6c314c9c25600057674df692c0006e0d5d8e2050b98be48e4", + // N: "b48130d6e07674df740e1d33b4816e0d5d8e20e2050b98be48e457674df74096ea", + // v: "9c3219b694befb9caac51a13eb1ac7053b02c654b6a0541cfa60c483592d478630" + // } ] diff --git a/test/run_tests_browser.js b/test/run_tests_browser.js index 40733e82..617d751a 100644 --- a/test/run_tests_browser.js +++ b/test/run_tests_browser.js @@ -18,12 +18,12 @@ function testCore(coreName, cb) { "hmac_test.js", "hmac_vectors.js", "pbkdf2_test.js", - "srp_test.js", - "srp_vectors.js", + "ecdh_test.js", + "ecdsa_test.js", "bn_test.js", "bn_vectors.js", - "ecdh_test.js", - "ecdsa_test.js" + "srp_test.js", + "srp_vectors.js" ], i; for (i=1; i Date: Wed, 20 Apr 2011 22:22:38 -0700 Subject: [PATCH 20/27] remove ecc --- core/ecc.js | 380 -------------------------------------- test/run_tests_browser.js | 2 - 2 files changed, 382 deletions(-) delete mode 100644 core/ecc.js diff --git a/core/ecc.js b/core/ecc.js deleted file mode 100644 index 504288fd..00000000 --- a/core/ecc.js +++ /dev/null @@ -1,380 +0,0 @@ -sjcl.ecc = {}; - -/** - * Represents a point on a curve in affine coordinates. - * @constructor - * @param {sjcl.ecc.curve} curve The curve that this point lies on. - * @param {bigInt} x The x coordinate. - * @param {bigInt} y The y coordinate. - */ -sjcl.ecc.point = function(curve,x,y) { - if (x === undefined) { - this.isIdentity = true; - } else { - this.x = x; - this.y = y; - this.isIdentity = false; - } - this.curve = curve; -}; - - - -sjcl.ecc.point.prototype = { - toJac: function() { - return new sjcl.ecc.pointJac(this.curve, this.x, this.y, new this.curve.field(1)); - }, - - mult: function(k) { - return this.toJac().mult(k, this).toAffine(); - }, - - /** - * Multiply this point by k, added to affine2*k2, and return the answer in Jacobian coordinates. - * @param {bigInt} k The coefficient to multiply this by. - * @param {bigInt} k2 The coefficient to multiply affine2 this by. - * @param {sjcl.ecc.point} affine The other point in affine coordinates. - * @return {sjcl.ecc.pointJac} The result of the multiplication and addition, in Jacobian coordinates. - */ - mult2: function(k, k2, affine2) { - return this.toJac().mult2(k, this, k2, affine2).toAffine(); - }, - - multiples: function() { - var m, i, j; - if (this._multiples === undefined) { - j = this.toJac().doubl(); - m = this._multiples = [new sjcl.ecc.point(this.curve), this, j.toAffine()]; - for (i=3; i<16; i++) { - j = j.add(this); - m.push(j.toAffine()); - } - } - return this._multiples; - }, - - isValid: function() { - return this.y.square().equals(this.curve.b.add(this.x.mul(this.curve.a.add(this.x.square())))); - }, - - toBits: function() { - return sjcl.bitArray.concat(this.x.toBits(), this.y.toBits()); - } -}; - -/** - * Represents a point on a curve in Jacobian coordinates. Coordinates can be specified as bigInts or strings (which - * will be converted to bigInts). - * - * @constructor - * @param {bigInt/string} x The x coordinate. - * @param {bigInt/string} y The y coordinate. - * @param {bigInt/string} z The z coordinate. - * @param {sjcl.ecc.curve} curve The curve that this point lies on. - */ -sjcl.ecc.pointJac = function(curve, x, y, z) { - if (x === undefined) { - this.isIdentity = true; - } else { - this.x = x; - this.y = y; - this.z = z; - this.isIdentity = false; - } - this.curve = curve; -}; - -sjcl.ecc.pointJac.prototype = { - /** - * Adds S and T and returns the result in Jacobian coordinates. Note that S must be in Jacobian coordinates and T must be in affine coordinates. - * @param {sjcl.ecc.pointJac} S One of the points to add, in Jacobian coordinates. - * @param {sjcl.ecc.point} T The other point to add, in affine coordinates. - * @return {sjcl.ecc.pointJac} The sum of the two points, in Jacobian coordinates. - */ - add: function(T) { - var S = this, sz2, c, d, c2, x1, x2, x, y1, y2, y, z; - if (S.curve !== T.curve) { - throw("sjcl.ecc.add(): Points must be on the same curve to add them!"); - } - - if (S.isIdentity) { - return T.toJac(); - } else if (T.isIdentity) { - return S; - } - - sz2 = S.z.square(); - c = T.x.mul(sz2).subM(S.x); - - if (c.equals(0)) { - if (S.y.equals(T.y.mul(sz2.mul(S.z)))) { - // same point - return S.doubl(); - } else { - // inverses - return new sjcl.ecc.pointJac(S.curve); - } - } - - d = T.y.mul(sz2.mul(S.z)).subM(S.y); - c2 = c.square(); - - x1 = d.square(); - x2 = c.square().mul(c).addM( S.x.add(S.x).mul(c2) ); - x = x1.subM(x2); - - y1 = S.x.mul(c2).subM(x).mul(d); - y2 = S.y.mul(c.square().mul(c)); - y = y1.subM(y2); - - z = S.z.mul(c); - - return new sjcl.ecc.pointJac(this.curve,x,y,z); - }, - - /** - * doubles this point. - * @return {sjcl.ecc.pointJac} The doubled point. - */ - doubl: function() { - if (this.isIdentity) { return this; } - - var - y2 = this.y.square(), - a = y2.mul(this.x.mul(4)), - b = y2.square().mul(8), - z2 = this.z.square(), - c = this.x.sub(z2).mul(3).mul(this.x.add(z2)), - x = c.square().subM(a).subM(a), - y = a.sub(x).mul(c).subM(b), - z = this.y.add(this.y).mul(this.z); - return new sjcl.ecc.pointJac(this.curve, x, y, z); - }, - - /** - * Returns a copy of this point converted to affine coordinates. - * @return {sjcl.ecc.point} The converted point. - */ - toAffine: function() { - if (this.isIdentity || this.z.equals(0)) { - return new sjcl.ecc.point(this.curve); - } - var zi = this.z.inverse(), zi2 = zi.square(); - return new sjcl.ecc.point(this.curve, this.x.mul(zi2).fullReduce(), this.y.mul(zi2.mul(zi)).fullReduce()); - }, - - /** - * Multiply this point by k and return the answer in Jacobian coordinates. - * @param {bigInt} k The coefficient to multiply by. - * @param {sjcl.ecc.point} affine This point in affine coordinates. - * @return {sjcl.ecc.pointJac} The result of the multiplication, in Jacobian coordinates. - */ - mult: function(k, affine) { - if (typeof(k) === "number") { - k = [k]; - } else if (k.limbs !== undefined) { - k = k.normalize().limbs; - } - - var i, j, out = new sjcl.ecc.point(this.curve).toJac(), multiples = affine.multiples(); - - for (i=k.length-1; i>=0; i--) { - for (j=sjcl.bn.prototype.radix-4; j>=0; j-=4) { - out = out.doubl().doubl().doubl().doubl().add(multiples[k[i]>>j & 0xF]); - } - } - - return out; - }, - - /** - * Multiply this point by k, added to affine2*k2, and return the answer in Jacobian coordinates. - * @param {bigInt} k The coefficient to multiply this by. - * @param {sjcl.ecc.point} affine This point in affine coordinates. - * @param {bigInt} k2 The coefficient to multiply affine2 this by. - * @param {sjcl.ecc.point} affine The other point in affine coordinates. - * @return {sjcl.ecc.pointJac} The result of the multiplication and addition, in Jacobian coordinates. - */ - mult2: function(k1, affine, k2, affine2) { - if (typeof(k1) === "number") { - k1 = [k1]; - } else if (k1.limbs !== undefined) { - k1 = k1.normalize().limbs; - } - - if (typeof(k2) === "number") { - k2 = [k2]; - } else if (k2.limbs !== undefined) { - k2 = k2.normalize().limbs; - } - - var i, j, out = new sjcl.ecc.point(this.curve).toJac(), m1 = affine.multiples(), - m2 = affine2.multiples(), l1, l2; - - for (i=Math.max(k1.length,k2.length)-1; i>=0; i--) { - l1 = k1[i] | 0; - l2 = k2[i] | 0; - for (j=sjcl.bn.prototype.radix-4; j>=0; j-=4) { - out = out.doubl().doubl().doubl().doubl().add(m1[l1>>j & 0xF]).add(m2[l2>>j & 0xF]); - } - } - - return out; - }, - - isValid: function() { - var z2 = this.z.square(), z4 = z2.square(), z6 = z4.mul(z2); - return this.y.square().equals( - this.curve.b.mul(z6).add(this.x.mul( - this.curve.a.mul(z4).add(this.x.square())))); - } -}; - -/** - * Construct an elliptic curve. Most users will not use this and instead start with one of the NIST curves defined below. - * - * @constructor - * @param {bigInt} p The prime modulus. - * @param {bigInt} r The prime order of the curve. - * @param {bigInt} a The constant a in the equation of the curve y^2 = x^3 + ax + b (for NIST curves, a is always -3). - * @param {bigInt} x The x coordinate of a base point of the curve. - * @param {bigInt} y The y coordinate of a base point of the curve. - */ -sjcl.ecc.curve = function(Field, r, a, b, x, y) { - this.field = Field; - this.r = Field.prototype.modulus.sub(r); - this.a = new Field(a); - this.b = new Field(b); - this.G = new sjcl.ecc.point(this, new Field(x), new Field(y)); -}; - -sjcl.ecc.curve.prototype.fromBits = function (bits) { - var w = sjcl.bitArray, l = this.field.prototype.exponent + 7 & -8, - p = new sjcl.ecc.point(this, this.field.fromBits(w.bitSlice(bits, 0, l)), - this.field.fromBits(w.bitSlice(bits, l, 2*l))); - if (!p.isValid()) { - throw new sjcl.exception.corrupt("not on the curve!"); - } - return p; -}; - -sjcl.ecc.curves = { - c192: new sjcl.ecc.curve( - sjcl.bn.prime.p192, - "0x662107c8eb94364e4b2dd7ce", - -3, - "0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", - "0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012", - "0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811"), - - c224: new sjcl.ecc.curve( - sjcl.bn.prime.p224, - "0xe95c1f470fc1ec22d6baa3a3d5c4", - -3, - "0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4", - "0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", - "0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"), - - c256: new sjcl.ecc.curve( - sjcl.bn.prime.p256, - "0x4319055358e8617b0c46353d039cdaae", - -3, - "0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", - "0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", - "0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"), - - c384: new sjcl.ecc.curve( - sjcl.bn.prime.p384, - "0x389cb27e0bc8d21fa7e5f24cb74f58851313e696333ad68c", - -3, - "0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef", - "0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7", - "0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f") -}; - - -/* Diffie-Hellman-like public-key system */ -sjcl.ecc._dh = function(cn) { - sjcl.ecc[cn] = { - publicKey: function(curve, point) { - this._curve = curve; - if (point instanceof Array) { - this._point = curve.fromBits(point); - } else { - this._point = point; - } - }, - - secretKey: function(curve, exponent) { - this._curve = curve; - this._exponent = exponent; - }, - - generateKeys: function(curve, paranoia) { - if (curve === undefined) { - curve = 256; - } - if (typeof curve === "number") { - curve = sjcl.ecc.curves['c'+curve]; - if (curve === undefined) { - throw new sjcl.exception.invalid("no such curve"); - } - } - var sec = sjcl.bn.random(curve.r, paranoia), pub = curve.G.mult(sec); - return { pub: new sjcl.ecc[cn].publicKey(curve, pub), - sec: new sjcl.ecc[cn].secretKey(curve, sec) }; - } - }; -}; - -sjcl.ecc._dh("elGamal"); - -sjcl.ecc.elGamal.publicKey.prototype = { - kem: function(paranoia) { - var sec = sjcl.bn.random(this._curve.r, paranoia), - tag = this._curve.G.mult(sec).toBits(), - key = sjcl.hash.sha256.hash(this._point.mult(sec).toBits()); - return { key: key, tag: tag }; - } -}; - -sjcl.ecc.elGamal.secretKey.prototype = { - unkem: function(tag) { - return sjcl.hash.sha256.hash(this._curve.fromBits(tag).mult(this._exponent).toBits()); - }, - - dh: function(pk) { - return sjcl.hash.sha256.hash(pk._point.mult(this._exponent).toBits()); - } -}; - -sjcl.ecc._dh("ecdsa"); - -sjcl.ecc.ecdsa.secretKey.prototype = { - sign: function(hash, paranoia) { - var R = this._curve.r, - l = R.bitLength(), - k = sjcl.bn.random(R.sub(1), paranoia).add(1), - r = this._curve.G.mult(k).x.mod(R), - s = sjcl.bn.fromBits(hash).add(r.mul(this._exponent)).inverseMod(R).mul(k).mod(R); - return sjcl.bitArray.concat(r.toBits(l), s.toBits(l)); - } -}; - -sjcl.ecc.ecdsa.publicKey.prototype = { - verify: function(hash, rs) { - var w = sjcl.bitArray, - R = this._curve.r, - l = R.bitLength(), - r = sjcl.bn.fromBits(w.bitSlice(rs,0,l)), - s = sjcl.bn.fromBits(w.bitSlice(rs,l,2*l)), - hG = sjcl.bn.fromBits(hash).mul(s).mod(R), - hA = r.mul(s).mod(R), - r2 = this._curve.G.mult2(hG, hA, this._point).x; - - if (r.equals(0) || s.equals(0) || r.greaterEquals(R) || s.greaterEquals(R) || !r2.equals(r)) { - throw (new sjcl.exception.corrupt("signature didn't check out")); - } - return true; - } -}; diff --git a/test/run_tests_browser.js b/test/run_tests_browser.js index 617d751a..007b66e7 100644 --- a/test/run_tests_browser.js +++ b/test/run_tests_browser.js @@ -18,8 +18,6 @@ function testCore(coreName, cb) { "hmac_test.js", "hmac_vectors.js", "pbkdf2_test.js", - "ecdh_test.js", - "ecdsa_test.js", "bn_test.js", "bn_vectors.js", "srp_test.js", From 90a62ba0400e51396b27f6ffe8158b614dd462cf Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Wed, 20 Apr 2011 22:22:54 -0700 Subject: [PATCH 21/27] add srp.js file header --- core/srp.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/srp.js b/core/srp.js index 68d76f44..59e3c517 100644 --- a/core/srp.js +++ b/core/srp.js @@ -1,4 +1,11 @@ /** @fileOverview Javascript SRP implementation. + * + * This file contains a partial implementation of the SRP (Secure Remote + * Password) password-authenticated key exchange protocol. Given a user + * identity, salt, and SRP group, it generates the SRP verifier that may + * be sent to a remote server to establish and SRP account. + * + * For more information, see http://srp.stanford.edu/. * * @author Quinn Slack */ From 3346363186e0ff7c66d49a738d97b5572cfe701c Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Wed, 20 Apr 2011 22:23:11 -0700 Subject: [PATCH 22/27] wrap params --- core/srp.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/srp.js b/core/srp.js index 59e3c517..52adef18 100644 --- a/core/srp.js +++ b/core/srp.js @@ -21,7 +21,8 @@ sjcl.keyexchange.srp = { * @param {String} I The username. * @param {String} P The password. * @param {Object} s A bitArray of the salt. - * @param {Object} group The SRP group. Use sjcl.keyexchange.srp.knownGroup to obtain this object. + * @param {Object} group The SRP group. Use sjcl.keyexchange.srp.knownGroup + to obtain this object. * @return {Object} A bitArray of SRP v. */ makeVerifier: function(I, P, s, group) { From 56154f318f5707b22bfae2e8db2f168d8e6b8d72 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Wed, 20 Apr 2011 22:23:17 -0700 Subject: [PATCH 23/27] remove duplicated comment --- core/srp.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/srp.js b/core/srp.js index 52adef18..d1dba280 100644 --- a/core/srp.js +++ b/core/srp.js @@ -27,8 +27,6 @@ sjcl.keyexchange.srp = { */ makeVerifier: function(I, P, s, group) { var x; - // From RFC 5054: - // v = g^x mod N x = this.makeX(I, P, s); x = sjcl.bn.fromBits(x); return group.g.powermod(x, group.N); From 0fbd2767763983ed83b2afd7163155f6ceecf1ba Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Wed, 20 Apr 2011 22:24:02 -0700 Subject: [PATCH 24/27] re-./configure after srp changes --- sjcl.js | 108 ++++++++++++++++++-------------------------------------- 1 file changed, 34 insertions(+), 74 deletions(-) diff --git a/sjcl.js b/sjcl.js index 107d002a..be131129 100644 --- a/sjcl.js +++ b/sjcl.js @@ -1,80 +1,40 @@ "use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message};this.message=a}}}; -sjcl.cipher.aes=function(a){this.p[0][0][0]||this.M();var b,c,d,e,f=this.p[0][4],g=this.p[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.c=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ +sjcl.cipher.aes=function(a){this.h[0][0][0]||this.w();var b,c,d,e,f=this.h[0][4],g=this.h[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.a=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ g[3][f[c&255]]}}; -sjcl.cipher.aes.prototype={encrypt:function(a){return this.U(a,0)},decrypt:function(a){return this.U(a,1)},p:[[[],[],[],[],[]],[[],[],[],[],[]]],M:function(){var a=this.p[0],b=this.p[1],c=a[4],d=b[4],e,f,g,h=[],i=[],j,k,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=j||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;k=h[e=h[j=h[f]]];m=k*0x1010101^e*0x10001^j*0x101^f*0x1010100;k=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=k=k<<24^k>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= -0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},U:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.c[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,i,j=c.length/4-2,k,l=4,m=[0,0,0,0];g=this.p[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(k=0;k>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[l];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[l+1];i=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[l+2];a=n[a>>>24]^o[d>>16& -255]^p[e>>8&255]^q[f&255]^c[l+3];l+=4;d=g;e=h;f=i}for(k=0;k<4;k++){m[b?3&-k:k]=r[d>>>24]<<24^r[e>>16&255]<<16^r[f>>8&255]<<8^r[a&255]^c[l++];g=d;d=e;e=f;f=a;a=g}return m}}; -sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.fa(a.slice(b/32),32-(b&31)).slice(1);return c===undefined?a:sjcl.bitArray.clamp(a,c-b)},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a[b/32|0]<<32-d^a[b/32+1|0]>>>d:a[b/32|0]>>>d)&(1<>7)*283)^e]=e;for(f=g=0;!c[f];f^=l||1,g=j[g]||1){i=g^g<<1^g<<2^g<<3^g<<4;i=i>>8^i&255^99;c[f]=i;d[i]=f;k=h[e=h[l=h[f]]];m=k*0x1010101^e*0x10001^l*0x101^f*0x1010100;k=h[i]*0x101^i*0x1010100;for(e=0;e<4;e++){a[e][f]=k=k<<24^k>>>8;b[e][i]=m=m<<24^m>>>8}}for(e= +0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},H:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.a[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,j,l=c.length/4-2,k,i=4,m=[0,0,0,0];g=this.h[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(k=0;k>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[i];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[i+1];j=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[i+2];a=n[a>>>24]^o[d>>16& +255]^p[e>>8&255]^q[f&255]^c[i+3];i+=4;d=g;e=h;f=j}for(k=0;k<4;k++){m[b?3&-k:k]=r[d>>>24]<<24^r[e>>16&255]<<16^r[f>>8&255]<<8^r[a&255]^c[i++];g=d;d=e;e=f;f=a;a=g}return m}}; +sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.P(a.slice(b/32),32-(b&31)).slice(1);return c===undefined?a:sjcl.bitArray.clamp(a,c-b)},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a[b/32|0]<<32-d^a[b/32+1|0]>>>d:a[b/32|0]>>>d)&(1<0&&b)a[c-1]=sjcl.bitArray.partial(b,a[c-1]&2147483648>>b-1,1);return a},partial:function(a,b,c){if(a===32)return b;return(c?b|0:b<<32-a)+a*0x10000000000},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return false;var c=0,d;for(d=0;d=32;b-=32){d.push(c);c=0}if(b===0)return d.concat(a);for(e=0;e>>b);c=a[e]<<32-b}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,b+a>32?c:d.pop(),1));return d},m:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}}; +a[d]^b[d];return c===0},P:function(a,b,c,d){var e;e=0;if(d===undefined)d=[];for(;b>=32;b-=32){d.push(c);c=0}if(b===0)return d.concat(a);for(e=0;e>>b);c=a[e]<<32-b}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,b+a>32?c:d.pop(),1));return d},k:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}}; sjcl.codec.utf8String={fromBits:function(a){var b="",c=sjcl.bitArray.bitLength(a),d,e;for(d=0;d>>24);e<<=8}return decodeURIComponent(escape(b))},toBits:function(a){a=unescape(encodeURIComponent(a));var b=[],c,d=0;for(c=0;c>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.R,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha256=function(a){this.c[0]||this.M();if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; -sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.h=this.w.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.q(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.h;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.d/ -4294967296));for(b.push(this.d|0);b.length;)this.q(b.splice(0,16));this.reset();return c},w:[],c:[],M:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.w[b]=a(Math.pow(c,0.5));this.c[b]=a(Math.pow(c,1/3));b++}},q:function(a){var b,c,d=a.slice(0),e=this.h,f=this.c,g=e[0],h=e[1],i=e[2],j=e[3],k=e[4],l=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ -b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(k>>>6^k>>>11^k>>>25^k<<26^k<<21^k<<7)+(m^k&(l^m))+f[a];n=m;m=l;l=k;k=j+b|0;j=i;i=h;h=g;g=b+(h&i^j&(h^i))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+i|0;e[3]=e[3]+j|0;e[4]=e[4]+k|0;e[5]=e[5]+l|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}};sjcl.hash.sha1=function(a){if(a){this.h=a.h.slice(0);this.e=a.e.slice(0);this.d=a.d}else this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()}; -sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.h=this.w.slice(0);this.e=[];this.d=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.e=sjcl.bitArray.concat(this.e,a);b=this.d;a=this.d=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)this.q(c.splice(0,16));return this},finalize:function(){var a,b=this.e,c=this.h;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0); -b.push(Math.floor(this.d/0x100000000));for(b.push(this.d|0);b.length;)this.q(b.splice(0,16));this.reset();return c},w:[1732584193,4023233417,2562383102,271733878,3285377520],c:[1518500249,1859775393,2400959708,3395469782],ja:function(a,b,c,d){if(a<=19)return b&c|~b&d;else if(a<=39)return b^c^d;else if(a<=59)return b&c|b&d|c&d;else if(a<=79)return b^c^d},D:function(a,b){return b<>>32-a},q:function(a){var b,c,d,e,f,g,h=a.slice(0),i=this.h;c=i[0];d=i[1];e=i[2];f=i[3];g=i[4];for(a=0;a<=79;a++){if(a>= -16)h[a]=this.D(1,h[a-3]^h[a-8]^h[a-14]^h[a-16]);b=this.D(5,c)+this.ja(a,d,e,f)+g+h[a]+this.c[Math.floor(a/20)]|0;g=f;f=e;e=this.D(30,d);d=c;c=b}i[0]=i[0]+c|0;i[1]=i[1]+d|0;i[2]=i[2]+e|0;i[3]=i[3]+f|0;i[4]=i[4]+g|0}}; -sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,i=h.bitLength(c)/8,j=h.bitLength(g)/8;e=e||64;d=d||[];if(i<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(f=2;f<4&&j>>>8*f;f++);if(f<15-i)f=15-i;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.T(a,b,c,d,e,f);g=sjcl.mode.ccm.V(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),i=f.clamp(b,h-e),j=f.bitSlice(b, -h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));i=sjcl.mode.ccm.V(a,i,c,j,e,b);a=sjcl.mode.ccm.T(a,i.data,c,d,e,b);if(!f.equal(i.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return i.data},T:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,i=h.m;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); -f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>3&15))*0x1010101;c=a.encrypt(f(c,e.concat(b,[g,g,g,g]).slice(d,d+4)));i.splice(d,0,c[0],c[1],c[2],c[3]);return i},decrypt:function(a,b,c,d){if(d&&d.length)throw new sjcl.exception.invalid("cbc can't authenticate data");if(sjcl.bitArray.bitLength(c)!==128)throw new sjcl.exception.invalid("cbc iv must be 128 bits");if(sjcl.bitArray.bitLength(b)&127||!b.length)throw new sjcl.exception.corrupt("cbc ciphertext must be a positive multiple of the block size"); -var e=sjcl.bitArray,f=e.m,g,h=[];for(d=0;d16)throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");c=g*0x1010101;if(!e.equal(e.bitSlice([c,c,c,c],0,g*8),e.bitSlice(h,h.length*32-g*8,h.length*32)))throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");return e.bitSlice(h,0,h.length*32-g*8)}}}; -sjcl.mode.ocb2={name:"ocb2",encrypt:function(a,b,c,d,e,f){if(sjcl.bitArray.bitLength(c)!==128)throw new sjcl.exception.invalid("ocb iv must be 128 bits");var g,h=sjcl.mode.ocb2.P,i=sjcl.bitArray,j=i.m,k=[0,0,0,0];c=h(a.encrypt(c));var l,m=[];d=d||[];e=e||64;for(g=0;g+4>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.aa=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.t=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b0;){b++;e>>>=1}this.j[g].update([d,this.Y++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.j[g].update([d,this.Y++,3,b,f,a.length]);this.j[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.s[g]+=b;this.n+=b;if(h===0){this.isReady()!==0&&this.Z("seeded", -Math.max(this.o,this.n));this.Z("progress",this.getProgress())}},isReady:function(a){a=this.Q[a!==undefined?a:this.I];return this.o&&this.o>=a?this.s[0]>80&&(new Date).valueOf()>this.ea?3:1:this.n>=a?2:0},getProgress:function(a){a=this.Q[a?a:this.I];return this.o>=a?1["0"]:this.n>a?1["0"]:this.n/a},startCollectors:function(){if(!this.u){if(window.addEventListener){window.addEventListener("load",this.A,false);window.addEventListener("mousemove",this.B,false)}else if(document.attachEvent){document.attachEvent("onload", -this.A);document.attachEvent("onmousemove",this.B)}else throw new sjcl.exception.bug("can't attach event");this.u=true}},stopCollectors:function(){if(this.u){if(window.removeEventListener){window.removeEventListener("load",this.A);window.removeEventListener("mousemove",this.B)}else if(window.detachEvent){window.detachEvent("onload",this.A);window.detachEvent("onmousemove",this.B)}this.u=false}},addEventListener:function(a,b){this.F[a][this.ha++]=b},removeEventListener:function(a,b){var c;a=this.F[a]; -var d=[];for(c in a)a.hasOwnProperty(c)&&a[c]===b&&d.push(c);for(b=0;b=1<this.o)this.o=c;this.O++;this.ma(b)},B:function(a){sjcl.random.addEntropy([a.x|| -a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},A:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},Z:function(a,b){var c;a=sjcl.random.F[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c -4)throw new sjcl.exception.invalid("json encrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,f);a=c.key.slice(0,f.ks/32);f.salt=c.salt}if(typeof b==="string")b=sjcl.codec.utf8String.toBits(b);c=new sjcl.cipher[f.cipher](a);e.k(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(c,b,f.iv,f.adata,f.tag);return e.encode(f)},decrypt:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.k(e.k(e.k({},e.defaults),e.decode(b)),c,true);if(typeof b.salt==="string")b.salt=sjcl.codec.base64.toBits(b.salt); -if(typeof b.iv==="string")b.iv=sjcl.codec.base64.toBits(b.iv);if(!sjcl.mode[b.mode]||!sjcl.cipher[b.cipher]||typeof a==="string"&&b.iter<=100||b.ts!==64&&b.ts!==96&&b.ts!==128||b.ks!==128&&b.ks!==192&&b.ks!==0x100||!b.iv||b.iv.length<2||b.iv.length>4)throw new sjcl.exception.invalid("json decrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,b);a=c.key.slice(0,b.ks/32);b.salt=c.salt}c=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(c,b.ct,b.iv,b.adata,b.tag);e.k(d, +sjcl.codec.base64={D:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",fromBits:function(a,b){var c="",d,e=0,f=sjcl.codec.base64.D,g=0,h=sjcl.bitArray.bitLength(a);for(d=0;c.length*6>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.D,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha256=function(a){this.a[0]||this.w();if(a){this.n=a.n.slice(0);this.i=a.i.slice(0);this.e=a.e}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; +sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.n=this.N.slice(0);this.i=[];this.e=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.i=sjcl.bitArray.concat(this.i,a);b=this.e;a=this.e=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.C(c.splice(0,16));return this},finalize:function(){var a,b=this.i,c=this.n;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.e/ +4294967296));for(b.push(this.e|0);b.length;)this.C(b.splice(0,16));this.reset();return c},N:[],a:[],w:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.N[b]=a(Math.pow(c,0.5));this.a[b]=a(Math.pow(c,1/3));b++}},C:function(a){var b,c,d=a.slice(0),e=this.n,f=this.a,g=e[0],h=e[1],j=e[2],l=e[3],k=e[4],i=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ +b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(k>>>6^k>>>11^k>>>25^k<<26^k<<21^k<<7)+(m^k&(i^m))+f[a];n=m;m=i;i=k;k=l+b|0;l=j;j=h;h=g;g=b+(h&j^l&(h^j))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+j|0;e[3]=e[3]+l|0;e[4]=e[4]+k|0;e[5]=e[5]+i|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}}; +sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,j=h.bitLength(c)/8,l=h.bitLength(g)/8;e=e||64;d=d||[];if(j<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(f=2;f<4&&l>>>8*f;f++);if(f<15-j)f=15-j;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.G(a,b,c,d,e,f);g=sjcl.mode.ccm.I(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),j=f.clamp(b,h-e),l=f.bitSlice(b, +h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));j=sjcl.mode.ccm.I(a,j,c,l,e,b);a=sjcl.mode.ccm.G(a,j.data,c,d,e,b);if(!f.equal(j.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return j.data},G:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,j=h.k;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); +f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.M=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.l=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b0;){b++;e>>>=1}this.b[g].update([d,this.J++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.b[g].update([d,this.J++,3,b,f,a.length]);this.b[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.j[g]+=b;this.f+=b;if(h===0){this.isReady()!==0&&this.K("seeded",Math.max(this.g, +this.f));this.K("progress",this.getProgress())}},isReady:function(a){a=this.B[a!==undefined?a:this.t];return this.g&&this.g>=a?this.j[0]>80&&(new Date).valueOf()>this.O?3:1:this.f>=a?2:0},getProgress:function(a){a=this.B[a?a:this.t];return this.g>=a?1["0"]:this.f>a?1["0"]:this.f/a},startCollectors:function(){if(!this.m){if(window.addEventListener){window.addEventListener("load",this.o,false);window.addEventListener("mousemove",this.p,false)}else if(document.attachEvent){document.attachEvent("onload", +this.o);document.attachEvent("onmousemove",this.p)}else throw new sjcl.exception.bug("can't attach event");this.m=true}},stopCollectors:function(){if(this.m){if(window.removeEventListener){window.removeEventListener("load",this.o);window.removeEventListener("mousemove",this.p)}else if(window.detachEvent){window.detachEvent("onload",this.o);window.detachEvent("onmousemove",this.p)}this.m=false}},addEventListener:function(a,b){this.r[a][this.Q++]=b},removeEventListener:function(a,b){var c;a=this.r[a]; +var d=[];for(c in a)a.hasOwnProperty(c)&&a[c]===b&&d.push(c);for(b=0;b=1<this.g)this.g=c;this.z++;this.T(b)},p:function(a){sjcl.random.addEntropy([a.x|| +a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},o:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},K:function(a,b){var c;a=sjcl.random.r[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c +4)throw new sjcl.exception.invalid("json encrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,f);a=c.key.slice(0,f.ks/32);f.salt=c.salt}if(typeof b==="string")b=sjcl.codec.utf8String.toBits(b);c=new sjcl.cipher[f.cipher](a);e.c(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(c,b,f.iv,f.adata,f.tag);return e.encode(f)},decrypt:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.c(e.c(e.c({},e.defaults),e.decode(b)),c,true);if(typeof b.salt==="string")b.salt=sjcl.codec.base64.toBits(b.salt); +if(typeof b.iv==="string")b.iv=sjcl.codec.base64.toBits(b.iv);if(!sjcl.mode[b.mode]||!sjcl.cipher[b.cipher]||typeof a==="string"&&b.iter<=100||b.ts!==64&&b.ts!==96&&b.ts!==128||b.ks!==128&&b.ks!==192&&b.ks!==0x100||!b.iv||b.iv.length<2||b.iv.length>4)throw new sjcl.exception.invalid("json decrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,b);a=c.key.slice(0,b.ks/32);b.salt=c.salt}c=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(c,b.ct,b.iv,b.adata,b.tag);e.c(d, b);d.key=a;return sjcl.codec.utf8String.fromBits(c)},encode:function(a){var b,c="{",d="";for(b in a)if(a.hasOwnProperty(b)){if(!b.match(/^[a-z0-9]+$/i))throw new sjcl.exception.invalid("json encode: invalid property name");c+=d+'"'+b+'":';d=",";switch(typeof a[b]){case "number":case "boolean":c+=a[b];break;case "string":c+='"'+escape(a[b])+'"';break;case "object":c+='"'+sjcl.codec.base64.fromBits(a[b],1)+'"';break;default:throw new sjcl.exception.bug("json encode: unsupported type");}}return c+"}"}, -decode:function(a){a=a.replace(/\s/g,"");if(!a.match(/^\{.*\}$/))throw new sjcl.exception.invalid("json decode: this isn't json!");a=a.replace(/^\{|\}$/g,"").split(/,/);var b={},c,d;for(c=0;c=this.limbs.length?0:this.limbs[a]},greaterEquals:function(a){if(typeof a==="number")a=new this.f(a);var b=0,c=0,d,e,f;for(d=Math.max(this.limbs.length,a.limbs.length)-1;d>=0;d--){e=this.getLimb(d);f=a.getLimb(d);c|=f-e&~b;b|=e-f&~c}return(c|~b)>>>31},toString:function(){this.fullReduce();var a="",b,c,d=this.limbs;for(b=0;b>d}b&&f.push(b);return this},halveM:function(){var a,b=0,c,d=this.radix,e=this.limbs;for(a=e.length-1;a>=0;a--){c=e[a];e[a]=c+b>> -1;b=(c&1)<0;c--){a.halveM();b.greaterEquals(a)&&b.subM(a).normalize()}return b.trim()},inverseMod:function(a){var b=new sjcl.bn(1),c=new sjcl.bn(0),d=new sjcl.bn(this), -e=new sjcl.bn(a),f,g=1;if(!(a.limbs[0]&1))throw new sjcl.exception.invalid("inverseMod: p must be odd");do{if(d.limbs[0]&1){if(!d.greaterEquals(e)){f=d;d=e;e=f;f=b;b=c;c=f}d.subM(e);d.normalize();b.greaterEquals(c)||b.addM(a);b.subM(c)}d.halveM();b.limbs[0]&1&&b.addM(a);b.normalize();b.halveM();for(f=g=0;f=0;b--)a=c.concat(a,[c.partial(this.radix,this.getLimb(b))]);return a},bitLength:function(){this.fullReduce();for(var a=this.radix*(this.limbs.length-1),b=this.limbs[this.limbs.length-1];b;b>>=1)a++;return a+7&-8}}; -sjcl.bn.fromBits=function(a){var b=new this,c=[],d=sjcl.bitArray,e=this.prototype,f=Math.min(this.bitLength||0x100000000,d.bitLength(a)),g=f%e.radix||e.radix;for(c[0]=d.extract(a,0,g);gj;){i=k.pop();o=k.length;for(h=0;h=0;b--)for(c=sjcl.bn.prototype.radix-4;c>=0;c-=4)d=d.doubl().doubl().doubl().doubl().add(e[a[b]>>c&15]);return d},mult2:function(a,b,c,d){if(typeof a==="number")a=[a];else if(a.limbs!== -undefined)a=a.normalize().limbs;if(typeof c==="number")c=[c];else if(c.limbs!==undefined)c=c.normalize().limbs;var e,f=(new sjcl.ecc.point(this.curve)).toJac();b=b.multiples();var g=d.multiples(),h,i;for(d=Math.max(a.length,c.length)-1;d>=0;d--){h=a[d]|0;i=c[d]|0;for(e=sjcl.bn.prototype.radix-4;e>=0;e-=4)f=f.doubl().doubl().doubl().doubl().add(b[h>>e&15]).add(g[i>>e&15])}return f},isValid:function(){var a=this.z.square(),b=a.square();a=b.mul(a);return this.y.square().equals(this.curve.b.mul(a).add(this.x.mul(this.curve.a.mul(b).add(this.x.square()))))}}; -sjcl.ecc.curve=function(a,b,c,d,e,f){this.field=a;this.r=a.prototype.modulus.sub(b);this.a=new a(c);this.b=new a(d);this.G=new sjcl.ecc.point(this,new a(e),new a(f))};sjcl.ecc.curve.prototype.fromBits=function(a){var b=sjcl.bitArray,c=this.field.prototype.exponent+7&-8;a=new sjcl.ecc.point(this,this.field.fromBits(b.bitSlice(a,0,c)),this.field.fromBits(b.bitSlice(a,c,2*c)));if(!a.isValid())throw new sjcl.exception.corrupt("not on the curve!");return a}; -sjcl.ecc.curves={c192:new sjcl.ecc.curve(sjcl.bn.prime.p192,"0x662107c8eb94364e4b2dd7ce",-3,"0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1","0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012","0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811"),c224:new sjcl.ecc.curve(sjcl.bn.prime.p224,"0xe95c1f470fc1ec22d6baa3a3d5c4",-3,"0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4","0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21","0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"), -c256:new sjcl.ecc.curve(sjcl.bn.prime.p256,"0x4319055358e8617b0c46353d039cdaae",-3,"0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b","0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296","0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"),c384:new sjcl.ecc.curve(sjcl.bn.prime.p384,"0x389cb27e0bc8d21fa7e5f24cb74f58851313e696333ad68c",-3,"0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef","0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7", -"0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f")}; -sjcl.ecc.W=function(a){sjcl.ecc[a]={publicKey:function(b,c){this.i=b;this.L=c instanceof Array?b.fromBits(c):c},secretKey:function(b,c){this.i=b;this.J=c},generateKeys:function(b,c){if(b===undefined)b=0x100;if(typeof b==="number"){b=sjcl.ecc.curves["c"+b];if(b===undefined)throw new sjcl.exception.invalid("no such curve");}c=sjcl.bn.random(b.r,c);var d=b.G.mult(c);return{pub:new sjcl.ecc[a].publicKey(b,d),sec:new sjcl.ecc[a].secretKey(b,c)}}}};sjcl.ecc.W("elGamal"); -sjcl.ecc.elGamal.publicKey.prototype={kem:function(a){a=sjcl.bn.random(this.i.r,a);var b=this.i.G.mult(a).toBits();return{key:sjcl.hash.sha256.hash(this.L.mult(a).toBits()),tag:b}}};sjcl.ecc.elGamal.secretKey.prototype={unkem:function(a){return sjcl.hash.sha256.hash(this.i.fromBits(a).mult(this.J).toBits())},dh:function(a){return sjcl.hash.sha256.hash(a.L.mult(this.J).toBits())}};sjcl.ecc.W("ecdsa"); -sjcl.ecc.ecdsa.secretKey.prototype={sign:function(a,b){var c=this.i.r,d=c.bitLength(),e=sjcl.bn.random(c.sub(1),b).add(1);b=this.i.G.mult(e).x.mod(c);a=sjcl.bn.fromBits(a).add(b.mul(this.J)).inverseMod(c).mul(e).mod(c);return sjcl.bitArray.concat(b.toBits(d),a.toBits(d))}}; -sjcl.ecc.ecdsa.publicKey.prototype={verify:function(a,b){var c=sjcl.bitArray,d=this.i.r,e=d.bitLength(),f=sjcl.bn.fromBits(c.bitSlice(b,0,e));b=sjcl.bn.fromBits(c.bitSlice(b,e,2*e));a=sjcl.bn.fromBits(a).mul(b).mod(d);c=f.mul(b).mod(d);a=this.i.G.mult2(a,c,this.L).x;if(f.equals(0)||b.equals(0)||f.greaterEquals(d)||b.greaterEquals(d)||!a.equals(f))throw new sjcl.exception.corrupt("signature didn't check out");return true}}; +decode:function(a){a=a.replace(/\s/g,"");if(!a.match(/^\{.*\}$/))throw new sjcl.exception.invalid("json decode: this isn't json!");a=a.replace(/^\{|\}$/g,"").split(/,/);var b={},c,d;for(c=0;c Date: Wed, 20 Apr 2011 22:29:14 -0700 Subject: [PATCH 25/27] undo ecc --- core/bitArray.js | 23 +-- core/cbc.js | 115 ----------- core/convenience.js | 15 +- core/ocb2.js | 5 +- core/sjcl.js | 2 +- test/cbc_test.js | 33 --- test/cbc_vectors.js | 413 -------------------------------------- test/ecdh_test.js | 18 -- test/ecdsa_test.js | 29 --- test/run_tests_browser.js | 2 - 10 files changed, 11 insertions(+), 644 deletions(-) delete mode 100644 core/cbc.js delete mode 100644 test/cbc_test.js delete mode 100644 test/cbc_vectors.js delete mode 100644 test/ecdh_test.js delete mode 100644 test/ecdsa_test.js diff --git a/core/bitArray.js b/core/bitArray.js index a6d06171..40e4acd0 100644 --- a/core/bitArray.js +++ b/core/bitArray.js @@ -31,7 +31,7 @@ sjcl.bitArray = { /** * Array slices in units of bits. - * @param {bitArray} a The array to slice. + * @param {bitArray a} The array to slice. * @param {Number} bstart The offset to the start of the slice, in bits. * @param {Number} bend The offset to the end of the slice, in bits. If this is undefined, * slice until the end of the array. @@ -42,27 +42,6 @@ sjcl.bitArray = { return (bend === undefined) ? a : sjcl.bitArray.clamp(a, bend-bstart); }, - /** - * 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<> 3) & 15)) * 0x1010101; - - /* Pad and encrypt. */ - iv = prp.encrypt(xor(iv,w.concat(plaintext,[bl,bl,bl,bl]).slice(i,i+4))); - output.splice(i,0,iv[0],iv[1],iv[2],iv[3]); - return output; - }, - - /** Decrypt in CBC mode. - * @param {Object} prp The block cipher. It must have a block size of 16 bytes. - * @param {bitArray} ciphertext The ciphertext data. - * @param {bitArray} iv The initialization value. - * @param {bitArray} [adata=[]] The authenticated data. It must be empty. - * @return The decrypted data, an array of bytes. - * @throws {sjcl.exception.invalid} if the IV isn't exactly 128 bits, or if any adata is specified. - * @throws {sjcl.exception.corrupt} if if the message is corrupt. - */ - decrypt: function(prp, ciphertext, iv, adata) { - if (adata && adata.length) { - throw new sjcl.exception.invalid("cbc can't authenticate data"); - } - if (sjcl.bitArray.bitLength(iv) !== 128) { - throw new sjcl.exception.invalid("cbc iv must be 128 bits"); - } - if ((sjcl.bitArray.bitLength(ciphertext) & 127) || !ciphertext.length) { - throw new sjcl.exception.corrupt("cbc ciphertext must be a positive multiple of the block size"); - } - var i, - w = sjcl.bitArray, - xor = w._xor4, - bi, bo, - output = []; - - adata = adata || []; - - for (i=0; i 16) { - throw new sjcl.exception.corrupt("pkcs#5 padding corrupt"); - } - bo = bi * 0x1010101; - if (!w.equal(w.bitSlice([bo,bo,bo,bo], 0, bi*8), - w.bitSlice(output, output.length*32 - bi*8, output.length*32))) { - throw new sjcl.exception.corrupt("pkcs#5 padding corrupt"); - } - - return w.bitSlice(output, 0, output.length*32 - bi*8); - } - }; -}; diff --git a/core/convenience.js b/core/convenience.js index d97f9dfb..2a7a3f25 100644 --- a/core/convenience.js +++ b/core/convenience.js @@ -58,8 +58,7 @@ /* do the encryption */ p.ct = sjcl.mode[p.mode].encrypt(prp, plaintext, p.iv, p.adata, p.tag); - //return j.encode(j._subtract(p, j.defaults)); - return j.encode(p); + return j.encode(j._subtract(p, j.defaults)); }, /** Simple decryption function. @@ -123,7 +122,7 @@ if (!i.match(/^[a-z0-9]+$/i)) { throw new sjcl.exception.invalid("json encode: invalid property name"); } - out += comma + '"' + i + '":'; + out += comma + i + ':'; comma = ','; switch (typeof obj[i]) { @@ -161,13 +160,13 @@ } var a = str.replace(/^\{|\}$/g, '').split(/,/), out={}, i, m; for (i=0; i Date: Wed, 20 Apr 2011 22:34:33 -0700 Subject: [PATCH 26/27] add back sjcl.bitArray.extract - needed for bn --- core/bitArray.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/bitArray.js b/core/bitArray.js index 40e4acd0..803b9c59 100644 --- a/core/bitArray.js +++ b/core/bitArray.js @@ -42,6 +42,27 @@ sjcl.bitArray = { return (bend === undefined) ? a : sjcl.bitArray.clamp(a, bend-bstart); }, + /** + * 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< Date: Wed, 20 Apr 2011 22:36:25 -0700 Subject: [PATCH 27/27] re-make sjcl.js after undoing ecc --- sjcl.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/sjcl.js b/sjcl.js index be131129..01d6ff4f 100644 --- a/sjcl.js +++ b/sjcl.js @@ -1,9 +1,9 @@ "use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message};this.message=a}}}; sjcl.cipher.aes=function(a){this.h[0][0][0]||this.w();var b,c,d,e,f=this.h[0][4],g=this.h[1];b=a.length;var h=1;if(b!==4&&b!==6&&b!==8)throw new sjcl.exception.invalid("invalid aes key size");this.a=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(a%b===0||b===8&&a%b===4){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255];if(a%b===0){c=c<<8^c>>>24^h<<24;h=h<<1^(h>>7)*283}}d[a]=d[a-b]^c}for(b=0;a;b++,a--){c=d[b&3?a:a-4];e[b]=a<=4||b<4?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^ g[3][f[c&255]]}}; -sjcl.cipher.aes.prototype={encrypt:function(a){return this.H(a,0)},decrypt:function(a){return this.H(a,1)},h:[[[],[],[],[],[]],[[],[],[],[],[]]],w:function(){var a=this.h[0],b=this.h[1],c=a[4],d=b[4],e,f,g,h=[],j=[],l,k,i,m;for(e=0;e<0x100;e++)j[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=l||1,g=j[g]||1){i=g^g<<1^g<<2^g<<3^g<<4;i=i>>8^i&255^99;c[f]=i;d[i]=f;k=h[e=h[l=h[f]]];m=k*0x1010101^e*0x10001^l*0x101^f*0x1010100;k=h[i]*0x101^i*0x1010100;for(e=0;e<4;e++){a[e][f]=k=k<<24^k>>>8;b[e][i]=m=m<<24^m>>>8}}for(e= -0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},H:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.a[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,j,l=c.length/4-2,k,i=4,m=[0,0,0,0];g=this.h[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(k=0;k>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[i];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[i+1];j=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[i+2];a=n[a>>>24]^o[d>>16& -255]^p[e>>8&255]^q[f&255]^c[i+3];i+=4;d=g;e=h;f=j}for(k=0;k<4;k++){m[b?3&-k:k]=r[d>>>24]<<24^r[e>>16&255]<<16^r[f>>8&255]<<8^r[a&255]^c[i++];g=d;d=e;e=f;f=a;a=g}return m}}; +sjcl.cipher.aes.prototype={encrypt:function(a){return this.H(a,0)},decrypt:function(a){return this.H(a,1)},h:[[[],[],[],[],[]],[[],[],[],[],[]]],w:function(){var a=this.h[0],b=this.h[1],c=a[4],d=b[4],e,f,g,h=[],i=[],k,j,l,m;for(e=0;e<0x100;e++)i[(h[e]=e<<1^(e>>7)*283)^e]=e;for(f=g=0;!c[f];f^=k||1,g=i[g]||1){l=g^g<<1^g<<2^g<<3^g<<4;l=l>>8^l&255^99;c[f]=l;d[l]=f;j=h[e=h[k=h[f]]];m=j*0x1010101^e*0x10001^k*0x101^f*0x1010100;j=h[l]*0x101^l*0x1010100;for(e=0;e<4;e++){a[e][f]=j=j<<24^j>>>8;b[e][l]=m=m<<24^m>>>8}}for(e= +0;e<5;e++){a[e]=a[e].slice(0);b[e]=b[e].slice(0)}},H:function(a,b){if(a.length!==4)throw new sjcl.exception.invalid("invalid aes block size");var c=this.a[b],d=a[0]^c[0],e=a[b?3:1]^c[1],f=a[2]^c[2];a=a[b?1:3]^c[3];var g,h,i,k=c.length/4-2,j,l=4,m=[0,0,0,0];g=this.h[b];var n=g[0],o=g[1],p=g[2],q=g[3],r=g[4];for(j=0;j>>24]^o[e>>16&255]^p[f>>8&255]^q[a&255]^c[l];h=n[e>>>24]^o[f>>16&255]^p[a>>8&255]^q[d&255]^c[l+1];i=n[f>>>24]^o[a>>16&255]^p[d>>8&255]^q[e&255]^c[l+2];a=n[a>>>24]^o[d>>16& +255]^p[e>>8&255]^q[f&255]^c[l+3];l+=4;d=g;e=h;f=i}for(j=0;j<4;j++){m[b?3&-j:j]=r[d>>>24]<<24^r[e>>16&255]<<16^r[f>>8&255]<<8^r[a&255]^c[l++];g=d;d=e;e=f;f=a;a=g}return m}}; sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.P(a.slice(b/32),32-(b&31)).slice(1);return c===undefined?a:sjcl.bitArray.clamp(a,c-b)},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a[b/32|0]<<32-d^a[b/32+1|0]>>>d:a[b/32|0]>>>d)&(1<0&&b)a[c-1]=sjcl.bitArray.partial(b,a[c-1]&2147483648>>b-1,1);return a},partial:function(a,b,c){if(a===32)return b;return(c?b|0:b<<32-a)+a*0x10000000000},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return false;var c=0,d;for(d=0;d=32;b-=32){d.push(c);c=0}if(b===0)return d.concat(a);for(e=0;e>>b);c=a[e]<<32-b}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,b+a>32?c:d.pop(),1));return d},k:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}}; @@ -12,18 +12,18 @@ sjcl.codec.hex={fromBits:function(a){var b="",c;for(c=0;c>>e)>>>26);if(e<6){g=a[d]<<6-e;e+=26;d++}else{g<<=6;e-=6}}for(;c.length&3&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"");var b=[],c,d=0,e=sjcl.codec.base64.D,f=0,g;for(c=0;c26){d-=26;b.push(f^g>>>d);f=g<<32-d}else{d+=6;f^=g<<32-d}}d&56&&b.push(sjcl.bitArray.partial(d&56,f,1));return b}};sjcl.hash.sha256=function(a){this.a[0]||this.w();if(a){this.n=a.n.slice(0);this.i=a.i.slice(0);this.e=a.e}else this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()}; sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.n=this.N.slice(0);this.i=[];this.e=0;return this},update:function(a){if(typeof a==="string")a=sjcl.codec.utf8String.toBits(a);var b,c=this.i=sjcl.bitArray.concat(this.i,a);b=this.e;a=this.e=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.C(c.splice(0,16));return this},finalize:function(){var a,b=this.i,c=this.n;b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.e/ -4294967296));for(b.push(this.e|0);b.length;)this.C(b.splice(0,16));this.reset();return c},N:[],a:[],w:function(){function a(e){return(e-Math.floor(e))*0x100000000|0}var b=0,c=2,d;a:for(;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue a;if(b<8)this.N[b]=a(Math.pow(c,0.5));this.a[b]=a(Math.pow(c,1/3));b++}},C:function(a){var b,c,d=a.slice(0),e=this.n,f=this.a,g=e[0],h=e[1],j=e[2],l=e[3],k=e[4],i=e[5],m=e[6],n=e[7];for(a=0;a<64;a++){if(a<16)b=d[a];else{b=d[a+1&15];c=d[a+14&15];b=d[a&15]=(b>>>7^b>>>18^ -b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(k>>>6^k>>>11^k>>>25^k<<26^k<<21^k<<7)+(m^k&(i^m))+f[a];n=m;m=i;i=k;k=l+b|0;l=j;j=h;h=g;g=b+(h&j^l&(h^j))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+j|0;e[3]=e[3]+l|0;e[4]=e[4]+k|0;e[5]=e[5]+i|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}}; -sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,j=h.bitLength(c)/8,l=h.bitLength(g)/8;e=e||64;d=d||[];if(j<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(f=2;f<4&&l>>>8*f;f++);if(f<15-j)f=15-j;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.G(a,b,c,d,e,f);g=sjcl.mode.ccm.I(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),j=f.clamp(b,h-e),l=f.bitSlice(b, -h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));j=sjcl.mode.ccm.I(a,j,c,l,e,b);a=sjcl.mode.ccm.G(a,j.data,c,d,e,b);if(!f.equal(j.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return j.data},G:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,j=h.k;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); -f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.M=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.l=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b>>7^b>>>18^ +b>>>3^b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0}b=b+n+(j>>>6^j>>>11^j>>>25^j<<26^j<<21^j<<7)+(m^j&(l^m))+f[a];n=m;m=l;l=j;j=k+b|0;k=i;i=h;h=g;g=b+(h&i^k&(h^i))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0}e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+i|0;e[3]=e[3]+k|0;e[4]=e[4]+j|0;e[5]=e[5]+l|0;e[6]=e[6]+m|0;e[7]=e[7]+n|0}}; +sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,i=h.bitLength(c)/8,k=h.bitLength(g)/8;e=e||64;d=d||[];if(i<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(f=2;f<4&&k>>>8*f;f++);if(f<15-i)f=15-i;c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.G(a,b,c,d,e,f);g=sjcl.mode.ccm.I(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),i=f.clamp(b,h-e),k=f.bitSlice(b, +h-e);h=(h-e)/8;if(g<7)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(b=2;b<4&&h>>>8*b;b++);if(b<15-g)b=15-g;c=f.clamp(c,8*(15-b));i=sjcl.mode.ccm.I(a,i,c,k,e,b);a=sjcl.mode.ccm.G(a,i.data,c,d,e,b);if(!f.equal(i.tag,a))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return i.data},G:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,i=h.k;e/=8;if(e%2||e<4||e>16)throw new sjcl.exception.invalid("ccm: invalid tag length");if(d.length>0xffffffff||b.length>0xffffffff)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"); +f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;if(c<=65279)g=[h.partial(16,c)];else if(c<=0xffffffff)g=h.concat([h.partial(16,65534)],[c]);g=h.concat(g,d);for(d=0;d>>31,a[1]<<1^a[2]>>>31,a[2]<<1^a[3]>>>31,a[3]<<1^(a[0]>>>31)*135]}};sjcl.misc.hmac=function(a,b){this.M=b=b||sjcl.hash.sha256;var c=[[],[]],d=b.prototype.blockSize/32;this.l=[new b,new b];if(a.length>d)a=b.hash(a);for(b=0;b0;){b++;e>>>=1}this.b[g].update([d,this.J++,2,b,f,a.length].concat(a));break;case "string":if(b===undefined)b=a.length;this.b[g].update([d,this.J++,3,b,f,a.length]);this.b[g].update(a);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string");}this.j[g]+=b;this.f+=b;if(h===0){this.isReady()!==0&&this.K("seeded",Math.max(this.g, this.f));this.K("progress",this.getProgress())}},isReady:function(a){a=this.B[a!==undefined?a:this.t];return this.g&&this.g>=a?this.j[0]>80&&(new Date).valueOf()>this.O?3:1:this.f>=a?2:0},getProgress:function(a){a=this.B[a?a:this.t];return this.g>=a?1["0"]:this.f>a?1["0"]:this.f/a},startCollectors:function(){if(!this.m){if(window.addEventListener){window.addEventListener("load",this.o,false);window.addEventListener("mousemove",this.p,false)}else if(document.attachEvent){document.attachEvent("onload", @@ -32,9 +32,9 @@ var d=[];for(c in a)a.hasOwnProperty(c)&&a[c]===b&&d.push(c);for(b=0;b=1<this.g)this.g=c;this.z++;this.T(b)},p:function(a){sjcl.random.addEntropy([a.x|| a.clientX||a.offsetX,a.y||a.clientY||a.offsetY],2,"mouse")},o:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},K:function(a,b){var c;a=sjcl.random.r[a];var d=[];for(c in a)a.hasOwnProperty(c)&&d.push(a[c]);for(c=0;c -4)throw new sjcl.exception.invalid("json encrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,f);a=c.key.slice(0,f.ks/32);f.salt=c.salt}if(typeof b==="string")b=sjcl.codec.utf8String.toBits(b);c=new sjcl.cipher[f.cipher](a);e.c(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(c,b,f.iv,f.adata,f.tag);return e.encode(f)},decrypt:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.c(e.c(e.c({},e.defaults),e.decode(b)),c,true);if(typeof b.salt==="string")b.salt=sjcl.codec.base64.toBits(b.salt); -if(typeof b.iv==="string")b.iv=sjcl.codec.base64.toBits(b.iv);if(!sjcl.mode[b.mode]||!sjcl.cipher[b.cipher]||typeof a==="string"&&b.iter<=100||b.ts!==64&&b.ts!==96&&b.ts!==128||b.ks!==128&&b.ks!==192&&b.ks!==0x100||!b.iv||b.iv.length<2||b.iv.length>4)throw new sjcl.exception.invalid("json decrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,b);a=c.key.slice(0,b.ks/32);b.salt=c.salt}c=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(c,b.ct,b.iv,b.adata,b.tag);e.c(d, -b);d.key=a;return sjcl.codec.utf8String.fromBits(c)},encode:function(a){var b,c="{",d="";for(b in a)if(a.hasOwnProperty(b)){if(!b.match(/^[a-z0-9]+$/i))throw new sjcl.exception.invalid("json encode: invalid property name");c+=d+'"'+b+'":';d=",";switch(typeof a[b]){case "number":case "boolean":c+=a[b];break;case "string":c+='"'+escape(a[b])+'"';break;case "object":c+='"'+sjcl.codec.base64.fromBits(a[b],1)+'"';break;default:throw new sjcl.exception.bug("json encode: unsupported type");}}return c+"}"}, -decode:function(a){a=a.replace(/\s/g,"");if(!a.match(/^\{.*\}$/))throw new sjcl.exception.invalid("json decode: this isn't json!");a=a.replace(/^\{|\}$/g,"").split(/,/);var b={},c,d;for(c=0;c4)throw new sjcl.exception.invalid("json decrypt: invalid parameters");if(typeof a==="string"){c=sjcl.misc.cachedPbkdf2(a,b);a=c.key.slice(0,b.ks/32);b.salt=c.salt}c=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(c, +b.ct,b.iv,b.adata,b.tag);e.c(d,b);d.key=a;return sjcl.codec.utf8String.fromBits(c)},encode:function(a){var b,c="{",d="";for(b in a)if(a.hasOwnProperty(b)){if(!b.match(/^[a-z0-9]+$/i))throw new sjcl.exception.invalid("json encode: invalid property name");c+=d+b+":";d=",";switch(typeof a[b]){case "number":case "boolean":c+=a[b];break;case "string":c+='"'+escape(a[b])+'"';break;case "object":c+='"'+sjcl.codec.base64.fromBits(a[b],1)+'"';break;default:throw new sjcl.exception.bug("json encode: unsupported type"); +}}return c+"}"},decode:function(a){a=a.replace(/\s/g,"");if(!a.match(/^\{.*\}$/))throw new sjcl.exception.invalid("json decode: this isn't json!");a=a.replace(/^\{|\}$/g,"").split(/,/);var b={},c,d;for(c=0;c