jww@chromium.org | cf5d32e5 | 2014-03-07 18:00:08 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 5 | #ifndef COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
| 6 | #define COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 7 | |
eroman@chromium.org | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 8 | #include <stdint.h> |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 9 | #include <string> |
eroman@chromium.org | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 10 | |
padolph@netflix.com | 5d6e9f6 | 2014-03-14 21:51:45 | [diff] [blame] | 11 | #include "base/values.h" |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 12 | #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
eroman@chromium.org | 04166f8 | 2014-02-19 06:11:04 | [diff] [blame] | 13 | #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 14 | |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 15 | namespace webcrypto { |
| 16 | |
eroman@chromium.org | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 17 | class Status; |
eroman@chromium.org | cca89748 | 2014-01-30 22:40:19 | [diff] [blame] | 18 | |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 19 | // Creates a WebCryptoAlgorithm without any parameters. |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 20 | blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id); |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 21 | |
eroman@chromium.org | 8238bb1c | 2014-02-26 15:16:25 | [diff] [blame] | 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 |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 24 | // algorithm that is not SHA*. |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 25 | blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( |
eroman | 5d519974 | 2014-12-10 22:18:07 | [diff] [blame] | 26 | blink::WebCryptoAlgorithmId hash_id, |
| 27 | unsigned int length_bits); |
| 28 | |
| 29 | // Same as above but without specifying a length. |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 30 | blink::WebCryptoAlgorithm CreateHmacImportAlgorithmNoLength( |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 31 | blink::WebCryptoAlgorithmId hash_id); |
| 32 | |
eroman@chromium.org | 5daca047 | 2014-03-18 20:27:08 | [diff] [blame] | 33 | // 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*. |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 35 | blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( |
eroman@chromium.org | 5daca047 | 2014-03-18 20:27:08 | [diff] [blame] | 36 | blink::WebCryptoAlgorithmId id, |
| 37 | blink::WebCryptoAlgorithmId hash_id); |
| 38 | |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 39 | // Creates an import algorithm for EC keys. |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 40 | blink::WebCryptoAlgorithm CreateEcImportAlgorithm( |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 41 | blink::WebCryptoAlgorithmId id, |
| 42 | blink::WebCryptoNamedCurve named_curve); |
| 43 | |
eroman@chromium.org | 9c2e9cf | 2014-05-23 23:13:47 | [diff] [blame] | 44 | // Returns true if the set bits in b make up a subset of the set bits in a. |
| 45 | bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a, |
| 46 | blink::WebCryptoKeyUsageMask b); |
| 47 | |
eroman@chromium.org | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 48 | bool KeyUsageAllows(const blink::WebCryptoKey& key, |
| 49 | const blink::WebCryptoKeyUsage usage); |
| 50 | |
eroman@chromium.org | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 51 | Status GetAesGcmTagLengthInBits(const blink::WebCryptoAesGcmParams* params, |
| 52 | unsigned int* tag_length_bits); |
| 53 | |
| 54 | Status GetAesKeyGenLengthInBits(const blink::WebCryptoAesKeyGenParams* params, |
| 55 | unsigned int* keylen_bits); |
| 56 | |
| 57 | Status GetHmacKeyGenLengthInBits(const blink::WebCryptoHmacKeyGenParams* params, |
| 58 | unsigned int* keylen_bits); |
| 59 | |
eroman | 5d519974 | 2014-12-10 22:18:07 | [diff] [blame] | 60 | // Gets the requested key length in bits for an HMAC import operation. |
| 61 | Status GetHmacImportKeyLengthBits( |
| 62 | const blink::WebCryptoHmacImportParams* params, |
| 63 | unsigned int key_data_byte_length, |
| 64 | unsigned int* keylen_bits); |
| 65 | |
eroman@chromium.org | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 66 | Status VerifyAesKeyLengthForImport(unsigned int keylen_bytes); |
| 67 | |
| 68 | Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages, |
habib.virji | f4aa92ee7 | 2014-12-19 10:04:34 | [diff] [blame] | 69 | blink::WebCryptoKeyUsageMask actual_usages, |
| 70 | bool allow_empty_usages); |
eroman@chromium.org | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 71 | |
eroman@chromium.org | e9aa71a | 2014-07-25 08:38:02 | [diff] [blame] | 72 | // Extracts the public exponent and modulus length from the Blink parameters. |
| 73 | // On success it is guaranteed that: |
| 74 | // * public_exponent is either 3 or 65537 |
| 75 | // * modulus_length_bits is a multiple of 8 |
| 76 | // * modulus_length is >= 256 |
| 77 | // * modulus_length is <= 16K |
| 78 | Status GetRsaKeyGenParameters( |
| 79 | const blink::WebCryptoRsaHashedKeyGenParams* params, |
| 80 | unsigned int* public_exponent, |
| 81 | unsigned int* modulus_length_bits); |
| 82 | |
eroman | df493e2 | 2014-12-09 23:21:33 | [diff] [blame] | 83 | // Verifies that |usages| is valid when importing a key of the given format. |
| 84 | Status VerifyUsagesBeforeImportAsymmetricKey( |
| 85 | blink::WebCryptoKeyFormat format, |
| 86 | blink::WebCryptoKeyUsageMask all_public_key_usages, |
| 87 | blink::WebCryptoKeyUsageMask all_private_key_usages, |
| 88 | blink::WebCryptoKeyUsageMask usages); |
| 89 | |
eroman | 401e4afc | 2014-12-10 17:41:04 | [diff] [blame] | 90 | // Truncates an octet string to a particular bit length. This is accomplished by |
| 91 | // resizing to the closest byte length, and then zero-ing the unused |
| 92 | // least-significant bits of the final byte. |
| 93 | // |
| 94 | // It is an error to call this function with a bit length that is larger than |
| 95 | // that of |bytes|. |
| 96 | // |
| 97 | // TODO(eroman): This operation is not yet defined by the WebCrypto spec, |
| 98 | // however this is a reasonable interpretation: |
| 99 | // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27402 |
| 100 | void TruncateToBitLength(size_t length_bits, std::vector<uint8_t>* bytes); |
| 101 | |
| 102 | // Rounds a bit count (up) to the nearest byte count. |
| 103 | // |
| 104 | // This is mathematically equivalent to (x + 7) / 8, however has no |
| 105 | // possibility of integer overflow. |
| 106 | template <typename T> |
| 107 | T NumBitsToBytes(T x) { |
| 108 | return (x / 8) + (7 + (x % 8)) / 8; |
| 109 | } |
| 110 | |
eroman | f93fd5b | 2014-12-11 00:21:06 | [diff] [blame] | 111 | // The "get key length" operation for AES keys. |
| 112 | Status GetAesKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 113 | bool* has_length_bits, |
| 114 | unsigned int* length_bits); |
| 115 | |
| 116 | // The "get key length" operation for HMAC keys. |
| 117 | Status GetHmacKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 118 | bool* has_length_bits, |
| 119 | unsigned int* length_bits); |
| 120 | |
eroman | ae20f98 | 2014-12-11 04:55:47 | [diff] [blame] | 121 | // Splits the combined usages given to GenerateKey() into the respective usages |
| 122 | // for the public key and private key. Returns an error if the usages are |
| 123 | // invalid. |
| 124 | Status GetUsagesForGenerateAsymmetricKey( |
| 125 | blink::WebCryptoKeyUsageMask combined_usages, |
| 126 | blink::WebCryptoKeyUsageMask all_public_usages, |
| 127 | blink::WebCryptoKeyUsageMask all_private_usages, |
| 128 | blink::WebCryptoKeyUsageMask* public_usages, |
| 129 | blink::WebCryptoKeyUsageMask* private_usages); |
| 130 | |
padolph@netflix.com | b28852b | 2013-12-04 04:57:11 | [diff] [blame] | 131 | } // namespace webcrypto |
| 132 | |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 133 | #endif // COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |