Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@stablelib/xchacha20poly1305

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stablelib/xchacha20poly1305 - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

typedoc.json

46

lib/xchacha20poly1305.bench.js

@@ -1,28 +0,26 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
var xchacha20poly1305_1 = require("./xchacha20poly1305");
var benchmark_1 = require("@stablelib/benchmark");
var buf8192 = benchmark_1.byteSeq(8192);
var buf1111 = benchmark_1.byteSeq(1111);
var buf0 = new Uint8Array(0);
var key = benchmark_1.byteSeq(32);
var nonce = benchmark_1.byteSeq(24);
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(key);
benchmark_1.report("XChaCha20Poly1305 seal 8K", benchmark_1.benchmark(function () { return aead.seal(nonce, buf8192); }, buf8192.length));
benchmark_1.report("XChaCha20Poly1305 seal 1111", benchmark_1.benchmark(function () { return aead.seal(nonce, buf1111); }, buf1111.length));
benchmark_1.report("XChaCha20Poly1305 seal 8K + AD", benchmark_1.benchmark(function () { return aead.seal(nonce, buf8192, buf8192); }, buf8192.length * 2));
benchmark_1.report("XChaCha20Poly1305 seal 1111 + AD", benchmark_1.benchmark(function () { return aead.seal(nonce, buf1111, buf1111); }, buf1111.length * 2));
benchmark_1.report("XChaCha20Poly1305 seal 0 + AD 8K", benchmark_1.benchmark(function () { return aead.seal(nonce, buf0, buf8192); }, buf8192.length));
var sealed8192 = aead.seal(nonce, buf8192);
var sealed1111 = aead.seal(nonce, buf1111);
var sealed8192ad = aead.seal(nonce, buf8192, buf8192);
var sealed1111ad = aead.seal(nonce, buf1111, buf1111);
benchmark_1.report("XChaCha20Poly1305 open 8K", benchmark_1.benchmark(function () { return aead.open(nonce, sealed8192); }, buf8192.length));
benchmark_1.report("XChaCha20Poly1305 open 1111", benchmark_1.benchmark(function () { return aead.open(nonce, sealed1111); }, buf1111.length));
benchmark_1.report("XChaCha20Poly1305 open 8K + AD", benchmark_1.benchmark(function () { return aead.open(nonce, sealed8192ad, buf8192); }, buf8192.length * 2));
benchmark_1.report("XChaCha20Poly1305 open 1111 + AD", benchmark_1.benchmark(function () { return aead.seal(nonce, sealed1111ad, buf1111); }, buf1111.length * 2));
import { XChaCha20Poly1305 } from "./xchacha20poly1305";
import { benchmark, report, byteSeq } from "@stablelib/benchmark";
const buf8192 = byteSeq(8192);
const buf1111 = byteSeq(1111);
const buf0 = new Uint8Array(0);
const key = byteSeq(32);
const nonce = byteSeq(24);
const aead = new XChaCha20Poly1305(key);
report("XChaCha20Poly1305 seal 8K", benchmark(() => aead.seal(nonce, buf8192), buf8192.length));
report("XChaCha20Poly1305 seal 1111", benchmark(() => aead.seal(nonce, buf1111), buf1111.length));
report("XChaCha20Poly1305 seal 8K + AD", benchmark(() => aead.seal(nonce, buf8192, buf8192), buf8192.length * 2));
report("XChaCha20Poly1305 seal 1111 + AD", benchmark(() => aead.seal(nonce, buf1111, buf1111), buf1111.length * 2));
report("XChaCha20Poly1305 seal 0 + AD 8K", benchmark(() => aead.seal(nonce, buf0, buf8192), buf8192.length));
const sealed8192 = aead.seal(nonce, buf8192);
const sealed1111 = aead.seal(nonce, buf1111);
const sealed8192ad = aead.seal(nonce, buf8192, buf8192);
const sealed1111ad = aead.seal(nonce, buf1111, buf1111);
report("XChaCha20Poly1305 open 8K", benchmark(() => aead.open(nonce, sealed8192), buf8192.length));
report("XChaCha20Poly1305 open 1111", benchmark(() => aead.open(nonce, sealed1111), buf1111.length));
report("XChaCha20Poly1305 open 8K + AD", benchmark(() => aead.open(nonce, sealed8192ad, buf8192), buf8192.length * 2));
report("XChaCha20Poly1305 open 1111 + AD", benchmark(() => aead.seal(nonce, sealed1111ad, buf1111), buf1111.length * 2));
sealed8192[0] ^= sealed8192[0];
benchmark_1.report("XChaCha20Poly1305 open (bad)", benchmark_1.benchmark(function () { return aead.open(nonce, sealed8192); }, buf8192.length));
report("XChaCha20Poly1305 open (bad)", benchmark(() => aead.open(nonce, sealed8192), buf8192.length));
//# sourceMappingURL=xchacha20poly1305.bench.js.map
/**
* Package xchacha20poly1305 implements XChaCha20-Poly1305 AEAD.
*/
import { AEAD } from "@stablelib/aead";
import type { AEAD } from "@stablelib/aead";
export declare const KEY_LENGTH = 32;

