@@ -32,7 +32,9 @@ const EVP_MD *createDigestAlgorithm(JSContext *cx, JS::HandleObject key) {
32
32
}
33
33
34
34
std::string_view name (name_chars.get (), name_length);
35
- if (name == " SHA-1" ) {
35
+ if (name == " MD5" ) {
36
+ return EVP_md5 ();
37
+ } else if (name == " SHA-1" ) {
36
38
return EVP_sha1 ();
37
39
} else if (name == " SHA-224" ) {
38
40
return EVP_sha224 ();
@@ -282,62 +284,6 @@ std::unique_ptr<CryptoKeyRSAComponents> createRSAPrivateKeyFromJWK(JSContext *cx
282
284
return privateKeyComponents;
283
285
}
284
286
285
- const char *algorithmName (CryptoAlgorithmIdentifier algorithm) {
286
- switch (algorithm) {
287
- case CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5: {
288
- return " RSASSA-PKCS1-v1_5" ;
289
- }
290
- case CryptoAlgorithmIdentifier::RSA_PSS: {
291
- return " RSA-PSS" ;
292
- }
293
- case CryptoAlgorithmIdentifier::RSA_OAEP: {
294
- return " RSA-OAEP" ;
295
- }
296
- case CryptoAlgorithmIdentifier::ECDSA: {
297
- return " ECDSA" ;
298
- }
299
- case CryptoAlgorithmIdentifier::ECDH: {
300
- return " ECDH" ;
301
- }
302
- case CryptoAlgorithmIdentifier::AES_CTR: {
303
- return " AES-CTR" ;
304
- }
305
- case CryptoAlgorithmIdentifier::AES_CBC: {
306
- return " AES-CBC" ;
307
- }
308
- case CryptoAlgorithmIdentifier::AES_GCM: {
309
- return " AES-GCM" ;
310
- }
311
- case CryptoAlgorithmIdentifier::AES_KW: {
312
- return " AES-KW" ;
313
- }
314
- case CryptoAlgorithmIdentifier::HMAC: {
315
- return " HMAC" ;
316
- }
317
- case CryptoAlgorithmIdentifier::SHA_1: {
318
- return " SHA-1" ;
319
- }
320
- case CryptoAlgorithmIdentifier::SHA_256: {
321
- return " SHA-256" ;
322
- }
323
- case CryptoAlgorithmIdentifier::SHA_384: {
324
- return " SHA-384" ;
325
- }
326
- case CryptoAlgorithmIdentifier::SHA_512: {
327
- return " SHA-512" ;
328
- }
329
- case CryptoAlgorithmIdentifier::HKDF: {
330
- return " HKDF" ;
331
- }
332
- case CryptoAlgorithmIdentifier::PBKDF2: {
333
- return " PBKDF2" ;
334
- }
335
- default : {
336
- MOZ_ASSERT_UNREACHABLE (" Unknown `CryptoAlgorithmIdentifier` value" );
337
- }
338
- }
339
- }
340
-
341
287
// Web Crypto API uses DOMExceptions to indicate errors
342
288
// We are adding the fields which are tested for in Web Platform Tests
343
289
// TODO: Implement DOMExceptions class and use that instead of duck-typing on an Error instance
@@ -445,6 +391,8 @@ JS::Result<CryptoAlgorithmIdentifier> normalizeIdentifier(JSContext *cx, JS::Han
445
391
return CryptoAlgorithmIdentifier::AES_KW;
446
392
} else if (algorithm == " HMAC" ) {
447
393
return CryptoAlgorithmIdentifier::HMAC;
394
+ } else if (algorithm == " MD5" ) {
395
+ return CryptoAlgorithmIdentifier::MD5;
448
396
} else if (algorithm == " SHA-1" ) {
449
397
return CryptoAlgorithmIdentifier::SHA_1;
450
398
} else if (algorithm == " SHA-256" ) {
@@ -465,6 +413,65 @@ JS::Result<CryptoAlgorithmIdentifier> normalizeIdentifier(JSContext *cx, JS::Han
465
413
}
466
414
} // namespace
467
415
416
+ const char *algorithmName (CryptoAlgorithmIdentifier algorithm) {
417
+ switch (algorithm) {
418
+ case CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5: {
419
+ return " RSASSA-PKCS1-v1_5" ;
420
+ }
421
+ case CryptoAlgorithmIdentifier::RSA_PSS: {
422
+ return " RSA-PSS" ;
423
+ }
424
+ case CryptoAlgorithmIdentifier::RSA_OAEP: {
425
+ return " RSA-OAEP" ;
426
+ }
427
+ case CryptoAlgorithmIdentifier::ECDSA: {
428
+ return " ECDSA" ;
429
+ }
430
+ case CryptoAlgorithmIdentifier::ECDH: {
431
+ return " ECDH" ;
432
+ }
433
+ case CryptoAlgorithmIdentifier::AES_CTR: {
434
+ return " AES-CTR" ;
435
+ }
436
+ case CryptoAlgorithmIdentifier::AES_CBC: {
437
+ return " AES-CBC" ;
438
+ }
439
+ case CryptoAlgorithmIdentifier::AES_GCM: {
440
+ return " AES-GCM" ;
441
+ }
442
+ case CryptoAlgorithmIdentifier::AES_KW: {
443
+ return " AES-KW" ;
444
+ }
445
+ case CryptoAlgorithmIdentifier::HMAC: {
446
+ return " HMAC" ;
447
+ }
448
+ case CryptoAlgorithmIdentifier::MD5: {
449
+ return " MD5" ;
450
+ }
451
+ case CryptoAlgorithmIdentifier::SHA_1: {
452
+ return " SHA-1" ;
453
+ }
454
+ case CryptoAlgorithmIdentifier::SHA_256: {
455
+ return " SHA-256" ;
456
+ }
457
+ case CryptoAlgorithmIdentifier::SHA_384: {
458
+ return " SHA-384" ;
459
+ }
460
+ case CryptoAlgorithmIdentifier::SHA_512: {
461
+ return " SHA-512" ;
462
+ }
463
+ case CryptoAlgorithmIdentifier::HKDF: {
464
+ return " HKDF" ;
465
+ }
466
+ case CryptoAlgorithmIdentifier::PBKDF2: {
467
+ return " PBKDF2" ;
468
+ }
469
+ default : {
470
+ MOZ_ASSERT_UNREACHABLE (" Unknown `CryptoAlgorithmIdentifier` value" );
471
+ }
472
+ }
473
+ }
474
+
468
475
// clang-format off
469
476
// / This table is from https://w3c.github.io/webcrypto/#h-note-15
470
477
// | Algorithm | encrypt | decrypt | sign | verify | digest | generateKey | deriveKey | deriveBits | importKey | exportKey | wrapKey | unwrapKey |
@@ -499,9 +506,13 @@ std::unique_ptr<CryptoAlgorithmDigest> CryptoAlgorithmDigest::normalize(JSContex
499
506
500
507
// The table listed at https://w3c.github.io/webcrypto/#h-note-15 is what defines which algorithms support which operations
501
508
// SHA-1, SHA-256, SHA-384, and SHA-512 are the only algorithms which support the digest operation
509
+ // We also support MD5 as an extra implementor defined algorithm
502
510
503
511
// Note: The specification states that none of the SHA algorithms take any parameters -- https://w3c.github.io/webcrypto/#sha-registration
504
512
switch (identifier) {
513
+ case CryptoAlgorithmIdentifier::MD5: {
514
+ return std::make_unique<CryptoAlgorithmMD5>();
515
+ }
505
516
case CryptoAlgorithmIdentifier::SHA_1: {
506
517
return std::make_unique<CryptoAlgorithmSHA1>();
507
518
}
@@ -991,7 +1002,7 @@ JSObject *CryptoAlgorithmRSASSA_PKCS1_v1_5_Import::toObject(JSContext *cx) {
991
1002
// Set the hash attribute of algorithm to the hash member of normalizedAlgorithm.
992
1003
JS::RootedObject hash (cx, JS_NewObject (cx, nullptr ));
993
1004
994
- auto hash_name = JS_NewStringCopyZ (cx, algorithmName (this ->hashIdentifier ));
1005
+ auto hash_name = JS_NewStringCopyZ (cx, builtins:: algorithmName (this ->hashIdentifier ));
995
1006
if (!hash_name) {
996
1007
return nullptr ;
997
1008
}
@@ -1006,6 +1017,9 @@ JSObject *CryptoAlgorithmRSASSA_PKCS1_v1_5_Import::toObject(JSContext *cx) {
1006
1017
return algorithm;
1007
1018
}
1008
1019
1020
+ JSObject *CryptoAlgorithmMD5::digest (JSContext *cx, std::span<uint8_t > data) {
1021
+ return ::builtins::digest (cx, data, EVP_md5 (), MD5_DIGEST_LENGTH);
1022
+ }
1009
1023
JSObject *CryptoAlgorithmSHA1::digest (JSContext *cx, std::span<uint8_t > data) {
1010
1024
return ::builtins::digest (cx, data, EVP_sha1 (), SHA_DIGEST_LENGTH);
1011
1025
}
0 commit comments