CryptTool.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. const common = require('../common');
  3. const fc = require('fast-check');
  4. const fs = require('fs');
  5. describe('CryptTool', function () {
  6. describe('cipher & decipher', function () {
  7. afterEach(async function () {
  8. // pause to let async functions conclude
  9. await new Promise(resolve => setTimeout(resolve, 1900));
  10. });
  11. this.timeout(30000);
  12. it('can en- and decrypt any message', async function () {
  13. await fc.assert(fc.asyncProperty(
  14. fc.string(),
  15. fc.string(),
  16. fc.string(),
  17. async function (key, password, message) {
  18. const clean = globalThis.cleanup();
  19. // ensure zlib is getting loaded
  20. PrivateBin.Controller.initZlib();
  21. Object.defineProperty(window, 'crypto', {
  22. value: new WebCrypto(),
  23. configurable: true,
  24. enumerable: true,
  25. writable: false
  26. });
  27. global.atob = common.atob;
  28. global.btoa = common.btoa;
  29. message = message.trim();
  30. const cipherMessage = await PrivateBin.CryptTool.cipher(
  31. key, password, message, []
  32. ),
  33. plaintext = await PrivateBin.CryptTool.decipher(
  34. key, password, cipherMessage
  35. );
  36. clean();
  37. const result = (message === plaintext);
  38. if (!result) console.log(plaintext, cipherMessage);
  39. return result;
  40. }
  41. ),
  42. {numRuns: 3});
  43. });
  44. it('does not truncate messages', async function () {
  45. const message = fs.readFileSync('test/compression-sample.txt', 'ascii').trim(),
  46. clean = globalThis.cleanup();
  47. Object.defineProperty(window, 'crypto', {
  48. value: new WebCrypto(),
  49. configurable: true,
  50. enumerable: true,
  51. writable: false
  52. });
  53. // ensure zlib is getting loaded
  54. PrivateBin.Controller.initZlib();
  55. global.atob = common.atob;
  56. global.btoa = common.btoa;
  57. const cipherMessage = await PrivateBin.CryptTool.cipher(
  58. 'foo', 'bar', message, []
  59. ),
  60. plaintext = await PrivateBin.CryptTool.decipher(
  61. 'foo', 'bar', cipherMessage
  62. );
  63. clean();
  64. if (message !== plaintext) {
  65. console.log(plaintext, cipherMessage);
  66. }
  67. assert.strictEqual(message, plaintext);
  68. });
  69. it('authenticates the document and parent IDs of comments', async function () {
  70. const clean = globalThis.cleanup();
  71. Object.defineProperty(window, 'crypto', {
  72. value: new WebCrypto(),
  73. configurable: true,
  74. enumerable: true,
  75. writable: false
  76. });
  77. // ensure zlib is getting loaded
  78. PrivateBin.Controller.initZlib();
  79. global.atob = common.atob;
  80. global.btoa = common.btoa;
  81. const cipherMessage = await PrivateBin.CryptTool.cipher(
  82. 'foo', 'bar', 'baz', ['5b65a01b43987bc2', '33a5671d11b29d01']
  83. ),
  84. plaintext = await PrivateBin.CryptTool.decipher(
  85. 'foo', 'bar', cipherMessage
  86. );
  87. cipherMessage[1][9] = '1111111111111111';
  88. const tampered = await PrivateBin.CryptTool.decipher(
  89. 'foo', 'bar', cipherMessage
  90. );
  91. clean();
  92. assert.strictEqual(cipherMessage[1].length, 10);
  93. assert.strictEqual(cipherMessage[1][8], '5b65a01b43987bc2');
  94. assert.strictEqual(plaintext, 'baz');
  95. assert.strictEqual(tampered, '');
  96. });
  97. it('can en- and decrypt a particular message (#260)', async function () {
  98. await fc.assert(fc.asyncProperty(
  99. fc.string(),
  100. fc.string(),
  101. async function (key, password) {
  102. const message = `
  103. 1 subgoal
  104. inv : Assert
  105. expr : Expr
  106. sBody : Instr
  107. deduction : (|- [|inv /\ assertOfExpr expr|] sBody [|inv|])%assert
  108. IHdeduction : (|= [|inv /\ assertOfExpr expr |] sBody [|inv|])%assert
  109. mem : Mem
  110. preInMem : inv mem
  111. m : Mem
  112. n : nat
  113. interpRel : interp (nth_iterate sBody n) (MemElem mem) = CpoElem Mem m
  114. lastIter : interp (nth_iterate sBody n) (MemElem mem) |=e expr_neg expr
  115. notLastIter : forall p : nat,
  116. p < n -> interp (nth_iterate sBody p) (MemElem mem) |=e expr
  117. isWhile : interp (while expr sBody) (MemElem mem) =
  118. interp (nth_iterate sBody n) (MemElem mem)
  119. ======================== ( 1 / 1 )
  120. conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
  121. `;
  122. const clean = globalThis.cleanup();
  123. // ensure zlib is getting loaded
  124. PrivateBin.Controller.initZlib();
  125. Object.defineProperty(window, 'crypto', {
  126. value: new WebCrypto(),
  127. configurable: true,
  128. enumerable: true,
  129. writable: false
  130. });
  131. global.atob = common.atob;
  132. global.btoa = common.btoa;
  133. const cipherMessage = await PrivateBin.CryptTool.cipher(
  134. key, password, message, []
  135. ),
  136. plaintext = await PrivateBin.CryptTool.decipher(
  137. key, password, cipherMessage
  138. );
  139. clean();
  140. const result = (message === plaintext);
  141. if (!result) console.log(plaintext, cipherMessage);
  142. return result;
  143. }
  144. ),
  145. {numRuns: 3});
  146. });
  147. });
  148. describe('getSymmetricKey', function () {
  149. this.timeout(10000);
  150. let keys = [];
  151. // the parameter is used to ensure the test is run more then one time
  152. it('returns random, non-empty keys', () => {
  153. fc.assert(fc.property(
  154. fc.integer(),
  155. function() {
  156. const clean = globalThis.cleanup();
  157. Object.defineProperty(window, 'crypto', {
  158. value: new WebCrypto(),
  159. configurable: true,
  160. enumerable: true,
  161. writable: false
  162. });
  163. const key = PrivateBin.CryptTool.getSymmetricKey(),
  164. result = (key !== '' && keys.indexOf(key) === -1);
  165. keys.push(key);
  166. clean();
  167. return result;
  168. }
  169. ),
  170. {numRuns: 10});
  171. });
  172. });
  173. });