Skip to content

Commit

Permalink
More comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 2, 2025
1 parent 7c74b0d commit 99d03d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/chacha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export const _poly1305_aead =

/**
* ChaCha20-Poly1305 from RFC 8439.
*
* Unsafe to use random nonces under the same key, due to collision chance.
* Prefer XChaCha instead.
*/
Expand All @@ -285,8 +286,9 @@ export const chacha20poly1305: ARXCipher = /* @__PURE__ */ wrapCipher(
);
/**
* XChaCha20-Poly1305 extended-nonce chacha.
*
* Can be safely used with random nonces (CSPRNG).
* [IRTF draft](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha).
* See [IRTF draft](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha).
*/
export const xchacha20poly1305: ARXCipher = /* @__PURE__ */ wrapCipher(
{ blockSize: 64, nonceLength: 24, tagLength: 16 },
Expand Down
2 changes: 2 additions & 0 deletions src/ff1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function getRound(radix: number, key: Uint8Array, tweak: Uint8Array, x: number[]

const EMPTY_BUF = new Uint8Array([]);

/** FPE-FF1 format-preserving encryption */
export function FF1(
radix: number,
key: Uint8Array,
Expand Down Expand Up @@ -153,6 +154,7 @@ const binLE = {
},
};

/** Binary version of FPE-FF1 format-preserving encryption. */
export function BinaryFF1(key: Uint8Array, tweak: Uint8Array = EMPTY_BUF): Cipher {
const ff1 = FF1(2, key, tweak);
return {
Expand Down
2 changes: 1 addition & 1 deletion src/salsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const xsalsa20poly1305: ARXCipher = /* @__PURE__ */ wrapCipher(
);

/**
* Alias to xsalsa20poly1305, for compatibility with libsodium / nacl
* Alias to `xsalsa20poly1305`, for compatibility with libsodium / nacl
*/
export function secretbox(
key: Uint8Array,
Expand Down
13 changes: 11 additions & 2 deletions src/webcrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ type CipherWithNonce = ((key: Uint8Array, nonce: Uint8Array, ...args: any[]) =>
nonceLength: number;
};

// Uses CSPRG for nonce, nonce injected in ciphertext
/**
* Uses CSPRG for nonce, nonce injected in ciphertext.
* @example
* const gcm = managedNonce(aes.gcm);
* const ciphr = gcm(key).encrypt(data);
* const plain = gcm(key).decrypt(ciph);
*/
export function managedNonce<T extends CipherWithNonce>(fn: T): RemoveNonce<T> {
const { nonceLength } = fn;
anumber(nonceLength);
Expand Down Expand Up @@ -138,10 +144,13 @@ function generate(algo: BlockMode) {
};
}

export const cbc: (key: Uint8Array, nonce: Uint8Array) => AsyncCipher = /* @__PURE__ */ (() =>
/** AES-CBC, native webcrypto version */
export const cbc: (key: Uint8Array, iv: Uint8Array) => AsyncCipher = /* @__PURE__ */ (() =>
generate(mode.CBC))();
/** AES-CTR, native webcrypto version */
export const ctr: (key: Uint8Array, nonce: Uint8Array) => AsyncCipher = /* @__PURE__ */ (() =>
generate(mode.CTR))();
/** AES-GCM, native webcrypto version */
export const gcm: (key: Uint8Array, nonce: Uint8Array, AAD?: Uint8Array) => AsyncCipher =
/* @__PURE__ */ (() => generate(mode.GCM))();

Expand Down

0 comments on commit 99d03d2

Please sign in to comment.