authCipher.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/authCipher.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 aes = require('./aes')
  12. var Buffer = require('safe-buffer').Buffer
  13. var Transform = require('cipher-base')
  14. var inherits = require('inherits')
  15. var GHASH = require('./ghash')
  16. var xor = require('buffer-xor')
  17. var incr32 = require('./incr32')
  18. function xorTest (a, b) {
  19. var out = 0
  20. if (a.length !== b.length) out++
  21. var len = Math.min(a.length, b.length)
  22. for (var i = 0; i < len; ++i) {
  23. out += (a[i] ^ b[i])
  24. }
  25. return out
  26. }
  27. function calcIv (self, iv, ck) {
  28. if (iv.length === 12) {
  29. self._finID = Buffer.concat([iv, Buffer.from([0, 0, 0, 1])])
  30. return Buffer.concat([iv, Buffer.from([0, 0, 0, 2])])
  31. }
  32. var ghash = new GHASH(ck)
  33. var len = iv.length
  34. var toPad = len % 16
  35. ghash.update(iv)
  36. if (toPad) {
  37. toPad = 16 - toPad
  38. ghash.update(Buffer.alloc(toPad, 0))
  39. }
  40. ghash.update(Buffer.alloc(8, 0))
  41. var ivBits = len * 8
  42. var tail = Buffer.alloc(8)
  43. tail.writeUIntBE(ivBits, 0, 8)
  44. ghash.update(tail)
  45. self._finID = ghash.state
  46. var out = Buffer.from(self._finID)
  47. incr32(out)
  48. return out
  49. }
  50. function StreamCipher (mode, key, iv, decrypt) {
  51. Transform.call(this)
  52. var h = Buffer.alloc(4, 0)
  53. this._cipher = new aes.AES(key)
  54. var ck = this._cipher.encryptBlock(h)
  55. this._ghash = new GHASH(ck)
  56. iv = calcIv(this, iv, ck)
  57. this._prev = Buffer.from(iv)
  58. this._cache = Buffer.allocUnsafe(0)
  59. this._secCache = Buffer.allocUnsafe(0)
  60. this._decrypt = decrypt
  61. this._alen = 0
  62. this._len = 0
  63. this._mode = mode
  64. this._authTag = null
  65. this._called = false
  66. }
  67. inherits(StreamCipher, Transform)
  68. StreamCipher.prototype._update = function (chunk) {
  69. if (!this._called && this._alen) {
  70. var rump = 16 - (this._alen % 16)
  71. if (rump < 16) {
  72. rump = Buffer.alloc(rump, 0)
  73. this._ghash.update(rump)
  74. }
  75. }
  76. this._called = true
  77. var out = this._mode.encrypt(this, chunk)
  78. if (this._decrypt) {
  79. this._ghash.update(chunk)
  80. } else {
  81. this._ghash.update(out)
  82. }
  83. this._len += chunk.length
  84. return out
  85. }
  86. StreamCipher.prototype._final = function () {
  87. if (this._decrypt && !this._authTag) throw new Error('Unsupported state or unable to authenticate data')
  88. var tag = xor(this._ghash.final(this._alen * 8, this._len * 8), this._cipher.encryptBlock(this._finID))
  89. if (this._decrypt && xorTest(tag, this._authTag)) throw new Error('Unsupported state or unable to authenticate data')
  90. this._authTag = tag
  91. this._cipher.scrub()
  92. }
  93. StreamCipher.prototype.getAuthTag = function getAuthTag () {
  94. if (this._decrypt || !Buffer.isBuffer(this._authTag)) throw new Error('Attempting to get auth tag in unsupported state')
  95. return this._authTag
  96. }
  97. StreamCipher.prototype.setAuthTag = function setAuthTag (tag) {
  98. if (!this._decrypt) throw new Error('Attempting to set auth tag in unsupported state')
  99. this._authTag = tag
  100. }
  101. StreamCipher.prototype.setAAD = function setAAD (buf) {
  102. if (this._called) throw new Error('Attempting to set AAD in unsupported state')
  103. this._ghash.update(buf)
  104. this._alen += buf.length
  105. }
  106. module.exports = StreamCipher
  107. }
  108. if (nodeEnv) {
  109. __define(__module.exports, __require, __module);
  110. }
  111. else {
  112. __quick_compile_project__.registerModuleFunc(__filename, function () {
  113. __define(__module.exports, __require, __module);
  114. });
  115. }
  116. })();