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