|
18 | 18 | // Make sure they are passing sensible data |
19 | 19 | for (var i = 0; i < arg.length; i++) { |
20 | 20 | 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 + ')'); |
22 | 22 | } |
23 | 23 | } |
24 | 24 |
|
|
186 | 186 |
|
187 | 187 |
|
188 | 188 | var AES = function(key) { |
| 189 | + if (!(this instanceof AES)) { |
| 190 | + throw Error('AES must be instanitated with `new`'); |
| 191 | + } |
| 192 | + |
189 | 193 | this.key = createBuffer(key); |
190 | 194 | this._prepare(); |
191 | 195 | } |
|
195 | 199 |
|
196 | 200 | var rounds = numberOfRounds[this.key.length]; |
197 | 201 | 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)'); |
199 | 203 | } |
200 | 204 |
|
201 | 205 | // encryption round keys |
|
283 | 287 |
|
284 | 288 | AES.prototype.encrypt = function(plaintext) { |
285 | 289 | 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)'); |
287 | 291 | } |
288 | 292 |
|
289 | 293 | var rounds = this._Ke.length - 1; |
|
322 | 326 |
|
323 | 327 | AES.prototype.decrypt = function(ciphertext) { |
324 | 328 | 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)'); |
326 | 330 | } |
327 | 331 |
|
328 | 332 | var rounds = this._Kd.length - 1; |
|
364 | 368 | * Mode Of Operation - Electonic Codebook (ECB) |
365 | 369 | */ |
366 | 370 | var ModeOfOperationECB = function(key) { |
| 371 | + if (!(this instanceof ModeOfOperationECB)) { |
| 372 | + throw Error('AES must be instanitated with `new`'); |
| 373 | + } |
| 374 | + |
367 | 375 | this.description = "Electronic Code Block"; |
368 | 376 | this.name = "ecb"; |
369 | 377 |
|
|
383 | 391 | * Mode Of Operation - Cipher Block Chaining (CBC) |
384 | 392 | */ |
385 | 393 | var ModeOfOperationCBC = function(key, iv) { |
| 394 | + if (!(this instanceof ModeOfOperationCBC)) { |
| 395 | + throw Error('AES must be instanitated with `new`'); |
| 396 | + } |
| 397 | + |
386 | 398 | this.description = "Cipher Block Chaining"; |
387 | 399 | this.name = "cbc"; |
388 | 400 |
|
389 | 401 | 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]); |
391 | 403 |
|
392 | 404 | } 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)'); |
394 | 406 | } |
395 | 407 |
|
396 | 408 | this._lastCipherblock = createBuffer(iv); |
|
400 | 412 |
|
401 | 413 | ModeOfOperationCBC.prototype.encrypt = function(plaintext) { |
402 | 414 | 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)'); |
404 | 416 | } |
405 | 417 |
|
406 | 418 | var precipherblock = createBuffer(plaintext); |
|
415 | 427 |
|
416 | 428 | ModeOfOperationCBC.prototype.decrypt = function(ciphertext) { |
417 | 429 | 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)'); |
419 | 431 | } |
420 | 432 |
|
421 | 433 | var plaintext = this._aes.decrypt(ciphertext); |
|
433 | 445 | * Mode Of Operation - Cipher Feedback (CFB) |
434 | 446 | */ |
435 | 447 | var ModeOfOperationCFB = function(key, iv, segmentSize) { |
| 448 | + if (!(this instanceof ModeOfOperationCFB)) { |
| 449 | + throw Error('AES must be instanitated with `new`'); |
| 450 | + } |
| 451 | + |
436 | 452 | this.description = "Cipher Feedback"; |
437 | 453 | this.name = "cfb"; |
438 | 454 |
|
439 | 455 | if (!iv) { |
440 | 456 | iv = createBuffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
441 | 457 |
|
442 | 458 | } 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)'); |
444 | 460 | } |
445 | 461 |
|
446 | 462 | if (!segmentSize) { segmentSize = 1; } |
|
454 | 470 |
|
455 | 471 | ModeOfOperationCFB.prototype.encrypt = function(plaintext) { |
456 | 472 | 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)'); |
458 | 474 | } |
459 | 475 |
|
460 | 476 | var encrypted = createBuffer(plaintext); |
|
476 | 492 |
|
477 | 493 | ModeOfOperationCFB.prototype.decrypt = function(ciphertext) { |
478 | 494 | 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)'); |
480 | 496 | } |
481 | 497 |
|
482 | 498 | var plaintext = createBuffer(ciphertext); |
|
501 | 517 | * Mode Of Operation - Output Feedback (OFB) |
502 | 518 | */ |
503 | 519 | var ModeOfOperationOFB = function(key, iv) { |
| 520 | + if (!(this instanceof ModeOfOperationOFB)) { |
| 521 | + throw Error('AES must be instanitated with `new`'); |
| 522 | + } |
| 523 | + |
504 | 524 | this.description = "Output Feedback"; |
505 | 525 | this.name = "ofb"; |
506 | 526 |
|
507 | 527 | if (!iv) { |
508 | 528 | iv = createBuffer([0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
509 | 529 |
|
510 | 530 | } 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)'); |
512 | 532 | } |
513 | 533 |
|
514 | 534 | this._lastPrecipher = createBuffer(iv); |
|
539 | 559 | * Counter object for CTR common mode of operation |
540 | 560 | */ |
541 | 561 | var Counter = function(initialValue) { |
| 562 | + if (!(this instanceof Counter)) { |
| 563 | + throw Error('Counter must be instanitated with `new`'); |
| 564 | + } |
| 565 | + |
542 | 566 | // We allow 0, but anything false-ish uses the default 1 |
543 | 567 | if (initialValue !== 0 && !initialValue) { initialValue = 1; } |
544 | 568 |
|
|
553 | 577 |
|
554 | 578 | Counter.prototype.setValue = function(value) { |
555 | 579 | 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)'); |
557 | 581 | } |
558 | 582 |
|
559 | 583 | for (var index = 15; index >= 0; --index) { |
|
564 | 588 |
|
565 | 589 | Counter.prototype.setBytes = function(bytes) { |
566 | 590 | 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)'); |
568 | 592 | } |
569 | 593 | this._counter = createBuffer(bytes); |
570 | 594 | }; |
|
585 | 609 | * Mode Of Operation - Counter (CTR) |
586 | 610 | */ |
587 | 611 | var ModeOfOperationCTR = function(key, counter) { |
| 612 | + if (!(this instanceof ModeOfOperationCTR)) { |
| 613 | + throw Error('AES must be instanitated with `new`'); |
| 614 | + } |
| 615 | + |
588 | 616 | this.description = "Counter"; |
589 | 617 | this.name = "ctr"; |
590 | 618 |
|
|
0 commit comments