@@ -86,24 +86,39 @@ Cryptography.Key = class extends Expression.Literal {
86
86
*/
87
87
88
88
switch ( this . key . algorithm . name ) {
89
- case "RSA-OAEP" :
90
- case "RSASSA-PKCS1-v1_5" : {
91
- let arrayBuffer ;
92
-
93
- if ( this . key . type === "private" ) {
94
- arrayBuffer = await window . crypto . subtle . exportKey ( "pkcs8" , this . key ) ;
89
+ case "RSA-OAEP" : // encryption
90
+ case "RSASSA-PKCS1-v1_5" : // signing
91
+ case "RSA-PSS" : // signing
92
+ {
93
+ let arrayBuffer ;
94
+
95
+ if ( this . key . type === "private" ) {
96
+ arrayBuffer = await window . crypto . subtle . exportKey ( "pkcs8" , this . key ) ;
97
+ }
98
+ else { // public
99
+ arrayBuffer = await window . crypto . subtle . exportKey ( "spki" , this . key ) ;
100
+ }
101
+
102
+ return [
103
+ Utils . bytesToBase64 ( new Uint8Array ( arrayBuffer ) ) , // value
104
+ this . key . type , // type
105
+ this . key . algorithm . name , // algorithm
106
+ this . key . algorithm . hash . name // parameter
107
+ ] ;
95
108
}
96
- else {
97
- arrayBuffer = await window . crypto . subtle . exportKey ( "spki" , this . key ) ;
109
+
110
+ case "AES-CTR" :
111
+ case "AES-CBC" :
112
+ case "AES-GCM" :
113
+ {
114
+ let arrayBuffer = await window . crypto . subtle . exportKey ( "raw" , this . key ) ;
115
+ return [
116
+ Utils . bytesToBase64 ( new Uint8Array ( arrayBuffer ) ) , // value
117
+ this . key . type , // type
118
+ this . key . algorithm . name , // algorithm
119
+ this . key . algorithm . length . toString ( ) // parameter
120
+ ] ;
98
121
}
99
-
100
- return [
101
- Utils . bytesToBase64 ( new Uint8Array ( arrayBuffer ) ) , // value
102
- this . key . type , // type
103
- this . key . algorithm . name , // algorithm
104
- this . key . algorithm . hash . name // parameter
105
- ] ;
106
- }
107
122
}
108
123
}
109
124
@@ -143,21 +158,35 @@ Cryptography.Key = class extends Expression.Literal {
143
158
*/
144
159
145
160
switch ( algorithmName ) {
146
- case "RSA-OAEP" :
147
- case "RSASSA-PKCS1-v1_5" : {
148
- if ( type == "private" ) {
149
- format = "pkcs8" ;
150
- keyData = Utils . base64ToBytes ( strings [ 0 ] ) ;
151
- algorithm = { name : algorithmName , hash : parameter } ;
152
- keyUsages = [ algorithmName == "RSA-OAEP" ? "decrypt" : "sign" ] ;
161
+ case "RSA-OAEP" : // encryption
162
+ case "RSASSA-PKCS1-v1_5" : // signing
163
+ case "RSA-PSS" : // signing
164
+ {
165
+ if ( type == "private" ) {
166
+ format = "pkcs8" ;
167
+ keyData = Utils . base64ToBytes ( strings [ 0 ] ) ;
168
+ algorithm = { name : algorithmName , hash : parameter } ;
169
+ keyUsages = [ algorithmName == "RSA-OAEP" ? "decrypt" : "sign" ] ;
170
+ }
171
+ else { // public
172
+ format = "spki" ;
173
+ keyData = Utils . base64ToBytes ( strings [ 0 ] ) ;
174
+ algorithm = { name : algorithmName , hash : parameter } ;
175
+ keyUsages = [ algorithmName == "RSA-OAEP" ? "encrypt" : "verify" ] ;
176
+ }
153
177
}
154
- else {
155
- format = "spki" ;
178
+ break ;
179
+
180
+ case "AES-CTR" :
181
+ case "AES-CBC" :
182
+ case "AES-GCM" :
183
+ {
184
+ format = "raw" ;
156
185
keyData = Utils . base64ToBytes ( strings [ 0 ] ) ;
157
- algorithm = { name : algorithmName , hash : parameter } ;
158
- keyUsages = [ algorithmName == "RSA-OAEP" ? " encrypt" : "verify " ] ;
186
+ algorithm = { name : algorithmName , length : Number ( parameter ) } ;
187
+ keyUsages = [ " encrypt", "decrypt " ] ;
159
188
}
160
- }
189
+ break ;
161
190
}
162
191
163
192
let promise = window . crypto . subtle . importKey (
@@ -174,7 +203,7 @@ Cryptography.Key = class extends Expression.Literal {
174
203
}
175
204
176
205
getLiteral ( ) {
177
- return "<" + this . key . type + " - " + this . key . usages + ">" ;
206
+ return "<" + this . key . type + " - " + this . key . usages . join ( ", " ) + ">" ;
178
207
}
179
208
}
180
209
@@ -191,14 +220,17 @@ Cryptography.setExpressions = function(module) {
191
220
)
192
221
) ;
193
222
223
+ // Operations
224
+
194
225
[
195
- [ "Key" , "GenerateAsymmetricKeysForEncryption" , 1 , 1 ] ,
196
- [ "Key" , "GenerateAsymmetricKeysForSigning" , 1 , 1 ] ,
197
- [ "Encryption" , "Encrypt" , 2 , 2 ] ,
198
- [ "Encryption" , "Decrypt" , 2 , 2 ] ,
226
+ [ "Key" , "GenerateAsymmetricKeysForEncryption" , 1 , 2 ] ,
227
+ [ "Key" , "GenerateSymmetricKeyForEncryption" , 1 , 2 ] ,
228
+ [ "Key" , "GenerateAsymmetricKeysForSigning" , 1 , 2 ] ,
229
+ [ "Encryption" , "Encrypt" , 2 , 3 ] ,
230
+ [ "Encryption" , "Decrypt" , 2 , 3 ] ,
199
231
[ "Hashing" , "Hash" , 2 , 2 ] ,
200
- [ "Signing" , "Sign" , 2 , 2 ] ,
201
- [ "Signing" , "Verify" , 3 , 3 ] ,
232
+ [ "Signing" , "Sign" , 2 , 3 ] ,
233
+ [ "Signing" , "Verify" , 3 , 4 ] ,
202
234
] . forEach (
203
235
row => Formulae . setExpression (
204
236
module ,
@@ -215,14 +247,43 @@ Cryptography.setExpressions = function(module) {
215
247
)
216
248
) ;
217
249
218
- [ "SHA-1" , "SHA-256" , "SHA-384" , "SHA-512" ] . forEach (
219
- tag => Formulae . setExpression (
250
+ Formulae . setExpression (
251
+ module ,
252
+ "Cryptography.Random" ,
253
+ {
254
+ clazz : Expression . Function ,
255
+ getTag : ( ) => "Cryptography.Random" ,
256
+ getMnemonic : ( ) => Cryptography . messages . mnemonicRandom ,
257
+ getName : ( ) => Cryptography . messages . nameRandom ,
258
+ getChildName : index => Cryptography . messages . childRandom ,
259
+ min : 1 ,
260
+ max : 1
261
+ }
262
+ ) ;
263
+
264
+ // Algorithms
265
+
266
+ [
267
+ [ "Hashing" , "SHA-1" ] ,
268
+ [ "Hashing" , "SHA-256" ] ,
269
+ [ "Hashing" , "SHA-384" ] ,
270
+ [ "Hashing" , "SHA-512" ] ,
271
+ [ "AsymmetricEncryption" , "RSA-OAEP" ] ,
272
+ [ "SymmetricEncryption" , "AES-CTR" ] ,
273
+ [ "SymmetricEncryption" , "AES-CBC" ] ,
274
+ [ "SymmetricEncryption" , "AES-GCM" ] ,
275
+ [ "Signing" , "RSASSA-PKCS1-v1_5" ] ,
276
+ [ "Signing" , "RSA-PSS" ] ,
277
+ [ "Signing" , "ECDSA" ] ,
278
+ [ "Signing" , "HMAC" ] ,
279
+ ] . forEach (
280
+ row => Formulae . setExpression (
220
281
module ,
221
- "Cryptography.Hashing. Algorithm." + tag ,
282
+ "Cryptography.Algorithm." + row [ 0 ] + "." + row [ 1 ] ,
222
283
{
223
284
clazz : Expression . LabelExpression ,
224
- getTag : ( ) => "Cryptography.Hashing. Algorithm." + tag ,
225
- getLabel : ( ) => tag ,
285
+ getTag : ( ) => "Cryptography.Algorithm." + row [ 0 ] + "." + row [ 1 ] ,
286
+ getLabel : ( ) => row [ 1 ] ,
226
287
getName : ( ) => "yyy"
227
288
}
228
289
)
0 commit comments