"use strict"; // Copyright (C) 2016 Dmitry Chestnykh // MIT License. See LICENSE file for details. Object.defineProperty(exports, "__esModule", { value: true }); var hmac_1 = require("@stablelib/hmac"); var wipe_1 = require("@stablelib/wipe"); /** * HMAC-based Extract-and-Expand Key Derivation Function. * * Implements HKDF from RFC5869. * * Expands the given master key with salt and info into * a limited stream of key material. */ var HKDF = /** @class */ (function () { /** * Create a new HKDF instance for the given hash function * with the master key, optional salt, and info. * * - Master key is a high-entropy secret key (not a password). * - Salt is a non-secret random value. * - Info is application- and/or context-specific information. */ function HKDF(hash, key, salt, info) { if (salt === void 0) { salt = new Uint8Array(0); } this._counter = new Uint8Array(1); // starts with zero this._hash = hash; this._info = info; // HKDF-Extract uses salt as HMAC key, and key as data. var okm = hmac_1.hmac(this._hash, salt, key); // Initialize HMAC for expanding with extracted key. this._hmac = new hmac_1.HMAC(hash, okm); // Allocate buffer. this._buffer = new Uint8Array(this._hmac.digestLength); this._bufpos = this._buffer.length; } // Fill buffer with new block of HKDF-Extract output. HKDF.prototype._fillBuffer = function () { // Increment counter. this._counter[0]++; var ctr = this._counter[0]; // Check if counter overflowed. if (ctr === 0) { throw new Error("hkdf: cannot expand more"); } // Prepare HMAC instance for new data with old key. this._hmac.reset(); // Hash in previous output if it was generated // (i.e. counter is greater than 1). if (ctr > 1) { this._hmac.update(this._buffer); } // Hash in info if it exists. if (this._info) { this._hmac.update(this._info); } // Hash in the counter. this._hmac.update(this._counter); // Output result to buffer and clean HMAC instance. this._hmac.finish(this._buffer); // Reset buffer position. this._bufpos = 0; }; /** * Expand returns next key material of the given length. * * It throws if expansion limit is reached (which is * 254 digests of the underlying HMAC function). */ HKDF.prototype.expand = function (length) { var out = new Uint8Array(length); for (var i = 0; i < out.length; i++) { if (this._bufpos === this._buffer.length) { this._fillBuffer(); } out[i] = this._buffer[this._bufpos++]; } return out; }; HKDF.prototype.clean = function () { this._hmac.clean(); wipe_1.wipe(this._buffer); wipe_1.wipe(this._counter); this._bufpos = 0; }; return HKDF; }()); exports.HKDF = HKDF; // TODO(dchest): maybe implement deriveKey? //# sourceMappingURL=hkdf.js.map