ghash.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/browserify-aes/ghash.js';
  5. var __require = nodeEnv ? function (request) {
  6. return cc.require(request);
  7. } : function (request) {
  8. return __quick_compile_project__.require(request, __filename);
  9. };
  10. function __define (exports, require, module) {
  11. if (!nodeEnv) {__quick_compile_project__.registerModule(__filename, module);}var Buffer = require('safe-buffer').Buffer
  12. var ZEROES = Buffer.alloc(16, 0)
  13. function toArray (buf) {
  14. return [
  15. buf.readUInt32BE(0),
  16. buf.readUInt32BE(4),
  17. buf.readUInt32BE(8),
  18. buf.readUInt32BE(12)
  19. ]
  20. }
  21. function fromArray (out) {
  22. var buf = Buffer.allocUnsafe(16)
  23. buf.writeUInt32BE(out[0] >>> 0, 0)
  24. buf.writeUInt32BE(out[1] >>> 0, 4)
  25. buf.writeUInt32BE(out[2] >>> 0, 8)
  26. buf.writeUInt32BE(out[3] >>> 0, 12)
  27. return buf
  28. }
  29. function GHASH (key) {
  30. this.h = key
  31. this.state = Buffer.alloc(16, 0)
  32. this.cache = Buffer.allocUnsafe(0)
  33. }
  34. // from http://bitwiseshiftleft.github.io/sjcl/doc/symbols/src/core_gcm.js.html
  35. // by Juho Vähä-Herttua
  36. GHASH.prototype.ghash = function (block) {
  37. var i = -1
  38. while (++i < block.length) {
  39. this.state[i] ^= block[i]
  40. }
  41. this._multiply()
  42. }
  43. GHASH.prototype._multiply = function () {
  44. var Vi = toArray(this.h)
  45. var Zi = [0, 0, 0, 0]
  46. var j, xi, lsbVi
  47. var i = -1
  48. while (++i < 128) {
  49. xi = (this.state[~~(i / 8)] & (1 << (7 - (i % 8)))) !== 0
  50. if (xi) {
  51. // Z_i+1 = Z_i ^ V_i
  52. Zi[0] ^= Vi[0]
  53. Zi[1] ^= Vi[1]
  54. Zi[2] ^= Vi[2]
  55. Zi[3] ^= Vi[3]
  56. }
  57. // Store the value of LSB(V_i)
  58. lsbVi = (Vi[3] & 1) !== 0
  59. // V_i+1 = V_i >> 1
  60. for (j = 3; j > 0; j--) {
  61. Vi[j] = (Vi[j] >>> 1) | ((Vi[j - 1] & 1) << 31)
  62. }
  63. Vi[0] = Vi[0] >>> 1
  64. // If LSB(V_i) is 1, V_i+1 = (V_i >> 1) ^ R
  65. if (lsbVi) {
  66. Vi[0] = Vi[0] ^ (0xe1 << 24)
  67. }
  68. }
  69. this.state = fromArray(Zi)
  70. }
  71. GHASH.prototype.update = function (buf) {
  72. this.cache = Buffer.concat([this.cache, buf])
  73. var chunk
  74. while (this.cache.length >= 16) {
  75. chunk = this.cache.slice(0, 16)
  76. this.cache = this.cache.slice(16)
  77. this.ghash(chunk)
  78. }
  79. }
  80. GHASH.prototype.final = function (abl, bl) {
  81. if (this.cache.length) {
  82. this.ghash(Buffer.concat([this.cache, ZEROES], 16))
  83. }
  84. this.ghash(fromArray([0, abl, 0, bl]))
  85. return this.state
  86. }
  87. module.exports = GHASH
  88. }
  89. if (nodeEnv) {
  90. __define(__module.exports, __require, __module);
  91. }
  92. else {
  93. __quick_compile_project__.registerModuleFunc(__filename, function () {
  94. __define(__module.exports, __require, __module);
  95. });
  96. }
  97. })();