/* global expect */ describe("Chacha",function(){ let cryptoJS=require("../../dist/main.js"); let util=cryptoJS.util; let Chacha20=cryptoJS.Chacha20; let encrypt=cryptoJS.encrypt; let decrypt=cryptoJS.decrypt; let str2utf8=util.str2utf8; let key=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]; let nonce1=[0,0,0,9,0,0,0,74,0,0,0,0]; let nonce2=[0,0,0,0,0,0,0,74,0,0,0,0]; let plaintext="Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."; describe("Chacha20",function(){ it("should return a correct test vector",function(){ let cipher=new Chacha20(key,nonce1); let output=cipher._computeBlock().map(x=>x>>>0); let expected=[0xe4e7f110,0x15593bd1,0x1fdd0f50,0xc47120a3,0xc7f4d1c7,0x0368c033,0x9aaa2204,0x4e6cd4c3,0x466482d2,0x09aa9f07,0x05d7c214,0xa2028bd9,0xd19c12b5,0xb94e16de,0xe883d0cb,0x4e3c50a2]; expect(output).toEqual(expected); }); it("should output a correct key stream",function(){ let cipher=new Chacha20(key,nonce2); let expected=[34,79,81,243,64,27,217,225,47,222,39,111,184,99,29,237,140,19,31,130,61,44,6, 226,126,79,202,236,158,243,207,120,138,59,10,163,114,96,10,146,181,121,116,205,237,43,147,52,121, 76,186,64,198,62,52,205,234,33,44,76,240,125,65,183,105,166,116,159,63,99,15,65,34,202,254,40,236, 77,196,126,38,212,52,109,112,185,140,115,243,233,197,58,196,12,89,69,57,139,110,218,26,131,44,137, 193,103,234,205,144,29,126,43,243,99]; expected.forEach(b=>{ expect(cipher.getByte()).toEqual(b); }); }); }); describe("encrypt",function(){ it("should correctly encrypt an example text",function(){ let ciphertext=[110,46,53,154,37,104,249,128,65,186,7,40,221,13,105,129,233,126,122,236,29, 67,96,194,10,39,175,204,253,159,174,11,249,27,101,197,82,71,51,171,143,89,61,171,205,98,179,87,22, 57,214,36,230,81,82,171,143,83,12,53,159,8,97,216,7,202,13,191,80,13,106,97,86,163,142,8,138,34, 182,94,82,188,81,77,22,204,248,6,129,140,233,26,183,121,55,54,90,249,11,191,116,163,91,230,180,11, 142,237,242,120,94,66,135,77]; expect(encrypt(str2utf8(plaintext),key,nonce2)).toEqual([nonce2,ciphertext]); }); }); describe("decrypt",function(){ it("should be able to decrypt a Chacha20 encrypted text",function(){ let text=str2utf8(plaintext); let key=[]; for(let i=0;i<16;i++){key.push(Math.floor(Math.random()*256));} let nonce=[]; for(let i=0;i<8;i++){nonce.push(Math.floor(Math.random()*256));} let [_,ciphertext]=encrypt(text,key,nonce); expect(decrypt(ciphertext,key,nonce)).toEqual(text); }); }); });