Write hkdf test

This commit is contained in:
morrigan
2019-11-05 13:25:03 +01:00
parent ded3cfe5bc
commit 9ef8858be4
2 changed files with 23 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
import {bytes32, bytes16, uint32, uint64, bytes} from './types/basic'
import { Buffer } from 'buffer';
import * as crypto from 'libp2p-crypto';
import { AEAD, x25519, HKDF } from 'bcrypto';
import { AEAD, x25519, HKDF, SHA256 } from 'bcrypto';
export interface KeyPair {
publicKey: bytes32,
@@ -42,6 +42,7 @@ type NoiseSession = {
mc: uint64,
i: boolean,
}
export type Hkdf = [bytes, bytes, bytes];
const minNonce = 0;
@@ -158,20 +159,20 @@ export class XXHandshake {
if (protocolName.length <= 32) {
const h = Buffer.alloc(32);
protocolName.copy(h);
return Promise.resolve(h)
return h;
} else {
return await this.getHash(protocolName, Buffer.from([]));
}
}
private getHkdf(ck: bytes32, ikm: bytes) : Array<Buffer> {
public getHkdf(ck: bytes32, ikm: bytes) : Hkdf {
const info = Buffer.alloc(0);
const prk = HKDF.extract('SHA256', ikm, ck);
const okm = HKDF.expand('SHA256', prk, info, ikm.length);
const prk = HKDF.extract(SHA256, ikm, ck);
const okm = HKDF.expand(SHA256, prk, info, 96);
const k1 = okm.slice(0, 16);
const k2 = okm.slice(16, 32);
const k3 = okm.slice(32, 64);
const k1 = okm.slice(0, 32);
const k2 = okm.slice(32, 64);
const k3 = okm.slice(64, 96);
return [ k1, k2, k3 ];
}

View File

@@ -16,4 +16,18 @@ describe("Index", () => {
const session = await xx.initSession(true, prologue, kpInitiator, kpResponder.publicKey);
console.log(session)
})
it("Test get HKDF", async () => {
const xx = new XXHandshake();
const ckBytes = Buffer.from('4e6f6973655f58585f32353531395f58436861436861506f6c795f53484132353600000000000000000000000000000000000000000000000000000000000000', 'hex');
const ikm = Buffer.from('a3eae50ea37a47e8a7aa0c7cd8e16528670536dcd538cebfd724fb68ce44f1910ad898860666227d4e8dd50d22a9a64d1c0a6f47ace092510161e9e442953da3', 'hex');
const ck = Buffer.alloc(32);
ckBytes.copy(ck);
const [k1, k2, k3] = xx.getHkdf(ck, ikm);
expect(k1.toString('hex')).to.equal('cc5659adff12714982f806e2477a8d5ddd071def4c29bb38777b7e37046f6914');
expect(k2.toString('hex')).to.equal('a16ada915e551ab623f38be674bb4ef15d428ae9d80688899c9ef9b62ef208fa');
expect(k3.toString('hex')).to.equal('ff67bf9727e31b06efc203907e6786667d2c7a74ac412b4d31a80ba3fd766f68');
})
});