]>
git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blob - src/js/sjcl-bip39.js
1 // Select components from sjcl to suit the crypto operations bip39 requires.
5 /** @fileOverview Javascript cryptography implementation.
7 * Crush to remove comments, shorten variable names and
8 * generally reduce transmission size.
11 * @author Mike Hamburg
16 /*jslint indent: 2, bitwise: false, nomen: false, plusplus: false, white: false, regexp: false */
17 /*global document, window, escape, unescape, module, require, Uint32Array */
19 /** @namespace The Stanford Javascript Crypto Library, top-level namespace. */
21 /** @namespace Symmetric ciphers. */
24 /** @namespace Hash functions. Right now only SHA256 is implemented. */
27 /** @namespace Key exchange functions. Right now only SRP is implemented. */
30 /** @namespace Block cipher modes of operation. */
33 /** @namespace Miscellaneous. HMAC and PBKDF2. */
37 * @namespace Bit array encoders and decoders.
40 * The members of this namespace are functions which translate between
41 * SJCL's bitArrays and other objects (usually strings). Because it
42 * isn't always clear which direction is encoding and which is decoding,
43 * the method names are "fromBits" and "toBits".
47 /** @namespace Exceptions. */
49 /** @constructor Ciphertext is corrupt. */
50 corrupt: function(message
) {
51 this.toString = function() { return "CORRUPT: "+this.message
; };
52 this.message
= message
;
55 /** @constructor Invalid parameter. */
56 invalid: function(message
) {
57 this.toString = function() { return "INVALID: "+this.message
; };
58 this.message
= message
;
61 /** @constructor Bug or missing feature in SJCL. @constructor */
62 bug: function(message
) {
63 this.toString = function() { return "BUG: "+this.message
; };
64 this.message
= message
;
67 /** @constructor Something isn't ready. */
68 notReady: function(message
) {
69 this.toString = function() { return "NOT READY: "+this.message
; };
70 this.message
= message
;
75 if(typeof module
!== 'undefined' && module
.exports
){
76 module
.exports
= sjcl
;
78 if (typeof define
=== "function") {
79 define([], function () {
87 /** @fileOverview Arrays of bits, encoded as arrays of Numbers.
90 * @author Mike Hamburg
94 /** @namespace Arrays of bits, encoded as arrays of Numbers.
98 * These objects are the currency accepted by SJCL's crypto functions.
102 * Most of our crypto primitives operate on arrays of 4-byte words internally,
103 * but many of them can take arguments that are not a multiple of 4 bytes.
104 * This library encodes arrays of bits (whose size need not be a multiple of 8
105 * bits) as arrays of 32-bit words. The bits are packed, big-endian, into an
106 * array of words, 32 bits at a time. Since the words are double-precision
107 * floating point numbers, they fit some extra data. We use this (in a private,
108 * possibly-changing manner) to encode the number of bits actually present
109 * in the last word of the array.
113 * Because bitwise ops clear this out-of-band data, these arrays can be passed
114 * to ciphers like AES which want arrays of words.
119 * Array slices in units of bits.
120 * @param {bitArray} a The array to slice.
121 * @param {Number} bstart The offset to the start of the slice, in bits.
122 * @param {Number} bend The offset to the end of the slice, in bits. If this is undefined,
123 * slice until the end of the array.
124 * @return {bitArray} The requested slice.
126 bitSlice: function (a
, bstart
, bend
) {
127 a
= sjcl
.bitArray
._shiftRight(a
.slice(bstart
/32), 32 - (bstart
& 31)).slice(1);
128 return (bend
=== undefined) ? a : sjcl
.bitArray
.clamp(a
, bend
-bstart
);
132 * Extract a number packed into a bit array.
133 * @param {bitArray} a The array to slice.
134 * @param {Number} bstart The offset to the start of the slice, in bits.
135 * @param {Number} length The length of the number to extract.
136 * @return {Number} The requested slice.
138 extract: function(a
, bstart
, blength
) {
139 // FIXME: this Math.floor is not necessary at all, but for some reason
140 // seems to suppress a bug in the Chromium JIT.
141 var x
, sh
= Math
.floor((-bstart
-blength
) & 31);
142 if ((bstart
+ blength
- 1 ^ bstart
) & -32) {
143 // it crosses a boundary
144 x
= (a
[bstart
/32|0] << (32 - sh
)) ^ (a
[bstart
/32+1|0] >>> sh
);
146 // within a single word
147 x
= a
[bstart
/32|0] >>> sh
;
149 return x
& ((1<<blength
) - 1);
153 * Concatenate two bit arrays.
154 * @param {bitArray} a1 The first array.
155 * @param {bitArray} a2 The second array.
156 * @return {bitArray} The concatenation of a1 and a2.
158 concat: function (a1
, a2
) {
159 if (a1
.length
=== 0 || a2
.length
=== 0) {
160 return a1
.concat(a2
);
163 var last
= a1
[a1
.length
-1], shift
= sjcl
.bitArray
.getPartial(last
);
165 return a1
.concat(a2
);
167 return sjcl
.bitArray
._shiftRight(a2
, shift
, last
|0, a1
.slice(0,a1
.length
-1));
172 * Find the length of an array of bits.
173 * @param {bitArray} a The array.
174 * @return {Number} The length of a, in bits.
176 bitLength: function (a
) {
178 if (l
=== 0) { return 0; }
180 return (l
-1) * 32 + sjcl
.bitArray
.getPartial(x
);
185 * @param {bitArray} a The array.
186 * @param {Number} len The length to truncate to, in bits.
187 * @return {bitArray} A new array, truncated to len bits.
189 clamp: function (a
, len
) {
190 if (a
.length
* 32 < len
) { return a
; }
191 a
= a
.slice(0, Math
.ceil(len
/ 32));
195 a
[l
-1] = sjcl
.bitArray
.partial(len
, a
[l
-1] & 0x80000000 >> (len
-1), 1);
201 * Make a partial word for a bit array.
202 * @param {Number} len The number of bits in the word.
203 * @param {Number} x The bits.
204 * @param {Number} [0] _end Pass 1 if x has already been shifted to the high side.
205 * @return {Number} The partial word.
207 partial: function (len
, x
, _end
) {
208 if (len
=== 32) { return x
; }
209 return (_end
? x
|0 : x
<< (32-len
)) + len
* 0x10000000000;
213 * Get the number of bits used by a partial word.
214 * @param {Number} x The partial word.
215 * @return {Number} The number of bits used by the partial word.
217 getPartial: function (x
) {
218 return Math
.round(x
/0x10000000000) || 32;
222 * Compare two arrays for equality in a predictable amount of time.
223 * @param {bitArray} a The first array.
224 * @param {bitArray} b The second array.
225 * @return {boolean} true if a == b; false otherwise.
227 equal: function (a
, b
) {
228 if (sjcl
.bitArray
.bitLength(a
) !== sjcl
.bitArray
.bitLength(b
)) {
232 for (i
=0; i
<a
.length
; i
++) {
238 /** Shift an array right.
239 * @param {bitArray} a The array to shift.
240 * @param {Number} shift The number of bits to shift.
241 * @param {Number} [carry=0] A byte to carry in
242 * @param {bitArray} [out=[]] An array to prepend to the output.
245 _shiftRight: function (a
, shift
, carry
, out
) {
246 var i
, last2
=0, shift2
;
247 if (out
=== undefined) { out
= []; }
249 for (; shift
>= 32; shift
-= 32) {
254 return out
.concat(a
);
257 for (i
=0; i
<a
.length
; i
++) {
258 out
.push(carry
| a
[i
]>>>shift
);
259 carry
= a
[i
] << (32-shift
);
261 last2
= a
.length
? a
[a
.length
-1] : 0;
262 shift2
= sjcl
.bitArray
.getPartial(last2
);
263 out
.push(sjcl
.bitArray
.partial(shift
+shift2
& 31, (shift
+ shift2
> 32) ? carry : out
.pop(),1));
267 /** xor a block of 4 words together.
270 _xor4: function(x
,y
) {
271 return [x
[0]^y
[0],x
[1]^y
[1],x
[2]^y
[2],x
[3]^y
[3]];
274 /** byteswap a word array inplace.
275 * (does not handle partial words)
276 * @param {sjcl.bitArray} a word array
277 * @return {sjcl.bitArray} byteswapped array
279 byteswapM: function(a
) {
280 var i
, v
, m
= 0xff00;
281 for (i
= 0; i
< a
.length
; ++i
) {
283 a
[i
] = (v
>>> 24) | ((v
>>> 8) & m
) | ((v
& m
) << 8) | (v
<< 24);
292 /** @fileOverview Bit array codec implementations.
294 * @author Emily Stark
295 * @author Mike Hamburg
299 /** @namespace UTF-8 strings */
300 sjcl
.codec
.utf8String
= {
301 /** Convert from a bitArray to a UTF-8 string. */
302 fromBits: function (arr
) {
303 var out
= "", bl
= sjcl
.bitArray
.bitLength(arr
), i
, tmp
;
304 for (i
=0; i
<bl
/8; i
++) {
308 out
+= String
.fromCharCode(tmp
>>> 24);
311 return decodeURIComponent(escape(out
));
314 /** Convert from a UTF-8 string to a bitArray. */
315 toBits: function (str
) {
316 str
= unescape(encodeURIComponent(str
));
317 var out
= [], i
, tmp
=0;
318 for (i
=0; i
<str
.length
; i
++) {
319 tmp
= tmp
<< 8 | str
.charCodeAt(i
);
326 out
.push(sjcl
.bitArray
.partial(8*(i
&3), tmp
));
335 /** @fileOverview Bit array codec implementations.
337 * @author Emily Stark
338 * @author Mike Hamburg
342 /** @namespace Hexadecimal */
344 /** Convert from a bitArray to a hex string. */
345 fromBits: function (arr
) {
347 for (i
=0; i
<arr
.length
; i
++) {
348 out
+= ((arr
[i
]|0)+0xF00000000000).toString(16).substr(4);
350 return out
.substr(0, sjcl
.bitArray
.bitLength(arr
)/4);//.replace(/(.{8})/g
, "$1 ");
352 /** Convert from a hex string to a bitArray. */
353 toBits: function (str
) {
355 str
= str
.replace(/\s|0x/g, "");
357 str
= str
+ "00000000";
358 for (i
=0; i
<str
.length
; i
+=8) {
359 out
.push(parseInt(str
.substr(i
,8),16)^0);
361 return sjcl
.bitArray
.clamp(out
, len
*4);
368 /** @fileOverview Javascript SHA-512 implementation.
370 * This implementation was written for CryptoJS by Jeff Mott and adapted for
371 * SJCL by Stefan Thomas.
373 * CryptoJS (c) 2009–2012 by Jeff Mott. All rights reserved.
374 * Released with New BSD License
376 * @author Emily Stark
377 * @author Mike Hamburg
380 * @author Stefan Thomas
384 * Context for a SHA-512 operation in progress.
386 * @class Secure Hash Algorithm, 512 bits.
388 sjcl
.hash
.sha512 = function (hash
) {
389 if (!this._key
[0]) { this._precompute(); }
391 this._h
= hash
._h
.slice(0);
392 this._buffer
= hash
._buffer
.slice(0);
393 this._length
= hash
._length
;
400 * Hash a string or an array of words.
402 * @param {bitArray|String} data the data to hash.
403 * @return {bitArray} The hash value, an array of 16 big-endian words.
405 sjcl
.hash
.sha512
.hash = function (data
) {
406 return (new sjcl
.hash
.sha512()).update(data
).finalize();
409 sjcl
.hash
.sha512
.prototype = {
411 * The hash's block size, in bits.
417 * Reset the hash state.
421 this._h
= this._init
.slice(0);
428 * Input several words to the hash.
429 * @param {bitArray|String} data the data to hash.
432 update: function (data
) {
433 if (typeof data
=== "string") {
434 data
= sjcl
.codec
.utf8String
.toBits(data
);
436 var i
, b
= this._buffer
= sjcl
.bitArray
.concat(this._buffer
, data
),
438 nl
= this._length
= ol
+ sjcl
.bitArray
.bitLength(data
);
439 for (i
= 1024+ol
& -1024; i
<= nl
; i
+= 1024) {
440 this._block(b
.splice(0,32));
446 * Complete hashing and output the hash value.
447 * @return {bitArray} The hash value, an array of 16 big-endian words.
449 finalize:function () {
450 var i
, b
= this._buffer
, h
= this._h
;
452 // Round out and push the buffer
453 b
= sjcl
.bitArray
.concat(b
, [sjcl
.bitArray
.partial(1,1)]);
455 // Round out the buffer to a multiple of 32 words, less the 4 length words.
456 for (i
= b
.length
+ 4; i
& 31; i
++) {
463 b
.push(Math
.floor(this._length
/ 0x100000000));
464 b
.push(this._length
| 0);
467 this._block(b
.splice(0,32));
475 * The SHA-512 initialization vector, to be precomputed.
481 * Least significant 24 bits of SHA512 initialization values.
483 * Javascript only has 53 bits of precision, so we compute the 40 most
484 * significant bits and add the remaining 24 bits as constants.
488 _initr: [ 0xbcc908, 0xcaa73b, 0x94f82b, 0x1d36f1, 0xe682d1, 0x3e6c1f, 0x41bd6b, 0x7e2179 ],
492 [0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
493 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179],
497 * The SHA-512 hash key, to be precomputed.
503 * Least significant 24 bits of SHA512 key values.
507 [0x28ae22, 0xef65cd, 0x4d3b2f, 0x89dbbc, 0x48b538, 0x05d019, 0x194f9b, 0x6d8118,
508 0x030242, 0x706fbe, 0xe4b28c, 0xffb4e2, 0x7b896f, 0x1696b1, 0xc71235, 0x692694,
509 0xf14ad2, 0x4f25e3, 0x8cd5b5, 0xac9c65, 0x2b0275, 0xa6e483, 0x41fbd4, 0x1153b5,
510 0x66dfab, 0xb43210, 0xfb213f, 0xef0ee4, 0xa88fc2, 0x0aa725, 0x03826f, 0x0e6e70,
511 0xd22ffc, 0x26c926, 0xc42aed, 0x95b3df, 0xaf63de, 0x77b2a8, 0xedaee6, 0x82353b,
512 0xf10364, 0x423001, 0xf89791, 0x54be30, 0xef5218, 0x65a910, 0x71202a, 0xbbd1b8,
513 0xd2d0c8, 0x41ab53, 0x8eeb99, 0x9b48a8, 0xc95a63, 0x418acb, 0x63e373, 0xb2b8a3,
514 0xefb2fc, 0x172f60, 0xf0ab72, 0x6439ec, 0x631e28, 0x82bde9, 0xc67915, 0x72532b,
515 0x26619c, 0xc0c207, 0xe0eb1e, 0x6ed178, 0x176fba, 0xc898a6, 0xf90dae, 0x1c471b,
516 0x047d84, 0xc72493, 0xc9bebc, 0x100d4c, 0x3e42b6, 0x657e2a, 0xd6faec, 0x475817],
520 [0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
521 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
522 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe, 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
523 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
524 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
525 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483, 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
526 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
527 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
528 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926, 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
529 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
530 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
531 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910, 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
532 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
533 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
534 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60, 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
535 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
536 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
537 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6, 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
538 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
539 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817],
543 * Function to precompute _init and _key.
546 _precompute: function () {
547 // XXX: This code is for precomputing the SHA256 constants, change for
548 // SHA512 and re-enable.
549 var i
= 0, prime
= 2, factor
;
551 function frac(x
) { return (x
-Math
.floor(x
)) * 0x100000000 | 0; }
552 function frac2(x
) { return (x
-Math
.floor(x
)) * 0x10000000000 & 0xff; }
554 outer: for (; i
<80; prime
++) {
555 for (factor
=2; factor
*factor
<= prime
; factor
++) {
556 if (prime
% factor
=== 0) {
563 this._init
[i
*2] = frac(Math
.pow(prime
, 1/2));
564 this._init
[i
*2+1] = (frac2(Math
.pow(prime
, 1/2)) << 24) | this._initr
[i
];
566 this._key
[i
*2] = frac(Math
.pow(prime
, 1/3));
567 this._key
[i
*2+1] = (frac2(Math
.pow(prime
, 1/3)) << 24) | this._keyr
[i
];
573 * Perform one cycle of SHA-512.
574 * @param {bitArray} words one block of words.
577 _block:function (words
) {
582 h0h
= h
[ 0], h0l
= h
[ 1], h1h
= h
[ 2], h1l
= h
[ 3],
583 h2h
= h
[ 4], h2l
= h
[ 5], h3h
= h
[ 6], h3l
= h
[ 7],
584 h4h
= h
[ 8], h4l
= h
[ 9], h5h
= h
[10], h5l
= h
[11],
585 h6h
= h
[12], h6l
= h
[13], h7h
= h
[14], h7l
= h
[15];
588 var ah
= h0h
, al
= h0l
, bh
= h1h
, bl
= h1l
,
589 ch
= h2h
, cl
= h2l
, dh
= h3h
, dl
= h3l
,
590 eh
= h4h
, el
= h4l
, fh
= h5h
, fl
= h5l
,
591 gh
= h6h
, gl
= h6l
, hh
= h7h
, hl
= h7l
;
593 for (i
=0; i
<80; i
++) {
594 // load up the input word for this round
600 var gamma0xh
= w
[(i
-15) * 2];
601 var gamma0xl
= w
[(i
-15) * 2 + 1];
603 ((gamma0xl
<< 31) | (gamma0xh
>>> 1)) ^
604 ((gamma0xl
<< 24) | (gamma0xh
>>> 8)) ^
607 ((gamma0xh
<< 31) | (gamma0xl
>>> 1)) ^
608 ((gamma0xh
<< 24) | (gamma0xl
>>> 8)) ^
609 ((gamma0xh
<< 25) | (gamma0xl
>>> 7));
612 var gamma1xh
= w
[(i
-2) * 2];
613 var gamma1xl
= w
[(i
-2) * 2 + 1];
615 ((gamma1xl
<< 13) | (gamma1xh
>>> 19)) ^
616 ((gamma1xh
<< 3) | (gamma1xl
>>> 29)) ^
619 ((gamma1xh
<< 13) | (gamma1xl
>>> 19)) ^
620 ((gamma1xl
<< 3) | (gamma1xh
>>> 29)) ^
621 ((gamma1xh
<< 26) | (gamma1xl
>>> 6));
624 var wr7h
= w
[(i
-7) * 2];
625 var wr7l
= w
[(i
-7) * 2 + 1];
627 var wr16h
= w
[(i
-16) * 2];
628 var wr16l
= w
[(i
-16) * 2 + 1];
630 // W(round) = gamma0 + W(round - 7) + gamma1 + W(round - 16)
631 wrl
= gamma0l
+ wr7l
;
632 wrh
= gamma0h
+ wr7h
+ ((wrl
>>> 0) < (gamma0l
>>> 0) ? 1 : 0);
634 wrh
+= gamma1h
+ ((wrl
>>> 0) < (gamma1l
>>> 0) ? 1 : 0);
636 wrh
+= wr16h
+ ((wrl
>>> 0) < (wr16l
>>> 0) ? 1 : 0);
640 w
[i
*2 + 1] = wrl
|= 0;
643 var chh
= (eh
& fh
) ^ (~eh
& gh
);
644 var chl
= (el
& fl
) ^ (~el
& gl
);
647 var majh
= (ah
& bh
) ^ (ah
& ch
) ^ (bh
& ch
);
648 var majl
= (al
& bl
) ^ (al
& cl
) ^ (bl
& cl
);
651 var sigma0h
= ((al
<< 4) | (ah
>>> 28)) ^ ((ah
<< 30) | (al
>>> 2)) ^ ((ah
<< 25) | (al
>>> 7));
652 var sigma0l
= ((ah
<< 4) | (al
>>> 28)) ^ ((al
<< 30) | (ah
>>> 2)) ^ ((al
<< 25) | (ah
>>> 7));
655 var sigma1h
= ((el
<< 18) | (eh
>>> 14)) ^ ((el
<< 14) | (eh
>>> 18)) ^ ((eh
<< 23) | (el
>>> 9));
656 var sigma1l
= ((eh
<< 18) | (el
>>> 14)) ^ ((eh
<< 14) | (el
>>> 18)) ^ ((el
<< 23) | (eh
>>> 9));
662 // t1 = h + sigma1 + ch + K(round) + W(round)
663 var t1l
= hl
+ sigma1l
;
664 var t1h
= hh
+ sigma1h
+ ((t1l
>>> 0) < (hl
>>> 0) ? 1 : 0);
666 t1h
+= chh
+ ((t1l
>>> 0) < (chl
>>> 0) ? 1 : 0);
668 t1h
+= krh
+ ((t1l
>>> 0) < (krl
>>> 0) ? 1 : 0);
669 t1l
= t1l
+ wrl
|0; // FF32..FF34 perf issue https://bugzilla.mozilla.org/show_bug.cgi?id=1054972
670 t1h
+= wrh
+ ((t1l
>>> 0) < (wrl
>>> 0) ? 1 : 0);
673 var t2l
= sigma0l
+ majl
;
674 var t2h
= sigma0h
+ majh
+ ((t2l
>>> 0) < (sigma0l
>>> 0) ? 1 : 0);
676 // Update working variables
684 eh
= (dh
+ t1h
+ ((el
>>> 0) < (dl
>>> 0) ? 1 : 0)) | 0;
691 al
= (t1l
+ t2l
) | 0;
692 ah
= (t1h
+ t2h
+ ((al
>>> 0) < (t1l
>>> 0) ? 1 : 0)) | 0;
696 h0l
= h
[1] = (h0l
+ al
) | 0;
697 h
[0] = (h0h
+ ah
+ ((h0l
>>> 0) < (al
>>> 0) ? 1 : 0)) | 0;
698 h1l
= h
[3] = (h1l
+ bl
) | 0;
699 h
[2] = (h1h
+ bh
+ ((h1l
>>> 0) < (bl
>>> 0) ? 1 : 0)) | 0;
700 h2l
= h
[5] = (h2l
+ cl
) | 0;
701 h
[4] = (h2h
+ ch
+ ((h2l
>>> 0) < (cl
>>> 0) ? 1 : 0)) | 0;
702 h3l
= h
[7] = (h3l
+ dl
) | 0;
703 h
[6] = (h3h
+ dh
+ ((h3l
>>> 0) < (dl
>>> 0) ? 1 : 0)) | 0;
704 h4l
= h
[9] = (h4l
+ el
) | 0;
705 h
[8] = (h4h
+ eh
+ ((h4l
>>> 0) < (el
>>> 0) ? 1 : 0)) | 0;
706 h5l
= h
[11] = (h5l
+ fl
) | 0;
707 h
[10] = (h5h
+ fh
+ ((h5l
>>> 0) < (fl
>>> 0) ? 1 : 0)) | 0;
708 h6l
= h
[13] = (h6l
+ gl
) | 0;
709 h
[12] = (h6h
+ gh
+ ((h6l
>>> 0) < (gl
>>> 0) ? 1 : 0)) | 0;
710 h7l
= h
[15] = (h7l
+ hl
) | 0;
711 h
[14] = (h7h
+ hh
+ ((h7l
>>> 0) < (hl
>>> 0) ? 1 : 0)) | 0;
718 /** @fileOverview HMAC implementation.
720 * @author Emily Stark
721 * @author Mike Hamburg
725 /** HMAC with the specified hash function.
727 * @param {bitArray} key the key for HMAC.
728 * @param {Object} [hash=sjcl.hash.sha256] The hash function to use.
730 sjcl
.misc
.hmac = function (key
, Hash
) {
731 this._hash
= Hash
= Hash
|| sjcl
.hash
.sha256
;
732 var exKey
= [[],[]], i
,
733 bs
= Hash
.prototype.blockSize
/ 32;
734 this._baseHash
= [new Hash(), new Hash()];
736 if (key
.length
> bs
) {
737 key
= Hash
.hash(key
);
740 for (i
=0; i
<bs
; i
++) {
741 exKey
[0][i
] = key
[i
]^0x36363636;
742 exKey
[1][i
] = key
[i
]^0x5C5C5C5C;
745 this._baseHash
[0].update(exKey
[0]);
746 this._baseHash
[1].update(exKey
[1]);
747 this._resultHash
= new Hash(this._baseHash
[0]);
750 /** HMAC with the specified hash function. Also called encrypt since it's a prf.
751 * @param {bitArray|String} data The data to mac.
753 sjcl
.misc
.hmac
.prototype.encrypt
= sjcl
.misc
.hmac
.prototype.mac = function (data
) {
754 if (!this._updated
) {
756 return this.digest(data
);
758 throw new sjcl
.exception
.invalid("encrypt on already updated hmac called!");
762 sjcl
.misc
.hmac
.prototype.reset = function () {
763 this._resultHash
= new this._hash(this._baseHash
[0]);
764 this._updated
= false;
767 sjcl
.misc
.hmac
.prototype.update = function (data
) {
768 this._updated
= true;
769 this._resultHash
.update(data
);
772 sjcl
.misc
.hmac
.prototype.digest = function () {
773 var w
= this._resultHash
.finalize(), result
= new (this._hash
)(this._baseHash
[1]).update(w
).finalize();
784 /** @fileOverview Password-based key-derivation function, version 2.0.
786 * @author Emily Stark
787 * @author Mike Hamburg
791 /** Password-Based Key-Derivation Function, version 2.0.
793 * Generate keys from passwords using PBKDF2-HMAC-SHA256.
795 * This is the method specified by RSA's PKCS #5 standard.
797 * @param {bitArray|String} password The password.
798 * @param {bitArray|String} salt The salt. Should have lots of entropy.
799 * @param {Number} [count=1000] The number of iterations. Higher numbers make the function slower but more secure.
800 * @param {Number} [length] The length of the derived key. Defaults to the
801 output size of the hash function.
802 * @param {Object} [Prff=sjcl.misc.hmac] The pseudorandom function family.
803 * @return {bitArray} the derived key.
805 sjcl
.misc
.pbkdf2 = function (password
, salt
, count
, length
, Prff
) {
806 count
= count
|| 1000;
808 if (length
< 0 || count
< 0) {
809 throw sjcl
.exception
.invalid("invalid params to pbkdf2");
812 if (typeof password
=== "string") {
813 password
= sjcl
.codec
.utf8String
.toBits(password
);
816 if (typeof salt
=== "string") {
817 salt
= sjcl
.codec
.utf8String
.toBits(salt
);
820 Prff
= Prff
|| sjcl
.misc
.hmac
;
822 var prf
= new Prff(password
),
823 u
, ui
, i
, j
, k
, out
= [], b
= sjcl
.bitArray
;
825 for (k
= 1; 32 * out
.length
< (length
|| 1); k
++) {
826 u
= ui
= prf
.encrypt(b
.concat(salt
,[k
]));
828 for (i
=1; i
<count
; i
++) {
829 ui
= prf
.encrypt(ui
);
830 for (j
=0; j
<ui
.length
; j
++) {
838 if (length
) { out
= b
.clamp(out
, length
); }
846 /** @fileOverview Javascript SHA-256 implementation.
848 * An older version of this implementation is available in the public
849 * domain, but this one is (c) Emily Stark, Mike Hamburg, Dan Boneh,
850 * Stanford University 2008-2010 and BSD-licensed for liability
853 * Special thanks to Aldo Cortesi for pointing out several bugs in
856 * @author Emily Stark
857 * @author Mike Hamburg
862 * Context for a SHA-256 operation in progress.
864 * @class Secure Hash Algorithm, 256 bits.
866 sjcl
.hash
.sha256 = function (hash
) {
867 if (!this._key
[0]) { this._precompute(); }
869 this._h
= hash
._h
.slice(0);
870 this._buffer
= hash
._buffer
.slice(0);
871 this._length
= hash
._length
;
878 * Hash a string or an array of words.
880 * @param {bitArray|String} data the data to hash.
881 * @return {bitArray} The hash value, an array of 16 big-endian words.
883 sjcl
.hash
.sha256
.hash = function (data
) {
884 return (new sjcl
.hash
.sha256()).update(data
).finalize();
887 sjcl
.hash
.sha256
.prototype = {
889 * The hash's block size, in bits.
895 * Reset the hash state.
899 this._h
= this._init
.slice(0);
906 * Input several words to the hash.
907 * @param {bitArray|String} data the data to hash.
910 update: function (data
) {
911 if (typeof data
=== "string") {
912 data
= sjcl
.codec
.utf8String
.toBits(data
);
914 var i
, b
= this._buffer
= sjcl
.bitArray
.concat(this._buffer
, data
),
916 nl
= this._length
= ol
+ sjcl
.bitArray
.bitLength(data
);
917 for (i
= 512+ol
& -512; i
<= nl
; i
+= 512) {
918 this._block(b
.splice(0,16));
924 * Complete hashing and output the hash value.
925 * @return {bitArray} The hash value, an array of 8 big-endian words.
927 finalize:function () {
928 var i
, b
= this._buffer
, h
= this._h
;
930 // Round out and push the buffer
931 b
= sjcl
.bitArray
.concat(b
, [sjcl
.bitArray
.partial(1,1)]);
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
++) {
939 b
.push(Math
.floor(this._length
/ 0x100000000));
940 b
.push(this._length
| 0);
943 this._block(b
.splice(0,16));
951 * The SHA-256 initialization vector, to be precomputed.
956 _init:[0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19],
960 * The SHA-256 hash key, to be precomputed.
966 [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
967 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
968 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
969 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
970 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
971 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
972 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
973 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2],
978 * Function to precompute _init and _key.
981 _precompute: function () {
982 var i
= 0, prime
= 2, factor
;
984 function frac(x
) { return (x
-Math
.floor(x
)) * 0x100000000 | 0; }
986 outer: for (; i
<64; prime
++) {
987 for (factor
=2; factor
*factor
<= prime
; factor
++) {
988 if (prime
% factor
=== 0) {
995 this._init
[i
] = frac(Math
.pow(prime
, 1/2));
997 this._key
[i
] = frac(Math
.pow(prime
, 1/3));
1003 * Perform one cycle of SHA-256.
1004 * @param {bitArray} words one block of words.
1007 _block:function (words
) {
1012 h0
= h
[0], h1
= h
[1], h2
= h
[2], h3
= h
[3],
1013 h4
= h
[4], h5
= h
[5], h6
= h
[6], h7
= h
[7];
1015 /* Rationale for placement of |0 :
1016 * If a value can overflow is original 32 bits by a factor of more than a few
1017 * million (2^23 ish), there is a possibility that it might overflow the
1018 * 53-bit mantissa and lose precision.
1020 * To avoid this, we clamp back to 32 bits by |'ing with 0 on any value that
1021 * propagates around the loop, and on the hash state h[]. I don't believe
1022 * that the clamps on h4 and on h0 are strictly necessary, but it's close
1023 * (for h4 anyway), and better safe than sorry.
1025 * The clamps on h[] are necessary for the output to be correct even in the
1026 * common case and for short inputs.
1028 for (i
=0; i
<64; i
++) {
1029 // load up the input word for this round
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) +
1037 w
[i
&15] + w
[(i
+9) & 15]) | 0;
1040 tmp
= (tmp
+ h7
+ (h4
>>>6 ^ h4
>>>11 ^ h4
>>>25 ^ h4
<<26 ^ h4
<<21 ^ h4
<<7) + (h6
^ h4
&(h5
^h6
)) + k
[i
]); // | 0;
1043 h7
= h6
; h6
= h5
; h5
= h4
;
1045 h3
= h2
; h2
= h1
; h1
= h0
;
1047 h0
= (tmp
+ ((h1
&h2
) ^ (h3
&(h1
^h2
))) + (h1
>>>2 ^ h1
>>>13 ^ h1
>>>22 ^ h1
<<30 ^ h1
<<19 ^ h1
<<10)) | 0;