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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use aes::cipher::crypto_common::Key;
use aes::{Aes128, Aes192, Aes256};
use js::context::JSContext;
use pkcs8::rand_core::{OsRng, RngCore};
use crate::dom::bindings::codegen::Bindings::CryptoKeyBinding::{
CryptoKeyMethods, KeyType, KeyUsage,
};
use crate::dom::bindings::codegen::Bindings::SubtleCryptoBinding::{JsonWebKey, KeyFormat};
use crate::dom::bindings::error::Error;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::cryptokey::{CryptoKey, Handle};
use crate::dom::globalscope::GlobalScope;
use crate::dom::subtlecrypto::{
CryptoAlgorithm, ExportedKey, JsonWebKeyExt, JwkStringField, KeyAlgorithmAndDerivatives,
SubtleAesDerivedKeyParams, SubtleAesKeyAlgorithm, SubtleAesKeyGenParams,
};
#[expect(clippy::enum_variant_names)]
pub(crate) enum AesAlgorithm {
AesCtr,
AesCbc,
AesGcm,
AesKw,
AesOcb,
}
/// <https://w3c.github.io/webcrypto/#aes-ctr-operations-generate-key>
/// <https://w3c.github.io/webcrypto/#aes-cbc-operations-generate-key>
/// <https://w3c.github.io/webcrypto/#aes-gcm-operations-generate-key>
/// <https://w3c.github.io/webcrypto/#aes-kw-operations-generate-key>
/// <https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-generate-key>
///
/// The step order in the specification of AES-OCB is slightly different, but it is equivalent to
/// this implementation.
pub(crate) fn generate_key(
aes_algorithm: AesAlgorithm,
cx: &mut JSContext,
global: &GlobalScope,
normalized_algorithm: &SubtleAesKeyGenParams,
extractable: bool,
usages: Vec<KeyUsage>,
) -> Result<DomRoot<CryptoKey>, Error> {
match aes_algorithm {
AesAlgorithm::AesCtr |
AesAlgorithm::AesCbc |
AesAlgorithm::AesGcm |
AesAlgorithm::AesOcb => {
// Step 1. If usages contains any entry which is not one of "encrypt", "decrypt",
// "wrapKey" or "unwrapKey", then throw a SyntaxError.
if usages.iter().any(|usage| {
!matches!(
usage,
KeyUsage::Encrypt | KeyUsage::Decrypt | KeyUsage::WrapKey | KeyUsage::UnwrapKey
)
}) {
return Err(Error::Syntax(Some(
"Usages contains an entry which is not one of \"encrypt\", \"decrypt\", \
\"wrapKey\" or \"unwrapKey\""
.to_string(),
)));
}
},
AesAlgorithm::AesKw => {
// Step 1. If usages contains any entry which is not one of "wrapKey" or "unwrapKey",
// then throw a SyntaxError.
if usages
.iter()
.any(|usage| !matches!(usage, KeyUsage::WrapKey | KeyUsage::UnwrapKey))
{
return Err(Error::Syntax(Some(
"Usages contains an entry which is not one of \"wrapKey\" or \"unwrapKey\""
.to_string(),
)));
}
},
}
// Step 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256,
// then throw an OperationError.
// Step 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.
// Step 4. If the key generation step fails, then throw an OperationError.
let handle =
match normalized_algorithm.length {
128 => {
let mut key_bytes = vec![0; 16];
OsRng.fill_bytes(&mut key_bytes);
Handle::Aes128Key(Key::<Aes128>::clone_from_slice(&key_bytes))
},
192 => {
let mut key_bytes = vec![0; 24];
OsRng.fill_bytes(&mut key_bytes);
Handle::Aes192Key(Key::<Aes192>::clone_from_slice(&key_bytes))
},
256 => {
let mut key_bytes = vec![0; 32];
OsRng.fill_bytes(&mut key_bytes);
Handle::Aes256Key(Key::<Aes256>::clone_from_slice(&key_bytes))
},
_ => return Err(Error::Operation(Some(
"The length member of normalizedAlgorithm is not equal to one of 128, 192 or 256"
.to_string(),
))),
};
// Step 5. Let key be a new CryptoKey object representing the generated AES key.
// Step 6. Let algorithm be a new AesKeyAlgorithm.
// Step 8. Set the length attribute of algorithm to equal the length member of
// normalizedAlgorithm.
// Step 9. Set the [[type]] internal slot of key to "secret".
// Step 10. Set the [[algorithm]] internal slot of key to algorithm.
// Step 11. Set the [[extractable]] internal slot of key to be extractable.
// Step 12. Set the [[usages]] internal slot of key to be usages.
let algorithm_name = match aes_algorithm {
AesAlgorithm::AesCtr => {
// Step 7. Set the name attribute of algorithm to "AES-CTR".
CryptoAlgorithm::AesCtr
},
AesAlgorithm::AesCbc => {
// Step 7. Set the name attribute of algorithm to "AES-CBC".
CryptoAlgorithm::AesCbc
},
AesAlgorithm::AesGcm => {
// Step 7. Set the name attribute of algorithm to "AES-GCM".
CryptoAlgorithm::AesGcm
},
AesAlgorithm::AesKw => {
// Step 7. Set the name attribute of algorithm to "AES-KW".
CryptoAlgorithm::AesKw
},
AesAlgorithm::AesOcb => {
// Step 7. Set the name attribute of algorithm to "AES-OCB".
CryptoAlgorithm::AesOcb
},
};
let algorithm = SubtleAesKeyAlgorithm {
name: algorithm_name,
length: normalized_algorithm.length,
};
let key = CryptoKey::new(
cx,
global,
KeyType::Secret,
extractable,
KeyAlgorithmAndDerivatives::AesKeyAlgorithm(algorithm),
usages,
handle,
);
// Step 13. Return key.
Ok(key)
}
/// <https://w3c.github.io/webcrypto/#aes-ctr-operations-import-key>
/// <https://w3c.github.io/webcrypto/#aes-cbc-operations-import-key>
/// <https://w3c.github.io/webcrypto/#aes-gcm-operations-import-key>
/// <https://w3c.github.io/webcrypto/#aes-kw-operations-import-key>
/// <https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-import-key>
///
/// The specification of AES-OCB has one more step at the beginning of the operation:
///
/// > Let keyData be the key data to be imported.
///
/// As it is simply used to name the variable, it is safe to omit it in the implementation below to
/// align with the specification of other AES algorithms.
pub(crate) fn import_key(
aes_algorithm: AesAlgorithm,
cx: &mut JSContext,
global: &GlobalScope,
format: KeyFormat,
key_data: &[u8],
extractable: bool,
usages: Vec<KeyUsage>,
) -> Result<DomRoot<CryptoKey>, Error> {
match &aes_algorithm {
AesAlgorithm::AesCtr |
AesAlgorithm::AesCbc |
AesAlgorithm::AesGcm |
AesAlgorithm::AesOcb => {
// Step 1. If usages contains an entry which is not one of "encrypt", "decrypt",
// "wrapKey" or "unwrapKey", then throw a SyntaxError.
if usages.iter().any(|usage| {
!matches!(
usage,
KeyUsage::Encrypt | KeyUsage::Decrypt | KeyUsage::WrapKey | KeyUsage::UnwrapKey
)
}) {
return Err(Error::Syntax(Some(
"Usages contains an entry which is not one of \"encrypt\", \"decrypt\", \
\"wrapKey\" or \"unwrapKey\""
.to_string(),
)));
}
},
AesAlgorithm::AesKw => {
// Step 1. If usages contains an entry which is not one of "wrapKey" or "unwrapKey",
// then throw a SyntaxError.
if usages
.iter()
.any(|usage| !matches!(usage, KeyUsage::WrapKey | KeyUsage::UnwrapKey))
{
return Err(Error::Syntax(Some(
"Usages contains an entry which is not one of \"wrapKey\" or \"unwrapKey\""
.to_string(),
)));
}
},
}
// Step 2.
let data;
match format {
// If format is "raw": (Only applied to AES-CTR, AES-CBC, AES-GCM, AES-KW)
KeyFormat::Raw
if matches!(
aes_algorithm,
AesAlgorithm::AesCtr |
AesAlgorithm::AesCbc |
AesAlgorithm::AesGcm |
AesAlgorithm::AesKw
) =>
{
// Step 2.1. Let data be keyData.
data = key_data.to_vec();
// Step 2.2. If the length in bits of data is not 128, 192 or 256 then throw a
// DataError.
if !matches!(data.len(), 16 | 24 | 32) {
return Err(Error::Data(Some(
"The length in bits of data is not 128, 192 or 256".to_string(),
)));
}
},
// If format is "raw-secret":
KeyFormat::Raw_secret => {
// Step 2.1. Let data be keyData.
data = key_data.to_vec();
// Step 2.2. If the length in bits of data is not 128, 192 or 256 then throw a
// DataError.
if !matches!(data.len(), 16 | 24 | 32) {
return Err(Error::Data(Some(
"The length in bits of key is not 128, 192 or 256".to_string(),
)));
}
},
// If format is "jwk":
KeyFormat::Jwk => {
// Step 2.1.
// If keyData is a JsonWebKey dictionary:
// Let jwk equal keyData.
// Otherwise:
// Throw a DataError.
let jwk = JsonWebKey::parse(cx, key_data)?;
// Step 2.2. If the kty field of jwk is not "oct", then throw a DataError.
if jwk.kty.as_ref().is_none_or(|kty| kty != "oct") {
return Err(Error::Data(Some(
"The kty field of jwk is not \"oct\"".to_string(),
)));
}
// Step 2.3. If jwk does not meet the requirements of Section 6.4 of JSON Web
// Algorithms [JWA], then throw a DataError.
// Step 2.4. Let data be the byte sequence obtained by decoding the k field of jwk.
data = jwk.decode_required_string_field(JwkStringField::K)?;
match aes_algorithm {
AesAlgorithm::AesCtr => {
// Step 2.5.
// If data has length 128 bits:
// If the alg field of jwk is present, and is not "A128CTR", then throw a
// DataError.
// If data has length 192 bits:
// If the alg field of jwk is present, and is not "A192CTR", then throw a
// DataError.
// If data has length 256 bits:
// If the alg field of jwk is present, and is not "A256CTR", then throw a
// DataError.
// Otherwise:
// throw a DataError.
let expected_alg = match data.len() {
16 => "A128CTR",
24 => "A192CTR",
32 => "A256CTR",
_ => {
return Err(Error::Data(Some(
"The length in bits of data is not 128, 192 or 256".to_string(),
)));
},
};
if jwk.alg.as_ref().is_none_or(|alg| alg != expected_alg) {
return Err(Error::Data(Some(format!(
"The alg field of jwk is present, and is not {}",
expected_alg
))));
}
},
AesAlgorithm::AesCbc => {
// Step 2.5.
// If data has length 128 bits:
// If the alg field of jwk is present, and is not "A128CBC", then throw a
// DataError.
// If data has length 192 bits:
// If the alg field of jwk is present, and is not "A192CBC", then throw a
// DataError.
// If data has length 256 bits:
// If the alg field of jwk is present, and is not "A256CBC", then throw a
// DataError.
// Otherwise:
// throw a DataError.
let expected_alg = match data.len() {
16 => "A128CBC",
24 => "A192CBC",
32 => "A256CBC",
_ => {
return Err(Error::Data(Some(
"The length in bits of data is not 128, 192 or 256".to_string(),
)));
},
};
if jwk.alg.as_ref().is_none_or(|alg| alg != expected_alg) {
return Err(Error::Data(Some(format!(
"The alg field of jwk is present, and is not {}",
expected_alg
))));
}
},
AesAlgorithm::AesGcm => {
// Step 2.5.
// If data has length 128 bits:
// If the alg field of jwk is present, and is not "A128GCM", then throw a
// DataError.
// If data has length 192 bits:
// If the alg field of jwk is present, and is not "A192GCM", then throw a
// DataError.
// If data has length 256 bits:
// If the alg field of jwk is present, and is not "A256GCM", then throw a
// DataError.
// Otherwise:
// throw a DataError.
let expected_alg = match data.len() {
16 => "A128GCM",
24 => "A192GCM",
32 => "A256GCM",
_ => {
return Err(Error::Data(Some(
"The length in bits of data is not 128, 192 or 256".to_string(),
)));
},
};
if jwk.alg.as_ref().is_none_or(|alg| alg != expected_alg) {
return Err(Error::Data(Some(format!(
"The alg field of jwk is present, and is not {}",
expected_alg
))));
}
},
AesAlgorithm::AesKw => {
// Step 2.5.
// If data has length 128 bits:
// If the alg field of jwk is present, and is not "A128KW", then throw a
// DataError.
// If data has length 192 bits:
// If the alg field of jwk is present, and is not "A192KW", then throw a
// DataError.
// If data has length 256 bits:
// If the alg field of jwk is present, and is not "A256KW", then throw a
// DataError.
// Otherwise:
// throw a DataError.
let expected_alg = match data.len() {
16 => "A128KW",
24 => "A192KW",
32 => "A256KW",
_ => {
return Err(Error::Data(Some(
"The length in bits of data is not 128, 192 or 256".to_string(),
)));
},
};
if jwk.alg.as_ref().is_none_or(|alg| alg != expected_alg) {
return Err(Error::Data(Some(format!(
"The alg field of jwk is present, and is not {}",
expected_alg
))));
}
},
AesAlgorithm::AesOcb => {
// Step 2.5.
// If data has length 128 bits:
// If the alg field of jwk is present, and is not "A128OCB", then throw a
// DataError.
// If data has length 192 bits:
// If the alg field of jwk is present, and is not "A192OCB", then throw a
// DataError.
// If data has length 256 bits:
// If the alg field of jwk is present, and is not "A256OCB", then throw a
// DataError.
// Otherwise:
// throw a DataError.
let expected_alg = match data.len() {
16 => "A128OCB",
24 => "A192OCB",
32 => "A256OCB",
_ => {
return Err(Error::Data(Some(
"The length in bits of key is not 128, 192 or 256".to_string(),
)));
},
};
if jwk.alg.as_ref().is_none_or(|alg| alg != expected_alg) {
return Err(Error::Data(Some(format!(
"The alg field of jwk is present, and is not {}",
expected_alg
))));
}
},
}
// Step 2.6. If usages is non-empty and the use field of jwk is present and is not
// "enc", then throw a DataError.
if !usages.is_empty() && jwk.use_.as_ref().is_some_and(|use_| use_ != "enc") {
return Err(Error::Data(Some(
"Usages is non-empty and the use field of jwk is present and is not \"enc\""
.to_string(),
)));
}
// Step 2.7. If the key_ops field of jwk is present, and is invalid according to the
// requirements of JSON Web Key [JWK] or does not contain all of the specified usages
// values, then throw a DataError.
jwk.check_key_ops(&usages)?;
// Step 2.8. If the ext field of jwk is present and has the value false and extractable
// is true, then throw a DataError.
if jwk.ext.is_some_and(|ext| !ext) && extractable {
return Err(Error::Data(Some(
"The ext field of jwk is present and has the value false and \
extractable is true"
.to_string(),
)));
}
},
// Otherwise:
_ => {
// throw a NotSupportedError.
return Err(Error::NotSupported(Some(
"Unupported import key format for AES key".to_string(),
)));
},
}
// Step 3. Let key be a new CryptoKey object representing an AES key with value data.
// Step 4. Set the [[type]] internal slot of key to "secret".
// Step 5. Let algorithm be a new AesKeyAlgorithm.
// Step 7. Set the length attribute of algorithm to the length, in bits, of data.
// Step 8. Set the [[algorithm]] internal slot of key to algorithm.
let handle = match data.len() {
16 => Handle::Aes128Key(Key::<Aes128>::clone_from_slice(&data)),
24 => Handle::Aes192Key(Key::<Aes192>::clone_from_slice(&data)),
32 => Handle::Aes256Key(Key::<Aes256>::clone_from_slice(&data)),
_ => {
return Err(Error::Data(Some(
"The length in bits of data is not 128, 192 or 256".to_string(),
)));
},
};
let algorithm = SubtleAesKeyAlgorithm {
name: match &aes_algorithm {
AesAlgorithm::AesCtr => {
// Step 6. Set the name attribute of algorithm to "AES-CTR".
CryptoAlgorithm::AesCtr
},
AesAlgorithm::AesCbc => {
// Step 6. Set the name attribute of algorithm to "AES-CBC".
CryptoAlgorithm::AesCbc
},
AesAlgorithm::AesGcm => {
// Step 6. Set the name attribute of algorithm to "AES-GCM".
CryptoAlgorithm::AesGcm
},
AesAlgorithm::AesKw => {
// Step 6. Set the name attribute of algorithm to "AES-KW".
CryptoAlgorithm::AesKw
},
AesAlgorithm::AesOcb => {
// Step 6. Set the name attribute of algorithm to "AES-OCB".
CryptoAlgorithm::AesOcb
},
},
length: data.len() as u16 * 8,
};
let key = CryptoKey::new(
cx,
global,
KeyType::Secret,
extractable,
KeyAlgorithmAndDerivatives::AesKeyAlgorithm(algorithm),
usages,
handle,
);
// Step 9. Return key.
Ok(key)
}
/// <https://w3c.github.io/webcrypto/#aes-ctr-operations-export-key>
/// <https://w3c.github.io/webcrypto/#aes-cbc-operations-export-key>
/// <https://w3c.github.io/webcrypto/#aes-gcm-operations-export-key>
/// <https://w3c.github.io/webcrypto/#aes-kw-operations-export-key>
/// <https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-export-key>
pub(crate) fn export_key(
aes_algorithm: AesAlgorithm,
format: KeyFormat,
key: &CryptoKey,
) -> Result<ExportedKey, Error> {
// Step 1. If the underlying cryptographic key material represented by the [[handle]] internal
// slot of key cannot be accessed, then throw an OperationError.
// Step 2.
let result = match format {
// If format is "raw": (Only applied to AES-CTR, AES-CBC, AES-GCM, AES-KW)
KeyFormat::Raw
if matches!(
aes_algorithm,
AesAlgorithm::AesCtr |
AesAlgorithm::AesCbc |
AesAlgorithm::AesGcm |
AesAlgorithm::AesKw
) =>
{
// Step 2.1. Let data be a byte sequence containing the raw octets of the key
// represented by [[handle]] internal slot of key.
let data = match key.handle() {
Handle::Aes128Key(key) => key.to_vec(),
Handle::Aes192Key(key) => key.to_vec(),
Handle::Aes256Key(key) => key.to_vec(),
_ => {
return Err(Error::Operation(Some(
"The key handle is not representing an AES key".to_string(),
)));
},
};
// Step 2.2. Let result be data.
ExportedKey::Bytes(data)
},
// If format is "raw-secret":
KeyFormat::Raw_secret => {
// Step 2.1. Let data be a byte sequence containing the raw octets of the key
// represented by [[handle]] internal slot of key.
let data = match key.handle() {
Handle::Aes128Key(key) => key.to_vec(),
Handle::Aes192Key(key) => key.to_vec(),
Handle::Aes256Key(key) => key.to_vec(),
_ => {
return Err(Error::Operation(Some(
"The key handle is not representing an AES key".to_string(),
)));
},
};
// Step 2.2. Let result be data.
ExportedKey::Bytes(data)
},
// If format is "jwk":
KeyFormat::Jwk => {
// Step 2.1. Let jwk be a new JsonWebKey dictionary.
// Step 2.2. Set the kty attribute of jwk to the string "oct".
let mut jwk = JsonWebKey {
kty: Some(DOMString::from("oct")),
..Default::default()
};
// Step 2.3. Set the k attribute of jwk to be a string containing the raw octets of the
// key represented by [[handle]] internal slot of key, encoded according to Section 6.4
// of JSON Web Algorithms [JWA].
let key_bytes = match key.handle() {
Handle::Aes128Key(key) => key.as_slice(),
Handle::Aes192Key(key) => key.as_slice(),
Handle::Aes256Key(key) => key.as_slice(),
_ => {
return Err(Error::Operation(Some(
"The key handle is not representing an AES key".to_string(),
)));
},
};
jwk.encode_string_field(JwkStringField::K, key_bytes);
match aes_algorithm {
AesAlgorithm::AesCtr => {
// Step 2.4.
// If the length attribute of key is 128:
// Set the alg attribute of jwk to the string "A128CTR".
// If the length attribute of key is 192:
// Set the alg attribute of jwk to the string "A192CTR".
// If the length attribute of key is 256:
// Set the alg attribute of jwk to the string "A256CTR".
let KeyAlgorithmAndDerivatives::AesKeyAlgorithm(algorithm) = key.algorithm()
else {
return Err(Error::Operation(None));
};
let alg = match algorithm.length {
128 => "A128CTR",
192 => "A192CTR",
256 => "A256CTR",
_ => return Err(Error::Operation(Some(
"The length attribute of the [[algorithm]] internal slot of key is not \
128, 192 or 256".to_string(),
)))
};
jwk.alg = Some(DOMString::from(alg));
},
AesAlgorithm::AesCbc => {
// Step 2.4.
// If the length attribute of key is 128:
// Set the alg attribute of jwk to the string "A128CBC".
// If the length attribute of key is 192:
// Set the alg attribute of jwk to the string "A192CBC".
// If the length attribute of key is 256:
// Set the alg attribute of jwk to the string "A256CBC".
let KeyAlgorithmAndDerivatives::AesKeyAlgorithm(algorithm) = key.algorithm()
else {
return Err(Error::Operation(None));
};
let alg = match algorithm.length {
128 => "A128CBC",
192 => "A192CBC",
256 => "A256CBC",
_ => return Err(Error::Operation(Some(
"The length attribute of the [[algorithm]] internal slot of key is not \
128, 192 or 256".to_string(),
)))
};
jwk.alg = Some(DOMString::from(alg));
},
AesAlgorithm::AesGcm => {
// Step 2.4.
// If the length attribute of key is 128:
// Set the alg attribute of jwk to the string "A128GCM".
// If the length attribute of key is 192:
// Set the alg attribute of jwk to the string "A192GCM".
// If the length attribute of key is 256:
// Set the alg attribute of jwk to the string "A256GCM".
let KeyAlgorithmAndDerivatives::AesKeyAlgorithm(algorithm) = key.algorithm()
else {
return Err(Error::Operation(None));
};
let alg = match algorithm.length {
128 => "A128GCM",
192 => "A192GCM",
256 => "A256GCM",
_ => return Err(Error::Operation(Some(
"The length attribute of the [[algorithm]] internal slot of key is not \
128, 192 or 256".to_string(),
)))
};
jwk.alg = Some(DOMString::from(alg));
},
AesAlgorithm::AesKw => {
// Step 2.4.
// If the length attribute of key is 128:
// Set the alg attribute of jwk to the string "A128KW".
// If the length attribute of key is 192:
// Set the alg attribute of jwk to the string "A192KW".
// If the length attribute of key is 256:
// Set the alg attribute of jwk to the string "A256KW".
let KeyAlgorithmAndDerivatives::AesKeyAlgorithm(algorithm) = key.algorithm()
else {
return Err(Error::Operation(None));
};
let alg = match algorithm.length {
128 => "A128KW",
192 => "A192KW",
256 => "A256KW",
_ => return Err(Error::Operation(Some(
"The length attribute of the [[algorithm]] internal slot of key is not \
128, 192 or 256".to_string(),
)))
};
jwk.alg = Some(DOMString::from(alg));
},
AesAlgorithm::AesOcb => {
// Step 2.4.
// If the length attribute of key is 128:
// Set the alg attribute of jwk to the string "A128OCB".
// If the length attribute of key is 192:
// Set the alg attribute of jwk to the string "A192OCB".
// If the length attribute of key is 256:
// Set the alg attribute of jwk to the string "A256OCB".
let KeyAlgorithmAndDerivatives::AesKeyAlgorithm(algorithm) = key.algorithm()
else {
return Err(Error::Operation(None));
};
let alg = match algorithm.length {
128 => "A128OCB",
192 => "A192OCB",
256 => "A256OCB",
_ => return Err(Error::Operation(Some(
"The length attribute of the [[algorithm]] internal slot of key is not \
128, 192 or 256".to_string(),
)))
};
jwk.alg = Some(DOMString::from(alg));
},
}
// Step 2.5. Set the key_ops attribute of jwk to equal the usages attribute of key.
jwk.set_key_ops(key.usages());
// Step 2.6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of
// key.
jwk.ext = Some(key.Extractable());
// Step 2.7. Let result be jwk.
ExportedKey::Jwk(Box::new(jwk))
},
_ => {
// throw a NotSupportedError.
return Err(Error::NotSupported(Some(
"Unsupported import key format for AES key".to_string(),
)));
},
};
// Step 3. Return result.
Ok(result)
}
/// <https://w3c.github.io/webcrypto/#aes-ctr-operations-get-key-length>
/// <https://w3c.github.io/webcrypto/#aes-cbc-operations-get-key-length>
/// <https://w3c.github.io/webcrypto/#aes-gcm-operations-get-key-length>
/// <https://w3c.github.io/webcrypto/#aes-kw-operations-get-key-length>
/// <https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-get-key-length>
pub(crate) fn get_key_length(
normalized_derived_key_algorithm: &SubtleAesDerivedKeyParams,
) -> Result<Option<u32>, Error> {
// Step 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then
// throw an OperationError.
if !matches!(normalized_derived_key_algorithm.length, 128 | 192 | 256) {
return Err(Error::Operation(Some(
"The length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256".to_string(),
)));
}
// Step 2. Return the length member of normalizedDerivedKeyAlgorithm.
Ok(Some(normalized_derived_key_algorithm.length as u32))
}