New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@stablelib/xchacha20

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stablelib/xchacha20 - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

typedoc.json

26

lib/xchacha20.bench.js

@@ -1,17 +0,15 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
var xchacha20_1 = require("./xchacha20");
var benchmark_1 = require("@stablelib/benchmark");
var buf16K = benchmark_1.byteSeq(16 << 10);
var buf8192 = benchmark_1.byteSeq(8192);
var buf1111 = benchmark_1.byteSeq(1111);
var buf64 = benchmark_1.byteSeq(64);
var key = benchmark_1.byteSeq(32);
var nonce = benchmark_1.byteSeq(24);
benchmark_1.report("ChaCha20 xor 16K", benchmark_1.benchmark(function () { return xchacha20_1.streamXOR(key, nonce, buf16K, buf16K); }, buf16K.length));
benchmark_1.report("ChaCha20 xor 8K", benchmark_1.benchmark(function () { return xchacha20_1.streamXOR(key, nonce, buf8192, buf8192); }, buf8192.length));
benchmark_1.report("ChaCha20 xor 1111", benchmark_1.benchmark(function () { return xchacha20_1.streamXOR(key, nonce, buf1111, buf1111); }, buf1111.length));
benchmark_1.report("ChaCha20 xor 64", benchmark_1.benchmark(function () { return xchacha20_1.streamXOR(key, nonce, buf64, buf64); }, buf64.length));
import { streamXOR } from "./xchacha20";
import { benchmark, report, byteSeq } from "@stablelib/benchmark";
const buf16K = byteSeq(16 << 10);
const buf8192 = byteSeq(8192);
const buf1111 = byteSeq(1111);
const buf64 = byteSeq(64);
const key = byteSeq(32);
const nonce = byteSeq(24);
report("ChaCha20 xor 16K", benchmark(() => streamXOR(key, nonce, buf16K, buf16K), buf16K.length));
report("ChaCha20 xor 8K", benchmark(() => streamXOR(key, nonce, buf8192, buf8192), buf8192.length));
report("ChaCha20 xor 1111", benchmark(() => streamXOR(key, nonce, buf1111, buf1111), buf1111.length));
report("ChaCha20 xor 64", benchmark(() => streamXOR(key, nonce, buf64, buf64), buf64.length));
//# sourceMappingURL=xchacha20.bench.js.map

@@ -1,13 +0,11 @@

