@@ -111,13 +111,13 @@ class SenderContext {
111111 #key: Uint8Array
112112 #base_nonce: Uint8Array
113113 #exporter_secret: Uint8Array
114- #mode: number
114+ #mode: typeof MODE_BASE | typeof MODE_PSK
115115 #seq: number = 0
116116 #mutex?: Mutex
117117
118118 constructor (
119119 suite : Triple ,
120- mode : number ,
120+ mode : typeof MODE_BASE | typeof MODE_PSK ,
121121 key : Uint8Array ,
122122 base_nonce : Uint8Array ,
123123 exporter_secret : Uint8Array ,
@@ -129,7 +129,11 @@ class SenderContext {
129129 this . #exporter_secret = exporter_secret
130130 }
131131
132- /** @returns The mode (0x00 = Base, 0x01 = PSK) for this context. */
132+ /**
133+ * @returns The mode (0x00 = Base, 0x01 = PSK) for this context.
134+ * @see {@link MODE_BASE }
135+ * @see {@link MODE_PSK }
136+ */
133137 get mode ( ) : number {
134138 return this . #mode
135139 }
@@ -253,13 +257,13 @@ class RecipientContext {
253257 #key: Uint8Array
254258 #base_nonce: Uint8Array
255259 #exporter_secret: Uint8Array
256- #mode: number
260+ #mode: typeof MODE_BASE | typeof MODE_PSK
257261 #seq: number = 0
258262 #mutex?: Mutex
259263
260264 constructor (
261265 suite : Triple ,
262- mode : number ,
266+ mode : typeof MODE_BASE | typeof MODE_PSK ,
263267 key : Uint8Array ,
264268 base_nonce : Uint8Array ,
265269 exporter_secret : Uint8Array ,
@@ -271,7 +275,11 @@ class RecipientContext {
271275 this . #exporter_secret = exporter_secret
272276 }
273277
274- /** @returns The mode (0x00 = Base, 0x01 = PSK) for this context. */
278+ /**
279+ * @returns The mode (0x00 = Base, 0x01 = PSK) for this context.
280+ * @see {@link MODE_BASE }
281+ * @see {@link MODE_PSK }
282+ */
275283 get mode ( ) : number {
276284 return this . #mode
277285 }
@@ -600,11 +608,16 @@ export class CipherSuite {
600608 }
601609
602610 /**
603- * Deterministically derives a key pair for this CipherSuite from input keying material. By
611+ * Deterministically derives a key pair for this CipherSuite's KEM from input keying material. By
604612 * default, private keys are derived as non-extractable (their value cannot be exported).
605613 *
606- * An `ikm` input MUST NOT be reused elsewhere, particularly not with `DeriveKeyPair()` of a
607- * different KEM.
614+ * > [!CAUTION]\
615+ * > Input keying material must not be reused elsewhere, particularly not with `DeriveKeyPair()` of
616+ * > a different KEM. Re-use across different KEMs could leak information about the private key.
617+ *
618+ * > [!CAUTION]\
619+ * > Input keying material should be generated from a cryptographically secure random source or
620+ * > derived from high-entropy secret material.
608621 *
609622 * @category Key Management
610623 * @example
@@ -695,7 +708,7 @@ export class CipherSuite {
695708 * const privateKey: HPKE.Key = await suite.DeserializePrivateKey(serialized)
696709 * ```
697710 *
698- * @param privateKey - Serialized private key
711+ * @param privateKey - Serialized private key (must be exactly { @link CipherSuite.KEM Nsk} bytes)
699712 * @param extractable - Whether the deserialized private key should be extractable (e.g. by
700713 * {@link SerializePrivateKey}) (default: false)
701714 *
@@ -732,7 +745,7 @@ export class CipherSuite {
732745 * const publicKey: HPKE.Key = await suite.DeserializePublicKey(serialized)
733746 * ```
734747 *
735- * @param publicKey - Serialized public key
748+ * @param publicKey - Serialized public key (must be exactly { @link CipherSuite.KEM Npk} bytes)
736749 *
737750 * @returns A Promise that resolves to the deserialized public key.
738751 */
@@ -1239,10 +1252,21 @@ interface Triple {
12391252 readonly AEAD : Readonly < AEAD >
12401253}
12411254
1242- /** Mode identifier for Base mode */
1255+ /**
1256+ * Mode identifier for Base mode (0x00).
1257+ *
1258+ * Base mode provides encryption to a public key without sender authentication. The recipient cannot
1259+ * verify who encrypted the message, only that someone with access to their public key did.
1260+ */
12431261export const MODE_BASE = 0x00
12441262
1245- /** Mode identifier for PSK mode */
1263+ /**
1264+ * Mode identifier for PSK mode (0x01).
1265+ *
1266+ * PSK (Pre-Shared Key) mode provides encryption with authentication using a pre-shared secret. Both
1267+ * sender and recipient must possess the same PSK and PSK ID. This provides implicit sender
1268+ * authentication.
1269+ */
12461270export const MODE_PSK = 0x01
12471271
12481272/**
@@ -1346,6 +1370,9 @@ export interface KeyPair {
13461370 * This interface is designed to be compatible with Web Cryptography's CryptoKey objects while
13471371 * allowing for custom key implementations that may not have all CryptoKey properties. It includes
13481372 * only the essential properties needed for HPKE operations and validations.
1373+ *
1374+ * Keys are created through {@link CipherSuite.GenerateKeyPair}, {@link CipherSuite.DeriveKeyPair},
1375+ * {@link CipherSuite.DeserializePrivateKey}, or {@link CipherSuite.DeserializePublicKey}.
13491376 */
13501377export interface Key {
13511378 /** The key algorithm properties */
@@ -1393,7 +1420,7 @@ function slice(buffer: Uint8Array, start?: number, end?: number) {
13931420 * Encodes an ASCII string into a Uint8Array.
13941421 *
13951422 * This utility function converts ASCII strings to byte arrays. It's exported for use in custom KEM,
1396- * KDF, or AEAD implementations.
1423+ * KDF, or AEAD implementations to encode identifiers or HPKE suite_id values .
13971424 *
13981425 * @param string - ASCII string to encode
13991426 *
@@ -1404,6 +1431,9 @@ export function encode(string: string): Uint8Array {
14041431 const bytes = new Uint8Array ( string . length )
14051432 for ( let i = 0 ; i < string . length ; i ++ ) {
14061433 const code = string . charCodeAt ( i )
1434+ if ( code > 0x7f ) {
1435+ throw new TypeError ( 'Input string must contain only ASCII characters' )
1436+ }
14071437 bytes [ i ] = code
14081438 }
14091439 return bytes
@@ -1919,7 +1949,7 @@ export interface KEM {
19191949 /**
19201950 * Deserializes a public key from bytes.
19211951 *
1922- * @param key - The serialized public key already validated to be at least {@link Npk} bytes
1952+ * @param key - The serialized public key already validated to be exactly {@link Npk} bytes
19231953 *
19241954 * @returns A promise resolving to a {@link !Key} or a Key interface-conforming object
19251955 */
@@ -1937,7 +1967,7 @@ export interface KEM {
19371967 /**
19381968 * Deserializes a private key from bytes.
19391969 *
1940- * @param key - The serialized private key already validated to be at least {@link Nsk} bytes
1970+ * @param key - The serialized private key already validated to be exactly {@link Nsk} bytes
19411971 * @param extractable - Whether the private key should be extractable
19421972 *
19431973 * @returns A promise resolving to a {@link !Key} or a Key interface-conforming object
@@ -2121,7 +2151,7 @@ export interface AEAD {
21212151 * into a byte string of specified length. It's exported for use in custom KEM, KDF, or AEAD
21222152 * implementations.
21232153 *
2124- * @param n - Non-negative integer to convert
2154+ * @param n - Non-negative safe integer to convert
21252155 * @param w - Desired length of output in bytes
21262156 *
21272157 * @returns A Uint8Array of length w containing the big-endian representation of n
0 commit comments