@noble/ciphers
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -66,2 +66,5 @@ "use strict"; | ||
_assert_js_1.default.bool(allow128bitKeys); | ||
const blockLen32 = blockLen / 4; | ||
if (blockLen % 4 !== 0) | ||
throw new Error('Salsa/ChaCha: blockLen should be aligned to 4 bytes'); | ||
return (key, nonce, data, output, counter = 0) => { | ||
@@ -124,10 +127,24 @@ _assert_js_1.default.bytes(key); | ||
const n32 = (0, utils_js_1.u32)(nonce); | ||
const d32 = (0, utils_js_1.u32)(data); | ||
const o32 = (0, utils_js_1.u32)(output); | ||
toClean.push(b32); | ||
for (let i = 0, ctr = counter; i < data.length; i += blockLen, ctr++) { | ||
const len = data.length; | ||
for (let pos = 0, ctr = counter; pos < len; ctr++) { | ||
core(sigma, k32, n32, b32, ctr, rounds); | ||
// TODO: case output to u32 && try to xor full blocks via u32? | ||
for (let j = i; j < i + blockLen && j < data.length; j++) | ||
output[j] = data[j] ^ block[j - i]; | ||
if (ctr >= 2 ** 32 - 1) | ||
throw new Error('Salsa/ChaCha: counter overflow'); | ||
const take = Math.min(blockLen, len - pos); | ||
// Fast path: we have at least one block in input | ||
if (take === blockLen) { | ||
const pos32 = pos / 4; | ||
if (pos % 4 !== 0) | ||
throw new Error('Salsa/ChaCha: wrong block position'); | ||
for (let j = 0; j < blockLen32; j++) | ||
o32[pos32 + j] = d32[pos32 + j] ^ b32[j]; | ||
pos += blockLen; | ||
continue; | ||
} | ||
for (let j = 0; j < take; j++) | ||
output[pos + j] = data[pos + j] ^ block[j]; | ||
pos += take; | ||
} | ||
@@ -134,0 +151,0 @@ for (let i = 0; i < toClean.length; i++) |
@@ -63,2 +63,5 @@ // Basic utils for salsa-like ciphers | ||
assert.bool(allow128bitKeys); | ||
const blockLen32 = blockLen / 4; | ||
if (blockLen % 4 !== 0) | ||
throw new Error('Salsa/ChaCha: blockLen should be aligned to 4 bytes'); | ||
return (key, nonce, data, output, counter = 0) => { | ||
@@ -121,10 +124,24 @@ assert.bytes(key); | ||
const n32 = u32(nonce); | ||
const d32 = u32(data); | ||
const o32 = u32(output); | ||
toClean.push(b32); | ||
for (let i = 0, ctr = counter; i < data.length; i += blockLen, ctr++) { | ||
const len = data.length; | ||
for (let pos = 0, ctr = counter; pos < len; ctr++) { | ||
core(sigma, k32, n32, b32, ctr, rounds); | ||
// TODO: case output to u32 && try to xor full blocks via u32? | ||
for (let j = i; j < i + blockLen && j < data.length; j++) | ||
output[j] = data[j] ^ block[j - i]; | ||
if (ctr >= 2 ** 32 - 1) | ||
throw new Error('Salsa/ChaCha: counter overflow'); | ||
const take = Math.min(blockLen, len - pos); | ||
// Fast path: we have at least one block in input | ||
if (take === blockLen) { | ||
const pos32 = pos / 4; | ||
if (pos % 4 !== 0) | ||
throw new Error('Salsa/ChaCha: wrong block position'); | ||
for (let j = 0; j < blockLen32; j++) | ||
o32[pos32 + j] = d32[pos32 + j] ^ b32[j]; | ||
pos += blockLen; | ||
continue; | ||
} | ||
for (let j = 0; j < take; j++) | ||
output[pos + j] = data[pos + j] ^ block[j]; | ||
pos += take; | ||
} | ||
@@ -131,0 +148,0 @@ for (let i = 0; i < toClean.length; i++) |
{ | ||
"name": "@noble/ciphers", | ||
"version": "0.1.2", | ||
"description": "Auditable & minimal JS implementation of AES SIV, Salsa20 and ChaCha20", | ||
"version": "0.1.3", | ||
"description": "Auditable & minimal JS implementation of Salsa20, ChaCha, Poly1305 & AES-SIV", | ||
"files": [ | ||
@@ -125,2 +125,5 @@ "esm", | ||
"poly1305", | ||
"xsalsa20poly1305", | ||
"chacha20poly1305", | ||
"xchacha20poly1305", | ||
"secretbox", | ||
@@ -127,0 +130,0 @@ "rijndael" |
# noble-ciphers | ||
Auditable & minimal JS implementation of AES SIV, Salsa20 and ChaCha20. | ||
Auditable & minimal JS implementation of Salsa20, ChaCha, Poly1305 & AES-SIV | ||
@@ -10,3 +10,3 @@ - 🔒 Auditable | ||
- 💼 AES: GCM (Galois Counter Mode), SIV (Nonce Misuse-Resistant encryption) | ||
- 💃 Salsa20, ChaCha20, XSalsa20, XChaCha20, Poly1305, ChaCha8, ChaCha12 | ||
- 💃 Salsa20, ChaCha, XSalsa20, XChaCha, Poly1305, ChaCha8, ChaCha12 | ||
- ✍️ FF1 format-preserving encryption | ||
@@ -119,3 +119,3 @@ - 🧂 Compatible with NaCl / libsodium secretbox | ||
To safely use random nonces, utilize XSalsa20 or XChaCha20: | ||
To safely use random nonces, utilize XSalsa20 or XChaCha: | ||
they increased nonce length to 192-bit, minimizing a chance of collision. | ||
@@ -370,2 +370,22 @@ AES-SIV is also fine. In situations where you can't use eXtended-nonce | ||
How does this compare to other implementations? | ||
- node.js native code is 3-10x faster than noble-ciphers | ||
- tweetnacl is 25% slower than noble-ciphers on 1KB+ inputs | ||
- noble-slow "slow, but more readable" version of noble-ciphers is 3-8x slower | ||
(check out `_slow.ts`) | ||
``` | ||
xsalsa20_poly1305 (encrypt, 1MB) | ||
├─tweetnacl x 112 ops/sec @ 8ms/op | ||
├─noble x 142 ops/sec @ 7ms/op | ||
└─noble-slow x 20 ops/sec @ 47ms/op | ||
chacha20_poly1305 (encrypt, 1MB) | ||
├─node x 1,369 ops/sec @ 729μs/op | ||
├─stablelib x 120 ops/sec @ 8ms/op | ||
├─noble x 142 ops/sec @ 7ms/op | ||
└─noble-slow x 19 ops/sec @ 51ms/op | ||
``` | ||
## Contributing & testing | ||
@@ -372,0 +392,0 @@ |
@@ -87,2 +87,4 @@ // Basic utils for salsa-like ciphers | ||
assert.bool(allow128bitKeys); | ||
const blockLen32 = blockLen / 4; | ||
if (blockLen % 4 !== 0) throw new Error('Salsa/ChaCha: blockLen should be aligned to 4 bytes'); | ||
return ( | ||
@@ -148,8 +150,20 @@ key: Uint8Array, | ||
const n32 = u32(nonce); | ||
const d32 = u32(data); | ||
const o32 = u32(output); | ||
toClean.push(b32); | ||
for (let i = 0, ctr = counter; i < data.length; i += blockLen, ctr++) { | ||
const len = data.length; | ||
for (let pos = 0, ctr = counter; pos < len; ctr++) { | ||
core(sigma, k32, n32, b32, ctr, rounds); | ||
// TODO: case output to u32 && try to xor full blocks via u32? | ||
for (let j = i; j < i + blockLen && j < data.length; j++) output[j] = data[j] ^ block[j - i]; | ||
if (ctr >= 2 ** 32 - 1) throw new Error('Salsa/ChaCha: counter overflow'); | ||
const take = Math.min(blockLen, len - pos); | ||
// Fast path: we have at least one block in input | ||
if (take === blockLen) { | ||
const pos32 = pos / 4; | ||
if (pos % 4 !== 0) throw new Error('Salsa/ChaCha: wrong block position'); | ||
for (let j = 0; j < blockLen32; j++) o32[pos32 + j] = d32[pos32 + j] ^ b32[j]; | ||
pos += blockLen; | ||
continue; | ||
} | ||
for (let j = 0; j < take; j++) output[pos + j] = data[pos + j] ^ block[j]; | ||
pos += take; | ||
} | ||
@@ -156,0 +170,0 @@ for (let i = 0; i < toClean.length; i++) toClean[i].fill(0); |
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
419048
5431
403