@stablelib/xsalsa20
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -1,13 +0,11 @@ | ||
"use strict"; | ||
// Copyright (C) 2016 Dmitry Chestnykh | ||
// MIT License. See LICENSE file for details. | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var xsalsa20_1 = require("./xsalsa20"); | ||
var benchmark_1 = require("@stablelib/benchmark"); | ||
var buf8192 = benchmark_1.byteSeq(8192); | ||
var buf1111 = benchmark_1.byteSeq(1111); | ||
var key = benchmark_1.byteSeq(32); | ||
var nonce = benchmark_1.byteSeq(24); | ||
benchmark_1.report("XSalsa20/20 xor 8K", benchmark_1.benchmark(function () { return xsalsa20_1.streamXOR(key, nonce, buf8192, buf8192); }, buf8192.length)); | ||
benchmark_1.report("XSalsa20/20 xor 1111", benchmark_1.benchmark(function () { return xsalsa20_1.streamXOR(key, nonce, buf1111, buf1111); }, buf1111.length)); | ||
import { streamXOR } from "./xsalsa20"; | ||
import { benchmark, report, byteSeq } from "@stablelib/benchmark"; | ||
const buf8192 = byteSeq(8192); | ||
const buf1111 = byteSeq(1111); | ||
const key = byteSeq(32); | ||
const nonce = byteSeq(24); | ||
report("XSalsa20/20 xor 8K", benchmark(() => streamXOR(key, nonce, buf8192, buf8192), buf8192.length)); | ||
report("XSalsa20/20 xor 1111", benchmark(() => streamXOR(key, nonce, buf1111, buf1111), buf1111.length)); | ||
//# sourceMappingURL=xsalsa20.bench.js.map |
@@ -1,11 +0,9 @@ | ||
"use strict"; | ||
// Copyright (C) 2016 Dmitry Chestnykh | ||
// MIT License. See LICENSE file for details. | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Package xsalsa20 implements XSalsa20 stream cipher. | ||
*/ | ||
var binary_1 = require("@stablelib/binary"); | ||
var salsa20_1 = require("@stablelib/salsa20"); | ||
var wipe_1 = require("@stablelib/wipe"); | ||
import { writeUint32LE } from "@stablelib/binary"; | ||
import { streamXOR as salsaStreamXOR } from "@stablelib/salsa20"; | ||
import { wipe } from "@stablelib/wipe"; | ||
/** | ||
@@ -19,4 +17,3 @@ * Encrypt src with Salsa20/20 stream generated for the given 32-byte key | ||
*/ | ||
function streamXOR(key, nonce, src, dst, nonceInplaceCounterLength) { | ||
if (nonceInplaceCounterLength === void 0) { nonceInplaceCounterLength = 0; } | ||
export function streamXOR(key, nonce, src, dst, nonceInplaceCounterLength = 0) { | ||
if (nonceInplaceCounterLength === 0) { | ||
@@ -35,3 +32,3 @@ if (nonce.length !== 24) { | ||
// stream -- "subkey". | ||
var subkey = hsalsa(key, nonce.subarray(0, 16), new Uint8Array(32)); | ||
const subkey = hsalsa(key, nonce.subarray(0, 16), new Uint8Array(32)); | ||
// Use last 8 bytes of 24-byte extended nonce as an actual nonce, | ||
@@ -42,8 +39,7 @@ // and a subkey derived in the previous step as key to encrypt. | ||
// nonce || counter, as we don't limit the end of nonce subarray. | ||
var result = salsa20_1.streamXOR(subkey, nonce.subarray(16), src, dst, nonceInplaceCounterLength); | ||
const result = salsaStreamXOR(subkey, nonce.subarray(16), src, dst, nonceInplaceCounterLength); | ||
// Clean subkey. | ||
wipe_1.wipe(subkey); | ||
wipe(subkey); | ||
return result; | ||
} | ||
exports.streamXOR = streamXOR; | ||
/** | ||
@@ -57,10 +53,8 @@ * Generate Salsa20/20 stream for the given 32-byte key and | ||
*/ | ||
function stream(key, nonce, dst, nonceInplaceCounterLength) { | ||
if (nonceInplaceCounterLength === void 0) { nonceInplaceCounterLength = 0; } | ||
wipe_1.wipe(dst); | ||
export function stream(key, nonce, dst, nonceInplaceCounterLength = 0) { | ||
wipe(dst); | ||
return streamXOR(key, nonce, dst, dst, nonceInplaceCounterLength); | ||
} | ||
exports.stream = stream; | ||
// Number of Salsa20 rounds (Salsa20/20). | ||
var ROUNDS = 20; | ||
const ROUNDS = 20; | ||
/** | ||
@@ -71,21 +65,21 @@ * HSalsa20 is a one-way function used in XSalsa20 to extend nonce, | ||
*/ | ||
function hsalsa(key, src, dst) { | ||
var x0 = 0x61707865; // "expa" | ||
var x1 = (key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0]; | ||
var x2 = (key[7] << 24) | (key[6] << 16) | (key[5] << 8) | key[4]; | ||
var x3 = (key[11] << 24) | (key[10] << 16) | (key[9] << 8) | key[8]; | ||
var x4 = (key[15] << 24) | (key[14] << 16) | (key[13] << 8) | key[12]; | ||
var x5 = 0x3320646E; // "nd 3" | ||
var x6 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; | ||
var x7 = (src[7] << 24) | (src[6] << 16) | (src[5] << 8) | src[4]; | ||
var x8 = (src[11] << 24) | (src[10] << 16) | (src[9] << 8) | src[8]; | ||
var x9 = (src[15] << 24) | (src[14] << 16) | (src[13] << 8) | src[12]; | ||
var x10 = 0x79622D32; // "2-by" | ||
var x11 = (key[19] << 24) | (key[18] << 16) | (key[17] << 8) | key[16]; | ||
var x12 = (key[23] << 24) | (key[22] << 16) | (key[21] << 8) | key[20]; | ||
var x13 = (key[27] << 24) | (key[26] << 16) | (key[25] << 8) | key[24]; | ||
var x14 = (key[31] << 24) | (key[30] << 16) | (key[29] << 8) | key[28]; | ||
var x15 = 0x6B206574; // "te k" | ||
var u; | ||
for (var i = 0; i < ROUNDS; i += 2) { | ||
export function hsalsa(key, src, dst) { | ||
let x0 = 0x61707865; // "expa" | ||
let x1 = (key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0]; | ||
let x2 = (key[7] << 24) | (key[6] << 16) | (key[5] << 8) | key[4]; | ||
let x3 = (key[11] << 24) | (key[10] << 16) | (key[9] << 8) | key[8]; | ||
let x4 = (key[15] << 24) | (key[14] << 16) | (key[13] << 8) | key[12]; | ||
let x5 = 0x3320646E; // "nd 3" | ||
let x6 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; | ||
let x7 = (src[7] << 24) | (src[6] << 16) | (src[5] << 8) | src[4]; | ||
let x8 = (src[11] << 24) | (src[10] << 16) | (src[9] << 8) | src[8]; | ||
let x9 = (src[15] << 24) | (src[14] << 16) | (src[13] << 8) | src[12]; | ||
let x10 = 0x79622D32; // "2-by" | ||
let x11 = (key[19] << 24) | (key[18] << 16) | (key[17] << 8) | key[16]; | ||
let x12 = (key[23] << 24) | (key[22] << 16) | (key[21] << 8) | key[20]; | ||
let x13 = (key[27] << 24) | (key[26] << 16) | (key[25] << 8) | key[24]; | ||
let x14 = (key[31] << 24) | (key[30] << 16) | (key[29] << 8) | key[28]; | ||
let x15 = 0x6B206574; // "te k" | ||
let u; | ||
for (let i = 0; i < ROUNDS; i += 2) { | ||
u = x0 + x12 | 0; | ||
@@ -156,13 +150,12 @@ x4 ^= u << 7 | u >>> (32 - 7); | ||
} | ||
binary_1.writeUint32LE(x0, dst, 0); | ||
binary_1.writeUint32LE(x5, dst, 4); | ||
binary_1.writeUint32LE(x10, dst, 8); | ||
binary_1.writeUint32LE(x15, dst, 12); | ||
binary_1.writeUint32LE(x6, dst, 16); | ||
binary_1.writeUint32LE(x7, dst, 20); | ||
binary_1.writeUint32LE(x8, dst, 24); | ||
binary_1.writeUint32LE(x9, dst, 28); | ||
writeUint32LE(x0, dst, 0); | ||
writeUint32LE(x5, dst, 4); | ||
writeUint32LE(x10, dst, 8); | ||
writeUint32LE(x15, dst, 12); | ||
writeUint32LE(x6, dst, 16); | ||
writeUint32LE(x7, dst, 20); | ||
writeUint32LE(x8, dst, 24); | ||
writeUint32LE(x9, dst, 28); | ||
return dst; | ||
} | ||
exports.hsalsa = hsalsa; | ||
//# sourceMappingURL=xsalsa20.js.map |
@@ -1,26 +0,25 @@ | ||
"use strict"; | ||
// Copyright (C) 2016 Dmitry Chestnykh | ||
// MIT License. See LICENSE file for details. | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var xsalsa20_1 = require("./xsalsa20"); | ||
var hex_1 = require("@stablelib/hex"); | ||
describe("xsalsa20.hsalsa", function () { | ||
it("should produce correct value", function () { | ||
var key = hex_1.decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); | ||
var src = hex_1.decode("FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0"); | ||
var good = "C6CB53882782B5B86DF1AB2ED9B810EC8A88C0A7F29211E693F0019FE0728858"; | ||
var dst = new Uint8Array(32); | ||
expect(hex_1.encode(xsalsa20_1.hsalsa(key, src, dst))).toBe(good); | ||
import { describe, expect, it } from 'vitest'; | ||
import { stream, hsalsa } from "./xsalsa20"; | ||
import { encode, decode } from "@stablelib/hex"; | ||
describe("xsalsa20.hsalsa", () => { | ||
it("should produce correct value", () => { | ||
const key = decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); | ||
const src = decode("FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0"); | ||
const good = "C6CB53882782B5B86DF1AB2ED9B810EC8A88C0A7F29211E693F0019FE0728858"; | ||
const dst = new Uint8Array(32); | ||
expect(encode(hsalsa(key, src, dst))).toBe(good); | ||
}); | ||
}); | ||
describe("xsalsa20.stream", function () { | ||
it("should produce correct result", function () { | ||
var key = hex_1.decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); | ||
var nonce = hex_1.decode("FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0EFEEEDECEBEAE9E8"); | ||
var good = "300885CCE813D8CDBE05F89706F9D5557041E4FADC3EBC5DB89C6CA60F7" + | ||
describe("xsalsa20.stream", () => { | ||
it("should produce correct result", () => { | ||
const key = decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); | ||
const nonce = decode("FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0EFEEEDECEBEAE9E8"); | ||
const good = "300885CCE813D8CDBE05F89706F9D5557041E4FADC3EBC5DB89C6CA60F7" + | ||
"3EDE4F91FF1F9521D3E9AF058E037E7FD0601DB9CCBD7A9F5CED151426F" + | ||
"DE32FC544F4F95576E2614377049C258664845A93D5FF5DD479CFEB55C7" + | ||
"579B60D419B8A8C03DA3494993577B4597DCB658BE52AB7"; | ||
var dst = new Uint8Array(good.length / 2); | ||
expect(hex_1.encode(xsalsa20_1.stream(key, nonce, dst))).toBe(good); | ||
const dst = new Uint8Array(good.length / 2); | ||
expect(encode(stream(key, nonce, dst))).toBe(good); | ||
}); | ||
@@ -27,0 +26,0 @@ }); |
{ | ||
"name": "@stablelib/xsalsa20", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "XSalsa20 (eXtended-nonce Salsa20) stream cipher", | ||
"main": "./lib/xsalsa20.js", | ||
"type": "module", | ||
"typings": "./lib/xsalsa20.d.ts", | ||
@@ -18,15 +19,15 @@ "author": "Dmitry Chestnykh", | ||
"build": "tsc", | ||
"test": "jasmine JASMINE_CONFIG_PATH=../../configs/jasmine.json", | ||
"test": "vitest run", | ||
"bench": "node ./lib/xsalsa20.bench.js" | ||
}, | ||
"dependencies": { | ||
"@stablelib/binary": "^1.0.1", | ||
"@stablelib/salsa20": "^1.0.2", | ||
"@stablelib/wipe": "^1.0.1" | ||
"@stablelib/binary": "^2.0.0", | ||
"@stablelib/salsa20": "^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) 2016 Dmitry Chestnykh | ||
// MIT License. See LICENSE file for details. | ||
import { describe, expect, it } from 'vitest'; | ||
import { stream, hsalsa } from "./xsalsa20"; | ||
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16
Yes
29247
424
+ Added@stablelib/binary@2.0.0(transitive)
+ Added@stablelib/constant-time@2.0.0(transitive)
+ Added@stablelib/int@2.0.0(transitive)
+ Added@stablelib/salsa20@2.0.0(transitive)
+ Added@stablelib/wipe@2.0.0(transitive)
- Removed@stablelib/binary@1.0.1(transitive)
- Removed@stablelib/constant-time@1.0.1(transitive)
- Removed@stablelib/int@1.0.1(transitive)
- Removed@stablelib/salsa20@1.0.2(transitive)
- Removed@stablelib/wipe@1.0.1(transitive)
Updated@stablelib/binary@^2.0.0
Updated@stablelib/salsa20@^2.0.0
Updated@stablelib/wipe@^2.0.0