blob: 45f3cb36e1cb24359c79f620970d354ef628167f [file] [log] [blame]
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -07001/*
2 * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
3 * in FIPS 180-2
4 * Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
5 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
6 * Distributed under the BSD License
7 * See http://pajhome.org.uk/crypt/md5 for details.
8 * Also http://anmar.eu.org/projects/jssha2/
9 */
10
11/*
12 * Configurable variables. You may need to tweak these to be compatible with
13 * the server-side, but the defaults work in most cases.
14 */
15var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
16var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
17
18/*
19 * These are the functions you'll usually want to call
20 * They take string arguments and return either hex or base-64 encoded strings
21 */
22
Axel Colin de Verdiere49c08a72012-06-05 23:04:16 -070023//@author axelcdv
24/**
25 * Computes the Sha-256 hash of the given byte array
26 * @param {byte[]}
27 * @return the hex string corresponding to the Sha-256 hash of the byte array
28 */
29function hex_sha256_from_bytes(byteArray){
30 return rstr2hex(binb2rstr(binb_sha256( byteArray2binb(byteArray), byteArray.length * 8)));
31}
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070032
33function hex_sha256(s) { return rstr2hex(rstr_sha256(str2rstr_utf8(s))); }
34function b64_sha256(s) { return rstr2b64(rstr_sha256(str2rstr_utf8(s))); }
35function any_sha256(s, e) { return rstr2any(rstr_sha256(str2rstr_utf8(s)), e); }
36function hex_hmac_sha256(k, d)
37 { return rstr2hex(rstr_hmac_sha256(str2rstr_utf8(k), str2rstr_utf8(d))); }
38function b64_hmac_sha256(k, d)
39 { return rstr2b64(rstr_hmac_sha256(str2rstr_utf8(k), str2rstr_utf8(d))); }
40function any_hmac_sha256(k, d, e)
41 { return rstr2any(rstr_hmac_sha256(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
42
43
44/*
45 function hex_sha256(s) { return rstr2hex(rstr_sha256(s)); }
46function b64_sha256(s) { return rstr2b64(rstr_sha256(s)); }
47function any_sha256(s, e) { return rstr2any(rstr_sha256(s), e); }
48function hex_hmac_sha256(k, d)
49 { return rstr2hex(rstr_hmac_sha256(str2rstr_utf8(k), d)); }
50function b64_hmac_sha256(k, d)
51 { return rstr2b64(rstr_hmac_sha256(str2rstr_utf8(k), d)); }
52function any_hmac_sha256(k, d, e)
53 { return rstr2any(rstr_hmac_sha256(str2rstr_utf8(k), d), e); }
54*/
55
56/*
57 * Perform a simple self-test to see if the VM is working
58 */
59function sha256_vm_test()
60{
61 return hex_sha256("abc").toLowerCase() ==
62 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
63}
64
Axel Colin de Verdiere49c08a72012-06-05 23:04:16 -070065/**
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070066 * Calculate the sha256 of a raw string
Axel Colin de Verdiere49c08a72012-06-05 23:04:16 -070067 * @param s: the raw string
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070068 */
69function rstr_sha256(s)
70{
71 return binb2rstr(binb_sha256(rstr2binb(s), s.length * 8));
72}
73
Axel Colin de Verdiere49c08a72012-06-05 23:04:16 -070074/**
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070075 * Calculate the HMAC-sha256 of a key and some data (raw strings)
76 */
77function rstr_hmac_sha256(key, data)
78{
79 var bkey = rstr2binb(key);
80 if(bkey.length > 16) bkey = binb_sha256(bkey, key.length * 8);
81
82 var ipad = Array(16), opad = Array(16);
83 for(var i = 0; i < 16; i++)
84 {
85 ipad[i] = bkey[i] ^ 0x36363636;
86 opad[i] = bkey[i] ^ 0x5C5C5C5C;
87 }
88
89 var hash = binb_sha256(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
90 return binb2rstr(binb_sha256(opad.concat(hash), 512 + 256));
91}
92
Axel Colin de Verdiere49c08a72012-06-05 23:04:16 -070093/**
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070094 * Convert a raw string to a hex string
95 */
96function rstr2hex(input)
97{
98 try { hexcase } catch(e) { hexcase=0; }
99 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
100 var output = "";
101 var x;
102 for(var i = 0; i < input.length; i++)
103 {
104 x = input.charCodeAt(i);
105 output += hex_tab.charAt((x >>> 4) & 0x0F)
106 + hex_tab.charAt( x & 0x0F);
107 }
108 return output;
109}
110
111/*
112 * Convert a raw string to a base-64 string
113 */
114function rstr2b64(input)
115{
116 try { b64pad } catch(e) { b64pad=''; }
117 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
118 var output = "";
119 var len = input.length;
120 for(var i = 0; i < len; i += 3)
121 {
122 var triplet = (input.charCodeAt(i) << 16)
123 | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
124 | (i + 2 < len ? input.charCodeAt(i+2) : 0);
125 for(var j = 0; j < 4; j++)
126 {
127 if(i * 8 + j * 6 > input.length * 8) output += b64pad;
128 else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
129 }
130 }
131 return output;
132}
133
134/*
135 * Convert a raw string to an arbitrary string encoding
136 */
137function rstr2any(input, encoding)
138{
139 var divisor = encoding.length;
140 var remainders = Array();
141 var i, q, x, quotient;
142
143 /* Convert to an array of 16-bit big-endian values, forming the dividend */
144 var dividend = Array(Math.ceil(input.length / 2));
145 for(i = 0; i < dividend.length; i++)
146 {
147 dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
148 }
149
150 /*
151 * Repeatedly perform a long division. The binary array forms the dividend,
152 * the length of the encoding is the divisor. Once computed, the quotient
153 * forms the dividend for the next step. We stop when the dividend is zero.
154 * All remainders are stored for later use.
155 */
156 while(dividend.length > 0)
157 {
158 quotient = Array();
159 x = 0;
160 for(i = 0; i < dividend.length; i++)
161 {
162 x = (x << 16) + dividend[i];
163 q = Math.floor(x / divisor);
164 x -= q * divisor;
165 if(quotient.length > 0 || q > 0)
166 quotient[quotient.length] = q;
167 }
168 remainders[remainders.length] = x;
169 dividend = quotient;
170 }
171
172 /* Convert the remainders to the output string */
173 var output = "";
174 for(i = remainders.length - 1; i >= 0; i--)
175 output += encoding.charAt(remainders[i]);
176
177 /* Append leading zero equivalents */
178 var full_length = Math.ceil(input.length * 8 /
179 (Math.log(encoding.length) / Math.log(2)))
180 for(i = output.length; i < full_length; i++)
181 output = encoding[0] + output;
182
183 return output;
184}
185
186/*
187 * Encode a string as utf-8.
188 * For efficiency, this assumes the input is valid utf-16.
189 */
190function str2rstr_utf8(input)
191{
192 var output = "";
193 var i = -1;
194 var x, y;
195
196 while(++i < input.length)
197 {
198 /* Decode utf-16 surrogate pairs */
199 x = input.charCodeAt(i);
200 y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
201 if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
202 {
203 x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
204 i++;
205 }
206
207 /* Encode output as utf-8 */
208 if(x <= 0x7F)
209 output += String.fromCharCode(x);
210 else if(x <= 0x7FF)
211 output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
212 0x80 | ( x & 0x3F));
213 else if(x <= 0xFFFF)
214 output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
215 0x80 | ((x >>> 6 ) & 0x3F),
216 0x80 | ( x & 0x3F));
217 else if(x <= 0x1FFFFF)
218 output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
219 0x80 | ((x >>> 12) & 0x3F),
220 0x80 | ((x >>> 6 ) & 0x3F),
221 0x80 | ( x & 0x3F));
222 }
223 return output;
224}
225
226/*
227 * Encode a string as utf-16
228 */
229function str2rstr_utf16le(input)
230{
231 var output = "";
232 for(var i = 0; i < input.length; i++)
233 output += String.fromCharCode( input.charCodeAt(i) & 0xFF,
234 (input.charCodeAt(i) >>> 8) & 0xFF);
235 return output;
236}
237
238function str2rstr_utf16be(input)
239{
240 var output = "";
241 for(var i = 0; i < input.length; i++)
242 output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
243 input.charCodeAt(i) & 0xFF);
244 return output;
245}
246
Axel Colin de Verdiere49c08a72012-06-05 23:04:16 -0700247/**
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700248 * Convert a raw string to an array of big-endian words
249 * Characters >255 have their high-byte silently ignored.
250 */
251function rstr2binb(input)
252{
253 console.log('Raw string comming is '+input);
254 var output = Array(input.length >> 2);
255 for(var i = 0; i < output.length; i++)
256 output[i] = 0;
257 for(var i = 0; i < input.length * 8; i += 8)
258 output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
259 return output;
260}
261
Axel Colin de Verdiere49c08a72012-06-05 23:04:16 -0700262/**
263 * @author axelcdv
264 * Convert a byte array to an array of big-endian words
265 * @param {byte[]} input
266 * @return the array of big-endian words
267 */
268function byteArray2binb(input){
269 console.log("Byte array coming is " + input);
270 var output = Array(input.length >> 2);
271 for(var i = 0; i < output.length; i++)
272 output[i] = 0;
273 for(var i = 0; i < input.length * 8; i += 8)
274 output[i>>5] |= (input[i / 8] & 0xFF) << (24 - i % 32);
275 return output;
276}
277
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700278/*
279 * Convert an array of big-endian words to a string
280 */
281function binb2rstr(input)
282{
283 var output = "";
284 for(var i = 0; i < input.length * 32; i += 8)
285 output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);
286 return output;
287}
288
289/*
290 * Main sha256 function, with its support functions
291 */
292function sha256_S (X, n) {return ( X >>> n ) | (X << (32 - n));}
293function sha256_R (X, n) {return ( X >>> n );}
294function sha256_Ch(x, y, z) {return ((x & y) ^ ((~x) & z));}
295function sha256_Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));}
296function sha256_Sigma0256(x) {return (sha256_S(x, 2) ^ sha256_S(x, 13) ^ sha256_S(x, 22));}
297function sha256_Sigma1256(x) {return (sha256_S(x, 6) ^ sha256_S(x, 11) ^ sha256_S(x, 25));}
298function sha256_Gamma0256(x) {return (sha256_S(x, 7) ^ sha256_S(x, 18) ^ sha256_R(x, 3));}
299function sha256_Gamma1256(x) {return (sha256_S(x, 17) ^ sha256_S(x, 19) ^ sha256_R(x, 10));}
300function sha256_Sigma0512(x) {return (sha256_S(x, 28) ^ sha256_S(x, 34) ^ sha256_S(x, 39));}
301function sha256_Sigma1512(x) {return (sha256_S(x, 14) ^ sha256_S(x, 18) ^ sha256_S(x, 41));}
302function sha256_Gamma0512(x) {return (sha256_S(x, 1) ^ sha256_S(x, 8) ^ sha256_R(x, 7));}
303function sha256_Gamma1512(x) {return (sha256_S(x, 19) ^ sha256_S(x, 61) ^ sha256_R(x, 6));}
304
305var sha256_K = new Array
306(
307 1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993,
308 -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987,
309 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522,
310 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986,
311 -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585,
312 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291,
313 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885,
314 -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344,
315 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218,
316 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872,
317 -1866530822, -1538233109, -1090935817, -965641998
318);
319
320function binb_sha256(m, l)
321{
322 var HASH = new Array(1779033703, -1150833019, 1013904242, -1521486534,
323 1359893119, -1694144372, 528734635, 1541459225);
324 var W = new Array(64);
325 var a, b, c, d, e, f, g, h;
326 var i, j, T1, T2;
327
328 /* append padding */
329 m[l >> 5] |= 0x80 << (24 - l % 32);
330 m[((l + 64 >> 9) << 4) + 15] = l;
331
332 for(i = 0; i < m.length; i += 16)
333 {
334 a = HASH[0];
335 b = HASH[1];
336 c = HASH[2];
337 d = HASH[3];
338 e = HASH[4];
339 f = HASH[5];
340 g = HASH[6];
341 h = HASH[7];
342
343 for(j = 0; j < 64; j++)
344 {
345 if (j < 16) W[j] = m[j + i];
346 else W[j] = safe_add(safe_add(safe_add(sha256_Gamma1256(W[j - 2]), W[j - 7]),
347 sha256_Gamma0256(W[j - 15])), W[j - 16]);
348
349 T1 = safe_add(safe_add(safe_add(safe_add(h, sha256_Sigma1256(e)), sha256_Ch(e, f, g)),
350 sha256_K[j]), W[j]);
351 T2 = safe_add(sha256_Sigma0256(a), sha256_Maj(a, b, c));
352 h = g;
353 g = f;
354 f = e;
355 e = safe_add(d, T1);
356 d = c;
357 c = b;
358 b = a;
359 a = safe_add(T1, T2);
360 }
361
362 HASH[0] = safe_add(a, HASH[0]);
363 HASH[1] = safe_add(b, HASH[1]);
364 HASH[2] = safe_add(c, HASH[2]);
365 HASH[3] = safe_add(d, HASH[3]);
366 HASH[4] = safe_add(e, HASH[4]);
367 HASH[5] = safe_add(f, HASH[5]);
368 HASH[6] = safe_add(g, HASH[6]);
369 HASH[7] = safe_add(h, HASH[7]);
370 }
371 return HASH;
372}
373
374function safe_add (x, y)
375{
376 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
377 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
378 return (msw << 16) | (lsw & 0xFFFF);
379}