Changeset - 51344a25b55d
[Not reviewed]
default
0 3 0
Laman - 5 years ago 2019-06-30 16:33:14

array padding refactored into util
3 files changed with 8 insertions and 12 deletions:
0 comments (0 inline, 0 general)
src/blake.js
Show inline comments
 
// https://tools.ietf.org/html/rfc7693
 
import {MASK,int32s2bytes,bytes2int32s} from "./util.js";
 
import {MASK,int32s2bytes,bytes2int32s,zeroPad} 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));}
 
	if(key.length>0){this.update(zeroPad(key,BLOCK_LEN));}
 
}
 

	
 
BLAKE2S.prototype.update=function(data){
 
	for(let i=0;i<data.length;i++){
 
		if(this._buffer.length==BLOCK_LEN){
 
			this._compress(false);
 
@@ -34,13 +30,13 @@ BLAKE2S.prototype.update=function(data){
 
		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._buffer=zeroPad(this._buffer,BLOCK_LEN);
 
	this._compress(true);
 
	return int32s2bytes(this._state).slice(0,this._outputLen);
 
};
 

	
 
BLAKE2S.prototype._compress=function(last){
 
	const SIGMA=[
src/chacha.js
Show inline comments
 
// https://tools.ietf.org/html/rfc7539
 
import {MASK,int32s2bytes,bytes2int32s} from "./util.js";
 
import {MASK,int32s2bytes,bytes2int32s,zeroPad} from "./util.js";
 

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

	
 
function createNonce(){
 
	let nonce=new Uint8Array(12);
 
	window.crypto.getRandomValues(nonce);
 
	return nonce;
 
}
 

	
 
function zeroPad(arr,length){
 
	return arr.concat((new Array(length)).fill(0)).slice(0,length);
 
}
 

	
 
/**
 
 * A Chacha20 cipher class.
 
 * @param {Array} key Array of bytes (integers: 0<=x<256). Short keys are padded to 32B, long keys are silently truncated.
 
 * @param {Array} nonce optional. If present, it must be an Array of bytes (integers: 0<=x<256). Short nonces are padded to 12B, long nonces are silently truncated.
 
 */
 
export function Chacha20(key,nonce){
src/util.js
Show inline comments
 
export const MASK=0xffffffff;
 

	
 
export function zeroPad(arr,length){
 
	return arr.concat((new Array(length)).fill(0)).slice(0,length);
 
}
 

	
 
export function bytes2int32(arr){
 
	return arr.reduce((acc,b,i)=>acc|b<<(i*8));
 
}
 

	
 
export function bytes2int32s(arr){
 
	let res=[];
0 comments (0 inline, 0 general)