@signature-kit/crypto 0.1.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +21 -0
  3. package/README.md +30 -0
  4. package/dist/base64.d.ts +3 -0
  5. package/dist/base64.d.ts.map +1 -0
  6. package/dist/base64.js +55 -0
  7. package/dist/config.d.ts +46 -0
  8. package/dist/config.d.ts.map +1 -0
  9. package/dist/config.js +78 -0
  10. package/dist/pem.d.ts +3 -0
  11. package/dist/pem.d.ts.map +1 -0
  12. package/dist/pem.js +15 -0
  13. package/dist/pkcs12.d.ts +13 -0
  14. package/dist/pkcs12.d.ts.map +1 -0
  15. package/dist/pkcs12.js +471 -0
  16. package/dist/primitives/aes.d.ts +17 -0
  17. package/dist/primitives/aes.d.ts.map +1 -0
  18. package/dist/primitives/aes.js +219 -0
  19. package/dist/primitives/des.d.ts +23 -0
  20. package/dist/primitives/des.d.ts.map +1 -0
  21. package/dist/primitives/des.js +271 -0
  22. package/dist/primitives/hmac.d.ts +39 -0
  23. package/dist/primitives/hmac.d.ts.map +1 -0
  24. package/dist/primitives/hmac.js +146 -0
  25. package/dist/primitives/padding.d.ts +11 -0
  26. package/dist/primitives/padding.d.ts.map +1 -0
  27. package/dist/primitives/padding.js +29 -0
  28. package/dist/primitives/pbkdf2.d.ts +19 -0
  29. package/dist/primitives/pbkdf2.d.ts.map +1 -0
  30. package/dist/primitives/pbkdf2.js +61 -0
  31. package/dist/primitives/rc2.d.ts +23 -0
  32. package/dist/primitives/rc2.d.ts.map +1 -0
  33. package/dist/primitives/rc2.js +192 -0
  34. package/dist/primitives/sha1.d.ts +15 -0
  35. package/dist/primitives/sha1.d.ts.map +1 -0
  36. package/dist/primitives/sha1.js +102 -0
  37. package/dist/primitives/sha256.d.ts +30 -0
  38. package/dist/primitives/sha256.d.ts.map +1 -0
  39. package/dist/primitives/sha256.js +146 -0
  40. package/dist/primitives/sha512.d.ts +22 -0
  41. package/dist/primitives/sha512.d.ts.map +1 -0
  42. package/dist/primitives/sha512.js +197 -0
  43. package/package.json +61 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * AES-CBC pure TypeScript implementation (FIPS 197).
