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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var AuthCipher = require('./authCipher')
  2. var Buffer = require('safe-buffer').Buffer
  3. var MODES = require('./modes')
  4. var StreamCipher = require('./streamCipher')
  5. var Transform = require('cipher-base')
  6. var aes = require('./aes')
  7. var ebtk = require('evp_bytestokey')
  8. var inherits = require('inherits')
  9. function Decipher (mode, key, iv) {
  10. Transform.call(this)
  11. this._cache = new Splitter()
  12. this._last = void 0
  13. this._cipher = new aes.AES(key)
  14. this._prev = Buffer.from(iv)
  15. this._mode = mode
  16. this._autopadding = true
  17. }
  18. inherits(Decipher, Transform)
  19. Decipher.prototype._update = function (data) {
  20. this._cache.add(data)
  21. var chunk
  22. var thing
  23. var out = []
  24. while ((chunk = this._cache.get(this._autopadding))) {
  25. thing = this._mode.decrypt(this, chunk)
  26. out.push(thing)
  27. }
  28. return Buffer.concat(out)
  29. }
  30. Decipher.prototype._final = function () {
  31. var chunk = this._cache.flush()
  32. if (this._autopadding) {
  33. return unpad(this._mode.decrypt(this, chunk))
  34. } else if (chunk) {
  35. throw new Error('data not multiple of block length')
  36. }
  37. }
  38. Decipher.prototype.setAutoPadding = function (setTo) {
  39. this._autopadding = !!setTo
  40. return this
  41. }
  42. function Splitter () {
  43. this.cache = Buffer.allocUnsafe(0)
  44. }
  45. Splitter.prototype.add = function (data) {
  46. this.cache = Buffer.concat([this.cache, data])
  47. }
  48. Splitter.prototype.get = function (autoPadding) {
  49. var out
  50. if (autoPadding) {
  51. if (this.cache.length > 16) {
  52. out = this.cache.slice(0, 16)
  53. this.cache = this.cache.slice(16)
  54. return out
  55. }
  56. } else {
  57. if (this.cache.length >= 16) {
  58. out = this.cache.slice(0, 16)
  59. this.cache = this.cache.slice(16)
  60. return out
  61. }
  62. }
  63. return null
  64. }
  65. Splitter.prototype.flush = function () {
  66. if (this.cache.length) return this.cache
  67. }
  68. function unpad (last) {
  69. var padded = last[15]
  70. if (padded < 1 || padded > 16) {
  71. throw new Error('unable to decrypt data')
  72. }
  73. var i = -1
  74. while (++i < padded) {
  75. if (last[(i + (16 - padded))] !== padded) {
  76. throw new Error('unable to decrypt data')
  77. }
  78. }
  79. if (padded === 16) return
  80. return last.slice(0, 16 - padded)
  81. }
  82. function createDecipheriv (suite, password, iv) {
  83. var config = MODES[suite.toLowerCase()]
  84. if (!config) throw new TypeError('invalid suite type')
  85. if (typeof iv === 'string') iv = Buffer.from(iv)
  86. if (config.mode !== 'GCM' && iv.length !== config.iv) throw new TypeError('invalid iv length ' + iv.length)
  87. if (typeof password === 'string') password = Buffer.from(password)
  88. if (password.length !== config.key / 8) throw new TypeError('invalid key length ' + password.length)
  89. if (config.type === 'stream') {
  90. return new StreamCipher(config.module, password, iv, true)
  91. } else if (config.type === 'auth') {
  92. return new AuthCipher(config.module, password, iv, true)
  93. }
  94. return new Decipher(config.module, password, iv)
  95. }
  96. function createDecipher (suite, password) {
  97. var config = MODES[suite.toLowerCase()]
  98. if (!config) throw new TypeError('invalid suite type')
  99. var keys = ebtk(password, false, config.key, config.iv)
  100. return createDecipheriv(suite, keys.key, keys.iv)
  101. }
  102. exports.createDecipher = createDecipher
  103. exports.createDecipheriv = createDecipheriv