Skip to content

Commit

Permalink
Ported rest of hashes to bouncycastle api
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Jul 19, 2011
1 parent 0bf5c5f commit d070088
Show file tree
Hide file tree
Showing 50 changed files with 862 additions and 1,418 deletions.
620 changes: 0 additions & 620 deletions lib/hash/before_refactoring/sha256.js

This file was deleted.

105 changes: 19 additions & 86 deletions lib/hash/before_refactoring/gost3411.js → lib/hash/gost3411.js
Expand Up @@ -7,6 +7,7 @@ var debug = require('sys').debug,
Long = require('long').Long;

const DIGEST_LENGTH = 32;
const BYTE_LENGTH = 32;

var GOST3411 = exports.GOST3411 = function() {
// Set up arrays
Expand All @@ -32,15 +33,20 @@ var GOST3411 = exports.GOST3411 = function() {
this.byteCount = Long.ZERO;
// Set up sbox
this.sBox = Gost28147.DSbox_A;
this.cipher = new Gost28147(null, this.sBox);
this.cipher = new Gost28147();
this.cipher.init(true, null, this.sBox);
// Reset state
this.reset();
}

GOST3411.prototype.digestSize = function() {
GOST3411.prototype.getDigestSize = function() {
return DIGEST_LENGTH;
}

GOST3411.prototype.getByteLength = function() {
return BYTE_LENGTH;
}

//A (x) = (x0 ^ x1) || x3 || x2 || x1
var A = function(self, src) {
for(var j = 0; j < 8; j++) {
Expand All @@ -53,9 +59,6 @@ var A = function(self, src) {
}

var P = function(self, src) {
// debug(self.K)
// debug(src)

for(var k = 0; k < 8; k++) {
self.K[4*k] = src[k];
self.K[1 + 4*k] = src[ 8 + k];
Expand All @@ -67,29 +70,15 @@ var P = function(self, src) {
}

var E = function(self, key, s, sOff, src, inOff) {
// debug("====================================== key :: " + key)
// debug("====================================== src :: " + src)
// debug("====================================== inOff :: " + inOff)

self.cipher.workingKey = self.cipher.generateWorkingKey(key);
// var a = self.cipher.encrypt(src.slice(inOff, inOff + 8), 0);
// debug(a)
// debug("====================================== workingKey :: " + self.cipher.workingKey)
// debug("====================================== src :: " + src)
// return a
var result = self.cipher.encrypt(src, inOff);
self.cipher.processBlock(src, inOff);
util.copy(s, sOff, src, inOff, self.cipher.getBlockSize());
return s;
}

var fw = function(self, src) {
cpyBytesToShort(src, self.wS);
// debug(" fw-wS-1 :: " + self.wS)

self.w_S[15] = self.wS[0] ^ self.wS[1] ^ self.wS[2] ^ self.wS[3] ^ self.wS[12] ^ self.wS[15];
// debug(" fw-w_S[15] :: " + self.w_S[15])
// debug(" fw-wS-2 :: " + self.wS)

util.copy(self.w_S, 0, self.wS, 1, 15);
cpyShortToBytes(self.w_S, src);
}
Expand All @@ -109,91 +98,56 @@ var cpyShortToBytes = function(wS, S) {

GOST3411.prototype.processBlock = function(src, inOff) {
this.M = src.slice(inOff, inOff + 32);

// debug("---------------------------------------------------- processBlock")

//key step 1
// H = h3 || h2 || h1 || h0
// S = s3 || s2 || s1 || s0
// System.arraycopy(H, 0, U, 0, 32);
this.U = this.H.slice(0, 32);
// debug(" U = " + this.U)
// System.arraycopy(M, 0, V, 0, 32);
this.V = this.M.slice(0, 32);
// debug(" V = " + this.V)

for(var j = 0; j < 32; j++) {
this.W[j] = this.U[j] ^ this.V[j];
}
// debug(" W = " + this.W)

// Encrypt gost28147-ECB
this.S = E(this, P(this, this.W), this.S, 0, this.H.slice(0), 0); // s0 = EK0 [h0]

// debug(" K = " + this.K)
// debug(" S = " + this.S)

//keys step 2,3,4
for (var i = 1; i < 4; i++) {
var tmpA = A(this, this.U);
for(var j = 0; j < 32; j++) {
this.U[j] = tmpA[j] ^ this.C[i][j];
}

// debug(" U = " + this.U)

this.V = A(this, A(this, this.V));

// debug(" V = " + this.V)

for(var j = 0; j < 32; j++) {
this.W[j] = this.U[j] ^ this.V[j];
}

// debug(" W = " + this.W)

// Encrypt gost28147-ECB
this.S = E(this, P(this, this.W), this.S, i * 8, this.H.slice(0), i * 8); // si = EKi [hi]

// debug(" S = " + this.S)
}

// x(M, H) = y61(H^y(M^y12(S)))
for(var n = 0; n < 12; n++) {
fw(this, this.S);
}

// debug(" S = " + this.S)

for(var n = 0; n < 32; n++) {
this.S[n] = this.S[n] ^ this.M[n];
}

// debug(" S = " + this.S)

fw(this, this.S);

// debug(" S = " + this.S)
// debug(" H = " + this.H)

for(var n = 0; n < 32; n++) {
this.S[n] = this.H[n] ^ this.S[n];
}

// debug(" S = " + this.S)

for(var n = 0; n < 61; n++) {
fw(this, this.S);
}

// debug(" S = " + this.S)

// debug("---------------------- this.H.length = " + this.H.length)

// System.arraycopy(S, 0, H, 0, H.length);
this.H = this.S.slice(0, this.H.length);

// debug(" H = " + this.H)
}

var update = function(self, byte) {
Expand Down Expand Up @@ -272,7 +226,7 @@ GOST3411.prototype.reset = function() {
this.C[2] = C2.slice(0);
}

GOST3411.prototype.algorithmName = function() {
GOST3411.prototype.getAlgorithmName = function() {
return "GOST3411";
}

Expand All @@ -286,47 +240,26 @@ var finish = function(self) {
update(self, 0);
}



// Pack.longToLittleEndian(byteCount * 8, L, 0); // get length into L (byteCount * 8 = bitCount)
//
// while (xBufOff != 0)
// {
// update((byte)0);
// }
//
// Process blocks
self.processBlock(self.L, 0);
self.processBlock(self.Sum, 0);

// processBlock(L, 0);
// processBlock(Sum, 0);
}

//
// Common to all digests
GOST3411.prototype.digest = function(encoding) {
// debug(" F ---------------------- this.H.length = " + this.H.length)
GOST3411.prototype.doFinal = function(output, index) {
finish(this);
// debug(" F ---------------------- this.H.length = " + this.H.length)

var output = this.H.slice(0, this.H.length);

// debug(" F ---------------------- this.H.length = " + this.H.length)
// debug(" F ---------------------- this.output.length = " + output.length)
// Ensure valid index
index = index == null ? 0 : index;
// Copy hash
for(var i = 0; i < this.H.length; i++) {
output[index + i] = this.H[i];
}

// Ouput digest
// var output = new Array(16);
// Reset
this.reset();

// Return based on encoding
if(encoding == null || encoding === 'binary') {
return util.arrayToBinaryString(output);
} else if(encoding === 'hex') {
return util.toHex(output);
} else if(encoding === 'array'){
return output ;
}
return DIGEST_LENGTH;
}

/**
Expand Down
35 changes: 16 additions & 19 deletions lib/hash/before_refactoring/ripemd128.js → lib/hash/ripemd128.js
Expand Up @@ -6,6 +6,7 @@ var debug = require('sys').debug,
Long = require('long').Long;

const DIGEST_LENGTH = 16;
const BYTE_LENGTH = 16;

var RIPEMD128 = exports.RIPEMD128 = function() {
// Call base class constructor
Expand All @@ -21,11 +22,15 @@ var RIPEMD128 = exports.RIPEMD128 = function() {

inherits(RIPEMD128, BaseDigest);

RIPEMD128.prototype.digestSize = function() {
RIPEMD128.prototype.getDigestSize = function() {
return DIGEST_LENGTH;
}

RIPEMD128.prototype.algorithmName = function() {
RIPEMD128.prototype.getByteLength = function() {
return BYTE_LENGTH;
}

RIPEMD128.prototype.getAlgorithmName = function() {
return "RIPEMD128";
}

Expand Down Expand Up @@ -316,24 +321,16 @@ RIPEMD128.prototype.processLength = function(bitLength) {

//
// Common to all digests
RIPEMD128.prototype.digest = function(encoding) {
RIPEMD128.prototype.doFinal = function(output, index) {
this.finish();

var output = new Array(16);
util.inPlaceEncodeUInt32R(this.H0, output, 0);
util.inPlaceEncodeUInt32R(this.H1, output, 4);
util.inPlaceEncodeUInt32R(this.H2, output, 8);
util.inPlaceEncodeUInt32R(this.H3, output, 12);

this.reset();

index = index == null ? 0 : index;
// Encode in place
util.inPlaceEncodeUInt32R(this.H0, output, index + 0);
util.inPlaceEncodeUInt32R(this.H1, output, index + 4);
util.inPlaceEncodeUInt32R(this.H2, output, index + 8);
util.inPlaceEncodeUInt32R(this.H3, output, index + 12);
this.reset();
// Return based on encoding
if(encoding == null || encoding === 'binary') {
return util.arrayToBinaryString(output);
} else if(encoding === 'hex') {
return util.toHex(output);
} else if(encoding === 'array'){
return output ;
}
return DIGEST_LENGTH;
}

37 changes: 17 additions & 20 deletions lib/hash/before_refactoring/ripemd160.js → lib/hash/ripemd160.js
Expand Up @@ -6,6 +6,7 @@ var debug = require('sys').debug,
Long = require('long').Long;

const DIGEST_LENGTH = 20;
const BYTE_LENGTH = 64;

var RIPEMD160 = exports.RIPEMD160 = function() {
// Call base class constructor
Expand All @@ -21,10 +22,14 @@ var RIPEMD160 = exports.RIPEMD160 = function() {

inherits(RIPEMD160, BaseDigest);

RIPEMD160.prototype.digestSize = function() {
RIPEMD160.prototype.getDigestSize = function() {
return DIGEST_LENGTH;
}

RIPEMD160.prototype.getByteLength = function() {
return BYTE_LENGTH;
}

RIPEMD160.prototype.processWord = function(src, inOff) {
this.X[this.xOff++] = (src[inOff] & 0xff) | ((src[inOff + 1] & 0xff) << 8)
| ((src[inOff + 2] & 0xff) << 16) | ((src[inOff + 3] & 0xff) << 24);
Expand Down Expand Up @@ -282,7 +287,7 @@ var f5 = function(x, y, z) {
return x ^ (y | ~z);
}

RIPEMD160.prototype.algorithmName = function() {
RIPEMD160.prototype.getAlgorithmName = function() {
return "RIPEMD160";
}

Expand Down Expand Up @@ -322,25 +327,17 @@ RIPEMD160.prototype.processLength = function(bitLength) {

//
// Common to all digests
RIPEMD160.prototype.digest = function(encoding) {
RIPEMD160.prototype.doFinal = function(output, index) {
this.finish();

var output = new Array(16);
util.inPlaceEncodeUInt32R(this.H0, output, 0);
util.inPlaceEncodeUInt32R(this.H1, output, 4);
util.inPlaceEncodeUInt32R(this.H2, output, 8);
util.inPlaceEncodeUInt32R(this.H3, output, 12);
util.inPlaceEncodeUInt32R(this.H4, output, 16);

this.reset();

index = index == null ? 0 : index;
// Encode the hash
util.inPlaceEncodeUInt32R(this.H0, output, index + 0);
util.inPlaceEncodeUInt32R(this.H1, output, index + 4);
util.inPlaceEncodeUInt32R(this.H2, output, index + 8);
util.inPlaceEncodeUInt32R(this.H3, output, index + 12);
util.inPlaceEncodeUInt32R(this.H4, output, index + 16);
this.reset();
// Return based on encoding
if(encoding == null || encoding === 'binary') {
return util.arrayToBinaryString(output);
} else if(encoding === 'hex') {
return util.toHex(output);
} else if(encoding === 'array'){
return output ;
}
return DIGEST_LENGTH;
}

0 comments on commit d070088

Please sign in to comment.