• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
6 #define CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
7 
8 #include <stdint.h>
9 #include <string>
10 
11 #include "base/values.h"
12 #include "content/common/content_export.h"
13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
15 
16 namespace content {
17 
18 namespace webcrypto {
19 
20 class Status;
21 
22 // Composes a Web Crypto usage mask from an array of JWK key_ops values.
23 CONTENT_EXPORT Status GetWebCryptoUsagesFromJwkKeyOps(
24     const base::ListValue* jwk_key_ops_value,
25     blink::WebCryptoKeyUsageMask* jwk_key_ops_mask);
26 
27 // Composes a JWK key_ops array from a Web Crypto usage mask.
28 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages(
29     blink::WebCryptoKeyUsageMask usage_mask);
30 
31 // Creates a WebCryptoAlgorithm without any parameters.
32 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateAlgorithm(
33     blink::WebCryptoAlgorithmId id);
34 
35 // Creates an HMAC import algorithm whose inner hash algorithm is determined by
36 // the specified algorithm ID. It is an error to call this method with a hash
37 // algorithm that is not SHA*.
38 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateHmacImportAlgorithm(
39     blink::WebCryptoAlgorithmId hash_id);
40 
41 // Creates an import algorithm for RSA algorithms that take a hash.
42 // It is an error to call this with a hash_id that is not a SHA*.
43 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm(
44     blink::WebCryptoAlgorithmId id,
45     blink::WebCryptoAlgorithmId hash_id);
46 
47 // Returns true if the set bits in b make up a subset of the set bits in a.
48 bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a,
49                        blink::WebCryptoKeyUsageMask b);
50 
51 bool KeyUsageAllows(const blink::WebCryptoKey& key,
52                     const blink::WebCryptoKeyUsage usage);
53 
54 bool IsAlgorithmRsa(blink::WebCryptoAlgorithmId alg_id);
55 bool IsAlgorithmAsymmetric(blink::WebCryptoAlgorithmId alg_id);
56 
57 Status GetAesGcmTagLengthInBits(const blink::WebCryptoAesGcmParams* params,
58                                 unsigned int* tag_length_bits);
59 
60 Status GetAesKeyGenLengthInBits(const blink::WebCryptoAesKeyGenParams* params,
61                                 unsigned int* keylen_bits);
62 
63 Status GetHmacKeyGenLengthInBits(const blink::WebCryptoHmacKeyGenParams* params,
64                                  unsigned int* keylen_bits);
65 
66 Status VerifyAesKeyLengthForImport(unsigned int keylen_bytes);
67 
68 Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages,
69                               blink::WebCryptoKeyUsageMask actual_usages);
70 
71 // Extracts the public exponent and modulus length from the Blink parameters.
72 // On success it is guaranteed that:
73 //   * public_exponent is either 3 or 65537
74 //   * modulus_length_bits is a multiple of 8
75 //   * modulus_length is >= 256
76 //   * modulus_length is <= 16K
77 Status GetRsaKeyGenParameters(
78     const blink::WebCryptoRsaHashedKeyGenParams* params,
79     unsigned int* public_exponent,
80     unsigned int* modulus_length_bits);
81 
82 }  // namespace webcrypto
83 
84 }  // namespace content
85 
86 #endif  // CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
87