Skip to content
This repository was archived by the owner on Feb 25, 2019. It is now read-only.

Commit d2530cf

Browse files
author
Ioan Budea
committed
feat(decrypt): completed decrypt method in JWT.js, added parameter for determineCek in KeyManagement class
1 parent 6c0556b commit d2530cf

File tree

4 files changed

+221
-102
lines changed

4 files changed

+221
-102
lines changed

examples/A128GCM-JWT.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ crypto.subtle
1313
.generateKey(
1414
{
1515
name: 'AES-GCM',
16-
length: 128
16+
length: 256
1717
},
1818
false,
1919
['encrypt', 'decrypt']

src/KeyManagement.js

+45-29
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55
const crypto = require('@trust/webcrypto')
66
const NotSupportedError = require('./errors/NotSupportedError')
7+
const { JWA } = require('@trust/jwa')
78

89
/**
9-
* SupportedAlgorithms
10+
* KeyManagement
1011
*/
1112
class KeyManagement {
1213

@@ -17,67 +18,82 @@ class KeyManagement {
1718
// Entries for key algorithms used to decide on
1819
// cek and compute the encrypted key
1920
this.keyAlgorithms = new Map([
20-
['dir', { mode: this.directEncryption }]
21+
['dir', { encrypt: this.direct, decrypt: this.direct }]
2122
])
2223
}
2324

24-
directEncryption (alg, key) {
25+
direct (alg, key) {
2526
return {
2627
cek: key,
2728
encrypted_key: new Uint8Array()
2829
}
2930
}
3031

3132
keyWrapOrEncrypt (alg, key) {
32-
let cek = new Uint8Array(this.keyAlgorithms.get(alg).cekLength)
33+
let cek, encrypted_key
34+
cek = new Uint8Array(this.keyAlgorithms.get(alg).cekLength)
3335
cek = crypto.getRandomValues(cek)
34-
let encrypted_key = JWA.encrypt(alg, key, cek)
35-
return {
36-
cek,
37-
encrypted_key
38-
}
36+
JWA.encryptKey(alg, cek, key)
37+
.then(result => {
38+
encrypted_key = result
39+
40+
return {
41+
cek,
42+
encrypted_key
43+
}
44+
})
3945
}
4046

4147
keyAgreeAndWrap (alg, key) {
4248
let cek = new Uint8Array(this.keyAlgorithms.get(alg).cekLength)
4349
cek = crypto.getRandomValues(cek)
4450
// use alg to agree on the key
45-
let agreedKey
46-
// probably this is not encrypt, but wrap
47-
// the spec is confusing
48-
let encrypted_key = JWA.encrypt(alg, agreedKey, cek)
49-
return {
50-
cek,
51-
encrypted_key
52-
}
51+
JWA.generateKey(alg)
52+
.then(agreedKey => {
53+
JWA.encryptKey(alg, cek, agreedKey)
54+
.then(result => {
55+
encrypted_key = result
56+
57+
return {
58+
cek,
59+
encrypted_key
60+
}
61+
})
62+
})
5363
}
5464

5565
directAgree (alg, key) {
56-
let agreedKey
57-
let cek = agreedKey
58-
return {
59-
cek,
60-
encrypted_key: new Uint8Array()
61-
}
66+
JWA.generateKey(alg)
67+
.then(agreedKey => {
68+
return {
69+
cek: agreedKey,
70+
encrypted_key: new Uint8Array()
71+
}
72+
})
6273
}
6374

6475
/**
65-
* normalize
76+
* determineCek
6677
*
6778
* @description
68-
* Call the corresponding method for the
69-
* algorithm type based on JWA alg name
79+
* Call the corresponding method for the algorithm type
80+
* based on JWA alg name
7081
*
82+
* @param {Boolean} verify
7183
* @param {Object} alg
7284
* @param {Object} key
7385
*
74-
* @returns {Object}
86+
* @returns {Promise}
7587
*/
76-
normalize (alg, key) {
88+
determineCek (verify, alg, key) {
7789
if (!this.keyAlgorithms.get(alg)) {
7890
throw new NotSupportedError("Key Algorithm is not supported")
7991
}
80-
return (this.keyAlgorithms.get(alg).mode)(alg, key)
92+
if (!verify) {
93+
return (this.keyAlgorithms.get(alg).encrypt)(alg, key)
94+
} else {
95+
return (this.keyAlgorithms.get(alg).decrypt)(alg, key)
96+
}
8197
}
8298
}
8399

0 commit comments

Comments
 (0)