-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathpbes2kw.ts
64 lines (56 loc) · 2 KB
/
pbes2kw.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { promisify } from 'util'
import { KeyObject, pbkdf2 as pbkdf2cb } from 'crypto'
import type { Pbes2KWDecryptFunction, Pbes2KWEncryptFunction } from '../interfaces.d'
import random from './random.js'
import { p2s as concatSalt } from '../../lib/buffer_utils.js'
import { encode as base64url } from './base64url.js'
import { wrap, unwrap } from './aeskw.js'
import checkP2s from '../../lib/check_p2s.js'
import { isCryptoKey } from './webcrypto.js'
import { checkEncCryptoKey } from '../../lib/crypto_key.js'
import isKeyObject from './is_key_object.js'
import invalidKeyInput from '../../lib/invalid_key_input.js'
import { types } from './is_key_like.js'
const pbkdf2 = promisify(pbkdf2cb)
function getPassword(key: unknown, alg: string) {
if (isKeyObject(key)) {
return key.export()
}
if (key instanceof Uint8Array) {
return key
}
if (isCryptoKey(key)) {
checkEncCryptoKey(key, alg, 'deriveBits', 'deriveKey')
return KeyObject.from(key).export()
}
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'))
}
export const encrypt: Pbes2KWEncryptFunction = async (
alg: string,
key: unknown,
cek: Uint8Array,
p2c: number = 2048,
p2s: Uint8Array = random(new Uint8Array(16)),
) => {
checkP2s(p2s)
const salt = concatSalt(alg, p2s)
const keylen = parseInt(alg.slice(13, 16), 10) >> 3
const password = getPassword(key, alg)
const derivedKey = await pbkdf2(password, salt, p2c, keylen, `sha${alg.slice(8, 11)}`)
const encryptedKey = await wrap(alg.slice(-6), derivedKey, cek)
return { encryptedKey, p2c, p2s: base64url(p2s) }
}
export const decrypt: Pbes2KWDecryptFunction = async (
alg: string,
key: unknown,
encryptedKey: Uint8Array,
p2c: number,
p2s: Uint8Array,
) => {
checkP2s(p2s)
const salt = concatSalt(alg, p2s)
const keylen = parseInt(alg.slice(13, 16), 10) >> 3
const password = getPassword(key, alg)
const derivedKey = await pbkdf2(password, salt, p2c, keylen, `sha${alg.slice(8, 11)}`)
return unwrap(alg.slice(-6), derivedKey, encryptedKey)
}