From d4c1672e06f4a8eb214e1149fcba0f57db1cb478 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 20 May 2016 14:41:25 +0200 Subject: [PATCH] rsa key compat with go --- .aegir.js | 13 +++++++++++ package.json | 11 +-------- src/key-stretcher.js | 12 +++++----- src/keys/rsa.js | 4 ++++ test/fixtures/go-key-rsa.js | 20 +++++++++++++++++ test/fixtures/go-rsa.key | 0 test/index.spec.js | 45 +++++++++++++++++++++++++++++++++++++ test/key-stretcher.spec.js | 2 +- 8 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 .aegir.js create mode 100644 test/fixtures/go-key-rsa.js delete mode 100644 test/fixtures/go-rsa.key diff --git a/.aegir.js b/.aegir.js new file mode 100644 index 0000000..b99a272 --- /dev/null +++ b/.aegir.js @@ -0,0 +1,13 @@ +'use strict' + +const path = require('path') + +module.exports = { + webpack: { + resolve: { + alias: { + 'node-forge': path.resolve(__dirname, 'vendor/forge.bundle.js') + } + } + } +} diff --git a/package.json b/package.json index 6de6c08..782af23 100644 --- a/package.json +++ b/package.json @@ -49,14 +49,5 @@ "bugs": { "url": "https://github.com/ipfs/js-libp2p-crypto/issues" }, - "homepage": "https://github.com/ipfs/js-libp2p-crypto", - "aegir": { - "webpack": { - "resolve": { - "alias": { - "node-forge": "../../vendor/forge.bundle.js" - } - } - } - } + "homepage": "https://github.com/ipfs/js-libp2p-crypto" } diff --git a/src/key-stretcher.js b/src/key-stretcher.js index 9751253..bb5be13 100644 --- a/src/key-stretcher.js +++ b/src/key-stretcher.js @@ -12,16 +12,16 @@ const cipherMap = { ivSize: 16, keySize: 32 }, - 'Blowfish': { + Blowfish: { ivSize: 8, cipherKeySize: 32 } } const hashMap = { - 'SHA1': 'sha1', - 'SHA256': 'sha256', - 'SHA512': 'sha512' + SHA1: 'sha1', + SHA256: 'sha256', + SHA512: 'sha512' } // Generates a set of keys for each party by stretching the shared key. @@ -43,8 +43,8 @@ module.exports = (cipherType, hashType, secret) => { } const cipherKeySize = cipher.keySize - const ivSize = cipher.ivSize - const hmacKeySize = 20 + const ivSize = cipher.ivSize + const hmacKeySize = 20 const seed = 'key expansion' const resultLength = 2 * (ivSize + cipherKeySize + hmacKeySize) diff --git a/src/keys/rsa.js b/src/keys/rsa.js index ef1782c..b127831 100644 --- a/src/keys/rsa.js +++ b/src/keys/rsa.js @@ -66,6 +66,10 @@ class RsaPrivateKey { } get public () { + if (!this._publicKey) { + throw new Error('public key not provided') + } + return new RsaPublicKey(this._publicKey) } diff --git a/test/fixtures/go-key-rsa.js b/test/fixtures/go-key-rsa.js new file mode 100644 index 0000000..7f6cf1a --- /dev/null +++ b/test/fixtures/go-key-rsa.js @@ -0,0 +1,20 @@ +'use strict' + +module.exports = { + private: { + hash: new Buffer([ + 18, 32, 168, 125, 165, 65, 34, 157, 209, 4, 24, 158, 80, 196, 125, 86, 103, 0, 228, 145, 109, 252, 153, 7, 189, 9, 16, 37, 239, 36, 48, 78, 214, 212 + ]), + key: new Buffer([ + 8, 0, 18, 192, 2, 48, 130, 1, 60, 2, 1, 0, 2, 65, 0, 230, 157, 160, 242, 74, 222, 87, 0, 77, 180, 91, 175, 217, 166, 2, 95, 193, 239, 195, 140, 224, 57, 84, 207, 46, 172, 113, 196, 20, 133, 117, 205, 45, 7, 224, 41, 40, 195, 254, 124, 14, 84, 223, 147, 67, 198, 48, 36, 53, 161, 112, 46, 153, 90, 19, 123, 94, 247, 5, 116, 1, 238, 32, 15, 2, 3, 1, 0, 1, 2, 65, 0, 191, 59, 140, 255, 254, 23, 123, 91, 148, 19, 240, 71, 213, 26, 181, 51, 68, 181, 150, 153, 214, 65, 148, 83, 45, 103, 239, 250, 225, 237, 125, 173, 111, 244, 37, 124, 87, 178, 86, 10, 14, 207, 63, 105, 213, 37, 81, 23, 230, 4, 222, 179, 144, 40, 252, 163, 190, 7, 241, 221, 28, 54, 225, 209, 2, 33, 0, 235, 132, 229, 150, 99, 182, 176, 194, 198, 65, 210, 160, 184, 70, 82, 49, 235, 199, 14, 11, 92, 66, 237, 45, 220, 72, 235, 1, 244, 145, 205, 57, 2, 33, 0, 250, 171, 146, 180, 188, 194, 14, 152, 52, 64, 38, 52, 158, 86, 46, 109, 66, 100, 122, 43, 88, 167, 143, 98, 104, 143, 160, 60, 171, 185, 31, 135, 2, 33, 0, 206, 47, 255, 203, 100, 170, 137, 31, 75, 240, 78, 84, 212, 95, 4, 16, 158, 73, 27, 27, 136, 255, 50, 163, 166, 169, 211, 204, 87, 111, 217, 201, 2, 33, 0, 177, 51, 194, 213, 3, 175, 7, 84, 47, 115, 189, 206, 106, 180, 47, 195, 203, 48, 110, 112, 224, 14, 43, 189, 124, 127, 51, 222, 79, 226, 225, 87, 2, 32, 67, 23, 190, 222, 106, 22, 115, 139, 217, 244, 178, 53, 153, 99, 5, 176, 72, 77, 193, 61, 67, 134, 37, 238, 69, 66, 159, 28, 39, 5, 238, 125 + ]) + }, + public: { + hash: new Buffer([ + 18, 32, 112, 151, 163, 167, 204, 243, 175, 123, 208, 162, 90, 84, 199, 174, 202, 110, 0, 119, 27, 202, 7, 149, 161, 251, 215, 168, 163, 54, 93, 54, 195, 20 + ]), + key: new Buffer([ + 8, 0, 18, 94, 48, 92, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 75, 0, 48, 72, 2, 65, 0, 230, 157, 160, 242, 74, 222, 87, 0, 77, 180, 91, 175, 217, 166, 2, 95, 193, 239, 195, 140, 224, 57, 84, 207, 46, 172, 113, 196, 20, 133, 117, 205, 45, 7, 224, 41, 40, 195, 254, 124, 14, 84, 223, 147, 67, 198, 48, 36, 53, 161, 112, 46, 153, 90, 19, 123, 94, 247, 5, 116, 1, 238, 32, 15, 2, 3, 1, 0, 1 + ]) + } +} diff --git a/test/fixtures/go-rsa.key b/test/fixtures/go-rsa.key deleted file mode 100644 index e69de29..0000000 diff --git a/test/index.spec.js b/test/index.spec.js index 281c6d8..43bdcf1 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -4,6 +4,7 @@ const expect = require('chai').expect const crypto = require('../src') +const fixtures = require('./fixtures/go-key-rsa') describe('libp2p-crypto', () => { let key @@ -26,4 +27,48 @@ describe('libp2p-crypto', () => { expect(key2.equals(key)).to.be.eql(true) }) + + describe('go interop', () => { + it('unmarshals private key', () => { + const key = crypto.unmarshalPrivateKey(fixtures.private.key) + const hash = fixtures.private.hash + + expect( + key.hash() + ).to.be.eql( + hash + ) + }) + + it('unmarshals public key', () => { + const key = crypto.unmarshalPublicKey(fixtures.public.key) + const hash = fixtures.public.hash + + expect( + key.hash() + ).to.be.eql( + hash + ) + }) + + it('unmarshal -> marshal, private key', () => { + const key = crypto.unmarshalPrivateKey(fixtures.private.key) + const marshalled = crypto.marshalPrivateKey(key) + expect( + fixtures.private.key.equals(marshalled) + ).to.be.eql( + true + ) + }) + + it('unmarshal -> marshal, public key', () => { + const key = crypto.unmarshalPublicKey(fixtures.public.key) + const marshalled = crypto.marshalPublicKey(key) + expect( + fixtures.public.key.equals(marshalled) + ).to.be.eql( + true + ) + }) + }) }) diff --git a/test/key-stretcher.spec.js b/test/key-stretcher.spec.js index a8f590e..7e6b7a2 100644 --- a/test/key-stretcher.spec.js +++ b/test/key-stretcher.spec.js @@ -8,7 +8,7 @@ const crypto = require('../src') describe('keyStretcher', () => { describe('generate', () => { const ciphers = ['AES-128', 'AES-256', 'Blowfish'] - const hashes = ['SHA1', 'SHA256'/*, 'SHA512'*/] + const hashes = ['SHA1', 'SHA256'] const res = crypto.generateEphemeralKeyPair('P-256') const secret = res.genSharedKey(res.key)