summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/exported/web_crypto_algorithm.cc
blob: 1628116da5bdc2882b7cf9f5822839db7fe4130c (plain)
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
/*
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/public/platform/web_crypto_algorithm.h"

#include <memory>
#include <utility>

#include "base/memory/ptr_util.h"
#include "base/stl_util.h"
#include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"

namespace blink {

namespace {

// A mapping from the algorithm ID to information about the algorithm.
constexpr WebCryptoAlgorithmInfo kAlgorithmIdToInfo[] = {
    {// Index 0
     "AES-CBC",
     {
         kWebCryptoAlgorithmParamsTypeAesCbcParams,         // Encrypt
         kWebCryptoAlgorithmParamsTypeAesCbcParams,         // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,                // Sign
         WebCryptoAlgorithmInfo::kUndefined,                // Verify
         WebCryptoAlgorithmInfo::kUndefined,                // Digest
         kWebCryptoAlgorithmParamsTypeAesKeyGenParams,      // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,                 // ImportKey
         kWebCryptoAlgorithmParamsTypeAesDerivedKeyParams,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,                // DeriveBits
         kWebCryptoAlgorithmParamsTypeAesCbcParams,         // WrapKey
         kWebCryptoAlgorithmParamsTypeAesCbcParams          // UnwrapKey
     }},
    {// Index 1
     "HMAC",
     {
         WebCryptoAlgorithmInfo::kUndefined,             // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,             // Decrypt
         kWebCryptoAlgorithmParamsTypeNone,              // Sign
         kWebCryptoAlgorithmParamsTypeNone,              // Verify
         WebCryptoAlgorithmInfo::kUndefined,             // Digest
         kWebCryptoAlgorithmParamsTypeHmacKeyGenParams,  // GenerateKey
         kWebCryptoAlgorithmParamsTypeHmacImportParams,  // ImportKey
         kWebCryptoAlgorithmParamsTypeHmacImportParams,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,             // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,             // WrapKey
         WebCryptoAlgorithmInfo::kUndefined              // UnwrapKey
     }},
    {// Index 2
     "RSASSA-PKCS1-v1_5",
     {
         WebCryptoAlgorithmInfo::kUndefined,                  // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,                  // Decrypt
         kWebCryptoAlgorithmParamsTypeNone,                   // Sign
         kWebCryptoAlgorithmParamsTypeNone,                   // Verify
         WebCryptoAlgorithmInfo::kUndefined,                  // Digest
         kWebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams,  // GenerateKey
         kWebCryptoAlgorithmParamsTypeRsaHashedImportParams,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,                  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,                  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,                  // WrapKey
         WebCryptoAlgorithmInfo::kUndefined                   // UnwrapKey
     }},
    {// Index 3
     "SHA-1",
     {
         WebCryptoAlgorithmInfo::kUndefined,  // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Sign
         WebCryptoAlgorithmInfo::kUndefined,  // Verify
         kWebCryptoAlgorithmParamsTypeNone,   // Digest
         WebCryptoAlgorithmInfo::kUndefined,  // GenerateKey
         WebCryptoAlgorithmInfo::kUndefined,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,  // WrapKey
         WebCryptoAlgorithmInfo::kUndefined   // UnwrapKey
     }},
    {// Index 4
     "SHA-256",
     {
         WebCryptoAlgorithmInfo::kUndefined,  // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Sign
         WebCryptoAlgorithmInfo::kUndefined,  // Verify
         kWebCryptoAlgorithmParamsTypeNone,   // Digest
         WebCryptoAlgorithmInfo::kUndefined,  // GenerateKey
         WebCryptoAlgorithmInfo::kUndefined,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,  // WrapKey
         WebCryptoAlgorithmInfo::kUndefined   // UnwrapKey
     }},
    {// Index 5
     "SHA-384",
     {
         WebCryptoAlgorithmInfo::kUndefined,  // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Sign
         WebCryptoAlgorithmInfo::kUndefined,  // Verify
         kWebCryptoAlgorithmParamsTypeNone,   // Digest
         WebCryptoAlgorithmInfo::kUndefined,  // GenerateKey
         WebCryptoAlgorithmInfo::kUndefined,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,  // WrapKey
         WebCryptoAlgorithmInfo::kUndefined   // UnwrapKey
     }},
    {// Index 6
     "SHA-512",
     {
         WebCryptoAlgorithmInfo::kUndefined,  // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,  // Sign
         WebCryptoAlgorithmInfo::kUndefined,  // Verify
         kWebCryptoAlgorithmParamsTypeNone,   // Digest
         WebCryptoAlgorithmInfo::kUndefined,  // GenerateKey
         WebCryptoAlgorithmInfo::kUndefined,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,  // WrapKey
         WebCryptoAlgorithmInfo::kUndefined   // UnwrapKey
     }},
    {// Index 7
     "AES-GCM",
     {
         kWebCryptoAlgorithmParamsTypeAesGcmParams,         // Encrypt
         kWebCryptoAlgorithmParamsTypeAesGcmParams,         // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,                // Sign
         WebCryptoAlgorithmInfo::kUndefined,                // Verify
         WebCryptoAlgorithmInfo::kUndefined,                // Digest
         kWebCryptoAlgorithmParamsTypeAesKeyGenParams,      // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,                 // ImportKey
         kWebCryptoAlgorithmParamsTypeAesDerivedKeyParams,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,                // DeriveBits
         kWebCryptoAlgorithmParamsTypeAesGcmParams,         // WrapKey
         kWebCryptoAlgorithmParamsTypeAesGcmParams          // UnwrapKey
     }},
    {// Index 8
     "RSA-OAEP",
     {
         kWebCryptoAlgorithmParamsTypeRsaOaepParams,          // Encrypt
         kWebCryptoAlgorithmParamsTypeRsaOaepParams,          // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,                  // Sign
         WebCryptoAlgorithmInfo::kUndefined,                  // Verify
         WebCryptoAlgorithmInfo::kUndefined,                  // Digest
         kWebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams,  // GenerateKey
         kWebCryptoAlgorithmParamsTypeRsaHashedImportParams,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,                  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,                  // DeriveBits
         kWebCryptoAlgorithmParamsTypeRsaOaepParams,          // WrapKey
         kWebCryptoAlgorithmParamsTypeRsaOaepParams           // UnwrapKey
     }},
    {// Index 9
     "AES-CTR",
     {
         kWebCryptoAlgorithmParamsTypeAesCtrParams,         // Encrypt
         kWebCryptoAlgorithmParamsTypeAesCtrParams,         // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,                // Sign
         WebCryptoAlgorithmInfo::kUndefined,                // Verify
         WebCryptoAlgorithmInfo::kUndefined,                // Digest
         kWebCryptoAlgorithmParamsTypeAesKeyGenParams,      // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,                 // ImportKey
         kWebCryptoAlgorithmParamsTypeAesDerivedKeyParams,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,                // DeriveBits
         kWebCryptoAlgorithmParamsTypeAesCtrParams,         // WrapKey
         kWebCryptoAlgorithmParamsTypeAesCtrParams          // UnwrapKey
     }},
    {// Index 10
     "AES-KW",
     {
         WebCryptoAlgorithmInfo::kUndefined,                // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,                // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,                // Sign
         WebCryptoAlgorithmInfo::kUndefined,                // Verify
         WebCryptoAlgorithmInfo::kUndefined,                // Digest
         kWebCryptoAlgorithmParamsTypeAesKeyGenParams,      // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,                 // ImportKey
         kWebCryptoAlgorithmParamsTypeAesDerivedKeyParams,  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,                // DeriveBits
         kWebCryptoAlgorithmParamsTypeNone,                 // WrapKey
         kWebCryptoAlgorithmParamsTypeNone                  // UnwrapKey
     }},
    {// Index 11
     "RSA-PSS",
     {
         WebCryptoAlgorithmInfo::kUndefined,                  // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,                  // Decrypt
         kWebCryptoAlgorithmParamsTypeRsaPssParams,           // Sign
         kWebCryptoAlgorithmParamsTypeRsaPssParams,           // Verify
         WebCryptoAlgorithmInfo::kUndefined,                  // Digest
         kWebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams,  // GenerateKey
         kWebCryptoAlgorithmParamsTypeRsaHashedImportParams,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,                  // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,                  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,                  // WrapKey
         WebCryptoAlgorithmInfo::kUndefined                   // UnwrapKey
     }},
    {// Index 12
     "ECDSA",
     {
         WebCryptoAlgorithmInfo::kUndefined,              // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,              // Decrypt
         kWebCryptoAlgorithmParamsTypeEcdsaParams,        // Sign
         kWebCryptoAlgorithmParamsTypeEcdsaParams,        // Verify
         WebCryptoAlgorithmInfo::kUndefined,              // Digest
         kWebCryptoAlgorithmParamsTypeEcKeyGenParams,     // GenerateKey
         kWebCryptoAlgorithmParamsTypeEcKeyImportParams,  // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,              // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,              // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,              // WrapKey
         WebCryptoAlgorithmInfo::kUndefined               // UnwrapKey
     }},
    {// Index 13
     "ECDH",
     {
         WebCryptoAlgorithmInfo::kUndefined,                // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,                // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,                // Sign
         WebCryptoAlgorithmInfo::kUndefined,                // Verify
         WebCryptoAlgorithmInfo::kUndefined,                // Digest
         kWebCryptoAlgorithmParamsTypeEcKeyGenParams,       // GenerateKey
         kWebCryptoAlgorithmParamsTypeEcKeyImportParams,    // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,                // GetKeyLength
         kWebCryptoAlgorithmParamsTypeEcdhKeyDeriveParams,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,                // WrapKey
         WebCryptoAlgorithmInfo::kUndefined                 // UnwrapKey
     }},
    {// Index 14
     "HKDF",
     {
         WebCryptoAlgorithmInfo::kUndefined,       // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,       // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,       // Sign
         WebCryptoAlgorithmInfo::kUndefined,       // Verify
         WebCryptoAlgorithmInfo::kUndefined,       // Digest
         WebCryptoAlgorithmInfo::kUndefined,       // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,        // ImportKey
         kWebCryptoAlgorithmParamsTypeNone,        // GetKeyLength
         kWebCryptoAlgorithmParamsTypeHkdfParams,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,       // WrapKey
         WebCryptoAlgorithmInfo::kUndefined        // UnwrapKey
     }},
    {// Index 15
     "PBKDF2",
     {
         WebCryptoAlgorithmInfo::kUndefined,         // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,         // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,         // Sign
         WebCryptoAlgorithmInfo::kUndefined,         // Verify
         WebCryptoAlgorithmInfo::kUndefined,         // Digest
         WebCryptoAlgorithmInfo::kUndefined,         // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,          // ImportKey
         kWebCryptoAlgorithmParamsTypeNone,          // GetKeyLength
         kWebCryptoAlgorithmParamsTypePbkdf2Params,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,         // WrapKey
         WebCryptoAlgorithmInfo::kUndefined          // UnwrapKey
     }},
    {// Index 16
     // TODO(crbug.com/1032821): Ed25519 is experimental behind a flag. See
     // https://chromestatus.com/feature/4913922408710144 for the status.
     "ED25519",
     {
         WebCryptoAlgorithmInfo::kUndefined,          // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,          // Decrypt
         kWebCryptoAlgorithmParamsTypeEd25519Params,  // Sign
         kWebCryptoAlgorithmParamsTypeEd25519Params,  // Verify
         WebCryptoAlgorithmInfo::kUndefined,          // Digest
         kWebCryptoAlgorithmParamsTypeNone,           // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,           // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,          // GetKeyLength
         WebCryptoAlgorithmInfo::kUndefined,          // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,          // WrapKey
         WebCryptoAlgorithmInfo::kUndefined           // UnwrapKey
     }},
    {// Index 17
     // TODO(crbug.com/1032821): X25519 is experimental behind a flag. See
     // https://chromestatus.com/feature/4913922408710144 for the status.
     "X25519",
     {
         WebCryptoAlgorithmInfo::kUndefined,                  // Encrypt
         WebCryptoAlgorithmInfo::kUndefined,                  // Decrypt
         WebCryptoAlgorithmInfo::kUndefined,                  // Sign
         WebCryptoAlgorithmInfo::kUndefined,                  // Verify
         WebCryptoAlgorithmInfo::kUndefined,                  // Digest
         kWebCryptoAlgorithmParamsTypeNone,                   // GenerateKey
         kWebCryptoAlgorithmParamsTypeNone,                   // ImportKey
         WebCryptoAlgorithmInfo::kUndefined,                  // GetKeyLength
         kWebCryptoAlgorithmParamsTypeX25519KeyDeriveParams,  // DeriveBits
         WebCryptoAlgorithmInfo::kUndefined,                  // WrapKey
         WebCryptoAlgorithmInfo::kUndefined                   // UnwrapKey
     }},
};

// Initializing the algorithmIdToInfo table above depends on knowing the enum
// values for algorithm IDs. If those ever change, the table will need to be
// updated.
static_assert(kWebCryptoAlgorithmIdAesCbc == 0, "AES CBC id must match");
static_assert(kWebCryptoAlgorithmIdHmac == 1, "HMAC id must match");
static_assert(kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5 == 2,
              "RSASSA-PKCS1-v1_5 id must match");
static_assert(kWebCryptoAlgorithmIdSha1 == 3, "SHA1 id must match");
static_assert(kWebCryptoAlgorithmIdSha256 == 4, "SHA256 id must match");
static_assert(kWebCryptoAlgorithmIdSha384 == 5, "SHA384 id must match");
static_assert(kWebCryptoAlgorithmIdSha512 == 6, "SHA512 id must match");
static_assert(kWebCryptoAlgorithmIdAesGcm == 7, "AES GCM id must match");
static_assert(kWebCryptoAlgorithmIdRsaOaep == 8, "RSA OAEP id must match");
static_assert(kWebCryptoAlgorithmIdAesCtr == 9, "AES CTR id must match");
static_assert(kWebCryptoAlgorithmIdAesKw == 10, "AESKW id must match");
static_assert(kWebCryptoAlgorithmIdRsaPss == 11, "RSA-PSS id must match");
static_assert(kWebCryptoAlgorithmIdEcdsa == 12, "ECDSA id must match");
static_assert(kWebCryptoAlgorithmIdEcdh == 13, "ECDH id must match");
static_assert(kWebCryptoAlgorithmIdHkdf == 14, "HKDF id must match");
static_assert(kWebCryptoAlgorithmIdPbkdf2 == 15, "Pbkdf2 id must match");
static_assert(kWebCryptoAlgorithmIdEd25519 == 16, "X25519 id must match");
static_assert(kWebCryptoAlgorithmIdX25519 == 17, "Ed25519 id must match");
static_assert(kWebCryptoAlgorithmIdLast == 17, "last id must match");
static_assert(10 == kWebCryptoOperationLast,
              "the parameter mapping needs to be updated");

}  // namespace

class WebCryptoAlgorithmPrivate
    : public ThreadSafeRefCounted<WebCryptoAlgorithmPrivate> {
 public:
  WebCryptoAlgorithmPrivate(WebCryptoAlgorithmId id,
                            std::unique_ptr<WebCryptoAlgorithmParams> params)
      : id(id), params(std::move(params)) {}

  WebCryptoAlgorithmId id;
  std::unique_ptr<WebCryptoAlgorithmParams> params;
};

WebCryptoAlgorithm::WebCryptoAlgorithm(
    WebCryptoAlgorithmId id,
    std::unique_ptr<WebCryptoAlgorithmParams> params)
    : private_(base::AdoptRef(
          new WebCryptoAlgorithmPrivate(id, std::move(params)))) {}

WebCryptoAlgorithm WebCryptoAlgorithm::CreateNull() {
  return WebCryptoAlgorithm();
}

WebCryptoAlgorithm WebCryptoAlgorithm::AdoptParamsAndCreate(
    WebCryptoAlgorithmId id,
    WebCryptoAlgorithmParams* params) {
  return WebCryptoAlgorithm(id, base::WrapUnique(params));
}

const WebCryptoAlgorithmInfo* WebCryptoAlgorithm::LookupAlgorithmInfo(
    WebCryptoAlgorithmId id) {
  const unsigned id_int = id;
  if (id_int >= base::size(kAlgorithmIdToInfo))
    return nullptr;
  return &kAlgorithmIdToInfo[id];
}

bool WebCryptoAlgorithm::IsNull() const {
  return private_.IsNull();
}

WebCryptoAlgorithmId WebCryptoAlgorithm::Id() const {
  DCHECK(!IsNull());
  return private_->id;
}

WebCryptoAlgorithmParamsType WebCryptoAlgorithm::ParamsType() const {
  DCHECK(!IsNull());
  if (!private_->params)
    return kWebCryptoAlgorithmParamsTypeNone;
  return private_->params->GetType();
}

const WebCryptoAesCbcParams* WebCryptoAlgorithm::AesCbcParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeAesCbcParams)
    return static_cast<WebCryptoAesCbcParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoAesCtrParams* WebCryptoAlgorithm::AesCtrParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeAesCtrParams)
    return static_cast<WebCryptoAesCtrParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoAesKeyGenParams* WebCryptoAlgorithm::AesKeyGenParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeAesKeyGenParams)
    return static_cast<WebCryptoAesKeyGenParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoHmacImportParams* WebCryptoAlgorithm::HmacImportParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeHmacImportParams)
    return static_cast<WebCryptoHmacImportParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoHmacKeyGenParams* WebCryptoAlgorithm::HmacKeyGenParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeHmacKeyGenParams)
    return static_cast<WebCryptoHmacKeyGenParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoAesGcmParams* WebCryptoAlgorithm::AesGcmParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeAesGcmParams)
    return static_cast<WebCryptoAesGcmParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoRsaOaepParams* WebCryptoAlgorithm::RsaOaepParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeRsaOaepParams)
    return static_cast<WebCryptoRsaOaepParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoRsaHashedImportParams*
WebCryptoAlgorithm::RsaHashedImportParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeRsaHashedImportParams)
    return static_cast<WebCryptoRsaHashedImportParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoRsaHashedKeyGenParams*
WebCryptoAlgorithm::RsaHashedKeyGenParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams)
    return static_cast<WebCryptoRsaHashedKeyGenParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoRsaPssParams* WebCryptoAlgorithm::RsaPssParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeRsaPssParams)
    return static_cast<WebCryptoRsaPssParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoEcdsaParams* WebCryptoAlgorithm::EcdsaParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeEcdsaParams)
    return static_cast<WebCryptoEcdsaParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoEcKeyGenParams* WebCryptoAlgorithm::EcKeyGenParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeEcKeyGenParams)
    return static_cast<WebCryptoEcKeyGenParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoEcKeyImportParams* WebCryptoAlgorithm::EcKeyImportParams()
    const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeEcKeyImportParams)
    return static_cast<WebCryptoEcKeyImportParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoEcdhKeyDeriveParams* WebCryptoAlgorithm::EcdhKeyDeriveParams()
    const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeEcdhKeyDeriveParams)
    return static_cast<WebCryptoEcdhKeyDeriveParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoAesDerivedKeyParams* WebCryptoAlgorithm::AesDerivedKeyParams()
    const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeAesDerivedKeyParams)
    return static_cast<WebCryptoAesDerivedKeyParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoHkdfParams* WebCryptoAlgorithm::HkdfParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeHkdfParams)
    return static_cast<WebCryptoHkdfParams*>(private_->params.get());
  return nullptr;
}

const WebCryptoPbkdf2Params* WebCryptoAlgorithm::Pbkdf2Params() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypePbkdf2Params)
    return static_cast<WebCryptoPbkdf2Params*>(private_->params.get());
  return nullptr;
}

const WebCryptoEd25519Params* WebCryptoAlgorithm::Ed25519Params() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeEd25519Params)
    return static_cast<WebCryptoEd25519Params*>(private_->params.get());
  return nullptr;
}

const WebCryptoX25519KeyDeriveParams*
WebCryptoAlgorithm::X25519KeyDeriveParams() const {
  DCHECK(!IsNull());
  if (ParamsType() == kWebCryptoAlgorithmParamsTypeX25519KeyDeriveParams)
    return static_cast<WebCryptoX25519KeyDeriveParams*>(private_->params.get());
  return nullptr;
}

bool WebCryptoAlgorithm::IsHash(WebCryptoAlgorithmId id) {
  switch (id) {
    case kWebCryptoAlgorithmIdSha1:
    case kWebCryptoAlgorithmIdSha256:
    case kWebCryptoAlgorithmIdSha384:
    case kWebCryptoAlgorithmIdSha512:
      return true;
    case kWebCryptoAlgorithmIdAesCbc:
    case kWebCryptoAlgorithmIdHmac:
    case kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
    case kWebCryptoAlgorithmIdAesGcm:
    case kWebCryptoAlgorithmIdRsaOaep:
    case kWebCryptoAlgorithmIdAesCtr:
    case kWebCryptoAlgorithmIdAesKw:
    case kWebCryptoAlgorithmIdRsaPss:
    case kWebCryptoAlgorithmIdEcdsa:
    case kWebCryptoAlgorithmIdEcdh:
    case kWebCryptoAlgorithmIdHkdf:
    case kWebCryptoAlgorithmIdPbkdf2:
    case kWebCryptoAlgorithmIdEd25519:
    case kWebCryptoAlgorithmIdX25519:
      break;
  }
  return false;
}

bool WebCryptoAlgorithm::IsKdf(WebCryptoAlgorithmId id) {
  switch (id) {
    case kWebCryptoAlgorithmIdHkdf:
    case kWebCryptoAlgorithmIdPbkdf2:
      return true;
    case kWebCryptoAlgorithmIdSha1:
    case kWebCryptoAlgorithmIdSha256:
    case kWebCryptoAlgorithmIdSha384:
    case kWebCryptoAlgorithmIdSha512:
    case kWebCryptoAlgorithmIdAesCbc:
    case kWebCryptoAlgorithmIdHmac:
    case kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
    case kWebCryptoAlgorithmIdAesGcm:
    case kWebCryptoAlgorithmIdRsaOaep:
    case kWebCryptoAlgorithmIdAesCtr:
    case kWebCryptoAlgorithmIdAesKw:
    case kWebCryptoAlgorithmIdRsaPss:
    case kWebCryptoAlgorithmIdEcdsa:
    case kWebCryptoAlgorithmIdEcdh:
    case kWebCryptoAlgorithmIdEd25519:
    case kWebCryptoAlgorithmIdX25519:
      break;
  }
  return false;
}

void WebCryptoAlgorithm::Assign(const WebCryptoAlgorithm& other) {
  private_ = other.private_;
}

void WebCryptoAlgorithm::Reset() {
  private_.Reset();
}

}  // namespace blink