-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathderiveKey.ts
More file actions
173 lines (155 loc) · 5.86 KB
/
Copy pathderiveKey.ts
File metadata and controls
173 lines (155 loc) · 5.86 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Kms, TypedArrayEncoder } from '@credo-ts/core'
import { Key, NativeAskar } from '@openwallet-foundation/askar-shared'
import { jwkEncToAskarAlg } from '../../utils'
export const askarSupportedKeyAgreementAlgorithms = [
'ECDH-ES',
'ECDH-ES+A128KW',
'ECDH-ES+A256KW',
'ECDH-HSALSA20',
] satisfies Kms.KnownJwaKeyAgreementAlgorithm[]
type AskarSupportedKeyAgreementEncryptOptions = Kms.KmsKeyAgreementEncryptOptions & {
algorithm: (typeof askarSupportedKeyAgreementAlgorithms)[number]
}
type AskarSupportedKeyAgreementDecryptOptions = Kms.KmsKeyAgreementDecryptOptions & {
algorithm: (typeof askarSupportedKeyAgreementAlgorithms)[number]
}
export function deriveEncryptionKey(options: {
keyAgreement: AskarSupportedKeyAgreementEncryptOptions
senderKey: Key
recipientKey: Key
encryption: Kms.KmsEncryptDataEncryption
}) {
const { keyAgreement, encryption, senderKey, recipientKey } = options
const askarEncryptionAlgorithm = jwkEncToAskarAlg[encryption.algorithm as keyof typeof jwkEncToAskarAlg]
if (!askarEncryptionAlgorithm) {
throw new Kms.KeyManagementAlgorithmNotSupportedError(
`encryption with algorithm '${encryption.algorithm}'`,
'askar'
)
}
// This should be handled on a higher level as we only support combined key agreemnt + encryption
if (keyAgreement.algorithm === 'ECDH-HSALSA20') {
throw new Kms.KeyManagementAlgorithmNotSupportedError(
`derive key for algorithm '${keyAgreement.algorithm}' with encryption algorithm '${encryption.algorithm}'`,
'askar'
)
}
const askarKeyWrappingAlgorithm =
keyAgreement.algorithm !== 'ECDH-ES'
? jwkEncToAskarAlg[keyAgreement.algorithm.replace('ECDH-ES+', '') as keyof typeof jwkEncToAskarAlg]
: undefined
const derivedKey = new Key(
NativeAskar.instance.keyDeriveEcdhEs({
algId: TypedArrayEncoder.fromUtf8String(
keyAgreement.algorithm === 'ECDH-ES' ? encryption.algorithm : keyAgreement.algorithm
),
receive: false,
apv: keyAgreement.apv ? new Uint8Array(keyAgreement.apv) : new Uint8Array([]),
apu: keyAgreement.apu ? new Uint8Array(keyAgreement.apu) : new Uint8Array([]),
algorithm: askarKeyWrappingAlgorithm ?? askarEncryptionAlgorithm,
ephemeralKey: senderKey,
recipientKey: recipientKey,
})
)
let contentEncryptionKey: Key | undefined
let encryptedContentEncryptionKey: Kms.KmsEncryptedKey | undefined
try {
// Key wrapping
if (keyAgreement.algorithm !== 'ECDH-ES') {
contentEncryptionKey = Key.generate(askarEncryptionAlgorithm)
const wrappedKey = derivedKey.wrapKey({
other: contentEncryptionKey,
})
encryptedContentEncryptionKey = {
encrypted: new Uint8Array(wrappedKey.ciphertext),
iv: new Uint8Array(wrappedKey.nonce),
tag: new Uint8Array(wrappedKey.tag),
}
}
return {
contentEncryptionKey: contentEncryptionKey ?? derivedKey,
encryptedContentEncryptionKey,
}
} catch (error) {
if (contentEncryptionKey) {
contentEncryptionKey.handle.free()
}
// We only free the derived key if there is no content encryption key
// as in this case the derived key is already freed in the finally clause
else {
derivedKey.handle.free()
}
throw error
} finally {
// If there is a content encryption key, it means we can free the
// derived key
if (contentEncryptionKey) {
derivedKey.handle.free()
}
}
}
export function deriveDecryptionKey(options: {
keyAgreement: AskarSupportedKeyAgreementDecryptOptions
senderKey: Key
recipientKey: Key
decryption: Kms.KmsDecryptDataDecryption
}) {
const { keyAgreement, decryption, senderKey, recipientKey } = options
const askarEncryptionAlgorithm = jwkEncToAskarAlg[decryption.algorithm as keyof typeof jwkEncToAskarAlg]
if (!askarEncryptionAlgorithm) {
throw new Kms.KeyManagementAlgorithmNotSupportedError(
`decryption with algorithm '${decryption.algorithm}'`,
'askar'
)
}
if (keyAgreement.algorithm === 'ECDH-HSALSA20') {
// This should be handled on a higher level as we only support combined key agreemnt + encryption
throw new Kms.KeyManagementAlgorithmNotSupportedError(
`derive key for algorithm '${keyAgreement.algorithm}' with encryption algorithm '${decryption.algorithm}'`,
'askar'
)
}
const askarKeyWrappingAlgorithm =
keyAgreement.algorithm !== 'ECDH-ES'
? jwkEncToAskarAlg[keyAgreement.algorithm.replace('ECDH-ES+', '') as keyof typeof jwkEncToAskarAlg]
: undefined
const derivedKey = new Key(
NativeAskar.instance.keyDeriveEcdhEs({
algId: TypedArrayEncoder.fromUtf8String(
keyAgreement.algorithm === 'ECDH-ES' ? decryption.algorithm : keyAgreement.algorithm
),
receive: true,
apv: keyAgreement.apv ? new Uint8Array(keyAgreement.apv) : new Uint8Array(),
apu: keyAgreement.apu ? new Uint8Array(keyAgreement.apu) : new Uint8Array(),
algorithm: askarKeyWrappingAlgorithm ?? askarEncryptionAlgorithm,
ephemeralKey: senderKey,
recipientKey: recipientKey,
})
)
let contentEncryptionKey: Key | undefined
try {
// Key unwrapping
if (keyAgreement.algorithm !== 'ECDH-ES') {
contentEncryptionKey = derivedKey.unwrapKey({
ciphertext: new Uint8Array(keyAgreement.encryptedKey.encrypted),
algorithm: askarEncryptionAlgorithm,
nonce: keyAgreement.encryptedKey.iv ? new Uint8Array(keyAgreement.encryptedKey.iv) : undefined,
tag: keyAgreement.encryptedKey.tag ? new Uint8Array(keyAgreement.encryptedKey.tag) : undefined,
})
}
return {
contentEncryptionKey: contentEncryptionKey ?? derivedKey,
}
} catch (error) {
if (contentEncryptionKey) {
contentEncryptionKey.handle.free()
} else {
derivedKey.handle.free()
}
throw error
} finally {
if (contentEncryptionKey) {
derivedKey.handle.free()
}
}
}