rsa key compat with go

This commit is contained in:
Friedel Ziegelmayer 2016-05-20 14:41:25 +02:00
parent fe202607eb
commit d4c1672e06
8 changed files with 90 additions and 17 deletions

13
.aegir.js Normal file
View File

@ -0,0 +1,13 @@
'use strict'
const path = require('path')
module.exports = {
webpack: {
resolve: {
alias: {
'node-forge': path.resolve(__dirname, 'vendor/forge.bundle.js')
}
}
}
}

View File

@ -49,14 +49,5 @@
"bugs": { "bugs": {
"url": "https://github.com/ipfs/js-libp2p-crypto/issues" "url": "https://github.com/ipfs/js-libp2p-crypto/issues"
}, },
"homepage": "https://github.com/ipfs/js-libp2p-crypto", "homepage": "https://github.com/ipfs/js-libp2p-crypto"
"aegir": {
"webpack": {
"resolve": {
"alias": {
"node-forge": "../../vendor/forge.bundle.js"
}
}
}
}
} }

View File

@ -12,16 +12,16 @@ const cipherMap = {
ivSize: 16, ivSize: 16,
keySize: 32 keySize: 32
}, },
'Blowfish': { Blowfish: {
ivSize: 8, ivSize: 8,
cipherKeySize: 32 cipherKeySize: 32
} }
} }
const hashMap = { const hashMap = {
'SHA1': 'sha1', SHA1: 'sha1',
'SHA256': 'sha256', SHA256: 'sha256',
'SHA512': 'sha512' SHA512: 'sha512'
} }
// Generates a set of keys for each party by stretching the shared key. // 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 cipherKeySize = cipher.keySize
const ivSize = cipher.ivSize const ivSize = cipher.ivSize
const hmacKeySize = 20 const hmacKeySize = 20
const seed = 'key expansion' const seed = 'key expansion'
const resultLength = 2 * (ivSize + cipherKeySize + hmacKeySize) const resultLength = 2 * (ivSize + cipherKeySize + hmacKeySize)

View File

@ -66,6 +66,10 @@ class RsaPrivateKey {
} }
get public () { get public () {
if (!this._publicKey) {
throw new Error('public key not provided')
}
return new RsaPublicKey(this._publicKey) return new RsaPublicKey(this._publicKey)
} }

20
test/fixtures/go-key-rsa.js vendored Normal file
View File

@ -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
])
}
}

View File

View File

@ -4,6 +4,7 @@
const expect = require('chai').expect const expect = require('chai').expect
const crypto = require('../src') const crypto = require('../src')
const fixtures = require('./fixtures/go-key-rsa')
describe('libp2p-crypto', () => { describe('libp2p-crypto', () => {
let key let key
@ -26,4 +27,48 @@ describe('libp2p-crypto', () => {
expect(key2.equals(key)).to.be.eql(true) 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
)
})
})
}) })

View File

@ -8,7 +8,7 @@ const crypto = require('../src')
describe('keyStretcher', () => { describe('keyStretcher', () => {
describe('generate', () => { describe('generate', () => {
const ciphers = ['AES-128', 'AES-256', 'Blowfish'] const ciphers = ['AES-128', 'AES-256', 'Blowfish']
const hashes = ['SHA1', 'SHA256'/*, 'SHA512'*/] const hashes = ['SHA1', 'SHA256']
const res = crypto.generateEphemeralKeyPair('P-256') const res = crypto.generateEphemeralKeyPair('P-256')
const secret = res.genSharedKey(res.key) const secret = res.genSharedKey(res.key)