diff --git a/src/crypto/cfb.js b/src/crypto/cfb.js index c003bf54..3bb9e1c6 100644 --- a/src/crypto/cfb.js +++ b/src/crypto/cfb.js @@ -21,6 +21,8 @@ * @module crypto/cfb */ +'use strict'; + var util = require('../util.js'), cipher = require('./cipher'); @@ -51,10 +53,12 @@ module.exports = { prefixrandom = prefixrandom + prefixrandom.charAt(block_size - 2) + prefixrandom.charAt(block_size - 1); util.print_debug("prefixrandom:" + util.hexstrdump(prefixrandom)); - var ciphertext = ""; + var ciphertext = ''; var i, n; // 1. The feedback register (FR) is set to the IV, which is all zeros. - for (i = 0; i < block_size; i++) FR[i] = 0; + for (i = 0; i < block_size; i++) { + FR[i] = 0; + } // 2. FR is encrypted to produce FRE (FR Encrypted). This is the // encryption of an all-zero value. @@ -62,10 +66,14 @@ module.exports = { // 3. FRE is xored with the first BS octets of random data prefixed to // the plaintext to produce C[1] through C[BS], the first BS octets // of ciphertext. - for (i = 0; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ prefixrandom.charCodeAt(i)); + for (i = 0; i < block_size; i++) { + ciphertext += String.fromCharCode(FRE[i] ^ prefixrandom.charCodeAt(i)); + } // 4. FR is loaded with C[1] through C[BS]. - for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i); + for (i = 0; i < block_size; i++) { + FR[i] = ciphertext.charCodeAt(i); + } // 5. FR is encrypted to produce FRE, the encryption of the first BS // octets of ciphertext. @@ -79,9 +87,13 @@ module.exports = { if (resync) { // 7. (The resync step) FR is loaded with C3-C10. - for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i + 2); + for (i = 0; i < block_size; i++) { + FR[i] = ciphertext.charCodeAt(i + 2); + } } else { - for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i); + for (i = 0; i < block_size; i++) { + FR[i] = ciphertext.charCodeAt(i); + } } // 8. FR is encrypted to produce FRE. FRE = cipherfn.encrypt(FR, key); @@ -90,11 +102,14 @@ module.exports = { // 9. FRE is xored with the first 8 octets of the given plaintext, now // that we have finished encrypting the 10 octets of prefixed data. // This produces C11-C18, the next 8 octets of ciphertext. - for (i = 0; i < block_size; i++) + for (i = 0; i < block_size; i++) { ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(i)); + } for (n = block_size + 2; n < plaintext.length; n += block_size) { // 10. FR is loaded with C11-C18 - for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(n + i); + for (i = 0; i < block_size; i++) { + FR[i] = ciphertext.charCodeAt(n + i); + } // 11. FR is encrypted to produce FRE. FRE = cipherfn.encrypt(FR); @@ -102,8 +117,9 @@ module.exports = { // 12. FRE is xored with the next 8 octets of plaintext, to produce the // next 8 octets of ciphertext. These are loaded into FR and the // process is repeated until the plaintext is used up. - for (i = 0; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt((n - 2) + - i)); + for (i = 0; i < block_size; i++) { + ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt((n - 2) + i)); + } } } else { plaintext = " " + plaintext; @@ -163,7 +179,9 @@ module.exports = { // initialisation vector - for (i = 0; i < block_size; i++) iblock[i] = 0; + for (i = 0; i < block_size; i++) { + iblock[i] = 0; + } iblock = cipherfn.encrypt(iblock); for (i = 0; i < block_size; i++) { @@ -199,10 +217,12 @@ module.exports = { var iblock = new Array(block_size); var ablock = new Array(block_size); var i, n = ''; - var text = []; + var text = ''; // initialisation vector - for (i = 0; i < block_size; i++) iblock[i] = 0; + for (i = 0; i < block_size; i++) { + iblock[i] = 0; + } iblock = cipherfn.encrypt(iblock, key); for (i = 0; i < block_size; i++) { @@ -226,33 +246,34 @@ module.exports = { */ if (resync) { - for (i = 0; i < block_size; i++) iblock[i] = ciphertext.charCodeAt(i + 2); + for (i = 0; i < block_size; i++) { + iblock[i] = ciphertext.charCodeAt(i + 2); + } for (n = block_size + 2; n < ciphertext.length; n += block_size) { ablock = cipherfn.encrypt(iblock); for (i = 0; i < block_size && i + n < ciphertext.length; i++) { iblock[i] = ciphertext.charCodeAt(n + i); - text.push(String.fromCharCode(ablock[i] ^ iblock[i])); + text += String.fromCharCode(ablock[i] ^ iblock[i]); } } } else { - for (i = 0; i < block_size; i++) iblock[i] = ciphertext.charCodeAt(i); + for (i = 0; i < block_size; i++) { + iblock[i] = ciphertext.charCodeAt(i); + } for (n = block_size; n < ciphertext.length; n += block_size) { ablock = cipherfn.encrypt(iblock); for (i = 0; i < block_size && i + n < ciphertext.length; i++) { iblock[i] = ciphertext.charCodeAt(n + i); - text.push(String.fromCharCode(ablock[i] ^ iblock[i])); + text += String.fromCharCode(ablock[i] ^ iblock[i]); } } } n = resync ? 0 : 2; - text = text.join(''); - text = text.substring(n, ciphertext.length - block_size - 2 + n); - return text; }, @@ -261,47 +282,50 @@ module.exports = { cipherfn = new cipher[cipherfn](key); var block_size = cipherfn.blockSize; - var blocki = ""; - var blockc = ""; + var blocki = ''; + var blockc = ''; var pos = 0; - var cyphertext = []; - var tempBlock = []; + var cyphertext = ''; + var tempBlock = ''; blockc = iv.substring(0, block_size); while (plaintext.length > block_size * pos) { var encblock = cipherfn.encrypt(util.str2bin(blockc)); blocki = plaintext.substring((pos * block_size), (pos * block_size) + block_size); - for (var i = 0; i < blocki.length; i++) - tempBlock.push(String.fromCharCode(blocki.charCodeAt(i) ^ encblock[i])); - blockc = tempBlock.join(''); - tempBlock = []; - cyphertext.push(blockc); + for (var i = 0; i < blocki.length; i++) { + tempBlock += String.fromCharCode(blocki.charCodeAt(i) ^ encblock[i]); + } + blockc = tempBlock; + tempBlock = ''; + cyphertext += blockc; pos++; } - return cyphertext.join(''); + return cyphertext; }, normalDecrypt: function(cipherfn, key, ciphertext, iv) { cipherfn = new cipher[cipherfn](key); var block_size = cipherfn.blockSize; - var blockp = ""; + var blockp = ''; var pos = 0; - var plaintext = []; + var plaintext = ''; var offset = 0; var i; if (iv === null) - for (i = 0; i < block_size; i++) blockp += String.fromCharCode(0); + for (i = 0; i < block_size; i++) { + blockp += String.fromCharCode(0); + } else blockp = iv.substring(0, block_size); while (ciphertext.length > (block_size * pos)) { var decblock = cipherfn.encrypt(util.str2bin(blockp)); blockp = ciphertext.substring((pos * (block_size)) + offset, (pos * (block_size)) + (block_size) + offset); for (i = 0; i < blockp.length; i++) { - plaintext.push(String.fromCharCode(blockp.charCodeAt(i) ^ decblock[i])); + plaintext += String.fromCharCode(blockp.charCodeAt(i) ^ decblock[i]); } pos++; } - return plaintext.join(''); + return plaintext; } }; diff --git a/src/crypto/cipher/aes.js b/src/crypto/cipher/aes.js index 3892aa08..db3a76fe 100644 --- a/src/crypto/cipher/aes.js +++ b/src/crypto/cipher/aes.js @@ -16,6 +16,8 @@ * @module crypto/cipher/aes */ +'use strict'; + var util = require('../../util.js'); // The round constants used in subkey expansion