diff options
Diffstat (limited to 'src/js/sjcl-bip39.js')
-rw-r--r-- | src/js/sjcl-bip39.js | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/src/js/sjcl-bip39.js b/src/js/sjcl-bip39.js index 537e3b8..447b565 100644 --- a/src/js/sjcl-bip39.js +++ b/src/js/sjcl-bip39.js | |||
@@ -26,13 +26,13 @@ var sjcl = { | |||
26 | 26 | ||
27 | /** @namespace Key exchange functions. Right now only SRP is implemented. */ | 27 | /** @namespace Key exchange functions. Right now only SRP is implemented. */ |
28 | keyexchange: {}, | 28 | keyexchange: {}, |
29 | 29 | ||
30 | /** @namespace Block cipher modes of operation. */ | 30 | /** @namespace Block cipher modes of operation. */ |
31 | mode: {}, | 31 | mode: {}, |
32 | 32 | ||
33 | /** @namespace Miscellaneous. HMAC and PBKDF2. */ | 33 | /** @namespace Miscellaneous. HMAC and PBKDF2. */ |
34 | misc: {}, | 34 | misc: {}, |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * @namespace Bit array encoders and decoders. | 37 | * @namespace Bit array encoders and decoders. |
38 | * | 38 | * |
@@ -43,7 +43,7 @@ var sjcl = { | |||
43 | * the method names are "fromBits" and "toBits". | 43 | * the method names are "fromBits" and "toBits". |
44 | */ | 44 | */ |
45 | codec: {}, | 45 | codec: {}, |
46 | 46 | ||
47 | /** @namespace Exceptions. */ | 47 | /** @namespace Exceptions. */ |
48 | exception: { | 48 | exception: { |
49 | /** @constructor Ciphertext is corrupt. */ | 49 | /** @constructor Ciphertext is corrupt. */ |
@@ -51,13 +51,13 @@ var sjcl = { | |||
51 | this.toString = function() { return "CORRUPT: "+this.message; }; | 51 | this.toString = function() { return "CORRUPT: "+this.message; }; |
52 | this.message = message; | 52 | this.message = message; |
53 | }, | 53 | }, |
54 | 54 | ||
55 | /** @constructor Invalid parameter. */ | 55 | /** @constructor Invalid parameter. */ |
56 | invalid: function(message) { | 56 | invalid: function(message) { |
57 | this.toString = function() { return "INVALID: "+this.message; }; | 57 | this.toString = function() { return "INVALID: "+this.message; }; |
58 | this.message = message; | 58 | this.message = message; |
59 | }, | 59 | }, |
60 | 60 | ||
61 | /** @constructor Bug or missing feature in SJCL. @constructor */ | 61 | /** @constructor Bug or missing feature in SJCL. @constructor */ |
62 | bug: function(message) { | 62 | bug: function(message) { |
63 | this.toString = function() { return "BUG: "+this.message; }; | 63 | this.toString = function() { return "BUG: "+this.message; }; |
@@ -159,7 +159,7 @@ sjcl.bitArray = { | |||
159 | if (a1.length === 0 || a2.length === 0) { | 159 | if (a1.length === 0 || a2.length === 0) { |
160 | return a1.concat(a2); | 160 | return a1.concat(a2); |
161 | } | 161 | } |
162 | 162 | ||
163 | var last = a1[a1.length-1], shift = sjcl.bitArray.getPartial(last); | 163 | var last = a1[a1.length-1], shift = sjcl.bitArray.getPartial(last); |
164 | if (shift === 32) { | 164 | if (shift === 32) { |
165 | return a1.concat(a2); | 165 | return a1.concat(a2); |
@@ -245,7 +245,7 @@ sjcl.bitArray = { | |||
245 | _shiftRight: function (a, shift, carry, out) { | 245 | _shiftRight: function (a, shift, carry, out) { |
246 | var i, last2=0, shift2; | 246 | var i, last2=0, shift2; |
247 | if (out === undefined) { out = []; } | 247 | if (out === undefined) { out = []; } |
248 | 248 | ||
249 | for (; shift >= 32; shift -= 32) { | 249 | for (; shift >= 32; shift -= 32) { |
250 | out.push(carry); | 250 | out.push(carry); |
251 | carry = 0; | 251 | carry = 0; |
@@ -253,7 +253,7 @@ sjcl.bitArray = { | |||
253 | if (shift === 0) { | 253 | if (shift === 0) { |
254 | return out.concat(a); | 254 | return out.concat(a); |
255 | } | 255 | } |
256 | 256 | ||
257 | for (i=0; i<a.length; i++) { | 257 | for (i=0; i<a.length; i++) { |
258 | out.push(carry | a[i]>>>shift); | 258 | out.push(carry | a[i]>>>shift); |
259 | carry = a[i] << (32-shift); | 259 | carry = a[i] << (32-shift); |
@@ -263,7 +263,7 @@ sjcl.bitArray = { | |||
263 | out.push(sjcl.bitArray.partial(shift+shift2 & 31, (shift + shift2 > 32) ? carry : out.pop(),1)); | 263 | out.push(sjcl.bitArray.partial(shift+shift2 & 31, (shift + shift2 > 32) ? carry : out.pop(),1)); |
264 | return out; | 264 | return out; |
265 | }, | 265 | }, |
266 | 266 | ||
267 | /** xor a block of 4 words together. | 267 | /** xor a block of 4 words together. |
268 | * @private | 268 | * @private |
269 | */ | 269 | */ |
@@ -295,7 +295,7 @@ sjcl.bitArray = { | |||
295 | * @author Mike Hamburg | 295 | * @author Mike Hamburg |
296 | * @author Dan Boneh | 296 | * @author Dan Boneh |
297 | */ | 297 | */ |
298 | 298 | ||
299 | /** @namespace UTF-8 strings */ | 299 | /** @namespace UTF-8 strings */ |
300 | sjcl.codec.utf8String = { | 300 | sjcl.codec.utf8String = { |
301 | /** Convert from a bitArray to a UTF-8 string. */ | 301 | /** Convert from a bitArray to a UTF-8 string. */ |
@@ -310,7 +310,7 @@ sjcl.codec.utf8String = { | |||
310 | } | 310 | } |
311 | return decodeURIComponent(escape(out)); | 311 | return decodeURIComponent(escape(out)); |
312 | }, | 312 | }, |
313 | 313 | ||
314 | /** Convert from a UTF-8 string to a bitArray. */ | 314 | /** Convert from a UTF-8 string to a bitArray. */ |
315 | toBits: function (str) { | 315 | toBits: function (str) { |
316 | str = unescape(encodeURIComponent(str)); | 316 | str = unescape(encodeURIComponent(str)); |
@@ -412,7 +412,7 @@ sjcl.hash.sha512.prototype = { | |||
412 | * @constant | 412 | * @constant |
413 | */ | 413 | */ |
414 | blockSize: 1024, | 414 | blockSize: 1024, |
415 | 415 | ||
416 | /** | 416 | /** |
417 | * Reset the hash state. | 417 | * Reset the hash state. |
418 | * @return this | 418 | * @return this |
@@ -423,7 +423,7 @@ sjcl.hash.sha512.prototype = { | |||
423 | this._length = 0; | 423 | this._length = 0; |
424 | return this; | 424 | return this; |
425 | }, | 425 | }, |
426 | 426 | ||
427 | /** | 427 | /** |
428 | * Input several words to the hash. | 428 | * Input several words to the hash. |
429 | * @param {bitArray|String} data the data to hash. | 429 | * @param {bitArray|String} data the data to hash. |
@@ -441,7 +441,7 @@ sjcl.hash.sha512.prototype = { | |||
441 | } | 441 | } |
442 | return this; | 442 | return this; |
443 | }, | 443 | }, |
444 | 444 | ||
445 | /** | 445 | /** |
446 | * Complete hashing and output the hash value. | 446 | * Complete hashing and output the hash value. |
447 | * @return {bitArray} The hash value, an array of 16 big-endian words. | 447 | * @return {bitArray} The hash value, an array of 16 big-endian words. |
@@ -736,12 +736,12 @@ sjcl.misc.hmac = function (key, Hash) { | |||
736 | if (key.length > bs) { | 736 | if (key.length > bs) { |
737 | key = Hash.hash(key); | 737 | key = Hash.hash(key); |
738 | } | 738 | } |
739 | 739 | ||
740 | for (i=0; i<bs; i++) { | 740 | for (i=0; i<bs; i++) { |
741 | exKey[0][i] = key[i]^0x36363636; | 741 | exKey[0][i] = key[i]^0x36363636; |
742 | exKey[1][i] = key[i]^0x5C5C5C5C; | 742 | exKey[1][i] = key[i]^0x5C5C5C5C; |
743 | } | 743 | } |
744 | 744 | ||
745 | this._baseHash[0].update(exKey[0]); | 745 | this._baseHash[0].update(exKey[0]); |
746 | this._baseHash[1].update(exKey[1]); | 746 | this._baseHash[1].update(exKey[1]); |
747 | this._resultHash = new Hash(this._baseHash[0]); | 747 | this._resultHash = new Hash(this._baseHash[0]); |
@@ -804,34 +804,34 @@ sjcl.misc.hmac.prototype.digest = function () { | |||
804 | */ | 804 | */ |
805 | sjcl.misc.pbkdf2 = function (password, salt, count, length, Prff) { | 805 | sjcl.misc.pbkdf2 = function (password, salt, count, length, Prff) { |
806 | count = count || 1000; | 806 | count = count || 1000; |
807 | 807 | ||
808 | if (length < 0 || count < 0) { | 808 | if (length < 0 || count < 0) { |
809 | throw sjcl.exception.invalid("invalid params to pbkdf2"); | 809 | throw sjcl.exception.invalid("invalid params to pbkdf2"); |
810 | } | 810 | } |
811 | 811 | ||
812 | if (typeof password === "string") { | 812 | if (typeof password === "string") { |
813 | password = sjcl.codec.utf8String.toBits(password); | 813 | password = sjcl.codec.utf8String.toBits(password); |
814 | } | 814 | } |
815 | 815 | ||
816 | if (typeof salt === "string") { | 816 | if (typeof salt === "string") { |
817 | salt = sjcl.codec.utf8String.toBits(salt); | 817 | salt = sjcl.codec.utf8String.toBits(salt); |
818 | } | 818 | } |
819 | 819 | ||
820 | Prff = Prff || sjcl.misc.hmac; | 820 | Prff = Prff || sjcl.misc.hmac; |
821 | 821 | ||
822 | var prf = new Prff(password), | 822 | var prf = new Prff(password), |
823 | u, ui, i, j, k, out = [], b = sjcl.bitArray; | 823 | u, ui, i, j, k, out = [], b = sjcl.bitArray; |
824 | 824 | ||
825 | for (k = 1; 32 * out.length < (length || 1); k++) { | 825 | for (k = 1; 32 * out.length < (length || 1); k++) { |
826 | u = ui = prf.encrypt(b.concat(salt,[k])); | 826 | u = ui = prf.encrypt(b.concat(salt,[k])); |
827 | 827 | ||
828 | for (i=1; i<count; i++) { | 828 | for (i=1; i<count; i++) { |
829 | ui = prf.encrypt(ui); | 829 | ui = prf.encrypt(ui); |
830 | for (j=0; j<ui.length; j++) { | 830 | for (j=0; j<ui.length; j++) { |
831 | u[j] ^= ui[j]; | 831 | u[j] ^= ui[j]; |
832 | } | 832 | } |
833 | } | 833 | } |
834 | 834 | ||
835 | out = out.concat(u); | 835 | out = out.concat(u); |
836 | } | 836 | } |
837 | 837 | ||
@@ -890,7 +890,7 @@ sjcl.hash.sha256.prototype = { | |||
890 | * @constant | 890 | * @constant |
891 | */ | 891 | */ |
892 | blockSize: 512, | 892 | blockSize: 512, |
893 | 893 | ||
894 | /** | 894 | /** |
895 | * Reset the hash state. | 895 | * Reset the hash state. |
896 | * @return this | 896 | * @return this |
@@ -901,7 +901,7 @@ sjcl.hash.sha256.prototype = { | |||
901 | this._length = 0; | 901 | this._length = 0; |
902 | return this; | 902 | return this; |
903 | }, | 903 | }, |
904 | 904 | ||
905 | /** | 905 | /** |
906 | * Input several words to the hash. | 906 | * Input several words to the hash. |
907 | * @param {bitArray|String} data the data to hash. | 907 | * @param {bitArray|String} data the data to hash. |
@@ -919,7 +919,7 @@ sjcl.hash.sha256.prototype = { | |||
919 | } | 919 | } |
920 | return this; | 920 | return this; |
921 | }, | 921 | }, |
922 | 922 | ||
923 | /** | 923 | /** |
924 | * Complete hashing and output the hash value. | 924 | * Complete hashing and output the hash value. |
925 | * @return {bitArray} The hash value, an array of 8 big-endian words. | 925 | * @return {bitArray} The hash value, an array of 8 big-endian words. |
@@ -929,12 +929,12 @@ sjcl.hash.sha256.prototype = { | |||
929 | 929 | ||
930 | // Round out and push the buffer | 930 | // Round out and push the buffer |
931 | b = sjcl.bitArray.concat(b, [sjcl.bitArray.partial(1,1)]); | 931 | b = sjcl.bitArray.concat(b, [sjcl.bitArray.partial(1,1)]); |
932 | 932 | ||
933 | // Round out the buffer to a multiple of 16 words, less the 2 length words. | 933 | // Round out the buffer to a multiple of 16 words, less the 2 length words. |
934 | for (i = b.length + 2; i & 15; i++) { | 934 | for (i = b.length + 2; i & 15; i++) { |
935 | b.push(0); | 935 | b.push(0); |
936 | } | 936 | } |
937 | 937 | ||
938 | // append the length | 938 | // append the length |
939 | b.push(Math.floor(this._length / 0x100000000)); | 939 | b.push(Math.floor(this._length / 0x100000000)); |
940 | b.push(this._length | 0); | 940 | b.push(this._length | 0); |
@@ -955,7 +955,7 @@ sjcl.hash.sha256.prototype = { | |||
955 | /* | 955 | /* |
956 | _init:[0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19], | 956 | _init:[0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19], |
957 | */ | 957 | */ |
958 | 958 | ||
959 | /** | 959 | /** |
960 | * The SHA-256 hash key, to be precomputed. | 960 | * The SHA-256 hash key, to be precomputed. |
961 | * @private | 961 | * @private |
@@ -990,7 +990,7 @@ sjcl.hash.sha256.prototype = { | |||
990 | continue outer; | 990 | continue outer; |
991 | } | 991 | } |
992 | } | 992 | } |
993 | 993 | ||
994 | if (i<8) { | 994 | if (i<8) { |
995 | this._init[i] = frac(Math.pow(prime, 1/2)); | 995 | this._init[i] = frac(Math.pow(prime, 1/2)); |
996 | } | 996 | } |
@@ -998,13 +998,13 @@ sjcl.hash.sha256.prototype = { | |||
998 | i++; | 998 | i++; |
999 | } | 999 | } |
1000 | }, | 1000 | }, |
1001 | 1001 | ||
1002 | /** | 1002 | /** |
1003 | * Perform one cycle of SHA-256. | 1003 | * Perform one cycle of SHA-256. |
1004 | * @param {bitArray} words one block of words. | 1004 | * @param {bitArray} words one block of words. |
1005 | * @private | 1005 | * @private |
1006 | */ | 1006 | */ |
1007 | _block:function (words) { | 1007 | _block:function (words) { |
1008 | var i, tmp, a, b, | 1008 | var i, tmp, a, b, |
1009 | w = words.slice(0), | 1009 | w = words.slice(0), |
1010 | h = this._h, | 1010 | h = this._h, |
@@ -1032,13 +1032,13 @@ sjcl.hash.sha256.prototype = { | |||
1032 | } else { | 1032 | } else { |
1033 | a = w[(i+1 ) & 15]; | 1033 | a = w[(i+1 ) & 15]; |
1034 | b = w[(i+14) & 15]; | 1034 | b = w[(i+14) & 15]; |
1035 | tmp = w[i&15] = ((a>>>7 ^ a>>>18 ^ a>>>3 ^ a<<25 ^ a<<14) + | 1035 | tmp = w[i&15] = ((a>>>7 ^ a>>>18 ^ a>>>3 ^ a<<25 ^ a<<14) + |
1036 | (b>>>17 ^ b>>>19 ^ b>>>10 ^ b<<15 ^ b<<13) + | 1036 | (b>>>17 ^ b>>>19 ^ b>>>10 ^ b<<15 ^ b<<13) + |
1037 | w[i&15] + w[(i+9) & 15]) | 0; | 1037 | w[i&15] + w[(i+9) & 15]) | 0; |
1038 | } | 1038 | } |
1039 | 1039 | ||
1040 | tmp = (tmp + h7 + (h4>>>6 ^ h4>>>11 ^ h4>>>25 ^ h4<<26 ^ h4<<21 ^ h4<<7) + (h6 ^ h4&(h5^h6)) + k[i]); // | 0; | 1040 | tmp = (tmp + h7 + (h4>>>6 ^ h4>>>11 ^ h4>>>25 ^ h4<<26 ^ h4<<21 ^ h4<<7) + (h6 ^ h4&(h5^h6)) + k[i]); // | 0; |
1041 | 1041 | ||
1042 | // shift register | 1042 | // shift register |
1043 | h7 = h6; h6 = h5; h5 = h4; | 1043 | h7 = h6; h6 = h5; h5 = h4; |
1044 | h4 = h3 + tmp | 0; | 1044 | h4 = h3 + tmp | 0; |