sha2.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Base class for SHA-2 cryptographic hash.
  16. *
  17. * Variable names follow the notation in FIPS PUB 180-3:
  18. * http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf.
  19. *
  20. * Some code similar to SHA1 are borrowed from sha1.js written by mschilder@.
  21. *
  22. */
  23. goog.provide('goog.crypt.Sha2');
  24. goog.require('goog.array');
  25. goog.require('goog.asserts');
  26. goog.require('goog.crypt.Hash');
  27. /**
  28. * SHA-2 cryptographic hash constructor.
  29. * This constructor should not be used directly to create the object. Rather,
  30. * one should use the constructor of the sub-classes.
  31. * @param {number} numHashBlocks The size of output in 16-byte blocks.
  32. * @param {!Array<number>} initHashBlocks The hash-specific initialization
  33. * @constructor
  34. * @extends {goog.crypt.Hash}
  35. * @struct
  36. */
  37. goog.crypt.Sha2 = function(numHashBlocks, initHashBlocks) {
  38. goog.crypt.Sha2.base(this, 'constructor');
  39. this.blockSize = goog.crypt.Sha2.BLOCKSIZE_;
  40. /**
  41. * A chunk holding the currently processed message bytes. Once the chunk has
  42. * 64 bytes, we feed it into computeChunk_ function and reset this.chunk_.
  43. * @private {!Array<number>|!Uint8Array}
  44. */
  45. this.chunk_ = goog.global['Uint8Array'] ? new Uint8Array(this.blockSize) :
  46. new Array(this.blockSize);
  47. /**
  48. * Current number of bytes in this.chunk_.
  49. * @private {number}
  50. */
  51. this.inChunk_ = 0;
  52. /**
  53. * Total number of bytes in currently processed message.
  54. * @private {number}
  55. */
  56. this.total_ = 0;
  57. /**
  58. * Holds the previous values of accumulated hash a-h in the computeChunk_
  59. * function.
  60. * @private {!Array<number>|!Int32Array}
  61. */
  62. this.hash_ = [];
  63. /**
  64. * The number of output hash blocks (each block is 4 bytes long).
  65. * @private {number}
  66. */
  67. this.numHashBlocks_ = numHashBlocks;
  68. /**
  69. * @private {!Array<number>} initHashBlocks
  70. */
  71. this.initHashBlocks_ = initHashBlocks;
  72. /**
  73. * Temporary array used in chunk computation. Allocate here as a
  74. * member rather than as a local within computeChunk_() as a
  75. * performance optimization to reduce the number of allocations and
  76. * reduce garbage collection.
  77. * @private {!Int32Array|!Array<number>}
  78. */
  79. this.w_ = goog.global['Int32Array'] ? new Int32Array(64) : new Array(64);
  80. if (!goog.isDef(goog.crypt.Sha2.Kx_)) {
  81. // This is the first time this constructor has been called.
  82. if (goog.global['Int32Array']) {
  83. // Typed arrays exist
  84. goog.crypt.Sha2.Kx_ = new Int32Array(goog.crypt.Sha2.K_);
  85. } else {
  86. // Typed arrays do not exist
  87. goog.crypt.Sha2.Kx_ = goog.crypt.Sha2.K_;
  88. }
  89. }
  90. this.reset();
  91. };
  92. goog.inherits(goog.crypt.Sha2, goog.crypt.Hash);
  93. /**
  94. * The block size
  95. * @private {number}
  96. */
  97. goog.crypt.Sha2.BLOCKSIZE_ = 512 / 8;
  98. /**
  99. * Contains data needed to pad messages less than BLOCK_SIZE_ bytes.
  100. * @private {!Array<number>}
  101. */
  102. goog.crypt.Sha2.PADDING_ = goog.array.concat(
  103. 128, goog.array.repeat(0, goog.crypt.Sha2.BLOCKSIZE_ - 1));
  104. /** @override */
  105. goog.crypt.Sha2.prototype.reset = function() {
  106. this.inChunk_ = 0;
  107. this.total_ = 0;
  108. this.hash_ = goog.global['Int32Array'] ?
  109. new Int32Array(this.initHashBlocks_) :
  110. goog.array.clone(this.initHashBlocks_);
  111. };
  112. /**
  113. * Helper function to compute the hashes for a given 512-bit message chunk.
  114. * @private
  115. */
  116. goog.crypt.Sha2.prototype.computeChunk_ = function() {
  117. var chunk = this.chunk_;
  118. goog.asserts.assert(chunk.length == this.blockSize);
  119. var rounds = 64;
  120. // Divide the chunk into 16 32-bit-words.
  121. var w = this.w_;
  122. var index = 0;
  123. var offset = 0;
  124. while (offset < chunk.length) {
  125. w[index++] = (chunk[offset] << 24) | (chunk[offset + 1] << 16) |
  126. (chunk[offset + 2] << 8) | (chunk[offset + 3]);
  127. offset = index * 4;
  128. }
  129. // Extend the w[] array to be the number of rounds.
  130. for (var i = 16; i < rounds; i++) {
  131. var w_15 = w[i - 15] | 0;
  132. var s0 = ((w_15 >>> 7) | (w_15 << 25)) ^ ((w_15 >>> 18) | (w_15 << 14)) ^
  133. (w_15 >>> 3);
  134. var w_2 = w[i - 2] | 0;
  135. var s1 = ((w_2 >>> 17) | (w_2 << 15)) ^ ((w_2 >>> 19) | (w_2 << 13)) ^
  136. (w_2 >>> 10);
  137. // As a performance optimization, construct the sum a pair at a time
  138. // with casting to integer (bitwise OR) to eliminate unnecessary
  139. // double<->integer conversions.
  140. var partialSum1 = ((w[i - 16] | 0) + s0) | 0;
  141. var partialSum2 = ((w[i - 7] | 0) + s1) | 0;
  142. w[i] = (partialSum1 + partialSum2) | 0;
  143. }
  144. var a = this.hash_[0] | 0;
  145. var b = this.hash_[1] | 0;
  146. var c = this.hash_[2] | 0;
  147. var d = this.hash_[3] | 0;
  148. var e = this.hash_[4] | 0;
  149. var f = this.hash_[5] | 0;
  150. var g = this.hash_[6] | 0;
  151. var h = this.hash_[7] | 0;
  152. for (var i = 0; i < rounds; i++) {
  153. var S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^
  154. ((a >>> 22) | (a << 10));
  155. var maj = ((a & b) ^ (a & c) ^ (b & c));
  156. var t2 = (S0 + maj) | 0;
  157. var S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^
  158. ((e >>> 25) | (e << 7));
  159. var ch = ((e & f) ^ ((~e) & g));
  160. // As a performance optimization, construct the sum a pair at a time
  161. // with casting to integer (bitwise OR) to eliminate unnecessary
  162. // double<->integer conversions.
  163. var partialSum1 = (h + S1) | 0;
  164. var partialSum2 = (ch + (goog.crypt.Sha2.Kx_[i] | 0)) | 0;
  165. var partialSum3 = (partialSum2 + (w[i] | 0)) | 0;
  166. var t1 = (partialSum1 + partialSum3) | 0;
  167. h = g;
  168. g = f;
  169. f = e;
  170. e = (d + t1) | 0;
  171. d = c;
  172. c = b;
  173. b = a;
  174. a = (t1 + t2) | 0;
  175. }
  176. this.hash_[0] = (this.hash_[0] + a) | 0;
  177. this.hash_[1] = (this.hash_[1] + b) | 0;
  178. this.hash_[2] = (this.hash_[2] + c) | 0;
  179. this.hash_[3] = (this.hash_[3] + d) | 0;
  180. this.hash_[4] = (this.hash_[4] + e) | 0;
  181. this.hash_[5] = (this.hash_[5] + f) | 0;
  182. this.hash_[6] = (this.hash_[6] + g) | 0;
  183. this.hash_[7] = (this.hash_[7] + h) | 0;
  184. };
  185. /** @override */
  186. goog.crypt.Sha2.prototype.update = function(message, opt_length) {
  187. if (!goog.isDef(opt_length)) {
  188. opt_length = message.length;
  189. }
  190. // Process the message from left to right up to |opt_length| bytes.
  191. // When we get a 512-bit chunk, compute the hash of it and reset
  192. // this.chunk_. The message might not be multiple of 512 bits so we
  193. // might end up with a chunk that is less than 512 bits. We store
  194. // such partial chunk in this.chunk_ and it will be filled up later
  195. // in digest().
  196. var n = 0;
  197. var inChunk = this.inChunk_;
  198. // The input message could be either byte array of string.
  199. if (goog.isString(message)) {
  200. while (n < opt_length) {
  201. this.chunk_[inChunk++] = message.charCodeAt(n++);
  202. if (inChunk == this.blockSize) {
  203. this.computeChunk_();
  204. inChunk = 0;
  205. }
  206. }
  207. } else if (goog.isArrayLike(message)) {
  208. while (n < opt_length) {
  209. var b = message[n++];
  210. if (!('number' == typeof b && 0 <= b && 255 >= b && b == (b | 0))) {
  211. throw Error('message must be a byte array');
  212. }
  213. this.chunk_[inChunk++] = b;
  214. if (inChunk == this.blockSize) {
  215. this.computeChunk_();
  216. inChunk = 0;
  217. }
  218. }
  219. } else {
  220. throw Error('message must be string or array');
  221. }
  222. // Record the current bytes in chunk to support partial update.
  223. this.inChunk_ = inChunk;
  224. // Record total message bytes we have processed so far.
  225. this.total_ += opt_length;
  226. };
  227. /** @override */
  228. goog.crypt.Sha2.prototype.digest = function() {
  229. var digest = [];
  230. var totalBits = this.total_ * 8;
  231. // Append pad 0x80 0x00*.
  232. if (this.inChunk_ < 56) {
  233. this.update(goog.crypt.Sha2.PADDING_, 56 - this.inChunk_);
  234. } else {
  235. this.update(
  236. goog.crypt.Sha2.PADDING_, this.blockSize - (this.inChunk_ - 56));
  237. }
  238. // Append # bits in the 64-bit big-endian format.
  239. for (var i = 63; i >= 56; i--) {
  240. this.chunk_[i] = totalBits & 255;
  241. totalBits /= 256; // Don't use bit-shifting here!
  242. }
  243. this.computeChunk_();
  244. // Finally, output the result digest.
  245. var n = 0;
  246. for (var i = 0; i < this.numHashBlocks_; i++) {
  247. for (var j = 24; j >= 0; j -= 8) {
  248. digest[n++] = ((this.hash_[i] >> j) & 255);
  249. }
  250. }
  251. return digest;
  252. };
  253. /**
  254. * Constants used in SHA-2.
  255. * @const
  256. * @private {!Array<number>}
  257. */
  258. goog.crypt.Sha2.K_ = [
  259. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
  260. 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  261. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
  262. 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  263. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
  264. 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  265. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
  266. 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  267. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
  268. 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  269. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  270. ];
  271. /**
  272. * Sha2.K as an Int32Array if this JS supports typed arrays; otherwise,
  273. * the same array as Sha2.K.
  274. *
  275. * The compiler cannot remove an Int32Array, even if it is not needed
  276. * (There are certain cases where creating an Int32Array is not
  277. * side-effect free). Instead, the first time we construct a Sha2
  278. * instance, we convert or assign Sha2.K as appropriate.
  279. * @private {undefined|!Array<number>|!Int32Array}
  280. */
  281. goog.crypt.Sha2.Kx_;