"use strict";
// Copyright (C) 2019 Kyle Den Hartog
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Package xchacha20 implements XChaCha20 stream cipher.
*/
var binary_1 = require("@stablelib/binary");
var wipe_1 = require("@stablelib/wipe");
var chacha_1 = require("@stablelib/chacha");
import { writeUint32LE } from "@stablelib/binary";
import { wipe } from "@stablelib/wipe";
import { streamXOR as chachaStreamXOR } from "@stablelib/chacha";
// Number of ChaCha rounds (ChaCha20).
var ROUNDS = 20;
const ROUNDS = 20;
/**

@@ -24,3 +22,3 @@ * Encrypt src with XChaCha20 stream generated for the given 32-byte key and

*/
function streamXOR(key, nonce, src, dst) {
export function streamXOR(key, nonce, src, dst) {
if (nonce.length !== 24) {

@@ -32,15 +30,14 @@ throw new Error("XChaCha20 nonce must be 24 bytes");

// stream -- "subkey".
var subkey = hchacha(key, nonce.subarray(0, 16), new Uint8Array(32));
const subkey = hchacha(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);
// If nonceInplaceCounterLength > 0, we'll still pass the correct
// nonce || counter, as we don't limit the end of nonce subarray.
var result = chacha_1.streamXOR(subkey, modifiedNonce, src, dst);
const result = chachaStreamXOR(subkey, modifiedNonce, src, dst);
// Clean subkey.
wipe_1.wipe(subkey);
wipe(subkey);
return result;
}
exports.streamXOR = streamXOR;
/**

@@ -56,7 +53,6 @@ * Generate XChaCha20 stream for the given 32-byte key and 12-byte

*/
function stream(key, nonce, dst) {
wipe_1.wipe(dst);
export function stream(key, nonce, dst) {
wipe(dst);
return streamXOR(key, nonce, dst, dst);
}
exports.stream = stream;
/**

@@ -68,36 +64,36 @@ * HChaCha is a one-way function used in XChaCha to extend nonce.

*/
function hchacha(key, src, dst) {
var j0 = 0x61707865; // "expa" -- ChaCha's "sigma" constant
var j1 = 0x3320646e; // "nd 3" for 32-byte keys
var j2 = 0x79622d32; // "2-by"
var j3 = 0x6b206574; // "te k"
var j4 = (key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0];
var j5 = (key[7] << 24) | (key[6] << 16) | (key[5] << 8) | key[4];
var j6 = (key[11] << 24) | (key[10] << 16) | (key[9] << 8) | key[8];
var j7 = (key[15] << 24) | (key[14] << 16) | (key[13] << 8) | key[12];
var j8 = (key[19] << 24) | (key[18] << 16) | (key[17] << 8) | key[16];
var j9 = (key[23] << 24) | (key[22] << 16) | (key[21] << 8) | key[20];
var j10 = (key[27] << 24) | (key[26] << 16) | (key[25] << 8) | key[24];
var j11 = (key[31] << 24) | (key[30] << 16) | (key[29] << 8) | key[28];
var j12 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0];
var j13 = (src[7] << 24) | (src[6] << 16) | (src[5] << 8) | src[4];
var j14 = (src[11] << 24) | (src[10] << 16) | (src[9] << 8) | src[8];
var j15 = (src[15] << 24) | (src[14] << 16) | (src[13] << 8) | src[12];
var x0 = j0;
var x1 = j1;
var x2 = j2;
var x3 = j3;
var x4 = j4;
var x5 = j5;
var x6 = j6;
var x7 = j7;
var x8 = j8;
var x9 = j9;
var x10 = j10;
var x11 = j11;
var x12 = j12;
var x13 = j13;
var x14 = j14;
var x15 = j15;
for (var i = 0; i < ROUNDS; i += 2) {
export function hchacha(key, src, dst) {
let j0 = 0x61707865; // "expa" -- ChaCha's "sigma" constant
let j1 = 0x3320646e; // "nd 3" for 32-byte keys
let j2 = 0x79622d32; // "2-by"
let j3 = 0x6b206574; // "te k"
let j4 = (key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0];
let j5 = (key[7] << 24) | (key[6] << 16) | (key[5] << 8) | key[4];
let j6 = (key[11] << 24) | (key[10] << 16) | (key[9] << 8) | key[8];
let j7 = (key[15] << 24) | (key[14] << 16) | (key[13] << 8) | key[12];
let j8 = (key[19] << 24) | (key[18] << 16) | (key[17] << 8) | key[16];
let j9 = (key[23] << 24) | (key[22] << 16) | (key[21] << 8) | key[20];
let j10 = (key[27] << 24) | (key[26] << 16) | (key[25] << 8) | key[24];
let j11 = (key[31] << 24) | (key[30] << 16) | (key[29] << 8) | key[28];
let j12 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0];
let j13 = (src[7] << 24) | (src[6] << 16) | (src[5] << 8) | src[4];
let j14 = (src[11] << 24) | (src[10] << 16) | (src[9] << 8) | src[8];
let j15 = (src[15] << 24) | (src[14] << 16) | (src[13] << 8) | src[12];
let x0 = j0;
let x1 = j1;
let x2 = j2;
let x3 = j3;
let x4 = j4;
let x5 = j5;
let x6 = j6;
let x7 = j7;
let x8 = j8;
let x9 = j9;
let x10 = j10;
let x11 = j11;
let x12 = j12;
let x13 = j13;
let x14 = j14;
let x15 = j15;
for (let i = 0; i < ROUNDS; i += 2) {
x0 = (x0 + x4) | 0;

@@ -200,13 +196,12 @@ x12 ^= x0;

}
binary_1.writeUint32LE(x0, dst, 0);
binary_1.writeUint32LE(x1, dst, 4);
binary_1.writeUint32LE(x2, dst, 8);
binary_1.writeUint32LE(x3, dst, 12);
binary_1.writeUint32LE(x12, dst, 16);
binary_1.writeUint32LE(x13, dst, 20);
binary_1.writeUint32LE(x14, dst, 24);
binary_1.writeUint32LE(x15, dst, 28);
writeUint32LE(x0, dst, 0);
writeUint32LE(x1, dst, 4);
writeUint32LE(x2, dst, 8);
writeUint32LE(x3, dst, 12);
writeUint32LE(x12, dst, 16);
writeUint32LE(x13, dst, 20);
writeUint32LE(x14, dst, 24);
writeUint32LE(x15, dst, 28);
return dst;
}
exports.hchacha = hchacha;
//# sourceMappingURL=xchacha20.js.map

@@ -1,27 +0,26 @@

"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("./xchacha20");
var hex_1 = require("@stablelib/hex");
import { describe, expect, it } from 'vitest';
import { hchacha, stream, streamXOR } from "./xchacha20";
import { encode, decode } from "@stablelib/hex";
// test taken from draft-arciszewski-xchacha-03 section 2.2.1
// see https://tools.ietf.org/html/draft-arciszewski-xchacha-03#section-2.2.1
describe("xchacha20.hchacha", function () {
it("should produce correct value", function () {
var key = hex_1.decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
var nonce = hex_1.decode("000000090000004A0000000031415927");
var good = "82413B4227B27BFED30E42508A877D73A0F9E4D58A74A853C12EC41326D3ECDC";
var dst = new Uint8Array(32);
var subkey = xchacha20_1.hchacha(key, nonce.subarray(0, 16), dst);
expect(hex_1.encode(subkey)).toBe(good);
describe("xchacha20.hchacha", () => {
it("should produce correct value", () => {
const key = decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
const nonce = decode("000000090000004A0000000031415927");
const good = "82413B4227B27BFED30E42508A877D73A0F9E4D58A74A853C12EC41326D3ECDC";
const dst = new Uint8Array(32);
const subkey = hchacha(key, nonce.subarray(0, 16), dst);
expect(encode(subkey)).toBe(good);
});
});
// test taken from XChaCha20 TV1 in libsodium (line 93 in libsodium/test/default/xchacha20.c)
describe("xchacha20.stream", function () {
it("should produce correct result", function () {
var key = hex_1.decode("79C99798AC67300BBB2704C95C341E3245F3DCB21761B98E52FF45B24F304FC4");
var nonce = hex_1.decode("B33FFD3096479BCFBC9AEE49417688A0A2554F8D95389419");
var good = "C6E9758160083AC604EF90E712CE6E75D7797590744E0CF060F013739C";
var dst = new Uint8Array(good.length / 2);
expect(hex_1.encode(xchacha20_1.stream(key, nonce, dst))).toBe(good);
describe("xchacha20.stream", () => {
it("should produce correct result", () => {
const key = decode("79C99798AC67300BBB2704C95C341E3245F3DCB21761B98E52FF45B24F304FC4");
const nonce = decode("B33FFD3096479BCFBC9AEE49417688A0A2554F8D95389419");
const good = "C6E9758160083AC604EF90E712CE6E75D7797590744E0CF060F013739C";
const dst = new Uint8Array(good.length / 2);
expect(encode(stream(key, nonce, dst))).toBe(good);
});

@@ -31,6 +30,6 @@ });

// see https://tools.ietf.org/html/draft-arciszewski-xchacha-03#appendix-A.3.2
describe("xchacha20.streamXOR", function () {
it("should produce correct result", function () {
var key = hex_1.decode("808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F");
var plaintext = hex_1.decode("5468652064686F6C65202870726F6E6F756E6365642022646F6C652229206973" +
describe("xchacha20.streamXOR", () => {
it("should produce correct result", () => {
const key = decode("808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F");
const plaintext = decode("5468652064686F6C65202870726F6E6F756E6365642022646F6C652229206973" +
"20616C736F206B6E6F776E2061732074686520417369617469632077696C6420" +

@@ -45,4 +44,4 @@ "646F672C2072656420646F672C20616E642077686973746C696E6720646F672E" +

"2066616D696C792043616E696461652E");
var nonce = hex_1.decode("404142434445464748494A4B4C4D4E4F5051525354555658");
var ciphertext = "4559ABBA4E48C16102E8BB2C05E6947F" +
const nonce = decode("404142434445464748494A4B4C4D4E4F5051525354555658");
const ciphertext = "4559ABBA4E48C16102E8BB2C05E6947F" +
"50A786DE162F9B0B7E592A9B53D0D4E9" +

@@ -66,6 +65,6 @@ "8D8D6410D540A1A6375B26D80DACE4FA" +

"93B93111C1A55DD7421A10184974C7C5";
var dst = new Uint8Array(ciphertext.length / 2);
expect(hex_1.encode(xchacha20_1.streamXOR(key, nonce, plaintext, dst))).toBe(ciphertext);
const dst = new Uint8Array(ciphertext.length / 2);
expect(encode(streamXOR(key, nonce, plaintext, dst))).toBe(ciphertext);
});
});
//# sourceMappingURL=xchacha20.test.js.map
{
"name": "@stablelib/xchacha20",
"version": "1.0.1",
"version": "2.0.0",
"description": "XChaCha20 stream cipher",
"main": "./lib/xchacha20.js",
"type": "module",
"typings": "./lib/xchacha20.d.ts",

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

"build": "tsc",
"test": "jasmine JASMINE_CONFIG_PATH=../../configs/jasmine.json",
"test": "vitest run",
"bench": "node ./lib/xchacha20.bench.js"
},
"dependencies": {
"@stablelib/binary": "^1.0.1",
"@stablelib/chacha": "^1.0.1",
"@stablelib/wipe": "^1.0.1"
"@stablelib/binary": "^2.0.0",
"@stablelib/chacha": "^2.0.0",
"@stablelib/wipe": "^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) 2019 Kyle Den Hartog
// MIT License. See LICENSE file for details.
import { describe, expect, it } from 'vitest';
import { hchacha, stream, streamXOR } from "./xchacha20";

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

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