go interop for keyStretcher

This commit is contained in:
Friedel Ziegelmayer 2016-05-20 15:55:19 +02:00
parent 7cb9d2820b
commit 893423aa93
4 changed files with 83 additions and 11 deletions

View File

@ -42,6 +42,29 @@ Returns an object of the form
}
```
### `keyStretcher(cipherType, hashType, secret)`
- `cipherType: String`, one of `'AES-128'`, `'AES-256'`, `'Blowfish'`
- `hashType: String`, one of `'SHA1'`, `SHA256`, `SHA512`
- `secret: Buffer`
Generates a set of keys for each party by stretching the shared key.
Returns an object of the form
```js
{
k1: {
iv: Buffer,
cipherKey: Buffer,
macKey: Buffer
},
k2: {
iv: Buffer,
cipherKey: Buffer,
macKey: Buffer
}
}
```
### `marshalPublicKey(key[, type])`
- `key: crypto.rsa.RsaPublicKey`

View File

@ -81,17 +81,14 @@ module.exports = (cipherType, hashType, secret) => {
const r1 = createBuffer(result.getBytes(half))
const r2 = createBuffer(result.getBytes())
const k1 = {
IV: r1.getBytes(ivSize),
CipherKey: r1.getBytes(cipherKeySize),
MacKey: r1.getBytes()
}
const createKey = (res) => ({
iv: new Buffer(res.getBytes(ivSize), 'binary'),
cipherKey: new Buffer(res.getBytes(cipherKeySize), 'binary'),
macKey: new Buffer(res.getBytes(), 'binary')
})
const k2 = {
IV: r2.getBytes(ivSize),
CipherKey: r2.getBytes(cipherKeySize),
MacKey: r2.getBytes()
return {
k1: createKey(r1),
k2: createKey(r2)
}
return {k1, k2}
}

31
test/fixtures/go-stretch-key.js vendored Normal file
View File

@ -0,0 +1,31 @@
'use strict'
module.exports = [{
cipher: 'AES-256',
hash: 'SHA256',
secret: new Buffer([
195, 191, 209, 165, 209, 201, 127, 122, 136, 111, 31, 66, 111, 68, 38, 155, 216, 204, 46, 181, 200, 188, 170, 204, 104, 74, 239, 251, 173, 114, 222, 234
]),
k1: {
iv: new Buffer([
208, 132, 203, 169, 253, 52, 40, 83, 161, 91, 17, 71, 33, 136, 67, 96
]),
cipherKey: new Buffer([
156, 48, 241, 157, 92, 248, 153, 186, 114, 127, 195, 114, 106, 104, 215, 133, 35, 11, 131, 137, 123, 70, 74, 26, 15, 60, 189, 32, 67, 221, 115, 137
]),
macKey: new Buffer([
6, 179, 91, 245, 224, 56, 153, 120, 77, 140, 29, 5, 15, 213, 187, 65, 137, 230, 202, 120
])
},
k2: {
iv: new Buffer([
236, 17, 34, 141, 90, 106, 197, 56, 197, 184, 157, 135, 91, 88, 112, 19
]),
cipherKey: new Buffer([
151, 145, 195, 219, 76, 195, 102, 109, 187, 231, 100, 150, 132, 245, 251, 130, 254, 37, 178, 55, 227, 34, 114, 39, 238, 34, 2, 193, 107, 130, 32, 87
]),
macKey: new Buffer([
3, 229, 77, 212, 241, 217, 23, 113, 220, 126, 38, 255, 18, 117, 108, 205, 198, 89, 1, 236
])
}
}]

View File

@ -4,11 +4,13 @@
const expect = require('chai').expect
const crypto = require('../src')
const fixtures = require('./fixtures/go-stretch-key')
describe('keyStretcher', () => {
describe('generate', () => {
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
const hashes = ['SHA1', 'SHA256']
// add 'SHA512' when https://github.com/digitalbazaar/forge/issues/401 is resolved
const res = crypto.generateEphemeralKeyPair('P-256')
const secret = res.genSharedKey(res.key)
@ -22,4 +24,23 @@ describe('keyStretcher', () => {
})
})
})
describe('go interop', () => {
fixtures.forEach((test) => {
it(`${test.cipher} - ${test.hash}`, () => {
const cipher = test.cipher
const hash = test.hash
const secret = test.secret
const keys = crypto.keyStretcher(cipher, hash, secret)
expect(keys.k1.iv).to.be.eql(test.k1.iv)
expect(keys.k1.cipherKey).to.be.eql(test.k1.cipherKey)
expect(keys.k1.macKey).to.be.eql(test.k1.macKey)
expect(keys.k2.iv).to.be.eql(test.k2.iv)
expect(keys.k2.cipherKey).to.be.eql(test.k2.cipherKey)
expect(keys.k2.macKey).to.be.eql(test.k2.macKey)
})
})
})
})