blob: ab711ab7a2c53d9ddfa2ac725c568922d8fa59ed [file] [log] [blame]
jww@chromium.orgcf5d32e52014-03-07 18:00:081// Copyright 2014 The Chromium Authors. All rights reserved.
padolph@netflix.comb28852b2013-12-04 04:57:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
erg56f12322015-04-17 00:51:485#ifndef COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_
6#define COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_
padolph@netflix.comb28852b2013-12-04 04:57:117
eroman@chromium.org53b6c9d22014-07-19 05:08:388#include <stdint.h>
padolph@netflix.comb28852b2013-12-04 04:57:119#include <string>
eroman@chromium.org53b6c9d22014-07-19 05:08:3810
padolph@netflix.com5d6e9f62014-03-14 21:51:4511#include "base/values.h"
padolph@netflix.comb28852b2013-12-04 04:57:1112#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
eroman@chromium.org04166f82014-02-19 06:11:0413#include "third_party/WebKit/public/platform/WebCryptoKey.h"
padolph@netflix.comb28852b2013-12-04 04:57:1114
padolph@netflix.comb28852b2013-12-04 04:57:1115namespace webcrypto {
16
eroman@chromium.org37be4cfa2014-03-20 05:39:3717class Status;
eroman@chromium.orgcca897482014-01-30 22:40:1918
padolph@netflix.comb28852b2013-12-04 04:57:1119// Creates a WebCryptoAlgorithm without any parameters.
erg56f12322015-04-17 00:51:4820blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id);
padolph@netflix.comb28852b2013-12-04 04:57:1121
eroman@chromium.org8238bb1c2014-02-26 15:16:2522// 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.comb28852b2013-12-04 04:57:1124// algorithm that is not SHA*.
erg56f12322015-04-17 00:51:4825blink::WebCryptoAlgorithm CreateHmacImportAlgorithm(
eroman5d5199742014-12-10 22:18:0726 blink::WebCryptoAlgorithmId hash_id,
27 unsigned int length_bits);
28
29// Same as above but without specifying a length.
erg56f12322015-04-17 00:51:4830blink::WebCryptoAlgorithm CreateHmacImportAlgorithmNoLength(
padolph@netflix.comb28852b2013-12-04 04:57:1131 blink::WebCryptoAlgorithmId hash_id);
32
eroman@chromium.org5daca0472014-03-18 20:27:0833// 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*.
erg56f12322015-04-17 00:51:4835blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm(
eroman@chromium.org5daca0472014-03-18 20:27:0836 blink::WebCryptoAlgorithmId id,
37 blink::WebCryptoAlgorithmId hash_id);
38
eromanb2ead6d2014-11-14 02:26:1439// Creates an import algorithm for EC keys.
erg56f12322015-04-17 00:51:4840blink::WebCryptoAlgorithm CreateEcImportAlgorithm(
eromanb2ead6d2014-11-14 02:26:1441 blink::WebCryptoAlgorithmId id,
42 blink::WebCryptoNamedCurve named_curve);
43
eroman@chromium.org9c2e9cf2014-05-23 23:13:4744// Returns true if the set bits in b make up a subset of the set bits in a.
45bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a,
46 blink::WebCryptoKeyUsageMask b);
47
eroman@chromium.org38409aec2014-07-19 00:54:5148bool KeyUsageAllows(const blink::WebCryptoKey& key,
49 const blink::WebCryptoKeyUsage usage);
50
eroman@chromium.org38409aec2014-07-19 00:54:5151Status GetAesGcmTagLengthInBits(const blink::WebCryptoAesGcmParams* params,
52 unsigned int* tag_length_bits);
53
54Status GetAesKeyGenLengthInBits(const blink::WebCryptoAesKeyGenParams* params,
55 unsigned int* keylen_bits);
56
57Status GetHmacKeyGenLengthInBits(const blink::WebCryptoHmacKeyGenParams* params,
58 unsigned int* keylen_bits);
59
eroman5d5199742014-12-10 22:18:0760// Gets the requested key length in bits for an HMAC import operation.
61Status GetHmacImportKeyLengthBits(
62 const blink::WebCryptoHmacImportParams* params,
63 unsigned int key_data_byte_length,
64 unsigned int* keylen_bits);
65
eroman@chromium.org38409aec2014-07-19 00:54:5166Status VerifyAesKeyLengthForImport(unsigned int keylen_bytes);
67
68Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages,
habib.virjif4aa92ee72014-12-19 10:04:3469 blink::WebCryptoKeyUsageMask actual_usages,
70 bool allow_empty_usages);
eroman@chromium.org38409aec2014-07-19 00:54:5171
eroman@chromium.orge9aa71a2014-07-25 08:38:0272// 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
78Status GetRsaKeyGenParameters(
79 const blink::WebCryptoRsaHashedKeyGenParams* params,
80 unsigned int* public_exponent,
81 unsigned int* modulus_length_bits);
82
eromandf493e22014-12-09 23:21:3383// Verifies that |usages| is valid when importing a key of the given format.
84Status VerifyUsagesBeforeImportAsymmetricKey(
85 blink::WebCryptoKeyFormat format,
86 blink::WebCryptoKeyUsageMask all_public_key_usages,
87 blink::WebCryptoKeyUsageMask all_private_key_usages,
88 blink::WebCryptoKeyUsageMask usages);
89
eroman401e4afc2014-12-10 17:41:0490// 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
100void 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.
106template <typename T>
107T NumBitsToBytes(T x) {
108 return (x / 8) + (7 + (x % 8)) / 8;
109}
110
eromanf93fd5b2014-12-11 00:21:06111// The "get key length" operation for AES keys.
112Status 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.
117Status GetHmacKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm,
118 bool* has_length_bits,
119 unsigned int* length_bits);
120
eromanae20f982014-12-11 04:55:47121// 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.
124Status 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.comb28852b2013-12-04 04:57:11131} // namespace webcrypto
132
erg56f12322015-04-17 00:51:48133#endif // COMPONENTS_WEBCRYPTO_WEBCRYPTO_UTIL_H_