diff --git a/chacha.js b/chacha.js new file mode 100644 --- /dev/null +++ b/chacha.js @@ -0,0 +1,49 @@ +// https://tools.ietf.org/html/rfc7693 + +MASK=0xffffffff; + +function int2bytes(x){ + var res=new Array(4); + for(var i=0;i<4;i++){ + res[i]=x&0xff; + x>>>=8; + } + return res; +} + +function lrot(x,shift){ + return (x<>>(32-shift))&MASK; +} + +function quarterRound(arr,ia,ib,ic,id){ + var a=arr[ia], b=arr[ib], c=arr[ic], d=arr[id]; + a=(a+b)&MASK; d=lrot(d^a,16); + c=(c+d)&MASK; b=lrot(b^c,12); + a=(a+b)&MASK; d=lrot(d^a,8); + c=(c+d)&MASK; b=lrot(b^c,7); + arr[ia]=a; arr[ib]=b; arr[ic]=c; arr[id]=d; +} + +function chacha(key,nonce,position){ + var x=input.slice(); + + for(var i=0;i<10;i++){ + // column round + quarterRound(x,0,4,8,12); + quarterRound(x,1,5,9,13); + quarterRound(x,2,6,10,14); + quarterRound(x,3,7,11,15); + // diagonal round + quarterRound(x,0,5,10,15); + quarterRound(x,1,6,11,12); + quarterRound(x,2,7,8,13); + quarterRound(x,3,4,9,14); + } + + return x.map((xi,i)=>(xi+input[i])&MASK); +} + +function encrypt(message,key){ + var nonce=Date.now(); + var nonce0=nonce; +}