@sebastienrousseau/crypto-lib 0.0.8 → 0.0.9

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.
@@ -11,15 +11,6 @@ export declare function session(data: dataSessionKey): Promise<import("openpgp")
11
11
  export declare function revoke(data: dataRevoke): Promise<ArmoredKeyResult>;
12
12
  export declare function sign(data: dataSign): Promise<string>;
13
13
  export declare function verify(data: dataVerify): Promise<import("openpgp").VerifyMessageResult<GenericWebStream<T>>>;
14
- declare const _default: {
15
- decrypt: typeof decrypt;
16
- encrypt: typeof encrypt;
17
- generate: typeof generate;
18
- reformat: typeof reformat;
19
- revoke: typeof revoke;
20
- session: typeof session;
21
- sign: typeof sign;
22
- verify: typeof verify;
23
- };
24
- export default _default;
14
+ export { loadKeystore } from "../key/keystore";
15
+ export type { Keystore } from "../key/keystore";
25
16
  //# sourceMappingURL=cryptolib.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cryptolib.d.ts","sourceRoot":"","sources":["../../src/bin/cryptolib.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,cAAc,EACd,QAAQ,EACR,UAAU,EACX,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAkB3D,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW;;;GAG9C;AAkBD,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW,mBAG9C;AAwBD,wBAAsB,QAAQ,CAAC,IAAI,EAAE,YAAY,mBAGhD;AAGD,wBAAsB,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAG5E;AAoBD,wBAAsB,OAAO,CAAC,IAAI,EAAE,cAAc,yCAGjD;AAgBD,wBAAsB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAGxE;AAiBD,wBAAsB,IAAI,CAAC,IAAI,EAAE,QAAQ,mBAGxC;AAiBD,wBAAsB,MAAM,CAAC,IAAI,EAAE,UAAU,uEAG5C;;;;;;;;;;;AAOD,wBAiBE"}
