1.5.3 • Published 9 months ago

futoin-hkdf v1.5.3

Weekly downloads
91,889
License
Apache-2.0
Repository
github
Last release
9 months ago

NPM Version NPM Downloads stable

NPM

About

Node.js implementation of RFC5869: HMAC-based Extract-and-Expand Key Derivation Function (HKDF).

Additionally, it supports a HKDF-Expand-Label variation based on RFC8446: The Transport Layer Security (TLS) Protocol Version 1.3, section 7.1. Key Schedule.

The implementation is fully compliant with test vectors provided in the RFC.

There are alternative modules, but they are:

  • much less performing and/or
  • have quite poor code quality at the moment and/or
  • are not compliant with RFC (e.g. work only with string parameters) and/or
  • not working with current Node.js versions and/or
  • do not support arbitrary hash functions and/or
  • not reliable dependency for FutoIn™ Security concept in general.

Standalone HKDF extract() and expand() actions are also available for advanced usage.

Documentation --> FutoIn™ Guide

Author: Andrey Galkin

Performance comparison

The figures in "derived keys per second".

  • futoin-hkdf - 74 642
    • fully compliant
  • node-hdkf/hdkf modules - 57 707 (~22% slower)
    • seems to be broken by design
    • produces wrong results with RFC test vectors
  • ctrlpanel-hdkf - 52 181 (~30% slower)
    • seems to be compliant
  • @stablelib/hkdf - 39 808 (~46% slower)
    • seems to be compliant

Installation for Node.js

Command line:

$ npm install futoin-hkdf --save

or:

$ yarn add futoin-hkdf --save

Examples

const hkdf = require('futoin-hkdf');

// Parameter overview
//-------------------
// initial keying material
const ikm = 'string-or-buffer';
// required output length in bytes
const length = 16;
// can be empty string or false equivalent
const salt = 'strongly-encouraged';
// optional parameter
const info = 'optional-context';
// HMAC hashing algorithm to use
const hash = 'SHA-256';

// Generic derivation
//-------------------
hkdf(ikm, length, {salt, info, hash}); // Buffer(length) - derived key
hkdf(ikm, length, {salt, info, hash}).toString('hex'); // String(2*length)

// NOTE: all optional paramaters are passed in object

// With some parameters omitted
//-------------------
hkdf(ikm, length, {salt});
hkdf(ikm, length, {info});
hkdf(ikm, length, {hash});
hkdf(ikm, length);

// Advanced usage (only if you know what you are doing)
//-------------------

// As in underlying Node.js crypto library
const lhash = hash.toLowerCase().replace( '-', '' ); // 'sha256'

hkdf.hash_length(lhash); // get hash_len
hkdf.extract(lhash, hash_len, ikm, salt); // run only step #1
hkdf.expand(lhash, hash_len, prk, length, info); // run only step #2

// TLS v1.3+
//-------------------
const hkdf_tls = require('futoin-hkdf/tls');

const label = 'tls13 ...';
const context = Buffer.from( /* E.g some binary hash generation */ '' );

hkdf_tls(ikm, length, {salt, label, context, hash}); // Buffer(length) - derived key

// Advanced usage
hkdf_tls.expand_label(lhash, hash_len, prk, length, label, context);

// Same as:
hkdf.expand(lhash, hash_len, prk, length, hkdf_tls.info(length, label, context));

API documentation

Functions

hkdf(ikm, length, salt, info, hash) ⇒ Buffer

HMAC-based Extract-and-Expand Key Derivation Function (HKDF)

Kind: global function
Returns: Buffer - Raw buffer with derived key of @p length bytes

ParamTypeDefaultDescription
ikmBuffer | stringInitial Keying Material
lengthintegerRequired byte length of output
saltBuffer | string''Optional salt (recommended)
infoBuffer | string''Optional context (safe to skip)
hashstring"'SHA-256'"HMAC hash function to use

hkdf.hash_length(hash) ⇒ integer

Get expected hash length.

Kind: static method of hkdf
Returns: integer - hash digest byte length
Note: Values are hardcoded with fallback for unknown algorithms.

ParamTypeDescription
hashstringHash algorithm (as in underlying Node.js crypto library)

hkdf.extract(hash, hash_len, ikm, salt) ⇒ Buffer

HKDF extract action.

Kind: static method of hkdf
Returns: Buffer - A buffer with pseudorandom key
Note: Values are hardcoded with fallback for unknown algorithms.

ParamTypeDescription
hashstringHash algorithm (as in underlying Node.js crypto library)
hash_lenintegerHash digest length
ikmBuffer | stringInitial Keying Material
saltBuffer | stringOptional salt (recommended)

