Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Cleanup.

  • Loading branch information...
commit a9d7c3a4c3139ae6d6f8e6d78e245223757c00a7 1 parent 12c396b
FrozenCow authored

Showing 3 changed files with 137 additions and 166 deletions. Show diff stats Hide diff stats

  1. +1 135 keepass.js
  2. +1 31 keepasscli.js
  3. +135 0 salsa20.js
136 keepass.js
@@ -163,141 +163,7 @@ exports.readDatabase = function(userKeys, filePath, result, error) {// try {
163 163 }
164 164 */
165 165 },
166   - 2: function Salsa20(key) {
167   - var iv = [ 0xE8, 0x30, 0x09, 0x4B, 0x97, 0x20, 0x5D, 0x2A ];
168   - var sigma = [ 0x61707865, 0x3320646E, 0x79622D32, 0x6B206574 ];
169   - var x = clearArray(new Array(16), 0);
170   - var state = clearArray(new Array(16), 0);
171   - var output = clearArray(new Array(64), 0);
172   - setupKey(state);
173   - setupIV(state);
174   -
175   - var outputPos = 64;
176   -
177   - function clearArray(b, n) {
178   - for(var i=0;i<b.length;i++) { b[i]=n; }
179   - return b;
180   - }
181   - function unsigned(a) {
182   - return ((a | 1) >>> 1) * 2 + (a & 1);
183   - }
184   - function lshift(a, b) {
185   - return unsigned(a << b);
186   - }
187   - function rshift(a, b) {
188   - return a >>> Math.min(32, b);
189   - }
190   - function bor(a, b) {
191   - return unsigned(a | b);
192   - }
193   - function bxor(a, b) {
194   - return unsigned(a ^ b);
195   - }
196   - function U8To32Little(b, offset) {
197   - return bor(bor(bor(b[offset], lshift(b[offset+1], 8)), lshift(b[offset+2], 16)), lshift(b[offset+3], 24));
198   - }
199   - function setupKey(state) {
200   - state[1] = U8To32Little(key, 0);
201   - state[2] = U8To32Little(key, 4);
202   - state[3] = U8To32Little(key, 8);
203   - state[4] = U8To32Little(key, 12);
204   - state[11] = U8To32Little(key, 16);
205   - state[12] = U8To32Little(key, 20);
206   - state[13] = U8To32Little(key, 24);
207   - state[14] = U8To32Little(key, 28);
208   - state[0] = sigma[0];
209   - state[5] = sigma[1];
210   - state[10] = sigma[2];
211   - state[15] = sigma[3];
212   - }
213   - function setupIV(state) {
214   - state[6] = U8To32Little(iv, 0);
215   - state[7] = U8To32Little(iv, 4);
216   - state[8] = 0;
217   - state[9] = 0;
218   - }
219   - function rotl32(x, b) {
220   - return bor(lshift(x, b), rshift(x, (32 - b)));
221   - }
222   - function add(a, b) {
223   - return (a + b);
224   - }
225   -
226   - function nextOutput() {
227   - x = state.slice(0);
228   - function rot(i, n) {
229   - x[i] = bxor(x[i], n);
230   - }
231   - for(var i = 0; i < 10; ++i) {
232   - rot( 4, rotl32(add(x[ 0], x[12]), 7));
233   - rot( 8, rotl32(add(x[ 4], x[ 0]), 9));
234   - rot(12, rotl32(add(x[ 8], x[ 4]), 13));
235   - rot( 0, rotl32(add(x[12], x[ 8]), 18));
236   - rot( 9, rotl32(add(x[ 5], x[ 1]), 7));
237   - rot(13, rotl32(add(x[ 9], x[ 5]), 9));
238   - rot( 1, rotl32(add(x[13], x[ 9]), 13));
239   - rot( 5, rotl32(add(x[ 1], x[13]), 18));
240   - rot(14, rotl32(add(x[10], x[ 6]), 7));
241   - rot( 2, rotl32(add(x[14], x[10]), 9));
242   - rot( 6, rotl32(add(x[ 2], x[14]), 13));
243   - rot(10, rotl32(add(x[ 6], x[ 2]), 18));
244   - rot( 3, rotl32(add(x[15], x[11]), 7));
245   - rot( 7, rotl32(add(x[ 3], x[15]), 9));
246   - rot(11, rotl32(add(x[ 7], x[ 3]), 13));
247   - rot(15, rotl32(add(x[11], x[ 7]), 18));
248   - rot( 1, rotl32(add(x[ 0], x[ 3]), 7));
249   - rot( 2, rotl32(add(x[ 1], x[ 0]), 9));
250   - rot( 3, rotl32(add(x[ 2], x[ 1]), 13));
251   - rot( 0, rotl32(add(x[ 3], x[ 2]), 18));
252   - rot( 6, rotl32(add(x[ 5], x[ 4]), 7));
253   - rot( 7, rotl32(add(x[ 6], x[ 5]), 9));
254   - rot( 4, rotl32(add(x[ 7], x[ 6]), 13));
255   - rot( 5, rotl32(add(x[ 4], x[ 7]), 18));
256   - rot(11, rotl32(add(x[10], x[ 9]), 7));
257   - rot( 8, rotl32(add(x[11], x[10]), 9));
258   - rot( 9, rotl32(add(x[ 8], x[11]), 13));
259   - rot(10, rotl32(add(x[ 9], x[ 8]), 18));
260   - rot(12, rotl32(add(x[15], x[14]), 7));
261   - rot(13, rotl32(add(x[12], x[15]), 9));
262   - rot(14, rotl32(add(x[13], x[12]), 13));
263   - rot(15, rotl32(add(x[14], x[13]), 18));
264   - }
265   - for(var i = 0; i < 16; ++i) {
266   - x[i] += state[i];
267   - }
268   - for(var i = 0; i < 16; ++i) {
269   - output[i << 2] = x[i] & 0xff;
270   - output[(i << 2) + 1] = rshift(x[i], 8) & 0xff;
271   - output[(i << 2) + 2] = rshift(x[i], 16) & 0xff;
272   - output[(i << 2) + 3] = rshift(x[i], 24) & 0xff;
273   - }
274   -
275   - state[8] = add(state[8], 1);
276   - if(state[8] === 0) {
277   - state[9] = add(state[9], 1);
278   - }
279   - }
280   -
281   - return function(byteCount) {
282   - var bytesRem = byteCount;
283   - var data = new Array(byteCount);
284   - var nOffset = 0;
285   - while (bytesRem > 0) {
286   - if (outputPos === 64) {
287   - nextOutput();
288   - outputPos = 0;
289   - }
290   - var nCopy = Math.min(64 - outputPos, bytesRem);
291   - for(var i=0;i<nCopy;i++){
292   - data[i+nOffset] = output[i+outputPos];
293   - }
294   - outputPos += nCopy;
295   - bytesRem -= nCopy;
296   - nOffset += nCopy;
297   - }
298   - return data;
299   - };
300   - }
  166 + 2: require('./salsa20').salsa20
301 167 }[header.randomStreamID])(crypto.SHA256(header.protectedStreamKey, {asBytes: true}));
302 168
303 169 // Create key with which to decrypt the rest of the file.
32 keepasscli.js
@@ -182,37 +182,7 @@ function main(args) {
182 182 function end() {
183 183
184 184 }
185   - /*keepass.readDatabase(userKeys, databaseFile, handleDatabase, function(error) {
186   - if (error instanceof Error) {
187   - console.error(error.message);
188   - } else if (typeof error === 'string') {
189   - console.error(error);
190   - } else {
191   - console.error('Unknown error:', error);
192   - }
193   - process.exit(1);
194   - });
195   -
196   - function handleDatabase(db) {
197   - (function handlegroup(group) {
198   - group.groups().forEach(handlegroup);
199   - group.entries().forEach(handleentry);
200   - })(db.root());
201   -
202   - function filter(entry) {
203   - return filters.every(function(f) { return f(entry); });
204   - }
205   -
206   - function handleentry(entry) {
207   - if (filter(entry)) {
208   - var leftoverActions = actions.slice(0);
209   - function next() {
210   - var action = leftoverActions.shift();
211   - action(entry, next);
212   - }
213   - }
214   - }
215   - }*/
216 185 }
217 186
  187 +// Slice 'node keepasscli.js' from the actual arguments.
