Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NOT FO MERGE!] Nodefy #9

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/npm-debug.log
27 changes: 27 additions & 0 deletions bin/export-cryptopro-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

const ExportKey = require('./export-key');

function ExportCryptoProKey() {
ExportKey.call(this);

this.toolName = 'gost-export-cryptopro-key';
this.containerKeysFiles = {
'header': 'header.key',
'name': 'name.key',
'primary': 'primary.key',
'masks': 'masks.key',
'primary2': 'primary2.key',
'masks2': 'masks2.key'
};
this.definedOptions.secondary = ['s', 'Extract from secondary keys', 'boolean', false];
}
require('util').inherits(ExportCryptoProKey, ExportKey);

ExportCryptoProKey.prototype.keyPromise = function keyPromise() {
return new this.gostCrypto.keys.CryptoProKeyContainer(
this.containerKeys()
).getKey(this.parsedOptions.password, this.parsedOptions.secondary);
}

new ExportCryptoProKey().run();
84 changes: 84 additions & 0 deletions bin/export-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module.exports = ExportKey

require('../lib/gostKeys');
const gostCrypto = require('../lib/gostCrypto');

function ExportKey() {
this.toolName = undefined;
this.containerKeysFiles = undefined;
this.definedOptions = {
container: ['c', 'Path to directory with container files', 'directory', '.'],
password: ['p', 'Private key`s password in case if encypted', 'string'],
format: ['f', 'Export format', ['PEM', 'DER'], 'PEM']
};
this.gostCrypto = gostCrypto;
this.parsedOptions = {};
}

ExportKey.prototype.run = function run() {
const cli = this.createCli();

this.parsedOptions = cli.options;

const format = this.parsedOptions.format;
this.keyPromise().then(function (key) {
var encodedKey = key.encode(format);
if ('PEM' == format) {
encodedKey += '\n';
} else if ('DER' == format) {
encodedKey = new Buffer(new Uint8Array(encodedKey));
}
process.stdout.write(encodedKey);

cli.ok('Private key successfully exported in STDOUT in "'+format+'" format.');
}).catch(function (e) {
cli.fatal(e.stack);
});
}

ExportKey.prototype.createCli = function createCli() {
const cli = require('cli')
.enable('version')
.setApp(__dirname + '/../package.json')
.setUsage(this.toolName+' [OPTIONS]');

var containerFiles = [];
for (var key in this.containerKeysFiles) {
containerFiles.push('"'+this.containerKeysFiles[key]+'"');
}
this.definedOptions.container[1] += ' '+containerFiles.join(', ');

cli.width = 120;
cli.option_width = 30;
cli.parse(this.definedOptions);

if (null == cli.options.container) {
cli.getUsage();
cli.exit(2);
}

return cli;
}

ExportKey.prototype.keyPromise = function keyPromise() {
throw new Error('Unimplemented method keyPromise')
}

ExportKey.prototype.containerKeys = function containerKeys() {
var result = {};
for (var key in this.containerKeysFiles) {
result[key] = this.readKey(this.containerKeysFiles[key]);
}

return result;
}

const fs = require('fs'),
path = require('path');

ExportKey.prototype.readKey = function readKey(relativePath) {
const root = path.normalize(this.parsedOptions.container),
resultPath = path.join(root, relativePath);

return new Uint8Array(fs.readFileSync(resultPath)).buffer;
}
25 changes: 25 additions & 0 deletions bin/export-signalcom-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env node

const ExportKey = require('./export-key');

function ExportSignalComKey() {
ExportKey.call(this);

this.toolName = 'gost-export-signalcom-key';
this.containerKeysFiles = {
'mk.db3': 'mk.db3',
'masks.db3': 'masks.db3',
'kek.opq': 'kek.opq'
};
this.definedOptions.key = ['k', 'Path to private key (relative to container)', 'path', 'keys/00000001.key'];
}
require('util').inherits(ExportSignalComKey, ExportKey);

ExportSignalComKey.prototype.keyPromise = function keyPromise() {
return new this.gostCrypto.keys.SignalComPrivateKeyInfo(
this.readKey(this.parsedOptions.key),
this.containerKeys()
).getKey(this.parsedOptions.password);
}

new ExportSignalComKey().run();
360 changes: 0 additions & 360 deletions dist/gostCrypto.dist.js

This file was deleted.

139 changes: 0 additions & 139 deletions dist/gostEngine.dist.js

