4
4
*/
5
5
const crypto = require ( '@trust/webcrypto' )
6
6
const NotSupportedError = require ( './errors/NotSupportedError' )
7
+ const { JWA } = require ( '@trust/jwa' )
7
8
8
9
/**
9
- * SupportedAlgorithms
10
+ * KeyManagement
10
11
*/
11
12
class KeyManagement {
12
13
@@ -17,67 +18,82 @@ class KeyManagement {
17
18
// Entries for key algorithms used to decide on
18
19
// cek and compute the encrypted key
19
20
this . keyAlgorithms = new Map ( [
20
- [ 'dir' , { mode : this . directEncryption } ]
21
+ [ 'dir' , { encrypt : this . direct , decrypt : this . direct } ]
21
22
] )
22
23
}
23
24
24
- directEncryption ( alg , key ) {
25
+ direct ( alg , key ) {
25
26
return {
26
27
cek : key ,
27
28
encrypted_key : new Uint8Array ( )
28
29
}
29
30
}
30
31
31
32
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 )
33
35
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
+ } )
39
45
}
40
46
41
47
keyAgreeAndWrap ( alg , key ) {
42
48
let cek = new Uint8Array ( this . keyAlgorithms . get ( alg ) . cekLength )
43
49
cek = crypto . getRandomValues ( cek )
44
50
// 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
+ } )
53
63
}
54
64
55
65
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
+ } )
62
73
}
63
74
64
75
/**
65
- * normalize
76
+ * determineCek
66
77
*
67
78
* @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
70
81
*
82
+ * @param {Boolean } verify
71
83
* @param {Object } alg
72
84
* @param {Object } key
73
85
*
74
- * @returns {Object }
86
+ * @returns {Promise }
75
87
*/
76
- normalize ( alg , key ) {
88
+ determineCek ( verify , alg , key ) {
77
89
if ( ! this . keyAlgorithms . get ( alg ) ) {
78
90
throw new NotSupportedError ( "Key Algorithm is not supported" )
79
91
}
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
+ }
81
97
}
82
98
}
83
99
0 commit comments