@@ -6,0 +6,0 @@ export declare const NONCE_LENGTH = 24;

@@ -1,11 +0,9 @@

"use strict";
// Copyright (C) 2019 Kyle Den Hartog
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
var xchacha20_1 = require("@stablelib/xchacha20");
var chacha20poly1305_1 = require("@stablelib/chacha20poly1305");
var wipe_1 = require("@stablelib/wipe");
exports.KEY_LENGTH = 32;
exports.NONCE_LENGTH = 24;
exports.TAG_LENGTH = 16;
import { hchacha } from "@stablelib/xchacha20";
import { ChaCha20Poly1305 } from "@stablelib/chacha20poly1305";
import { wipe } from "@stablelib/wipe";
export const KEY_LENGTH = 32;
export const NONCE_LENGTH = 24;
export const TAG_LENGTH = 16;
/**

@@ -17,10 +15,11 @@ * XChaCha20-Poly1305 Authenticated Encryption with Associated Data.

*/
var XChaCha20Poly1305 = /** @class */ (function () {
export class XChaCha20Poly1305 {
nonceLength = NONCE_LENGTH;
tagLength = TAG_LENGTH;
_key;
/**
* Creates a new instance with the given 32-byte key.
*/
function XChaCha20Poly1305(key) {
this.nonceLength = exports.NONCE_LENGTH;
this.tagLength = exports.TAG_LENGTH;
if (key.length !== exports.KEY_LENGTH) {
constructor(key) {
if (key.length !== KEY_LENGTH) {
throw new Error("ChaCha20Poly1305 needs 32-byte key");

@@ -45,3 +44,3 @@ }

*/
XChaCha20Poly1305.prototype.seal = function (nonce, plaintext, associatedData, dst) {
seal(nonce, plaintext, associatedData, dst) {
if (nonce.length !== 24) {

@@ -53,14 +52,14 @@ throw new Error("XChaCha20Poly1305: incorrect nonce length");

// stream -- "subkey".
var subKey = xchacha20_1.hchacha(this._key, nonce.subarray(0, 16), new Uint8Array(32));
const subKey = hchacha(this._key, nonce.subarray(0, 16), new Uint8Array(32));
// Use last 8 bytes of 24-byte extended nonce as an actual nonce prefixed by 4 zero bytes,
// and a subkey derived in the previous step as key to encrypt.
var modifiedNonce = new Uint8Array(12);
const modifiedNonce = new Uint8Array(12);
modifiedNonce.set(nonce.subarray(16), 4);
var chaChaPoly = new chacha20poly1305_1.ChaCha20Poly1305(subKey);
var result = chaChaPoly.seal(modifiedNonce, plaintext, associatedData, dst);
wipe_1.wipe(subKey);
wipe_1.wipe(modifiedNonce);
const chaChaPoly = new ChaCha20Poly1305(subKey);
const result = chaChaPoly.seal(modifiedNonce, plaintext, associatedData, dst);
wipe(subKey);
wipe(modifiedNonce);
chaChaPoly.clean();
return result;
};
}
/**

@@ -82,3 +81,3 @@ * Authenticates sealed ciphertext (which includes authentication tag) and

*/
XChaCha20Poly1305.prototype.open = function (nonce, sealed, associatedData, dst) {
open(nonce, sealed, associatedData, dst) {
if (nonce.length !== 24) {

@@ -96,3 +95,3 @@ throw new Error("XChaCha20Poly1305: incorrect nonce length");

*/
var subKey = xchacha20_1.hchacha(this._key, nonce.subarray(0, 16), new Uint8Array(32));
const subKey = hchacha(this._key, nonce.subarray(0, 16), new Uint8Array(32));
/**

@@ -102,3 +101,3 @@ * Generate Nonce as defined - remaining 8 bytes of the nonce prefixed with

*/
var modifiedNonce = new Uint8Array(12);
const modifiedNonce = new Uint8Array(12);
modifiedNonce.set(nonce.subarray(16), 4);

@@ -108,16 +107,14 @@ /**

*/
var chaChaPoly = new chacha20poly1305_1.ChaCha20Poly1305(subKey);
var result = chaChaPoly.open(modifiedNonce, sealed, associatedData, dst);
wipe_1.wipe(subKey);
wipe_1.wipe(modifiedNonce);
const chaChaPoly = new ChaCha20Poly1305(subKey);
const result = chaChaPoly.open(modifiedNonce, sealed, associatedData, dst);
wipe(subKey);
wipe(modifiedNonce);
chaChaPoly.clean();
return result;
};
XChaCha20Poly1305.prototype.clean = function () {
wipe_1.wipe(this._key);
}
clean() {
wipe(this._key);
return this;
};
return XChaCha20Poly1305;
}());
exports.XChaCha20Poly1305 = XChaCha20Poly1305;
}
}
//# sourceMappingURL=xchacha20poly1305.js.map

@@ -1,8 +0,7 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
var xchacha20poly1305_1 = require("./xchacha20poly1305");
var hex_1 = require("@stablelib/hex");
var testVectors = [
import { describe, expect, it } from 'vitest';
import { XChaCha20Poly1305 } from "./xchacha20poly1305";
import { encode, decode } from "@stablelib/hex";
const testVectors = [
/**

@@ -33,80 +32,76 @@ * Test vector from draft-irtf-cfrg-xchacha

// TODO(dchest): add more various tests.
describe("XChaCha20Poly1305", function () {
it("should correctly seal", function () {
testVectors.forEach(function (v) {
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var sealed = aead.seal(hex_1.decode(v.nonce), hex_1.decode(v.plaintext), v.aad ? hex_1.decode(v.aad) : undefined);
expect(hex_1.encode(sealed)).toBe(v.result);
describe("XChaCha20Poly1305", () => {
it("should correctly seal", () => {
testVectors.forEach(v => {
const aead = new XChaCha20Poly1305(decode(v.key));
const sealed = aead.seal(decode(v.nonce), decode(v.plaintext), v.aad ? decode(v.aad) : undefined);
expect(encode(sealed)).toBe(v.result);
});
});
it("should correctly open", function () {
testVectors.forEach(function (v) {
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var plaintext = aead.open(hex_1.decode(v.nonce), hex_1.decode(v.result), v.aad ? hex_1.decode(v.aad) : undefined);
it("should correctly open", () => {
testVectors.forEach(v => {
const aead = new XChaCha20Poly1305(decode(v.key));
const plaintext = aead.open(decode(v.nonce), decode(v.result), v.aad ? decode(v.aad) : undefined);
expect(plaintext).not.toBeNull();
if (plaintext) {
expect(hex_1.encode(plaintext)).toBe(v.plaintext);
expect(encode(plaintext)).toBe(v.plaintext);
}
});
});
it("should not open when ciphertext is corrupted", function () {
var v = testVectors[0];
var sealed = hex_1.decode(v.result);
it("should not open when ciphertext is corrupted", () => {
const v = testVectors[0];
const sealed = decode(v.result);
sealed[0] ^= sealed[0];
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var plaintext = aead.open(hex_1.decode(v.nonce), sealed, v.aad ? hex_1.decode(v.aad) : undefined);
const aead = new XChaCha20Poly1305(decode(v.key));
const plaintext = aead.open(decode(v.nonce), sealed, v.aad ? decode(v.aad) : undefined);
expect(plaintext).toBeNull();
});
it("should not open when tag is corrupted", function () {
var v = testVectors[0];
var sealed = hex_1.decode(v.result);
it("should not open when tag is corrupted", () => {
const v = testVectors[0];
const sealed = decode(v.result);
sealed[sealed.length - 1] ^= sealed[sealed.length - 1];
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var plaintext = aead.open(hex_1.decode(v.nonce), sealed, v.aad ? hex_1.decode(v.aad) : undefined);
const aead = new XChaCha20Poly1305(decode(v.key));
const plaintext = aead.open(decode(v.nonce), sealed, v.aad ? decode(v.aad) : undefined);
expect(plaintext).toBeNull();
});
it("should seal to dst it is provided", function () {
var v = testVectors[0];
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var plaintext = hex_1.decode(v.plaintext);
var ad = v.aad ? hex_1.decode(v.aad) : undefined;
var dst = new Uint8Array(plaintext.length + aead.tagLength);
var sealed = aead.seal(hex_1.decode(v.nonce), hex_1.decode(v.plaintext), ad, dst);
expect(hex_1.encode(dst)).toBe(hex_1.encode(sealed));
expect(hex_1.encode(sealed)).toBe(v.result);
it("should seal to dst it is provided", () => {
const v = testVectors[0];
const aead = new XChaCha20Poly1305(decode(v.key));
const plaintext = decode(v.plaintext);
const ad = v.aad ? decode(v.aad) : undefined;
const dst = new Uint8Array(plaintext.length + aead.tagLength);
const sealed = aead.seal(decode(v.nonce), decode(v.plaintext), ad, dst);
expect(encode(dst)).toBe(encode(sealed));
expect(encode(sealed)).toBe(v.result);
});
it("should throw if seal got dst of wrong length", function () {
var v = testVectors[0];
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var plaintext = hex_1.decode(v.plaintext);
var ad = v.aad ? hex_1.decode(v.aad) : undefined;
var dst = new Uint8Array(plaintext.length + aead.tagLength - 1); // wrong length
expect(function () {
return aead.seal(hex_1.decode(v.nonce), hex_1.decode(v.plaintext), ad, dst);
}).toThrowError(/length/);
it("should throw if seal got dst of wrong length", () => {
const v = testVectors[0];
const aead = new XChaCha20Poly1305(decode(v.key));
const plaintext = decode(v.plaintext);
const ad = v.aad ? decode(v.aad) : undefined;
const dst = new Uint8Array(plaintext.length + aead.tagLength - 1); // wrong length
expect(() => aead.seal(decode(v.nonce), decode(v.plaintext), ad, dst)).toThrowError(/length/);
});
it("should open to dst it is provided", function () {
var v = testVectors[0];
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var sealed = hex_1.decode(v.result);
var ad = v.aad ? hex_1.decode(v.aad) : undefined;
var dst = new Uint8Array(sealed.length - aead.tagLength);
var plaintext = aead.open(hex_1.decode(v.nonce), hex_1.decode(v.result), ad, dst);
it("should open to dst it is provided", () => {
const v = testVectors[0];
const aead = new XChaCha20Poly1305(decode(v.key));
const sealed = decode(v.result);
const ad = v.aad ? decode(v.aad) : undefined;
const dst = new Uint8Array(sealed.length - aead.tagLength);
const plaintext = aead.open(decode(v.nonce), decode(v.result), ad, dst);
expect(plaintext).not.toBeNull();
if (plaintext) {
expect(hex_1.encode(dst)).toBe(hex_1.encode(plaintext));
expect(hex_1.encode(plaintext)).toBe(v.plaintext);
expect(encode(dst)).toBe(encode(plaintext));
expect(encode(plaintext)).toBe(v.plaintext);
}
});
it("should throw if open got dst of wrong length", function () {
var v = testVectors[0];
var aead = new xchacha20poly1305_1.XChaCha20Poly1305(hex_1.decode(v.key));
var sealed = hex_1.decode(v.result);
var ad = v.aad ? hex_1.decode(v.aad) : undefined;
var dst = new Uint8Array(sealed.length - aead.tagLength + 1); // wrong length
expect(function () {
return aead.open(hex_1.decode(v.nonce), hex_1.decode(v.result), ad, dst);
}).toThrowError(/length/);
it("should throw if open got dst of wrong length", () => {
const v = testVectors[0];
const aead = new XChaCha20Poly1305(decode(v.key));
const sealed = decode(v.result);
const ad = v.aad ? decode(v.aad) : undefined;
const dst = new Uint8Array(sealed.length - aead.tagLength + 1); // wrong length
expect(() => aead.open(decode(v.nonce), decode(v.result), ad, dst)).toThrowError(/length/);
});
});
//# sourceMappingURL=xchacha20poly1305.test.js.map
{
"name": "@stablelib/xchacha20poly1305",
"version": "1.0.1",
"version": "2.0.0",
"description": "XChaCha20-Poly1305 AEAD (draft-irtf-cfrg-xchacha-01)",
"main": "./lib/xchacha20poly1305.js",
"type": "module",
"typings": "./lib/xchacha20poly1305.d.ts",

@@ -25,17 +26,17 @@ "contributors": [

"build": "tsc",
"test": "jasmine JASMINE_CONFIG_PATH=../../configs/jasmine.json",
"test": "vitest run",
"bench": "node ./lib/xchacha20poly1305.bench.js"
},
"dependencies": {
"@stablelib/aead": "^1.0.1",
"@stablelib/chacha20poly1305": "^1.0.1",
"@stablelib/constant-time": "^1.0.1",
"@stablelib/wipe": "^1.0.1",
"@stablelib/xchacha20": "^1.0.1"
"@stablelib/aead": "^2.0.0",
"@stablelib/chacha20poly1305": "^2.0.0",
"@stablelib/constant-time": "^2.0.0",
"@stablelib/wipe": "^2.0.0",
"@stablelib/xchacha20": "^2.0.0"
},
"devDependencies": {
"@stablelib/benchmark": "^1.0.1",
"@stablelib/hex": "^1.0.1"
"@stablelib/benchmark": "^2.0.0",
"@stablelib/hex": "^2.0.0"
},
"gitHead": "03dadf27703120d54e6be8436525228ee1c4299b"
"gitHead": "ecfe9109b3c05419fd3ffc16da6c8255b08ad64f"
}
{
"extends": "../../configs/tsconfig.json",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"removeComments": false,
"preserveConstEnums": true,
"moduleResolution": "node",
"newLine": "LF",
"sourceMap": true,
"declaration": true,
"outDir": "lib",
"lib": [
"es5",
"es2015.promise",
"dom",
"scripthost"
]
},

@@ -23,0 +6,0 @@ "exclude": [

// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
import { describe, expect, it } from 'vitest';
import { XChaCha20Poly1305 } from "./xchacha20poly1305";

@@ -5,0 +6,0 @@ import { encode, decode } from "@stablelib/hex";

@@ -8,3 +8,3 @@ // Copyright (C) 2019 Kyle Den Hartog

import { AEAD } from "@stablelib/aead";
import type { AEAD } from "@stablelib/aead";
import { hchacha } from "@stablelib/xchacha20";

@@ -11,0 +11,0 @@ import { ChaCha20Poly1305 } from "@stablelib/chacha20poly1305";

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc