Skip to content

Commit

Permalink
Added IDEA and MARS bouncycastle api
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Jul 18, 2011
1 parent 9ec3fab commit 8ca919b
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ var ND = 0xFFFFFFFF;
var USHORT = 0x10000;

var IDEA = exports.IDEA = function(key) {
}

IDEA.prototype.init = function(forEncryption, key) {
this.forEncryption = forEncryption;
// Create 16 but values
var l_key = new Array(KEYLEN);

// Create the 16 bit values
for(var i = 0; i < key.length / 2; i++) {
l_key[i] = util.decodeUInt16(key, (i*2));
}

// Create an encryption key
this.encryptionKey = createEncryptionKey(l_key.slice(0));
// Create a decryption key
this.decryptionKey = createDecryptionKey(l_key.slice(0));

if(forEncryption) {
this.key = createEncryptionKey(l_key.slice(0));
} else {
this.key = createDecryptionKey(l_key.slice(0));
}
}

var createDecryptionKey = function(key) {
Expand Down Expand Up @@ -87,20 +93,19 @@ var createEncryptionKey = function(key) {
return key;
}

IDEA.prototype.getBlockSize = function() {
return BlockSize;
}

IDEA.prototype.encrypt = function(src, index) {
index = index == null ? 0 : index;
return this.encryptBlock(src, index)
}
// Block size of cipher
IDEA.prototype.getBlockSize = function() { return BlockSize; }
// Algorithm Name
IDEA.prototype.getAlgorithmName = function() { return "IDEA"; }
// Reset cipher
IDEA.prototype.reset = function() {}

IDEA.prototype.encryptBlock = function(block, index) {
return this.cryptPair(block, this.encryptionKey, index);
// Process a block
IDEA.prototype.processBlock = function(input, inOff, inLen) {
cryptPair(input, this.key, inOff);
}

IDEA.prototype.cryptPair = function(block, key, index) {
var cryptPair = function(block, key, index) {
// Prepare the word array
var word = [util.decodeUInt16(block, index)
, util.decodeUInt16(block, index + 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ var debug = require('sys').debug,
// Mars Cipher
const BlockSize = 16;

var Mars = exports.Mars = function(key) {
// key = key.reverse()
var Mars = exports.Mars = function() {
}

Mars.prototype.init = function(forEncryption, key) {
this.forEncryption = forEncryption;
var k = this.key = new Array(40);
var t = new Array(15);
var numberOfWords = key.length/4;
Expand Down Expand Up @@ -55,16 +58,23 @@ var Mars = exports.Mars = function(key) {
m = Long.fromNumber(m & 0x7ffffffc).getLowBitsUnsigned();
w = Long.fromNumber(w ^ util.rotl(Sbox[265 + (k[i] & 3)], k[i - 1]) & m).getLowBitsUnsigned();
k[i] = w;
}
}
}

Mars.prototype.getBlockSize = function() {
return BlockSize;
}
// Block size of cipher
Mars.prototype.getBlockSize = function() { return BlockSize; }
// Algorithm Name
Mars.prototype.getAlgorithmName = function() { return "MARS"; }
// Reset cipher
Mars.prototype.reset = function() {}

Mars.prototype.encrypt = function(src, index) {
index = index == null ? 0 : index;
return this.encryptBlock(src, index);
// Process a block
Mars.prototype.processBlock = function(input, inOff, inLen) {
if(this.forEncryption) {
encryptBlock(this, input, inOff);
} else {
decryptBlock(this, input, inOff);
}
}

var S = function(a) {
Expand All @@ -79,17 +89,17 @@ var S1 = function(a) {
return Sbox[((a)&0xff) + 256];
}

Mars.prototype.encryptBlock = function(block, index) {
var encryptBlock = function(self, block, index) {
// Decode the integers
var a = Long.fromNumber(util.decodeUInt32R(block, index)).getLowBitsUnsigned();
var b = Long.fromNumber(util.decodeUInt32R(block, index + 4)).getLowBitsUnsigned();
var c = Long.fromNumber(util.decodeUInt32R(block, index + 8)).getLowBitsUnsigned();
var d = Long.fromNumber(util.decodeUInt32R(block, index + 12)).getLowBitsUnsigned();

a = Long.fromNumber(a + this.key[0]).getLowBitsUnsigned();
b = Long.fromNumber(b + this.key[1]).getLowBitsUnsigned();
c = Long.fromNumber(c + this.key[2]).getLowBitsUnsigned();
d = Long.fromNumber(d + this.key[3]).getLowBitsUnsigned();
a = Long.fromNumber(a + self.key[0]).getLowBitsUnsigned();
b = Long.fromNumber(b + self.key[1]).getLowBitsUnsigned();
c = Long.fromNumber(c + self.key[2]).getLowBitsUnsigned();
d = Long.fromNumber(d + self.key[3]).getLowBitsUnsigned();
var l = 0, m = 0, r = 0, t = 0;

for(var i = 0; i < 8; i++) {
Expand All @@ -104,9 +114,9 @@ Mars.prototype.encryptBlock = function(block, index) {

for(var i = 0; i < 16; i++) {
t = Long.fromNumber(util.rotl(a, 13)).getLowBitsUnsigned();
var rInt = Long.fromNumber(t).multiply(Long.fromNumber(this.key[2*i + 5])).getLowBitsUnsigned();
var rInt = Long.fromNumber(t).multiply(Long.fromNumber(self.key[2*i + 5])).getLowBitsUnsigned();
r = Long.fromNumber(util.rotl(rInt, 10)).getLowBitsUnsigned();
m = Long.fromNumber(a + this.key[2*i+4]).getLowBitsUnsigned();
m = Long.fromNumber(a + self.key[2*i+4]).getLowBitsUnsigned();
l = Long.fromNumber(util.rotl((S(m) ^ util.rotr(r, 5) ^ r), r)).getLowBitsUnsigned();
c = Long.fromNumber(c + util.rotl(m, util.rotr(r, 5))).getLowBitsUnsigned();
if(i < 8) {
Expand Down Expand Up @@ -134,35 +144,29 @@ Mars.prototype.encryptBlock = function(block, index) {
a = b, b = c, c = d, d = t;
}

a = Long.fromNumber(a - this.key[36]).getLowBitsUnsigned();
b = Long.fromNumber(b - this.key[37]).getLowBitsUnsigned();
c = Long.fromNumber(c - this.key[38]).getLowBitsUnsigned();
d = Long.fromNumber(d - this.key[39]).getLowBitsUnsigned();
a = Long.fromNumber(a - self.key[36]).getLowBitsUnsigned();
b = Long.fromNumber(b - self.key[37]).getLowBitsUnsigned();
c = Long.fromNumber(c - self.key[38]).getLowBitsUnsigned();
d = Long.fromNumber(d - self.key[39]).getLowBitsUnsigned();

util.inPlaceEncodeUInt32R(a, block, index);
util.inPlaceEncodeUInt32R(b, block, index + 4);
util.inPlaceEncodeUInt32R(c, block, index + 8);
util.inPlaceEncodeUInt32R(d, block, index + 12);
return block;
}

Mars.prototype.decrypt = function(src, index) {
index = index == null ? 0 : index;
return this.decryptBlock(src, index);
}

Mars.prototype.decryptBlock = function(block, index) {
var decryptBlock = function(self, block, index) {
// Decode the integers
var d = Long.fromNumber(util.decodeUInt32R(block, index)).getLowBitsUnsigned();
var c = Long.fromNumber(util.decodeUInt32R(block, index + 4)).getLowBitsUnsigned();
var b = Long.fromNumber(util.decodeUInt32R(block, index + 8)).getLowBitsUnsigned();
var a = Long.fromNumber(util.decodeUInt32R(block, index + 12)).getLowBitsUnsigned();
var l = 0, m = 0, r = 0, t = 0;

d = Long.fromNumber(d + this.key[36]).getLowBitsUnsigned();
c = Long.fromNumber(c + this.key[37]).getLowBitsUnsigned();
b = Long.fromNumber(b + this.key[38]).getLowBitsUnsigned();
a = Long.fromNumber(a + this.key[39]).getLowBitsUnsigned();
d = Long.fromNumber(d + self.key[36]).getLowBitsUnsigned();
c = Long.fromNumber(c + self.key[37]).getLowBitsUnsigned();
b = Long.fromNumber(b + self.key[38]).getLowBitsUnsigned();
a = Long.fromNumber(a + self.key[39]).getLowBitsUnsigned();

for(var i = 0; i < 8; i++) {
b = Long.fromNumber(b ^ S0(a)).add(Long.fromNumber(S1(a >>> 8))).getLowBitsUnsigned()
Expand All @@ -176,9 +180,9 @@ Mars.prototype.decryptBlock = function(block, index) {

for(var i = 0; i < 16; i++) {
t = Long.fromNumber(util.rotr(a, 13)).getLowBitsUnsigned();
var rInt = Long.fromNumber(a).multiply(Long.fromNumber(this.key[35-2*i])).getLowBitsUnsigned();
var rInt = Long.fromNumber(a).multiply(Long.fromNumber(self.key[35-2*i])).getLowBitsUnsigned();
r = Long.fromNumber(util.rotl(rInt, 10)).getLowBitsUnsigned();
m = Long.fromNumber(t + this.key[34-2*i]).getLowBitsUnsigned();
m = Long.fromNumber(t + self.key[34-2*i]).getLowBitsUnsigned();

var lInt1 = Long.fromNumber(S(m) ^ Long.fromNumber(util.rotr(r, 5)).getLowBitsUnsigned() ^ r).getLowBitsUnsigned();
l = Long.fromNumber(util.rotl(lInt1, r)).getLowBitsUnsigned();
Expand Down Expand Up @@ -209,17 +213,16 @@ Mars.prototype.decryptBlock = function(block, index) {
a = b, b = c, c = d, d =t;
}

d = Long.fromNumber(d - this.key[0]).getLowBitsUnsigned();
c = Long.fromNumber(c - this.key[1]).getLowBitsUnsigned();
b = Long.fromNumber(b - this.key[2]).getLowBitsUnsigned();
a = Long.fromNumber(a - this.key[3]).getLowBitsUnsigned();
d = Long.fromNumber(d - self.key[0]).getLowBitsUnsigned();
c = Long.fromNumber(c - self.key[1]).getLowBitsUnsigned();
b = Long.fromNumber(b - self.key[2]).getLowBitsUnsigned();
a = Long.fromNumber(a - self.key[3]).getLowBitsUnsigned();

// Return the encrypted bytes
util.inPlaceEncodeUInt32R(d, block, index);
util.inPlaceEncodeUInt32R(c, block, index + 4);
util.inPlaceEncodeUInt32R(b, block, index + 8);
util.inPlaceEncodeUInt32R(a, block, index + 12);
return block;
}

const Sbox = [
Expand Down
146 changes: 0 additions & 146 deletions test/idea_test.js

This file was deleted.

Loading

0 comments on commit 8ca919b

Please sign in to comment.