Skip to content

Commit f387521

Browse files
committed
Fixed exception throwing for invalid parameters.
1 parent 05dfc82 commit f387521

File tree

3 files changed

+234
-16
lines changed

3 files changed

+234
-16
lines changed

index.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// Make sure they are passing sensible data
1919
for (var i = 0; i < arg.length; i++) {
2020
if (arg[i] < 0 || arg[i] >= 256 || typeof arg[i] !== 'number') {
21-
throw new Error('invalid byte at index ' + i + '(' + arg[i] + ')');
21+
throw new Error('invalid byte (' + arg[i] + ':' + i + ')');
2222
}
2323
}
2424

@@ -186,6 +186,10 @@
186186

187187

188188
var AES = function(key) {
189+
if (!(this instanceof AES)) {
190+
throw Error('AES must be instanitated with `new`');
191+
}
192+
189193
this.key = createBuffer(key);
190194
this._prepare();
191195
}
@@ -195,7 +199,7 @@
195199

196200
var rounds = numberOfRounds[this.key.length];
197201
if (rounds == null) {
198-
throw new Error('invalid key size (must be length 16, 24 or 32)');
202+
throw new Error('invalid key size (must be 16, 24 or 32 bytes)');
199203
}
200204

201205
// encryption round keys
@@ -283,7 +287,7 @@
283287

