You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
8.9 KiB

4 years ago
  1. /**
  2. * Secure Hash Algorithm with 160-bit digest (SHA-1) implementation.
  3. *
  4. * @author Dave Longley
  5. *
  6. * Copyright (c) 2010-2015 Digital Bazaar, Inc.
  7. */
  8. var forge = require('./forge');
  9. require('./md');
  10. require('./util');
  11. var sha1 = module.exports = forge.sha1 = forge.sha1 || {};
  12. forge.md.sha1 = forge.md.algorithms.sha1 = sha1;
  13. /**
  14. * Creates a SHA-1 message digest object.
  15. *
  16. * @return a message digest object.
  17. */
  18. sha1.create = function() {
  19. // do initialization as necessary
  20. if(!_initialized) {
  21. _init();
  22. }
  23. // SHA-1 state contains five 32-bit integers
  24. var _state = null;
  25. // input buffer
  26. var _input = forge.util.createBuffer();
  27. // used for word storage
  28. var _w = new Array(80);
  29. // message digest object
  30. var md = {
  31. algorithm: 'sha1',
  32. blockLength: 64,
  33. digestLength: 20,
  34. // 56-bit length of message so far (does not including padding)
  35. messageLength: 0,
  36. // true message length
  37. fullMessageLength: null,
  38. // size of message length in bytes
  39. messageLengthSize: 8
  40. };
  41. /**
  42. * Starts the digest.
  43. *
  44. * @return this digest object.
  45. */
  46. md.start = function() {
  47. // up to 56-bit message length for convenience
  48. md.messageLength = 0;
  49. // full message length (set md.messageLength64 for backwards-compatibility)
  50. md.fullMessageLength = md.messageLength64 = [];
  51. var int32s = md.messageLengthSize / 4;
  52. for(var i = 0; i < int32s; ++i) {
  53. md.fullMessageLength.push(0);
  54. }
  55. _input = forge.util.createBuffer();
  56. _state = {
  57. h0: 0x67452301,
  58. h1: 0xEFCDAB89,
  59. h2: 0x98BADCFE,
  60. h3: 0x10325476,
  61. h4: 0xC3D2E1F0
  62. };
  63. return md;
  64. };
  65. // start digest automatically for first time
  66. md.start();
  67. /**
  68. * Updates the digest with the given message input. The given input can
  69. * treated as raw input (no encoding will be applied) or an encoding of
  70. * 'utf8' maybe given to encode the input using UTF-8.
  71. *
  72. * @param msg the message input to update with.
  73. * @param encoding the encoding to use (default: 'raw', other: 'utf8').
  74. *
  75. * @return this digest object.
  76. */
  77. md.update = function(msg, encoding) {
  78. if(encoding === 'utf8') {
  79. msg = forge.util.encodeUtf8(msg);
  80. }
  81. // update message length
  82. var len = msg.length;
  83. md.messageLength += len;
  84. len = [(len / 0x100000000) >>> 0, len >>> 0];
  85. for(var i = md.fullMessageLength.length - 1; i >= 0; --i) {
  86. md.fullMessageLength[i] += len[1];
  87. len[1] = len[0] + ((md.fullMessageLength[i] / 0x100000000) >>> 0);
  88. md.fullMessageLength[i] = md.fullMessageLength[i] >>> 0;
  89. len[0] = ((len[1] / 0x100000000) >>> 0);
  90. }
  91. // add bytes to input buffer
  92. _input.putBytes(msg);
  93. // process bytes
  94. _update(_state, _w, _input);
  95. // compact input buffer every 2K or if empty
  96. if(_input.read > 2048 || _input.length() === 0) {
  97. _input.compact();
  98. }
  99. return md;
  100. };
  101. /**
  102. * Produces the digest.
  103. *
  104. * @return a byte buffer containing the digest value.
  105. */
  106. md.digest = function() {
  107. /* Note: Here we copy the remaining bytes in the input buffer and
  108. add the appropriate SHA-1 padding. Then we do the final update
  109. on a copy of the state so that if the user wants to get
  110. intermediate digests they can do so. */
  111. /* Determine the number of bytes that must be added to the message
  112. to ensure its length is congruent to 448 mod 512. In other words,
  113. the data to be digested must be a multiple of 512 bits (or 128 bytes).
  114. This data includes the message, some padding, and the length of the
  115. message. Since the length of the message will be encoded as 8 bytes (64
  116. bits), that means that the last segment of the data must have 56 bytes
  117. (448 bits) of message and padding. Therefore, the length of the message
  118. plus the padding must be congruent to 448 mod 512 because
  119. 512 - 128 = 448.
  120. In order to fill up the message length it must be filled with
  121. padding that begins with 1 bit followed by all 0 bits. Padding
  122. must *always* be present, so if the message length is already
  123. congruent to 448 mod 512, then 512 padding bits must be added. */
  124. var finalBlock = forge.util.createBuffer();
  125. finalBlock.putBytes(_input.bytes());
  126. // compute remaining size to be digested (include message length size)
  127. var remaining = (
  128. md.fullMessageLength[md.fullMessageLength.length - 1] +
  129. md.messageLengthSize);
  130. // add padding for overflow blockSize - overflow
  131. // _padding starts with 1 byte with first bit is set (byte value 128), then
  132. // there may be up to (blockSize - 1) other pad bytes
  133. var overflow = remaining & (md.blockLength - 1);
  134. finalBlock.putBytes(_padding.substr(0, md.blockLength - overflow));
  135. // serialize message length in bits in big-endian order; since length
  136. // is stored in bytes we multiply by 8 and add carry from next int
  137. var next, carry;
  138. var bits = md.fullMessageLength[0] * 8;
  139. for(var i = 0; i < md.fullMessageLength.length - 1; ++i) {
  140. next = md.fullMessageLength[i + 1] * 8;
  141. carry = (next / 0x100000000) >>> 0;
  142. bits += carry;
  143. finalBlock.putInt32(bits >>> 0);
  144. bits = next >>> 0;
  145. }
  146. finalBlock.putInt32(bits);
  147. var s2 = {
  148. h0: _state.h0,
  149. h1: _state.h1,
  150. h2: _state.h2,
  151. h3: _state.h3,
  152. h4: _state.h4
  153. };
  154. _update(s2, _w, finalBlock);
  155. var rval = forge.util.createBuffer();
  156. rval.putInt32(s2.h0);
  157. rval.putInt32(s2.h1);
  158. rval.putInt32(s2.h2);
  159. rval.putInt32(s2.h3);
  160. rval.putInt32(s2.h4);
  161. return rval;
  162. };
  163. return md;
  164. };
  165. // sha-1 padding bytes not initialized yet
  166. var _padding = null;
  167. var _initialized = false;
  168. /**
  169. * Initializes the constant tables.
  170. */
  171. function _init() {
  172. // create padding
  173. _padding = String.fromCharCode(128);
  174. _padding += forge.util.fillString(String.fromCharCode(0x00), 64);
  175. // now initialized
  176. _initialized = true;
  177. }
  178. /**
  179. * Updates a SHA-1 state with the given byte buffer.
  180. *
  181. * @param s the SHA-1 state to update.
  182. * @param w the array to use to store words.
  183. * @param bytes the byte buffer to update with.
  184. */
  185. function _update(s, w, bytes) {
  186. // consume 512 bit (64 byte) chunks
  187. var t, a, b, c, d, e, f, i;
  188. var len = bytes.length();
  189. while(len >= 64) {
  190. // the w array will be populated with sixteen 32-bit big-endian words
  191. // and then extended into 80 32-bit words according to SHA-1 algorithm
  192. // and for 32-79 using Max Locktyukhin's optimization
  193. // initialize hash value for this chunk
  194. a = s.h0;
  195. b = s.h1;
  196. c = s.h2;
  197. d = s.h3;
  198. e = s.h4;
  199. // round 1
  200. for(i = 0; i < 16; ++i) {
  201. t = bytes.getInt32();
  202. w[i] = t;
  203. f = d ^ (b & (c ^ d));
  204. t = ((a << 5) | (a >>> 27)) + f + e + 0x5A827999 + t;
  205. e = d;
  206. d = c;
  207. // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
  208. c = ((b << 30) | (b >>> 2)) >>> 0;
  209. b = a;
  210. a = t;
  211. }
  212. for(; i < 20; ++i) {
  213. t = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]);
  214. t = (t << 1) | (t >>> 31);
  215. w[i] = t;
  216. f = d ^ (b & (c ^ d));
  217. t = ((a << 5) | (a >>> 27)) + f + e + 0x5A827999 + t;
  218. e = d;
  219. d = c;
  220. // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
  221. c = ((b << 30) | (b >>> 2)) >>> 0;
  222. b = a;
  223. a = t;
  224. }
  225. // round 2
  226. for(; i < 32; ++i) {
  227. t = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]);
  228. t = (t << 1) | (t >>> 31);
  229. w[i] = t;
  230. f = b ^ c ^ d;
  231. t = ((a << 5) | (a >>> 27)) + f + e + 0x6ED9EBA1 + t;
  232. e = d;
  233. d = c;
  234. // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
  235. c = ((b << 30) | (b >>> 2)) >>> 0;
  236. b = a;
  237. a = t;
  238. }
  239. for(; i < 40; ++i) {
  240. t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]);
  241. t = (t << 2) | (t >>> 30);
  242. w[i] = t;
  243. f = b ^ c ^ d;
  244. t = ((a << 5) | (a >>> 27)) + f + e + 0x6ED9EBA1 + t;
  245. e = d;
  246. d = c;
  247. // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
  248. c = ((b << 30) | (b >>> 2)) >>> 0;
  249. b = a;
  250. a = t;
  251. }
  252. // round 3
  253. for(; i < 60; ++i) {
  254. t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]);
  255. t = (t << 2) | (t >>> 30);
  256. w[i] = t;
  257. f = (b & c) | (d & (b ^ c));
  258. t = ((a << 5) | (a >>> 27)) + f + e + 0x8F1BBCDC + t;
  259. e = d;
  260. d = c;
  261. // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
  262. c = ((b << 30) | (b >>> 2)) >>> 0;
  263. b = a;
  264. a = t;
  265. }
  266. // round 4
  267. for(; i < 80; ++i) {
  268. t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]);
  269. t = (t << 2) | (t >>> 30);
  270. w[i] = t;
  271. f = b ^ c ^ d;
  272. t = ((a << 5) | (a >>> 27)) + f + e + 0xCA62C1D6 + t;
  273. e = d;
  274. d = c;
  275. // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
  276. c = ((b << 30) | (b >>> 2)) >>> 0;
  277. b = a;
  278. a = t;
  279. }
  280. // update hash state
  281. s.h0 = (s.h0 + a) | 0;
  282. s.h1 = (s.h1 + b) | 0;
  283. s.h2 = (s.h2 + c) | 0;
  284. s.h3 = (s.h3 + d) | 0;
  285. s.h4 = (s.h4 + e) | 0;
  286. len -= 64;
  287. }
  288. }