CryptTool.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.initZ();
  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.initZ();
  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('can en- and decrypt a particular message (#260)', async function () {
  70. await fc.assert(fc.asyncProperty(
  71. fc.string(),
  72. fc.string(),
  73. async function (key, password) {
  74. const message = `
  75. 1 subgoal
  76. inv : Assert
  77. expr : Expr
  78. sBody : Instr
  79. deduction : (|- [|inv /\ assertOfExpr expr|] sBody [|inv|])%assert
  80. IHdeduction : (|= [|inv /\ assertOfExpr expr |] sBody [|inv|])%assert
  81. mem : Mem
  82. preInMem : inv mem
  83. m : Mem
  84. n : nat
  85. interpRel : interp (nth_iterate sBody n) (MemElem mem) = CpoElem Mem m
  86. lastIter : interp (nth_iterate sBody n) (MemElem mem) |=e expr_neg expr
  87. notLastIter : forall p : nat,
  88. p < n -> interp (nth_iterate sBody p) (MemElem mem) |=e expr
  89. isWhile : interp (while expr sBody) (MemElem mem) =
  90. interp (nth_iterate sBody n) (MemElem mem)
  91. ======================== ( 1 / 1 )
  92. conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
  93. `;
  94. const clean = globalThis.cleanup();
  95. // ensure zlib is getting loaded
  96. PrivateBin.Controller.initZ();
  97. Object.defineProperty(window, 'crypto', {
  98. value: new WebCrypto(),
  99. configurable: true,
  100. enumerable: true,
  101. writable: false
  102. });
  103. const cipherMessage = await PrivateBin.CryptTool.cipher(
  104. key, password, message, []
  105. ),
  106. plaintext = await PrivateBin.CryptTool.decipher(
  107. key, password, cipherMessage
  108. );
  109. clean();
  110. const result = (message === plaintext);
  111. if (!result) console.log(plaintext, cipherMessage);
  112. return result;
  113. }
  114. ),
  115. {numRuns: 3});
  116. });
  117. });
  118. describe('getSymmetricKey', function () {
  119. this.timeout(10000);
  120. let keys = [];
  121. // the parameter is used to ensure the test is run more then one time
  122. it('returns random, non-empty keys', () => {
  123. fc.assert(fc.property(
  124. fc.integer(),
  125. function(counter) {
  126. const clean = globalThis.cleanup();
  127. Object.defineProperty(window, 'crypto', {
  128. value: new WebCrypto(),
  129. configurable: true,
  130. enumerable: true,
  131. writable: false
  132. });
  133. const key = PrivateBin.CryptTool.getSymmetricKey(),
  134. result = (key !== '' && keys.indexOf(key) === -1);
  135. keys.push(key);
  136. clean();
  137. return result;
  138. }
  139. ),
  140. {numRuns: 10});
  141. });
  142. });
  143. });