| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 'use strict';
- const common = require('../common');
- describe('CryptTool', function () {
- describe('cipher & decipher', function () {
- afterEach(async function () {
- // pause to let async functions conclude
- await new Promise(resolve => setTimeout(resolve, 1900));
- });
- this.timeout(30000);
- it('can en- and decrypt any message', function () {
- jsc.assert(jsc.forall(
- 'string',
- 'string',
- 'string',
- async function (key, password, message) {
- const clean = jsdom();
- // ensure zlib is getting loaded
- $.PrivateBin.Controller.initZ();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- global.atob = common.atob;
- global.btoa = common.btoa;
- message = message.trim();
- const cipherMessage = await $.PrivateBin.CryptTool.cipher(
- key, password, message, []
- ),
- plaintext = await $.PrivateBin.CryptTool.decipher(
- key, password, cipherMessage
- );
- clean();
- const result = (message === plaintext);
- if (!result) console.log(plaintext, cipherMessage);
- return result;
- }
- ),
- {tests: 3});
- });
- // The below static unit tests are included to ensure deciphering of "classic"
- // SJCL based pastes still works
- it(
- 'supports PrivateBin v1 ciphertext with password (SJCL & browser atob)',
- async function () {
- delete global.Base64;
- const clean = jsdom();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- global.atob = common.atob;
- // Of course you can easily decipher the following texts, if you like.
- // Bonus points for finding their sources and hidden meanings.
- const paste = await $.PrivateBin.CryptTool.decipher(
- '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
- // -- "That's amazing. I've got the same combination on my luggage."
- Array.apply(0, Array(6)).map((_,b) => b + 1).join(''),
- '{"iv":"4HNFIl7eYbCh6HuShctTIA==","v":1,"iter":10000,"ks"' +
- ':256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","sa' +
- 'lt":"u0lQvePq6L0=","ct":"fGPUVrDyaVr1ZDGb+kqQ3CPEW8x4YKG' +
- 'fzHDmA0Vjkh250aWNe7Cnigkps9aaFVMX9AaerrTp3yZbojJtNqVGMfL' +
- 'dUTu+53xmZHqRKxCCqSfDNSNoW4Oxk5OVgAtRyuG4bXHDsWTXDNz2xce' +
- 'qzVFqhkwTwlUchrV7uuFK/XUKTNjPFM744moivIcBbfM2FOeKlIFs8RY' +
- 'PYuvqQhp2rMLlNGwwKh//4kykQsHMQDeSDuJl8stMQzgWR/btUBZuwNZ' +
- 'EydkMH6IPpTdf5WTSrZ+wC2OK0GutCm4UaEe6txzaTMfu+WRVu4PN6q+' +
- 'N+2zljWJ1XdpVcN/i0Sv4QVMym0Xa6y0eccEhj/69o47PmExmMMeEwEx' +
- 'ImPalMNT9JUSiZdOZJ/GdzwrwoIuq1mdQR6vSH+XJ/8jXJQ7bjjJVJYX' +
- 'TcT0Di5jixArI2Kpp1GGlGVFbLgPugwU1wczg+byqeDOAECXRRnQcoge' +
- 'aJtVcRwXwfy4j3ORFcblYMilxyHqKBewcYPRVBGtBs50cVjSIkAfR84r' +
- 'nc1nfvnxK/Gmm+4VBNHI6ODWNpRolVMCzXjbKYnV3Are5AgSpsTqaGl4' +
- '1VJGpcco6cAwi4K0Bys1seKR+bLSdUgqRrkEqSRSdu3/VTu9HhEk8an0' +
- 'rjTE4CBB5/LMn16p0TGLoOb32odKFIEtpanVvLjeyiVMvSxcgYLNnTi/' +
- '5FiaAC4pJxRD+AZHedU1FICUeEXxIcac/4E5qjkHjX9SpQtLl80QLIVn' +
- 'jNliZm7QLB/nKu7W8Jb0+/CiTdV3Q9LhxlH4ciprnX+W0B00BKYFHnL9' +
- 'jRVzKdXhf1EHydbXMAfpCjHAXIVCkFakJinQBDIIw/SC6Yig0u0ddEID' +
- '2B7LYAP1iE4RZwzTrxCB+ke2jQr8c20Jj6u6ShFOPC9DCw9XupZ4HAal' +
- 'VG00kSgjus+b8zrVji3/LKEhb4EBzp1ctBJCFTeXwej8ZETLoXTylev5' +
- 'dlwZSYAbuBPPcbFR/xAIPx3uDabd1E1gTqUc68ICIGhd197Mb2eRWiSv' +
- 'Hr5SPsASerMxId6XA6+iQlRiI+NDR+TGVNmCnfxSlyPFMOHGTmslXOGI' +
- 'qGfBR8l4ft8YVZ70lCwmwTuViGc75ULSf9mM57/LmRzQFMYQtvI8IFK9' +
- 'JaQEMY5xz0HLtR4iyQUUdwR9e0ytBNdWF2a2WPDEnJuY/QJo4GzTlgv4' +
- 'QUxMXI5htsn2rf0HxCFu7Po8DNYLxTS+67hYjDIYWYaEIc8LXWMLyDm9' +
- 'C5fARPJ4F2BIWgzgzkNj+dVjusft2XnziamWdbS5u3kuRlVuz5LQj+R5' +
- 'imnqQAincdZTkTT1nYx+DatlOLllCYIHffpI="}'
- );
- clean();
- const result = typeof paste === 'string' && paste.includes('securely packed in iron');
- if (!result) console.log(paste);
- assert.ok(result);
- }
- );
- it(
- 'supports PrivateBin v1 ciphertext no password (SJCL & browser atob)',
- async function () {
- delete global.Base64;
- const clean = jsdom();
- // ensure zlib is getting loaded
- $.PrivateBin.Controller.initZ();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- global.atob = common.atob;
- // Of course you can easily decipher the following texts, if you like.
- // Bonus points for finding their sources and hidden meanings.
- const paste = await $.PrivateBin.CryptTool.decipher(
- 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
- '', // no password
- '{"iv":"WA42mdxIVXUwBqZu7JYNiw==","v":1,"iter":10000,"ks"' +
- ':256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","sa' +
- 'lt":"jN6CjbQMJCM=","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zEdJc5dSD/eNr' +
- '99gjHX7wphJ6umKMM+fn6PcbYJkhDh2GlJL5COXjXfm/5aj/vuyaRRWZ' +
- 'MZtmnYpGAtAPg7AUG"}'
- );
- clean();
- const result = typeof paste === 'string' && paste.includes('Sol is right');
- if (!result) console.log(paste);
- assert.ok(result);
- }
- );
- it(
- 'supports ZeroBin ciphertext with password (SJCL & Base64 1.7)',
- async function () {
- global.Base64 = require('../base64-1.7').Base64;
- const clean = jsdom();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- global.atob = common.atob;
- // Of course you can easily decipher the following texts, if you like.
- // Bonus points for finding their sources and hidden meanings.
- const paste = await $.PrivateBin.CryptTool.decipher(
- '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
- // -- "That's amazing. I've got the same combination on my luggage."
- Array.apply(0, Array(6)).map((_,b) => b + 1).join(''),
- '{"iv":"aTnR2qBL1CAmLX8FdWe3VA==","v":1,"iter":10000,"ks"' +
- ':256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","sa' +
- 'lt":"u0lQvePq6L0=","ct":"A3nBTvICZtYy6xqbIJE0c8Veored5lM' +
- 'JUGgGUm4581wjrPFlU0Q0tUZSf+RUUoZj2jqDa4kiyyZ5YNMe30hNMV0' +
- 'oVSalNhRgD9svVMnPuF162IbyhVCwr7ULjT981CHxVlGNqGqmIU6L/Xi' +
- 'xgdArxAA8x1GCrfAkBWWGeq8Qw5vJPG/RCHpwR4Wy3azrluqeyERBzma' +
- 'OQjO/kM35TiI6IrLYFyYyL7upYlxAaxS0XBMZvN8QU8Lnerwvh5JVC6O' +
- 'kkKrhogajTJIKozCF79yI78c50LUh7tTuI3Yoh7+fXxhoODvQdYFmoiU' +
- 'lrutN7Y5ZMRdITvVu8fTYtX9c7Fiufmcq5icEimiHp2g1bvfpOaGOsFT' +
- '+XNFgC9215jcp5mpBdN852xs7bUtw+nDrf+LsDEX6iRpRZ+PYgLDN5xQ' +
- 'T1ByEtYbeP+tO38pnx72oZdIB3cj8UkOxnxdNiZM5YB5egn4jUj1fHot' +
- '1I69WoTiUJipZ5PIATv7ScymRB+AYzjxjurQ9lVfX9QtAbEH2dhdmoUo' +
- '3IDRSXpWNCe9RC1aUIyWfZO7oI7FEohNscHNTLEcT+wFnFUPByLlXmjN' +
- 'Z7FKeNpvUm3jTY4t4sbZH8o2dUl624PAw1INcJ6FKqWGWwoFT2j1MYC+' +
- 'YV/LkLTdjuWfayvwLMh27G/FfKCRbW36vqinegqpPDylsx9+3oFkEw3y' +
- '5Z8+44oN91rE/4Md7JhPJeRVlFC9TNCj4dA+EVhbbQqscvSnIH2uHkMw' +
- '7mNNo7xba/YT9KoPDaniqnYqb+q2pX1WNWE7dLS2wfroMAS3kh8P22DA' +
- 'V37AeiNoD2PcI6ZcHbRdPa+XRrRcJhSPPW7UQ0z4OvBfjdu/w390QxAx' +
- 'SxvZewoh49fKKB6hTsRnZb4tpHkjlww=="}'
- );
- clean();
- delete global.Base64;
- const result = typeof paste === 'string' && paste.includes('securely packed in iron');
- if (!result) console.log(paste);
- assert.ok(result);
- }
- );
- it(
- 'supports ZeroBin ciphertext no password (SJCL & Base64 1.7)',
- async function () {
- global.Base64 = require('../base64-1.7').Base64;
- const clean = jsdom();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- global.atob = common.atob;
- const paste = await $.PrivateBin.CryptTool.decipher(
- 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
- '', // no password
- '{"iv":"Z7lAZQbkrqGMvruxoSm6Pw==","v":1,"iter":10000,"ks"' +
- ':256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","sa' +
- 'lt":"jN6CjbQMJCM=","ct":"PuOPWB3i2FPcreSrLYeQf84LdE8RHjs' +
- 'c+MGtiOr4b7doNyWKYtkNorbRadxaPnEee2/Utrp1MIIfY5juJSy8RGw' +
- 'EPX5ciWcYe6EzsXWznsnvhmpKNj9B7eIIrfSbxfy8E2e/g7xav1nive+' +
- 'ljToka3WT1DZ8ILQd/NbnJeHWaoSEOfvz8+d8QJPb1tNZvs7zEY95Dum' +
- 'QwbyOsIMKAvcZHJ9OJNpujXzdMyt6DpcFcqlldWBZ/8q5rAUTw0HNx/r' +
- 'CgbhAxRYfNoTLIcMM4L0cXbPSgCjwf5FuO3EdE13mgEDhcClW79m0Qvc' +
- 'nIh8xgzYoxLbp0+AwvC/MbZM8savN/0ieWr2EKkZ04ggiOIEyvfCUuNp' +
- 'rQBYO+y8kKduNEN6by0Yf4LRCPfmwN+GezDLuzTnZIMhPbGqUAdgV6Ex' +
- 'qK2ULEEIrQEMoOuQIxfoMhqLlzG79vXGt2O+BY+4IiYfvmuRLks4UXfy' +
- 'HqxPXTJg48IYbGs0j4TtJPUgp3523EyYLwEGyVTAuWhYAmVIwd/hoV7d' +
- '7tmfcF73w9dufDFI3LNca2KxzBnWNPYvIZKBwWbq8ncxkb191dP6mjEi' +
- '7NnhqVk5A6vIBbu4AC5PZf76l6yep4xsoy/QtdDxCMocCXeAML9MQ9uP' +
- 'QbuspOKrBvMfN5igA1kBqasnxI472KBNXsdZnaDddSVUuvhTcETM="}'
- );
- clean();
- delete global.Base64;
- const result = typeof paste === 'string' && paste.includes('Sol is right');
- if (!result) console.log(paste);
- assert.ok(result);
- }
- );
- it('does not truncate messages', async function () {
- const message = fs.readFileSync('test/compression-sample.txt', 'ascii').trim(),
- clean = jsdom();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- // ensure zlib is getting loaded
- $.PrivateBin.Controller.initZ();
- global.atob = common.atob;
- global.btoa = common.btoa;
- const cipherMessage = await $.PrivateBin.CryptTool.cipher(
- 'foo', 'bar', message, []
- ),
- plaintext = await $.PrivateBin.CryptTool.decipher(
- 'foo', 'bar', cipherMessage
- );
- clean();
- const result = (message === plaintext);
- if (!result) console.log(plaintext, cipherMessage);
- assert.ok(result);
- });
- it('can en- and decrypt a particular message (#260)', function () {
- jsc.assert(jsc.forall(
- 'string',
- 'string',
- async function (key, password) {
- const message = `
- 1 subgoal
- inv : Assert
- expr : Expr
- sBody : Instr
- deduction : (|- [|inv /\ assertOfExpr expr|] sBody [|inv|])%assert
- IHdeduction : (|= [|inv /\ assertOfExpr expr |] sBody [|inv|])%assert
- mem : Mem
- preInMem : inv mem
- m : Mem
- n : nat
- interpRel : interp (nth_iterate sBody n) (MemElem mem) = CpoElem Mem m
- lastIter : interp (nth_iterate sBody n) (MemElem mem) |=e expr_neg expr
- notLastIter : forall p : nat,
- p < n -> interp (nth_iterate sBody p) (MemElem mem) |=e expr
- isWhile : interp (while expr sBody) (MemElem mem) =
- interp (nth_iterate sBody n) (MemElem mem)
- ======================== ( 1 / 1 )
- conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
- `;
- const clean = jsdom();
- // ensure zlib is getting loaded
- $.PrivateBin.Controller.initZ();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- const cipherMessage = await $.PrivateBin.CryptTool.cipher(
- key, password, message, []
- ),
- plaintext = await $.PrivateBin.CryptTool.decipher(
- key, password, cipherMessage
- );
- clean();
- const result = (message === plaintext);
- if (!result) console.log(plaintext, cipherMessage);
- return result;
- }
- ),
- {tests: 3});
- });
- });
- describe('getSymmetricKey', function () {
- this.timeout(10000);
- let keys = [];
- // the parameter is used to ensure the test is run more then one time
- it('returns random, non-empty keys', function () {
- jsc.assert(jsc.forall(
- 'integer',
- function(counter) {
- const clean = jsdom();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- writeable: false,
- });
- const key = $.PrivateBin.CryptTool.getSymmetricKey(),
- result = (key !== '' && keys.indexOf(key) === -1);
- keys.push(key);
- clean();
- return result;
- }
- ),
- {tests: 10});
- });
- });
- });
|