node-noise
Version:
44 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Nonce = exports.MAX_NONCE = exports.MIN_NONCE = void 0;
exports.MIN_NONCE = 0;
// For performance reasons, the nonce is represented as a JS `number`
// Although JS `number` can safely represent integers up to 2 ** 53 - 1, we choose to only use
// 4 bytes to store the data for performance reason.
// This is a slight deviation from the noise spec, which describes the max nonce as 2 ** 64 - 2
// The effect is that this implementation will need a new handshake to be performed after fewer messages are exchanged than other implementations with full uint64 nonces.
// this MAX_NONCE is still a large number of messages, so the practical effect of this is negligible.
exports.MAX_NONCE = 0xffffffff;
var ERR_MAX_NONCE = 'Cipherstate has reached maximum n, a new handshake must be performed';
/**
* The nonce is an uint that's increased over time.
* Maintaining different representations help improve performance.
*/
var Nonce = /** @class */ (function () {
function Nonce(n) {
if (n === void 0) { n = exports.MIN_NONCE; }
this.n = n;
this.bytes = new Uint8Array(12);
this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength);
this.view.setUint32(4, n, true);
}
Nonce.prototype.increment = function () {
this.n++;
// Even though we're treating the nonce as 8 bytes, RFC7539 specifies 12 bytes for a nonce.
this.view.setUint32(4, this.n, true);
};
Nonce.prototype.getBytes = function () {
return this.bytes;
};
Nonce.prototype.getUint64 = function () {
return this.n;
};
Nonce.prototype.assertValue = function () {
if (this.n > exports.MAX_NONCE) {
throw new Error(ERR_MAX_NONCE);
}
};
return Nonce;
}());
exports.Nonce = Nonce;
//# sourceMappingURL=nonce.js.map