@signature-kit/crypto 0.2.1 → 0.2.2

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 (41) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/base64.d.ts +1 -1
  3. package/dist/base64.d.ts.map +1 -1
  4. package/dist/base64.js +15 -14
  5. package/dist/config.d.ts +3 -7
  6. package/dist/config.d.ts.map +1 -1
  7. package/dist/config.js +1 -7
  8. package/dist/pem.d.ts +1 -1
  9. package/dist/pem.d.ts.map +1 -1
  10. package/dist/pem.js +14 -8
  11. package/dist/pkcs12.d.ts +2 -10
  12. package/dist/pkcs12.d.ts.map +1 -1
  13. package/dist/pkcs12.js +368 -124
  14. package/dist/primitives/aes.d.ts +1 -14
  15. package/dist/primitives/aes.d.ts.map +1 -1
  16. package/dist/primitives/aes.js +8 -47
  17. package/dist/primitives/des.d.ts +1 -20
  18. package/dist/primitives/des.d.ts.map +1 -1
  19. package/dist/primitives/des.js +6 -67
  20. package/dist/primitives/hmac.d.ts +0 -30
  21. package/dist/primitives/hmac.d.ts.map +1 -1
  22. package/dist/primitives/hmac.js +4 -46
  23. package/dist/primitives/padding.d.ts +1 -8
  24. package/dist/primitives/padding.d.ts.map +1 -1
  25. package/dist/primitives/padding.js +2 -11
  26. package/dist/primitives/pbkdf2.d.ts +1 -17
  27. package/dist/primitives/pbkdf2.d.ts.map +1 -1
  28. package/dist/primitives/pbkdf2.js +8 -30
  29. package/dist/primitives/rc2.d.ts +1 -20
  30. package/dist/primitives/rc2.d.ts.map +1 -1
  31. package/dist/primitives/rc2.js +11 -69
  32. package/dist/primitives/sha1.d.ts +0 -13
  33. package/dist/primitives/sha1.d.ts.map +1 -1
  34. package/dist/primitives/sha1.js +1 -28
  35. package/dist/primitives/sha256.d.ts +0 -26
  36. package/dist/primitives/sha256.d.ts.map +1 -1
  37. package/dist/primitives/sha256.js +0 -34
  38. package/dist/primitives/sha512.d.ts +0 -19
  39. package/dist/primitives/sha512.d.ts.map +1 -1
  40. package/dist/primitives/sha512.js +1 -32
  41. package/package.json +7 -5
@@ -1,17 +1,4 @@
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
1
  import { Effect } from "effect";
8
- import { CryptoError } from "../config";
9
- /**
10
- * Decrypt data encrypted with AES-CBC + PKCS#7 padding.
11
- *
12
- * @param key - 16, 24, or 32 bytes
13
- * @param iv - 16 bytes
14
- * @param ciphertext - Must be a multiple of 16 bytes
15
- */
2
+ import { CryptoError } from "../config.js";
16
3
  export declare const aesCbcDecrypt: (key: Uint8Array, iv: Uint8Array, ciphertext: Uint8Array) => Effect.Effect<Uint8Array, CryptoError>;
17
4
  //# sourceMappingURL=aes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"aes.d.ts","sourceRoot":"","sources":["../../src/primitives/aes.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,WAAW,CAAC;AAsMpF;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,GACxB,KAAK,UAAU,EACf,IAAI,UAAU,EACd,YAAY,UAAU,KACrB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CA8BvC,CAAC"}