1
+ {"version":3,"file":"cryptolib.d.ts","sourceRoot":"","sources":["../../src/bin/cryptolib.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,cAAc,EACd,QAAQ,EACR,UAAU,EACX,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAkB3D,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW;;;GAG9C;AAkBD,wBAAsB,OAAO,CAAC,IAAI,EAAE,WAAW,mBAG9C;AAwBD,wBAAsB,QAAQ,CAAC,IAAI,EAAE,YAAY,mBAGhD;AAGD,wBAAsB,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAG5E;AAoBD,wBAAsB,OAAO,CAAC,IAAI,EAAE,cAAc,yCAGjD;AAgBD,wBAAsB,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAGxE;AAiBD,wBAAsB,IAAI,CAAC,IAAI,EAAE,QAAQ,mBAGxC;AAiBD,wBAAsB,MAAM,CAAC,IAAI,EAAE,UAAU,uEAG5C;AAOD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,YAAY,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC"}
@@ -33,6 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.loadKeystore = void 0;
36
37
  exports.decrypt = decrypt;
37
38
  exports.encrypt = encrypt;
38
39
  exports.generate = generate;
@@ -74,14 +75,6 @@ async function verify(data) {
74
75
  const verify = await key.verify.default(data);
75
76
  return verify;
76
77
  }
77
- exports.default = {
78
- decrypt,
79
- encrypt,
80
- generate,
81
- reformat,
82
- revoke,
83
- session,
84
- sign,
85
- verify,
86
- };
78
+ var keystore_1 = require("../key/keystore");
79
+ Object.defineProperty(exports, "loadKeystore", { enumerable: true, get: function () { return keystore_1.loadKeystore; } });
87
80
  //# sourceMappingURL=cryptolib.js.map
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import CryptoLib from "./bin/cryptolib";
2
- export default CryptoLib;
1
+ export * as pgp from "./bin/cryptolib";
3
2
  export * from "./modern";
4
3
  export * from "./high-level";
5
4
  export * from "./keys";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,SAAS,MAAM,iBAAiB,CAAC;AAGxC,eAAe,SAAS,CAAC;AAGzB,cAAc,UAAU,CAAC;AAGzB,cAAc,cAAc,CAAC;AAG7B,cAAc,QAAQ,CAAC;AAGvB,cAAc,aAAa,CAAC;AAG5B,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,cAAc,sBAAsB,CAAC;AAGrC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AAGnC,cAAc,SAAS,CAAC;AAGxB,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,YAAY,EACV,aAAa,EACb,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,UAAU,CAAC;AAGlB,OAAO,EACL,YAAY,EACZ,cAAc,EACd,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,aAAa,GACd,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAGxD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,GAAG,MAAM,iBAAiB,CAAC;AAGvC,cAAc,UAAU,CAAC;AAGzB,cAAc,cAAc,CAAC;AAG7B,cAAc,QAAQ,CAAC;AAGvB,cAAc,aAAa,CAAC;AAG5B,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,cAAc,sBAAsB,CAAC;AAGrC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AAGnC,cAAc,SAAS,CAAC;AAGxB,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,YAAY,EACV,aAAa,EACb,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,UAAU,CAAC;AAGlB,OAAO,EACL,YAAY,EACZ,cAAc,EACd,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,aAAa,GACd,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAGxD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -15,9 +15,6 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
- };
21
18
  var __importStar = (this && this.__importStar) || (function () {
22
19
  var ownKeys = function(o) {
23
20
  ownKeys = Object.getOwnPropertyNames || function (o) {
@@ -35,13 +32,12 @@ var __importStar = (this && this.__importStar) || (function () {
35
32
  return result;
36
33
  };
37
34
  })();
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
35
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
40
37
  };
41
38
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.wipeMemory = exports.SecureBuffer = exports.timingSafeEqual = exports.CryptoErrorCode = exports.CryptoError = exports.isDeprecated = exports.recommended = exports.listAlgorithms = exports.getAlgorithm = exports.crypto = exports.tokens = exports.protocols = void 0;
43
- const cryptolib_1 = __importDefault(require("./bin/cryptolib"));
44
- exports.default = cryptolib_1.default;
39
+ exports.wipeMemory = exports.SecureBuffer = exports.timingSafeEqual = exports.CryptoErrorCode = exports.CryptoError = exports.isDeprecated = exports.recommended = exports.listAlgorithms = exports.getAlgorithm = exports.crypto = exports.tokens = exports.protocols = exports.pgp = void 0;
40
+ exports.pgp = __importStar(require("./bin/cryptolib"));
45
41
  __exportStar(require("./modern"), exports);
46
42
  __exportStar(require("./high-level"), exports);
47
43
  __exportStar(require("./keys"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"keygen.d.ts","sourceRoot":"","sources":["../../src/keys/keygen.ts"],"names":[],"mappings":"AA4BA,eAAO,MAAM,cAAc,mJAajB,CAAC;AAGX,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAG3D,MAAM,WAAW,WAAW;IAE1B,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEzB,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAC;IAEhC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B;AAGD,MAAM,WAAW,gBAAgB;IAE/B,SAAS,EAAE,MAAM,CAAC;IAElB,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,YAAY,CAAC;IAExB,GAAG,EAAE,MAAM,CAAC;IAEZ,QAAQ,EAAE,WAAW,CAAC;CACvB;AAuBD,wBAAgB,eAAe,CAC7B,SAAS,EAAE,YAAY,EACvB,QAAQ,GAAE,WAAgB,GACzB,gBAAgB,CAkGlB"}
1
+ {"version":3,"file":"keygen.d.ts","sourceRoot":"","sources":["../../src/keys/keygen.ts"],"names":[],"mappings":"AA4BA,eAAO,MAAM,cAAc,mJAajB,CAAC;AAGX,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAG3D,MAAM,WAAW,WAAW;IAE1B,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEzB,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAC;IAEhC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B;AAGD,MAAM,WAAW,gBAAgB;IAE/B,SAAS,EAAE,MAAM,CAAC;IAElB,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,YAAY,CAAC;IAExB,GAAG,EAAE,MAAM,CAAC;IAEZ,QAAQ,EAAE,WAAW,CAAC;CACvB;AAsDD,wBAAgB,eAAe,CAC7B,SAAS,EAAE,YAAY,EACvB,QAAQ,GAAE,WAAgB,GACzB,gBAAgB,CAiBlB"}
@@ -31,91 +31,27 @@ function generateKid(publicKey) {
31
31
  const digest = (0, sha2_js_1.sha256)(publicKey);
32
32
  return (0, serialize_1.bytesToBase64url)(digest.subarray(0, 16));
33
33
  }
34
+ const fromSecret = (privateKey, derive) => ({ publicKey: derive(privateKey), privateKey });
35
+ const fromPq = (kp) => ({ publicKey: kp.publicKey, privateKey: kp.secretKey });
36
+ const KEYGEN = {
37
+ ed25519: () => fromSecret(ed25519_js_1.ed25519.utils.randomSecretKey(), ed25519_js_1.ed25519.getPublicKey),
38
+ x25519: () => fromSecret((0, utils_js_1.randomBytes)(32), ed25519_js_1.x25519.getPublicKey),
39
+ ed448: () => fromSecret(ed448_js_1.ed448.utils.randomSecretKey(), ed448_js_1.ed448.getPublicKey),
40
+ x448: () => fromSecret((0, utils_js_1.randomBytes)(56), ed448_js_1.x448.getPublicKey),
41
+ p256: () => fromSecret(nist_js_1.p256.utils.randomSecretKey(), nist_js_1.p256.getPublicKey),
42
+ p384: () => fromSecret(nist_js_1.p384.utils.randomSecretKey(), nist_js_1.p384.getPublicKey),
43
+ "ml-kem-512": () => fromPq(ml_kem_js_1.ml_kem512.keygen()),
44
+ "ml-kem-768": () => fromPq(ml_kem_js_1.ml_kem768.keygen()),
45
+ "ml-kem-1024": () => fromPq(ml_kem_js_1.ml_kem1024.keygen()),
46
+ "ml-dsa-44": () => fromPq(ml_dsa_js_1.ml_dsa44.keygen()),
47
+ "ml-dsa-65": () => fromPq(ml_dsa_js_1.ml_dsa65.keygen()),
48
+ "ml-dsa-87": () => fromPq(ml_dsa_js_1.ml_dsa87.keygen()),
49
+ };
34
50
  function generateKeyPair(algorithm, metadata = {}) {
35
- let publicKey;
36
- let privateKey;
37
- switch (algorithm) {
38
- case "ed25519": {
39
- const priv = ed25519_js_1.ed25519.utils.randomSecretKey();
40
- const pub = ed25519_js_1.ed25519.getPublicKey(priv);
41
- privateKey = priv;
42
- publicKey = pub;
43
- break;
44
- }
45
- case "x25519": {
46
- const priv = (0, utils_js_1.randomBytes)(32);
47
- const pub = ed25519_js_1.x25519.getPublicKey(priv);
48
- privateKey = priv;
49
- publicKey = pub;
50
- break;
51
- }
52
- case "ed448": {
53
- const priv = ed448_js_1.ed448.utils.randomSecretKey();
54
- const pub = ed448_js_1.ed448.getPublicKey(priv);
55
- privateKey = priv;
56
- publicKey = pub;
57
- break;
58
- }
59
- case "x448": {
60
- const priv = (0, utils_js_1.randomBytes)(56);
61
- const pub = ed448_js_1.x448.getPublicKey(priv);
62
- privateKey = priv;
63
- publicKey = pub;
64
- break;
65
- }
66
- case "p256": {
67
- const priv = nist_js_1.p256.utils.randomSecretKey();
68
- const pub = nist_js_1.p256.getPublicKey(priv);
69
- privateKey = priv;
70
- publicKey = pub;
71
- break;
72
- }
73
- case "p384": {
74
- const priv = nist_js_1.p384.utils.randomSecretKey();
75
- const pub = nist_js_1.p384.getPublicKey(priv);
76
- privateKey = priv;
77
- publicKey = pub;
78
- break;
79
- }
80
- case "ml-kem-512": {
81
- const kp = ml_kem_js_1.ml_kem512.keygen();
82
- publicKey = kp.publicKey;
83
- privateKey = kp.secretKey;
84
- break;
85
- }
86
- case "ml-kem-768": {
87
- const kp = ml_kem_js_1.ml_kem768.keygen();
88
- publicKey = kp.publicKey;
89
- privateKey = kp.secretKey;
90
- break;
91
- }
92
- case "ml-kem-1024": {
93
- const kp = ml_kem_js_1.ml_kem1024.keygen();
94
- publicKey = kp.publicKey;
95
- privateKey = kp.secretKey;
96
- break;
97
- }
98
- case "ml-dsa-44": {
99
- const kp = ml_dsa_js_1.ml_dsa44.keygen();
100
- publicKey = kp.publicKey;
101
- privateKey = kp.secretKey;
102
- break;
103
- }
104
- case "ml-dsa-65": {
105
- const kp = ml_dsa_js_1.ml_dsa65.keygen();
106
- publicKey = kp.publicKey;
107
- privateKey = kp.secretKey;
108
- break;
109
- }
110
- case "ml-dsa-87": {
111
- const kp = ml_dsa_js_1.ml_dsa87.keygen();
112
- publicKey = kp.publicKey;
113
- privateKey = kp.secretKey;
114
- break;
115
- }
116
- default:
117
- throw new Error(`Unsupported algorithm: ${algorithm}. Supported: ${exports.KEY_ALGORITHMS.join(", ")}`);
51
+ if (!Object.hasOwn(KEYGEN, algorithm)) {
52
+ throw new Error(`Unsupported algorithm: ${algorithm}. Supported: ${exports.KEY_ALGORITHMS.join(", ")}`);
118
53
  }
54
+ const { publicKey, privateKey } = KEYGEN[algorithm]();
119
55
  const kid = metadata.kid ?? generateKid(publicKey);
120
56
  return {
121
57
  publicKey: toHex(publicKey),
@@ -30,6 +30,7 @@ export interface PasetoPayload {
30
30
  footer?: string;
31
31
  }
32
32
  export declare function pae(...pieces: Uint8Array[]): Uint8Array;
33
+ export declare function localEncryptWithNonce(opts: PasetoLocalEncryptOptions, nonce: Uint8Array): PasetoToken;
33
34
  declare function localEncrypt(opts: PasetoLocalEncryptOptions): PasetoToken;
34
35
  declare function localDecrypt(opts: PasetoLocalDecryptOptions): PasetoPayload;
35
36
  declare function publicSign(opts: PasetoPublicSignOptions): PasetoToken;
@@ -1 +1 @@
1
- {"version":3,"file":"paseto.d.ts","sourceRoot":"","sources":["../../src/tokens/paseto.ts"],"names":[],"mappings":"AAqDA,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,uBAAuB;IAEtC,SAAS,EAAE,MAAM,CAAC;IAElB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,SAAS,EAAE,MAAM,CAAC;IAElB,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,WAAW;IAE1B,KAAK,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,aAAa;IAE5B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAkBD,wBAAgB,GAAG,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CAoBvD;AAoCD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,WAAW,CA+ClE;AAeD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,aAAa,CA4DpE;AAiBD,iBAAS,UAAU,CAAC,IAAI,EAAE,uBAAuB,GAAG,WAAW,CA8B9D;AAcD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,aAAa,CAkDpE;AAaD,eAAO,MAAM,OAAO;;;CAKV,CAAC;AAWX,eAAO,MAAM,QAAQ;;;CAKX,CAAC"}
1
+ {"version":3,"file":"paseto.d.ts","sourceRoot":"","sources":["../../src/tokens/paseto.ts"],"names":[],"mappings":"AAyDA,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,uBAAuB;IAEtC,SAAS,EAAE,MAAM,CAAC;IAElB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,SAAS,EAAE,MAAM,CAAC;IAElB,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,WAAW;IAE1B,KAAK,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,aAAa;IAE5B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAkBD,wBAAgB,GAAG,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CAoBvD;AA+FD,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,yBAAyB,EAC/B,KAAK,EAAE,UAAU,GAChB,WAAW,CA2Bb;AAiBD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,WAAW,CAElE;AAyBD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,aAAa,CAqCpE;AAiBD,iBAAS,UAAU,CAAC,IAAI,EAAE,uBAAuB,GAAG,WAAW,CA8B9D;AAcD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,aAAa,CA0CpE;AAaD,eAAO,MAAM,OAAO;;;CAKV,CAAC;AAWX,eAAO,MAAM,QAAQ;;;CAKX,CAAC"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.v4public = exports.v4local = void 0;
4
4
  exports.pae = pae;
5
+ exports.localEncryptWithNonce = localEncryptWithNonce;
5
6
  const chacha_js_1 = require("@noble/ciphers/chacha.js");
6
7
  const utils_js_1 = require("@noble/ciphers/utils.js");
7
8
  const ed25519_js_1 = require("@noble/curves/ed25519.js");
@@ -10,8 +11,10 @@ const V4_LOCAL_HEADER = "v4.local.";
10
11
  const V4_PUBLIC_HEADER = "v4.public.";
11
12
  const NONCE_LEN = 32;
12
13
  const EK_LEN = 32;
13
- const N3_LEN = 24;
14
+ const N2_LEN = 24;
15
+ const TAG_LEN = 32;
14
16
  const SIG_LEN = 64;
17
+ const BASE64URL_RE = /^[A-Za-z0-9_-]*$/;
15
18
  const HEX_RE = /^[0-9a-fA-F]*$/;
16
19
  function pae(...pieces) {
17
20
  const le64 = (n) => {
@@ -43,88 +46,90 @@ function toBase64url(buf) {
43
46
  return Buffer.from(buf).toString("base64url");
44
47
  }
45
48
  function fromBase64url(s) {
46
- const padded = s.replace(/-/g, "+").replace(/_/g, "/");
47
- return Buffer.from(padded, "base64");
49
+ const bytes = Buffer.from(s, "base64url");
50
+ if (!BASE64URL_RE.test(s) || bytes.toString("base64url") !== s) {
51
+ throw new Error("Invalid base64url encoding");
52
+ }
53
+ return bytes;
48
54
  }
49
55
  const encoder = new TextEncoder();
50
- function localEncrypt(opts) {
51
- const { key, payload, footer = "", implicit = "" } = opts;
56
+ function localKey(key) {
52
57
  const keyBytes = hexToBytes(key);
53
58
  if (keyBytes.length !== 32) {
54
59
  throw new Error(`Key must be 32 bytes, got ${keyBytes.length}`);
55
60
  }
56
- const header = encoder.encode(V4_LOCAL_HEADER);
57
- const message = encoder.encode(JSON.stringify(payload));
61
+ return keyBytes;
62
+ }
63
+ function deriveFromNonce(key, info, nonce, dkLen) {
64
+ const label = encoder.encode(info);
65
+ const msg = new Uint8Array(label.length + nonce.length);
66
+ msg.set(label);
67
+ msg.set(nonce, label.length);
68
+ return (0, blake2_js_1.blake2b)(msg, { key, dkLen });
69
+ }
70
+ function localSubkeys(key, nonce) {
71
+ const tmp = deriveFromNonce(key, "paseto-encryption-key", nonce, EK_LEN + N2_LEN);
72
+ return {
73
+ ek: tmp.subarray(0, EK_LEN),
74
+ n2: tmp.subarray(EK_LEN),
75
+ ak: deriveFromNonce(key, "paseto-auth-key-for-aead", nonce, TAG_LEN),
76
+ };
77
+ }
78
+ function localTag(ak, nonce, ciphertext, footer, implicit) {
79
+ const preAuth = pae(encoder.encode(V4_LOCAL_HEADER), nonce, ciphertext, footer, implicit);
80
+ return (0, blake2_js_1.blake2b)(preAuth, { key: ak, dkLen: TAG_LEN });
81
+ }
82
+ function localEncryptWithNonce(opts, nonce) {
83
+ const { key, payload, footer = "", implicit = "" } = opts;
84
+ const keyBytes = localKey(key);
85
+ if (nonce.length !== NONCE_LEN) {
86
+ throw new Error(`Nonce must be ${NONCE_LEN} bytes, got ${nonce.length}`);
87
+ }
58
88
  const footerBytes = encoder.encode(footer);
59
- const implicitBytes = encoder.encode(implicit);
60
- const randomN = (0, utils_js_1.randomBytes)(NONCE_LEN);
61
- const nonce = (0, blake2_js_1.blake2b)(message, { key: randomN, dkLen: NONCE_LEN });
62
- const ekInput = new Uint8Array(keyBytes.length + 16);
63
- ekInput.set(keyBytes);
64
- ekInput.set(nonce.subarray(0, 16), keyBytes.length);
65
- const derived = (0, blake2_js_1.blake2b)(encoder.encode("paseto-encryption-key"), {
66
- key: ekInput,
67
- dkLen: EK_LEN + N3_LEN,
68
- });
69
- const ek = derived.subarray(0, EK_LEN);
70
- const n3 = derived.subarray(EK_LEN, EK_LEN + N3_LEN);
71
- const aad = pae(header, nonce, footerBytes, implicitBytes);
72
- const cipher = (0, chacha_js_1.xchacha20poly1305)(ek, n3, aad);
73
- const ciphertext = cipher.encrypt(message);
74
- const body = new Uint8Array(NONCE_LEN + ciphertext.length);
89
+ const { ek, n2, ak } = localSubkeys(keyBytes, nonce);
90
+ const ciphertext = (0, chacha_js_1.xchacha20)(ek, n2, encoder.encode(JSON.stringify(payload)));
91
+ const tag = localTag(ak, nonce, ciphertext, footerBytes, encoder.encode(implicit));
92
+ const body = new Uint8Array(NONCE_LEN + ciphertext.length + TAG_LEN);
75
93
  body.set(nonce);
76
94
  body.set(ciphertext, NONCE_LEN);
95
+ body.set(tag, NONCE_LEN + ciphertext.length);
77
96
  let token = V4_LOCAL_HEADER + toBase64url(body);
78
97
  if (footer) {
79
98
  token += "." + toBase64url(footerBytes);
80
99
  }
81
100
  return { token };
82
101
  }
102
+ function localEncrypt(opts) {
103
+ return localEncryptWithNonce(opts, (0, utils_js_1.randomBytes)(NONCE_LEN));
104
+ }
105
+ function assertFooter(footerB64, footer) {
106
+ const expected = encoder.encode(footer);
107
+ if (!(0, utils_js_1.equalBytes)(fromBase64url(footerB64), expected)) {
108
+ throw new Error("Footer mismatch");
109
+ }
110
+ }
83
111
  function localDecrypt(opts) {
84
112
  const { key, token, footer = "", implicit = "" } = opts;
85
- const keyBytes = hexToBytes(key);
86
- if (keyBytes.length !== 32) {
87
- throw new Error(`Key must be 32 bytes, got ${keyBytes.length}`);
88
- }
113
+ const keyBytes = localKey(key);
89
114
  if (!token.startsWith(V4_LOCAL_HEADER)) {
90
115
  throw new Error(`Invalid token header: expected "${V4_LOCAL_HEADER}"`);
91
116
  }
92
- const withoutHeader = token.slice(V4_LOCAL_HEADER.length);
93
- const parts = withoutHeader.split(".");
94
- const bodyB64 = parts[0];
95
- const footerB64 = parts[1] ?? "";
96
- const footerBytes = encoder.encode(footer);
97
- if (footerB64) {
98
- const tokenFooter = fromBase64url(footerB64);
99
- if (Buffer.from(tokenFooter).toString("utf8") !== footer) {
100
- throw new Error("Footer mismatch");
101
- }
102
- }
103
- else if (footer) {
104
- throw new Error("Footer mismatch");
105
- }
106
- const body = fromBase64url(bodyB64);
107
- if (body.length < NONCE_LEN + 16) {
117
+ const parts = token.slice(V4_LOCAL_HEADER.length).split(".");
118
+ assertFooter(parts[1] ?? "", footer);
119
+ const body = fromBase64url(parts[0]);
120
+ if (body.length < NONCE_LEN + TAG_LEN) {
108
121
  throw new Error("Token body too short");
109
122
  }
110
- const header = encoder.encode(V4_LOCAL_HEADER);
111
- const implicitBytes = encoder.encode(implicit);
112
123
  const nonce = body.subarray(0, NONCE_LEN);
113
- const ciphertext = body.subarray(NONCE_LEN);
114
- const ekInput = new Uint8Array(keyBytes.length + 16);
115
- ekInput.set(keyBytes);
116
- ekInput.set(nonce.subarray(0, 16), keyBytes.length);
117
- const derived = (0, blake2_js_1.blake2b)(encoder.encode("paseto-encryption-key"), {
118
- key: ekInput,
119
- dkLen: EK_LEN + N3_LEN,
120
- });
121
- const ek = derived.subarray(0, EK_LEN);
122
- const n3 = derived.subarray(EK_LEN, EK_LEN + N3_LEN);
123
- const aad = pae(header, nonce, footerBytes, implicitBytes);
124
- const cipher = (0, chacha_js_1.xchacha20poly1305)(ek, n3, aad);
125
- const plaintext = cipher.decrypt(ciphertext);
126
- const payloadStr = Buffer.from(plaintext).toString("utf8");
127
- const payload = JSON.parse(payloadStr);
124
+ const ciphertext = body.subarray(NONCE_LEN, body.length - TAG_LEN);
125
+ const tag = body.subarray(body.length - TAG_LEN);
126
+ const { ek, n2, ak } = localSubkeys(keyBytes, nonce);
127
+ const expected = localTag(ak, nonce, ciphertext, encoder.encode(footer), encoder.encode(implicit));
128
+ if (!(0, utils_js_1.equalBytes)(tag, expected)) {
129
+ throw new Error("Invalid token: authentication failed");
130
+ }
131
+ const plaintext = (0, chacha_js_1.xchacha20)(ek, n2, ciphertext);
132
+ const payload = JSON.parse(Buffer.from(plaintext).toString("utf8"));
128
133
  return { payload, ...(footer ? { footer } : {}) };
129
134
  }
130
135
  function publicSign(opts) {
@@ -162,16 +167,8 @@ function publicVerify(opts) {
162
167
  const parts = withoutHeader.split(".");
163
168
  const bodyB64 = parts[0];
164
169
  const footerB64 = parts[1] ?? "";
170
+ assertFooter(footerB64, footer);
165
171
  const footerBytes = encoder.encode(footer);
166
- if (footerB64) {
167
- const tokenFooter = fromBase64url(footerB64);
168
- if (Buffer.from(tokenFooter).toString("utf8") !== footer) {
169
- throw new Error("Footer mismatch");
170
- }
171
- }
172
- else if (footer) {
173
- throw new Error("Footer mismatch");
174
- }
175
172
  const body = fromBase64url(bodyB64);
176
173
  if (body.length < SIG_LEN) {
177
174
  throw new Error("Token body too short");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebastienrousseau/crypto-lib",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Crypto Lib is a powerful intuitive cryptographic JavaScript library that encapsulates common algorithms, functions and provides an interface for low-level cryptographic operations.",
5
5
  "author": "Sebastien Rousseau <sebastienrousseau@users.noreply.github.com>",
6
6
  "license": "MIT OR Apache-2.0",
@@ -16,12 +16,86 @@
16
16
  "engines": {
17
17
  "node": ">=22.0.0"
18
18
  },
19
+ "type": "commonjs",
19
20
  "main": "./dist/index.js",
20
21
  "types": "./dist/index.d.ts",
21
- "sideEffects": false,
22
- "bin": {
23
- "cryptolib": "./dist/index.js"
22
+ "typesVersions": {
23
+ "*": {
24
+ "modern": [
25
+ "./dist/modern/index.d.ts"
26
+ ],
27
+ "high-level": [
28
+ "./dist/high-level/index.d.ts"
29
+ ],
30
+ "keys": [
31
+ "./dist/keys/index.d.ts"
32
+ ],
33
+ "streaming": [
34
+ "./dist/streaming/index.d.ts"
35
+ ],
36
+ "protocols": [
37
+ "./dist/protocols/index.d.ts"
38
+ ],
39
+ "tokens": [
40
+ "./dist/tokens/index.d.ts"
41
+ ],
42
+ "accel": [
43
+ "./dist/accel/index.d.ts"
44
+ ],
45
+ "pgp": [
46
+ "./dist/bin/cryptolib.d.ts"
47
+ ]
48
+ }
49
+ },
50
+ "exports": {
51
+ ".": {
52
+ "types": "./dist/index.d.ts",
53
+ "require": "./dist/index.js",
54
+ "default": "./dist/index.js"
55
+ },
56
+ "./modern": {
57
+ "types": "./dist/modern/index.d.ts",
58
+ "require": "./dist/modern/index.js",
59
+ "default": "./dist/modern/index.js"
60
+ },
61
+ "./high-level": {
62
+ "types": "./dist/high-level/index.d.ts",
63
+ "require": "./dist/high-level/index.js",
64
+ "default": "./dist/high-level/index.js"
65
+ },
66
+ "./keys": {
67
+ "types": "./dist/keys/index.d.ts",
68
+ "require": "./dist/keys/index.js",
69
+ "default": "./dist/keys/index.js"
70
+ },
71
+ "./streaming": {
72
+ "types": "./dist/streaming/index.d.ts",
73
+ "require": "./dist/streaming/index.js",
74
+ "default": "./dist/streaming/index.js"
75
+ },
76
+ "./protocols": {
77
+ "types": "./dist/protocols/index.d.ts",
78
+ "require": "./dist/protocols/index.js",
79
+ "default": "./dist/protocols/index.js"
80
+ },
81
+ "./tokens": {
82
+ "types": "./dist/tokens/index.d.ts",
83
+ "require": "./dist/tokens/index.js",
84
+ "default": "./dist/tokens/index.js"
85
+ },
86
+ "./accel": {
87
+ "types": "./dist/accel/index.d.ts",
88
+ "require": "./dist/accel/index.js",
89
+ "default": "./dist/accel/index.js"
90
+ },
91
+ "./pgp": {
92
+ "types": "./dist/bin/cryptolib.d.ts",
93
+ "require": "./dist/bin/cryptolib.js",
94
+ "default": "./dist/bin/cryptolib.js"
95
+ },
96
+ "./package.json": "./package.json"
24
97
  },
98
+ "sideEffects": false,
25
99
  "files": [
26
100
  "dist/**/*.js",
27
101
  "dist/**/*.d.ts",
@@ -36,15 +110,16 @@
36
110
  "openpgp": "^6.3.1"
37
111
  },
38
112
  "devDependencies": {
39
- "@types/chai": "^4.3.7",
113
+ "@types/chai": "^5.2.3",
40
114
  "@types/chai-as-promised": "^8.0.2",
41
- "@types/mocha": "^10.0.2",
115
+ "@types/mocha": "^10.0.10",
42
116
  "@types/node": "^26.6.3",
43
117
  "c8": "^12.0.0",
44
- "chai": "^4.5.0",
45
- "chai-as-promised": "^7.1.2",
46
- "mocha": "^10.8.2",
47
- "prettier": "^3.9.8",
118
+ "chai": "^6.3.0",
119
+ "chai-as-promised": "^8.0.2",
120
+ "fast-check": "^4.10.2",
121
+ "mocha": "^12.0.3",
122
+ "prettier": "^3.9.9",
48
123
  "rimraf": "^6.1.3",
49
124
  "ts-node": "^10.9.2",
50
125
  "typedoc": "^0.28.20",
@@ -66,8 +141,8 @@
66
141
  "scripts": {
67
142
  "build": "tsc --build tsconfig.json",
68
143
  "clean": "rimraf ./coverage ./dist ./docs",
69
- "lint": "eslint --ext .ts src __tests__",
70
- "lint:fix": "eslint --cache --fix --ext .ts src __tests__",
144
+ "lint": "eslint src __tests__",
145
+ "lint:fix": "eslint --cache --fix src __tests__",
71
146
  "format": "prettier --write src/**/*.ts",
72
147
  "format:check": "prettier --check \"src/**/*.ts\"",
73
148
  "start": "node dist/index.js",