@verii/crypto 1.0.0-pre.1752076816 → 1.0.0-pre.1754289942

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@verii/crypto",
3
- "version": "1.0.0-pre.1752076816",
4
- "description": "Set of secp256k1 based functions used in Velocity Foundation projects",
3
+ "version": "1.0.0-pre.1754289942",
4
+ "description": "Set of crypto functions used in Verii projects",
5
5
  "repository": "https://github.com/LFDT-Verii/core",
6
6
  "main": "index.js",
7
7
  "publishConfig": {
@@ -16,9 +16,11 @@
16
16
  "license": "Apache-2.0",
17
17
  "dependencies": {
18
18
  "@trust/keyto": "~2.0.0-alpha1",
19
- "@verii/test-regexes": "1.0.0-pre.1752076816",
20
- "argon2": "0.43.0",
21
- "canonicalize": "^2.0.0",
19
+ "@verii/test-regexes": "1.0.0-pre.1754289942",
20
+ "argon2": "0.43.1",
21
+ "bigint-crypto-utils": "3.3.0",
22
+ "canonicalize": "^2.1.0",
23
+ "cborg": "4.2.12",
22
24
  "elliptic": "^6.6.1",
23
25
  "lodash": "^4.17.21",
24
26
  "multihashing": "~0.3.3",
@@ -42,5 +44,5 @@
42
44
  "lib"
43
45
  ]
44
46
  },
45
- "gitHead": "5885ce94149cc0102b2bc9cde18834293174bfaf"
47
+ "gitHead": "772b121fb3eb2ee0077a1b5c9eec3281215e9e9f"
46
48
  }