284288
AES.prototype.encrypt = function(plaintext) {
285289
if (plaintext.length != 16) {
286-
return new Error('plaintext must be a block of size 16');
290+
throw new Error('invalid plaintext size (must be 16 bytes)');
287291
}
288292

289293
var rounds = this._Ke.length - 1;
@@ -322,7 +326,7 @@
322326

323327
AES.prototype.decrypt = function(ciphertext) {
324328
if (ciphertext.length != 16) {
325-
return new Error('ciphertext must be a block of size 16');
329+
throw new Error('invalid ciphertext size (must be 16 bytes)');
326330
}
327331

328332
var rounds = this._Kd.length - 1;
@@ -364,6 +368,10 @@
364368
* Mode Of Operation - Electonic Codebook (ECB)
365369
*/
366370
var ModeOfOperationECB = function(key) {
371+
if (!(this instanceof ModeOfOperationECB)) {
372+
throw Error('AES must be instanitated with `new`');
373+
}
374+
367375
this.description = "Electronic Code Block";
368376
this.name = "ecb";
369377

@@ -383,14 +391,18 @@
383391
* Mode Of Operation - Cipher Block Chaining (CBC)
384392
*/
385393
var ModeOfOperationCBC = function(key, iv) {
394+
if (!(this instanceof ModeOfOperationCBC)) {
395+
throw Error('AES must be instanitated with `new`');
396+
}
397+
386398
this.description = "Cipher Block Chaining";
387399
this.name = "cbc";
388400

389401
if (!iv) {
390-
iv = createBuffer([0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
402+
iv = createBuffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
391403

392404
} else if (iv.length != 16) {
393-
return new Error('initialation vector iv must be of length 16');
405+
throw new Error('invalid initialation vector size (must be 16 bytes)');
394406
}
395407

396408
this._lastCipherblock = createBuffer(iv);
@@ -400,7 +412,7 @@
400412

401413
ModeOfOperationCBC.prototype.encrypt = function(plaintext) {
402414
if (plaintext.length != 16) {
403-
return new Error('plaintext must be a block of size 16');
415+
throw new Error('invalid plaintext size (must be 16 bytes)');
404416
}
405417

406418
var precipherblock = createBuffer(plaintext);
@@ -415,7 +427,7 @@
415427

416428
ModeOfOperationCBC.prototype.decrypt = function(ciphertext) {
417429
if (ciphertext.length != 16) {
418-
return new Error('ciphertext must be a block of size 16');
430+
throw new Error('invalid ciphertext size (must be 16 bytes)');
419431
}
420432

421433
var plaintext = this._aes.decrypt(ciphertext);
@@ -433,14 +445,18 @@
433445
* Mode Of Operation - Cipher Feedback (CFB)
434446
*/
435447
var ModeOfOperationCFB = function(key, iv, segmentSize) {
448+
if (!(this instanceof ModeOfOperationCFB)) {
449+
throw Error('AES must be instanitated with `new`');
450+
}
451+
436452
this.description = "Cipher Feedback";
437453
this.name = "cfb";
438454

439455
if (!iv) {
440456
iv = createBuffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
441457

442458
} else if (iv.length != 16) {
443-
return new Error('initialation vector iv must be of length 16');
459+
throw new Error('invalid initialation vector size (must be 16 size)');
444460
}
445461

446462
if (!segmentSize) { segmentSize = 1; }
@@ -454,7 +470,7 @@
454470

455471
ModeOfOperationCFB.prototype.encrypt = function(plaintext) {
456472
if ((plaintext.length % this.segmentSize) != 0) {
457-
return new Error('plaintext must be a block of size module segmentSize (' + this.segmentSize + ')');
473+
throw new Error('invalid plaintext size (must be segmentSize bytes)');
458474
}
459475

460476
var encrypted = createBuffer(plaintext);
@@ -476,7 +492,7 @@
476492

477493
ModeOfOperationCFB.prototype.decrypt = function(ciphertext) {
478494
if ((ciphertext.length % this.segmentSize) != 0) {
479-
return new Error('ciphertext must be a block of size module segmentSize (' + this.segmentSize + ')');
495+
throw new Error('invalid ciphertext size (must be segmentSize bytes)');
480496
}
481497

482498
var plaintext = createBuffer(ciphertext);
@@ -501,14 +517,18 @@
501517
* Mode Of Operation - Output Feedback (OFB)
502518
*/
503519
var ModeOfOperationOFB = function(key, iv) {
520+
if (!(this instanceof ModeOfOperationOFB)) {
521+
throw Error('AES must be instanitated with `new`');
522+
}
523+
504524
this.description = "Output Feedback";
505525
this.name = "ofb";
506526

507527
if (!iv) {
508528
iv = createBuffer([0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
509529

510530
} else if (iv.length != 16) {
511-
return new Error('initialation vector iv must be of length 16');
531+
throw new Error('invalid initialation vector size (must be 16 bytes)');
512532
}
513533

514534
this._lastPrecipher = createBuffer(iv);
@@ -539,6 +559,10 @@
539559
* Counter object for CTR common mode of operation
540560
*/
541561
var Counter = function(initialValue) {
562+
if (!(this instanceof Counter)) {
563+
throw Error('Counter must be instanitated with `new`');
564+
}
565+
542566
// We allow 0, but anything false-ish uses the default 1
543567
if (initialValue !== 0 && !initialValue) { initialValue = 1; }
544568

@@ -553,7 +577,7 @@
553577

554578
Counter.prototype.setValue = function(value) {
555579
if (typeof(value) !== 'number' || parseInt(value) != value) {
556-
throw new Error('value must be an integer');
580+
throw new Error('invalid counter value (must be an integer)');
557581
}
558582

559583
for (var index = 15; index >= 0; --index) {
@@ -564,7 +588,7 @@
564588

565589
Counter.prototype.setBytes = function(bytes) {
566590
if (bytes.length != 16) {
567-
throw new Error('invalid counter bytes size (must be 16)');
591+
throw new Error('invalid counter bytes size (must be 16 bytes)');
568592
}
569593
this._counter = createBuffer(bytes);
570594
};
@@ -585,6 +609,10 @@
585609
* Mode Of Operation - Counter (CTR)
586610
*/
587611
var ModeOfOperationCTR = function(key, counter) {
612+
if (!(this instanceof ModeOfOperationCTR)) {
613+
throw Error('AES must be instanitated with `new`');
614+
}
615+
588616
this.description = "Counter";
589617
this.name = "ctr";
590618

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aes-js",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"bugs": {
55
"url": "http://github.com/ricmoo/aes-js/issues",
66
"email": "github@ricmoo.com"
@@ -11,7 +11,7 @@
1111
},
1212
"main": "index.js",
1313
"scripts": {
14-
"test": "node test/test-aes.js && node test/test-counter.js && node test/test-buffer.js"
14+
"test": "node test/test-aes.js && node test/test-counter.js && node test/test-buffer.js && node test/test-errors.js"
1515
},
1616
"repository": {
1717
"type": "git",

0 commit comments

Comments
 (0)