hkdf.expand(hash, hash_len, prk, length, info) ⇒ Buffer

HKDF expand action.

Kind: static method of hkdf
Returns: Buffer - A buffer with output keying material
Note: Values are hardcoded with fallback for unknown algorithms.

ParamTypeDescription
hashstringHash algorithm (as in underlying Node.js crypto library)
hash_lenintegerHash digest length
prkBuffer | stringA buffer with pseudorandom key
lengthintegerlength of output keying material in octets
infoBuffer | stringOptional context (safe to skip)

tls(ikm, length, salt, label, info, hash) ⇒ Buffer

TLS v1.3 HKDF-extract + HKFD-Expand-Label action

Kind: global function
Returns: Buffer - Raw buffer with derived key of @p length bytes
Note: label and context are limited to 255 bytes!

ParamTypeDefaultDescription
ikmBuffer | stringInitial Keying Material
lengthintegerRequired byte length of output
saltBuffer | string''Optional salt (required by fact)
labelBuffer | string''Optional label (required by fact)
infoBuffer | string''Optional context (safe to skip)
hashstring"'SHA-256'"HMAC hash function to use

tls.info(length, label, context) ⇒ Buffer

Encode HKDF context parameter in TLS v1.3 style based on RFC8446 TLS v1.3.

Kind: static method of tls
Returns: Buffer - A buffer with encoded HKDF context
Note: label and context are limited to 255 bytes!

ParamTypeDescription
lengthintegerlength of output keying material in octets
labelstringASCII label
contextBuffer | stringBinary context or empty string

tls.expand_label(hash, hash_len, prk, length, label, context) ⇒ Buffer

TLS-HKDF expand label action - a HKDF-Expand-Label variation based on RFC8446 TLS v1.3.

Kind: static method of tls
Returns: Buffer - A buffer with output keying material
Note: label and context are limited to 255 bytes!

ParamTypeDescription
hashstringHash algorithm (as in underlying Node.js crypto library)
hash_lenintegerHash digest length
prkBuffer | stringA buffer with pseudorandom key
lengthintegerlength of output keying material in octets
labelstringASCII label
contextBuffer | stringBinary context or empty string
whatsapp-devwa-webjseog_seog_xx@basisapi/baileys@basisapi/baileys-mobile@reclaimprotocol/reclaim-crypto-sdk@ravindu01manoj/sew-queen-webbaileys-apidarkleysmdsew-queen-web@ravindu01manoj/sew-queenwhatsapptracker-smp@s2click/venomelmenoufi-api-whatsapi_whats_elmenoufi@joaosouz4/venom-botwhatsvinoardrive-conn@bitexchange-ltd/bitexchange-server-7.5.0-beta.3@hyperspaceinc/next-authsuper-chats@evekran/ml-encryptericraj-auth0-nextjs@slonbook/baileyslibra-core-jsriprealtestmarvolo-botmarvolo-upsymbol-sdk-twixbaileys-bates@infinitebrahmanuniverse/nolb-futsew-websewlistsewqueen-wa-webravindumanoj@go-hd/op-gettbaileys@everything-registry/sub-chunk-1713nwncexpo-symbol-sdk@arifzyn/baileyseciesjs12beciesjscbecc-crypto-helper@evertonbzr/baileysenvelope-jssymbol-sdk-crypto@queenanya/baileys-qr@queenanya/baileys2queen-natsumi-web-api@simulacrum/auth0-cypress@skiff-org/skiff-crypto@seydx/hap-nodejs@sergeant61/baileyshavelockhelisson-whatsapphap-nodejshap-proxy@silasak/next-auth@pljtelecom/baileys@shopify/libra-web-wallet-utilsgraphql-thingszappaulo@sekmet/next-authexpress-openid-connectwa-decryptf2s-venom-botwabblewaapicoonrender-next-authevobotbaileyswtwawwmc-wapixchat-cryptowppconnect-dvwppconnect-iagopitunti-whatsapp-client2wm-kulap-libragarfield-bot-dbgarfieldx-bot-dbxxdrnv@pst4r8/baileys2024@rafaelgomes/carnagehomebridge-plugin-wrapper@queenanya/baileys@stableness/wabble@ravindu01manoj/web-api@reclaimprotocol/crypto-sdk@reclaimprotocol/reclaim-nodehydra-botwangsafffinpro-cryptopatched-express-openid-connectpassmanwhatstrackerappwhatsvawhatsviwhatsapp_elmenoufiwhatsbotjs
1.5.3

9 months ago

1.5.2

1 year ago

1.5.1

2 years ago

1.4.3

2 years ago

1.5.0

2 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.3

3 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.9.0

6 years ago