123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895 |
- ;(function (root, factory, undef) {
- if (typeof exports === "object") {
-
- module.exports = exports = factory(require("./core"), require("./evpkdf"));
- }
- else if (typeof define === "function" && define.amd) {
-
- define(["./core", "./evpkdf"], factory);
- }
- else {
-
- factory(root.CryptoJS);
- }
- }(this, function (CryptoJS) {
-
- CryptoJS.lib.Cipher || (function (undefined) {
-
- var C = CryptoJS;
- var C_lib = C.lib;
- var Base = C_lib.Base;
- var WordArray = C_lib.WordArray;
- var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm;
- var C_enc = C.enc;
- var Utf8 = C_enc.Utf8;
- var Base64 = C_enc.Base64;
- var C_algo = C.algo;
- var EvpKDF = C_algo.EvpKDF;
-
- var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({
-
- cfg: Base.extend(),
-
- createEncryptor: function (key, cfg) {
- return this.create(this._ENC_XFORM_MODE, key, cfg);
- },
-
- createDecryptor: function (key, cfg) {
- return this.create(this._DEC_XFORM_MODE, key, cfg);
- },
-
- init: function (xformMode, key, cfg) {
-
- this.cfg = this.cfg.extend(cfg);
-
- this._xformMode = xformMode;
- this._key = key;
-
- this.reset();
- },
-
- reset: function () {
-
- BufferedBlockAlgorithm.reset.call(this);
-
- this._doReset();
- },
-
- process: function (dataUpdate) {
-
- this._append(dataUpdate);
-
- return this._process();
- },
-
- finalize: function (dataUpdate) {
-
- if (dataUpdate) {
- this._append(dataUpdate);
- }
-
- var finalProcessedData = this._doFinalize();
- return finalProcessedData;
- },
- keySize: 128/32,
- ivSize: 128/32,
- _ENC_XFORM_MODE: 1,
- _DEC_XFORM_MODE: 2,
-
- _createHelper: (function () {
- function selectCipherStrategy(key) {
- if (typeof key == 'string') {
- return PasswordBasedCipher;
- } else {
- return SerializableCipher;
- }
- }
- return function (cipher) {
- return {
- encrypt: function (message, key, cfg) {
- return selectCipherStrategy(key).encrypt(cipher, message, key, cfg);
- },
- decrypt: function (ciphertext, key, cfg) {
- return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg);
- }
- };
- };
- }())
- });
-
- var StreamCipher = C_lib.StreamCipher = Cipher.extend({
- _doFinalize: function () {
-
- var finalProcessedBlocks = this._process(!!'flush');
- return finalProcessedBlocks;
- },
- blockSize: 1
- });
-
- var C_mode = C.mode = {};
-
- var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({
-
- createEncryptor: function (cipher, iv) {
- return this.Encryptor.create(cipher, iv);
- },
-
- createDecryptor: function (cipher, iv) {
- return this.Decryptor.create(cipher, iv);
- },
-
- init: function (cipher, iv) {
- this._cipher = cipher;
- this._iv = iv;
- }
- });
-
- var CBC = C_mode.CBC = (function () {
-
- var CBC = BlockCipherMode.extend();
-
- CBC.Encryptor = CBC.extend({
-
- processBlock: function (words, offset) {
-
- var cipher = this._cipher;
- var blockSize = cipher.blockSize;
-
- xorBlock.call(this, words, offset, blockSize);
- cipher.encryptBlock(words, offset);
-
- this._prevBlock = words.slice(offset, offset + blockSize);
- }
- });
-
- CBC.Decryptor = CBC.extend({
-
- processBlock: function (words, offset) {
-
- var cipher = this._cipher;
- var blockSize = cipher.blockSize;
-
- var thisBlock = words.slice(offset, offset + blockSize);
-
- cipher.decryptBlock(words, offset);
- xorBlock.call(this, words, offset, blockSize);
-
- this._prevBlock = thisBlock;
- }
- });
- function xorBlock(words, offset, blockSize) {
- var block;
-
- var iv = this._iv;
-
- if (iv) {
- block = iv;
-
- this._iv = undefined;
- } else {
- block = this._prevBlock;
- }
-
- for (var i = 0; i < blockSize; i++) {
- words[offset + i] ^= block[i];
- }
- }
- return CBC;
- }());
-
- var C_pad = C.pad = {};
-
- var Pkcs7 = C_pad.Pkcs7 = {
-
- pad: function (data, blockSize) {
-
- var blockSizeBytes = blockSize * 4;
-
- var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
-
- var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes;
-
- var paddingWords = [];
- for (var i = 0; i < nPaddingBytes; i += 4) {
- paddingWords.push(paddingWord);
- }
- var padding = WordArray.create(paddingWords, nPaddingBytes);
-
- data.concat(padding);
- },
-
- unpad: function (data) {
-
- var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
-
- data.sigBytes -= nPaddingBytes;
- }
- };
-
- var BlockCipher = C_lib.BlockCipher = Cipher.extend({
-
- cfg: Cipher.cfg.extend({
- mode: CBC,
- padding: Pkcs7
- }),
- reset: function () {
- var modeCreator;
-
- Cipher.reset.call(this);
-
- var cfg = this.cfg;
- var iv = cfg.iv;
- var mode = cfg.mode;
-
- if (this._xformMode == this._ENC_XFORM_MODE) {
- modeCreator = mode.createEncryptor;
- } else {
- modeCreator = mode.createDecryptor;
-
- this._minBufferSize = 1;
- }
- if (this._mode && this._mode.__creator == modeCreator) {
- this._mode.init(this, iv && iv.words);
- } else {
- this._mode = modeCreator.call(mode, this, iv && iv.words);
- this._mode.__creator = modeCreator;
- }
- },
- _doProcessBlock: function (words, offset) {
- this._mode.processBlock(words, offset);
- },
- _doFinalize: function () {
- var finalProcessedBlocks;
-
- var padding = this.cfg.padding;
-
- if (this._xformMode == this._ENC_XFORM_MODE) {
-
- padding.pad(this._data, this.blockSize);
-
- finalProcessedBlocks = this._process(!!'flush');
- } else {
-
- finalProcessedBlocks = this._process(!!'flush');
-
- padding.unpad(finalProcessedBlocks);
- }
- return finalProcessedBlocks;
- },
- blockSize: 128/32
- });
-
- var CipherParams = C_lib.CipherParams = Base.extend({
-
- init: function (cipherParams) {
- this.mixIn(cipherParams);
- },
-
- toString: function (formatter) {
- return (formatter || this.formatter).stringify(this);
- }
- });
-
- var C_format = C.format = {};
-
- var OpenSSLFormatter = C_format.OpenSSL = {
-
- stringify: function (cipherParams) {
- var wordArray;
-
- var ciphertext = cipherParams.ciphertext;
- var salt = cipherParams.salt;
-
- if (salt) {
- wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
- } else {
- wordArray = ciphertext;
- }
- return wordArray.toString(Base64);
- },
-
- parse: function (openSSLStr) {
- var salt;
-
- var ciphertext = Base64.parse(openSSLStr);
-
- var ciphertextWords = ciphertext.words;
-
- if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
-
- salt = WordArray.create(ciphertextWords.slice(2, 4));
-
- ciphertextWords.splice(0, 4);
- ciphertext.sigBytes -= 16;
- }
- return CipherParams.create({ ciphertext: ciphertext, salt: salt });
- }
- };
-
- var SerializableCipher = C_lib.SerializableCipher = Base.extend({
-
- cfg: Base.extend({
- format: OpenSSLFormatter
- }),
-
- encrypt: function (cipher, message, key, cfg) {
-
- cfg = this.cfg.extend(cfg);
-
- var encryptor = cipher.createEncryptor(key, cfg);
- var ciphertext = encryptor.finalize(message);
-
- var cipherCfg = encryptor.cfg;
-
- return CipherParams.create({
- ciphertext: ciphertext,
- key: key,
- iv: cipherCfg.iv,
- algorithm: cipher,
- mode: cipherCfg.mode,
- padding: cipherCfg.padding,
- blockSize: cipher.blockSize,
- formatter: cfg.format
- });
- },
-
- decrypt: function (cipher, ciphertext, key, cfg) {
-
- cfg = this.cfg.extend(cfg);
-
- ciphertext = this._parse(ciphertext, cfg.format);
-
- var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
- return plaintext;
- },
-
- _parse: function (ciphertext, format) {
- if (typeof ciphertext == 'string') {
- return format.parse(ciphertext, this);
- } else {
- return ciphertext;
- }
- }
- });
-
- var C_kdf = C.kdf = {};
-
- var OpenSSLKdf = C_kdf.OpenSSL = {
-
- execute: function (password, keySize, ivSize, salt, hasher) {
-
- if (!salt) {
- salt = WordArray.random(64/8);
- }
-
- if (!hasher) {
- var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
- } else {
- var key = EvpKDF.create({ keySize: keySize + ivSize, hasher: hasher }).compute(password, salt);
- }
-
- var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
- key.sigBytes = keySize * 4;
-
- return CipherParams.create({ key: key, iv: iv, salt: salt });
- }
- };
-
- var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({
-
- cfg: SerializableCipher.cfg.extend({
- kdf: OpenSSLKdf
- }),
-
- encrypt: function (cipher, message, password, cfg) {
-
- cfg = this.cfg.extend(cfg);
-
- var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, cfg.salt, cfg.hasher);
-
- cfg.iv = derivedParams.iv;
-
- var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg);
-
- ciphertext.mixIn(derivedParams);
- return ciphertext;
- },
-
- decrypt: function (cipher, ciphertext, password, cfg) {
-
- cfg = this.cfg.extend(cfg);
-
- ciphertext = this._parse(ciphertext, cfg.format);
-
- var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt, cfg.hasher);
-
- cfg.iv = derivedParams.iv;
-
- var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg);
- return plaintext;
- }
- });
- }());
- }));
|