From 893423aa93f24f472c41ae84da8d4dbe2cee1966 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 20 May 2016 15:55:19 +0200 Subject: [PATCH] go interop for keyStretcher --- README.md | 23 +++++++++++++++++++++++ src/key-stretcher.js | 19 ++++++++----------- test/fixtures/go-stretch-key.js | 31 +++++++++++++++++++++++++++++++ test/key-stretcher.spec.js | 21 +++++++++++++++++++++ 4 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/go-stretch-key.js diff --git a/README.md b/README.md index 78304aa..c250003 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/src/key-stretcher.js b/src/key-stretcher.js index bb5be13..7aa918b 100644 --- a/src/key-stretcher.js +++ b/src/key-stretcher.js @@ -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} } diff --git a/test/fixtures/go-stretch-key.js b/test/fixtures/go-stretch-key.js new file mode 100644 index 0000000..597ffcb --- /dev/null +++ b/test/fixtures/go-stretch-key.js @@ -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 + ]) + } +}] diff --git a/test/key-stretcher.spec.js b/test/key-stretcher.spec.js index 7e6b7a2..2861bba 100644 --- a/test/key-stretcher.spec.js +++ b/test/key-stretcher.spec.js @@ -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) + }) + }) + }) })