3
+ *
4
+ * Supports AES-128, AES-192, and AES-256 in CBC mode with PKCS#7 padding.
5
+ * Zero runtime dependencies. Works in any JS environment.
6
+ */
7
+ import { Effect } from "effect";
8
+ import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "../config";
9
+ import { removePkcs7Padding } from "./padding";
10
+ // AES S-box
11
+ const SBOX = new Uint8Array([
12
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
13
+ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
14
+ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
15
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
16
+ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
17
+ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
18
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
19
+ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
20
+ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
21
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
22
+ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
23
+ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
24
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
25
+ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
26
+ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
27
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
28
+ ]);
29
+ // AES inverse S-box
30
+ const SBOX_INV = new Uint8Array([
31
+ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
32
+ 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
33
+ 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
34
+ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
35
+ 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
36
+ 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
37
+ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
38
+ 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
39
+ 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
40
+ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
41
+ 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
42
+ 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
43
+ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
44
+ 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
45
+ 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
46
+ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
47
+ ]);
48
+ // Galois field multiplication in GF(2^8) with irreducible polynomial 0x11b
49
+ function gmul(a, b) {
50
+ let p = 0;
51
+ for (let i = 0; i < 8; i++) {
52
+ if (b & 1)
53
+ p ^= a;
54
+ const hiBit = a & 0x80;
55
+ a = (a << 1) & 0xff;
56
+ if (hiBit)
57
+ a ^= 0x1b;
58
+ b >>= 1;
59
+ }
60
+ return p;
61
+ }
62
+ // Round constant
63
+ const RCON = new Uint8Array([0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]);
64
+ /**
65
+ * Expand AES key into round keys.
66
+ * key: 16, 24, or 32 bytes
67
+ * Returns Uint8Array of (Nk+6+1)*16 bytes (all round keys concatenated)
68
+ */
69
+ function expandKey(key) {
70
+ const nk = key.length / 4; // 4, 6, or 8
71
+ const nr = nk + 6; // 10, 12, or 14 rounds
72
+ const nWords = (nr + 1) * 4;
73
+ const w = new Uint8Array(nWords * 4);
74
+ w.set(key);
75
+ for (let i = nk; i < nWords; i++) {
76
+ const t = w.subarray((i - 1) * 4, i * 4);
77
+ const temp = new Uint8Array(4);
78
+ temp.set(t);
79
+ if (i % nk === 0) {
80
+ // RotWord + SubWord + Rcon
81
+ const t0 = temp[0];
82
+ temp[0] = SBOX[temp[1]] ^ RCON[i / nk - 1];
83
+ temp[1] = SBOX[temp[2]];
84
+ temp[2] = SBOX[temp[3]];
85
+ temp[3] = SBOX[t0];
86
+ }
87
+ else if (nk > 6 && i % nk === 4) {
88
+ // SubWord only for AES-256
89
+ temp[0] = SBOX[temp[0]];
90
+ temp[1] = SBOX[temp[1]];
91
+ temp[2] = SBOX[temp[2]];
92
+ temp[3] = SBOX[temp[3]];
93
+ }
94
+ const base = (i - nk) * 4;
95
+ w[i * 4] = w[base] ^ temp[0];
96
+ w[i * 4 + 1] = w[base + 1] ^ temp[1];
97
+ w[i * 4 + 2] = w[base + 2] ^ temp[2];
98
+ w[i * 4 + 3] = w[base + 3] ^ temp[3];
99
+ }
100
+ return w;
101
+ }
102
+ /** XOR state with round key (in-place) */
103
+ function addRoundKey(state, rk, round) {
104
+ const offset = round * 16;
105
+ for (let i = 0; i < 16; i++) {
106
+ state[i] ^= rk[offset + i];
107
+ }
108
+ }
109
+ /** Apply inverse S-box to each byte */
110
+ function invSubBytes(state) {
111
+ for (let i = 0; i < 16; i++) {
112
+ state[i] = SBOX_INV[state[i]];
113
+ }
114
+ }
115
+ /** Inverse shift rows (right) */
116
+ function invShiftRows(state) {
117
+ let t;
118
+ // Row 1: shift 1 right
119
+ t = state[13];
120
+ state[13] = state[9];
121
+ state[9] = state[5];
122
+ state[5] = state[1];
123
+ state[1] = t;
124
+ // Row 2: shift 2 right
125
+ t = state[2];
126
+ state[2] = state[10];
127
+ state[10] = t;
128
+ t = state[6];
129
+ state[6] = state[14];
130
+ state[14] = t;
131
+ // Row 3: shift 3 right (= 1 left)
132
+ t = state[3];
133
+ state[3] = state[7];
134
+ state[7] = state[11];
135
+ state[11] = state[15];
136
+ state[15] = t;
137
+ }
138
+ /** Inverse mix columns */
139
+ function invMixColumns(state) {
140
+ for (let col = 0; col < 4; col++) {
141
+ const s0 = state[col * 4];
142
+ const s1 = state[col * 4 + 1];
143
+ const s2 = state[col * 4 + 2];
144
+ const s3 = state[col * 4 + 3];
145
+ state[col * 4] = gmul(0x0e, s0) ^ gmul(0x0b, s1) ^ gmul(0x0d, s2) ^ gmul(0x09, s3);
146
+ state[col * 4 + 1] = gmul(0x09, s0) ^ gmul(0x0e, s1) ^ gmul(0x0b, s2) ^ gmul(0x0d, s3);
147
+ state[col * 4 + 2] = gmul(0x0d, s0) ^ gmul(0x09, s1) ^ gmul(0x0e, s2) ^ gmul(0x0b, s3);
148
+ state[col * 4 + 3] = gmul(0x0b, s0) ^ gmul(0x0d, s1) ^ gmul(0x09, s2) ^ gmul(0x0e, s3);
149
+ }
150
+ }
151
+ /** Decrypt a single 16-byte block in place using the expanded round key */
152
+ function aesDecryptBlock(block, rk, nr) {
153
+ const state = new Uint8Array(block);
154
+ addRoundKey(state, rk, nr);
155
+ for (let round = nr - 1; round > 0; round--) {
156
+ invShiftRows(state);
157
+ invSubBytes(state);
158
+ addRoundKey(state, rk, round);
159
+ invMixColumns(state);
160
+ }
161
+ invShiftRows(state);
162
+ invSubBytes(state);
163
+ addRoundKey(state, rk, 0);
164
+ block.set(state);
165
+ }
166
+ /**
167
+ * Pure AES-CBC block decryption. Returns the still-padded plaintext.
168
+ *
169
+ * Preconditions (guaranteed by the only caller `aesCbcDecrypt`): `key.length`
170
+ * is 16/24/32, `iv.length === 16`, `ciphertext.length % 16 === 0`. Never throws.
171
+ */
172
+ function aesCbcDecryptRaw(key, iv, ciphertext) {
173
+ const nr = key.length / 4 + 6; // 10, 12, or 14
174
+ const rk = expandKey(key);
175
+ const plaintext = new Uint8Array(ciphertext.length);
176
+ let prev = iv;
177
+ for (let i = 0; i < ciphertext.length; i += 16) {
178
+ const block = new Uint8Array(ciphertext.subarray(i, i + 16));
179
+ const ct = new Uint8Array(block); // save ciphertext block for next IV
180
+ aesDecryptBlock(block, rk, nr);
181
+ // XOR with previous ciphertext block
182
+ for (let j = 0; j < 16; j++) {
183
+ plaintext[i + j] = block[j] ^ prev[j];
184
+ }
185
+ prev = ct;
186
+ }
187
+ return plaintext;
188
+ }
189
+ /**
190
+ * Decrypt data encrypted with AES-CBC + PKCS#7 padding.
191
+ *
192
+ * @param key - 16, 24, or 32 bytes
193
+ * @param iv - 16 bytes
194
+ * @param ciphertext - Must be a multiple of 16 bytes
195
+ */
196
+ export const aesCbcDecrypt = (key, iv, ciphertext) => {
197
+ if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
198
+ return Effect.fail(new CryptoError({
199
+ code: CryptoErrorCodeValue.cipherError,
200
+ reason: `AES: invalid key length ${key.length}`,
201
+ operation: CryptoOperationValue.cipherAes,
202
+ }));
203
+ }
204
+ if (iv.length !== 16) {
205
+ return Effect.fail(new CryptoError({
206
+ code: CryptoErrorCodeValue.cipherError,
207
+ reason: "AES-CBC: IV must be 16 bytes",
208
+ operation: CryptoOperationValue.cipherAes,
209
+ }));
210
+ }
211
+ if (ciphertext.length % 16 !== 0) {
212
+ return Effect.fail(new CryptoError({
213
+ code: CryptoErrorCodeValue.cipherError,
214
+ reason: "AES-CBC: ciphertext must be multiple of 16 bytes",
215
+ operation: CryptoOperationValue.cipherAes,
216
+ }));
217
+ }
218
+ return removePkcs7Padding(aesCbcDecryptRaw(key, iv, ciphertext), 16);
219
+ };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * DES and 3DES-EDE-CBC pure TypeScript implementation (FIPS 46-3).
3
+ *
4
+ * Implements single DES (used as building block) and triple-DES EDE
5
+ * (Encrypt-Decrypt-Encrypt with three independent keys) in CBC mode
6
+ * with PKCS#7 unpadding.
7
+ *
8
+ * Zero runtime dependencies. Works in any JS environment.
9
+ *
10
+ * ⚠️ 3DES is considered legacy/deprecated. It is used here exclusively for
11
+ * PKCS#12 compatibility (ICP-Brasil A1 certificates).
12
+ */
13
+ import { Effect } from "effect";
14
+ import { CryptoError } from "../config";
15
+ /**
16
+ * Decrypt data using 3DES-EDE-CBC with PKCS#7 unpadding.
17
+ *
18
+ * @param key - 24 bytes (three 8-byte DES keys: K1, K2, K3)
19
+ * @param iv - 8 bytes
20
+ * @param ciphertext - Must be a multiple of 8 bytes
21
+ */
22
+ export declare const tripleDesCbcDecrypt: (key: Uint8Array, iv: Uint8Array, ciphertext: Uint8Array) => Effect.Effect<Uint8Array, CryptoError>;
23
+ //# sourceMappingURL=des.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"des.d.ts","sourceRoot":"","sources":["../../src/primitives/des.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,WAAW,CAAC;AA6QpF;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,GAC9B,KAAK,UAAU,EACf,IAAI,UAAU,EACd,YAAY,UAAU,KACrB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CA8BvC,CAAC"}
@@ -0,0 +1,271 @@
1
+ /**
2
+ * DES and 3DES-EDE-CBC pure TypeScript implementation (FIPS 46-3).
3
+ *
4
+ * Implements single DES (used as building block) and triple-DES EDE
5
+ * (Encrypt-Decrypt-Encrypt with three independent keys) in CBC mode
6
+ * with PKCS#7 unpadding.
7
+ *
8
+ * Zero runtime dependencies. Works in any JS environment.
9
+ *
10
+ * ⚠️ 3DES is considered legacy/deprecated. It is used here exclusively for
11
+ * PKCS#12 compatibility (ICP-Brasil A1 certificates).
12
+ */
13
+ import { Effect } from "effect";
14
+ import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "../config";
15
+ import { removePkcs7Padding } from "./padding";
16
+ // DES permutation tables are 1-based (bit positions) converted to 0-based.
17
+ // Initial Permutation (IP)
18
+ const IP = new Uint8Array([
19
+ 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64,
20
+ 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53,
21
+ 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
22
+ ]);
23
+ // Final Permutation (IP^-1)
24
+ const FP = new Uint8Array([
25
+ 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37,
26
+ 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2,
27
+ 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25,
28
+ ]);
29
+ // PC-1 permutation (64 bits → 56 bits, split into C and D)
30
+ const PC1 = new Uint8Array([
31
+ 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60,
32
+ 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
33
+ 13, 5, 28, 20, 12, 4,
34
+ ]);
35
+ // PC-2 permutation (56 bits → 48 bits per sub-key)
36
+ const PC2 = new Uint8Array([
37
+ 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52,
38
+ 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32,
39
+ ]);
40
+ // Expansion permutation E (32 bits → 48 bits)
41
+ const E = new Uint8Array([
42
+ 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19,
43
+ 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1,
44
+ ]);
45
+ // P permutation (after S-boxes, 32 bits → 32 bits)
46
+ const P_TABLE = new Uint8Array([
47
+ 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13,
48
+ 30, 6, 22, 11, 4, 25,
49
+ ]);
50
+ // S-boxes: 8 boxes, each 4×16 = 64 values
51
+ const S_BOXES = [
52
+ new Uint8Array([
53
+ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11,
54
+ 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5,
55
+ 11, 3, 14, 10, 0, 6, 13,
56
+ ]),
57
+ new Uint8Array([
58
+ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10,
59
+ 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2,
60
+ 11, 6, 7, 12, 0, 5, 14, 9,
61
+ ]),
62
+ new Uint8Array([
63
+ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12,
64
+ 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4,
65
+ 15, 14, 3, 11, 5, 2, 12,
66
+ ]),
67
+ new Uint8Array([
68
+ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1,
69
+ 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9,
70
+ 4, 5, 11, 12, 7, 2, 14,
71
+ ]),
72
+ new Uint8Array([
73
+ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10,
74
+ 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6,
75
+ 15, 0, 9, 10, 4, 5, 3,
76
+ ]),
77
+ new Uint8Array([
78
+ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14,
79
+ 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10,
80
+ 11, 14, 1, 7, 6, 0, 8, 13,
81
+ ]),
82
+ new Uint8Array([
83
+ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12,
84
+ 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9,
85
+ 5, 0, 15, 14, 2, 3, 12,
86
+ ]),
87
+ new Uint8Array([
88
+ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11,
89
+ 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13,
90
+ 15, 12, 9, 0, 3, 5, 6, 11,
91
+ ]),
92
+ ];
93
+ // Number of left rotations per round
94
+ const LEFT_ROTATIONS = new Uint8Array([1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]);
95
+ /** Get a specific bit (1-based) from a byte array */
96
+ function getBit(data, pos) {
97
+ const p = pos - 1;
98
+ return (data[p >> 3] >> (7 - (p & 7))) & 1;
99
+ }
100
+ /** Apply a permutation to bits */
101
+ function permute(data, table, outBits) {
102
+ const result = new Uint8Array(Math.ceil(outBits / 8));
103
+ for (let i = 0; i < outBits; i++) {
104
+ const bit = getBit(data, table[i]);
105
+ result[i >> 3] |= bit << (7 - (i & 7));
106
+ }
107
+ return result;
108
+ }
109
+ /** Left-rotate a 28-bit value by n positions */
110
+ function rotateLeft28(v, n) {
111
+ return ((v << n) | (v >>> (28 - n))) & 0x0fffffff;
112
+ }
113
+ /** Generate 16 48-bit sub-keys from a 64-bit DES key */
114
+ function generateSubkeys(key) {
115
+ // Apply PC-1
116
+ const kp = permute(key, PC1, 56);
117
+ // Split into C (bits 1-28) and D (bits 29-56) stored in 32-bit integers
118
+ let C = 0, D = 0;
119
+ for (let i = 0; i < 28; i++) {
120
+ C |= getBit(kp, i + 1) << (27 - i);
121
+ D |= getBit(kp, i + 29) << (27 - i);
122
+ }
123
+ const subkeys = [];
124
+ for (let round = 0; round < 16; round++) {
125
+ C = rotateLeft28(C, LEFT_ROTATIONS[round]);
126
+ D = rotateLeft28(D, LEFT_ROTATIONS[round]);
127
+ // Combine C and D into 7 bytes for PC-2
128
+ const cd = new Uint8Array(7);
129
+ for (let i = 0; i < 28; i++) {
130
+ const bit = (C >> (27 - i)) & 1;
131
+ cd[i >> 3] |= bit << (7 - (i & 7));
132
+ }
133
+ for (let i = 0; i < 28; i++) {
134
+ const bit = (D >> (27 - i)) & 1;
135
+ cd[(i + 28) >> 3] |= bit << (7 - ((i + 28) & 7));
136
+ }
137
+ subkeys.push(permute(cd, PC2, 48));
138
+ }
139
+ return subkeys;
140
+ }
141
+ /** DES Feistel function f(R, K) */
142
+ function feistel(R, K) {
143
+ // Expand R (32 bits) to 48 bits using E table
144
+ const expanded = permute(R, E, 48);
145
+ // XOR with sub-key
146
+ for (let i = 0; i < 6; i++) {
147
+ expanded[i] ^= K[i];
148
+ }
149
+ // S-box substitution: 48 bits → 32 bits
150
+ const sOut = new Uint8Array(4);
151
+ for (let box = 0; box < 8; box++) {
152
+ const bitPos = box * 6;
153
+ const byteOff = bitPos >> 3;
154
+ const bitOff = bitPos & 7;
155
+ // Extract 6 bits
156
+ let sixBits;
157
+ if (bitOff <= 2) {
158
+ sixBits = (expanded[byteOff] >> (2 - bitOff)) & 0x3f;
159
+ }
160
+ else {
161
+ sixBits =
162
+ ((expanded[byteOff] << (bitOff - 2)) | (expanded[byteOff + 1] >> (10 - bitOff))) & 0x3f;
163
+ }
164
+ // Row = b1 (MSB) and b6 (LSB) of the 6-bit group: row = (b1 << 1) | b6
165
+ const row = ((sixBits >> 4) & 2) | (sixBits & 1);
166
+ const col = (sixBits >> 1) & 0x0f;
167
+ const val = S_BOXES[box][row * 16 + col];
168
+ // Write 4 bits of output
169
+ const outBitPos = box * 4;
170
+ const outByteOff = outBitPos >> 3;
171
+ const outBitOff = outBitPos & 7;
172
+ if (outBitOff <= 4) {
173
+ sOut[outByteOff] |= val << (4 - outBitOff);
174
+ }
175
+ else {
176
+ sOut[outByteOff] |= val >> (outBitOff - 4);
177
+ sOut[outByteOff + 1] |= (val << (12 - outBitOff)) & 0xff;
178
+ }
179
+ }
180
+ return permute(sOut, P_TABLE, 32);
181
+ }
182
+ /**
183
+ * Encrypt or decrypt a single 8-byte DES block.
184
+ * @param block - 8 bytes (modified in place)
185
+ * @param subkeys - 16 sub-keys (forward for encrypt, reversed for decrypt)
186
+ */
187
+ function desBlock(block, subkeys) {
188
+ const perm = permute(block, IP, 64);
189
+ let L = perm.subarray(0, 4);
190
+ let R = perm.subarray(4, 8);
191
+ for (let round = 0; round < 16; round++) {
192
+ const fOut = feistel(R, subkeys[round]);
193
+ const newR = new Uint8Array(4);
194
+ for (let i = 0; i < 4; i++) {
195
+ newR[i] = L[i] ^ fOut[i];
196
+ }
197
+ L = R;
198
+ R = newR;
199
+ }
200
+ // Final: R16 || L16
201
+ const preOut = new Uint8Array(8);
202
+ preOut.set(R, 0);
203
+ preOut.set(L, 4);
204
+ const out = permute(preOut, FP, 64);
205
+ block.set(out);
206
+ }
207
+ /**
208
+ * Pure 3DES-EDE-CBC decryption returning still-padded plaintext.
209
+ * Never throws. Caller is responsible for validation and unpadding.
210
+ *
211
+ * @param key - 24 bytes (three 8-byte DES keys: K1, K2, K3)
212
+ * @param iv - 8 bytes
213
+ * @param ciphertext - Must be a multiple of 8 bytes
214
+ */
215
+ function tripleDesCbcDecryptRaw(key, iv, ciphertext) {
216
+ // Generate sub-keys for each of the three DES keys
217
+ const sk1 = generateSubkeys(key.subarray(0, 8));
218
+ const sk2 = generateSubkeys(key.subarray(8, 16));
219
+ const sk3 = generateSubkeys(key.subarray(16, 24));
220
+ // Reversed sub-keys for decryption (K1 and K3 are applied in reverse)
221
+ const sk1r = [...sk1].reverse();
222
+ const sk3r = [...sk3].reverse();
223
+ const plaintext = new Uint8Array(ciphertext.length);
224
+ let prev = new Uint8Array(iv);
225
+ for (let i = 0; i < ciphertext.length; i += 8) {
226
+ const block = new Uint8Array(ciphertext.subarray(i, i + 8));
227
+ const ctBlock = new Uint8Array(block); // save for CBC chain
228
+ // 3DES-EDE decrypt: D(K3) → E(K2) → D(K1)
229
+ // But "EDE" for decryption means: decrypt with K3, encrypt with K2, decrypt with K1
230
+ desBlock(block, sk3r); // Decrypt with K3
231
+ desBlock(block, sk2); // Encrypt with K2
232
+ desBlock(block, sk1r); // Decrypt with K1
233
+ // CBC: XOR with previous ciphertext block
234
+ for (let j = 0; j < 8; j++) {
235
+ plaintext[i + j] = block[j] ^ prev[j];
236
+ }
237
+ prev = ctBlock;
238
+ }
239
+ return plaintext;
240
+ }
241
+ /**
242
+ * Decrypt data using 3DES-EDE-CBC with PKCS#7 unpadding.
243
+ *
244
+ * @param key - 24 bytes (three 8-byte DES keys: K1, K2, K3)
245
+ * @param iv - 8 bytes
246
+ * @param ciphertext - Must be a multiple of 8 bytes
247
+ */
248
+ export const tripleDesCbcDecrypt = (key, iv, ciphertext) => {
249
+ if (key.length !== 24) {
250
+ return Effect.fail(new CryptoError({
251
+ code: CryptoErrorCodeValue.cipherError,
252
+ reason: `3DES: key must be 24 bytes, got ${key.length}`,
253
+ operation: CryptoOperationValue.cipherDes,
254
+ }));
255
+ }
256
+ if (iv.length !== 8) {
257
+ return Effect.fail(new CryptoError({
258
+ code: CryptoErrorCodeValue.cipherError,
259
+ reason: "3DES-CBC: IV must be 8 bytes",
260
+ operation: CryptoOperationValue.cipherDes,
261
+ }));
262
+ }
263
+ if (ciphertext.length % 8 !== 0) {
264
+ return Effect.fail(new CryptoError({
265
+ code: CryptoErrorCodeValue.cipherError,
266
+ reason: "3DES-CBC: ciphertext must be multiple of 8 bytes",
267
+ operation: CryptoOperationValue.cipherDes,
268
+ }));
269
+ }
270
+ return removePkcs7Padding(tripleDesCbcDecryptRaw(key, iv, ciphertext), 8);
271
+ };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * HMAC pure TypeScript implementation (RFC 2104).
3
+ *
4
+ * Supports SHA-1, SHA-256, SHA-384, and SHA-512 as the underlying hash.
5
+ * Zero runtime dependencies. Works in any JS environment.
6
+ */
7
+ import { Schema } from "effect";
8
+ export declare const HmacHashAlgorithmSchema: Schema.Literals<readonly ["sha1", "sha256", "sha384", "sha512"]>;
9
+ export type HmacHashAlgorithm = (typeof HmacHashAlgorithmSchema)["Type"];
10
+ /**
11
+ * Compute HMAC(key, data) using the specified hash algorithm.
12
+ *
13
+ * @param alg - Hash algorithm: 'sha1' | 'sha256' | 'sha384' | 'sha512'
14
+ * @param key - Secret key (any length)
15
+ * @param data - Message data
16
+ * @returns HMAC digest as Uint8Array (same length as hash output)
17
+ */
18
+ export declare function hmac(alg: HmacHashAlgorithm, key: Uint8Array, data: Uint8Array): Uint8Array;
19
+ /**
20
+ * A precomputed HMAC context for a fixed key.
21
+ *
22
+ * When calling HMAC many times with the same key (e.g., PBKDF2), precomputing
23
+ * the ipad and opad half-states avoids redundant work on every call.
24
+ *
25
+ * Currently optimized for SHA-256 (the most common PBKDF2 PRF).
26
+ * Falls back to the standard hmac() for other algorithms.
27
+ */
28
+ export type HmacContext = {
29
+ /** Compute HMAC for the given message data */
30
+ readonly compute: (data: Uint8Array) => Uint8Array;
31
+ };
32
+ /**
33
+ * Create a precomputed HMAC context for a fixed key and algorithm.
34
+ *
35
+ * For SHA-256, this uses block-state precomputation for best performance.
36
+ * For other algorithms, falls back to standard HMAC with cached key pads.
37
+ */
38
+ export declare function createHmac(alg: HmacHashAlgorithm, key: Uint8Array): HmacContext;
39
+ //# sourceMappingURL=hmac.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmac.d.ts","sourceRoot":"","sources":["../../src/primitives/hmac.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKhC,eAAO,MAAM,uBAAuB,kEAA0D,CAAC;AAC/F,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,uBAAuB,CAAC,CAAC,MAAM,CAAC,CAAC;AAyBzE;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU,CAgC1F;AAED;;;;;;;;GAQG;AAEH,MAAM,MAAM,WAAW,GAAG;IACxB,8CAA8C;IAC9C,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,UAAU,CAAC;CACpD,CAAC;AA0CF;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,GAAG,WAAW,CAkC/E"}