1
+ {"version":3,"file":"aes.d.ts","sourceRoot":"","sources":["../../src/primitives/aes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,cAAc,CAAC;AA4KvF,eAAO,MAAM,aAAa,QACnB,UAAU,MACX,UAAU,cACF,UAAU,KACrB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CA8BvC,CAAC"}
@@ -1,13 +1,6 @@
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
1
  import { Effect } from "effect";
8
- import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "../config";
9
- import { removePkcs7Padding } from "./padding";
10
- // AES S-box
2
+ import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "../config.js";
3
+ import { removePkcs7Padding } from "./padding.js";
11
4
  const SBOX = new Uint8Array([
12
5
  0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
13
6
  0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
@@ -26,7 +19,6 @@ const SBOX = new Uint8Array([
26
19
  0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
27
20
  0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
28
21
  ]);
29
- // AES inverse S-box
30
22
  const SBOX_INV = new Uint8Array([
31
23
  0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
32
24
  0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
@@ -45,30 +37,23 @@ const SBOX_INV = new Uint8Array([
45
37
  0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
46
38
  0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
47
39
  ]);
48
- // Galois field multiplication in GF(2^8) with irreducible polynomial 0x11b
49
40
  function gmul(a, b) {
50
41
  let p = 0;
51
42
  for (let i = 0; i < 8; i++) {
52
- if (b & 1)
43
+ if ((b & 1) !== 0)
53
44
  p ^= a;
54
45
  const hiBit = a & 0x80;
55
46
  a = (a << 1) & 0xff;
56
- if (hiBit)
47
+ if (hiBit !== 0)
57
48
  a ^= 0x1b;
58
49
  b >>= 1;
59
50
  }
60
51
  return p;
61
52
  }
62
- // Round constant
63
53
  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
54
  function expandKey(key) {
70
- const nk = key.length / 4; // 4, 6, or 8
71
- const nr = nk + 6; // 10, 12, or 14 rounds
55
+ const nk = key.length / 4;
56
+ const nr = nk + 6;
72
57
  const nWords = (nr + 1) * 4;
73
58
  const w = new Uint8Array(nWords * 4);
74
59
  w.set(key);
@@ -77,7 +62,6 @@ function expandKey(key) {
77
62
  const temp = new Uint8Array(4);
78
63
  temp.set(t);
79
64
  if (i % nk === 0) {
80
- // RotWord + SubWord + Rcon
81
65
  const t0 = temp[0];
82
66
  temp[0] = SBOX[temp[1]] ^ RCON[i / nk - 1];
83
67
  temp[1] = SBOX[temp[2]];
@@ -85,7 +69,6 @@ function expandKey(key) {
85
69
  temp[3] = SBOX[t0];
86
70
  }
87
71
  else if (nk > 6 && i % nk === 4) {
88
- // SubWord only for AES-256
89
72
  temp[0] = SBOX[temp[0]];
90
73
  temp[1] = SBOX[temp[1]];
91
74
  temp[2] = SBOX[temp[2]];
@@ -99,43 +82,36 @@ function expandKey(key) {
99
82
  }
100
83
  return w;
101
84
  }
102
- /** XOR state with round key (in-place) */
103
85
  function addRoundKey(state, rk, round) {
104
86
  const offset = round * 16;
105
87
  for (let i = 0; i < 16; i++) {
106
88
  state[i] ^= rk[offset + i];
107
89
  }
108
90
  }
109
- /** Apply inverse S-box to each byte */
110
91
  function invSubBytes(state) {
111
92
  for (let i = 0; i < 16; i++) {
112
93
  state[i] = SBOX_INV[state[i]];
113
94
  }
114
95
  }
115
- /** Inverse shift rows (right) */
116
96
  function invShiftRows(state) {
117
97
  let t;
118
- // Row 1: shift 1 right
119
98
  t = state[13];
120
99
  state[13] = state[9];
121
100
  state[9] = state[5];
122
101
  state[5] = state[1];
123
102
  state[1] = t;
124
- // Row 2: shift 2 right
125
103
  t = state[2];
126
104
  state[2] = state[10];
127
105
  state[10] = t;
128
106
  t = state[6];
129
107
  state[6] = state[14];
130
108
  state[14] = t;
131
- // Row 3: shift 3 right (= 1 left)
132
109
  t = state[3];
133
110
  state[3] = state[7];
134
111
  state[7] = state[11];
135
112
  state[11] = state[15];
136
113
  state[15] = t;
137
114
  }
138
- /** Inverse mix columns */
139
115
  function invMixColumns(state) {
140
116
  for (let col = 0; col < 4; col++) {
141
117
  const s0 = state[col * 4];
@@ -148,7 +124,6 @@ function invMixColumns(state) {
148
124
  state[col * 4 + 3] = gmul(0x0b, s0) ^ gmul(0x0d, s1) ^ gmul(0x09, s2) ^ gmul(0x0e, s3);
149
125
  }
150
126
  }
151
- /** Decrypt a single 16-byte block in place using the expanded round key */
152
127
  function aesDecryptBlock(block, rk, nr) {
153
128
  const state = new Uint8Array(block);
154
129
  addRoundKey(state, rk, nr);
@@ -163,22 +138,15 @@ function aesDecryptBlock(block, rk, nr) {
163
138
  addRoundKey(state, rk, 0);
164
139
  block.set(state);
165
140
  }
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
141
  function aesCbcDecryptRaw(key, iv, ciphertext) {
173
- const nr = key.length / 4 + 6; // 10, 12, or 14
142
+ const nr = key.length / 4 + 6;
174
143
  const rk = expandKey(key);
175
144
  const plaintext = new Uint8Array(ciphertext.length);
176
145
  let prev = iv;
177
146
  for (let i = 0; i < ciphertext.length; i += 16) {
178
147
  const block = new Uint8Array(ciphertext.subarray(i, i + 16));
179
- const ct = new Uint8Array(block); // save ciphertext block for next IV
148
+ const ct = new Uint8Array(block);
180
149
  aesDecryptBlock(block, rk, nr);
181
- // XOR with previous ciphertext block
182
150
  for (let j = 0; j < 16; j++) {
183
151
  plaintext[i + j] = block[j] ^ prev[j];
184
152
  }
@@ -186,13 +154,6 @@ function aesCbcDecryptRaw(key, iv, ciphertext) {
186
154
  }
187
155
  return plaintext;
188
156
  }
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
157
  export const aesCbcDecrypt = (key, iv, ciphertext) => {
197
158
  if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
198
159
  return Effect.fail(new CryptoError({
@@ -1,23 +1,4 @@
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
1
  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
- */
2
+ import { CryptoError } from "../config.js";
22
3
  export declare const tripleDesCbcDecrypt: (key: Uint8Array, iv: Uint8Array, ciphertext: Uint8Array) => Effect.Effect<Uint8Array, CryptoError>;
23
4
  //# sourceMappingURL=des.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"des.d.ts","sourceRoot":"","sources":["../../src/primitives/des.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAA8C,MAAM,cAAc,CAAC;AAkOvF,eAAO,MAAM,mBAAmB,QACzB,UAAU,MACX,UAAU,cACF,UAAU,KACrB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CA8BvC,CAAC"}
@@ -1,53 +1,33 @@
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
1
  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)
2
+ import { CryptoError, CryptoErrorCodeValue, CryptoOperationValue } from "../config.js";
3
+ import { removePkcs7Padding } from "./padding.js";
18
4
  const IP = new Uint8Array([
19
5
  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
6
  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
7
  45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
22
8
  ]);
23
- // Final Permutation (IP^-1)
24
9
  const FP = new Uint8Array([
25
10
  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
11
  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
12
  42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25,
28
13
  ]);
29
- // PC-1 permutation (64 bits → 56 bits, split into C and D)
30
14
  const PC1 = new Uint8Array([
31
15
  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
16
  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
17
  13, 5, 28, 20, 12, 4,
34
18
  ]);
35
- // PC-2 permutation (56 bits → 48 bits per sub-key)
36
19
  const PC2 = new Uint8Array([
37
20
  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
21
  31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32,
39
22
  ]);
40
- // Expansion permutation E (32 bits → 48 bits)
41
23
  const E = new Uint8Array([
42
24
  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
25
  20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1,
44
26
  ]);
45
- // P permutation (after S-boxes, 32 bits → 32 bits)
46
27
  const P_TABLE = new Uint8Array([
47
28
  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
29
  30, 6, 22, 11, 4, 25,
49
30
  ]);
50
- // S-boxes: 8 boxes, each 4×16 = 64 values
51
31
  const S_BOXES = [
52
32
  new Uint8Array([
53
33
  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,
@@ -90,14 +70,11 @@ const S_BOXES = [
90
70
  15, 12, 9, 0, 3, 5, 6, 11,
91
71
  ]),
92
72
  ];
93
- // Number of left rotations per round
94
73
  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
74
  function getBit(data, pos) {
97
75
  const p = pos - 1;
98
76
  return (data[p >> 3] >> (7 - (p & 7))) & 1;
99
77
  }
100
- /** Apply a permutation to bits */
101
78
  function permute(data, table, outBits) {
102
79
  const result = new Uint8Array(Math.ceil(outBits / 8));
103
80
  for (let i = 0; i < outBits; i++) {
@@ -106,15 +83,11 @@ function permute(data, table, outBits) {
106
83
  }
107
84
  return result;
108
85
  }
109
- /** Left-rotate a 28-bit value by n positions */
110
86
  function rotateLeft28(v, n) {
111
87
  return ((v << n) | (v >>> (28 - n))) & 0x0fffffff;
112
88
  }
113
- /** Generate 16 48-bit sub-keys from a 64-bit DES key */
114
89
  function generateSubkeys(key) {
115
- // Apply PC-1
116
90
  const kp = permute(key, PC1, 56);
117
- // Split into C (bits 1-28) and D (bits 29-56) stored in 32-bit integers
118
91
  let C = 0, D = 0;
119
92
  for (let i = 0; i < 28; i++) {
120
93
  C |= getBit(kp, i + 1) << (27 - i);
@@ -124,7 +97,6 @@ function generateSubkeys(key) {
124
97
  for (let round = 0; round < 16; round++) {
125
98
  C = rotateLeft28(C, LEFT_ROTATIONS[round]);
126
99
  D = rotateLeft28(D, LEFT_ROTATIONS[round]);
127
- // Combine C and D into 7 bytes for PC-2
128
100
  const cd = new Uint8Array(7);
129
101
  for (let i = 0; i < 28; i++) {
130
102
  const bit = (C >> (27 - i)) & 1;
@@ -138,21 +110,16 @@ function generateSubkeys(key) {
138
110
  }
139
111
  return subkeys;
140
112
  }
141
- /** DES Feistel function f(R, K) */
142
113
  function feistel(R, K) {
143
- // Expand R (32 bits) to 48 bits using E table
144
114
  const expanded = permute(R, E, 48);
145
- // XOR with sub-key
146
115
  for (let i = 0; i < 6; i++) {
147
116
  expanded[i] ^= K[i];
148
117
  }
149
- // S-box substitution: 48 bits → 32 bits
150
118
  const sOut = new Uint8Array(4);
151
119
  for (let box = 0; box < 8; box++) {
152
120
  const bitPos = box * 6;
153
121
  const byteOff = bitPos >> 3;
154
122
  const bitOff = bitPos & 7;
155
- // Extract 6 bits
156
123
  let sixBits;
157
124
  if (bitOff <= 2) {
158
125
  sixBits = (expanded[byteOff] >> (2 - bitOff)) & 0x3f;
@@ -161,11 +128,9 @@ function feistel(R, K) {
161
128
  sixBits =
162
129
  ((expanded[byteOff] << (bitOff - 2)) | (expanded[byteOff + 1] >> (10 - bitOff))) & 0x3f;
163
130
  }
164
- // Row = b1 (MSB) and b6 (LSB) of the 6-bit group: row = (b1 << 1) | b6
165
131
  const row = ((sixBits >> 4) & 2) | (sixBits & 1);
166
132
  const col = (sixBits >> 1) & 0x0f;
167
133
  const val = S_BOXES[box][row * 16 + col];
168
- // Write 4 bits of output
169
134
  const outBitPos = box * 4;
170
135
  const outByteOff = outBitPos >> 3;
171
136
  const outBitOff = outBitPos & 7;
@@ -179,11 +144,6 @@ function feistel(R, K) {
179
144
  }
180
145
  return permute(sOut, P_TABLE, 32);
181
146
  }
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
147
  function desBlock(block, subkeys) {
188
148
  const perm = permute(block, IP, 64);
189
149
  let L = perm.subarray(0, 4);
@@ -197,40 +157,26 @@ function desBlock(block, subkeys) {
197
157
  L = R;
198
158
  R = newR;
199
159
  }
200
- // Final: R16 || L16
201
160
  const preOut = new Uint8Array(8);
202
161
  preOut.set(R, 0);
203
162
  preOut.set(L, 4);
204
163
  const out = permute(preOut, FP, 64);
205
164
  block.set(out);
206
165
  }
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
166
  function tripleDesCbcDecryptRaw(key, iv, ciphertext) {
216
- // Generate sub-keys for each of the three DES keys
217
167
  const sk1 = generateSubkeys(key.subarray(0, 8));
218
168
  const sk2 = generateSubkeys(key.subarray(8, 16));
219
169
  const sk3 = generateSubkeys(key.subarray(16, 24));
220
- // Reversed sub-keys for decryption (K1 and K3 are applied in reverse)
221
170
  const sk1r = [...sk1].reverse();
222
171
  const sk3r = [...sk3].reverse();
223
172
  const plaintext = new Uint8Array(ciphertext.length);
224
173
  let prev = new Uint8Array(iv);
225
174
  for (let i = 0; i < ciphertext.length; i += 8) {
226
175
  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
176
+ const ctBlock = new Uint8Array(block);
177
+ desBlock(block, sk3r);
178
+ desBlock(block, sk2);
179
+ desBlock(block, sk1r);
234
180
  for (let j = 0; j < 8; j++) {
235
181
  plaintext[i + j] = block[j] ^ prev[j];
236
182
  }
@@ -238,13 +184,6 @@ function tripleDesCbcDecryptRaw(key, iv, ciphertext) {
238
184
  }
239
185
  return plaintext;
240
186
  }
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
187
  export const tripleDesCbcDecrypt = (key, iv, ciphertext) => {
249
188
  if (key.length !== 24) {
250
189
  return Effect.fail(new CryptoError({
@@ -1,39 +1,9 @@
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
1
  import { Schema } from "effect";
8
2
  export declare const HmacHashAlgorithmSchema: Schema.Literals<readonly ["sha1", "sha256", "sha384", "sha512"]>;
9
3
  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
4
  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
5
  export type HmacContext = {
29
- /** Compute HMAC for the given message data */
30
6
  readonly compute: (data: Uint8Array) => Uint8Array;
31
7
  };
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
8
  export declare function createHmac(alg: HmacHashAlgorithm, key: Uint8Array): HmacContext;
39
9
  //# sourceMappingURL=hmac.d.ts.map
@@ -1 +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;AAsCzE;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU,CAwB1F;AAED;;;;;;;;GAQG;AAEH,MAAM,MAAM,WAAW,GAAG;IACxB,8CAA8C;IAC9C,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,UAAU,CAAC;CACpD,CAAC;AAkCF;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,GAAG,WAAW,CA4B/E"}
1
+ {"version":3,"file":"hmac.d.ts","sourceRoot":"","sources":["../../src/primitives/hmac.ts"],"names":[],"mappings":"AAAA,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;AA+BzE,wBAAgB,IAAI,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU,CAqB1F;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,UAAU,CAAC;CACpD,CAAC;AAuBF,wBAAgB,UAAU,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,GAAG,WAAW,CA2B/E"}
@@ -1,24 +1,11 @@
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
1
  import { Schema } from "effect";
8
- import { sha1 } from "./sha1";
9
- import { sha256, sha256ProcessBlock, sha256WithState } from "./sha256";
10
- import { sha384, sha512 } from "./sha512";
2
+ import { sha1 } from "./sha1.js";
3
+ import { sha256, sha256ProcessBlock, sha256WithState } from "./sha256.js";
4
+ import { sha384, sha512 } from "./sha512.js";
11
5
  export const HmacHashAlgorithmSchema = Schema.Literals(["sha1", "sha256", "sha384", "sha512"]);
12
- /**
13
- * Returns the block size (in bytes) for the given hash algorithm.
14
- * SHA-1/SHA-256: 64 bytes; SHA-384/SHA-512: 128 bytes.
15
- */
16
6
  function blockSize(alg) {
17
7
  return alg === "sha384" || alg === "sha512" ? 128 : 64;
18
8
  }
19
- /**
20
- * Dispatch to the appropriate hash function.
21
- */
22
9
  function hashFn(alg, data) {
23
10
  switch (alg) {
24
11
  case "sha1":
@@ -43,75 +30,46 @@ const normalizeHmacKey = (alg, key) => {
43
30
  normalized.set(hashed);
44
31
  return normalized;
45
32
  };
46
- /**
47
- * Compute HMAC(key, data) using the specified hash algorithm.
48
- *
49
- * @param alg - Hash algorithm: 'sha1' | 'sha256' | 'sha384' | 'sha512'
50
- * @param key - Secret key (any length)
51
- * @param data - Message data
52
- * @returns HMAC digest as Uint8Array (same length as hash output)
53
- */
54
33
  export function hmac(alg, key, data) {
55
34
  const B = blockSize(alg);
56
35
  const k = normalizeHmacKey(alg, key);
57
- // Step 2: ipad = k XOR 0x36, opad = k XOR 0x5c
58
36
  const ipad = new Uint8Array(B);
59
37
  const opad = new Uint8Array(B);
60
38
  for (let i = 0; i < B; i++) {
61
39
  ipad[i] = k[i] ^ 0x36;
62
40
  opad[i] = k[i] ^ 0x5c;
63
41
  }
64
- // Step 3: inner = H(ipad || data)
65
42
  const inner = new Uint8Array(B + data.length);
66
43
  inner.set(ipad);
67
44
  inner.set(data, B);
68
45
  const innerHash = hashFn(alg, inner);
69
- // Step 4: outer = H(opad || inner)
70
46
  const outer = new Uint8Array(B + innerHash.length);
71
47
  outer.set(opad);
72
48
  outer.set(innerHash, B);
73
49
  return hashFn(alg, outer);
74
50
  }
75
- /**
76
- * Create a precomputed HMAC context for SHA-256 with a fixed key.
77
- *
78
- * Precomputes the ipad and opad block states so each call to `compute()`
79
- * only needs to process the message data portion — saving ~2 SHA-256 block
80
- * compressions per call.
81
- */
82
51
  function createHmacSha256Context(key) {
83
- const B = 64; // SHA-256 block size
52
+ const B = 64;
84
53
  const k = normalizeHmacKey("sha256", key);
85
- // Precompute padded ipad and opad blocks
86
54
  const ipadBlock = new Uint8Array(B);
87
55
  const opadBlock = new Uint8Array(B);
88
56
  for (let i = 0; i < B; i++) {
89
57
  ipadBlock[i] = k[i] ^ 0x36;
90
58
  opadBlock[i] = k[i] ^ 0x5c;
91
59
  }
92
- // Process each block through SHA-256 to get half-states
93
60
  const ipadState = sha256ProcessBlock(ipadBlock);
94
61
  const opadState = sha256ProcessBlock(opadBlock);
95
62
  return {
96
63
  compute(data) {
97
- // inner = H(ipad_state, data) — skip the ipad block compression
98
64
  const innerHash = sha256WithState(ipadState, data, B);
99
- // outer = H(opad_state, innerHash) — skip the opad block compression
100
65
  return sha256WithState(opadState, innerHash, B);
101
66
  },
102
67
  };
103
68
  }
104
- /**
105
- * Create a precomputed HMAC context for a fixed key and algorithm.
106
- *
107
- * For SHA-256, this uses block-state precomputation for best performance.
108
- * For other algorithms, falls back to standard HMAC with cached key pads.
109
- */
110
69
  export function createHmac(alg, key) {
111
70
  if (alg === "sha256") {
112
71
  return createHmacSha256Context(key);
113
72
  }
114
- // Generic fallback: precompute padded ipad/opad (saves XOR and alloc per call)
115
73
  const B = blockSize(alg);
116
74
  const k = normalizeHmacKey(alg, key);
117
75
  const ipad = new Uint8Array(B);
@@ -1,11 +1,4 @@
1
- /**
2
- * PKCS#7 unpadding helper.
3
- *
4
- * Validates the trailing PKCS#7 padding of a decrypted block stream and returns
5
- * the plaintext with the padding removed. Invalid padding fails with a tagged
6
- * `CryptoError` at the decision point rather than throwing.
7
- */
8
1
  import { Effect } from "effect";
9
- import { CryptoError } from "../config";
2
+ import { CryptoError } from "../config.js";
10
3
  export declare const removePkcs7Padding: (data: Uint8Array, blockSize: number) => Effect.Effect<Uint8Array, CryptoError>;
11
4
  //# sourceMappingURL=padding.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"padding.d.ts","sourceRoot":"","sources":["../../src/primitives/padding.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAAwB,MAAM,WAAW,CAAC;AAE9D,eAAO,MAAM,kBAAkB,GAC7B,MAAM,UAAU,EAChB,WAAW,MAAM,KAChB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CAsBvC,CAAC"}
1
+ {"version":3,"file":"padding.d.ts","sourceRoot":"","sources":["../../src/primitives/padding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAAwB,MAAM,cAAc,CAAC;AAEjE,eAAO,MAAM,kBAAkB,SACvB,UAAU,aACL,MAAM,KAChB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,CAqBvC,CAAC"}
@@ -1,16 +1,7 @@
1
- /**
2
- * PKCS#7 unpadding helper.
3
- *
4
- * Validates the trailing PKCS#7 padding of a decrypted block stream and returns
5
- * the plaintext with the padding removed. Invalid padding fails with a tagged
6
- * `CryptoError` at the decision point rather than throwing.
7
- */
8
1
  import { Effect } from "effect";
9
- import { CryptoError, CryptoErrorCodeValue } from "../config";
2
+ import { CryptoError, CryptoErrorCodeValue } from "../config.js";
10
3
  export const removePkcs7Padding = (data, blockSize) => {
11
- if (data.length === 0)
12
- return Effect.succeed(data);
13
- const pad = data[data.length - 1];
4
+ const pad = data[data.length - 1] ?? 0;
14
5
  if (pad === 0 || pad > blockSize) {
15
6
  return Effect.fail(new CryptoError({
16
7
  code: CryptoErrorCodeValue.cipherError,
@@ -1,19 +1,3 @@
1
- /**
2
- * PBKDF2 pure TypeScript implementation (RFC 2898 / PKCS#5).
3
- *
4
- * Uses HMAC with any supported hash as the pseudo-random function.
5
- * Zero runtime dependencies. Works in any JS environment.
6
- */
7
- import { type HmacHashAlgorithm } from "./hmac";
8
- /**
9
- * Derive a key of `dkLen` bytes from a password and salt using PBKDF2.
10
- *
11
- * @param prf - Hash algorithm for HMAC PRF: 'sha1' | 'sha256' | 'sha384' | 'sha512'
12
- * @param password - Password as raw Uint8Array
13
- * @param salt - Cryptographic salt
14
- * @param iterations - Iteration count (must be >= 1)
15
- * @param dkLen - Desired derived key length in bytes
16
- * @returns Derived key as Uint8Array
17
- */
1
+ import type { HmacHashAlgorithm } from "./hmac.js";
18
2
  export declare function pbkdf2(prf: HmacHashAlgorithm, password: Uint8Array, salt: Uint8Array, iterations: number, dkLen: number): Uint8Array;
19
3
  //# sourceMappingURL=pbkdf2.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pbkdf2.d.ts","sourceRoot":"","sources":["../../src/primitives/pbkdf2.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAc,KAAK,iBAAiB,EAAQ,MAAM,QAAQ,CAAC;AAElE;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CACpB,GAAG,EAAE,iBAAiB,EACtB,QAAQ,EAAE,UAAU,EACpB,IAAI,EAAE,UAAU,EAChB,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,GACZ,UAAU,CAqBZ"}
1
+ {"version":3,"file":"pbkdf2.d.ts","sourceRoot":"","sources":["../../src/primitives/pbkdf2.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAEnD,wBAAgB,MAAM,CACpB,GAAG,EAAE,iBAAiB,EACtB,QAAQ,EAAE,UAAU,EACpB,IAAI,EAAE,UAAU,EAChB,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,GACZ,UAAU,CA2BZ"}