Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: chrome/browser/resources/cryptotoken/sha256.js

Issue 249913002: FIDO U2F component extension (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge with HEAD Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // SHA256 in javascript by mschilder.
6 //
7 // SHA256 {
8 // SHA256();
9 // void reset();
10 // void update(byte[] data, opt_length);
11 // byte[32] digest();
12 // }
13
14 /** @constructor */
15 function SHA256() {
16 this._buf = new Array(64);
17 this._W = new Array(64);
18 this._pad = new Array(64);
19 this._k = [
20 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923 f82a4, 0xab1c5ed5,
21 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bd c06a7, 0xc19bf174,
22 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb 0a9dc, 0x76f988da,
23 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06c a6351, 0x14292967,
24 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c 2c92e, 0x92722c85,
25 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40 e3585, 0x106aa070,
26 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9 cca4f, 0x682e6ff3,
27 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef 9a3f7, 0xc67178f2];
28
29 this._pad[0] = 0x80;
30 for (var i = 1; i < 64; ++i) this._pad[i] = 0;
31
32 this.reset();
33 }
34
35 SHA256.prototype.reset = function() {
36 this._chain = [
37 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f 83d9ab, 0x5be0cd19];
38
39 this._inbuf = 0;
40 this._total = 0;
41 };
42
43 SHA256.prototype._compress = function(buf) {
44 var W = this._W;
45 var k = this._k;
46
47 function _rotr(w, r) { return ((w << (32 - r)) | (w >>> r)); };
48
49 // get 16 big endian words
50 for (var i = 0; i < 64; i += 4) {
51 var w = (buf[i] << 24) | (buf[i + 1] << 16) | (buf[i + 2] << 8) | (buf[i + 3 ]);
52 W[i / 4] = w;
53 }
54
55 // expand to 64 words
56 for (var i = 16; i < 64; ++i) {
57 var s0 = _rotr(W[i - 15], 7) ^ _rotr(W[i - 15], 18) ^ (W[i - 15] >>> 3);
58 var s1 = _rotr(W[i - 2], 17) ^ _rotr(W[i - 2], 19) ^ (W[i - 2] >>> 10);
59 W[i] = (W[i - 16] + s0 + W[i - 7] + s1) & 0xffffffff;
60 }
61
62 var A = this._chain[0];
63 var B = this._chain[1];
64 var C = this._chain[2];
65 var D = this._chain[3];
66 var E = this._chain[4];
67 var F = this._chain[5];
68 var G = this._chain[6];
69 var H = this._chain[7];
70
71 for (var i = 0; i < 64; ++i) {
72 var S0 = _rotr(A, 2) ^ _rotr(A, 13) ^ _rotr(A, 22);
73 var maj = (A & B) ^ (A & C) ^ (B & C);
74 var t2 = (S0 + maj) & 0xffffffff;
75 var S1 = _rotr(E, 6) ^ _rotr(E, 11) ^ _rotr(E, 25);
76 var ch = (E & F) ^ ((~E) & G);
77 var t1 = (H + S1 + ch + k[i] + W[i]) & 0xffffffff;
78
79 H = G;
80 G = F;
81 F = E;
82 E = (D + t1) & 0xffffffff;
83 D = C;
84 C = B;
85 B = A;
86 A = (t1 + t2) & 0xffffffff;
87 }
88
89 this._chain[0] += A;
90 this._chain[1] += B;
91 this._chain[2] += C;
92 this._chain[3] += D;
93 this._chain[4] += E;
94 this._chain[5] += F;
95 this._chain[6] += G;
96 this._chain[7] += H;
97 };
98
99 SHA256.prototype.update = function(bytes, opt_length) {
100 if (!opt_length) opt_length = bytes.length;
101
102 this._total += opt_length;
103 for (var n = 0; n < opt_length; ++n) {
104 this._buf[this._inbuf++] = bytes[n];
105 if (this._inbuf == 64) {
106 this._compress(this._buf);
107 this._inbuf = 0;
108 }
109 }
110 };
111
112 SHA256.prototype.updateRange = function(bytes, start, end) {
113 this._total += (end - start);
114 for (var n = start; n < end; ++n) {
115 this._buf[this._inbuf++] = bytes[n];
116 if (this._inbuf == 64) {
117 this._compress(this._buf);
118 this._inbuf = 0;
119 }
120 }
121 };
122
123 /**
124 * Optionally update the hash with additional arguments, and return the
125 * resulting hash value.
126 * @param {...*} var_args
127 * @return the SHA256 hash value.
128 */
129 SHA256.prototype.digest = function(var_args) {
130 for (var i = 0; i < arguments.length; ++i)
131 this.update(arguments[i]);
132
133 var digest = new Array(32);
134 var totalBits = this._total * 8;
135
136 // add pad 0x80 0x00*
137 if (this._inbuf < 56)
138 this.update(this._pad, 56 - this._inbuf);
139 else
140 this.update(this._pad, 64 - (this._inbuf - 56));
141
142 // add # bits, big endian
143 for (var i = 63; i >= 56; --i) {
144 this._buf[i] = totalBits & 255;
145 totalBits >>>= 8;
146 }
147
148 this._compress(this._buf);
149
150 var n = 0;
151 for (var i = 0; i < 8; ++i)
152 for (var j = 24; j >= 0; j -= 8)
153 digest[n++] = (this._chain[i] >> j) & 255;
154
155 return digest;
156 };
OLDNEW
« no previous file with comments | « chrome/browser/resources/cryptotoken/requestqueue.js ('k') | chrome/browser/resources/cryptotoken/signer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698