package/src/constants.js CHANGED
@@ -26,8 +26,10 @@ const KeyPurposes = {
26
26
 
27
27
  // TODO If we create a keys entity package, KeyAlgorithms should probably be moved there
28
28
  const KeyAlgorithms = {
29
- SECP256K1: 'SECP256K1',
29
+ // Could be renamed to JsonWebAlgorithms
30
+ SECP256K1: 'SECP256K1', // Also ES256K
30
31
  ES256: 'ES256',
32
+ RS256: 'RS256',
31
33
  };
32
34
 
33
35
  // TODO If we create a keys entity package, KeyEncodings should probably be moved there
package/src/crypto.js CHANGED
@@ -22,6 +22,7 @@ const randomNumber = require('random-number-csprng');
22
22
  const multihash = require('multihashing');
23
23
  const keyto = require('@trust/keyto');
24
24
  const { HEX_FORMAT } = require('@verii/test-regexes');
25
+ const { KeyAlgorithms } = require('./constants');
25
26
 
26
27
  const secp256k1 = new EC('secp256k1');
27
28
 
@@ -35,14 +36,33 @@ const createCommitment = (val) => {
35
36
  return Buffer.from(hash).toString('base64');
36
37
  };
37
38
 
38
- const generateJWAKeyPair = (config) =>
39
- config.algorithm === 'rsa'
39
+ const generateJWAKeyPair = (dsaOrConfig) => {
40
+ const jwaConfig = isString(dsaOrConfig)
41
+ ? dsaJwaConfigMap[dsaOrConfig]
42
+ : dsaOrConfig;
43
+
44
+ return jwaConfig.algorithm === 'rsa'
40
45
  ? generateKeyPair({ type: 'rsa', format: 'jwk', modulusLength: 2048 })
41
46
  : generateKeyPair({
42
47
  type: 'ec',
43
48
  format: 'jwk',
44
- curve: config.curve,
49
+ curve: jwaConfig.curve,
45
50
  });
51
+ };
52
+
53
+ const dsaJwaConfigMap = {
54
+ [KeyAlgorithms.SECP256K1]: {
55
+ algorithm: 'ec',
56
+ curve: 'secp256k1',
57
+ },
58
+ [KeyAlgorithms.ES256]: {
59
+ algorithm: 'ec',
60
+ curve: 'P-256',
61
+ },
62
+ [KeyAlgorithms.RS256]: {
63
+ algorithm: 'rsa',
64
+ },
65
+ };
46
66
 
47
67
  const generateKeyPair = (options = {}) => {
48
68
  const { format = 'hex', type = 'ec' } = options;
@@ -154,30 +174,47 @@ const verifyBase64Signature = (value, signature, publicKey) => {
154
174
  );
155
175
  };
156
176
 
157
- const encrypt = (text, secret) => {
158
- const iv = crypto.randomBytes(16);
177
+ const encryptBuffer = (buffer, secret) =>
178
+ doEncrypt(secret, (cipher) => cipher.update(buffer));
179
+
180
+ const encrypt = (text, secret) =>
181
+ doEncrypt(secret, (cipher) => cipher.update(text, 'utf8')).toString('base64');
182
+
183
+ const decryptBuffer = (encrypted, secret) =>
184
+ doDecrypt(encrypted, secret, (encryptedBuffer, decipher) =>
185
+ Buffer.concat([decipher.update(encryptedBuffer), decipher.final()])
186
+ );
187
+
188
+ const decrypt = (encrypted, secret) => {
189
+ const bData = Buffer.from(encrypted, 'base64');
190
+ return doDecrypt(
191
+ bData,
192
+ secret,
193
+ (encryptedBuffer, decipher) =>
194
+ decipher.update(encryptedBuffer, 'binary', 'utf8') +
195
+ decipher.final('utf8')
196
+ );
197
+ };
198
+
199
+ const doEncrypt = (secret, callback) => {
159
200
  const salt = crypto.randomBytes(64);
201
+ const iv = crypto.randomBytes(16);
160
202
  const key = crypto.pbkdf2Sync(secret, salt, 2145, 32, 'sha512');
161
203
  const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
162
-
163
- const encrypted = Buffer.concat([
164
- cipher.update(text, 'utf8'),
165
- cipher.final(),
166
- ]);
204
+ const encrypted = Buffer.concat([callback(cipher), cipher.final()]);
167
205
  const tag = cipher.getAuthTag();
168
- return Buffer.concat([salt, iv, tag, encrypted]).toString('base64');
206
+ return Buffer.concat([salt, iv, tag, encrypted]);
169
207
  };
170
208
 
171
- const decrypt = (encrypted, secret) => {
172
- const bData = Buffer.from(encrypted, 'base64');
173
- const salt = bData.slice(0, 64);
174
- const iv = bData.slice(64, 80);
175
- const tag = bData.slice(80, 96);
176
- const text = bData.slice(96);
209
+ const doDecrypt = (encrypted, secret, callback) => {
210
+ const salt = encrypted.slice(0, 64);
211
+ const iv = encrypted.slice(64, 80);
212
+ const tag = encrypted.slice(80, 96);
213
+ const buffer = encrypted.slice(96);
177
214
  const key = crypto.pbkdf2Sync(secret, salt, 2145, 32, 'sha512');
178
215
  const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
179
216
  decipher.setAuthTag(tag);
180
- return decipher.update(text, 'binary', 'utf8') + decipher.final('utf8');
217
+ return callback(buffer, decipher);
181
218
  };
182
219
 
183
220
  const generateRandomNumber = (length) =>
@@ -198,9 +235,9 @@ const initBuildRefreshToken = (bitLength = 512) => {
198
235
  return () => generateRandomBytes(byteLength).toString('hex');
199
236
  };
200
237
 
201
- const deriveEncryptionSecretFromPassword = async (contentHash) => {
202
- const salt = Buffer.from(contentHash.slice(-16), 'hex');
203
- const secret = await argon2.hash(contentHash, {
238
+ const deriveEncryptionSecretFromPassword = async (password) => {
239
+ const salt = Buffer.from(password.slice(-16), 'hex');
240
+ const secret = await argon2.hash(password, {
204
241
  type: argon2.argon2i,
205
242
  salt,
206
243
  raw: true,
@@ -219,6 +256,8 @@ module.exports = {
219
256
  publicKeyHexToPem,
220
257
  encrypt,
221
258
  decrypt,
259
+ encryptBuffer,
260
+ decryptBuffer,
222
261
  signPayload,
223
262
  verifyPayload,
224
263
  hashAndEncodeHex,