encrypter.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var aes = require('./aes')
  2. var Transform = require('cipher-base')
  3. var inherits = require('inherits')
  4. var modes = require('./modes')
  5. var ebtk = require('evp_bytestokey')
  6. var StreamCipher = require('./streamCipher')
  7. var AuthCipher = require('./authCipher')
  8. inherits(Cipher, Transform)
  9. function Cipher (mode, key, iv) {
  10. if (!(this instanceof Cipher)) {
  11. return new Cipher(mode, key, iv)
  12. }
  13. Transform.call(this)
  14. this._cache = new Splitter()
  15. this._cipher = new aes.AES(key)
  16. this._prev = new Buffer(iv.length)
  17. iv.copy(this._prev)
  18. this._mode = mode
  19. this._autopadding = true
  20. }
  21. Cipher.prototype._update = function (data) {
  22. this._cache.add(data)
  23. var chunk
  24. var thing
  25. var out = []
  26. while ((chunk = this._cache.get())) {
  27. thing = this._mode.encrypt(this, chunk)
  28. out.push(thing)
  29. }
  30. return Buffer.concat(out)
  31. }
  32. Cipher.prototype._final = function () {
  33. var chunk = this._cache.flush()
  34. if (this._autopadding) {
  35. chunk = this._mode.encrypt(this, chunk)
  36. this._cipher.scrub()
  37. return chunk
  38. } else if (chunk.toString('hex') !== '10101010101010101010101010101010') {
  39. this._cipher.scrub()
  40. throw new Error('data not multiple of block length')
  41. }
  42. }
  43. Cipher.prototype.setAutoPadding = function (setTo) {
  44. this._autopadding = !!setTo
  45. return this
  46. }
  47. function Splitter () {
  48. if (!(this instanceof Splitter)) {
  49. return new Splitter()
  50. }
  51. this.cache = new Buffer('')
  52. }
  53. Splitter.prototype.add = function (data) {
  54. this.cache = Buffer.concat([this.cache, data])
  55. }
  56. Splitter.prototype.get = function () {
  57. if (this.cache.length > 15) {
  58. var out = this.cache.slice(0, 16)
  59. this.cache = this.cache.slice(16)
  60. return out
  61. }
  62. return null
  63. }
  64. Splitter.prototype.flush = function () {
  65. var len = 16 - this.cache.length
  66. var padBuff = new Buffer(len)
  67. var i = -1
  68. while (++i < len) {
  69. padBuff.writeUInt8(len, i)
  70. }
  71. var out = Buffer.concat([this.cache, padBuff])
  72. return out
  73. }
  74. var modelist = {
  75. ECB: require('./modes/ecb'),
  76. CBC: require('./modes/cbc'),
  77. CFB: require('./modes/cfb'),
  78. CFB8: require('./modes/cfb8'),
  79. CFB1: require('./modes/cfb1'),
  80. OFB: require('./modes/ofb'),
  81. CTR: require('./modes/ctr'),
  82. GCM: require('./modes/ctr')
  83. }
  84. function createCipheriv (suite, password, iv) {
  85. var config = modes[suite.toLowerCase()]
  86. if (!config) {
  87. throw new TypeError('invalid suite type')
  88. }
  89. if (typeof iv === 'string') {
  90. iv = new Buffer(iv)
  91. }
  92. if (typeof password === 'string') {
  93. password = new Buffer(password)
  94. }
  95. if (password.length !== config.key / 8) {
  96. throw new TypeError('invalid key length ' + password.length)
  97. }
  98. if (iv.length !== config.iv) {
  99. throw new TypeError('invalid iv length ' + iv.length)
  100. }
  101. if (config.type === 'stream') {
  102. return new StreamCipher(modelist[config.mode], password, iv)
  103. } else if (config.type === 'auth') {
  104. return new AuthCipher(modelist[config.mode], password, iv)
  105. }
  106. return new Cipher(modelist[config.mode], password, iv)
  107. }
  108. function createCipher (suite, password) {
  109. var config = modes[suite.toLowerCase()]
  110. if (!config) {
  111. throw new TypeError('invalid suite type')
  112. }
  113. var keys = ebtk(password, false, config.key, config.iv)
  114. return createCipheriv(suite, keys.key, keys.iv)
  115. }
  116. exports.createCipheriv = createCipheriv
  117. exports.createCipher = createCipher