{"name":"@noble/ciphers","version":"0.5.3","keywords":["salsa20","chacha","aes","cryptography","crypto","noble","cipher","ciphers","xsalsa20","xchacha20","poly1305","xsalsa20poly1305","chacha20poly1305","xchacha20poly1305","secretbox","rijndael","siv","ff1"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"@noble/ciphers@0.5.3","homepage":"https://paulmillr.com/noble/","bugs":{"url":"https://github.com/paulmillr/noble-ciphers/issues"},"dist":{"shasum":"48b536311587125e0d0c1535f73ec8375cd76b23","tarball":"http://registry-npm.ywwl.com/@noble/ciphers/download/@noble/ciphers-0.5.3.tgz","fileCount":103,"integrity":"sha512-B0+6IIHiqEs3BPMT0hcRmHvEj2QHOLu+uwt+tqDDeVd0oyVzh7BPrDcPjRnV1PV/5LaknXJJQvOuRGR0zQJz+w==","signatures":[{"sig":"MEYCIQDdKqsrvtxgJDCUKKFllz3Tk964RpypRbU/OTQe2HWPrAIhALF0sak214Hk/dJK+D+jx5NjwuNqe0oQPxP1Cxu7ImHS","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@noble%2fciphers@0.5.3","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":624276,"size":94941,"noattachment":false,"key":"/@noble/ciphers/-/@noble/ciphers-0.5.3.tgz"},"main":"index.js","types":"./index.d.ts","browser":{"./crypto":"./crypto.js","node:crypto":false},"exports":{".":{"types":"./index.d.ts","import":"./esm/index.js","default":"./index.js"},"./aes":{"types":"./aes.d.ts","import":"./esm/aes.js","default":"./aes.js"},"./ff1":{"types":"./ff1.d.ts","import":"./esm/ff1.js","default":"./ff1.js"},"./index":{"types":"./index.d.ts","import":"./esm/index.js","default":"./index.js"},"./salsa":{"types":"./salsa.d.ts","import":"./esm/salsa.js","default":"./salsa.js"},"./utils":{"types":"./utils.d.ts","import":"./esm/utils.js","default":"./utils.js"},"./_micro":{"types":"./_micro.d.ts","import":"./esm/_micro.js","default":"./_micro.js"},"./chacha":{"types":"./chacha.d.ts","import":"./esm/chacha.js","default":"./chacha.js"},"./crypto":{"node":{"import":"./esm/cryptoNode.js","default":"./cryptoNode.js"},"types":"./crypto.d.ts","import":"./esm/crypto.js","default":"./crypto.js"},"./_polyval":{"types":"./_polyval.d.ts","import":"./esm/_polyval.js","default":"./_polyval.js"},"./_poly1305":{"types":"./_poly1305.d.ts","import":"./esm/_poly1305.js","default":"./_poly1305.js"},"./webcrypto":{"types":"./webcrypto.d.ts","import":"./esm/webcrypto.js","default":"./webcrypto.js"}},"funding":"https://paulmillr.com/funding/","gitHead":"df9fa72cbbd553c7f86082ea3b7c4c493a83bcd7","scripts":{"lint":"prettier --check 'src/**/*.{js,ts}' 'test/**/*.{js,ts,mjs}'","test":"node test/index.js","bench":"node benchmark/aead.js noble && node benchmark/ciphers.js noble","build":"npm run build:clean; tsc && tsc -p tsconfig.esm.json","format":"prettier --write 'src/**/*.{js,ts}' 'test/**/*.{js,ts,mjs}'","bench:all":"node benchmark/{aead,ciphers,poly}.js","build:clean":"rm *.{js,d.ts,js.map,d.ts.map} esm/*.{js,d.ts,js.map,d.ts.map} 2> /dev/null","bench:install":"cd benchmark && npm install && cd ../../","build:release":"cd build && npm i && npm run build"},"_npmUser":{"name":"paulmillr","email":"paul@paulmillr.com"},"repository":{"url":"git+https://github.com/paulmillr/noble-ciphers.git","type":"git"},"_npmVersion":"10.7.0","description":"Auditable & minimal JS implementation of Salsa20, ChaCha and AES","directories":{},"sideEffects":false,"_nodeVersion":"20.12.2","_hasShrinkwrap":false,"devDependencies":{"prettier":"3.1.1","fast-check":"3.0.0","typescript":"5.3.2","@scure/base":"1.1.3","micro-bmark":"0.3.1","micro-should":"0.4.0","@paulmillr/jsbt":"0.1.0"},"_npmOperationalInternal":{"tmp":"tmp/ciphers_0.5.3_1715003011882_0.9925035315540254","host":"s3://npm-registry-packages"},"readme":"# noble-ciphers\n\nAuditable & minimal JS implementation of Salsa20, ChaCha and AES.\n\n- 🔒 Auditable\n- 🔻 Tree-shakeable: unused code is excluded from your builds\n- 🏎 Fast: hand-optimized for caveats of JS engines\n- 🔍 Reliable: property-based / cross-library / wycheproof tests ensure correctness\n- 💼 AES: ECB, CBC, CTR, CFB, GCM, SIV (nonce misuse-resistant), AESKW, AESKWP\n- 💃 Salsa20, ChaCha, XSalsa20, XChaCha, ChaCha8, ChaCha12, Poly1305\n- 🥈 Two AES implementations: pure JS or friendly wrapper around webcrypto\n- 🪶 53KB (9KB gzipped) for everything, 7KB (3KB gzipped) for ChaCha build\n\nTake a glance at [GitHub Discussions](https://github.com/paulmillr/noble-ciphers/discussions) for questions and support.\n\n### This library belongs to _noble_ cryptography\n\n> **noble cryptography** — high-security, easily auditable set of contained cryptographic libraries and tools.\n\n- Zero or minimal dependencies\n- Highly readable TypeScript / JS code\n- PGP-signed releases and transparent NPM builds\n- All libraries:\n  [ciphers](https://github.com/paulmillr/noble-ciphers),\n  [curves](https://github.com/paulmillr/noble-curves),\n  [hashes](https://github.com/paulmillr/noble-hashes),\n  [post-quantum](https://github.com/paulmillr/noble-post-quantum),\n  4kb [secp256k1](https://github.com/paulmillr/noble-secp256k1) /\n  [ed25519](https://github.com/paulmillr/noble-ed25519)\n- [Check out homepage](https://paulmillr.com/noble/)\n  for reading resources, documentation and apps built with noble\n\n## Usage\n\n> npm install @noble/ciphers\n\nWe support all major platforms and runtimes.\nFor [Deno](https://deno.land), ensure to use\n[npm specifier](https://deno.land/manual@v1.28.0/node/npm_specifiers).\nFor React Native, you may need a\n[polyfill for getRandomValues](https://github.com/LinusU/react-native-get-random-values).\nA standalone file\n[noble-ciphers.js](https://github.com/paulmillr/noble-ciphers/releases) is also available.\n\n```js\n// import * from '@noble/ciphers'; // Error: use sub-imports, to ensure small app size\nimport { xchacha20poly1305 } from '@noble/ciphers/chacha';\n// import { xchacha20poly1305 } from 'npm:@noble/ciphers@1.0.0/chacha'; // Deno\n```\n\n- [Examples](#examples)\n  - [Encrypt with XChaCha20-Poly1305](#encrypt-with-xchacha20-poly1305)\n  - [Encrypt with AES-256-GCM](#encrypt-with-aes-256-gcm)\n  - [AES: gcm, siv, ctr, cfb, cbc, ecb](#aes-gcm-siv-ctr-cfb-cbc-ecb)\n  - [Friendly WebCrypto AES](#friendly-webcrypto-aes)\n  - [AESKW and AESKWP](#aeskw-and-aeskwp)\n  - [Auto-handle nonces](#auto-handle-nonces)\n  - [Reuse array for input and output](#reuse-array-for-input-and-output)\n  - [All imports](#all-imports)\n- [Internals](#internals)\n  - [Implemented primitives](#implemented-primitives)\n  - [Which cipher should I pick?](#which-cipher-should-i-pick)\n  - [How to encrypt properly](#how-to-encrypt-properly)\n  - [Nonces](#nonces)\n  - [Encryption limits](#encryption-limits)\n  - [AES internals and block modes](#aes-internals-and-block-modes)\n- [Security](#security)\n- [Speed](#speed)\n- [Upgrading](#upgrading)\n- [Contributing & testing](#contributing--testing)\n- [Resources](#resources)\n\n## Examples\n\n#### Encrypt with XChaCha20-Poly1305\n\n```js\nimport { xchacha20poly1305 } from '@noble/ciphers/chacha';\nimport { utf8ToBytes } from '@noble/ciphers/utils';\nimport { randomBytes } from '@noble/ciphers/webcrypto';\nconst key = randomBytes(32);    // random key\n// const key = new Uint8Array([ // existing key\n//   169, 88, 160, 139, 168, 29, 147, 196, 14, 88, 237, 76, 243, 177, 109, 140,\n//   195, 140, 80, 10, 216, 134, 215, 71, 191, 48, 20, 104, 189, 37, 38, 55,\n// ]);\n// import { hexToBytes } from '@noble/ciphers/utils'; // hex key\n// const key = hexToBytes('4b7f89bac90a1086fef73f5da2cbe93b2fae9dfbf7678ae1f3e75fd118ddf999');\nconst nonce = randomBytes(24);\nconst chacha = xchacha20poly1305(key, nonce);\nconst data = utf8ToBytes('hello, noble');\nconst ciphertext = chacha.encrypt(data);\nconst data_ = chacha.decrypt(ciphertext); // utils.bytesToUtf8(data_) === data\n```\n\n#### Encrypt with AES-256-GCM\n\n```js\nimport { gcm } from '@noble/ciphers/aes';\nimport { utf8ToBytes } from '@noble/ciphers/utils';\nimport { randomBytes } from '@noble/ciphers/webcrypto';\nconst key = randomBytes(32);\nconst nonce = randomBytes(24);\nconst aes = gcm(key, nonce);\nconst data = utf8ToBytes('hello, noble');\nconst ciphertext = aes.encrypt(data);\nconst data_ = aes.decrypt(ciphertext); // utils.bytesToUtf8(data_) === data\n```\n\n#### AES: gcm, siv, ctr, cfb, cbc, ecb\n\n```js\nimport { gcm, siv, ctr, cfb, cbc, ecb } from '@noble/ciphers/aes';\nimport { randomBytes } from '@noble/ciphers/webcrypto';\nconst plaintext = new Uint8Array(32).fill(16);\nconst key = randomBytes(32); // 24 for AES-192, 16 for AES-128\nfor (let cipher of [gcm, siv]) {\n  const stream = cipher(key, randomBytes(12));\n  const ciphertext_ = stream.encrypt(plaintext);\n  const plaintext_ = stream.decrypt(ciphertext_);\n}\nfor (const cipher of [ctr, cbc, cbc]) {\n  const stream = cipher(key, randomBytes(16));\n  const ciphertext_ = stream.encrypt(plaintext);\n  const plaintext_ = stream.decrypt(ciphertext_);\n}\nfor (const cipher of [ecb]) {\n  const stream = cipher(key);\n  const ciphertext_ = stream.encrypt(plaintext);\n  const plaintext_ = stream.decrypt(ciphertext_);\n}\n```\n\n#### Friendly WebCrypto AES\n\nNoble implements AES. Sometimes people want to use built-in `crypto.subtle` instead. However, it has terrible API. We simplify access to built-ins.\n\n> [!NOTE]  \n> Webcrypto methods are always async.\n\n```js\nimport { gcm, ctr, cbc, randomBytes } from '@noble/ciphers/webcrypto';\nconst plaintext = new Uint8Array(32).fill(16);\nconst key = randomBytes(32);\nfor (const cipher of [gcm]) {\n  const stream = cipher(key, randomBytes(12));\n  const ciphertext_ = await stream.encrypt(plaintext);\n  const plaintext_ = await stream.decrypt(ciphertext_);\n}\nfor (const cipher of [ctr, cbc]) {\n  const stream = cipher(key, randomBytes(16));\n  const ciphertext_ = await stream.encrypt(plaintext);\n  const plaintext_ = await stream.decrypt(ciphertext_);\n}\n```\n\n#### AESKW and AESKWP\n\n```ts\nimport { aeskw, aeskwp } from '@noble/ciphers/aes';\nimport { hexToBytes } from '@noble/ciphers/utils';\n\nconst kek = hexToBytes('000102030405060708090A0B0C0D0E0F');\nconst keyData = hexToBytes('00112233445566778899AABBCCDDEEFF');\nconst ciphertext =  aeskw(kek).encrypt(keyData);\n```\n\n#### Auto-handle nonces\n\nWe provide API that manages nonce internally instead of exposing them to library's user.\n\nFor `encrypt`, a `nonceBytes`-length buffer is fetched from CSPRNG and prenended to encrypted ciphertext.\n\nFor `decrypt`, first `nonceBytes` of ciphertext are treated as nonce.\n\n```js\nimport { xchacha20poly1305 } from '@noble/ciphers/chacha';\nimport { managedNonce } from '@noble/ciphers/webcrypto';\nimport { hexToBytes, utf8ToBytes } from '@noble/ciphers/utils';\nconst key = hexToBytes('fa686bfdffd3758f6377abbc23bf3d9bdc1a0dda4a6e7f8dbdd579fa1ff6d7e1');\nconst chacha = managedNonce(xchacha20poly1305)(key); // manages nonces for you\nconst data = utf8ToBytes('hello, noble');\nconst ciphertext = chacha.encrypt(data);\nconst data_ = chacha.decrypt(ciphertext);\n```\n\n#### Reuse array for input and output\n\nTo avoid additional allocations, Uint8Array can be reused\nbetween encryption and decryption calls.\n\n> [!NOTE]  \n> Some ciphers don't support unaligned (`byteOffset % 4 !== 0`) Uint8Array as\n> destination. It can decrease performance, making the optimization pointless.\n\n```js\nimport { chacha20poly1305 } from '@noble/ciphers/chacha';\nimport { utf8ToBytes } from '@noble/ciphers/utils';\nimport { randomBytes } from '@noble/ciphers/webcrypto';\n\nconst key = randomBytes(32);\nconst nonce = randomBytes(12);\nconst inputLength = 12;\nconst tagLength = 16;\n\nconst buf = new Uint8Array(inputLength + tagLength);\nconst _data = utf8ToBytes('hello, noble'); // length == 12\nbuf.set(_data, 0); // first inputLength bytes\nconst _start = buf.subarray(0, inputLength);\n\nconst chacha = chacha20poly1305(key, nonce);\nchacha.encrypt(_start, buf);\nchacha.decrypt(buf, _start); // _start now same as _data\n```\n\n#### All imports\n\n```js\nimport { gcm, siv } from '@noble/ciphers/aes';\nimport { xsalsa20poly1305 } from '@noble/ciphers/salsa';\nimport { secretbox } from '@noble/ciphers/salsa'; // == xsalsa20poly1305\nimport { chacha20poly1305, xchacha20poly1305 } from '@noble/ciphers/chacha';\n\n// Unauthenticated encryption: make sure to use HMAC or similar\nimport { ctr, cfb, cbc, ecb } from '@noble/ciphers/aes';\nimport { salsa20, xsalsa20 } from '@noble/ciphers/salsa';\nimport { chacha20, xchacha20, chacha8, chacha12 } from '@noble/ciphers/chacha';\n\n// KW\nimport { aeskw, aeskwp } from '@noble/ciphers/aes';\n\n// Utilities\nimport { bytesToHex, hexToBytes, bytesToUtf8, utf8ToBytes } from '@noble/ciphers/utils';\nimport { managedNonce, randomBytes } from '@noble/ciphers/webcrypto';\nimport { poly1305 } from '@noble/ciphers/_poly1305';\nimport { ghash, polyval } from '@noble/ciphers/_polyval';\n```\n\n## Internals\n\n### Implemented primitives\n\n- [Salsa20](https://cr.yp.to/snuffle.html) stream cipher was released in 2005.\nSalsa's goal was to implement AES replacement that does not rely on S-Boxes,\nwhich are hard to implement in a constant-time manner.\nSalsa20 is usually faster than AES, a big deal on slow, budget mobile phones.\n    - [XSalsa20](https://cr.yp.to/snuffle/xsalsa-20110204.pdf), extended-nonce\nvariant was released in 2008. It switched nonces from 96-bit to 192-bit,\nand became safe to be picked at random.\n    - Nacl / Libsodium popularized term \"secretbox\", a simple black-box\nauthenticated encryption. Secretbox is just xsalsa20-poly1305. We provide the\nalias and corresponding seal / open methods. We don't provide \"box\" or \"sealedbox\".\n    - Check out [PDF](https://cr.yp.to/snuffle/salsafamily-20071225.pdf) and\n[wiki](https://en.wikipedia.org/wiki/Salsa20).\n- [ChaCha20](https://cr.yp.to/chacha.html) stream cipher was released\n  in 2008. ChaCha aims to increase the diffusion per round, but had slightly less\n  cryptanalysis. It was standardized in\n  [RFC 8439](https://datatracker.ietf.org/doc/html/rfc8439) and is now used in TLS 1.3.\n    - [XChaCha20](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha)\n      extended-nonce variant is also provided. Similar to XSalsa, it's safe to use with\n      randomly-generated nonces.\n    - Check out [PDF](http://cr.yp.to/chacha/chacha-20080128.pdf) and [wiki](https://en.wikipedia.org/wiki/Salsa20).\n- [AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard)\nis a variant of Rijndael block cipher, standardized by NIST in 2001.\nWe provide the fastest available pure JS implementation.\n    - We support AES-128, AES-192 and AES-256: the mode is selected dynamically,\n      based on key length (16, 24, 32).\n    - [AES-GCM-SIV](https://en.wikipedia.org/wiki/AES-GCM-SIV)\n    nonce-misuse-resistant mode is also provided. It's recommended to use it,\n    to prevent catastrophic consequences of nonce reuse. Our implementation of SIV\n    has the same speed as GCM: there is no performance hit.\n    - We also have AESKW and AESKWP from\n      [RFC 3394](https://datatracker.ietf.org/doc/html/rfc3394) / [RFC 5649](https://datatracker.ietf.org/doc/html/rfc5649)\n    - Check out [AES internals and block modes](#aes-internals-and-block-modes).\n- We expose polynomial-evaluation MACs: [Poly1305](https://cr.yp.to/mac.html), AES-GCM's [GHash](https://en.wikipedia.org/wiki/Galois/Counter_Mode) and\nAES-SIV's [Polyval](https://en.wikipedia.org/wiki/AES-GCM-SIV).\n    - Poly1305 ([PDF](https://cr.yp.to/mac/poly1305-20050329.pdf),\n    [wiki](https://en.wikipedia.org/wiki/Poly1305))\n    is a fast and parallel secret-key message-authentication code suitable for\n    a wide variety of applications. It was standardized in\n    [RFC 8439](https://datatracker.ietf.org/doc/html/rfc8439) and is now used in TLS 1.3.\n    - Polynomial MACs are not perfect for every situation:\n    they lack Random Key Robustness: the MAC can be forged, and can't\n    be used in PAKE schemes. See\n    [invisible salamanders attack](https://keymaterial.net/2020/09/07/invisible-salamanders-in-aes-gcm-siv/).\n    To combat invisible salamanders, `hash(key)` can be included in ciphertext,\n    however, this would violate ciphertext indistinguishability:\n    an attacker would know which key was used - so `HKDF(key, i)`\n    could be used instead.\n- Format-preserving encryption algorithm (FPE-FF1) specified in NIST Special Publication 800-38G.\n[See more info](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38G.pdf).\n\n### Which cipher should I pick?\n\nWe suggest to use XChaCha20-Poly1305.\nIf you can't use it, prefer AES-GCM-SIV, or AES-GCM.\n\n### How to encrypt properly\n\n- Use unpredictable key with enough entropy\n  - Random key must be using cryptographically secure random number generator (CSPRNG), not `Math.random` etc.\n  - Non-random key generated from KDF is fine\n  - Re-using key is fine, but be aware of rules for cryptographic key wear-out and [encryption limits](#encryption-limits)\n- Use new nonce every time and [don't repeat it](#nonces)\n  - chacha and salsa20 are fine for sequential counters that _never_ repeat: `01, 02...`\n  - xchacha and xsalsa20 should be used for random nonces instead\n  - AES-GCM should use 12-byte nonces: smaller nonces are security risk\n- Prefer authenticated encryption (AEAD)\n  - chacha20poly1305 / aes-gcm / ChaCha + HMAC / AES + HMAC is good\n  - chacha20 / aes-ctr / aes-cbc without poly1305 or hmac is bad\n  - Flipping bits or ciphertext substitution won't be detected in unauthenticated ciphers\n- Don't re-use keys between different protocols\n  - For example, using secp256k1 key in AES can be bad\n  - Use hkdf or, at least, a hash function to create sub-key instead\n\n### Nonces\n\nMost ciphers need a key and a nonce (aka initialization vector / IV) to encrypt a data:\n\n    ciphertext = encrypt(plaintext, key, nonce)\n\nRepeating (key, nonce) pair with different plaintexts would allow an attacker to decrypt it:\n\n    ciphertext_a = encrypt(plaintext_a, key, nonce)\n    ciphertext_b = encrypt(plaintext_b, key, nonce)\n    stream_diff = xor(ciphertext_a, ciphertext_b)   # Break encryption\n\nSo, you can't repeat nonces. One way of doing so is using counters:\n\n    for i in 0..:\n        ciphertext[i] = encrypt(plaintexts[i], key, i)\n\nAnother is generating random nonce every time:\n\n    for i in 0..:\n        rand_nonces[i] = random()\n        ciphertext[i] = encrypt(plaintexts[i], key, rand_nonces[i])\n\nCounters are OK, but it's not always possible to store current counter value:\ne.g. in decentralized, unsyncable systems.\n\nRandomness is OK, but there's a catch:\nChaCha20 and AES-GCM use 96-bit / 12-byte nonces, which implies higher chance of collision.\nIn the example above, `random()` can collide and produce repeating nonce.\nChance is even higher for 64-bit nonces, which GCM allows - don't use them.\n\nTo safely use random nonces, utilize XSalsa20 or XChaCha:\nthey increased nonce length to 192-bit, minimizing a chance of collision.\nAES-SIV is also fine. In situations where you can't use eXtended-nonce\nalgorithms, key rotation is advised. hkdf would work great for this case.\n\n### Encryption limits\n\nA \"protected message\" would mean a probability of `2**-50` that a passive attacker\nsuccessfully distinguishes the ciphertext outputs of the AEAD scheme from the outputs\nof a random function. See [draft-irtf-cfrg-aead-limits](https://datatracker.ietf.org/doc/draft-irtf-cfrg-aead-limits/) for details.\n\n- Max message size:\n  - AES-GCM: ~68GB, `2**36-256`\n  - Salsa, ChaCha, XSalsa, XChaCha: ~256GB, `2**38-64`\n- Max amount of protected messages, under same key:\n  - AES-GCM: `2**32.5`\n  - Salsa, ChaCha: `2**46`, but only integrity is affected, not confidentiality\n  - XSalsa, XChaCha: `2**72`\n- Max amount of protected messages, across all keys:\n  - AES-GCM: `2**69/B` where B is max blocks encrypted by a key. Meaning\n    `2**59` for 1KB, `2**49` for 1MB, `2**39` for 1GB\n  - Salsa, ChaCha, XSalsa, XChaCha: `2**100`\n\n##### AES internals and block modes\n\n`cipher = encrypt(block, key)`. Data is split into 128-bit blocks. Encrypted in 10/12/14 rounds (128/192/256bit). Every round does:\n\n1. **S-box**, table substitution\n2. **Shift rows**, cyclic shift left of all rows of data array\n3. **Mix columns**, multiplying every column by fixed polynomial\n4. **Add round key**, round_key xor i-th column of array\n\nFor non-deterministic (not ECB) schemes, initialization vector (IV) is mixed to block/key;\nand each new round either depends on previous block's key, or on some counter.\n\n- ECB — simple deterministic replacement. Dangerous: always map x to y. See [AES Penguin](https://words.filippo.io/the-ecb-penguin/)\n- CBC — key is previous round’s block. Hard to use: need proper padding, also needs MAC\n- CTR — counter, allows to create streaming cipher. Requires good IV. Parallelizable. OK, but no MAC\n- GCM — modern CTR, parallel, with MAC\n- SIV — synthetic initialization vector, nonce-misuse-resistant. Guarantees that, when a nonce is repeated,\n  the only security loss is that identical plaintexts will produce identical ciphertexts.\n- XTS — used in hard drives. Similar to ECB (deterministic), but has `[i][j]`\n  tweak arguments corresponding to sector i and 16-byte block (part of sector) j. Not authenticated!\n\nGCM / SIV are not ideal:\n\n- Conservative key wear-out is `2**32` (4B) msgs\n- MAC can be forged: see Poly1305 section above. Same for SIV\n\n## Security\n\nThe library has not been independently audited yet.\n\nIt is tested against property-based, cross-library and Wycheproof vectors,\nand has fuzzing by [Guido Vranken's cryptofuzz](https://github.com/guidovranken/cryptofuzz).\n\nIf you see anything unusual: investigate and report.\n\n### Constant-timeness\n\n_JIT-compiler_ and _Garbage Collector_ make \"constant time\" extremely hard to\nachieve [timing attack](https://en.wikipedia.org/wiki/Timing_attack) resistance\nin a scripting language. Which means _any other JS library can't have\nconstant-timeness_. Even statically typed Rust, a language without GC,\n[makes it harder to achieve constant-time](https://www.chosenplaintext.ca/open-source/rust-timing-shield/security)\nfor some cases. If your goal is absolute security, don't use any JS lib — including bindings to native ones.\nUse low-level libraries & languages. Nonetheless we're targetting algorithmic constant time.\n\nThe library uses T-tables for AES, which\n[leak access timings](https://cr.yp.to/antiforgery/cachetiming-20050414.pdf).\nThis is also done in [OpenSSL](https://github.com/openssl/openssl/blob/2f33265039cdbd0e4589c80970e02e208f3f94d2/crypto/aes/aes_core.c#L706) and\n[Go stdlib](https://cs.opensource.google/go/go/+/refs/tags/go1.22.6:src/crypto/aes/const.go;l=90) for performance reasons.\n\n### Supply chain security\n\n- **Commits** are signed with PGP keys, to prevent forgery. Make sure to verify commit signatures.\n- **Releases** are transparent and built on GitHub CI. Make sure to verify [provenance](https://docs.npmjs.com/generating-provenance-statements) logs\n- **Rare releasing** is followed to ensure less re-audit need for end-users\n- **Dependencies** are minimized and locked-down:\n  - If your app has 500 dependencies, any dep could get hacked and you'll be downloading\n    malware with every install. We make sure to use as few dependencies as possible\n  - We prevent automatic dependency updates by locking-down version ranges. Every update is checked with `npm-diff`\n- **Dev Dependencies** are only used if you want to contribute to the repo. They are disabled for end-users:\n  - scure-base, micro-bmark and micro-should are developed by the same author and follow identical security practices\n  - prettier (linter), fast-check (property-based testing) and typescript are used for code quality, vector generation and ts compilation. The packages are big, which makes it hard to audit their source code thoroughly and fully\n\n### Randomness\n\nWe're deferring to built-in\n[crypto.getRandomValues](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)\nwhich is considered cryptographically secure (CSPRNG).\n\nIn the past, browsers had bugs that made it weak: it may happen again.\nImplementing a userspace CSPRNG to get resilient to the weakness\nis even worse: there is no reliable userspace source of quality entropy.\n\n## Speed\n\nTo summarize, noble is the fastest JS implementation of Salsa, ChaCha and AES.\n\nYou can gain additional speed-up and\navoid memory allocations by passing `output`\nuint8array into encrypt / decrypt methods.\n\nBenchmark results on Apple M2 with node v22:\n\n```\nencrypt (64B)\n├─xsalsa20poly1305 x 485,908 ops/sec @ 2μs/op\n├─chacha20poly1305 x 414,250 ops/sec @ 2μs/op\n├─xchacha20poly1305 x 331,674 ops/sec @ 3μs/op\n├─aes-256-gcm x 144,237 ops/sec @ 6μs/op\n└─aes-256-gcm-siv x 121,373 ops/sec @ 8μs/op\nencrypt (1KB)\n├─xsalsa20poly1305 x 136,574 ops/sec @ 7μs/op\n├─chacha20poly1305 x 136,017 ops/sec @ 7μs/op\n├─xchacha20poly1305 x 126,008 ops/sec @ 7μs/op\n├─aes-256-gcm x 40,149 ops/sec @ 24μs/op\n└─aes-256-gcm-siv x 37,420 ops/sec @ 26μs/op\nencrypt (8KB)\n├─xsalsa20poly1305 x 22,517 ops/sec @ 44μs/op\n├─chacha20poly1305 x 23,187 ops/sec @ 43μs/op\n├─xchacha20poly1305 x 22,837 ops/sec @ 43μs/op\n├─aes-256-gcm x 7,993 ops/sec @ 125μs/op\n└─aes-256-gcm-siv x 7,836 ops/sec @ 127μs/op\nencrypt (1MB)\n├─xsalsa20poly1305 x 186 ops/sec @ 5ms/op\n├─chacha20poly1305 x 191 ops/sec @ 5ms/op\n├─xchacha20poly1305 x 191 ops/sec @ 5ms/op\n├─aes-256-gcm x 71 ops/sec @ 14ms/op\n└─aes-256-gcm-siv x 75 ops/sec @ 13ms/op\n```\n\nUnauthenticated encryption:\n\n```\nencrypt (64B)\n├─salsa x 1,221,001 ops/sec @ 819ns/op\n├─chacha x 1,373,626 ops/sec @ 728ns/op\n├─xsalsa x 1,019,367 ops/sec @ 981ns/op\n└─xchacha x 1,019,367 ops/sec @ 981ns/op\nencrypt (1KB)\n├─salsa x 349,162 ops/sec @ 2μs/op\n├─chacha x 372,717 ops/sec @ 2μs/op\n├─xsalsa x 327,868 ops/sec @ 3μs/op\n└─xchacha x 332,446 ops/sec @ 3μs/op\nencrypt (8KB)\n├─salsa x 55,178 ops/sec @ 18μs/op\n├─chacha x 51,535 ops/sec @ 19μs/op\n├─xsalsa x 54,274 ops/sec @ 18μs/op\n└─xchacha x 55,645 ops/sec @ 17μs/op\nencrypt (1MB)\n├─salsa x 451 ops/sec @ 2ms/op\n├─chacha x 464 ops/sec @ 2ms/op\n├─xsalsa x 455 ops/sec @ 2ms/op\n└─xchacha x 462 ops/sec @ 2ms/op\n\nAES\nencrypt (64B)\n├─ctr-256 x 679,347 ops/sec @ 1μs/op\n├─cbc-256 x 699,300 ops/sec @ 1μs/op\n└─ecb-256 x 717,875 ops/sec @ 1μs/op\nencrypt (1KB)\n├─ctr-256 x 93,423 ops/sec @ 10μs/op\n├─cbc-256 x 95,721 ops/sec @ 10μs/op\n└─ecb-256 x 154,726 ops/sec @ 6μs/op\nencrypt (8KB)\n├─ctr-256 x 12,908 ops/sec @ 77μs/op\n├─cbc-256 x 13,411 ops/sec @ 74μs/op\n└─ecb-256 x 22,681 ops/sec @ 44μs/op\nencrypt (1MB)\n├─ctr-256 x 105 ops/sec @ 9ms/op\n├─cbc-256 x 108 ops/sec @ 9ms/op\n└─ecb-256 x 181 ops/sec @ 5ms/op\n```\n\nCompare to other implementations:\n\n```\nxsalsa20poly1305 (encrypt, 1MB)\n├─tweetnacl x 108 ops/sec @ 9ms/op\n└─noble x 190 ops/sec @ 5ms/op\n\nchacha20poly1305 (encrypt, 1MB)\n├─node x 1,360 ops/sec @ 735μs/op\n├─stablelib x 117 ops/sec @ 8ms/op\n└─noble x 193 ops/sec @ 5ms/op\n\nchacha (encrypt, 1MB)\n├─node x 2,035 ops/sec @ 491μs/op\n├─stablelib x 206 ops/sec @ 4ms/op\n└─noble x 474 ops/sec @ 2ms/op\n\nctr-256 (encrypt, 1MB)\n├─node x 3,530 ops/sec @ 283μs/op\n├─stablelib x 70 ops/sec @ 14ms/op\n├─aesjs x 31 ops/sec @ 32ms/op\n├─noble-webcrypto x 4,589 ops/sec @ 217μs/op\n└─noble x 107 ops/sec @ 9ms/op\n\ncbc-256 (encrypt, 1MB)\n├─node x 993 ops/sec @ 1ms/op\n├─stablelib x 63 ops/sec @ 15ms/op\n├─aesjs x 29 ops/sec @ 34ms/op\n├─noble-webcrypto x 1,087 ops/sec @ 919μs/op\n└─noble x 110 ops/sec @ 9ms/op\n\ngcm-256 (encrypt, 1MB)\n├─node x 3,196 ops/sec @ 312μs/op\n├─stablelib x 27 ops/sec @ 36ms/op\n├─noble-webcrypto x 4,059 ops/sec @ 246μs/op\n└─noble x 74 ops/sec @ 13ms/op\n```\n\n## Contributing & testing\n\n1. Clone the repository\n2. `npm install` to install build dependencies like TypeScript\n3. `npm run build` to compile TypeScript code\n4. `npm run test` will execute all main tests\n\n## Resources\n\nCheck out [paulmillr.com/noble](https://paulmillr.com/noble/)\nfor useful resources, articles, documentation and demos\nrelated to the library.\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2023 Paul Miller [(https://paulmillr.com)](https://paulmillr.com)\nCopyright (c) 2016 Thomas Pornin <pornin@bolet.org>\n\nSee LICENSE file.\n","publish_time":1715003012048,"maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"_cnpm_publish_time":1715003012048,"dist-tags":{"latest":"2.4.0"}}