-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmcrypt.js
212 lines (195 loc) · 5.06 KB
/
mcrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Modified from jsmcrypt version 0.1 - Copyright 2012 F. Doering
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
var rijndael = require('./rijndael');
var ciphers = {
'rijndael-128': {blockSize: 16, keySize: 32},
'rijndael-192': {blockSize: 24, keySize: 32},
'rijndael-256': {blockSize: 32, keySize: 32}
};
var rijndaelCipher = function(block, key, encrypt) {
if(encrypt) {
rijndael.encrypt(block, key);
} else {
rijndael.decrypt(block, key);
}
return block;
};
var getBlockSize = function(cipher) {
if(!ciphers[cipher]) {
return false;
}
return ciphers[cipher].blockSize;
};
var getKeySize = function(cipher) {
if(!ciphers[cipher]) {
return false;
}
return ciphers[cipher].keySize;
};
var crypt = function(encrypt, text, IV, key, cipher, mode) {
if(!text) return true;
var blockS = ciphers[cipher].blockSize;
var chunkS = blockS;
var iv = [];
switch(mode) {
case 'cfb':
chunkS = 1;
case 'cbc':
case 'ncfb':
case 'nofb':
case 'ctr':
if(!IV) {
throw 'JS-Rijndael crypt: IV is required for mode ' + mode;
}
if(IV.length != blockS) {
throw 'JS-Rijndael crypt: IV must be ' + blockS + ' bytes long for ' + cipher;
}
iv = IV.slice();
break;
case 'ecb':
break;
default:
throw 'JS-Rijndael crypt: Unsupported mode of operation ' + cMode;
}
var chunks = Math.ceil(text.length/chunkS);
var orig = text.length;
while(text.length < chunks*chunkS) {
text.push(0);
}
var out = [];
switch(mode) {
case 'ecb':
for(var i = 0; i < chunks; i++) {
for(var j = 0; j < chunkS; j++) {
iv[j] = text[(i*chunkS)+j];
}
rijndaelCipher(iv, key, encrypt);
for(var j = 0; j < chunkS; j++) {
out.push(iv[j]);
}
var last;
do {
last = out.pop();
} while(last == 0)
out.push(last);
}
break;
case 'cbc':
if(encrypt) {
for(var i = 0; i < chunks; i++) {
for(var j = 0; j < chunkS; j++) {
iv[j] = text[(i*chunkS)+j]^iv[j];
}
rijndaelCipher(iv, key, true);
for(var j = 0; j < chunkS; j++) {
out.push(iv[j]);
}
}
} else {
for(var i = 0; i < chunks; i++) {
var temp = iv;
iv = new Array(chunkS);
for(var j = 0; j < chunkS; j++) {
iv[j] = text[(i*chunkS)+j];
}
var decr = iv.slice(0);
rijndaelCipher(decr, key, false);
for(var j = 0; j < chunkS; j++) {
out.push(temp[j]^decr[j]);
}
}
var last;
do {
last = out.pop();
} while(last == 0)
out.push(last);
}
break;
case 'cfb':
for(var i = 0; i < chunks; i++) {
var temp = iv.slice(0);
rijndaelCipher(temp, key, true);
temp = temp[0]^text[i];
iv.push(encrypt?temp:text[i]);
iv.shift();
out.push(temp);
}
out = out.splice(0, orig);
break;
case 'ncfb':
for(var i = 0; i < chunks; i++) {
rijndaelCipher(iv, key, true);
for(var j = 0; j < chunkS; j++) {
var temp = text[(i*chunkS)+j];
iv[j] = temp^iv[j];
out.push(iv[j]);
if(!encrypt) {
iv[j] = temp;
}
}
}
out = out.splice(0, orig);
break;
case 'nofb':
for(var i = 0; i < chunks; i++) {
rijndaelCipher(iv, key, true);
for(var j = 0; j < chunkS; j++) {
out.push(text[(i*chunkS)+j]^iv[j]);
}
}
out = out.slice(0, orig);
break;
case 'ctr':
for(var i = 0; i < chunks; i++) {
temp = iv.slice(0);
rijndaelCipher(temp, key, true);
for(var j = 0; j < chunkS; j++) {
out.push(text[(i*chunkS)+j]^temp[j]);
}
var carry = 1;
var index = chunkS;
do {
index--;
iv[index]++;
carry = iv[index] >> 8;
iv[index] &= 255;
} while(carry)
}
out = out.slice(0, orig);
break;
}
return out;
};
module.exports = {
encrypt: function(message, IV, key, cipher, mode) {
return crypt(true, message, IV, key, cipher, mode);
},
decrypt: function(ctext, IV, key, cipher, mode) {
return crypt(false, ctext, IV, key, cipher, mode);
},
getBlockSize: getBlockSize,
getIVSize: getBlockSize,
getKeySize: getKeySize,
listAlgorithms: function() {
return Object.keys(ciphers);
},
listModes: function() {
return ['ecb', 'cbc', 'cfb', 'ncfb', 'nofb', 'ctr'];
}
}