@basmilius/apple-encryption 0.9.19 → 0.10.1

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/dist/index.d.mts CHANGED
@@ -1,43 +1,130 @@
1
1
  declare namespace chacha20_d_exports {
2
2
  export { CHACHA20_AUTH_TAG_LENGTH, CHACHA20_NONCE_LENGTH, DecryptionError, EncryptedData, decrypt, encrypt, padNonce };
3
3
  }
4
+ /**
5
+ * Error thrown when ChaCha20-Poly1305 decryption fails due to an
6
+ * authentication tag mismatch, indicating the ciphertext was tampered
7
+ * with or the wrong key/nonce was used.
8
+ */
4
9
  declare class DecryptionError extends Error {
5
10
  constructor(message?: string);
6
11
  }
12
+ /** Length in bytes of the Poly1305 authentication tag appended to ciphertext. */
7
13
  declare const CHACHA20_AUTH_TAG_LENGTH = 16;
14
+ /** Required nonce length in bytes for ChaCha20-Poly1305. Shorter nonces are zero-padded. */
8
15
  declare const CHACHA20_NONCE_LENGTH = 12;
16
+ /**
17
+ * Decrypts a ChaCha20-Poly1305 sealed message and verifies its authentication tag.
18
+ *
19
+ * @param key - 256-bit encryption key.
20
+ * @param nonce - Nonce (up to 12 bytes; shorter nonces are left-padded with zeros).
21
+ * @param aad - Additional authenticated data, or null if none.
22
+ * @param ciphertext - The encrypted payload (without the auth tag).
23
+ * @param authTag - The 16-byte Poly1305 authentication tag.
24
+ * @returns The decrypted plaintext.
25
+ * @throws DecryptionError if the authentication tag does not match.
26
+ */
9
27
  declare function decrypt(key: Buffer, nonce: Buffer, aad: Buffer | null, ciphertext: Buffer, authTag: Buffer): Buffer;
28
+ /**
29
+ * Encrypts a plaintext message using ChaCha20-Poly1305 authenticated encryption.
30
+ *
31
+ * @param key - 256-bit encryption key.
32
+ * @param nonce - Nonce (up to 12 bytes; shorter nonces are left-padded with zeros).
33
+ * @param aad - Additional authenticated data, or null if none.
34
+ * @param plaintext - The data to encrypt.
35
+ * @returns The ciphertext and its Poly1305 authentication tag.
36
+ */
10
37
  declare function encrypt(key: Buffer, nonce: Buffer, aad: Buffer | null, plaintext: Buffer): EncryptedData;
38
+ /**
39
+ * Pads a nonce to the required 12-byte length by prepending zero bytes.
40
+ * If the nonce is already 12 bytes or longer, it is returned unchanged.
41
+ *
42
+ * @param nonce - The nonce to pad.
43
+ * @returns A nonce buffer of at least 12 bytes.
44
+ */
11
45
  declare function padNonce(nonce: Buffer): Buffer;
46
+ /**
47
+ * Result of a ChaCha20-Poly1305 encryption operation containing
48
+ * the ciphertext and its authentication tag as separate buffers.
49
+ */
12
50
  type EncryptedData = {
13
- readonly ciphertext: Buffer;
51
+ /** The encrypted payload. */readonly ciphertext: Buffer; /** The 16-byte Poly1305 authentication tag for integrity verification. */
14
52
  readonly authTag: Buffer;
15
53
  };
16
54
  //#endregion
17
55
  //#region src/types.d.ts
56
+ /**
57
+ * Represents a cryptographic key pair consisting of a public key and a secret key.
58
+ * Used by both Ed25519 (signing) and Curve25519 (key exchange) operations.
59
+ */
18
60
  type KeyPair = {
19
- readonly publicKey: Uint8Array;
61
+ /** The public key component, safe to share with other parties. */readonly publicKey: Uint8Array; /** The secret (private) key component, must be kept confidential. */
20
62
  readonly secretKey: Uint8Array;
21
63
  };
22
64
  declare namespace curve25519_d_exports {
23
65
  export { generateKeyPair$1 as generateKeyPair, generateSharedSecKey };
24
66
  }
67
+ /**
68
+ * Generates a new Curve25519 key pair for Diffie-Hellman key exchange.
69
+ *
70
+ * @returns A key pair with a public key and secret key.
71
+ */
25
72
  declare function generateKeyPair$1(): KeyPair;
73
+ /**
74
+ * Computes a shared secret using Curve25519 Diffie-Hellman key exchange.
75
+ * Both parties derive the same shared secret from their own private key
76
+ * and the other party's public key.
77
+ *
78
+ * @param priKey - The local party's private (secret) key.
79
+ * @param pubKey - The remote party's public key.
80
+ * @returns The 32-byte shared secret.
81
+ */
26
82
  declare function generateSharedSecKey(priKey: Uint8Array, pubKey: Uint8Array): Uint8Array;
