OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ | 5 #ifndef COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
6 #define COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ | 6 #define COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
13 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 13 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
14 | 14 |
| 15 // TODO(eroman): The remaining functions in this file need to be sorted |
| 16 // out. They should be moved to a more domain specific location (for |
| 17 // instance test helpers, asymmetric algorithm helpers, etc.). |
| 18 |
15 namespace webcrypto { | 19 namespace webcrypto { |
16 | 20 |
17 class Status; | 21 class Status; |
18 | 22 |
19 // Creates a WebCryptoAlgorithm without any parameters. | 23 // Creates a WebCryptoAlgorithm without any parameters. |
20 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id); | 24 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id); |
21 | 25 |
22 // Creates an HMAC import algorithm whose inner hash algorithm is determined by | |
23 // the specified algorithm ID. It is an error to call this method with a hash | |
24 // algorithm that is not SHA*. | |
25 blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( | |
26 blink::WebCryptoAlgorithmId hash_id, | |
27 unsigned int length_bits); | |
28 | |
29 // Same as above but without specifying a length. | |
30 blink::WebCryptoAlgorithm CreateHmacImportAlgorithmNoLength( | |
31 blink::WebCryptoAlgorithmId hash_id); | |
32 | |
33 // Creates an import algorithm for RSA algorithms that take a hash. | 26 // Creates an import algorithm for RSA algorithms that take a hash. |
34 // It is an error to call this with a hash_id that is not a SHA*. | 27 // It is an error to call this with a hash_id that is not a SHA*. |
35 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( | 28 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( |
36 blink::WebCryptoAlgorithmId id, | 29 blink::WebCryptoAlgorithmId id, |
37 blink::WebCryptoAlgorithmId hash_id); | 30 blink::WebCryptoAlgorithmId hash_id); |
38 | 31 |
39 // Creates an import algorithm for EC keys. | 32 // Creates an import algorithm for EC keys. |
40 blink::WebCryptoAlgorithm CreateEcImportAlgorithm( | 33 blink::WebCryptoAlgorithm CreateEcImportAlgorithm( |
41 blink::WebCryptoAlgorithmId id, | 34 blink::WebCryptoAlgorithmId id, |
42 blink::WebCryptoNamedCurve named_curve); | 35 blink::WebCryptoNamedCurve named_curve); |
43 | 36 |
44 // Returns true if the set bits in b make up a subset of the set bits in a. | 37 // Returns true if the set bits in b make up a subset of the set bits in a. |
45 bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a, | 38 bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a, |
46 blink::WebCryptoKeyUsageMask b); | 39 blink::WebCryptoKeyUsageMask b); |
47 | 40 |
48 Status GetAesGcmTagLengthInBits(const blink::WebCryptoAesGcmParams* params, | |
49 unsigned int* tag_length_bits); | |
50 | |
51 Status GetAesKeyGenLengthInBits(const blink::WebCryptoAesKeyGenParams* params, | |
52 unsigned int* keylen_bits); | |
53 | |
54 Status GetHmacKeyGenLengthInBits(const blink::WebCryptoHmacKeyGenParams* params, | |
55 unsigned int* keylen_bits); | |
56 | |
57 // Gets the requested key length in bits for an HMAC import operation. | |
58 Status GetHmacImportKeyLengthBits( | |
59 const blink::WebCryptoHmacImportParams* params, | |
60 unsigned int key_data_byte_length, | |
61 unsigned int* keylen_bits); | |
62 | |
63 Status VerifyAesKeyLengthForImport(unsigned int keylen_bytes); | |
64 | |
65 Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages, | 41 Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages, |
66 blink::WebCryptoKeyUsageMask actual_usages, | 42 blink::WebCryptoKeyUsageMask actual_usages, |
67 bool allow_empty_usages); | 43 bool allow_empty_usages); |
68 | 44 |
69 // Extracts the public exponent and modulus length from the Blink parameters. | 45 // Extracts the public exponent and modulus length from the Blink parameters. |
70 // On success it is guaranteed that: | 46 // On success it is guaranteed that: |
71 // * public_exponent is either 3 or 65537 | 47 // * public_exponent is either 3 or 65537 |
72 // * modulus_length_bits is a multiple of 8 | 48 // * modulus_length_bits is a multiple of 8 |
73 // * modulus_length is >= 256 | 49 // * modulus_length is >= 256 |
74 // * modulus_length is <= 16K | 50 // * modulus_length is <= 16K |
(...skipping 23 matching lines...) Expand all Loading... |
98 | 74 |
99 // Rounds a bit count (up) to the nearest byte count. | 75 // Rounds a bit count (up) to the nearest byte count. |
100 // | 76 // |
101 // This is mathematically equivalent to (x + 7) / 8, however has no | 77 // This is mathematically equivalent to (x + 7) / 8, however has no |
102 // possibility of integer overflow. | 78 // possibility of integer overflow. |
103 template <typename T> | 79 template <typename T> |
104 T NumBitsToBytes(T x) { | 80 T NumBitsToBytes(T x) { |
105 return (x / 8) + (7 + (x % 8)) / 8; | 81 return (x / 8) + (7 + (x % 8)) / 8; |
106 } | 82 } |
107 | 83 |
108 // The "get key length" operation for AES keys. | |
109 Status GetAesKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm, | |
110 bool* has_length_bits, | |
111 unsigned int* length_bits); | |
112 | |
113 // The "get key length" operation for HMAC keys. | |
114 Status GetHmacKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm, | |
115 bool* has_length_bits, | |
116 unsigned int* length_bits); | |
117 | 84 |
118 // Splits the combined usages given to GenerateKey() into the respective usages | 85 // Splits the combined usages given to GenerateKey() into the respective usages |
119 // for the public key and private key. Returns an error if the usages are | 86 // for the public key and private key. Returns an error if the usages are |
120 // invalid. | 87 // invalid. |
121 Status GetUsagesForGenerateAsymmetricKey( | 88 Status GetUsagesForGenerateAsymmetricKey( |
122 blink::WebCryptoKeyUsageMask combined_usages, | 89 blink::WebCryptoKeyUsageMask combined_usages, |
123 blink::WebCryptoKeyUsageMask all_public_usages, | 90 blink::WebCryptoKeyUsageMask all_public_usages, |
124 blink::WebCryptoKeyUsageMask all_private_usages, | 91 blink::WebCryptoKeyUsageMask all_private_usages, |
125 blink::WebCryptoKeyUsageMask* public_usages, | 92 blink::WebCryptoKeyUsageMask* public_usages, |
126 blink::WebCryptoKeyUsageMask* private_usages); | 93 blink::WebCryptoKeyUsageMask* private_usages); |
127 | 94 |
128 } // namespace webcrypto | 95 } // namespace webcrypto |
129 | 96 |
130 #endif // COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ | 97 #endif // COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
OLD | NEW |