This file was deleted.

4 changes: 2 additions & 2 deletions gostASN1.js → lib/gostASN1.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostCrypto', 'gostCoding', 'gostSecurity'], factory);
define(['./gostCrypto', './gostCoding', './gostSecurity'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCrypto'), require('gostCoding'), require('gostSecurity'));
module.exports = factory(require('./gostCrypto'), require('./gostCoding'), require('./gostSecurity'));
} else {
root.GostASN1 = factory(root.gostCrypto, root.GostCoding, root.GostSecurity);
}
Expand Down
4 changes: 2 additions & 2 deletions gostCMS.js → lib/gostCMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostCrypto', 'gostASN1', 'gostCert'], factory);
define(['./gostCrypto', './gostASN1', './gostCert'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCrypto'), require('gostASN1'), require('gostCert'));
module.exports = factory(require('./gostCrypto'), require('./gostASN1'), require('./gostCert'));
} else {
root.GostCMS = factory(root.gostCrypto, root.GostASN1, root.GostCert);
}
Expand Down
4 changes: 2 additions & 2 deletions gostCert.js → lib/gostCert.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostCrypto', 'gostASN1'], factory);
define(['./gostCrypto', './gostASN1'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCrypto'), require('gostASN1'));
module.exports = factory(require('./gostCrypto'), require('./gostASN1'));
} else {
root.GostCert = factory(root.gostCrypto, root.GostASN1);
}
Expand Down
47 changes: 29 additions & 18 deletions gostCipher.js → lib/gostCipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostRandom'], factory);
define(['./gostRandom'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostRandom'));
module.exports = factory(require('./gostRandom'));
} else {
root.GostCipher = factory(root.GostRandom);
}
Expand Down Expand Up @@ -1642,23 +1642,28 @@
key[j] ^= mask[j];
}
// Test MAC for packKey with default sBox on zero 32 bytes array
var zero32 = new Uint8Array(k);
var test = verifyMAC.call(this, key, mac, zero32);
if (!test) {
// Try to use different sBoxes
var names = ['E-A', 'E-B', 'E-C', 'E-D', 'E-SC'];
for (var i = 0, n = names.length; i < n; i++) {
this.sBox = sBoxes[names[i]];
test = verifyMAC.call(this, key, mac, zero32);
if (test)
break;
}
}
var test = verifyMACSC.call(this, key, mac, function () { return new Uint8Array(k) });
if (!test)
throw new DataError('Invalid main key MAC');
return key.buffer;
} // </editor-fold>

function verifyMACSC(key, mac, dataCallable)
{
// Use default and then try to use different sBoxes
var names = ['default', 'E-A', 'E-B', 'E-C', 'E-D', 'E-SC'];
for (var i = 0, n = names.length; i < n; i++) {
if (typeof sBoxes[names[i]] !== 'undefined') {
this.sBox = sBoxes[names[i]];
}
if (verifyMAC.call(this, key, mac, dataCallable.call(this))) {
return true;
}
}

return false;
}

/**
* Algorithm name GOST 28147-SCKW<br><br>
*
Expand Down Expand Up @@ -1700,16 +1705,22 @@
*/
function unwrapKeySC(kek, cek) // <editor-fold defaultstate="collapsed">
{
var m = this.blockSize >> 1, n = this.keySize;
var m = this.blockSize >> 1, n = new Uint8Array(cek).length - m;
var k = buffer(kek);
var c = buffer(cek);
if (k.byteLength !== n)
if (k.byteLength !== this.keySize)
k = unpackKeySC.call(this, k);
var enc = new Uint8Array(c, 0, n); // Encrypted kek
var mac = new Uint8Array(c, n, m); // MAC for clear kek
var d = decryptECB.call(this, k, enc);
if (!verifyMAC.call(this, k, mac, d))

var d;
var test = verifyMACSC.call(this, k, mac, function () {
d = decryptECB.call(this, k, enc);
return d;
});
if (!test)
throw new DataError('Invalid key MAC');

return d;
} // </editor-fold>

Expand Down
4 changes: 2 additions & 2 deletions gostCipher.test.js → lib/gostCipher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
(function (root, factory) {

if (typeof define === 'function' && define.amd) {
define(['gostCoding', 'gostCipher'], factory);
define(['./gostCoding', './gostCipher'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCoding'), require('gostCipher'));
module.exports = factory(require('./gostCoding'), require('./gostCipher'));
} else {
if (typeof importScripts !== 'undefined') {
if (!root.onmessage)
Expand Down
8 changes: 4 additions & 4 deletions gostCoding.js → lib/gostCoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostCrypto'], factory);
define(['./gostCrypto'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCrypto'));
module.exports = factory(require('./gostCrypto'));
} else {
root.GostCoding = factory(root.gostCrypto);
}
Expand Down Expand Up @@ -123,7 +123,7 @@
var m3 = 2, s = '';
for (var n = d.length, u24 = 0, i = 0; i < n; i++) {
m3 = i % 3;
if (i > 0 && (i * 4 / 3) % (12 * slen) === 0)
if (i > 0 && (i * 4 / 3) % (8 * slen) === 0)
s += '\r\n';
u24 |= d[i] << (16 >>> m3 & 24);
if (m3 === 2 || n - i === 1) {
Expand Down Expand Up @@ -677,7 +677,7 @@
var bytelen = 0, ba = [], offset = 0;
for (var i = k, n = content.length; i < n; i += size - k) {
ba[i] = encodeBER({
object: new Unit8Array(content.buffer, i, Math.min(size - k, n - i)),
object: new Uint8Array(content.buffer, i, Math.min(size - k, n - i)),
tagNumber: tagNumber,
tagClass: 0,
tagConstructed: false
Expand Down
12 changes: 8 additions & 4 deletions gostCrypto.js → lib/gostCrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostRandom'], factory);
define(['./gostRandom', './gostEngine'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostRandom'));
module.exports = factory(require('./gostRandom'), require('./gostEngine'));
} else {
root.gostCrypto = factory(root.GostRandom);
root.gostCrypto = factory(root.GostRandom, root.gostEngine);
}
// </editor-fold>

}(this, function (GostRandom) {
}(this, function (GostRandom, gostEngine) {

/*
* Algorithm normalization
Expand Down Expand Up @@ -803,6 +803,10 @@

}

if (!root.gostEngine) {
root.gostEngine = gostEngine;
}

if (!worker) {
// Import main module
// Reason: we are already in worker process or Worker interface is not
Expand Down
4 changes: 2 additions & 2 deletions gostDigest.js → lib/gostDigest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostRandom', 'gostCipher'], factory);
define(['./gostRandom', './gostCipher'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostRandom'), require('gostCipher'));
module.exports = factory(require('./gostRandom'), require('./gostCipher'));
} else {
root.GostDigest = factory(root.GostRandom, root.GostCipher);
}
Expand Down
4 changes: 2 additions & 2 deletions gostDigest.test.js → lib/gostDigest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['gostCoding', 'gostDigest'], factory);
define(['./gostCoding', './gostDigest'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCoding'), require('gostCipher'), require('gostDigest'));
module.exports = factory(require('./gostCoding'), require('./gostCipher'), require('./gostDigest'));
} else {
if (typeof importScripts !== 'undefined') {
if (!root.onmessage)
Expand Down
28 changes: 2 additions & 26 deletions gostEngine.js → lib/gostEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostRandom', 'gostCipher', 'gostDigest', 'gostSign'], factory);
define(['./gostRandom', './gostCipher', './gostDigest', './gostSign'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostRandom'), require('gostCipher'), require('gostDigest'), require('gostSign'));
module.exports = factory(require('./gostRandom'), require('./gostCipher'), require('./gostDigest'), require('./gostSign'));
} else {
if (typeof importScripts !== 'undefined') {
if (!(root.GostRandom && root.GostCipher && root.GostDigest && root.GostSign))
Expand Down Expand Up @@ -437,30 +437,6 @@
}
})();
}

// Local importScripts procedure for include dependens
var importScripts = function () {
for (var i = 0, n = arguments.length; i < n; i++) {
var name = arguments[i].split('.'),
src = baseUrl + name[0] + nameSuffix + '.' + name[1];
var el = document.querySelector('script[src="' + src + '"]');
if (!el) {
el = document.createElement('script');
el.setAttribute('src', src);
document.head.appendChild(el);
}
}
};

// Import engines
if (!GostRandom)
importScripts('gostRandom.js');
if (!GostCipher)
importScripts('gostCipher.js');
if (!GostDigest)
importScripts('gostDigest.js');
if (!GostSign)
importScripts('gostSign.js');
} // </editor-fold>

return gostEngine;
Expand Down
Loading