218 188 main(process.argv.slice(2));
135 salsa20.js
... ... @@ -0,0 +1,135 @@
  1 +exports.salsa20 = function(key) {
  2 + var iv = [ 0xE8, 0x30, 0x09, 0x4B, 0x97, 0x20, 0x5D, 0x2A ];
  3 + var sigma = [ 0x61707865, 0x3320646E, 0x79622D32, 0x6B206574 ];
  4 + var x = clearArray(new Array(16), 0);
  5 + var state = clearArray(new Array(16), 0);
  6 + var output = clearArray(new Array(64), 0);
  7 + setupKey(state);
  8 + setupIV(state);
  9 +
  10 + var outputPos = 64;
  11 +
  12 + function clearArray(b, n) {
  13 + for(var i=0;i<b.length;i++) { b[i]=n; }
  14 + return b;
  15 + }
  16 + function unsigned(a) {
  17 + return ((a | 1) >>> 1) * 2 + (a & 1);
  18 + }
  19 + function lshift(a, b) {
  20 + return unsigned(a << b);
  21 + }
  22 + function rshift(a, b) {
  23 + return a >>> Math.min(32, b);
  24 + }
  25 + function bor(a, b) {
  26 + return unsigned(a | b);
  27 + }
  28 + function bxor(a, b) {
  29 + return unsigned(a ^ b);
  30 + }
  31 + function U8To32Little(b, offset) {
  32 + return bor(bor(bor(b[offset], lshift(b[offset+1], 8)), lshift(b[offset+2], 16)), lshift(b[offset+3], 24));
  33 + }
  34 + function setupKey(state) {
  35 + state[1] = U8To32Little(key, 0);
  36 + state[2] = U8To32Little(key, 4);
  37 + state[3] = U8To32Little(key, 8);
  38 + state[4] = U8To32Little(key, 12);
  39 + state[11] = U8To32Little(key, 16);
  40 + state[12] = U8To32Little(key, 20);
  41 + state[13] = U8To32Little(key, 24);
  42 + state[14] = U8To32Little(key, 28);
  43 + state[0] = sigma[0];
  44 + state[5] = sigma[1];
  45 + state[10] = sigma[2];
  46 + state[15] = sigma[3];
  47 + }
  48 + function setupIV(state) {
  49 + state[6] = U8To32Little(iv, 0);
  50 + state[7] = U8To32Little(iv, 4);
  51 + state[8] = 0;
  52 + state[9] = 0;
  53 + }
  54 + function rotl32(x, b) {
  55 + return bor(lshift(x, b), rshift(x, (32 - b)));
  56 + }
  57 + function add(a, b) {
  58 + return (a + b);
  59 + }
  60 +
  61 + function nextOutput() {
  62 + x = state.slice(0);
  63 + function rot(i, n) {
  64 + x[i] = bxor(x[i], n);
  65 + }
  66 + for(var i = 0; i < 10; ++i) {
  67 + rot( 4, rotl32(add(x[ 0], x[12]), 7));
  68 + rot( 8, rotl32(add(x[ 4], x[ 0]), 9));
  69 + rot(12, rotl32(add(x[ 8], x[ 4]), 13));
  70 + rot( 0, rotl32(add(x[12], x[ 8]), 18));
  71 + rot( 9, rotl32(add(x[ 5], x[ 1]), 7));
  72 + rot(13, rotl32(add(x[ 9], x[ 5]), 9));
  73 + rot( 1, rotl32(add(x[13], x[ 9]), 13));
  74 + rot( 5, rotl32(add(x[ 1], x[13]), 18));
  75 + rot(14, rotl32(add(x[10], x[ 6]), 7));
  76 + rot( 2, rotl32(add(x[14], x[10]), 9));
  77 + rot( 6, rotl32(add(x[ 2], x[14]), 13));
  78 + rot(10, rotl32(add(x[ 6], x[ 2]), 18));
  79 + rot( 3, rotl32(add(x[15], x[11]), 7));
  80 + rot( 7, rotl32(add(x[ 3], x[15]), 9));
  81 + rot(11, rotl32(add(x[ 7], x[ 3]), 13));
  82 + rot(15, rotl32(add(x[11], x[ 7]), 18));
  83 + rot( 1, rotl32(add(x[ 0], x[ 3]), 7));
  84 + rot( 2, rotl32(add(x[ 1], x[ 0]), 9));
  85 + rot( 3, rotl32(add(x[ 2], x[ 1]), 13));
  86 + rot( 0, rotl32(add(x[ 3], x[ 2]), 18));
  87 + rot( 6, rotl32(add(x[ 5], x[ 4]), 7));
  88 + rot( 7, rotl32(add(x[ 6], x[ 5]), 9));
  89 + rot( 4, rotl32(add(x[ 7], x[ 6]), 13));
  90 + rot( 5, rotl32(add(x[ 4], x[ 7]), 18));
  91 + rot(11, rotl32(add(x[10], x[ 9]), 7));
  92 + rot( 8, rotl32(add(x[11], x[10]), 9));
  93 + rot( 9, rotl32(add(x[ 8], x[11]), 13));
  94 + rot(10, rotl32(add(x[ 9], x[ 8]), 18));
  95 + rot(12, rotl32(add(x[15], x[14]), 7));
  96 + rot(13, rotl32(add(x[12], x[15]), 9));
  97 + rot(14, rotl32(add(x[13], x[12]), 13));
  98 + rot(15, rotl32(add(x[14], x[13]), 18));
  99 + }
  100 + for(var i = 0; i < 16; ++i) {
  101 + x[i] += state[i];
  102 + }
  103 + for(var i = 0; i < 16; ++i) {
  104 + output[i << 2] = x[i] & 0xff;
  105 + output[(i << 2) + 1] = rshift(x[i], 8) & 0xff;
  106 + output[(i << 2) + 2] = rshift(x[i], 16) & 0xff;
  107 + output[(i << 2) + 3] = rshift(x[i], 24) & 0xff;
  108 + }
  109 +
  110 + state[8] = add(state[8], 1);
  111 + if(state[8] === 0) {
  112 + state[9] = add(state[9], 1);
  113 + }
  114 + }
  115 +
  116 + return function(byteCount) {
  117 + var bytesRem = byteCount;
  118 + var data = new Array(byteCount);
  119 + var nOffset = 0;
  120 + while (bytesRem > 0) {
  121 + if (outputPos === 64) {
  122 + nextOutput();
  123 + outputPos = 0;
  124 + }
  125 + var nCopy = Math.min(64 - outputPos, bytesRem);
  126 + for(var i=0;i<nCopy;i++){
  127 + data[i+nOffset] = output[i+outputPos];
  128 + }
  129 + outputPos += nCopy;
  130 + bytesRem -= nCopy;
  131 + nOffset += nCopy;
  132 + }
  133 + return data;
  134 + };
  135 +};

0 comments on commit a9d7c3a

Please sign in to comment.
Something went wrong with that request. Please try again.