@darkbio/crypto 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,10 +60,17 @@ await cose.verifyDetached(signature, cbor.text.value("payload"), signer.publicKe
60
60
  // Sign and encrypt a payload to the recipient, then open and verify it back.
61
61
  // The second argument is authenticated but must be supplied separately.
62
62
  const metadata = cbor.text.value("metadata");
63
- const sealed = await cose.seal(cbor.text.value("payload"), metadata, signer, recipient.publicKey(), domain);
63
+ const padding = cose.Padding.buckets({ floor: 8192, step: 20 });
64
+ const sealed = await cose.seal(cbor.text.value("payload"), metadata, signer, recipient.publicKey(), domain, padding);
64
65
  const opened = await cose.open(cbor.text.bytes(sealed), metadata, recipient, signer.publicKey(), domain, 60);
65
66
  ```
66
67
 
68
+ `cose.seal` and `cose.encrypt` take a `cose.Padding` policy after the domain. `cose.Padding.none()` adds no padding. Buckets pad the signed envelope with zeros inside the encryption, to the smallest of a series of sizes that starts at `floor` bytes and grows by `1/step` of each, rounded up. The example's first sizes are 8,192, 8,602 and 9,033 bytes.
69
+
70
+ Both bucket parameters must be integers from 1 to 4,294,967,295, or `cose.Padding.buckets` throws.
71
+
72
+ `cose.open` and `cose.decrypt` need no policy. They strip any number of zero bytes after the signed envelope and refuse a nonzero byte.
73
+
67
74
  ## Native packages
68
75
 
69
76
  The underlying implementation exists in two sibling repos, which track the same feature set and API surfaces, released at corresponding version points.
package/dist/cose.d.ts CHANGED
@@ -1,6 +1,30 @@
1
1
  import { Decodable, Encodable } from './cbor.js';
2
2
  import { SecretKey as XdsaSecretKey, PublicKey as XdsaPublicKey, Fingerprint as XdsaFingerprint } from './xdsa.js';
3
3
  import { SecretKey as XhpkeSecretKey, PublicKey as XhpkePublicKey, Fingerprint as XhpkeFingerprint } from './xhpke.js';
4
+ /**
5
+ * How many zero bytes a sender appends to the signed envelope inside the
6
+ * encryption, so the ciphertext's length shows little about the message.
7
+ *
8
+ * Receivers strip any number of zeros, so the policy is the sender's alone and
9
+ * can change without them.
10
+ */
11
+ export declare class Padding {
12
+ private readonly build;
13
+ private constructor();
14
+ /** No padding: the plaintext is the signed envelope alone. */
15
+ static none(): Padding;
16
+ /**
17
+ * Zeros after the signed envelope, up to the smallest size that fits. Sizes
18
+ * start at `floor`, and each next one is the previous one plus `1/step` of
19
+ * it, rounded up.
20
+ *
21
+ * @throws If `floor` or `step` is not an integer from 1 to 4,294,967,295
22
+ */
23
+ static buckets({ floor, step }: {
24
+ floor: number;
25
+ step: number;
26
+ }): Padding;
27
+ }
4
28
  /**
5
29
  * Creates a COSE_Sign1 signature with an embedded payload.
6
30
  *
@@ -119,12 +143,14 @@ export declare function recipient(ciphertext: Uint8Array): Promise<XhpkeFingerpr
119
143
  * @param signerKey - The xDSA secret key to sign with
120
144
  * @param recipientKey - The xHPKE public key to encrypt to
121
145
  * @param domain - Application domain for HPKE key derivation
146
+ * @param padding - Sender's policy for zeros after the signed envelope
122
147
  * @returns The serialized COSE_Encrypt0 envelope holding the encrypted
123
148
  * COSE_Sign1
124
149
  * @throws CodecError if a message does not fit its codec
125
150
  * @throws If a message falls outside the restricted CBOR type system
151
+ * @throws If `padding` is not a {@link Padding}
126
152
  */
127
- export declare function seal<S, A>(msgToSeal: Encodable<S>, msgToAuth: Encodable<A>, signerKey: XdsaSecretKey, recipientKey: XhpkePublicKey, domain: Uint8Array): Promise<Uint8Array>;
153
+ export declare function seal<S, A>(msgToSeal: Encodable<S>, msgToAuth: Encodable<A>, signerKey: XdsaSecretKey, recipientKey: XhpkePublicKey, domain: Uint8Array, padding: Padding): Promise<Uint8Array>;
128
154
  /**
129
155
  * Decrypts and verifies a sealed message.
130
156
  *
@@ -158,10 +184,12 @@ export declare function open<T, A>(msgToOpen: Decodable<T>, msgToAuth: Encodable
158
184
  * @param msgToAuth - The same additional authenticated data used during sealing
159
185
  * @param recipientKey - The xHPKE public key to encrypt to
160
186
  * @param domain - Application domain for HPKE key derivation
187
+ * @param padding - Sender's policy for zeros after the signed envelope
161
188
  * @returns The serialized COSE_Encrypt0 envelope
162
189
  * @throws CodecError if `msgToAuth` does not fit its codec
190
+ * @throws If `padding` is not a {@link Padding}
163
191
  */
164
- export declare function encrypt<A>(sign1: Uint8Array, msgToAuth: Encodable<A>, recipientKey: XhpkePublicKey, domain: Uint8Array): Promise<Uint8Array>;
192
+ export declare function encrypt<A>(sign1: Uint8Array, msgToAuth: Encodable<A>, recipientKey: XhpkePublicKey, domain: Uint8Array, padding: Padding): Promise<Uint8Array>;
165
193
  /**
166
194
  * Decrypts a sealed message without verifying the signature.
167
195
  *
@@ -169,13 +197,16 @@ export declare function encrypt<A>(sign1: Uint8Array, msgToAuth: Encodable<A>, r
169
197
  * to extract the signer's fingerprint, then {@link verify} with the same
170
198
  * `msgToAuth` and `domain` to verify.
171
199
  *
200
+ * It strips the zero bytes after the COSE_Sign1, accepting any number of them,
201
+ * and returns the COSE_Sign1 as encoded.
202
+ *
172
203
  * @param msgToOpen - The serialized COSE_Encrypt0 envelope
173
204
  * @param msgToAuth - The same additional authenticated data used during sealing
174
205
  * @param recipientKey - The xHPKE secret key to decrypt with
175
206
  * @param domain - Application domain for HPKE key derivation
176
207
  * @returns The decrypted COSE_Sign1 envelope, not yet verified
177
208
  * @throws If the envelope is malformed or does not decrypt for this key,
178
- * `msgToAuth` and `domain`
209
+ * `msgToAuth` and `domain`, or a nonzero byte follows the COSE_Sign1
179
210
  * @throws CodecError if `msgToAuth` does not fit its codec
180
211
  */
181
212
  export declare function decrypt<A>(msgToOpen: Uint8Array, msgToAuth: Encodable<A>, recipientKey: XhpkeSecretKey, domain: Uint8Array): Promise<Uint8Array>;
@@ -1 +1 @@
1
- {"version":3,"file":"cose.d.ts","sourceRoot":"","sources":["../src/cose.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AAEH,OAAO,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;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,IAAI,CAAC,CAAC,EAAE,CAAC,EAC7B,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,EACvB,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,EACvB,YAAY,EAAE,cAAc,EAC5B,SAAS,EAAE,aAAa,EACxB,MAAM,EAAE,UAAU,EAClB,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,CAAC,CAAC,CAeZ;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC7B,KAAK,EAAE,UAAU,EACjB,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,EACvB,YAAY,EAAE,cAAc,EAC5B,MAAM,EAAE,UAAU,EAClB,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,UAAU,CAAC,CAiBrB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC7B,SAAS,EAAE,UAAU,EACrB,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,EACvB,YAAY,EAAE,cAAc,EAC5B,MAAM,EAAE,UAAU,GACjB,OAAO,CAAC,UAAU,CAAC,CAQrB"}
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@
7
7
  * authenticated messages are bound to a `cbor` codec, which declares their
8
8
  * shape. The WASM module loads on the first call of any async function, so
9
9
  * there is no separate setup step.
10
+ * COSE sealing and encryption take a sender padding policy. Opening strips
11
+ * zero padding without needing that policy.
10
12
  *
11
13
  * @module
12
14
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;GAWG;AAEH,4DAA4D;AAC5D,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,kCAAkC;AAClC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,8CAA8C;AAC9C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,yDAAyD;AACzD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,wDAAwD;AACxD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,8BAA8B;AAC9B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AACpC,uDAAuD;AACvD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,4CAA4C;AAC5C,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;GAaG;AAEH,4DAA4D;AAC5D,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,kCAAkC;AAClC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,8CAA8C;AAC9C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,yDAAyD;AACzD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,wDAAwD;AACxD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,8BAA8B;AAC9B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AACpC,uDAAuD;AACvD,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,4CAA4C;AAC5C,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC"}