diff options
author | Azul <azul@leap.se> | 2012-07-20 11:12:22 +0200 |
---|---|---|
committer | Azul <azul@leap.se> | 2012-07-20 11:12:22 +0200 |
commit | 057ff90cae59196d61dcdb46babb8d228dedcf97 (patch) | |
tree | 396428d2591881381adb21cd34d8beb9a6cba950 /src | |
parent | 50de80c5e817476ac95a096c718a66f5555fcd05 (diff) |
added an index file to use with sprockets
Diffstat (limited to 'src')
-rw-r--r-- | src/MD5.js | 207 | ||||
-rw-r--r-- | src/SHA1.js | 174 | ||||
-rw-r--r-- | src/SHA256.js | 127 | ||||
-rw-r--r-- | src/aes.js | 771 | ||||
-rw-r--r-- | src/cryptoHelpers.js | 185 | ||||
-rw-r--r-- | src/jsbn.js | 586 | ||||
-rw-r--r-- | src/jsbn2.js | 672 | ||||
-rw-r--r-- | src/prng4.js | 76 | ||||
-rw-r--r-- | src/rng.js | 99 | ||||
-rw-r--r-- | src/srp.js | 386 | ||||
-rw-r--r-- | src/srp_register.js | 46 |
11 files changed, 0 insertions, 3329 deletions
diff --git a/src/MD5.js b/src/MD5.js deleted file mode 100644 index 55cb8cc..0000000 --- a/src/MD5.js +++ /dev/null @@ -1,207 +0,0 @@ -/** -* -* MD5 (Message-Digest Algorithm) -* http://www.webtoolkit.info/ -* -**/ - -var MD5 = function (string) { - - function RotateLeft(lValue, iShiftBits) { - return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits)); - } - - function AddUnsigned(lX,lY) { - var lX4,lY4,lX8,lY8,lResult; - lX8 = (lX & 0x80000000); - lY8 = (lY & 0x80000000); - lX4 = (lX & 0x40000000); - lY4 = (lY & 0x40000000); - lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF); - if (lX4 & lY4) { - return (lResult ^ 0x80000000 ^ lX8 ^ lY8); - } - if (lX4 | lY4) { - if (lResult & 0x40000000) { - return (lResult ^ 0xC0000000 ^ lX8 ^ lY8); - } else { - return (lResult ^ 0x40000000 ^ lX8 ^ lY8); - } - } else { - return (lResult ^ lX8 ^ lY8); - } - } - - function F(x,y,z) { return (x & y) | ((~x) & z); } - function G(x,y,z) { return (x & z) | (y & (~z)); } - function H(x,y,z) { return (x ^ y ^ z); } - function I(x,y,z) { return (y ^ (x | (~z))); } - - function FF(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function GG(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function HH(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function II(a,b,c,d,x,s,ac) { - a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac)); - return AddUnsigned(RotateLeft(a, s), b); - }; - - function ConvertToWordArray(string) { - var lWordCount; - var lMessageLength = string.length; - var lNumberOfWords_temp1=lMessageLength + 8; - var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64; - var lNumberOfWords = (lNumberOfWords_temp2+1)*16; - var lWordArray=Array(lNumberOfWords-1); - var lBytePosition = 0; - var lByteCount = 0; - while ( lByteCount < lMessageLength ) { - lWordCount = (lByteCount-(lByteCount % 4))/4; - lBytePosition = (lByteCount % 4)*8; - lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<<lBytePosition)); - lByteCount++; - } - lWordCount = (lByteCount-(lByteCount % 4))/4; - lBytePosition = (lByteCount % 4)*8; - lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition); - lWordArray[lNumberOfWords-2] = lMessageLength<<3; - lWordArray[lNumberOfWords-1] = lMessageLength>>>29; - return lWordArray; - }; - - function WordToHex(lValue) { - var WordToHexValue="",WordToHexValue_temp="",lByte,lCount; - for (lCount = 0;lCount<=3;lCount++) { - lByte = (lValue>>>(lCount*8)) & 255; - WordToHexValue_temp = "0" + lByte.toString(16); - WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2); - } - return WordToHexValue; - }; - - function Utf8Encode(string) { - string = string.replace(/\r\n/g,"\n"); - var utftext = ""; - - for (var n = 0; n < string.length; n++) { - - var c = string.charCodeAt(n); - - if (c < 128) { - utftext += String.fromCharCode(c); - } - else if((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 192); - utftext += String.fromCharCode((c & 63) | 128); - } - else { - utftext += String.fromCharCode((c >> 12) | 224); - utftext += String.fromCharCode(((c >> 6) & 63) | 128); - utftext += String.fromCharCode((c & 63) | 128); - } - - } - - return utftext; - }; - - var x=Array(); - var k,AA,BB,CC,DD,a,b,c,d; - var S11=7, S12=12, S13=17, S14=22; - var S21=5, S22=9 , S23=14, S24=20; - var S31=4, S32=11, S33=16, S34=23; - var S41=6, S42=10, S43=15, S44=21; - - string = Utf8Encode(string); - - x = ConvertToWordArray(string); - - a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476; - - for (k=0;k<x.length;k+=16) { - AA=a; BB=b; CC=c; DD=d; - a=FF(a,b,c,d,x[k+0], S11,0xD76AA478); - d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756); - c=FF(c,d,a,b,x[k+2], S13,0x242070DB); - b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE); - a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF); - d=FF(d,a,b,c,x[k+5], S12,0x4787C62A); - c=FF(c,d,a,b,x[k+6], S13,0xA8304613); - b=FF(b,c,d,a,x[k+7], S14,0xFD469501); - a=FF(a,b,c,d,x[k+8], S11,0x698098D8); - d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF); - c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1); - b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE); - a=FF(a,b,c,d,x[k+12],S11,0x6B901122); - d=FF(d,a,b,c,x[k+13],S12,0xFD987193); - c=FF(c,d,a,b,x[k+14],S13,0xA679438E); - b=FF(b,c,d,a,x[k+15],S14,0x49B40821); - a=GG(a,b,c,d,x[k+1], S21,0xF61E2562); - d=GG(d,a,b,c,x[k+6], S22,0xC040B340); - c=GG(c,d,a,b,x[k+11],S23,0x265E5A51); - b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA); - a=GG(a,b,c,d,x[k+5], S21,0xD62F105D); - d=GG(d,a,b,c,x[k+10],S22,0x2441453); - c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681); - b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8); - a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6); - d=GG(d,a,b,c,x[k+14],S22,0xC33707D6); - c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87); - b=GG(b,c,d,a,x[k+8], S24,0x455A14ED); - a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905); - d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8); - c=GG(c,d,a,b,x[k+7], S23,0x676F02D9); - b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A); - a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942); - d=HH(d,a,b,c,x[k+8], S32,0x8771F681); - c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122); - b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C); - a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44); - d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9); - c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60); - b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70); - a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6); - d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA); - c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085); - b=HH(b,c,d,a,x[k+6], S34,0x4881D05); - a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039); - d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5); - c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8); - b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665); - a=II(a,b,c,d,x[k+0], S41,0xF4292244); - d=II(d,a,b,c,x[k+7], S42,0x432AFF97); - c=II(c,d,a,b,x[k+14],S43,0xAB9423A7); - b=II(b,c,d,a,x[k+5], S44,0xFC93A039); - a=II(a,b,c,d,x[k+12],S41,0x655B59C3); - d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92); - c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D); - b=II(b,c,d,a,x[k+1], S44,0x85845DD1); - a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F); - d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0); - c=II(c,d,a,b,x[k+6], S43,0xA3014314); - b=II(b,c,d,a,x[k+13],S44,0x4E0811A1); - a=II(a,b,c,d,x[k+4], S41,0xF7537E82); - d=II(d,a,b,c,x[k+11],S42,0xBD3AF235); - c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB); - b=II(b,c,d,a,x[k+9], S44,0xEB86D391); - a=AddUnsigned(a,AA); - b=AddUnsigned(b,BB); - c=AddUnsigned(c,CC); - d=AddUnsigned(d,DD); - } - - var temp = WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d); - - return temp.toLowerCase(); -} diff --git a/src/SHA1.js b/src/SHA1.js deleted file mode 100644 index e772cbe..0000000 --- a/src/SHA1.js +++ /dev/null @@ -1,174 +0,0 @@ -/** -* -* Secure Hash Algorithm (SHA1) -* http://www.webtoolkit.info/ -* -**/ - -function SHA1 (msg) { - - function rotate_left(n,s) { - var t4 = ( n<<s ) | (n>>>(32-s)); - return t4; - }; - - function lsb_hex(val) { - var str=""; - var i; - var vh; - var vl; - - for( i=0; i<=6; i+=2 ) { - vh = (val>>>(i*4+4))&0x0f; - vl = (val>>>(i*4))&0x0f; - str += vh.toString(16) + vl.toString(16); - } - return str; - }; - - function cvt_hex(val) { - var str=""; - var i; - var v; - - for( i=7; i>=0; i-- ) { - v = (val>>>(i*4))&0x0f; - str += v.toString(16); - } - return str; - }; - - - function Utf8Encode(string) { - string = string.replace(/\r\n/g,"\n"); - var utftext = ""; - - for (var n = 0; n < string.length; n++) { - - var c = string.charCodeAt(n); - - if (c < 128) { - utftext += String.fromCharCode(c); - } - else if((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 192); - utftext += String.fromCharCode((c & 63) | 128); - } - else { - utftext += String.fromCharCode((c >> 12) | 224); - utftext += String.fromCharCode(((c >> 6) & 63) | 128); - utftext += String.fromCharCode((c & 63) | 128); - } - - } - - return utftext; - }; - - var blockstart; - var i, j; - var W = new Array(80); - var H0 = 0x67452301; - var H1 = 0xEFCDAB89; - var H2 = 0x98BADCFE; - var H3 = 0x10325476; - var H4 = 0xC3D2E1F0; - var A, B, C, D, E; - var temp; - - msg = Utf8Encode(msg); - - var msg_len = msg.length; - - var word_array = new Array(); - for( i=0; i<msg_len-3; i+=4 ) { - j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 | - msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3); - word_array.push( j ); - } - - switch( msg_len % 4 ) { - case 0: - i = 0x080000000; - break; - case 1: - i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000; - break; - - case 2: - i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000; - break; - - case 3: - i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8 | 0x80; - break; - } - - word_array.push( i ); - - while( (word_array.length % 16) != 14 ) word_array.push( 0 ); - - word_array.push( msg_len>>>29 ); - word_array.push( (msg_len<<3)&0x0ffffffff ); - - - for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) { - - for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i]; - for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); - - A = H0; - B = H1; - C = H2; - D = H3; - E = H4; - - for( i= 0; i<=19; i++ ) { - temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff; - E = D; - D = C; - C = rotate_left(B,30); - B = A; - A = temp; - } - - for( i=20; i<=39; i++ ) { - temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff; - E = D; - D = C; - C = rotate_left(B,30); - B = A; - A = temp; - } - - for( i=40; i<=59; i++ ) { - temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff; - E = D; - D = C; - C = rotate_left(B,30); - B = A; - A = temp; - } - - for( i=60; i<=79; i++ ) { - temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff; - E = D; - D = C; - C = rotate_left(B,30); - B = A; - A = temp; - } - - H0 = (H0 + A) & 0x0ffffffff; - H1 = (H1 + B) & 0x0ffffffff; - H2 = (H2 + C) & 0x0ffffffff; - H3 = (H3 + D) & 0x0ffffffff; - H4 = (H4 + E) & 0x0ffffffff; - - } - - var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4); - - return temp.toLowerCase(); - -} diff --git a/src/SHA256.js b/src/SHA256.js deleted file mode 100644 index 1a852c0..0000000 --- a/src/SHA256.js +++ /dev/null @@ -1,127 +0,0 @@ -/** -* -* Secure Hash Algorithm (SHA256) -* http://www.webtoolkit.info/ -* -* Original code by Angel Marin, Paul Johnston. -* -**/ - -function SHA256(s){ - - var chrsz = 8; - var hexcase = 0; - - function safe_add (x, y) { - var lsw = (x & 0xFFFF) + (y & 0xFFFF); - var msw = (x >> 16) + (y >> 16) + (lsw >> 16); - return (msw << 16) | (lsw & 0xFFFF); - } - - function S (X, n) { return ( X >>> n ) | (X << (32 - n)); } - function R (X, n) { return ( X >>> n ); } - function Ch(x, y, z) { return ((x & y) ^ ((~x) & z)); } - function Maj(x, y, z) { return ((x & y) ^ (x & z) ^ (y & z)); } - function Sigma0256(x) { return (S(x, 2) ^ S(x, 13) ^ S(x, 22)); } - function Sigma1256(x) { return (S(x, 6) ^ S(x, 11) ^ S(x, 25)); } - function Gamma0256(x) { return (S(x, 7) ^ S(x, 18) ^ R(x, 3)); } - function Gamma1256(x) { return (S(x, 17) ^ S(x, 19) ^ R(x, 10)); } - - function core_sha256 (m, l) { - var K = new Array(0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2); - var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); - var W = new Array(64); - var a, b, c, d, e, f, g, h, i, j; - var T1, T2; - - m[l >> 5] |= 0x80 << (24 - l % 32); - m[((l + 64 >> 9) << 4) + 15] = l; - - for ( var i = 0; i<m.length; i+=16 ) { - a = HASH[0]; - b = HASH[1]; - c = HASH[2]; - d = HASH[3]; - e = HASH[4]; - f = HASH[5]; - g = HASH[6]; - h = HASH[7]; - - for ( var j = 0; j<64; j++) { - if (j < 16) W[j] = m[j + i]; - else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]); - - T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]); - T2 = safe_add(Sigma0256(a), Maj(a, b, c)); - - h = g; - g = f; - f = e; - e = safe_add(d, T1); - d = c; - c = b; - b = a; - a = safe_add(T1, T2); - } - - HASH[0] = safe_add(a, HASH[0]); - HASH[1] = safe_add(b, HASH[1]); - HASH[2] = safe_add(c, HASH[2]); - HASH[3] = safe_add(d, HASH[3]); - HASH[4] = safe_add(e, HASH[4]); - HASH[5] = safe_add(f, HASH[5]); - HASH[6] = safe_add(g, HASH[6]); - HASH[7] = safe_add(h, HASH[7]); - } - return HASH; - } - - function str2binb (str) { - var bin = Array(); - var mask = (1 << chrsz) - 1; - for(var i = 0; i < str.length * chrsz; i += chrsz) { - bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32); - } - return bin; - } - - function Utf8Encode(string) { - string = string.replace(/\r\n/g,"\n"); - var utftext = ""; - - for (var n = 0; n < string.length; n++) { - - var c = string.charCodeAt(n); - - if (c < 128) { - utftext += String.fromCharCode(c); - } - else if((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 192); - utftext += String.fromCharCode((c & 63) | 128); - } - else { - utftext += String.fromCharCode((c >> 12) | 224); - utftext += String.fromCharCode(((c >> 6) & 63) | 128); - utftext += String.fromCharCode((c & 63) | 128); - } - - } - - return utftext; - } - - function binb2hex (binarray) { - var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; - var str = ""; - for(var i = 0; i < binarray.length * 4; i++) { - str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + - hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); - } - return str; - } - - s = Utf8Encode(s); - return binb2hex(core_sha256(str2binb(s), s.length * chrsz)); - -} diff --git a/src/aes.js b/src/aes.js deleted file mode 100644 index 5682618..0000000 --- a/src/aes.js +++ /dev/null @@ -1,771 +0,0 @@ -/* - * aes.js: implements AES - Advanced Encryption Standard - * from the SlowAES project, http://code.google.com/p/slowaes/ - * - * Copyright (c) 2008 Josh Davis ( http://www.josh-davis.org ), - * Mark Percival ( http://mpercival.com ), - * - * Ported from C code written by Laurent Haan ( http://www.progressive-coding.com ) - * - * Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/ - */ - -var slowAES = { - /* - * START AES SECTION - */ - aes:{ - // structure of valid key sizes - keySize:{ - SIZE_128:16, - SIZE_192:24, - SIZE_256:32 - }, - - // Rijndael S-box - sbox:[ - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 ], - - // Rijndael Inverted S-box - rsbox: - [ 0x52, 0x09, 0x6a, 0x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0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4 - , 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f - , 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef - , 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61 - , 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d ], - - /* rotate the word eight bits to the left */ - rotate:function(word) - { - var c = word[0]; - for (var i = 0; i < 3; i++) - word[i] = word[i+1]; - word[3] = c; - - return word; - }, - - // Rijndael Rcon - Rcon:[ - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, - 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, - 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, - 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, - 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, - 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, - 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, - 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, - 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, - 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, - 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, - 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, - 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, - 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, - 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, - 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, - 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, - 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, - 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, - 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb ], - - G2X: [ - 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, - 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, - 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x40, 0x42, 0x44, 0x46, - 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, - 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, - 0x78, 0x7a, 0x7c, 0x7e, 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, - 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, 0xa0, 0xa2, 0xa4, 0xa6, - 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, - 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, - 0xd8, 0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, - 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, 0x1b, 0x19, 0x1f, 0x1d, - 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, - 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, - 0x23, 0x21, 0x27, 0x25, 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, - 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, 0x7b, 0x79, 0x7f, 0x7d, - 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, - 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f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0x8d, 0x84, 0x87, 0x82, 0x81, 0x9b, 0x98, 0x9d, 0x9e, - 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, - 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, - 0xbf, 0xbc, 0xb9, 0xba, 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, - 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, 0xcb, 0xc8, 0xcd, 0xce, - 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, - 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, - 0x4f, 0x4c, 0x49, 0x4a, 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, - 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, 0x3b, 0x38, 0x3d, 0x3e, - 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, - 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, - 0x1f, 0x1c, 0x19, 0x1a - ], - - G9X: [ - 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, - 0x6c, 0x65, 0x7e, 0x77, 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, - 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, 0x3b, 0x32, 0x29, 0x20, - 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, - 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x1f, 0x14, 0x09, 0x02, - 0x33, 0x38, 0x25, 0x2e, 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, - 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, 0x3c, 0x37, 0x2a, 0x21, - 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, - 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, - 0x75, 0x7e, 0x63, 0x68, 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, - 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, 0x7a, 0x71, 0x6c, 0x67, - 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, - 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, - 0xbe, 0xb5, 0xa8, 0xa3 - ], - - GDX: [ - 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, - 0x5c, 0x51, 0x46, 0x4b, 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, - 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, 0xbb, 0xb6, 0xa1, 0xac, - 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, - 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, - 0x37, 0x3a, 0x2d, 0x20, 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, - 0x05, 0x08, 0x1f, 0x12, 0x31, 0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0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, - 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, - 0xa4, 0xaa, 0xb8, 0xb6, 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, - 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, 0x37, 0x39, 0x2b, 0x25, - 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, - 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, - 0x9f, 0x91, 0x83, 0x8d - ], - - // Key Schedule Core - core:function(word,iteration) - { - /* rotate the 32-bit word 8 bits to the left */ - word = this.rotate(word); - /* apply S-Box substitution on all 4 parts of the 32-bit word */ - for (var i = 0; i < 4; ++i) - word[i] = this.sbox[word[i]]; - /* XOR the output of the rcon operation with i to the first part (leftmost) only */ - word[0] = word[0]^this.Rcon[iteration]; - return word; - }, - - /* Rijndael's key expansion - * expands an 128,192,256 key into an 176,208,240 bytes key - * - * expandedKey is a pointer to an char array of large enough size - * key is a pointer to a non-expanded key - */ - expandKey:function(key,size) - { - var expandedKeySize = (16*(this.numberOfRounds(size)+1)); - - /* current expanded keySize, in bytes */ - var currentSize = 0; - var rconIteration = 1; - var t = []; // temporary 4-byte variable - - var expandedKey = []; - for(var i = 0;i < expandedKeySize;i++) - expandedKey[i] = 0; - - /* set the 16,24,32 bytes of the expanded key to the input key */ - for (var j = 0; j < size; j++) - expandedKey[j] = key[j]; - currentSize += size; - - while (currentSize < expandedKeySize) - { - /* assign the previous 4 bytes to the temporary value t */ - for (var k = 0; k < 4; k++) - t[k] = expandedKey[(currentSize - 4) + k]; - - /* every 16,24,32 bytes we apply the core schedule to t - * and increment rconIteration afterwards - */ - if(currentSize % size == 0) - t = this.core(t, rconIteration++); - - /* For 256-bit keys, we add an extra sbox to the calculation */ - if(size == this.keySize.SIZE_256 && ((currentSize % size) == 16)) - for(var l = 0; l < 4; l++) - t[l] = this.sbox[t[l]]; - - /* We XOR t with the four-byte block 16,24,32 bytes before the new expanded key. - * This becomes the next four bytes in the expanded key. - */ - for(var m = 0; m < 4; m++) { - expandedKey[currentSize] = expandedKey[currentSize - size] ^ t[m]; - currentSize++; - } - } - return expandedKey; - }, - - // Adds (XORs) the round key to the state - addRoundKey:function(state,roundKey) - { - for (var i = 0; i < 16; i++) - state[i] ^= roundKey[i]; - return state; - }, - - // Creates a round key from the given expanded key and the - // position within the expanded key. - createRoundKey:function(expandedKey,roundKeyPointer) - { - var roundKey = []; - for (var i = 0; i < 4; i++) - for (var j = 0; j < 4; j++) - roundKey[j*4+i] = expandedKey[roundKeyPointer + i*4 + j]; - return roundKey; - }, - - /* substitute all the values from the state with the value in the SBox - * using the state value as index for the SBox - */ - subBytes:function(state,isInv) - { - for (var i = 0; i < 16; i++) - state[i] = isInv?this.rsbox[state[i]]:this.sbox[state[i]]; - return state; - }, - - /* iterate over the 4 rows and call shiftRow() with that row */ - shiftRows:function(state,isInv) - { - for (var i = 0; i < 4; i++) - state = this.shiftRow(state,i*4, i,isInv); - return state; - }, - - /* each iteration shifts the row to the left by 1 */ - shiftRow:function(state,statePointer,nbr,isInv) - { - for (var i = 0; i < nbr; i++) - { - if(isInv) - { - var tmp = state[statePointer + 3]; - for (var j = 3; j > 0; j--) - state[statePointer + j] = state[statePointer + j-1]; - state[statePointer] = tmp; - } - else - { - var tmp = state[statePointer]; - for (var j = 0; j < 3; j++) - state[statePointer + j] = state[statePointer + j+1]; - state[statePointer + 3] = tmp; - } - } - return state; - }, - - // galois multiplication of 8 bit characters a and b - galois_multiplication:function(a,b) - { - var p = 0; - for(var counter = 0; counter < 8; counter++) - { - if((b & 1) == 1) - p ^= a; - if(p > 0x100) p ^= 0x100; - var hi_bit_set = (a & 0x80); //keep p 8 bit - a <<= 1; - if(a > 0x100) a ^= 0x100; //keep a 8 bit - if(hi_bit_set == 0x80) - a ^= 0x1b; - if(a > 0x100) a ^= 0x100; //keep a 8 bit - b >>= 1; - if(b > 0x100) b ^= 0x100; //keep b 8 bit - } - return p; - }, - - // galois multipication of the 4x4 matrix - mixColumns:function(state,isInv) - { - var column = []; - /* iterate over the 4 columns */ - for (var i = 0; i < 4; i++) - { - /* construct one column by iterating over the 4 rows */ - for (var j = 0; j < 4; j++) - column[j] = state[(j*4)+i]; - /* apply the mixColumn on one column */ - column = this.mixColumn(column,isInv); - /* put the values back into the state */ - for (var k = 0; k < 4; k++) - state[(k*4)+i] = column[k]; - } - return state; - }, - - // galois multipication of 1 column of the 4x4 matrix - mixColumn:function(column,isInv) - { - var mult = []; - if(isInv) - mult = [14,9,13,11]; - else - mult = [2,1,1,3]; - var cpy = []; - for(var i = 0; i < 4; i++) - cpy[i] = column[i]; - - column[0] = this.galois_multiplication(cpy[0],mult[0]) ^ - this.galois_multiplication(cpy[3],mult[1]) ^ - this.galois_multiplication(cpy[2],mult[2]) ^ - this.galois_multiplication(cpy[1],mult[3]); - column[1] = this.galois_multiplication(cpy[1],mult[0]) ^ - this.galois_multiplication(cpy[0],mult[1]) ^ - this.galois_multiplication(cpy[3],mult[2]) ^ - this.galois_multiplication(cpy[2],mult[3]); - column[2] = this.galois_multiplication(cpy[2],mult[0]) ^ - this.galois_multiplication(cpy[1],mult[1]) ^ - this.galois_multiplication(cpy[0],mult[2]) ^ - this.galois_multiplication(cpy[3],mult[3]); - column[3] = this.galois_multiplication(cpy[3],mult[0]) ^ - this.galois_multiplication(cpy[2],mult[1]) ^ - this.galois_multiplication(cpy[1],mult[2]) ^ - this.galois_multiplication(cpy[0],mult[3]); - return column; - }, - - // applies the 4 operations of the forward round in sequence - round:function(state, roundKey) - { - state = this.subBytes(state,false); - state = this.shiftRows(state,false); - state = this.mixColumns(state,false); - state = this.addRoundKey(state, roundKey); - return state; - }, - - // applies the 4 operations of the inverse round in sequence - invRound:function(state,roundKey) - { - state = this.shiftRows(state,true); - state = this.subBytes(state,true); - state = this.addRoundKey(state, roundKey); - state = this.mixColumns(state,true); - return state; - }, - - /* - * Perform the initial operations, the standard round, and the final operations - * of the forward aes, creating a round key for each round - */ - main:function(state,expandedKey,nbrRounds) - { - state = this.addRoundKey(state, this.createRoundKey(expandedKey,0)); - for (var i = 1; i < nbrRounds; i++) - state = this.round(state, this.createRoundKey(expandedKey,16*i)); - state = this.subBytes(state,false); - state = this.shiftRows(state,false); - state = this.addRoundKey(state, this.createRoundKey(expandedKey,16*nbrRounds)); - return state; - }, - - /* - * Perform the initial operations, the standard round, and the final operations - * of the inverse aes, creating a round key for each round - */ - invMain:function(state, expandedKey, nbrRounds) - { - state = this.addRoundKey(state, this.createRoundKey(expandedKey,16*nbrRounds)); - for (var i = nbrRounds-1; i > 0; i--) - state = this.invRound(state, this.createRoundKey(expandedKey,16*i)); - state = this.shiftRows(state,true); - state = this.subBytes(state,true); - state = this.addRoundKey(state, this.createRoundKey(expandedKey,0)); - return state; - }, - - numberOfRounds:function(size) - { - var nbrRounds; - switch (size) /* set the number of rounds */ - { - case this.keySize.SIZE_128: - nbrRounds = 10; - break; - case this.keySize.SIZE_192: - nbrRounds = 12; - break; - case this.keySize.SIZE_256: - nbrRounds = 14; - break; - default: - return null; - break; - } - return nbrRounds; - }, - - // encrypts a 128 bit input block against the given key of size specified - encrypt:function(input,key,size) - { - var output = []; - var block = []; /* the 128 bit block to encode */ - var nbrRounds = this.numberOfRounds(size); - /* Set the block values, for the block: - * a0,0 a0,1 a0,2 a0,3 - * a1,0 a1,1 a1,2 a1,3 - * a2,0 a2,1 a2,2 a2,3 - * a3,0 a3,1 a3,2 a3,3 - * the mapping order is a0,0 a1,0 a2,0 a3,0 a0,1 a1,1 ... a2,3 a3,3 - */ - for (var i = 0; i < 4; i++) /* iterate over the columns */ - for (var j = 0; j < 4; j++) /* iterate over the rows */ - block[(i+(j*4))] = input[(i*4)+j]; - - /* expand the key into an 176, 208, 240 bytes key */ - var expandedKey = this.expandKey(key, size); /* the expanded key */ - /* encrypt the block using the expandedKey */ - block = this.main(block, expandedKey, nbrRounds); - for (var k = 0; k < 4; k++) /* unmap the block again into the output */ - for (var l = 0; l < 4; l++) /* iterate over the rows */ - output[(k*4)+l] = block[(k+(l*4))]; - return output; - }, - - // decrypts a 128 bit input block against the given key of size specified - decrypt:function(input, key, size) - { - var output = []; - var block = []; /* the 128 bit block to decode */ - var nbrRounds = this.numberOfRounds(size); - /* Set the block values, for the block: - * a0,0 a0,1 a0,2 a0,3 - * a1,0 a1,1 a1,2 a1,3 - * a2,0 a2,1 a2,2 a2,3 - * a3,0 a3,1 a3,2 a3,3 - * the mapping order is a0,0 a1,0 a2,0 a3,0 a0,1 a1,1 ... a2,3 a3,3 - */ - for (var i = 0; i < 4; i++) /* iterate over the columns */ - for (var j = 0; j < 4; j++) /* iterate over the rows */ - block[(i+(j*4))] = input[(i*4)+j]; - /* expand the key into an 176, 208, 240 bytes key */ - var expandedKey = this.expandKey(key, size); - /* decrypt the block using the expandedKey */ - block = this.invMain(block, expandedKey, nbrRounds); - for (var k = 0; k < 4; k++)/* unmap the block again into the output */ - for (var l = 0; l < 4; l++)/* iterate over the rows */ - output[(k*4)+l] = block[(k+(l*4))]; - return output; - } - }, - /* - * END AES SECTION - */ - - /* - * START MODE OF OPERATION SECTION - */ - //structure of supported modes of operation - modeOfOperation:{ - OFB:0, - CFB:1, - CBC:2 - }, - - // gets a properly padded block - getPaddedBlock: function(bytesIn,start,end,mode) - { - if(end - start > 16) - end = start + 16; - - var array = bytesIn.slice(start, end); - - if (mode == this.modeOfOperation.CBC) - { - var cpad = 16 - array.length; - while(array.length < 16) - { - array.push(cpad); - } - } - - return array; - }, - - /* - * Mode of Operation Encryption - * bytesIn - Input String as array of bytes - * mode - mode of type modeOfOperation - * key - a number array of length 'size' - * size - the bit length of the key - * iv - the 128 bit number array Initialization Vector - */ - encrypt: function (bytesIn, mode, key, size, iv) - { - if(key.length%size) - { - throw 'Key length does not match specified size.'; - } - if(iv.length%16) - { - throw 'iv length must be 128 bits.'; - } - // the AES input/output - var byteArray = []; - var input = []; - var output = []; - var ciphertext = []; - var cipherOut = []; - // char firstRound - var firstRound = true; - if (bytesIn !== null) - { - for (var j = 0;j < Math.ceil(bytesIn.length/16); j++) - { - var start = j*16; - var end = j*16+16; - if(j*16+16 > bytesIn.length) - end = bytesIn.length; - byteArray = this.getPaddedBlock(bytesIn,start,end,mode); - if (mode == this.modeOfOperation.CFB) - { - if (firstRound) - { - output = this.aes.encrypt(iv, key, size); - firstRound = false; - } - else - output = this.aes.encrypt(input, key, size); - for (var i = 0; i < 16; i++) - ciphertext[i] = byteArray[i] ^ output[i]; - for(var k = 0;k < end-start;k++) - cipherOut.push(ciphertext[k]); - input = ciphertext; - } - else if (mode == this.modeOfOperation.OFB) - { - if (firstRound) - { - output = this.aes.encrypt(iv, key, size); - firstRound = false; - } - else - output = this.aes.encrypt(input, key, size); - for (var i = 0; i < 16; i++) - ciphertext[i] = byteArray[i] ^ output[i]; - for(var k = 0;k < end-start;k++) - cipherOut.push(ciphertext[k]); - input = output; - } - else if (mode == this.modeOfOperation.CBC) - { - for (var i = 0; i < 16; i++) - input[i] = byteArray[i] ^ ((firstRound) ? iv[i] : ciphertext[i]); - firstRound = false; - ciphertext = this.aes.encrypt(input, key, size); - // always 16 bytes because of the padding for CBC - for(var k = 0;k < 16;k++) - cipherOut.push(ciphertext[k]); - } - } - } - return {mode:mode,originalsize:bytesIn.length,cipher:cipherOut}; - }, - - /* - * Mode of Operation Decryption - * cipherIn - Encrypted String as array of bytes - * originalsize - The unencrypted string length - required for CBC - * mode - mode of type modeOfOperation - * key - a number array of length 'size' - * size - the bit length of the key - * iv - the 128 bit number array Initialization Vector - */ - decrypt:function(cipherIn,originalsize,mode,key,size,iv) - { - if(key.length%size) - { - throw 'Key length does not match specified size.'; - return null; - } - if(iv.length%16) - { - throw 'iv length must be 128 bits.'; - } - // the AES input/output - var ciphertext = []; - var input = []; - var output = []; - var byteArray = []; - var bytesOut = []; - // char firstRound - var firstRound = true; - if (cipherIn !== null) - { - for (var j = 0;j < Math.ceil(cipherIn.length/16); j++) - { - var start = j*16; - var end = j*16+16; - if(j*16+16 > cipherIn.length) - end = cipherIn.length; - ciphertext = this.getPaddedBlock(cipherIn,start,end,mode); - if (mode == this.modeOfOperation.CFB) - { - if (firstRound) - { - output = this.aes.encrypt(iv, key, size); - firstRound = false; - } - else - output = this.aes.encrypt(input, key, size); - for (i = 0; i < 16; i++) - byteArray[i] = output[i] ^ ciphertext[i]; - for(var k = 0;k < end-start;k++) - bytesOut.push(byteArray[k]); - input = ciphertext; - } - else if (mode == this.modeOfOperation.OFB) - { - if (firstRound) - { - output = this.aes.encrypt(iv, key, size); - firstRound = false; - } - else - output = this.aes.encrypt(input, key, size); - for (i = 0; i < 16; i++) - byteArray[i] = output[i] ^ ciphertext[i]; - for(var k = 0;k < end-start;k++) - bytesOut.push(byteArray[k]); - input = output; - } - else if(mode == this.modeOfOperation.CBC) - { - output = this.aes.decrypt(ciphertext, key, size); - for (i = 0; i < 16; i++) - byteArray[i] = ((firstRound) ? iv[i] : input[i]) ^ output[i]; - firstRound = false; - if (originalsize < end) - for(var k = 0;k < originalsize-start;k++) - bytesOut.push(byteArray[k]); - else - for(var k = 0;k < end-start;k++) - bytesOut.push(byteArray[k]); - input = ciphertext; - } - } - } - return bytesOut; - } - /* - * END MODE OF OPERATION SECTION - */ -}; - diff --git a/src/cryptoHelpers.js b/src/cryptoHelpers.js deleted file mode 100644 index a77ee42..0000000 --- a/src/cryptoHelpers.js +++ /dev/null @@ -1,185 +0,0 @@ -/* - * cryptoHelpers.js: implements AES - Advanced Encryption Standard - * from the SlowAES project, http://code.google.com/p/slowaes/ - * - * Copyright (c) 2008 Josh Davis ( http://www.josh-davis.org ), - * Mark Percival ( http://mpercival.com ), - * Johan Sundstrom ( http://ecmanaut.blogspot.com ), - * John Resig ( http://ejohn.org ) - * - * Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/ - */ - - - -var cryptoHelpers = { - - // encodes a unicode string to UTF8 (8 bit characters are critical to AES functioning properly) - encode_utf8:function(s) - { - try{return unescape(encodeURIComponent(s));} - catch(e){throw 'error during utf8 encoding: cryptoHelpers.encode_utf8.';} - }, - - - // decodes a UTF8 string back to unicode - decode_utf8:function(s) - { - try{return decodeURIComponent(escape(s));} - catch(e){throw('error during utf8 decoding: cryptoHelpers.decode_utf8.');} - }, - - //convert a number array to a hex string - toHex:function() - { - var array = []; - if(arguments.length == 1 && arguments[0].constructor == Array) - array = arguments[0]; - else - array = arguments; - var ret = ''; - for(var i = 0;i < array.length;i++) - ret += (array[i] < 16 ? '0' : '') + array[i].toString(16); - return ret.toLowerCase(); - }, - - //convert a hex string to a number array - toNumbers:function(s) - { - var ret = []; - s.replace(/(..)/g,function(s){ - ret.push(parseInt(s,16)); - }); - return ret; - }, - - // get a random number in the range [min,max] - getRandom:function(min,max) - { - if(min === null) - min = 0; - if(max === null) - max = 1; - return Math.floor(Math.random()*(max+1)) + min; - }, - - generateSharedKey:function(len) - { - if(len === null) - len = 16; - var key = []; - for(var i = 0; i < len*2; i++) - key.push(this.getRandom(0,255)); - return key; - }, - - generatePrivateKey:function(s,size) - { - var sha = jsHash.sha2.arr_sha256(s); - return sha.slice(0,size); - }, - - convertStringToByteArray: function(s) - { - var byteArray = []; - for(var i = 0;i < s.length;i++) - { - byteArray.push(s.charCodeAt(i)); - } - return byteArray; - }, - - convertByteArrayToString: function(byteArray) - { - var s = ''; - for(var i = 0;i < byteArray.length;i++) - { - s += String.fromCharCode(byteArray[i]); - } - return s; - }, - - base64: { - // Takes a Nx16x1 byte array and converts it to Base64 - - chars: [ - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '+', '/', - '=' // for decoding purposes - ], - - encode_line: function(flatArr){ - var b64 = ''; - - for (var i = 0; i < flatArr.length; i += 3){ - b64 += this.chars[flatArr[i] >> 2]; - b64 += this.chars[((flatArr[i] & 3) << 4) | (flatArr[i + 1] >> 4)]; - if (!(flatArr[i + 1] === null)){ - b64 += this.chars[((flatArr[i + 1] & 15) << 2) | (flatArr[i + 2] >> 6)]; - }else{ - b64 += '='; - } - if (!(flatArr[i + 2] === null)){ - b64 += this.chars[flatArr[i + 2] & 63]; - }else{ - b64 += '='; - } - } - return b64; - }, - - encode: function(flatArr) - { - var b64 = this.encode_line(flatArr); - // OpenSSL is super particular about line breaks - var broken_b64 = b64.slice(0, 64) + '\n'; - for (var i = 1; i < (Math.ceil(b64.length / 64)); i++) - { - broken_b64 += b64.slice(i * 64, i * 64 + 64) + (Math.ceil(b64.length / 64) == i + 1 ? '': '\n'); - } - return broken_b64; - }, - - decode: function(string) - { - string = string.replace(/[\r\n\t ]+/g, '') + '===='; // drop all whitespaces and pad with '=' (end of b64 marker) - var flatArr = []; - var c = []; - //var b = []; - for (var i = 0; true ; i = i + 4){ - c[0] = this.chars.indexOf(string.charAt(i)); - if(c[0] == 64){ - return flatArr; - } - c[1] = this.chars.indexOf(string.charAt(i + 1)); - c[2] = this.chars.indexOf(string.charAt(i + 2)); - c[3] = this.chars.indexOf(string.charAt(i + 3)); - - if( - (c[0] < 0) || // char1 is wrong - (c[1] < 0) || (c[1] == 64) || // char2 is wrong - (c[2] < 0) || // char3 is neither an valid char nor '=' - (c[3] < 0) // char4 is neither an valid char nor '=' - ){ - throw 'error during base64 decoding at pos '+i+': cryptoHelpers.base64.decode.'; - } - - flatArr.push((c[0] << 2) | (c[1] >> 4)); - if(c[2] >= 0 && c[2] < 64){ - flatArr.push(((c[1] & 15) << 4) | (c[2] >> 2)); - if(c[3] >= 0 && c[2] < 64){ - flatArr.push(((c[2] & 3) << 6) | c[3]); - } - } - } - } - } -}; - diff --git a/src/jsbn.js b/src/jsbn.js deleted file mode 100644 index f557d12..0000000 --- a/src/jsbn.js +++ /dev/null @@ -1,586 +0,0 @@ -/* - * Copyright (c) 2003-2005 Tom Wu - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, - * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY - * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, - * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER - * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF - * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * In addition, the following condition applies: - * - * All redistributions must retain an intact copy of this copyright notice - * and disclaimer. - */ - -// Basic JavaScript BN library - subset useful for RSA encryption. - -// Bits per digit -var dbits; - -// JavaScript engine analysis -var canary = 0xdeadbeefcafe; -var j_lm = ((canary&0xffffff)==0xefcafe); - -// (public) Constructor -function BigInteger(a,b,c) { - if(a != null) - if("number" == typeof a) this.fromNumber(a,b,c); - else if(b == null && "string" != typeof a) this.fromString(a,256); - else this.fromString(a,b); -} - -// return new, unset BigInteger -function nbi() { return new BigInteger(null); } - -// am: Compute w_j += (x*this_i), propagate carries, -// c is initial carry, returns final carry. -// c < 3*dvalue, x < 2*dvalue, this_i < dvalue -// We need to select the fastest one that works in this environment. - -// am1: use a single mult and divide to get the high bits, -// max digit bits should be 26 because -// max internal value = 2*dvalue^2-2*dvalue (< 2^53) -function am1(i,x,w,j,c,n) { - while(--n >= 0) { - var v = x*this[i++]+w[j]+c; - c = Math.floor(v/0x4000000); - w[j++] = v&0x3ffffff; - } - return c; -} -// am2 avoids a big mult-and-extract completely. -// Max digit bits should be <= 30 because we do bitwise ops -// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) -function am2(i,x,w,j,c,n) { - var xl = x&0x7fff, xh = x>>15; - while(--n >= 0) { - var l = this[i]&0x7fff; - var h = this[i++]>>15; - var m = xh*l+h*xl; - l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff); - c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); - w[j++] = l&0x3fffffff; - } - return c; -} -// Alternately, set max digit bits to 28 since some -// browsers slow down when dealing with 32-bit numbers. -function am3(i,x,w,j,c,n) { - var xl = x&0x3fff, xh = x>>14; - while(--n >= 0) { - var l = this[i]&0x3fff; - var h = this[i++]>>14; - var m = xh*l+h*xl; - l = xl*l+((m&0x3fff)<<14)+w[j]+c; - c = (l>>28)+(m>>14)+xh*h; - w[j++] = l&0xfffffff; - } - return c; -} -if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) { - BigInteger.prototype.am = am2; - dbits = 30; -} -else if(j_lm && (navigator.appName != "Netscape")) { - BigInteger.prototype.am = am1; - dbits = 26; -} -else { // Mozilla/Netscape seems to prefer am3 - BigInteger.prototype.am = am3; - dbits = 28; -} - -BigInteger.prototype.DB = dbits; -BigInteger.prototype.DM = ((1<<dbits)-1); -BigInteger.prototype.DV = (1<<dbits); - -var BI_FP = 52; -BigInteger.prototype.FV = Math.pow(2,BI_FP); -BigInteger.prototype.F1 = BI_FP-dbits; -BigInteger.prototype.F2 = 2*dbits-BI_FP; - -// Digit conversions -var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz"; -var BI_RC = new Array(); -var rr,vv; -rr = "0".charCodeAt(0); -for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv; -rr = "a".charCodeAt(0); -for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; -rr = "A".charCodeAt(0); -for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; - -function int2char(n) { return BI_RM.charAt(n); } -function intAt(s,i) { - var c = BI_RC[s.charCodeAt(i)]; - return (c==null)?-1:c; -} - -// (protected) copy this to r -function bnpCopyTo(r) { - for(var i = this.t-1; i >= 0; --i) r[i] = this[i]; - r.t = this.t; - r.s = this.s; -} - -// (protected) set from integer value x, -DV <= x < DV -function bnpFromInt(x) { - this.t = 1; - this.s = (x<0)?-1:0; - if(x > 0) this[0] = x; - else if(x < -1) this[0] = x+DV; - else this.t = 0; -} - -// return bigint initialized to value -function nbv(i) { var r = nbi(); r.fromInt(i); return r; } - -// (protected) set from string and radix -function bnpFromString(s,b) { - var k; - if(b == 16) k = 4; - else if(b == 8) k = 3; - else if(b == 256) k = 8; // byte array - else if(b == 2) k = 1; - else if(b == 32) k = 5; - else if(b == 4) k = 2; - else { this.fromRadix(s,b); return; } - this.t = 0; - this.s = 0; - var i = s.length, mi = false, sh = 0; - while(--i >= 0) { - var x = (k==8)?s[i]&0xff:intAt(s,i); - if(x < 0) { - if(s.charAt(i) == "-") mi = true; - continue; - } - mi = false; - if(sh == 0) - this[this.t++] = x; - else if(sh+k > this.DB) { - this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh; - this[this.t++] = (x>>(this.DB-sh)); - } - else - this[this.t-1] |= x<<sh; - sh += k; - if(sh >= this.DB) sh -= this.DB; - } - if(k == 8 && (s[0]&0x80) != 0) { - this.s = -1; - if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh; - } - this.clamp(); - if(mi) BigInteger.ZERO.subTo(this,this); -} - -// (protected) clamp off excess high words -function bnpClamp() { - var c = this.s&this.DM; - while(this.t > 0 && this[this.t-1] == c) --this.t; -} - -// (public) return string representation in given radix -function bnToString(b) { - if(this.s < 0) return "-"+this.negate().toString(b); - var k; - if(b == 16) k = 4; - else if(b == 8) k = 3; - else if(b == 2) k = 1; - else if(b == 32) k = 5; - else if(b == 4) k = 2; - else return this.toRadix(b); - var km = (1<<k)-1, d, m = false, r = "", i = this.t; - var p = this.DB-(i*this.DB)%k; - if(i-- > 0) { - if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); } - while(i >= 0) { - if(p < k) { - d = (this[i]&((1<<p)-1))<<(k-p); - d |= this[--i]>>(p+=this.DB-k); - } - else { - d = (this[i]>>(p-=k))&km; - if(p <= 0) { p += this.DB; --i; } - } - if(d > 0) m = true; - if(m) r += int2char(d); - } - } - return m?r:"0"; -} - -// (public) -this -function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; } - -// (public) |this| -function bnAbs() { return (this.s<0)?this.negate():this; } - -// (public) return + if this > a, - if this < a, 0 if equal -function bnCompareTo(a) { - var r = this.s-a.s; - if(r != 0) return r; - var i = this.t; - r = i-a.t; - if(r != 0) return r; - while(--i >= 0) if((r=this[i]-a[i]) != 0) return r; - return 0; -} - -// returns bit length of the integer x -function nbits(x) { - var r = 1, t; - if((t=x>>>16) != 0) { x = t; r += 16; } - if((t=x>>8) != 0) { x = t; r += 8; } - if((t=x>>4) != 0) { x = t; r += 4; } - if((t=x>>2) != 0) { x = t; r += 2; } - if((t=x>>1) != 0) { x = t; r += 1; } - return r; -} - -// (public) return the number of bits in "this" -function bnBitLength() { - if(this.t <= 0) return 0; - return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM)); -} - -// (protected) r = this << n*DB -function bnpDLShiftTo(n,r) { - var i; - for(i = this.t-1; i >= 0; --i) r[i+n] = this[i]; - for(i = n-1; i >= 0; --i) r[i] = 0; - r.t = this.t+n; - r.s = this.s; -} - -// (protected) r = this >> n*DB -function bnpDRShiftTo(n,r) { - for(var i = n; i < this.t; ++i) r[i-n] = this[i]; - r.t = Math.max(this.t-n,0); - r.s = this.s; -} - -// (protected) r = this << n -function bnpLShiftTo(n,r) { - var bs = n%this.DB; - var cbs = this.DB-bs; - var bm = (1<<cbs)-1; - var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i; - for(i = this.t-1; i >= 0; --i) { - r[i+ds+1] = (this[i]>>cbs)|c; - c = (this[i]&bm)<<bs; - } - for(i = ds-1; i >= 0; --i) r[i] = 0; - r[ds] = c; - r.t = this.t+ds+1; - r.s = this.s; - r.clamp(); -} - -// (protected) r = this >> n -function bnpRShiftTo(n,r) { - r.s = this.s; - var ds = Math.floor(n/this.DB); - if(ds >= this.t) { r.t = 0; return; } - var bs = n%this.DB; - var cbs = this.DB-bs; - var bm = (1<<bs)-1; - r[0] = this[ds]>>bs; - for(var i = ds+1; i < this.t; ++i) { - r[i-ds-1] |= (this[i]&bm)<<cbs; - r[i-ds] = this[i]>>bs; - } - if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs; - r.t = this.t-ds; - r.clamp(); -} - -// (protected) r = this - a -function bnpSubTo(a,r) { - var i = 0, c = 0, m = Math.min(a.t,this.t); - while(i < m) { - c += this[i]-a[i]; - r[i++] = c&this.DM; - c >>= this.DB; - } - if(a.t < this.t) { - c -= a.s; - while(i < this.t) { - c += this[i]; - r[i++] = c&this.DM; - c >>= this.DB; - } - c += this.s; - } - else { - c += this.s; - while(i < a.t) { - c -= a[i]; - r[i++] = c&this.DM; - c >>= this.DB; - } - c -= a.s; - } - r.s = (c<0)?-1:0; - if(c < -1) r[i++] = this.DV+c; - else if(c > 0) r[i++] = c; - r.t = i; - r.clamp(); -} - -// (protected) r = this * a, r != this,a (HAC 14.12) -// "this" should be the larger one if appropriate. -function bnpMultiplyTo(a,r) { - var x = this.abs(), y = a.abs(); - var i = x.t; - r.t = i+y.t; - while(--i >= 0) r[i] = 0; - for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t); - r.s = 0; - r.clamp(); - if(this.s != a.s) BigInteger.ZERO.subTo(r,r); -} - -// (protected) r = this^2, r != this (HAC 14.16) -function bnpSquareTo(r) { - var x = this.abs(); - var i = r.t = 2*x.t; - while(--i >= 0) r[i] = 0; - for(i = 0; i < x.t-1; ++i) { - var c = x.am(i,x[i],r,2*i,0,1); - if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) { - r[i+x.t] -= x.DV; - r[i+x.t+1] = 1; - } - } - if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1); - r.s = 0; - r.clamp(); -} - -// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) -// r != q, this != m. q or r may be null. -function bnpDivRemTo(m,q,r) { - var pm = m.abs(); - if(pm.t <= 0) return; - var pt = this.abs(); - if(pt.t < pm.t) { - if(q != null) q.fromInt(0); - if(r != null) this.copyTo(r); - return; - } - if(r == null) r = nbi(); - var y = nbi(), ts = this.s, ms = m.s; - var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus - if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } - else { pm.copyTo(y); pt.copyTo(r); } - var ys = y.t; - var y0 = y[ys-1]; - if(y0 == 0) return; - var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0); - var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2; - var i = r.t, j = i-ys, t = (q==null)?nbi():q; - y.dlShiftTo(j,t); - if(r.compareTo(t) >= 0) { - r[r.t++] = 1; - r.subTo(t,r); - } - BigInteger.ONE.dlShiftTo(ys,t); - t.subTo(y,y); // "negative" y so we can replace sub with am later - while(y.t < ys) y[y.t++] = 0; - while(--j >= 0) { - // Estimate quotient digit - var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2); - if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out - y.dlShiftTo(j,t); - r.subTo(t,r); - while(r[i] < --qd) r.subTo(t,r); - } - } - if(q != null) { - r.drShiftTo(ys,q); - if(ts != ms) BigInteger.ZERO.subTo(q,q); - } - r.t = ys; - r.clamp(); - if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder - if(ts < 0) BigInteger.ZERO.subTo(r,r); -} - -// (public) this mod a -function bnMod(a) { - var r = nbi(); - this.abs().divRemTo(a,null,r); - if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r); - return r; -} - -// Modular reduction using "classic" algorithm -function Classic(m) { this.m = m; } -function cConvert(x) { - if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m); - else return x; -} -function cRevert(x) { return x; } -function cReduce(x) { x.divRemTo(this.m,null,x); } -function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } -function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); } - -Classic.prototype.convert = cConvert; -Classic.prototype.revert = cRevert; -Classic.prototype.reduce = cReduce; -Classic.prototype.mulTo = cMulTo; -Classic.prototype.sqrTo = cSqrTo; - -// (protected) return "-1/this % 2^DB"; useful for Mont. reduction -// justification: -// xy == 1 (mod m) -// xy = 1+km -// xy(2-xy) = (1+km)(1-km) -// x[y(2-xy)] = 1-k^2m^2 -// x[y(2-xy)] == 1 (mod m^2) -// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 -// should reduce x and y(2-xy) by m^2 at each step to keep size bounded. -// JS multiply "overflows" differently from C/C++, so care is needed here. -function bnpInvDigit() { - if(this.t < 1) return 0; - var x = this[0]; - if((x&1) == 0) return 0; - var y = x&3; // y == 1/x mod 2^2 - y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4 - y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8 - y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16 - // last step - calculate inverse mod DV directly; - // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints - y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits - // we really want the negative inverse, and -DV < y < DV - return (y>0)?this.DV-y:-y; -} - -// Montgomery reduction -function Montgomery(m) { - this.m = m; - this.mp = m.invDigit(); - this.mpl = this.mp&0x7fff; - this.mph = this.mp>>15; - this.um = (1<<(m.DB-15))-1; - this.mt2 = 2*m.t; -} - -// xR mod m -function montConvert(x) { - var r = nbi(); - x.abs().dlShiftTo(this.m.t,r); - r.divRemTo(this.m,null,r); - if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r); - return r; -} - -// x/R mod m -function montRevert(x) { - var r = nbi(); - x.copyTo(r); - this.reduce(r); - return r; -} - -// x = x/R mod m (HAC 14.32) -function montReduce(x) { - while(x.t <= this.mt2) // pad x so am has enough room later - x[x.t++] = 0; - for(var i = 0; i < this.m.t; ++i) { - // faster way of calculating u0 = x[i]*mp mod DV - var j = x[i]&0x7fff; - var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM; - // use am to combine the multiply-shift-add into one call - j = i+this.m.t; - x[j] += this.m.am(0,u0,x,i,0,this.m.t); - // propagate carry - while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; } - } - x.clamp(); - x.drShiftTo(this.m.t,x); - if(x.compareTo(this.m) >= 0) x.subTo(this.m,x); -} - -// r = "x^2/R mod m"; x != r -function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); } - -// r = "xy/R mod m"; x,y != r -function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } - -Montgomery.prototype.convert = montConvert; -Montgomery.prototype.revert = montRevert; -Montgomery.prototype.reduce = montReduce; -Montgomery.prototype.mulTo = montMulTo; -Montgomery.prototype.sqrTo = montSqrTo; - -// (protected) true iff this is even -function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; } - -// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) -function bnpExp(e,z) { - if(e > 0xffffffff || e < 1) return BigInteger.ONE; - var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; - g.copyTo(r); - while(--i >= 0) { - z.sqrTo(r,r2); - if((e&(1<<i)) > 0) z.mulTo(r2,g,r); - else { var t = r; r = r2; r2 = t; } - } - return z.revert(r); -} - -// (public) this^e % m, 0 <= e < 2^32 -function bnModPowInt(e,m) { - var z; - if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); - return this.exp(e,z); -} - -// protected -BigInteger.prototype.copyTo = bnpCopyTo; -BigInteger.prototype.fromInt = bnpFromInt; -BigInteger.prototype.fromString = bnpFromString; -BigInteger.prototype.clamp = bnpClamp; -BigInteger.prototype.dlShiftTo = bnpDLShiftTo; -BigInteger.prototype.drShiftTo = bnpDRShiftTo; -BigInteger.prototype.lShiftTo = bnpLShiftTo; -BigInteger.prototype.rShiftTo = bnpRShiftTo; -BigInteger.prototype.subTo = bnpSubTo; -BigInteger.prototype.multiplyTo = bnpMultiplyTo; -BigInteger.prototype.squareTo = bnpSquareTo; -BigInteger.prototype.divRemTo = bnpDivRemTo; -BigInteger.prototype.invDigit = bnpInvDigit; -BigInteger.prototype.isEven = bnpIsEven; -BigInteger.prototype.exp = bnpExp; - -// public -BigInteger.prototype.toString = bnToString; -BigInteger.prototype.negate = bnNegate; -BigInteger.prototype.abs = bnAbs; -BigInteger.prototype.compareTo = bnCompareTo; -BigInteger.prototype.bitLength = bnBitLength; -BigInteger.prototype.mod = bnMod; -BigInteger.prototype.modPowInt = bnModPowInt; - -// "constants" -BigInteger.ZERO = nbv(0); -BigInteger.ONE = nbv(1); diff --git a/src/jsbn2.js b/src/jsbn2.js deleted file mode 100644 index b135844..0000000 --- a/src/jsbn2.js +++ /dev/null @@ -1,672 +0,0 @@ -/* - * Copyright (c) 2003-2005 Tom Wu - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, - * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY - * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, - * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER - * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF - * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * In addition, the following condition applies: - * - * All redistributions must retain an intact copy of this copyright notice - * and disclaimer. - */ - -// Extended JavaScript BN functions, required for RSA private ops. - -// (public) -function bnClone() { var r = nbi(); this.copyTo(r); return r; } - -// (public) return value as integer -function bnIntValue() { - if(this.s < 0) { - if(this.t == 1) return this[0]-this.DV; - else if(this.t == 0) return -1; - } - else if(this.t == 1) return this[0]; - else if(this.t == 0) return 0; - // assumes 16 < DB < 32 - return ((this[1]&((1<<(32-this.DB))-1))<<this.DB)|this[0]; -} - -// (public) return value as byte -function bnByteValue() { return (this.t==0)?this.s:(this[0]<<24)>>24; } - -// (public) return value as short (assumes DB>=16) -function bnShortValue() { return (this.t==0)?this.s:(this[0]<<16)>>16; } - -// (protected) return x s.t. r^x < DV -function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); } - -// (public) 0 if this == 0, 1 if this > 0 -function bnSigNum() { - if(this.s < 0) return -1; - else if(this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0; - else return 1; -} - -// (protected) convert to radix string -function bnpToRadix(b) { - if(b == null) b = 10; - if(this.signum() == 0 || b < 2 || b > 36) return "0"; - var cs = this.chunkSize(b); - var a = Math.pow(b,cs); - var d = nbv(a), y = nbi(), z = nbi(), r = ""; - this.divRemTo(d,y,z); - while(y.signum() > 0) { - r = (a+z.intValue()).toString(b).substr(1) + r; - y.divRemTo(d,y,z); - } - return z.intValue().toString(b) + r; -} - -// (protected) convert from radix string -function bnpFromRadix(s,b) { - this.fromInt(0); - if(b == null) b = 10; - var cs = this.chunkSize(b); - var d = Math.pow(b,cs), mi = false, j = 0, w = 0; - for(var i = 0; i < s.length; ++i) { - var x = intAt(s,i); - if(x < 0) { - if(s.charAt(i) == "-" && this.signum() == 0) mi = true; - continue; - } - w = b*w+x; - if(++j >= cs) { - this.dMultiply(d); - this.dAddOffset(w,0); - j = 0; - w = 0; - } - } - if(j > 0) { - this.dMultiply(Math.pow(b,j)); - this.dAddOffset(w,0); - } - if(mi) BigInteger.ZERO.subTo(this,this); -} - -// (protected) alternate constructor -function bnpFromNumber(a,b,c) { - if("number" == typeof b) { - // new BigInteger(int,int,RNG) - if(a < 2) this.fromInt(1); - else { - this.fromNumber(a,c); - if(!this.testBit(a-1)) // force MSB set - this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this); - if(this.isEven()) this.dAddOffset(1,0); // force odd - while(!this.isProbablePrime(b)) { - this.dAddOffset(2,0); - if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this); - } - } - } - else { - // new BigInteger(int,RNG) - var x = new Array(), t = a&7; - x.length = (a>>3)+1; - b.nextBytes(x); - if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0; - this.fromString(x,256); - } -} - -// (public) convert to bigendian byte array -function bnToByteArray() { - var i = this.t, r = new Array(); - r[0] = this.s; - var p = this.DB-(i*this.DB)%8, d, k = 0; - if(i-- > 0) { - if(p < this.DB && (d = this[i]>>p) != (this.s&this.DM)>>p) - r[k++] = d|(this.s<<(this.DB-p)); - while(i >= 0) { - if(p < 8) { - d = (this[i]&((1<<p)-1))<<(8-p); - d |= this[--i]>>(p+=this.DB-8); - } - else { - d = (this[i]>>(p-=8))&0xff; - if(p <= 0) { p += this.DB; --i; } - } - if((d&0x80) != 0) d |= -256; - if(k == 0 && (this.s&0x80) != (d&0x80)) ++k; - if(k > 0 || d != this.s) r[k++] = d; - } - } - return r; -} - -function bnEquals(a) { return(this.compareTo(a)==0); } -function bnMin(a) { return(this.compareTo(a)<0)?this:a; } -function bnMax(a) { return(this.compareTo(a)>0)?this:a; } - -// (protected) r = this op a (bitwise) -function bnpBitwiseTo(a,op,r) { - var i, f, m = Math.min(a.t,this.t); - for(i = 0; i < m; ++i) r[i] = op(this[i],a[i]); - if(a.t < this.t) { - f = a.s&this.DM; - for(i = m; i < this.t; ++i) r[i] = op(this[i],f); - r.t = this.t; - } - else { - f = this.s&this.DM; - for(i = m; i < a.t; ++i) r[i] = op(f,a[i]); - r.t = a.t; - } - r.s = op(this.s,a.s); - r.clamp(); -} - -// (public) this & a -function op_and(x,y) { return x&y; } -function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; } - -// (public) this | a -function op_or(x,y) { return x|y; } -function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; } - -// (public) this ^ a -function op_xor(x,y) { return x^y; } -function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; } - -// (public) this & ~a -function op_andnot(x,y) { return x&~y; } -function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; } - -// (public) ~this -function bnNot() { - var r = nbi(); - for(var i = 0; i < this.t; ++i) r[i] = this.DM&~this[i]; - r.t = this.t; - r.s = ~this.s; - return r; -} - -// (public) this << n -function bnShiftLeft(n) { - var r = nbi(); - if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r); - return r; -} - -// (public) this >> n -function bnShiftRight(n) { - var r = nbi(); - if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r); - return r; -} - -// return index of lowest 1-bit in x, x < 2^31 -function lbit(x) { - if(x == 0) return -1; - var r = 0; - if((x&0xffff) == 0) { x >>= 16; r += 16; } - if((x&0xff) == 0) { x >>= 8; r += 8; } - if((x&0xf) == 0) { x >>= 4; r += 4; } - if((x&3) == 0) { x >>= 2; r += 2; } - if((x&1) == 0) ++r; - return r; -} - -// (public) returns index of lowest 1-bit (or -1 if none) -function bnGetLowestSetBit() { - for(var i = 0; i < this.t; ++i) - if(this[i] != 0) return i*this.DB+lbit(this[i]); - if(this.s < 0) return this.t*this.DB; - return -1; -} - -// return number of 1 bits in x -function cbit(x) { - var r = 0; - while(x != 0) { x &= x-1; ++r; } - return r; -} - -// (public) return number of set bits -function bnBitCount() { - var r = 0, x = this.s&this.DM; - for(var i = 0; i < this.t; ++i) r += cbit(this[i]^x); - return r; -} - -// (public) true iff nth bit is set -function bnTestBit(n) { - var j = Math.floor(n/this.DB); - if(j >= this.t) return(this.s!=0); - return((this[j]&(1<<(n%this.DB)))!=0); -} - -// (protected) this op (1<<n) -function bnpChangeBit(n,op) { - var r = BigInteger.ONE.shiftLeft(n); - this.bitwiseTo(r,op,r); - return r; -} - -// (public) this | (1<<n) -function bnSetBit(n) { return this.changeBit(n,op_or); } - -// (public) this & ~(1<<n) -function bnClearBit(n) { return this.changeBit(n,op_andnot); } - -// (public) this ^ (1<<n) -function bnFlipBit(n) { return this.changeBit(n,op_xor); } - -// (protected) r = this + a -function bnpAddTo(a,r) { - var i = 0, c = 0, m = Math.min(a.t,this.t); - while(i < m) { - c += this[i]+a[i]; - r[i++] = c&this.DM; - c >>= this.DB; - } - if(a.t < this.t) { - c += a.s; - while(i < this.t) { - c += this[i]; - r[i++] = c&this.DM; - c >>= this.DB; - } - c += this.s; - } - else { - c += this.s; - while(i < a.t) { - c += a[i]; - r[i++] = c&this.DM; - c >>= this.DB; - } - c += a.s; - } - r.s = (c<0)?-1:0; - if(c > 0) r[i++] = c; - else if(c < -1) r[i++] = this.DV+c; - r.t = i; - r.clamp(); -} - -// (public) this + a -function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; } - -// (public) this - a -function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; } - -// (public) this * a -function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; } - -// (public) this / a -function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; } - -// (public) this % a -function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; } - -// (public) [this/a,this%a] -function bnDivideAndRemainder(a) { - var q = nbi(), r = nbi(); - this.divRemTo(a,q,r); - return new Array(q,r); -} - -// (protected) this *= n, this >= 0, 1 < n < DV -function bnpDMultiply(n) { - this[this.t] = this.am(0,n-1,this,0,0,this.t); - ++this.t; - this.clamp(); -} - -// (protected) this += n << w words, this >= 0 -function bnpDAddOffset(n,w) { - while(this.t <= w) this[this.t++] = 0; - this[w] += n; - while(this[w] >= this.DV) { - this[w] -= this.DV; - if(++w >= this.t) this[this.t++] = 0; - ++this[w]; - } -} - -// A "null" reducer -function NullExp() {} -function nNop(x) { return x; } -function nMulTo(x,y,r) { x.multiplyTo(y,r); } -function nSqrTo(x,r) { x.squareTo(r); } - -NullExp.prototype.convert = nNop; -NullExp.prototype.revert = nNop; -NullExp.prototype.mulTo = nMulTo; -NullExp.prototype.sqrTo = nSqrTo; - -// (public) this^e -function bnPow(e) { return this.exp(e,new NullExp()); } - -// (protected) r = lower n words of "this * a", a.t <= n -// "this" should be the larger one if appropriate. -function bnpMultiplyLowerTo(a,n,r) { - var i = Math.min(this.t+a.t,n); - r.s = 0; // assumes a,this >= 0 - r.t = i; - while(i > 0) r[--i] = 0; - var j; - for(j = r.t-this.t; i < j; ++i) r[i+this.t] = this.am(0,a[i],r,i,0,this.t); - for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a[i],r,i,0,n-i); - r.clamp(); -} - -// (protected) r = "this * a" without lower n words, n > 0 -// "this" should be the larger one if appropriate. -function bnpMultiplyUpperTo(a,n,r) { - --n; - var i = r.t = this.t+a.t-n; - r.s = 0; // assumes a,this >= 0 - while(--i >= 0) r[i] = 0; - for(i = Math.max(n-this.t,0); i < a.t; ++i) - r[this.t+i-n] = this.am(n-i,a[i],r,0,0,this.t+i-n); - r.clamp(); - r.drShiftTo(1,r); -} - -// Barrett modular reduction -function Barrett(m) { - // setup Barrett - this.r2 = nbi(); - this.q3 = nbi(); - BigInteger.ONE.dlShiftTo(2*m.t,this.r2); - this.mu = this.r2.divide(m); - this.m = m; -} - -function barrettConvert(x) { - if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); - else if(x.compareTo(this.m) < 0) return x; - else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; } -} - -function barrettRevert(x) { return x; } - -// x = x mod m (HAC 14.42) -function barrettReduce(x) { - x.drShiftTo(this.m.t-1,this.r2); - if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); } - this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3); - this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2); - while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1); - x.subTo(this.r2,x); - while(x.compareTo(this.m) >= 0) x.subTo(this.m,x); -} - -// r = x^2 mod m; x != r -function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); } - -// r = x*y mod m; x,y != r -function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } - -Barrett.prototype.convert = barrettConvert; -Barrett.prototype.revert = barrettRevert; -Barrett.prototype.reduce = barrettReduce; -Barrett.prototype.mulTo = barrettMulTo; -Barrett.prototype.sqrTo = barrettSqrTo; - -// (public) this^e % m (HAC 14.85) -function bnModPow(e,m) { - var i = e.bitLength(), k, r = nbv(1), z; - if(i <= 0) return r; - else if(i < 18) k = 1; - else if(i < 48) k = 3; - else if(i < 144) k = 4; - else if(i < 768) k = 5; - else k = 6; - if(i < 8) - z = new Classic(m); - else if(m.isEven()) - z = new Barrett(m); - else - z = new Montgomery(m); - - // precomputation - var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1; - g[1] = z.convert(this); - if(k > 1) { - var g2 = nbi(); - z.sqrTo(g[1],g2); - while(n <= km) { - g[n] = nbi(); - z.mulTo(g2,g[n-2],g[n]); - n += 2; - } - } - - var j = e.t-1, w, is1 = true, r2 = nbi(), t; - i = nbits(e[j])-1; - while(j >= 0) { - if(i >= k1) w = (e[j]>>(i-k1))&km; - else { - w = (e[j]&((1<<(i+1))-1))<<(k1-i); - if(j > 0) w |= e[j-1]>>(this.DB+i-k1); - } - - n = k; - while((w&1) == 0) { w >>= 1; --n; } - if((i -= n) < 0) { i += this.DB; --j; } - if(is1) { // ret == 1, don't bother squaring or multiplying it - g[w].copyTo(r); - is1 = false; - } - else { - while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; } - if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; } - z.mulTo(r2,g[w],r); - } - - while(j >= 0 && (e[j]&(1<<i)) == 0) { - z.sqrTo(r,r2); t = r; r = r2; r2 = t; - if(--i < 0) { i = this.DB-1; --j; } - } - } - return z.revert(r); -} - -// (public) gcd(this,a) (HAC 14.54) -function bnGCD(a) { - var x = (this.s<0)?this.negate():this.clone(); - var y = (a.s<0)?a.negate():a.clone(); - if(x.compareTo(y) < 0) { var t = x; x = y; y = t; } - var i = x.getLowestSetBit(), g = y.getLowestSetBit(); - if(g < 0) return x; - if(i < g) g = i; - if(g > 0) { - x.rShiftTo(g,x); - y.rShiftTo(g,y); - } - while(x.signum() > 0) { - if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x); - if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y); - if(x.compareTo(y) >= 0) { - x.subTo(y,x); - x.rShiftTo(1,x); - } - else { - y.subTo(x,y); - y.rShiftTo(1,y); - } - } - if(g > 0) y.lShiftTo(g,y); - return y; -} - -// (protected) this % n, n < 2^26 -function bnpModInt(n) { - if(n <= 0) return 0; - var d = this.DV%n, r = (this.s<0)?n-1:0; - if(this.t > 0) - if(d == 0) r = this[0]%n; - else for(var i = this.t-1; i >= 0; --i) r = (d*r+this[i])%n; - return r; -} - -// (public) 1/this % m (HAC 14.61) -function bnModInverse(m) { - var ac = m.isEven(); - if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO; - var u = m.clone(), v = this.clone(); - var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1); - while(u.signum() != 0) { - while(u.isEven()) { - u.rShiftTo(1,u); - if(ac) { - if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); } - a.rShiftTo(1,a); - } - else if(!b.isEven()) b.subTo(m,b); - b.rShiftTo(1,b); - } - while(v.isEven()) { - v.rShiftTo(1,v); - if(ac) { - if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); } - c.rShiftTo(1,c); - } - else if(!d.isEven()) d.subTo(m,d); - d.rShiftTo(1,d); - } - if(u.compareTo(v) >= 0) { - u.subTo(v,u); - if(ac) a.subTo(c,a); - b.subTo(d,b); - } - else { - v.subTo(u,v); - if(ac) c.subTo(a,c); - d.subTo(b,d); - } - } - if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO; - if(d.compareTo(m) >= 0) return d.subtract(m); - if(d.signum() < 0) d.addTo(m,d); else return d; - if(d.signum() < 0) return d.add(m); else return d; -} - -var lowprimes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509]; -var lplim = (1<<26)/lowprimes[lowprimes.length-1]; - -// (public) test primality with certainty >= 1-.5^t -function bnIsProbablePrime(t) { - var i, x = this.abs(); - if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) { - for(i = 0; i < lowprimes.length; ++i) - if(x[0] == lowprimes[i]) return true; - return false; - } - if(x.isEven()) return false; - i = 1; - while(i < lowprimes.length) { - var m = lowprimes[i], j = i+1; - while(j < lowprimes.length && m < lplim) m *= lowprimes[j++]; - m = x.modInt(m); - while(i < j) if(m%lowprimes[i++] == 0) return false; - } - return x.millerRabin(t); -} - -// (protected) true if probably prime (HAC 4.24, Miller-Rabin) -function bnpMillerRabin(t) { - var n1 = this.subtract(BigInteger.ONE); - var k = n1.getLowestSetBit(); - if(k <= 0) return false; - var r = n1.shiftRight(k); - t = (t+1)>>1; - if(t > lowprimes.length) t = lowprimes.length; - var a = nbi(); - for(var i = 0; i < t; ++i) { - a.fromInt(lowprimes[i]); - var y = a.modPow(r,this); - if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) { - var j = 1; - while(j++ < k && y.compareTo(n1) != 0) { - y = y.modPowInt(2,this); - if(y.compareTo(BigInteger.ONE) == 0) return false; - } - if(y.compareTo(n1) != 0) return false; - } - } - return true; -} - -// protected -BigInteger.prototype.chunkSize = bnpChunkSize; -BigInteger.prototype.toRadix = bnpToRadix; -BigInteger.prototype.fromRadix = bnpFromRadix; -BigInteger.prototype.fromNumber = bnpFromNumber; -BigInteger.prototype.bitwiseTo = bnpBitwiseTo; -BigInteger.prototype.changeBit = bnpChangeBit; -BigInteger.prototype.addTo = bnpAddTo; -BigInteger.prototype.dMultiply = bnpDMultiply; -BigInteger.prototype.dAddOffset = bnpDAddOffset; -BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo; -BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo; -BigInteger.prototype.modInt = bnpModInt; -BigInteger.prototype.millerRabin = bnpMillerRabin; - -// public -BigInteger.prototype.clone = bnClone; -BigInteger.prototype.intValue = bnIntValue; -BigInteger.prototype.byteValue = bnByteValue; -BigInteger.prototype.shortValue = bnShortValue; -BigInteger.prototype.signum = bnSigNum; -BigInteger.prototype.toByteArray = bnToByteArray; -BigInteger.prototype.equals = bnEquals; -BigInteger.prototype.min = bnMin; -BigInteger.prototype.max = bnMax; -BigInteger.prototype.and = bnAnd; -BigInteger.prototype.or = bnOr; -BigInteger.prototype.xor = bnXor; -BigInteger.prototype.andNot = bnAndNot; -BigInteger.prototype.not = bnNot; -BigInteger.prototype.shiftLeft = bnShiftLeft; -BigInteger.prototype.shiftRight = bnShiftRight; -BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit; -BigInteger.prototype.bitCount = bnBitCount; -BigInteger.prototype.testBit = bnTestBit; -BigInteger.prototype.setBit = bnSetBit; -BigInteger.prototype.clearBit = bnClearBit; -BigInteger.prototype.flipBit = bnFlipBit; -BigInteger.prototype.add = bnAdd; -BigInteger.prototype.subtract = bnSubtract; -BigInteger.prototype.multiply = bnMultiply; -BigInteger.prototype.divide = bnDivide; -BigInteger.prototype.remainder = bnRemainder; -BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder; -BigInteger.prototype.modPow = bnModPow; -BigInteger.prototype.modInverse = bnModInverse; -BigInteger.prototype.pow = bnPow; -BigInteger.prototype.gcd = bnGCD; -BigInteger.prototype.isProbablePrime = bnIsProbablePrime; - -// BigInteger interfaces not implemented in jsbn: - -// BigInteger(int signum, byte[] magnitude) -// double doubleValue() -// float floatValue() -// int hashCode() -// long longValue() -// static BigInteger valueOf(long val) diff --git a/src/prng4.js b/src/prng4.js deleted file mode 100644 index ca3026d..0000000 --- a/src/prng4.js +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2003-2005 Tom Wu - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, - * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY - * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, - * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER - * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF - * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * In addition, the following condition applies: - * - * All redistributions must retain an intact copy of this copyright notice - * and disclaimer. - */ - -// prng4.js - uses Arcfour as a PRNG - -function Arcfour() { - this.i = 0; - this.j = 0; - this.S = new Array(); -} - -// Initialize arcfour context from key, an array of ints, each from [0..255] -function ARC4init(key) { - var i, j, t; - for(i = 0; i < 256; ++i) - this.S[i] = i; - j = 0; - for(i = 0; i < 256; ++i) { - j = (j + this.S[i] + key[i % key.length]) & 255; - t = this.S[i]; - this.S[i] = this.S[j]; - this.S[j] = t; - } - this.i = 0; - this.j = 0; -} - -function ARC4next() { - var t; - this.i = (this.i + 1) & 255; - this.j = (this.j + this.S[this.i]) & 255; - t = this.S[this.i]; - this.S[this.i] = this.S[this.j]; - this.S[this.j] = t; - return this.S[(t + this.S[this.i]) & 255]; -} - -Arcfour.prototype.init = ARC4init; -Arcfour.prototype.next = ARC4next; - -// Plug in your RNG constructor here -function prng_newstate() { - return new Arcfour(); -} - -// Pool size must be a multiple of 4 and greater than 32. -// An array of bytes the size of the pool will be passed to init() -var rng_psize = 256; diff --git a/src/rng.js b/src/rng.js deleted file mode 100644 index 8d8de48..0000000 --- a/src/rng.js +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2003-2005 Tom Wu - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, - * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY - * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, - * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER - * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF - * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * In addition, the following condition applies: - * - * All redistributions must retain an intact copy of this copyright notice - * and disclaimer. - */ - -// Random number generator - requires a PRNG backend, e.g. prng4.js - -// For best results, put code like -// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'> -// in your main HTML document. - -var rng_state; -var rng_pool; -var rng_pptr; - -// Mix in a 32-bit integer into the pool -function rng_seed_int(x) { - rng_pool[rng_pptr++] ^= x & 255; - rng_pool[rng_pptr++] ^= (x >> 8) & 255; - rng_pool[rng_pptr++] ^= (x >> 16) & 255; - rng_pool[rng_pptr++] ^= (x >> 24) & 255; - if(rng_pptr >= rng_psize) rng_pptr -= rng_psize; -} - -// Mix in the current time (w/milliseconds) into the pool -function rng_seed_time() { - rng_seed_int(new Date().getTime()); -} - -// Initialize the pool with junk if needed. -if(rng_pool == null) { - rng_pool = new Array(); - rng_pptr = 0; - var t; - if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) { - // Extract entropy (256 bits) from NS4 RNG if available - var z = window.crypto.random(32); - for(t = 0; t < z.length; ++t) - rng_pool[rng_pptr++] = z.charCodeAt(t) & 255; - } - while(rng_pptr < rng_psize) { // extract some randomness from Math.random() - t = Math.floor(65536 * Math.random()); - rng_pool[rng_pptr++] = t >>> 8; - rng_pool[rng_pptr++] = t & 255; - } - rng_pptr = 0; - rng_seed_time(); - //rng_seed_int(window.screenX); - //rng_seed_int(window.screenY); -} - -function rng_get_byte() { - if(rng_state == null) { - rng_seed_time(); - rng_state = prng_newstate(); - rng_state.init(rng_pool); - for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) - rng_pool[rng_pptr] = 0; - rng_pptr = 0; - //rng_pool = null; - } - // TODO: allow reseeding after first request - return rng_state.next(); -} - -function rng_get_bytes(ba) { - var i; - for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte(); -} - -function SecureRandom() {} - -SecureRandom.prototype.nextBytes = rng_get_bytes; diff --git a/src/srp.js b/src/srp.js deleted file mode 100644 index 6af54ea..0000000 --- a/src/srp.js +++ /dev/null @@ -1,386 +0,0 @@ -function SRP() -{ - // Variables that will be used in the SRP protocol - var Nstr = "115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3"; - var N = new BigInteger(Nstr, 16); - var g = new BigInteger("2"); - var k = new BigInteger("c46d46600d87fef149bd79b81119842f3c20241fda67d06ef412d8f6d9479c58", 16); - var rng = new SecureRandom(); - var a = new BigInteger(32, rng); - var A = g.modPow(a, N); - while(A.mod(N) == 0) - { - a = new BigInteger(32, rng); - A = g.modPow(a, N); - } - var Astr = A.toString(16); - var S = null; - var K = null; - var M = null; - var M2 = null; - var url = document.getElementById("srp_url").value; - var server = document.getElementById("srp_server").value; - var that = this; - var authenticated = false; - var I = document.getElementById("srp_username").value; - var p = document.getElementById("srp_password").value; - var xhr = null; - - // *** Accessor methods *** - - // allows setting the random number A for testing - - this.calculateAndSetA = function(_a) - { - a = new BigInteger(_a, 16); - A = g.modPow(a, N); - Astr = A.toString(16); - return Astr; - }; - - // Returns the user's identity - this.getI = function() - { - return I; - }; - - // Returns the XMLHttpRequest object - this.getxhr = function() - { - return xhr; - }; - - // Returns the base URL - this.geturl = function() - { - return url; - }; - // Returns the BigInteger, g - this.getg = function() - { - return g; - }; - - // Returns the BigInteger, N - this.getN = function() - { - return N; - }; - - // Calculates the X value and return it as a BigInteger - this.calcX = function(s) - { - return new BigInteger(SHA256(s + SHA256(I + ":" + p)), 16); - }; - - // Translates the django path to PHP and ASP.NET paths - this.paths = function(str) - { - // For now, str will be the django path - // This function will translate for other backends. - if(server == "django") - { - return str; - } - }; - - // Check whether or not a variable is defined - function isdefined ( variable) - { - return (typeof(window[variable]) != "undefined"); - }; - - // *** Actions *** - - // Perform ajax requests at the specified url, with the specified parameters - // Calling back the specified function. - this.ajaxRequest = function(full_url, params, callback) - { - if( window.XMLHttpRequest) - xhr = new XMLHttpRequest(); - else if (window.ActiveXObject){ - try{ - xhr = new ActiveXObject("Microsoft.XMLHTTP"); - }catch (e){} - } - else - { - that.error_message("Ajax not supported."); - return; - } - if(xhr){ - xhr.onreadystatechange = function() { - if(xhr.readyState == 4 && xhr.status == 200) { - callback(parseResponse()); - } - }; - xhr.open("POST", full_url, true); - xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); - xhr.setRequestHeader("Content-length", params.length); - xhr.send(params); - } - else - { - that.error_message("Ajax failed."); - } - }; - - function parseResponse() { - if (responseIsXML()) { - return parseXML(xhr.responseXML); - } else if (responseIsJSON()) { - return JSON.parse(xhr.responseText); - } - }; - - function responseIsXML() { - return (xhr.responseType == 'document') || - (xhr.getResponseHeader("Content-Type").indexOf('application/xml') >= 0) - } - - function responseIsJSON() { - return (xhr.responseType == 'json') || - (xhr.getResponseHeader("Content-Type").indexOf('application/json') >= 0) - } - - function parseXML(xml) { - if (xml.getElementsByTagName("r").length > 0) { - return parseAttributesOfElement(xml.getElementsByTagName("r")[0]); - } else { - return parseNodes(xml.childNodes); - } - }; - - function parseAttributesOfElement(elem) { - var response = {}; - for (var i = 0; i < elem.attributes.length; i++) { - var attrib = elem.attributes[i]; - if (attrib.specified) { - response[attrib.name] = attrib.value; - } - } - return response; - }; - - function parseNodes(nodes) { - var response = {}; - for (var i = 0; i < nodes.length; i++) { - var node = nodes[i]; - response[node.tagName] = node.textContent || true; - } - return response; - }; - - // Start the login process by identifying the user - this.identify = function() - { - var handshake_url = url + that.paths("handshake/"); - var params = "I="+I+"&A="+Astr; - that.ajaxRequest(handshake_url, params, receive_salts); - }; - - // Receive login salts from the server, start calculations - function receive_salts(response) - { - if(response.error) { - that.error_message(response.error); - } - // B = 0 will make the algorithm always succeed - refuse such a server - // answer - else if(response.B == 0) { - that.error_message("Server send random number 0 - this is not allowed"); - } - // If there is no algorithm specified, calculate M given s, B, and P - else if(!response.a) - { - calculations(response.s, response.B, p); - that.ajaxRequest(url+that.paths("authenticate/"), "M="+M, confirm_authentication); - } - // If there is an algorithm specified, start the login process - else { - upgrade(response.s, response.B, response.a, response.d); - } - }; - // Calculate S, M, and M2 - // This is the client side of the SRP specification - function calculations(s, ephemeral, pass) - { - //S -> C: s | B - var B = new BigInteger(ephemeral, 16); - var Bstr = ephemeral; - // u = H(A,B) - var u = new BigInteger(SHA256(Astr + Bstr), 16); - // x = H(s, H(I:p)) - var x = new BigInteger(SHA256(s + SHA256(I + ":" + pass)), 16); - //S = (B - kg^x) ^ (a + ux) - var kgx = k.multiply(g.modPow(x, N)); - var aux = a.add(u.multiply(x)); - S = B.subtract(kgx).modPow(aux, N); - // M = H(H(N) xor H(g), H(I), s, A, B, K) - var Mstr = A.toString(16) + B.toString(16) + S.toString(16); - M = SHA256(Mstr); - M2 = SHA256(A.toString(16) + M + S.toString(16)); - //M2 = H(A, M, K) - }; - - // Receive M2 from the server and verify it - function confirm_authentication(response) - { - if(response.M) - { - if(response.M == M2) - { - authenticated = true; - that.success(); - } - else - that.error_message("Server key does not match"); - } - else if (response.error) - that.error_message(response.error); - }; - - // *** Upgrades *** - - // Start the process to upgrade the user's account - function upgrade(s,ephemeral,algo,dsalt) - { - // First we need to import the hash functions - import_hashes(); - - // Once the hash functions are imported, do the calculations using the hashpass as the password - function do_upgrade() - { - // If sha1 and md5 are still undefined, sleep again - if(!isdefined("SHA1") || !isdefined("MD5")) - { - window.setTimeout(do_upgrade, 10); - return; - } - if(algo == "sha1") - hashfun = SHA1; - else if(algo == "md5") - hashfun = MD5; - //alert(hashfun(dsalt+p)); - calculations(s, ephemeral, hashfun(dsalt+p)); - that.ajaxRequest(url+that.paths("upgrade/authenticate/"), "M="+M, confirm_upgrade); - }; - window.setTimeout(do_upgrade,10); - }; - - // Encrypt plaintext using slowAES - function encrypt(plaintext) - { - var key = cryptoHelpers.toNumbers(that.key()); - var byteMessage = cryptoHelpers.convertStringToByteArray(plaintext); - var iv = new Array(16); - rng.nextBytes(iv); - var paddedByteMessage = slowAES.getPaddedBlock(byteMessage, 0, byteMessage.length, slowAES.modeOfOperation.CFB); - var ciphertext = slowAES.encrypt(paddedByteMessage, slowAES.modeOfOperation.CFB, key, key.length, iv).cipher; - var retstring = cryptoHelpers.base64.encode(iv.concat(ciphertext)); - while(retstring.indexOf("+",0) > -1) - retstring = retstring.replace("+", "_"); - return retstring; - }; - - // Receive the server's M, confirming that the server has HASH(p) - // Next, send P in plaintext (this is the **only** time it should ever be sent plain text) - function confirm_upgrade(response) - { - if(response.M) - { - if(response.M == M2) - { - K = SHA256(S.toString(16)); - var auth_url = url + that.paths("upgrade/verifier/"); - that.ajaxRequest(auth_url, "p="+encrypt(p)+"&l="+p.length, confirm_verifier); - } - else - that.error_message("Server key does not match"); - } - else if (response.error) - { - that.error_message(response.error); - } - }; - - // After sending the password, check that the response is OK, then reidentify - function confirm_verifier(response) - { - K = null; - if(response.ok) - that.identify(); - else - that.error_message("Verifier could not be confirmed"); - }; - - // This loads javascript libraries. Fname is the path to the library to be imported - function import_file(fname) - { - var scriptElt = document.createElement('script'); - scriptElt.type = 'text/javascript'; - scriptElt.src = fname; - document.getElementsByTagName('head')[0].appendChild(scriptElt); - }; - // If we need SHA1 or MD5, we need to load the javascript files - function import_hashes() - { - // First check that the functions aren't already loaded - if(isdefined("SHA1") && isdefined("MD5")) return; - // Get the directory that this javascript file was loaded from - var arr=that.srpPath.split("/"); - var path = arr.slice(0, arr.length-1).join("/"); - // If this file is called srp.min.js, we will load the packed hash file - if(arr[arr.length-1] == "srp.min.js") - import_file(path+"/crypto.min.js"); - // Otherwise, this file is presumably srp.js, and we will load individual hash files - else - { - import_file(path+"/MD5.js"); - import_file(path+"/SHA1.js"); - import_file(path+"/cryptoHelpers.js"); - import_file(path+"/aes.js"); - } - } - - // This function is called when authentication is successful. - // Developers can set this to other functions in specific implementations - // and change the functionality. - this.success = function() - { - var forward_url = document.getElementById("srp_forward").value; - if(forward_url.charAt(0) != "#") - window.location = forward_url; - else - { - window.location = forward_url; - alert("Login successful."); - } - }; - // If someone wants to use the session key for encrypting traffic, they can - // access the key with this function. - this.key = function() - { - if(K == null) - if(authenticated) - { - K = SHA256(S.toString(16)); - return K; - } - else - that.error_message("User has not been authenticated."); - else - return K; - }; - - // If an error occurs, raise it as an alert. - // Developers can set this to an alternative function to handle erros differently. - this.error_message = function(t) - { - alert(t); - }; - }; - // This line is run while the document is loading - // It gets a list of all <script> tags and finds the last instance. - // The path to this script is the "src" attribute of that tag. - SRP.prototype.srpPath = document.getElementsByTagName('script')[document.getElementsByTagName('script').length-1].getAttribute("src"); diff --git a/src/srp_register.js b/src/srp_register.js deleted file mode 100644 index 3966d79..0000000 --- a/src/srp_register.js +++ /dev/null @@ -1,46 +0,0 @@ -function SRP_REGISTER() -{ - var that; - - // Initiate the registration process - SRP.prototype.register = function() - { - that = this; - var handshake_url = this.geturl() + this.paths("register/salt/"); - var params = "I="+this.getI(); - this.ajaxRequest(handshake_url, params, this.register_receive_salt); - }; - - // Receive the salt for registration - SRP.prototype.register_receive_salt = function(response) - { - if(response.salt) - { - var s = response.salt; - var x = that.calcX(s); - var v = that.getg().modPow(x, that.getN()); - that.register_send_verifier(v.toString(16)); - } - else if(response.error) - { - that.error_message(response.error); - } - }; - // Send the verifier to the server - SRP.prototype.register_send_verifier = function(v) - { - var params = "v="+v; - var auth_url = that.geturl() + that.paths("register/user/"); - that.ajaxRequest(auth_url, params, that.registered_user); - }; - - // The user has been registered successfully, now login - SRP.prototype.registered_user = function(response) - { - if(response.ok) - { - that.identify(); - } - }; -}; -SRP_REGISTER(); |