/
rc6.js
185 lines (153 loc) · 5.28 KB
/
rc6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var debug = require('sys').debug,
inspect = require('sys').inspect,
util = require('utils'),
Long = require('long').Long;
// RC6 Implementation in Javascript
const BlockSize = 16;
var ND = 0xFFFFFFFF;
var RC6 = exports.RC6 = function() {
}
RC6.prototype.init = function(forEncryption, key) {
this.forEncryption = forEncryption;
var keyLength = key.length;
key = key.slice(0);
var L = new Array(8);
var S = this.S = new Array(44);
S[0] = 0xB7E15163;
// Translate key to integers
for(var i = 0; i < (key.length / 4); i++) {
var valueArray = key.slice((i * 4), (i * 4 + 4)).reverse();
L[i] = Long.fromNumber(util.decodeUInt32(valueArray, 0)).getLowBitsUnsigned();
}
for(var i = 1; i < 44; i++) {
S[i]= S[i - 1] + 0x9E3779B9;
}
var A = 0;
var B = 0;
var i = 0;
var j = 0;
var v = 132;
// Adjust the keyLength
keyLength >>= 2;
for(var s = 1; s <= v; s++) {
A = S[i] = rotl((S[i] + A + B), 3);
B = L[j] = rotl(Long.fromNumber(L[j] + A + B).getLowBitsUnsigned(), Long.fromNumber(A + B).getLowBitsUnsigned());
i = (i + 1) % 44;
j = (j + 1) % keyLength;
}
}
var rotl = function(r, kr) {
kr = kr & 0x1F
if(kr > 0) {
return Long.fromNumber((((r & ND) << kr) | ((r &ND) >>> (32 - kr)))).getLowBitsUnsigned();
}
return r;
}
var rotr = function(r, kr) {
kr = kr & 0x1F
if(kr > 0) {
var a = Long.fromNumber(r).shiftRightUnsigned(kr);
var b = Long.fromNumber(r).shiftLeft(32-kr);
return Long.fromNumber(a.getLowBitsUnsigned()&ND | b.getLowBitsUnsigned()&ND).getLowBitsUnsigned();
}
return r;
}
// Block size of cipher
RC6.prototype.getBlockSize = function() { return BlockSize; }
// Algorithm Name
RC6.prototype.getAlgorithmName = function() { return "RC6"; }
// Reset cipher
RC6.prototype.reset = function() {}
// Process a block
RC6.prototype.processBlock = function(input, inOff, out, outOff) {
inOff = inOff == null ? 0 : inOff;
outOff = outOff == null ? 0 : outOff;
if(this.forEncryption) {
return encryptBlock(this, input, inOff, out, outOff);
} else {
return decryptBlock(this, input, inOff, out, outOff);
}
}
var encryptBlock = function(self, input, index, out, outOff) {
var regs = new Array(4);
var S = self.S;
for(var i = 0; i < 4; i ++) {
regs[i] = Long.fromNumber(util.decodeUInt32R(input, index + (i * 4))).getLowBitsUnsigned();
}
var A = regs[0];
var B = regs[1];
var C = regs[2];
var D = regs[3];
B = Long.fromNumber(B + S[0]).getLowBitsUnsigned();
D = Long.fromNumber(D + S[1]).getLowBitsUnsigned();
for(var j = 1; j <= 20; j++) {
// Calculate t
var br = Long.fromNumber(B).multiply(Long.fromNumber(((B << 1) + 1)));
var t = Long.fromNumber(rotl(br.getLowBitsUnsigned(), 5)).getLowBitsUnsigned();
// Calculate u
var br = Long.fromNumber(D).multiply(Long.fromNumber(((D << 1) + 1)));
var u = Long.fromNumber(rotl(br.getLowBitsUnsigned(), 5)).getLowBitsUnsigned();
// Calculate A
A = Long.fromNumber(rotl(Long.fromNumber(A ^ t).getLowBitsUnsigned(), u))
.add(Long.fromNumber(S[j << 1])).getLowBitsUnsigned();
// Calculate C
C = Long.fromNumber(rotl(Long.fromNumber(C ^ u), t))
.add(Long.fromNumber(S[(j << 1) + 1])).getLowBitsUnsigned();
var temp = A;
A = B;
B = C;
C = D;
D = temp;
}
// Adjust the variables
A = Long.fromNumber(A).add(Long.fromNumber(S[42])).getLowBitsUnsigned();
C = Long.fromNumber(C).add(Long.fromNumber(S[43])).getLowBitsUnsigned();
// Return the encrypted bytes
util.inPlaceEncodeUInt32R(A, out, outOff);
util.inPlaceEncodeUInt32R(B, out, outOff + 4);
util.inPlaceEncodeUInt32R(C, out, outOff + 8);
util.inPlaceEncodeUInt32R(D, out, outOff + 12);
return BlockSize;
}
var decryptBlock = function(self, input, index, out, outOff) {
var regs = new Array(4);
var S = self.S;
for(var i = 0; i < 4; i ++) {
regs[i] = Long.fromNumber(util.decodeUInt32R(input, index + (i * 4))).getLowBitsUnsigned();
}
var A = regs[0];
var B = regs[1];
var C = regs[2];
var D = regs[3];
C = Long.fromNumber(C - S[43]).getLowBitsUnsigned();
A = Long.fromNumber(A - S[42]).getLowBitsUnsigned();
for(var j = 20; j >= 1; j--) {
var temp = A;
A = D;
temp2 = B;
B = temp;
temp = C;
C = temp2;
D = temp;
// Calculate t
var br = Long.fromNumber(B).multiply(Long.fromNumber(((B << 1) + 1)));
var t = Long.fromNumber(rotl(br.getLowBitsUnsigned(), 5)).getLowBitsUnsigned();
// Calculate u
var br = Long.fromNumber(D).multiply(Long.fromNumber(((D << 1) + 1)));
var u = Long.fromNumber(rotl(br.getLowBitsUnsigned(), 5)).getLowBitsUnsigned();
// Calculate A
var br = Long.fromNumber(A - S[j << 1]).getLowBitsUnsigned();
A = Long.fromNumber(rotr(br, u) ^ t).getLowBitsUnsigned();
// Calculate C
var br = Long.fromNumber(C - S[(j << 1) + 1]).getLowBitsUnsigned();
C = Long.fromNumber(rotr(br, t) ^ u).getLowBitsUnsigned();
}
D = Long.fromNumber(D - S[1]).getLowBitsUnsigned();
B = Long.fromNumber(B - S[0]).getLowBitsUnsigned();
// Return the decrypted bytes
util.inPlaceEncodeUInt32R(A, out, outOff);
util.inPlaceEncodeUInt32R(B, out, outOff + 4);
util.inPlaceEncodeUInt32R(C, out, outOff + 8);
util.inPlaceEncodeUInt32R(D, out, outOff + 12);
return BlockSize;
}