123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- var forge = require('./forge');
- require('./util');
- module.exports = forge.cipher = forge.cipher || {};
- forge.cipher.algorithms = forge.cipher.algorithms || {};
- forge.cipher.createCipher = function(algorithm, key) {
- var api = algorithm;
- if(typeof api === 'string') {
- api = forge.cipher.getAlgorithm(api);
- if(api) {
- api = api();
- }
- }
- if(!api) {
- throw new Error('Unsupported algorithm: ' + algorithm);
- }
-
- return new forge.cipher.BlockCipher({
- algorithm: api,
- key: key,
- decrypt: false
- });
- };
- forge.cipher.createDecipher = function(algorithm, key) {
- var api = algorithm;
- if(typeof api === 'string') {
- api = forge.cipher.getAlgorithm(api);
- if(api) {
- api = api();
- }
- }
- if(!api) {
- throw new Error('Unsupported algorithm: ' + algorithm);
- }
-
- return new forge.cipher.BlockCipher({
- algorithm: api,
- key: key,
- decrypt: true
- });
- };
- forge.cipher.registerAlgorithm = function(name, algorithm) {
- name = name.toUpperCase();
- forge.cipher.algorithms[name] = algorithm;
- };
- forge.cipher.getAlgorithm = function(name) {
- name = name.toUpperCase();
- if(name in forge.cipher.algorithms) {
- return forge.cipher.algorithms[name];
- }
- return null;
- };
- var BlockCipher = forge.cipher.BlockCipher = function(options) {
- this.algorithm = options.algorithm;
- this.mode = this.algorithm.mode;
- this.blockSize = this.mode.blockSize;
- this._finish = false;
- this._input = null;
- this.output = null;
- this._op = options.decrypt ? this.mode.decrypt : this.mode.encrypt;
- this._decrypt = options.decrypt;
- this.algorithm.initialize(options);
- };
- BlockCipher.prototype.start = function(options) {
- options = options || {};
- var opts = {};
- for(var key in options) {
- opts[key] = options[key];
- }
- opts.decrypt = this._decrypt;
- this._finish = false;
- this._input = forge.util.createBuffer();
- this.output = options.output || forge.util.createBuffer();
- this.mode.start(opts);
- };
- BlockCipher.prototype.update = function(input) {
- if(input) {
-
- this._input.putBuffer(input);
- }
-
- while(!this._op.call(this.mode, this._input, this.output, this._finish) &&
- !this._finish) {}
-
- this._input.compact();
- };
- BlockCipher.prototype.finish = function(pad) {
-
-
- if(pad && (this.mode.name === 'ECB' || this.mode.name === 'CBC')) {
- this.mode.pad = function(input) {
- return pad(this.blockSize, input, false);
- };
- this.mode.unpad = function(output) {
- return pad(this.blockSize, output, true);
- };
- }
-
- var options = {};
- options.decrypt = this._decrypt;
-
- options.overflow = this._input.length() % this.blockSize;
- if(!this._decrypt && this.mode.pad) {
- if(!this.mode.pad(this._input, options)) {
- return false;
- }
- }
-
- this._finish = true;
- this.update();
- if(this._decrypt && this.mode.unpad) {
- if(!this.mode.unpad(this.output, options)) {
- return false;
- }
- }
- if(this.mode.afterFinish) {
- if(!this.mode.afterFinish(this.output, options)) {
- return false;
- }
- }
- return true;
- };
|