decrypter.test.js 5.0 KB

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