Changeset - bcd08758ec7c
[Not reviewed]
default
0 3 1
Laman - 5 years ago 2019-06-29 18:09:21

testing blake
4 files changed with 47 insertions and 1 deletions:
0 comments (0 inline, 0 general)
spec/test/blakeSpec.js
Show inline comments
 
new file 100644
 
/* global expect */
 

	
 
describe("Blake2",function(){
 
	let cryptoJS=require("../../dist/main.js");
 
	let util=cryptoJS.util;
 
	let blake2s=cryptoJS.blake2s;
 
	let str2utf8=util.str2utf8;
 
	
 
	let msg=str2utf8("abc");
 
	let longMsg=str2utf8("0123456789.10.456789.20.456789.30.456789.40.456789.50.456789.60.456789.70.456789.80.456789.90.456789");
 
	let key=str2utf8("zoqpiz");
 
	let longKey=str2utf8("zoqpizjutyclcmkamzhhmhvchxjtefjy");
 
	
 
	describe("blake2s",function(){
 
		it("should return a correct variable size output",function(){
 
			expect(util.bytes2hex(blake2s(msg,16))).toEqual("aa4938119b1dc7b87cbad0ffd200d0ae");
 
		});
 
		it("should work with just a message",function(){
 
			expect(util.bytes2hex(blake2s(msg))).toEqual("508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982");
 
		});
 
		it("should work for multi block messages",function(){
 
			expect(util.bytes2hex(blake2s(longMsg.slice(0,64)))).toEqual("70484f89974551454d596350dda8af2aa6f0811b527549b9ecfe7adede063753");
 
			expect(util.bytes2hex(blake2s(longMsg.slice(0,65)))).toEqual("af14d4f74947bbde734d0e3015c667cc80676efe4349be235be8046e9e45e0ae");
 
			expect(util.bytes2hex(blake2s(longMsg))).toEqual("59a44e5e417d07fb382505ee7e67c23e0d476d354abc81899960bcab677beee1");
 
		});
 
		it("should work for keyed messages",function(){
 
			expect(util.bytes2hex(blake2s(msg,32,key))).toEqual("0da0b6a54e8f294b60bb25c572700166ddb9d124257ff36f9f43f18b844adf9f");
 
		});
 
		it("should work with long keyes",function(){
 
			expect(util.bytes2hex(blake2s(msg,32,longKey))).toEqual("09ef85c9942bebdeb866c6ade769220fd9b851aead642017f6d59bf7e2a32037");
 
		});
 
		it("should work with an empty input",function(){
 
			expect(util.bytes2hex(blake2s([]))).toEqual("69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9");
 
		});
 
	});
 
	
 
});
spec/test/utilSpec.js
Show inline comments
 
/* global expect */
 

	
 
describe("Util",function(){
 
  let util=require("../../src/util.js");
 
  let cryptoJS=require("../../dist/main.js");
 
	let util=cryptoJS.util;
 
	let utf=[ // https://tools.ietf.org/html/rfc3629#page-8
 
		["",[]],
 
		["abc",[97,98,99]],
 
		["å",[195,165]],
 
		["🚅",[240,159,154,133]],
 
		["žír",[0xc5,0xbe,0xc3,0xad,114]],
 
		["A\u2262\u0391.",[0x41,0xE2,0x89,0xA2,0xCE,0x91,0x2E]],
 
		["\uD55C\uAD6D\uC5B4",[0xED,0x95,0x9C,0xEA,0xB5,0xAD,0xEC,0x96,0xB4]],
 
		["\ud84c\udfb4",[0xf0,0xa3,0x8e,0xb4]]
 
	];
 
	
 
	describe("str2utf8",function(){
 
		it("should encode a String into bytes in UTF-8",function(){
 
			utf.forEach(couple=>expect(util.str2utf8(couple[0])).toEqual(couple[1]));
 
		});
 
	});
 
	
 
	describe("utf82str",function(){
 
		it("should decode a String from UTF-8 bytes",function(){
 
			utf.forEach(couple=>expect(util.utf82str(couple[1])).toEqual(couple[0]));
 
		});
 
	});
 
});
src/blake.js
Show inline comments
 
// https://tools.ietf.org/html/rfc7693
 
import {MASK,int32s2bytes,bytes2int32s} from "./util.js";
 

	
 
const BLOCK_LEN=64;
 

	
 
const IV=[0x6A09E667,0xBB67AE85,0x3C6EF372,0xA54FF53A,0x510E527F,0x9B05688C,0x1F83D9AB,0x5BE0CD19];
 

	
 