27
83
  declare namespace ed25519_d_exports {
28
84
  export { generateKeyPair, sign, verify };
29
85
  }
86
+ /**
87
+ * Generates a new Ed25519 key pair for digital signature operations.
88
+ *
89
+ * @returns A key pair with a public key and secret key.
90
+ */
30
91
  declare function generateKeyPair(): KeyPair;
92
+ /**
93
+ * Creates an Ed25519 digital signature for a message.
94
+ *
95
+ * @param message - The data to sign.
96
+ * @param secretKey - The signer's secret (private) key.
97
+ * @returns The 64-byte Ed25519 signature.
98
+ */
31
99
  declare function sign(message: Uint8Array, secretKey: Uint8Array): Uint8Array;
100
+ /**
101
+ * Verifies an Ed25519 digital signature against a message and public key.
102
+ *
103
+ * @param message - The original signed data.
104
+ * @param signature - The 64-byte Ed25519 signature to verify.
105
+ * @param publicKey - The signer's public key.
106
+ * @returns True if the signature is valid, false otherwise.
107
+ */
32
108
  declare function verify(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean;
33
109
  //#endregion
34
110
  //#region src/hkdf.d.ts
111
+ /**
112
+ * Derives a cryptographic key using HKDF (HMAC-based Key Derivation Function)
113
+ * as defined in RFC 5869. Used throughout the protocol stack to derive
114
+ * encryption and authentication keys from shared secrets.
115
+ *
116
+ * @param options - The HKDF parameters including hash algorithm, input key material, salt, info, and desired output length.
117
+ * @returns The derived key material as a Buffer.
118
+ */
35
119
  declare function export_default(options: HKDFOptions): Buffer;
120
+ /**
121
+ * Configuration options for HKDF key derivation.
122
+ */
36
123
  type HKDFOptions = {
37
- readonly hash: string;
38
- readonly key: Buffer;
39
- readonly length: number;
40
- readonly salt: Buffer;
124
+ /** The hash algorithm to use (e.g. 'sha512'). */readonly hash: string; /** The input key material (e.g. a shared secret from Diffie-Hellman). */
125
+ readonly key: Buffer; /** The desired length of the derived key in bytes. */
126
+ readonly length: number; /** Optional salt value for the extract step (can be zero-length). */
127
+ readonly salt: Buffer; /** Context and application-specific info string for the expand step. */
41
128
  readonly info: Buffer;
42
129
  };
43
130
  //#endregion
package/dist/index.mjs CHANGED
@@ -833,14 +833,32 @@ var chacha20_exports = /* @__PURE__ */ __exportAll({
833
833
  encrypt: () => encrypt,
834
834
  padNonce: () => padNonce
835
835
  });
836
+ /**
837
+ * Error thrown when ChaCha20-Poly1305 decryption fails due to an
838
+ * authentication tag mismatch, indicating the ciphertext was tampered
839
+ * with or the wrong key/nonce was used.
840
+ */
836
841
  var DecryptionError = class extends Error {
837
842
  constructor(message = "Decryption failed: authentication tag mismatch") {
838
843
  super(message);
839
844
  this.name = "DecryptionError";
840
845
  }
841
846
  };
847
+ /** Length in bytes of the Poly1305 authentication tag appended to ciphertext. */
842
848
  const CHACHA20_AUTH_TAG_LENGTH = 16;
849
+ /** Required nonce length in bytes for ChaCha20-Poly1305. Shorter nonces are zero-padded. */
843
850
  const CHACHA20_NONCE_LENGTH = 12;
851
+ /**
852
+ * Decrypts a ChaCha20-Poly1305 sealed message and verifies its authentication tag.
853
+ *
854
+ * @param key - 256-bit encryption key.
855
+ * @param nonce - Nonce (up to 12 bytes; shorter nonces are left-padded with zeros).
856
+ * @param aad - Additional authenticated data, or null if none.
857
+ * @param ciphertext - The encrypted payload (without the auth tag).
858
+ * @param authTag - The 16-byte Poly1305 authentication tag.
859
+ * @returns The decrypted plaintext.
860
+ * @throws DecryptionError if the authentication tag does not match.
861
+ */
844
862
  function decrypt(key, nonce, aad, ciphertext, authTag) {
845
863
  nonce = padNonce(nonce);
846
864
  const chacha = new ChaCha20Poly1305(key);
@@ -849,6 +867,15 @@ function decrypt(key, nonce, aad, ciphertext, authTag) {
849
867
  if (!plaintext) throw new DecryptionError();
850
868
  return Buffer.from(plaintext);
851
869
  }
870
+ /**
871
+ * Encrypts a plaintext message using ChaCha20-Poly1305 authenticated encryption.
872
+ *
873
+ * @param key - 256-bit encryption key.
874
+ * @param nonce - Nonce (up to 12 bytes; shorter nonces are left-padded with zeros).
875
+ * @param aad - Additional authenticated data, or null if none.
876
+ * @param plaintext - The data to encrypt.
877
+ * @returns The ciphertext and its Poly1305 authentication tag.
878
+ */
852
879
  function encrypt(key, nonce, aad, plaintext) {
853
880
  nonce = padNonce(nonce);
854
881
  const sealed = new ChaCha20Poly1305(key).seal(nonce, plaintext, aad ?? void 0);
@@ -857,6 +884,13 @@ function encrypt(key, nonce, aad, plaintext) {
857
884
  authTag: Buffer.from(sealed.subarray(sealed.length - 16))
858
885
  };
859
886
  }
887
+ /**
888
+ * Pads a nonce to the required 12-byte length by prepending zero bytes.
889
+ * If the nonce is already 12 bytes or longer, it is returned unchanged.
890
+ *
891
+ * @param nonce - The nonce to pad.
892
+ * @returns A nonce buffer of at least 12 bytes.
893
+ */
860
894
  function padNonce(nonce) {
861
895
  if (nonce.length >= 12) return nonce;
862
896
  return Buffer.concat([Buffer.alloc(12 - nonce.length, 0), nonce]);
@@ -1474,9 +1508,23 @@ var curve25519_exports = /* @__PURE__ */ __exportAll({
1474
1508
  generateKeyPair: () => generateKeyPair$2,
1475
1509
  generateSharedSecKey: () => generateSharedSecKey
1476
1510
  });
1511
+ /**
1512
+ * Generates a new Curve25519 key pair for Diffie-Hellman key exchange.
1513
+ *
1514
+ * @returns A key pair with a public key and secret key.
1515
+ */
1477
1516
  function generateKeyPair$2() {
1478
1517
  return generateKeyPair$3();
1479
1518
  }
1519
+ /**
1520
+ * Computes a shared secret using Curve25519 Diffie-Hellman key exchange.
1521
+ * Both parties derive the same shared secret from their own private key
1522
+ * and the other party's public key.
1523
+ *
1524
+ * @param priKey - The local party's private (secret) key.
1525
+ * @param pubKey - The remote party's public key.
1526
+ * @returns The 32-byte shared secret.
1527
+ */
1480
1528
  function generateSharedSecKey(priKey, pubKey) {
1481
1529
  return sharedKey(priKey, pubKey);
1482
1530
  }
@@ -2965,18 +3013,46 @@ var ed25519_exports = /* @__PURE__ */ __exportAll({
2965
3013
  sign: () => sign,
2966
3014
  verify: () => verify
2967
3015
  });
3016
+ /**
3017
+ * Generates a new Ed25519 key pair for digital signature operations.
3018
+ *
3019
+ * @returns A key pair with a public key and secret key.
3020
+ */
2968
3021
  function generateKeyPair() {
2969
3022
  return generateKeyPair$1();
2970
3023
  }
3024
+ /**
3025
+ * Creates an Ed25519 digital signature for a message.
3026
+ *
3027
+ * @param message - The data to sign.
3028
+ * @param secretKey - The signer's secret (private) key.
3029
+ * @returns The 64-byte Ed25519 signature.
3030
+ */
2971
3031
  function sign(message, secretKey) {
2972
3032
  return sign$1(secretKey, message);
2973
3033
  }
3034
+ /**
3035
+ * Verifies an Ed25519 digital signature against a message and public key.
3036
+ *
3037
+ * @param message - The original signed data.
3038
+ * @param signature - The 64-byte Ed25519 signature to verify.
3039
+ * @param publicKey - The signer's public key.
3040
+ * @returns True if the signature is valid, false otherwise.
3041
+ */
2974
3042
  function verify(message, signature, publicKey) {
2975
3043
  return verify$1(publicKey, message, signature);
2976
3044
  }
2977
3045
 
2978
3046
  //#endregion
2979
3047
  //#region src/hkdf.ts
3048
+ /**
3049
+ * Derives a cryptographic key using HKDF (HMAC-based Key Derivation Function)
3050
+ * as defined in RFC 5869. Used throughout the protocol stack to derive
3051
+ * encryption and authentication keys from shared secrets.
3052
+ *
3053
+ * @param options - The HKDF parameters including hash algorithm, input key material, salt, info, and desired output length.
3054
+ * @returns The derived key material as a Buffer.
3055
+ */
2980
3056
  function hkdf_default(options) {
2981
3057
  return Buffer.from(hkdfSync(options.hash, options.key, options.salt, options.info, options.length));
2982
3058
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@basmilius/apple-encryption",
3
3
  "description": "Common encryption utilities for Apple Protocols.",
4
- "version": "0.9.19",
4
+ "version": "0.10.1",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {