@enbox/crypto 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -102
- package/dist/browser.js +6 -10
- package/dist/browser.js.map +4 -4
- package/dist/browser.mjs +6 -10
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/algorithms/aes-gcm.js +1 -1
- package/dist/esm/algorithms/aes-gcm.js.map +1 -1
- package/dist/esm/algorithms/ecdsa.js +9 -5
- package/dist/esm/algorithms/ecdsa.js.map +1 -1
- package/dist/esm/algorithms/eddsa.js +9 -5
- package/dist/esm/algorithms/eddsa.js.map +1 -1
- package/dist/esm/algorithms/sha-2.js +1 -1
- package/dist/esm/algorithms/sha-2.js.map +1 -1
- package/dist/esm/crypto-error.js +41 -0
- package/dist/esm/crypto-error.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jose/jwk.js +52 -26
- package/dist/esm/jose/jwk.js.map +1 -1
- package/dist/esm/local-key-manager.js +3 -2
- package/dist/esm/local-key-manager.js.map +1 -1
- package/dist/esm/primitives/aes-ctr.js.map +1 -1
- package/dist/esm/primitives/aes-gcm.js.map +1 -1
- package/dist/esm/primitives/aes-kw.js +246 -0
- package/dist/esm/primitives/aes-kw.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +1 -1
- package/dist/esm/primitives/concat-kdf.js.map +1 -1
- package/dist/esm/primitives/ed25519.js +3 -3
- package/dist/esm/primitives/ed25519.js.map +1 -1
- package/dist/esm/primitives/hkdf.js +79 -0
- package/dist/esm/primitives/hkdf.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +49 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -1
- package/dist/esm/primitives/secp256k1.js +4 -4
- package/dist/esm/primitives/secp256k1.js.map +1 -1
- package/dist/esm/primitives/secp256r1.js +4 -4
- package/dist/esm/primitives/secp256r1.js.map +1 -1
- package/dist/esm/primitives/x25519.js +1 -1
- package/dist/esm/primitives/x25519.js.map +1 -1
- package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
- package/dist/esm/primitives/xchacha20.js +1 -1
- package/dist/esm/primitives/xchacha20.js.map +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
- package/dist/types/algorithms/aes-gcm.d.ts +2 -2
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
- package/dist/types/algorithms/ecdsa.d.ts +1 -1
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
- package/dist/types/algorithms/eddsa.d.ts +2 -2
- package/dist/types/algorithms/eddsa.d.ts.map +1 -1
- package/dist/types/algorithms/sha-2.d.ts +1 -1
- package/dist/types/algorithms/sha-2.d.ts.map +1 -1
- package/dist/types/crypto-error.d.ts +29 -0
- package/dist/types/crypto-error.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/jose/jwk.d.ts.map +1 -1
- package/dist/types/local-key-manager.d.ts +3 -3
- package/dist/types/local-key-manager.d.ts.map +1 -1
- package/dist/types/primitives/aes-kw.d.ts +103 -0
- package/dist/types/primitives/aes-kw.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +1 -1
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
- package/dist/types/primitives/hkdf.d.ts +90 -0
- package/dist/types/primitives/hkdf.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +58 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
- package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
- package/dist/types/types/cipher.d.ts +1 -1
- package/dist/types/types/crypto-api.d.ts +3 -3
- package/dist/types/types/crypto-api.d.ts.map +1 -1
- package/dist/types/types/params-direct.d.ts +79 -1
- package/dist/types/types/params-direct.d.ts.map +1 -1
- package/dist/utils.js.map +2 -2
- package/package.json +21 -34
- package/src/algorithms/aes-ctr.ts +1 -1
- package/src/algorithms/aes-gcm.ts +3 -2
- package/src/algorithms/ecdsa.ts +13 -7
- package/src/algorithms/eddsa.ts +9 -9
- package/src/algorithms/sha-2.ts +2 -2
- package/src/crypto-error.ts +45 -0
- package/src/index.ts +3 -0
- package/src/jose/jwk.ts +32 -32
- package/src/local-key-manager.ts +14 -13
- package/src/primitives/aes-ctr.ts +1 -1
- package/src/primitives/aes-gcm.ts +5 -5
- package/src/primitives/aes-kw.ts +269 -0
- package/src/primitives/concat-kdf.ts +4 -2
- package/src/primitives/ed25519.ts +6 -6
- package/src/primitives/hkdf.ts +121 -0
- package/src/primitives/pbkdf2.ts +91 -0
- package/src/primitives/secp256k1.ts +6 -6
- package/src/primitives/secp256r1.ts +6 -6
- package/src/primitives/x25519.ts +3 -3
- package/src/primitives/xchacha20-poly1305.ts +57 -4
- package/src/primitives/xchacha20.ts +1 -1
- package/src/types/cipher.ts +1 -1
- package/src/types/crypto-api.ts +5 -5
- package/src/types/params-direct.ts +97 -1
- package/src/utils.ts +2 -2
- package/dist/cjs/algorithms/aes-ctr.js +0 -188
- package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
- package/dist/cjs/algorithms/aes-gcm.js +0 -196
- package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
- package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
- package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
- package/dist/cjs/algorithms/ecdsa.js +0 -352
- package/dist/cjs/algorithms/ecdsa.js.map +0 -1
- package/dist/cjs/algorithms/eddsa.js +0 -325
- package/dist/cjs/algorithms/eddsa.js.map +0 -1
- package/dist/cjs/algorithms/sha-2.js +0 -119
- package/dist/cjs/algorithms/sha-2.js.map +0 -1
- package/dist/cjs/index.js +0 -41
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/jose/jwe.js +0 -3
- package/dist/cjs/jose/jwe.js.map +0 -1
- package/dist/cjs/jose/jwk.js +0 -278
- package/dist/cjs/jose/jwk.js.map +0 -1
- package/dist/cjs/jose/jws.js +0 -3
- package/dist/cjs/jose/jws.js.map +0 -1
- package/dist/cjs/jose/jwt.js +0 -3
- package/dist/cjs/jose/jwt.js.map +0 -1
- package/dist/cjs/jose/utils.js +0 -60
- package/dist/cjs/jose/utils.js.map +0 -1
- package/dist/cjs/local-key-manager.js +0 -521
- package/dist/cjs/local-key-manager.js.map +0 -1
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/primitives/aes-ctr.js +0 -398
- package/dist/cjs/primitives/aes-ctr.js.map +0 -1
- package/dist/cjs/primitives/aes-gcm.js +0 -425
- package/dist/cjs/primitives/aes-gcm.js.map +0 -1
- package/dist/cjs/primitives/concat-kdf.js +0 -215
- package/dist/cjs/primitives/concat-kdf.js.map +0 -1
- package/dist/cjs/primitives/ed25519.js +0 -651
- package/dist/cjs/primitives/ed25519.js.map +0 -1
- package/dist/cjs/primitives/pbkdf2.js +0 -120
- package/dist/cjs/primitives/pbkdf2.js.map +0 -1
- package/dist/cjs/primitives/secp256k1.js +0 -958
- package/dist/cjs/primitives/secp256k1.js.map +0 -1
- package/dist/cjs/primitives/secp256r1.js +0 -959
- package/dist/cjs/primitives/secp256r1.js.map +0 -1
- package/dist/cjs/primitives/sha256.js +0 -93
- package/dist/cjs/primitives/sha256.js.map +0 -1
- package/dist/cjs/primitives/x25519.js +0 -498
- package/dist/cjs/primitives/x25519.js.map +0 -1
- package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
- package/dist/cjs/primitives/xchacha20.js +0 -316
- package/dist/cjs/primitives/xchacha20.js.map +0 -1
- package/dist/cjs/types/cipher.js +0 -3
- package/dist/cjs/types/cipher.js.map +0 -1
- package/dist/cjs/types/crypto-api.js +0 -3
- package/dist/cjs/types/crypto-api.js.map +0 -1
- package/dist/cjs/types/hasher.js +0 -3
- package/dist/cjs/types/hasher.js.map +0 -1
- package/dist/cjs/types/identifier.js +0 -3
- package/dist/cjs/types/identifier.js.map +0 -1
- package/dist/cjs/types/key-compressor.js +0 -3
- package/dist/cjs/types/key-compressor.js.map +0 -1
- package/dist/cjs/types/key-converter.js +0 -3
- package/dist/cjs/types/key-converter.js.map +0 -1
- package/dist/cjs/types/key-deriver.js +0 -3
- package/dist/cjs/types/key-deriver.js.map +0 -1
- package/dist/cjs/types/key-generator.js +0 -3
- package/dist/cjs/types/key-generator.js.map +0 -1
- package/dist/cjs/types/key-io.js +0 -3
- package/dist/cjs/types/key-io.js.map +0 -1
- package/dist/cjs/types/key-wrapper.js +0 -3
- package/dist/cjs/types/key-wrapper.js.map +0 -1
- package/dist/cjs/types/params-direct.js +0 -3
- package/dist/cjs/types/params-direct.js.map +0 -1
- package/dist/cjs/types/params-enclosed.js +0 -3
- package/dist/cjs/types/params-enclosed.js.map +0 -1
- package/dist/cjs/types/params-kms.js +0 -3
- package/dist/cjs/types/params-kms.js.map +0 -1
- package/dist/cjs/types/signer.js +0 -3
- package/dist/cjs/types/signer.js.map +0 -1
- package/dist/cjs/utils.js +0 -173
- package/dist/cjs/utils.js.map +0 -1
|
@@ -1,959 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
39
|
-
var t = {};
|
|
40
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
41
|
-
t[p] = s[p];
|
|
42
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
43
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
44
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
45
|
-
t[p[i]] = s[p[i]];
|
|
46
|
-
}
|
|
47
|
-
return t;
|
|
48
|
-
};
|
|
49
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
-
if (!m) return o;
|
|
52
|
-
var i = m.call(o), r, ar = [], e;
|
|
53
|
-
try {
|
|
54
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
-
}
|
|
56
|
-
catch (error) { e = { error: error }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e) throw e.error; }
|
|
62
|
-
}
|
|
63
|
-
return ar;
|
|
64
|
-
};
|
|
65
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
75
|
-
exports.P256 = exports.Secp256r1 = void 0;
|
|
76
|
-
var common_1 = require("@enbox/common");
|
|
77
|
-
var sha256_1 = require("@noble/hashes/sha256");
|
|
78
|
-
var p256_1 = require("@noble/curves/p256");
|
|
79
|
-
var utils_1 = require("@noble/curves/abstract/utils");
|
|
80
|
-
var jwk_js_1 = require("../jose/jwk.js");
|
|
81
|
-
/**
|
|
82
|
-
* The `Secp256r1` class provides a comprehensive suite of utilities for working with
|
|
83
|
-
* the secp256r1 (aka P-256) elliptic curve, commonly used in blockchain and cryptographic
|
|
84
|
-
* applications. This class includes methods for key generation, conversion, signing, verification,
|
|
85
|
-
* and Elliptic Curve Diffie-Hellman (ECDH) key agreement.
|
|
86
|
-
*
|
|
87
|
-
* The class supports conversions between raw byte formats and JSON Web Key (JWK) formats. It
|
|
88
|
-
* adheres to RFC6979 for ECDSA signing and verification and RFC6090 for ECDH.
|
|
89
|
-
*
|
|
90
|
-
* Key Features:
|
|
91
|
-
* - Key Generation: Generate secp256r1 private keys in JWK format.
|
|
92
|
-
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
93
|
-
* - Public Key Derivation: Derive public keys from private keys.
|
|
94
|
-
* - ECDH Shared Secret Computation: Securely derive shared secrets using private and public keys.
|
|
95
|
-
* - ECDSA Signing and Verification: Sign data and verify signatures with secp256r1 keys.
|
|
96
|
-
* - Key Validation: Validate the mathematical correctness of secp256r1 keys.
|
|
97
|
-
*
|
|
98
|
-
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
99
|
-
* JavaScript environments, and use `Uint8Array` for binary data handling.
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* ```ts
|
|
103
|
-
* // Key Generation
|
|
104
|
-
* const privateKey = await Secp256r1.generateKey();
|
|
105
|
-
*
|
|
106
|
-
* // Public Key Derivation
|
|
107
|
-
* const publicKey = await Secp256r1.computePublicKey({ key: privateKey });
|
|
108
|
-
* console.log(publicKey === await Secp256r1.getPublicKey({ key: privateKey })); // Output: true
|
|
109
|
-
*
|
|
110
|
-
* // ECDH Shared Secret Computation
|
|
111
|
-
* const sharedSecret = await Secp256r1.sharedSecret({
|
|
112
|
-
* privateKeyA: privateKey,
|
|
113
|
-
* publicKeyB: anotherPublicKey
|
|
114
|
-
* });
|
|
115
|
-
*
|
|
116
|
-
* // ECDSA Signing
|
|
117
|
-
* const signature = await Secp256r1.sign({
|
|
118
|
-
* key: privateKey,
|
|
119
|
-
* data: new TextEncoder().encode('Message')
|
|
120
|
-
* });
|
|
121
|
-
*
|
|
122
|
-
* // ECDSA Signature Verification
|
|
123
|
-
* const isValid = await Secp256r1.verify({
|
|
124
|
-
* key: publicKey,
|
|
125
|
-
* signature: signature,
|
|
126
|
-
* data: new TextEncoder().encode('Message')
|
|
127
|
-
* });
|
|
128
|
-
*
|
|
129
|
-
* // Key Conversion
|
|
130
|
-
* const publicKeyBytes = await Secp256r1.publicKeyToBytes({ publicKey });
|
|
131
|
-
* const privateKeyBytes = await Secp256r1.privateKeyToBytes({ privateKey });
|
|
132
|
-
* const compressedPublicKey = await Secp256r1.compressPublicKey({ publicKeyBytes });
|
|
133
|
-
* const uncompressedPublicKey = await Secp256r1.decompressPublicKey({ publicKeyBytes });
|
|
134
|
-
*
|
|
135
|
-
* // Key Validation
|
|
136
|
-
* const isPrivateKeyValid = await Secp256r1.validatePrivateKey({ privateKeyBytes });
|
|
137
|
-
* const isPublicKeyValid = await Secp256r1.validatePublicKey({ publicKeyBytes });
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
var Secp256r1 = /** @class */ (function () {
|
|
141
|
-
function Secp256r1() {
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Adjusts an ECDSA signature to a normalized, low-S form.
|
|
145
|
-
*
|
|
146
|
-
* @remarks
|
|
147
|
-
* All ECDSA signatures, regardless of the curve, consist of two components, `r` and `s`, both of
|
|
148
|
-
* which are integers. The curve's order (the total number of points on the curve) is denoted by
|
|
149
|
-
* `n`. In a valid ECDSA signature, both `r` and `s` must be in the range [1, n-1]. However, due
|
|
150
|
-
* to the mathematical properties of ECDSA, if `(r, s)` is a valid signature, then `(r, n - s)` is
|
|
151
|
-
* also a valid signature for the same message and public key. In other words, for every
|
|
152
|
-
* signature, there's a "mirror" signature that's equally valid. For these elliptic curves:
|
|
153
|
-
*
|
|
154
|
-
* - Low S Signature: A signature where the `s` component is in the lower half of the range,
|
|
155
|
-
* specifically less than or equal to `n/2`.
|
|
156
|
-
*
|
|
157
|
-
* - High S Signature: This is where the `s` component is in the upper half of the range, greater
|
|
158
|
-
* than `n/2`.
|
|
159
|
-
*
|
|
160
|
-
* The practical implication is that a third-party can forge a second valid signature for the same
|
|
161
|
-
* message by negating the `s` component of the original signature, without any knowledge of the
|
|
162
|
-
* private key. This is known as a "signature malleability" attack.
|
|
163
|
-
*
|
|
164
|
-
* This type of forgery is not a problem in all systems, but it can be an issue in systems that
|
|
165
|
-
* rely on digital signature uniqueness to ensure transaction integrity. For example, in Bitcoin,
|
|
166
|
-
* transaction malleability is an issue because it allows for the modification of transaction
|
|
167
|
-
* identifiers (and potentially, transactions themselves) after they're signed but before they're
|
|
168
|
-
* confirmed in a block. By enforcing low `s` values, the Bitcoin network reduces the likelihood of
|
|
169
|
-
* this occurring, making the system more secure and predictable.
|
|
170
|
-
*
|
|
171
|
-
* For this reason, it's common practice to normalize ECDSA signatures to a low-S form. This
|
|
172
|
-
* form is considered standard and preferable in some systems and is known as the "normalized"
|
|
173
|
-
* form of the signature.
|
|
174
|
-
*
|
|
175
|
-
* This method takes a signature, and if it's high-S, returns the normalized low-S form. If the
|
|
176
|
-
* signature is already low-S, it's returned unmodified. It's important to note that this
|
|
177
|
-
* method does not change the validity of the signature but makes it compliant with systems that
|
|
178
|
-
* enforce low-S signatures.
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```ts
|
|
182
|
-
* const signature = new Uint8Array([...]); // Your ECDSA signature
|
|
183
|
-
* const adjustedSignature = await Secp256r1.adjustSignatureToLowS({ signature });
|
|
184
|
-
* // Now 'adjustedSignature' is in the low-S form.
|
|
185
|
-
* ```
|
|
186
|
-
*
|
|
187
|
-
* @param params - The parameters for the signature adjustment.
|
|
188
|
-
* @param params.signature - The ECDSA signature as a `Uint8Array`.
|
|
189
|
-
*
|
|
190
|
-
* @returns A Promise that resolves to the adjusted signature in low-S form as a `Uint8Array`.
|
|
191
|
-
*/
|
|
192
|
-
Secp256r1.adjustSignatureToLowS = function (_a) {
|
|
193
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
194
|
-
var signatureObject, adjustedSignatureObject, adjustedSignature;
|
|
195
|
-
var signature = _b.signature;
|
|
196
|
-
return __generator(this, function (_c) {
|
|
197
|
-
signatureObject = p256_1.secp256r1.Signature.fromCompact(signature);
|
|
198
|
-
if (signatureObject.hasHighS()) {
|
|
199
|
-
adjustedSignatureObject = signatureObject.normalizeS();
|
|
200
|
-
adjustedSignature = adjustedSignatureObject.toCompactRawBytes();
|
|
201
|
-
return [2 /*return*/, adjustedSignature];
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
// Return the unmodified signature if it is already in low-S format.
|
|
205
|
-
return [2 /*return*/, signature];
|
|
206
|
-
}
|
|
207
|
-
return [2 /*return*/];
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
213
|
-
*
|
|
214
|
-
* @remarks
|
|
215
|
-
* This method takes a private key represented as a byte array (Uint8Array) and
|
|
216
|
-
* converts it into a JWK object. The conversion involves extracting the
|
|
217
|
-
* elliptic curve point (x and y coordinates) from the private key and encoding
|
|
218
|
-
* them into base64url format, alongside other JWK parameters.
|
|
219
|
-
*
|
|
220
|
-
* The resulting JWK object includes the following properties:
|
|
221
|
-
* - `kty`: Key Type, set to 'EC' for Elliptic Curve.
|
|
222
|
-
* - `crv`: Curve Name, set to 'P-256'.
|
|
223
|
-
* - `d`: The private key component, base64url-encoded.
|
|
224
|
-
* - `x`: The x-coordinate of the public key point, base64url-encoded.
|
|
225
|
-
* - `y`: The y-coordinate of the public key point, base64url-encoded.
|
|
226
|
-
*
|
|
227
|
-
* This method is useful for converting raw public keys into a standardized
|
|
228
|
-
* JSON format, facilitating their use in cryptographic operations and making
|
|
229
|
-
* them easy to share and store.
|
|
230
|
-
*
|
|
231
|
-
* @example
|
|
232
|
-
* ```ts
|
|
233
|
-
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual private key bytes
|
|
234
|
-
* const privateKey = await Secp256r1.bytesToPrivateKey({ privateKeyBytes });
|
|
235
|
-
* ```
|
|
236
|
-
*
|
|
237
|
-
* @param params - The parameters for the private key conversion.
|
|
238
|
-
* @param params.privateKeyBytes - The raw private key as a Uint8Array.
|
|
239
|
-
*
|
|
240
|
-
* @returns A Promise that resolves to the private key in JWK format.
|
|
241
|
-
*/
|
|
242
|
-
Secp256r1.bytesToPrivateKey = function (_a) {
|
|
243
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
244
|
-
var point, privateKey, _c;
|
|
245
|
-
var privateKeyBytes = _b.privateKeyBytes;
|
|
246
|
-
return __generator(this, function (_d) {
|
|
247
|
-
switch (_d.label) {
|
|
248
|
-
case 0: return [4 /*yield*/, Secp256r1.getCurvePoint({ keyBytes: privateKeyBytes })];
|
|
249
|
-
case 1:
|
|
250
|
-
point = _d.sent();
|
|
251
|
-
privateKey = {
|
|
252
|
-
kty: 'EC',
|
|
253
|
-
crv: 'P-256',
|
|
254
|
-
d: common_1.Convert.uint8Array(privateKeyBytes).toBase64Url(),
|
|
255
|
-
x: common_1.Convert.uint8Array(point.x).toBase64Url(),
|
|
256
|
-
y: common_1.Convert.uint8Array(point.y).toBase64Url()
|
|
257
|
-
};
|
|
258
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
259
|
-
_c = privateKey;
|
|
260
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
261
|
-
case 2:
|
|
262
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
263
|
-
_c.kid = _d.sent();
|
|
264
|
-
return [2 /*return*/, privateKey];
|
|
265
|
-
}
|
|
266
|
-
});
|
|
267
|
-
});
|
|
268
|
-
};
|
|
269
|
-
/**
|
|
270
|
-
* Converts a raw public key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
271
|
-
*
|
|
272
|
-
* @remarks
|
|
273
|
-
* This method accepts a public key in a byte array (Uint8Array) format and
|
|
274
|
-
* transforms it to a JWK object. It involves decoding the elliptic curve point
|
|
275
|
-
* (x and y coordinates) from the raw public key bytes and encoding them into
|
|
276
|
-
* base64url format, along with setting appropriate JWK parameters.
|
|
277
|
-
*
|
|
278
|
-
* The resulting JWK object includes the following properties:
|
|
279
|
-
* - `kty`: Key Type, set to 'EC' for Elliptic Curve.
|
|
280
|
-
* - `crv`: Curve Name, set to 'P-256'.
|
|
281
|
-
* - `x`: The x-coordinate of the public key point, base64url-encoded.
|
|
282
|
-
* - `y`: The y-coordinate of the public key point, base64url-encoded.
|
|
283
|
-
*
|
|
284
|
-
* This method is useful for converting raw public keys into a standardized
|
|
285
|
-
* JSON format, facilitating their use in cryptographic operations and making
|
|
286
|
-
* them easy to share and store.
|
|
287
|
-
*
|
|
288
|
-
* @example
|
|
289
|
-
* ```ts
|
|
290
|
-
* const publicKeyBytes = new Uint8Array([...]); // Replace with actual public key bytes
|
|
291
|
-
* const publicKey = await Secp256r1.bytesToPublicKey({ publicKeyBytes });
|
|
292
|
-
* ```
|
|
293
|
-
*
|
|
294
|
-
* @param params - The parameters for the public key conversion.
|
|
295
|
-
* @param params.publicKeyBytes - The raw public key as a Uint8Array.
|
|
296
|
-
*
|
|
297
|
-
* @returns A Promise that resolves to the public key in JWK format.
|
|
298
|
-
*/
|
|
299
|
-
Secp256r1.bytesToPublicKey = function (_a) {
|
|
300
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
301
|
-
var point, publicKey, _c;
|
|
302
|
-
var publicKeyBytes = _b.publicKeyBytes;
|
|
303
|
-
return __generator(this, function (_d) {
|
|
304
|
-
switch (_d.label) {
|
|
305
|
-
case 0: return [4 /*yield*/, Secp256r1.getCurvePoint({ keyBytes: publicKeyBytes })];
|
|
306
|
-
case 1:
|
|
307
|
-
point = _d.sent();
|
|
308
|
-
publicKey = {
|
|
309
|
-
kty: 'EC',
|
|
310
|
-
crv: 'P-256',
|
|
311
|
-
x: common_1.Convert.uint8Array(point.x).toBase64Url(),
|
|
312
|
-
y: common_1.Convert.uint8Array(point.y).toBase64Url()
|
|
313
|
-
};
|
|
314
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
315
|
-
_c = publicKey;
|
|
316
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: publicKey })];
|
|
317
|
-
case 2:
|
|
318
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
319
|
-
_c.kid = _d.sent();
|
|
320
|
-
return [2 /*return*/, publicKey];
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
};
|
|
325
|
-
/**
|
|
326
|
-
* Converts a public key to its compressed form.
|
|
327
|
-
*
|
|
328
|
-
* @remarks
|
|
329
|
-
* This method takes a public key represented as a byte array and compresses it. Public key
|
|
330
|
-
* compression is a process that reduces the size of the public key by removing the y-coordinate,
|
|
331
|
-
* making it more efficient for storage and transmission. The compressed key retains the same
|
|
332
|
-
* level of security as the uncompressed key.
|
|
333
|
-
*
|
|
334
|
-
* @example
|
|
335
|
-
* ```ts
|
|
336
|
-
* const uncompressedPublicKeyBytes = new Uint8Array([...]); // Replace with actual uncompressed public key bytes
|
|
337
|
-
* const compressedPublicKey = await Secp256r1.compressPublicKey({
|
|
338
|
-
* publicKeyBytes: uncompressedPublicKeyBytes
|
|
339
|
-
* });
|
|
340
|
-
* ```
|
|
341
|
-
*
|
|
342
|
-
* @param params - The parameters for the public key compression.
|
|
343
|
-
* @param params.publicKeyBytes - The public key as a Uint8Array.
|
|
344
|
-
*
|
|
345
|
-
* @returns A Promise that resolves to the compressed public key as a Uint8Array.
|
|
346
|
-
*/
|
|
347
|
-
Secp256r1.compressPublicKey = function (_a) {
|
|
348
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
349
|
-
var point;
|
|
350
|
-
var publicKeyBytes = _b.publicKeyBytes;
|
|
351
|
-
return __generator(this, function (_c) {
|
|
352
|
-
point = p256_1.secp256r1.ProjectivePoint.fromHex(publicKeyBytes);
|
|
353
|
-
// Return the compressed form of the public key.
|
|
354
|
-
return [2 /*return*/, point.toRawBytes(true)];
|
|
355
|
-
});
|
|
356
|
-
});
|
|
357
|
-
};
|
|
358
|
-
/**
|
|
359
|
-
* Derives the public key in JWK format from a given private key.
|
|
360
|
-
*
|
|
361
|
-
* @remarks
|
|
362
|
-
* This method takes a private key in JWK format and derives its corresponding public key,
|
|
363
|
-
* also in JWK format. The derivation process involves converting the private key to a raw
|
|
364
|
-
* byte array, then computing the elliptic curve point (x and y coordinates) from this private
|
|
365
|
-
* key. These coordinates are then encoded into base64url format to construct the public key in
|
|
366
|
-
* JWK format.
|
|
367
|
-
*
|
|
368
|
-
* The process ensures that the derived public key correctly corresponds to the given private key,
|
|
369
|
-
* adhering to the secp256r1 elliptic curve standards. This method is useful in cryptographic
|
|
370
|
-
* operations where a public key is needed for operations like signature verification, but only
|
|
371
|
-
* the private key is available.
|
|
372
|
-
*
|
|
373
|
-
* @example
|
|
374
|
-
* ```ts
|
|
375
|
-
* const privateKey = { ... }; // A Jwk object representing a secp256r1 private key
|
|
376
|
-
* const publicKey = await Secp256r1.computePublicKey({ key: privateKey });
|
|
377
|
-
* ```
|
|
378
|
-
*
|
|
379
|
-
* @param params - The parameters for the public key derivation.
|
|
380
|
-
* @param params.key - The private key in JWK format from which to derive the public key.
|
|
381
|
-
*
|
|
382
|
-
* @returns A Promise that resolves to the derived public key in JWK format.
|
|
383
|
-
*/
|
|
384
|
-
Secp256r1.computePublicKey = function (_a) {
|
|
385
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
386
|
-
var privateKeyBytes, point, publicKey, _c;
|
|
387
|
-
var key = _b.key;
|
|
388
|
-
return __generator(this, function (_d) {
|
|
389
|
-
switch (_d.label) {
|
|
390
|
-
case 0: return [4 /*yield*/, Secp256r1.privateKeyToBytes({ privateKey: key })];
|
|
391
|
-
case 1:
|
|
392
|
-
privateKeyBytes = _d.sent();
|
|
393
|
-
return [4 /*yield*/, Secp256r1.getCurvePoint({ keyBytes: privateKeyBytes })];
|
|
394
|
-
case 2:
|
|
395
|
-
point = _d.sent();
|
|
396
|
-
publicKey = {
|
|
397
|
-
kty: 'EC',
|
|
398
|
-
crv: 'P-256',
|
|
399
|
-
x: common_1.Convert.uint8Array(point.x).toBase64Url(),
|
|
400
|
-
y: common_1.Convert.uint8Array(point.y).toBase64Url()
|
|
401
|
-
};
|
|
402
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
403
|
-
_c = publicKey;
|
|
404
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: publicKey })];
|
|
405
|
-
case 3:
|
|
406
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
407
|
-
_c.kid = _d.sent();
|
|
408
|
-
return [2 /*return*/, publicKey];
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
};
|
|
413
|
-
/**
|
|
414
|
-
* Converts an ASN.1 DER encoded ECDSA signature to a compact R+S format.
|
|
415
|
-
*
|
|
416
|
-
* @remarks
|
|
417
|
-
* This method is used for converting an ECDSA signature from the ASN.1 DER encoding to the more
|
|
418
|
-
* compact R+S format. This conversion is often required when dealing with ECDSA signatures in
|
|
419
|
-
* certain cryptographic standards such as JWS (JSON Web Signature).
|
|
420
|
-
*
|
|
421
|
-
* The method decodes the DER-encoded signature, extracts the R and S values, and concatenates
|
|
422
|
-
* them into a single byte array. This process involves handling the ASN.1 structure to correctly
|
|
423
|
-
* parse the R and S values, considering padding and integer encoding specifics of DER.
|
|
424
|
-
*
|
|
425
|
-
* @example
|
|
426
|
-
* ```ts
|
|
427
|
-
* const derSignature = new Uint8Array([...]); // Replace with your DER-encoded signature
|
|
428
|
-
* const signature = await Secp256r1.convertDerToCompactSignature({ derSignature });
|
|
429
|
-
* ```
|
|
430
|
-
*
|
|
431
|
-
* @param params - The parameters for the signature conversion.
|
|
432
|
-
* @param params.derSignature - The signature in ASN.1 DER format as a `Uint8Array`.
|
|
433
|
-
*
|
|
434
|
-
* @returns A Promise that resolves to the signature in compact R+S format as a `Uint8Array`.
|
|
435
|
-
*/
|
|
436
|
-
Secp256r1.convertDerToCompactSignature = function (_a) {
|
|
437
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
438
|
-
var signatureObject, compactSignature;
|
|
439
|
-
var derSignature = _b.derSignature;
|
|
440
|
-
return __generator(this, function (_c) {
|
|
441
|
-
signatureObject = p256_1.secp256r1.Signature.fromDER(derSignature);
|
|
442
|
-
compactSignature = signatureObject.toCompactRawBytes();
|
|
443
|
-
return [2 /*return*/, compactSignature];
|
|
444
|
-
});
|
|
445
|
-
});
|
|
446
|
-
};
|
|
447
|
-
/**
|
|
448
|
-
* Converts a public key to its uncompressed form.
|
|
449
|
-
*
|
|
450
|
-
* @remarks
|
|
451
|
-
* This method takes a compressed public key represented as a byte array and decompresses it.
|
|
452
|
-
* Public key decompression involves reconstructing the y-coordinate from the x-coordinate,
|
|
453
|
-
* resulting in the full public key. This method is used when the uncompressed key format is
|
|
454
|
-
* required for certain cryptographic operations or interoperability.
|
|
455
|
-
*
|
|
456
|
-
* @example
|
|
457
|
-
* ```ts
|
|
458
|
-
* const compressedPublicKeyBytes = new Uint8Array([...]); // Replace with actual compressed public key bytes
|
|
459
|
-
* const decompressedPublicKey = await Secp256r1.decompressPublicKey({
|
|
460
|
-
* publicKeyBytes: compressedPublicKeyBytes
|
|
461
|
-
* });
|
|
462
|
-
* ```
|
|
463
|
-
*
|
|
464
|
-
* @param params - The parameters for the public key decompression.
|
|
465
|
-
* @param params.publicKeyBytes - The public key as a Uint8Array.
|
|
466
|
-
*
|
|
467
|
-
* @returns A Promise that resolves to the uncompressed public key as a Uint8Array.
|
|
468
|
-
*/
|
|
469
|
-
Secp256r1.decompressPublicKey = function (_a) {
|
|
470
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
471
|
-
var point;
|
|
472
|
-
var publicKeyBytes = _b.publicKeyBytes;
|
|
473
|
-
return __generator(this, function (_c) {
|
|
474
|
-
point = p256_1.secp256r1.ProjectivePoint.fromHex(publicKeyBytes);
|
|
475
|
-
// Return the uncompressed form of the public key.
|
|
476
|
-
return [2 /*return*/, point.toRawBytes(false)];
|
|
477
|
-
});
|
|
478
|
-
});
|
|
479
|
-
};
|
|
480
|
-
/**
|
|
481
|
-
* Generates a secp256r1 private key in JSON Web Key (JWK) format.
|
|
482
|
-
*
|
|
483
|
-
* @remarks
|
|
484
|
-
* This method creates a new private key suitable for use with the secp256r1
|
|
485
|
-
* elliptic curve. The key is generated using cryptographically secure random
|
|
486
|
-
* number generation to ensure its uniqueness and security. The resulting
|
|
487
|
-
* private key adheres to the JWK format, specifically tailored for secp256r1,
|
|
488
|
-
* making it compatible with common cryptographic standards and easy to use in
|
|
489
|
-
* various cryptographic processes.
|
|
490
|
-
*
|
|
491
|
-
* The private key generated by this method includes the following components:
|
|
492
|
-
* - `kty`: Key Type, set to 'EC' for Elliptic Curve.
|
|
493
|
-
* - `crv`: Curve Name, set to 'P-256'.
|
|
494
|
-
* - `d`: The private key component, base64url-encoded.
|
|
495
|
-
* - `x`: The x-coordinate of the public key point, derived from the private key, base64url-encoded.
|
|
496
|
-
* - `y`: The y-coordinate of the public key point, derived from the private key, base64url-encoded.
|
|
497
|
-
*
|
|
498
|
-
* The key is returned in a format suitable for direct use in signin and key agreement operations.
|
|
499
|
-
*
|
|
500
|
-
* @example
|
|
501
|
-
* ```ts
|
|
502
|
-
* const privateKey = await Secp256r1.generateKey();
|
|
503
|
-
* ```
|
|
504
|
-
*
|
|
505
|
-
* @returns A Promise that resolves to the generated private key in JWK format.
|
|
506
|
-
*/
|
|
507
|
-
Secp256r1.generateKey = function () {
|
|
508
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
509
|
-
var privateKeyBytes, privateKey, _a;
|
|
510
|
-
return __generator(this, function (_b) {
|
|
511
|
-
switch (_b.label) {
|
|
512
|
-
case 0:
|
|
513
|
-
privateKeyBytes = p256_1.secp256r1.utils.randomPrivateKey();
|
|
514
|
-
return [4 /*yield*/, Secp256r1.bytesToPrivateKey({ privateKeyBytes: privateKeyBytes })];
|
|
515
|
-
case 1:
|
|
516
|
-
privateKey = _b.sent();
|
|
517
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
518
|
-
_a = privateKey;
|
|
519
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
520
|
-
case 2:
|
|
521
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
522
|
-
_a.kid = _b.sent();
|
|
523
|
-
return [2 /*return*/, privateKey];
|
|
524
|
-
}
|
|
525
|
-
});
|
|
526
|
-
});
|
|
527
|
-
};
|
|
528
|
-
/**
|
|
529
|
-
* Retrieves the public key properties from a given private key in JWK format.
|
|
530
|
-
*
|
|
531
|
-
* @remarks
|
|
532
|
-
* This method extracts the public key portion from a secp256r1 private key in JWK format. It does
|
|
533
|
-
* so by removing the private key property 'd' and making a shallow copy, effectively yielding the
|
|
534
|
-
* public key. The method sets the 'kid' (key ID) property using the JWK thumbprint if it is not
|
|
535
|
-
* already defined. This approach is used under the assumption that a private key in JWK format
|
|
536
|
-
* always contains the corresponding public key properties.
|
|
537
|
-
*
|
|
538
|
-
* Note: This method offers a significant performance advantage, being about 200 times faster
|
|
539
|
-
* than `computePublicKey()`. However, it does not mathematically validate the private key, nor
|
|
540
|
-
* does it derive the public key from the private key. It simply extracts existing public key
|
|
541
|
-
* properties from the private key object. This makes it suitable for scenarios where speed is
|
|
542
|
-
* critical and the private key's integrity is already assured.
|
|
543
|
-
*
|
|
544
|
-
* @example
|
|
545
|
-
* ```ts
|
|
546
|
-
* const privateKey = { ... }; // A Jwk object representing a secp256r1 private key
|
|
547
|
-
* const publicKey = await Secp256r1.getPublicKey({ key: privateKey });
|
|
548
|
-
* ```
|
|
549
|
-
*
|
|
550
|
-
* @param params - The parameters for retrieving the public key properties.
|
|
551
|
-
* @param params.key - The private key in JWK format.
|
|
552
|
-
*
|
|
553
|
-
* @returns A Promise that resolves to the public key in JWK format.
|
|
554
|
-
*/
|
|
555
|
-
Secp256r1.getPublicKey = function (_a) {
|
|
556
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
557
|
-
var d, publicKey, _c, _d;
|
|
558
|
-
var _e;
|
|
559
|
-
var key = _b.key;
|
|
560
|
-
return __generator(this, function (_f) {
|
|
561
|
-
switch (_f.label) {
|
|
562
|
-
case 0:
|
|
563
|
-
// Verify the provided JWK represents an elliptic curve (EC) secp256r1 private key.
|
|
564
|
-
if (!((0, jwk_js_1.isEcPrivateJwk)(key) && key.crv === 'P-256')) {
|
|
565
|
-
throw new Error("Secp256r1: The provided key is not a 'P-256' private JWK.");
|
|
566
|
-
}
|
|
567
|
-
d = key.d, publicKey = __rest(key, ["d"]);
|
|
568
|
-
if (!((_e =
|
|
569
|
-
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
570
|
-
publicKey.kid) !== null && _e !== void 0)) return [3 /*break*/, 1];
|
|
571
|
-
_c = _e;
|
|
572
|
-
return [3 /*break*/, 3];
|
|
573
|
-
case 1:
|
|
574
|
-
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
575
|
-
_d = publicKey;
|
|
576
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: publicKey })];
|
|
577
|
-
case 2:
|
|
578
|
-
_c = (_d.kid = _f.sent());
|
|
579
|
-
_f.label = 3;
|
|
580
|
-
case 3:
|
|
581
|
-
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
582
|
-
_c;
|
|
583
|
-
return [2 /*return*/, publicKey];
|
|
584
|
-
}
|
|
585
|
-
});
|
|
586
|
-
});
|
|
587
|
-
};
|
|
588
|
-
/**
|
|
589
|
-
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
590
|
-
*
|
|
591
|
-
* @remarks
|
|
592
|
-
* This method takes a private key in JWK format and extracts its raw byte representation.
|
|
593
|
-
* It specifically focuses on the 'd' parameter of the JWK, which represents the private
|
|
594
|
-
* key component in base64url encoding. The method decodes this value into a byte array.
|
|
595
|
-
*
|
|
596
|
-
* This conversion is essential for operations that require the private key in its raw
|
|
597
|
-
* binary form, such as certain low-level cryptographic operations or when interfacing
|
|
598
|
-
* with systems and libraries that expect keys in a byte array format.
|
|
599
|
-
*
|
|
600
|
-
* @example
|
|
601
|
-
* ```ts
|
|
602
|
-
* const privateKey = { ... }; // An X25519 private key in JWK format
|
|
603
|
-
* const privateKeyBytes = await Secp256r1.privateKeyToBytes({ privateKey });
|
|
604
|
-
* ```
|
|
605
|
-
*
|
|
606
|
-
* @param params - The parameters for the private key conversion.
|
|
607
|
-
* @param params.privateKey - The private key in JWK format.
|
|
608
|
-
*
|
|
609
|
-
* @returns A Promise that resolves to the private key as a Uint8Array.
|
|
610
|
-
*/
|
|
611
|
-
Secp256r1.privateKeyToBytes = function (_a) {
|
|
612
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
613
|
-
var privateKeyBytes;
|
|
614
|
-
var privateKey = _b.privateKey;
|
|
615
|
-
return __generator(this, function (_c) {
|
|
616
|
-
// Verify the provided JWK represents a valid EC P-256 private key.
|
|
617
|
-
if (!(0, jwk_js_1.isEcPrivateJwk)(privateKey)) {
|
|
618
|
-
throw new Error("Secp256r1: The provided key is not a valid EC private key.");
|
|
619
|
-
}
|
|
620
|
-
privateKeyBytes = common_1.Convert.base64Url(privateKey.d).toUint8Array();
|
|
621
|
-
return [2 /*return*/, privateKeyBytes];
|
|
622
|
-
});
|
|
623
|
-
});
|
|
624
|
-
};
|
|
625
|
-
/**
|
|
626
|
-
* Converts a public key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
627
|
-
*
|
|
628
|
-
* @remarks
|
|
629
|
-
* This method accepts a public key in JWK format and converts it into its raw binary
|
|
630
|
-
* form. The conversion process involves decoding the 'x' and 'y' parameters of the JWK
|
|
631
|
-
* (which represent the x and y coordinates of the elliptic curve point, respectively)
|
|
632
|
-
* from base64url format into a byte array. The method then concatenates these values,
|
|
633
|
-
* along with a prefix indicating the key format, to form the full public key.
|
|
634
|
-
*
|
|
635
|
-
* This function is particularly useful for use cases where the public key is needed
|
|
636
|
-
* in its raw byte format, such as for certain cryptographic operations or when
|
|
637
|
-
* interfacing with systems that require raw key formats.
|
|
638
|
-
*
|
|
639
|
-
* @example
|
|
640
|
-
* ```ts
|
|
641
|
-
* const publicKey = { ... }; // A Jwk public key object
|
|
642
|
-
* const publicKeyBytes = await Secp256r1.publicKeyToBytes({ publicKey });
|
|
643
|
-
* ```
|
|
644
|
-
*
|
|
645
|
-
* @param params - The parameters for the public key conversion.
|
|
646
|
-
* @param params.publicKey - The public key in JWK format.
|
|
647
|
-
*
|
|
648
|
-
* @returns A Promise that resolves to the public key as a Uint8Array.
|
|
649
|
-
*/
|
|
650
|
-
Secp256r1.publicKeyToBytes = function (_a) {
|
|
651
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
652
|
-
var prefix, x, y, publicKeyBytes;
|
|
653
|
-
var publicKey = _b.publicKey;
|
|
654
|
-
return __generator(this, function (_c) {
|
|
655
|
-
// Verify the provided JWK represents a valid EC P-256 public key, which must have a 'y' value.
|
|
656
|
-
if (!((0, jwk_js_1.isEcPublicJwk)(publicKey) && publicKey.y)) {
|
|
657
|
-
throw new Error("Secp256r1: The provided key is not a valid EC public key.");
|
|
658
|
-
}
|
|
659
|
-
prefix = new Uint8Array([0x04]);
|
|
660
|
-
x = common_1.Convert.base64Url(publicKey.x).toUint8Array();
|
|
661
|
-
y = common_1.Convert.base64Url(publicKey.y).toUint8Array();
|
|
662
|
-
publicKeyBytes = new Uint8Array(__spreadArray(__spreadArray(__spreadArray([], __read(prefix), false), __read(x), false), __read(y), false));
|
|
663
|
-
return [2 /*return*/, publicKeyBytes];
|
|
664
|
-
});
|
|
665
|
-
});
|
|
666
|
-
};
|
|
667
|
-
/**
|
|
668
|
-
* Computes an RFC6090-compliant Elliptic Curve Diffie-Hellman (ECDH) shared secret
|
|
669
|
-
* using secp256r1 private and public keys in JSON Web Key (JWK) format.
|
|
670
|
-
*
|
|
671
|
-
* @remarks
|
|
672
|
-
* This method facilitates the ECDH key agreement protocol, which is a method of securely
|
|
673
|
-
* deriving a shared secret between two parties based on their private and public keys.
|
|
674
|
-
* It takes the private key of one party (privateKeyA) and the public key of another
|
|
675
|
-
* party (publicKeyB) to compute a shared secret. The shared secret is derived from the
|
|
676
|
-
* x-coordinate of the elliptic curve point resulting from the multiplication of the
|
|
677
|
-
* public key with the private key.
|
|
678
|
-
*
|
|
679
|
-
* Note: When performing Elliptic Curve Diffie-Hellman (ECDH) key agreement,
|
|
680
|
-
* the resulting shared secret is a point on the elliptic curve, which
|
|
681
|
-
* consists of an x-coordinate and a y-coordinate. With a 256-bit curve like
|
|
682
|
-
* secp256r1, each of these coordinates is 32 bytes (256 bits) long. However,
|
|
683
|
-
* in the ECDH process, it's standard practice to use only the x-coordinate
|
|
684
|
-
* of the shared secret point as the resulting shared key. This is because
|
|
685
|
-
* the y-coordinate does not add to the entropy of the key, and both parties
|
|
686
|
-
* can independently compute the x-coordinate. Consquently, this implementation
|
|
687
|
-
* omits the y-coordinate for simplicity and standard compliance.
|
|
688
|
-
*
|
|
689
|
-
* @example
|
|
690
|
-
* ```ts
|
|
691
|
-
* const privateKeyA = { ... }; // A Jwk private key object for party A
|
|
692
|
-
* const publicKeyB = { ... }; // A Jwk public key object for party B
|
|
693
|
-
* const sharedSecret = await Secp256r1.sharedSecret({
|
|
694
|
-
* privateKeyA,
|
|
695
|
-
* publicKeyB
|
|
696
|
-
* });
|
|
697
|
-
* ```
|
|
698
|
-
*
|
|
699
|
-
* @param params - The parameters for the shared secret computation.
|
|
700
|
-
* @param params.privateKeyA - The private key in JWK format of one party.
|
|
701
|
-
* @param params.publicKeyB - The public key in JWK format of the other party.
|
|
702
|
-
*
|
|
703
|
-
* @returns A Promise that resolves to the computed shared secret as a Uint8Array.
|
|
704
|
-
*/
|
|
705
|
-
Secp256r1.sharedSecret = function (_a) {
|
|
706
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
707
|
-
var privateKeyABytes, publicKeyBBytes, sharedSecret;
|
|
708
|
-
var privateKeyA = _b.privateKeyA, publicKeyB = _b.publicKeyB;
|
|
709
|
-
return __generator(this, function (_c) {
|
|
710
|
-
switch (_c.label) {
|
|
711
|
-
case 0:
|
|
712
|
-
// Ensure that keys from the same key pair are not specified.
|
|
713
|
-
if ('x' in privateKeyA && 'x' in publicKeyB && privateKeyA.x === publicKeyB.x) {
|
|
714
|
-
throw new Error("Secp256r1: ECDH shared secret cannot be computed from a single key pair's public and private keys.");
|
|
715
|
-
}
|
|
716
|
-
return [4 /*yield*/, Secp256r1.privateKeyToBytes({ privateKey: privateKeyA })];
|
|
717
|
-
case 1:
|
|
718
|
-
privateKeyABytes = _c.sent();
|
|
719
|
-
return [4 /*yield*/, Secp256r1.publicKeyToBytes({ publicKey: publicKeyB })];
|
|
720
|
-
case 2:
|
|
721
|
-
publicKeyBBytes = _c.sent();
|
|
722
|
-
sharedSecret = p256_1.secp256r1.getSharedSecret(privateKeyABytes, publicKeyBBytes, true);
|
|
723
|
-
// Remove the leading byte that indicates the sign of the y-coordinate
|
|
724
|
-
// of the point on the elliptic curve. See note above.
|
|
725
|
-
return [2 /*return*/, sharedSecret.slice(1)];
|
|
726
|
-
}
|
|
727
|
-
});
|
|
728
|
-
});
|
|
729
|
-
};
|
|
730
|
-
/**
|
|
731
|
-
* Generates an RFC6979-compliant ECDSA signature of given data using a secp256r1 private key.
|
|
732
|
-
*
|
|
733
|
-
* @remarks
|
|
734
|
-
* This method signs the provided data with a specified private key using the ECDSA
|
|
735
|
-
* (Elliptic Curve Digital Signature Algorithm) signature algorithm, as defined in RFC6979.
|
|
736
|
-
* The data to be signed is first hashed using the SHA-256 algorithm, and this hash is then
|
|
737
|
-
* signed using the private key. The output is a digital signature in the form of a
|
|
738
|
-
* Uint8Array, which uniquely corresponds to both the data and the private key used for signing.
|
|
739
|
-
*
|
|
740
|
-
* This method is commonly used in cryptographic applications to ensure data integrity and
|
|
741
|
-
* authenticity. The signature can later be verified by parties with access to the corresponding
|
|
742
|
-
* public key, ensuring that the data has not been tampered with and was indeed signed by the
|
|
743
|
-
* holder of the private key.
|
|
744
|
-
*
|
|
745
|
-
* @example
|
|
746
|
-
* ```ts
|
|
747
|
-
* const data = new TextEncoder().encode('Messsage'); // Data to be signed
|
|
748
|
-
* const privateKey = { ... }; // A Jwk object representing a secp256r1 private key
|
|
749
|
-
* const signature = await Secp256r1.sign({
|
|
750
|
-
* key: privateKey,
|
|
751
|
-
* data
|
|
752
|
-
* });
|
|
753
|
-
* ```
|
|
754
|
-
*
|
|
755
|
-
* @param params - The parameters for the signing operation.
|
|
756
|
-
* @param params.key - The private key to use for signing, represented in JWK format.
|
|
757
|
-
* @param params.data - The data to sign, represented as a Uint8Array.
|
|
758
|
-
*
|
|
759
|
-
* @returns A Promise that resolves to the signature as a Uint8Array.
|
|
760
|
-
*/
|
|
761
|
-
Secp256r1.sign = function (_a) {
|
|
762
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
763
|
-
var privateKeyBytes, digest, signatureObject, signature;
|
|
764
|
-
var data = _b.data, key = _b.key;
|
|
765
|
-
return __generator(this, function (_c) {
|
|
766
|
-
switch (_c.label) {
|
|
767
|
-
case 0: return [4 /*yield*/, Secp256r1.privateKeyToBytes({ privateKey: key })];
|
|
768
|
-
case 1:
|
|
769
|
-
privateKeyBytes = _c.sent();
|
|
770
|
-
digest = (0, sha256_1.sha256)(data);
|
|
771
|
-
signatureObject = p256_1.secp256r1.sign(digest, privateKeyBytes);
|
|
772
|
-
signature = signatureObject.toCompactRawBytes();
|
|
773
|
-
return [2 /*return*/, signature];
|
|
774
|
-
}
|
|
775
|
-
});
|
|
776
|
-
});
|
|
777
|
-
};
|
|
778
|
-
/**
|
|
779
|
-
* Validates a given private key to ensure its compliance with the secp256r1 curve standards.
|
|
780
|
-
*
|
|
781
|
-
* @remarks
|
|
782
|
-
* This method checks whether a provided private key is a valid 32-byte number and falls within
|
|
783
|
-
* the range defined by the secp256r1 curve's order. It is essential for ensuring the private
|
|
784
|
-
* key's mathematical correctness in the context of secp256r1-based cryptographic operations.
|
|
785
|
-
*
|
|
786
|
-
* Note that this validation strictly pertains to the key's format and numerical validity; it does
|
|
787
|
-
* not assess whether the key corresponds to a known entity or its security status (e.g., whether
|
|
788
|
-
* it has been compromised).
|
|
789
|
-
*
|
|
790
|
-
* @example
|
|
791
|
-
* ```ts
|
|
792
|
-
* const privateKeyBytes = new Uint8Array([...]); // A 32-byte private key
|
|
793
|
-
* const isValid = await Secp256r1.validatePrivateKey({ privateKeyBytes });
|
|
794
|
-
* console.log(isValid); // true or false based on the key's validity
|
|
795
|
-
* ```
|
|
796
|
-
*
|
|
797
|
-
* @param params - The parameters for the key validation.
|
|
798
|
-
* @param params.privateKeyBytes - The private key to validate, represented as a Uint8Array.
|
|
799
|
-
*
|
|
800
|
-
* @returns A Promise that resolves to a boolean indicating whether the private key is valid.
|
|
801
|
-
*/
|
|
802
|
-
Secp256r1.validatePrivateKey = function (_a) {
|
|
803
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
804
|
-
var privateKeyBytes = _b.privateKeyBytes;
|
|
805
|
-
return __generator(this, function (_c) {
|
|
806
|
-
return [2 /*return*/, p256_1.secp256r1.utils.isValidPrivateKey(privateKeyBytes)];
|
|
807
|
-
});
|
|
808
|
-
});
|
|
809
|
-
};
|
|
810
|
-
/**
|
|
811
|
-
* Validates a given public key to confirm its mathematical correctness on the secp256r1 curve.
|
|
812
|
-
*
|
|
813
|
-
* @remarks
|
|
814
|
-
* This method checks if the provided public key represents a valid point on the secp256r1 curve.
|
|
815
|
-
* It decodes the key's Weierstrass points (x and y coordinates) and verifies their validity
|
|
816
|
-
* against the curve's parameters. A valid point must lie on the curve and meet specific
|
|
817
|
-
* mathematical criteria defined by the curve's equation.
|
|
818
|
-
*
|
|
819
|
-
* It's important to note that this method does not verify the key's ownership or whether it has
|
|
820
|
-
* been compromised; it solely focuses on the key's adherence to the curve's mathematical
|
|
821
|
-
* principles.
|
|
822
|
-
*
|
|
823
|
-
* @example
|
|
824
|
-
* ```ts
|
|
825
|
-
* const publicKeyBytes = new Uint8Array([...]); // A public key in byte format
|
|
826
|
-
* const isValid = await Secp256r1.validatePublicKey({ publicKeyBytes });
|
|
827
|
-
* console.log(isValid); // true if the key is valid on the secp256r1 curve, false otherwise
|
|
828
|
-
* ```
|
|
829
|
-
*
|
|
830
|
-
* @param params - The parameters for the key validation.
|
|
831
|
-
* @param params.publicKeyBytes - The public key to validate, represented as a Uint8Array.
|
|
832
|
-
*
|
|
833
|
-
* @returns A Promise that resolves to a boolean indicating the public key's validity on
|
|
834
|
-
* the secp256r1 curve.
|
|
835
|
-
*/
|
|
836
|
-
Secp256r1.validatePublicKey = function (_a) {
|
|
837
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
838
|
-
var point;
|
|
839
|
-
var publicKeyBytes = _b.publicKeyBytes;
|
|
840
|
-
return __generator(this, function (_c) {
|
|
841
|
-
try {
|
|
842
|
-
point = p256_1.secp256r1.ProjectivePoint.fromHex(publicKeyBytes);
|
|
843
|
-
// Check if points are on the Short Weierstrass curve.
|
|
844
|
-
point.assertValidity();
|
|
845
|
-
}
|
|
846
|
-
catch (error) {
|
|
847
|
-
return [2 /*return*/, false];
|
|
848
|
-
}
|
|
849
|
-
return [2 /*return*/, true];
|
|
850
|
-
});
|
|
851
|
-
});
|
|
852
|
-
};
|
|
853
|
-
/**
|
|
854
|
-
* Verifies an RFC6979-compliant ECDSA signature against given data and a secp256r1 public key.
|
|
855
|
-
*
|
|
856
|
-
* @remarks
|
|
857
|
-
* This method validates a digital signature to ensure that it was generated by the holder of the
|
|
858
|
-
* corresponding private key and that the signed data has not been altered. The signature
|
|
859
|
-
* verification is performed using the ECDSA (Elliptic Curve Digital Signature Algorithm) as
|
|
860
|
-
* specified in RFC6979. The data to be verified is first hashed using the SHA-256 algorithm, and
|
|
861
|
-
* this hash is then used along with the public key to verify the signature.
|
|
862
|
-
*
|
|
863
|
-
* The method returns a boolean value indicating whether the signature is valid. A valid signature
|
|
864
|
-
* proves that the signed data was indeed signed by the owner of the private key corresponding to
|
|
865
|
-
* the provided public key and that the data has not been tampered with since it was signed.
|
|
866
|
-
*
|
|
867
|
-
* Note: The verification process does not consider the malleability of low-s signatures, which
|
|
868
|
-
* may be relevant in certain contexts, such as Bitcoin transactions.
|
|
869
|
-
*
|
|
870
|
-
* @example
|
|
871
|
-
* ```ts
|
|
872
|
-
* const data = new TextEncoder().encode('Messsage'); // Data that was signed
|
|
873
|
-
* const publicKey = { ... }; // Public key in JWK format corresponding to the private key that signed the data
|
|
874
|
-
* const signature = new Uint8Array([...]); // Signature to verify
|
|
875
|
-
* const isSignatureValid = await Secp256r1.verify({
|
|
876
|
-
* key: publicKey,
|
|
877
|
-
* signature,
|
|
878
|
-
* data
|
|
879
|
-
* });
|
|
880
|
-
* console.log(isSignatureValid); // true if the signature is valid, false otherwise
|
|
881
|
-
* ```
|
|
882
|
-
*
|
|
883
|
-
* @param params - The parameters for the signature verification.
|
|
884
|
-
* @param params.key - The public key used for verification, represented in JWK format.
|
|
885
|
-
* @param params.signature - The signature to verify, represented as a Uint8Array.
|
|
886
|
-
* @param params.data - The data that was signed, represented as a Uint8Array.
|
|
887
|
-
*
|
|
888
|
-
* @returns A Promise that resolves to a boolean indicating whether the signature is valid.
|
|
889
|
-
*/
|
|
890
|
-
Secp256r1.verify = function (_a) {
|
|
891
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
892
|
-
var publicKeyBytes, digest, isValid;
|
|
893
|
-
var key = _b.key, signature = _b.signature, data = _b.data;
|
|
894
|
-
return __generator(this, function (_c) {
|
|
895
|
-
switch (_c.label) {
|
|
896
|
-
case 0: return [4 /*yield*/, Secp256r1.publicKeyToBytes({ publicKey: key })];
|
|
897
|
-
case 1:
|
|
898
|
-
publicKeyBytes = _c.sent();
|
|
899
|
-
digest = (0, sha256_1.sha256)(data);
|
|
900
|
-
isValid = p256_1.secp256r1.verify(signature, digest, publicKeyBytes, { lowS: false });
|
|
901
|
-
return [2 /*return*/, isValid];
|
|
902
|
-
}
|
|
903
|
-
});
|
|
904
|
-
});
|
|
905
|
-
};
|
|
906
|
-
/**
|
|
907
|
-
* Returns the elliptic curve point (x and y coordinates) for a given secp256r1 key.
|
|
908
|
-
*
|
|
909
|
-
* @remarks
|
|
910
|
-
* This method extracts the elliptic curve point from a given secp256r1 key, whether
|
|
911
|
-
* it's a private or a public key. For a private key, the method first computes the
|
|
912
|
-
* corresponding public key and then extracts the x and y coordinates. For a public key,
|
|
913
|
-
* it directly returns these coordinates. The coordinates are represented as Uint8Array.
|
|
914
|
-
*
|
|
915
|
-
* The x and y coordinates represent the key's position on the elliptic curve and can be
|
|
916
|
-
* used in various cryptographic operations, such as digital signatures or key agreement
|
|
917
|
-
* protocols.
|
|
918
|
-
*
|
|
919
|
-
* @example
|
|
920
|
-
* ```ts
|
|
921
|
-
* // For a private key
|
|
922
|
-
* const privateKey = new Uint8Array([...]); // A 32-byte private key
|
|
923
|
-
* const { x: xFromPrivateKey, y: yFromPrivateKey } = await Secp256r1.getCurvePoint({ keyBytes: privateKey });
|
|
924
|
-
*
|
|
925
|
-
* // For a public key
|
|
926
|
-
* const publicKey = new Uint8Array([...]); // A 33-byte or 65-byte public key
|
|
927
|
-
* const { x: xFromPublicKey, y: yFromPublicKey } = await Secp256r1.getCurvePoint({ keyBytes: publicKey });
|
|
928
|
-
* ```
|
|
929
|
-
*
|
|
930
|
-
* @param params - The parameters for the curve point decoding operation.
|
|
931
|
-
* @param params.keyBytes - The key for which to get the elliptic curve point.
|
|
932
|
-
* Can be either a private key or a public key.
|
|
933
|
-
* The key should be passed as a `Uint8Array`.
|
|
934
|
-
*
|
|
935
|
-
* @returns A Promise that resolves to an object with properties 'x' and 'y',
|
|
936
|
-
* each being a Uint8Array representing the x and y coordinates of the key point on the
|
|
937
|
-
* elliptic curve.
|
|
938
|
-
*/
|
|
939
|
-
Secp256r1.getCurvePoint = function (_a) {
|
|
940
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
941
|
-
var point, x, y;
|
|
942
|
-
var keyBytes = _b.keyBytes;
|
|
943
|
-
return __generator(this, function (_c) {
|
|
944
|
-
// If key is a private key, first compute the public key.
|
|
945
|
-
if (keyBytes.byteLength === 32) {
|
|
946
|
-
keyBytes = p256_1.secp256r1.getPublicKey(keyBytes);
|
|
947
|
-
}
|
|
948
|
-
point = p256_1.secp256r1.ProjectivePoint.fromHex(keyBytes);
|
|
949
|
-
x = (0, utils_1.numberToBytesBE)(point.x, 32);
|
|
950
|
-
y = (0, utils_1.numberToBytesBE)(point.y, 32);
|
|
951
|
-
return [2 /*return*/, { x: x, y: y }];
|
|
952
|
-
});
|
|
953
|
-
});
|
|
954
|
-
};
|
|
955
|
-
return Secp256r1;
|
|
956
|
-
}());
|
|
957
|
-
exports.Secp256r1 = Secp256r1;
|
|
958
|
-
exports.P256 = Secp256r1;
|
|
959
|
-
//# sourceMappingURL=secp256r1.js.map
|