function padEnd(arr,length,val=0){
 
	return arr.concat((new Array(length-arr.length)).fill(0));
 
}
 

	
 
function rrot(x,shift){
 
	return ((x>>>shift)|(x<<(32-shift)))&MASK;
 
}
 

	
 
export function BLAKE2S(outputLen=32,key=[]){
 
	this._buffer=[];
 
	this._dataLen=[0,0]; // low, high
 
	this._outputLen=outputLen;
 
	
 
	this._state=IV.slice();
 
	this._state[0]^=0x01010000^(key.length<<8)^this._outputLen;
 
	
 
	if(key.length>0){this.update(padEnd(key,BLOCK_LEN,0));}
 
}
 

	
 
BLAKE2S.prototype.update=function(data){
 
	for(let i=0;i<data.length;i++){
 
		if(this._buffer.length==BLOCK_LEN){
 
			this._compress(false);
 
			this._buffer=[];
 
		}
 
		this._buffer.push(data[i]);
 
		this._dataLen[0]=(this._dataLen[0]+1)&MASK;
 
		if(this._dataLen[0]<this._buffer.length){this._dataLen[1]++;}
 
	}
 
};
 

	
 
BLAKE2S.prototype.digest=function(){
 
	this._buffer=padEnd(this._buffer,BLOCK_LEN,0);
 
	this._compress(true);
 
	return int32s2bytes(this._state).slice(0,this._outputLen);
 
};
 

	
 
BLAKE2S.prototype._compress=function(last){
 
	const SIGMA=[
 
		[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
 
		[14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3],
 
		[11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4],
 
		[7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8],
 
		[9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13],
 
		[2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9],
 
		[12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11],
 
		[13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10],
 
		[6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5],
 
		[10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0]
 
	];
 
	let v=this._state.concat(IV);
 
	v[12]^=this._dataLen[0];
 
	v[13]^=this._dataLen[1];
 
	if(last){v[14]^=MASK;}
 
	let data=bytes2int32s(this._buffer);
 

	
 
	for(let i=0;i<10;i++){
 
		let perm=SIGMA[i%10];
 
		this._mix(v,0,4,8,12,data[perm[0]],data[perm[1]]);
 
		this._mix(v,1,5,9,13,data[perm[2]],data[perm[3]]);
 
		this._mix(v,2,6,10,14,data[perm[4]],data[perm[5]]);
 
		this._mix(v,3,7,11,15,data[perm[6]],data[perm[7]]);
 

	
 
		this._mix(v,0,5,10,15,data[perm[8]],data[perm[9]]);
 
		this._mix(v,1,6,11,12,data[perm[10]],data[perm[11]]);
 
		this._mix(v,2,7,8,13,data[perm[12]],data[perm[13]]);
 
		this._mix(v,3,4,9,14,data[perm[14]],data[perm[15]]);
 
	}
 

	
 
	this._state=this._state.map((x,i)=>x^v[i]^v[i+8]);
 
};
 

	
 
BLAKE2S.prototype._mix=function(arr,ia,ib,ic,id,x,y){
 
	let a=arr[ia], b=arr[ib], c=arr[ic], d=arr[id];
 
	a=(a+b+x)&MASK; d=rrot(d^a,16);
 
	c=(c+d)&MASK; b=rrot(b^c,12);
 
	a=(a+b+y)&MASK; d=rrot(d^a,8);
 
	c=(c+d)&MASK; b=rrot(b^c,7);
 
	arr[ia]=a; arr[ib]=b; arr[ic]=c; arr[id]=d;
 
};
 

	
 
export function blake2s(data,outputLen=32,key=[]){
 
	let h=new BLAKE2S(outputLen,key);
 
	for(let i=0;i<data.length;i+=BLOCK_LEN){h.update(data.slice(i,i+BLOCK_LEN));}
 
	return h.digest();
 
}
src/main.js
Show inline comments
 
import * as util from "./util.js";
 
import {blake2s} from "./blake.js";
 
import {Chacha20} from "./chacha.js";
 

	
 
export default {util,blake2s,Chacha20};
 

	
 
// export for tests running on Node
 
if(typeof module!=='undefined'&&module.hasOwnProperty('exports')){
 
	module.exports.util=util;
 
	module.exports.blake2s=blake2s;
 
	module.exports.Chacha20=Chacha20;
 
}
 
	
 
\ No newline at end of file
0 comments (0 inline, 0 general)