decrypter.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // see docs/hlse.md for instructions on how test data was generated
  2. import QUnit from 'qunit';
  3. import {unpad} from 'pkcs7';
  4. import sinon from 'sinon';
  5. import {decrypt, Decrypter, AsyncStream} from '../src';
  6. // see docs/hlse.md for instructions on how test data was generated
  7. const stringFromBytes = function(bytes) {
  8. let result = '';
  9. for (let i = 0; i < bytes.length; i++) {
  10. result += String.fromCharCode(bytes[i]);
  11. }
  12. return result;
  13. };
  14. QUnit.module('Decryption');
  15. QUnit.test('decrypts a single AES-128 with PKCS7 block', function() {
  16. let key = new Uint32Array([0, 0, 0, 0]);
  17. let initVector = key;
  18. // the string "howdy folks" encrypted
  19. let encrypted = new Uint8Array([
  20. 0xce, 0x90, 0x97, 0xd0,
  21. 0x08, 0x46, 0x4d, 0x18,
  22. 0x4f, 0xae, 0x01, 0x1c,
  23. 0x82, 0xa8, 0xf0, 0x67
  24. ]);
  25. QUnit.deepEqual('howdy folks',
  26. stringFromBytes(unpad(decrypt(encrypted, key, initVector))),
  27. 'decrypted with a byte array key'
  28. );
  29. });
  30. QUnit.test('decrypts multiple AES-128 blocks with CBC', function() {
  31. let key = new Uint32Array([0, 0, 0, 0]);
  32. let initVector = key;
  33. // the string "0123456789abcdef01234" encrypted
  34. let encrypted = new Uint8Array([
  35. 0x14, 0xf5, 0xfe, 0x74,
  36. 0x69, 0x66, 0xf2, 0x92,
  37. 0x65, 0x1c, 0x22, 0x88,
  38. 0xbb, 0xff, 0x46, 0x09,
  39. 0x0b, 0xde, 0x5e, 0x71,
  40. 0x77, 0x87, 0xeb, 0x84,
  41. 0xa9, 0x54, 0xc2, 0x45,
  42. 0xe9, 0x4e, 0x29, 0xb3
  43. ]);
  44. QUnit.deepEqual('0123456789abcdef01234',
  45. stringFromBytes(unpad(decrypt(encrypted, key, initVector))),
  46. 'decrypted multiple blocks');
  47. });
  48. QUnit.test(
  49. 'verify that the deepcopy works by doing two decrypts in the same test',
  50. function() {
  51. let key = new Uint32Array([0, 0, 0, 0]);
  52. let initVector = key;
  53. // the string "howdy folks" encrypted
  54. let pkcs7Block = new Uint8Array([
  55. 0xce, 0x90, 0x97, 0xd0,
  56. 0x08, 0x46, 0x4d, 0x18,
  57. 0x4f, 0xae, 0x01, 0x1c,
  58. 0x82, 0xa8, 0xf0, 0x67
  59. ]);
  60. QUnit.deepEqual('howdy folks',
  61. stringFromBytes(unpad(decrypt(pkcs7Block, key, initVector))),
  62. 'decrypted with a byte array key'
  63. );
  64. // the string "0123456789abcdef01234" encrypted
  65. let cbcBlocks = new Uint8Array([
  66. 0x14, 0xf5, 0xfe, 0x74,
  67. 0x69, 0x66, 0xf2, 0x92,
  68. 0x65, 0x1c, 0x22, 0x88,
  69. 0xbb, 0xff, 0x46, 0x09,
  70. 0x0b, 0xde, 0x5e, 0x71,
  71. 0x77, 0x87, 0xeb, 0x84,
  72. 0xa9, 0x54, 0xc2, 0x45,
  73. 0xe9, 0x4e, 0x29, 0xb3
  74. ]);
  75. QUnit.deepEqual('0123456789abcdef01234',
  76. stringFromBytes(unpad(decrypt(cbcBlocks, key, initVector))),
  77. 'decrypted multiple blocks');
  78. });
  79. QUnit.module('Incremental Processing', {
  80. beforeEach() {
  81. this.clock = sinon.useFakeTimers();
  82. },
  83. afterEach() {
  84. this.clock.restore();
  85. }
  86. });
  87. QUnit.test('executes a callback after a timeout', function() {
  88. let asyncStream = new AsyncStream();
  89. let calls = '';
  90. asyncStream.push(function() {
  91. calls += 'a';
  92. });
  93. this.clock.tick(asyncStream.delay);
  94. QUnit.equal(calls, 'a', 'invoked the callback once');
  95. this.clock.tick(asyncStream.delay);
  96. QUnit.equal(calls, 'a', 'only invoked the callback once');
  97. });
  98. QUnit.test('executes callback in series', function() {
  99. let asyncStream = new AsyncStream();
  100. let calls = '';
  101. asyncStream.push(function() {
  102. calls += 'a';
  103. });
  104. asyncStream.push(function() {
  105. calls += 'b';
  106. });
  107. this.clock.tick(asyncStream.delay);
  108. QUnit.equal(calls, 'a', 'invoked the first callback');
  109. this.clock.tick(asyncStream.delay);
  110. QUnit.equal(calls, 'ab', 'invoked the second');
  111. });
  112. QUnit.module('Incremental Decryption', {
  113. beforeEach() {
  114. this.clock = sinon.useFakeTimers();
  115. },
  116. afterEach() {
  117. this.clock.restore();
  118. }
  119. });
  120. QUnit.test('asynchronously decrypts a 4-word block', function() {
  121. let key = new Uint32Array([0, 0, 0, 0]);
  122. let initVector = key;
  123. // the string "howdy folks" encrypted
  124. let encrypted = new Uint8Array([0xce, 0x90, 0x97, 0xd0,
  125. 0x08, 0x46, 0x4d, 0x18,
  126. 0x4f, 0xae, 0x01, 0x1c,
  127. 0x82, 0xa8, 0xf0, 0x67]);
  128. let decrypted;
  129. let decrypter = new Decrypter(encrypted,
  130. key,
  131. initVector,
  132. function(error, result) {
  133. if (error) {
  134. throw new Error(error);
  135. }
  136. decrypted = result;
  137. });
  138. QUnit.ok(!decrypted, 'asynchronously decrypts');
  139. this.clock.tick(decrypter.asyncStream_.delay * 2);
  140. QUnit.ok(decrypted, 'completed decryption');
  141. QUnit.deepEqual('howdy folks',
  142. stringFromBytes(decrypted),
  143. 'decrypts and unpads the result');
  144. });
  145. QUnit.test('breaks up input greater than the step value', function() {
  146. let encrypted = new Int32Array(Decrypter.STEP + 4);
  147. let done = false;
  148. let decrypter = new Decrypter(encrypted,
  149. new Uint32Array(4),
  150. new Uint32Array(4),
  151. function() {
  152. done = true;
  153. });
  154. this.clock.tick(decrypter.asyncStream_.delay * 2);
  155. QUnit.ok(!done, 'not finished after two ticks');
  156. this.clock.tick(decrypter.asyncStream_.delay);
  157. QUnit.ok(done, 'finished after the last chunk is decrypted');
  158. });