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.
5 #ifndef CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
6 #define CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
12 #include "base/strings/string_piece.h"
13 #include "base/values.h"
14 #include "content/common/content_export.h"
15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
16 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
24 // This function decodes unpadded 'base64url' encoded data, as described in
25 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5.
26 // In Web Crypto, this type of encoding is only used inside JWK.
27 CONTENT_EXPORT
bool Base64DecodeUrlSafe(const std::string
& input
,
30 // Returns an unpadded 'base64url' encoding of the input data, the opposite of
31 // Base64DecodeUrlSafe() above.
32 CONTENT_EXPORT
std::string
Base64EncodeUrlSafe(const base::StringPiece
& input
);
33 CONTENT_EXPORT
std::string
Base64EncodeUrlSafe(
34 const std::vector
<uint8_t>& input
);
36 // Composes a Web Crypto usage mask from an array of JWK key_ops values.
37 CONTENT_EXPORT Status
GetWebCryptoUsagesFromJwkKeyOps(
38 const base::ListValue
* jwk_key_ops_value
,
39 blink::WebCryptoKeyUsageMask
* jwk_key_ops_mask
);
41 // Composes a JWK key_ops array from a Web Crypto usage mask.
42 base::ListValue
* CreateJwkKeyOpsFromWebCryptoUsages(
43 blink::WebCryptoKeyUsageMask usage_mask
);
45 // Creates a WebCryptoAlgorithm without any parameters.
46 CONTENT_EXPORT
blink::WebCryptoAlgorithm
CreateAlgorithm(
47 blink::WebCryptoAlgorithmId id
);
49 // Creates an HMAC import algorithm whose inner hash algorithm is determined by
50 // the specified algorithm ID. It is an error to call this method with a hash
51 // algorithm that is not SHA*.
52 CONTENT_EXPORT
blink::WebCryptoAlgorithm
CreateHmacImportAlgorithm(
53 blink::WebCryptoAlgorithmId hash_id
);
55 // Creates an import algorithm for RSA algorithms that take a hash.
56 // It is an error to call this with a hash_id that is not a SHA*.
57 CONTENT_EXPORT
blink::WebCryptoAlgorithm
CreateRsaHashedImportAlgorithm(
58 blink::WebCryptoAlgorithmId id
,
59 blink::WebCryptoAlgorithmId hash_id
);
61 // Returns true if the set bits in b make up a subset of the set bits in a.
62 bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a
,
63 blink::WebCryptoKeyUsageMask b
);
65 bool KeyUsageAllows(const blink::WebCryptoKey
& key
,
66 const blink::WebCryptoKeyUsage usage
);
68 bool IsAlgorithmRsa(blink::WebCryptoAlgorithmId alg_id
);
69 bool IsAlgorithmAsymmetric(blink::WebCryptoAlgorithmId alg_id
);
71 Status
GetAesGcmTagLengthInBits(const blink::WebCryptoAesGcmParams
* params
,
72 unsigned int* tag_length_bits
);
74 Status
GetAesKeyGenLengthInBits(const blink::WebCryptoAesKeyGenParams
* params
,
75 unsigned int* keylen_bits
);
77 Status
GetHmacKeyGenLengthInBits(const blink::WebCryptoHmacKeyGenParams
* params
,
78 unsigned int* keylen_bits
);
80 Status
VerifyAesKeyLengthForImport(unsigned int keylen_bytes
);
82 Status
CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages
,
83 blink::WebCryptoKeyUsageMask actual_usages
);
85 // Extracts the public exponent and modulus length from the Blink parameters.
86 // On success it is guaranteed that:
87 // * public_exponent is either 3 or 65537
88 // * modulus_length_bits is a multiple of 8
89 // * modulus_length is >= 256
90 // * modulus_length is <= 16K
91 Status
GetRsaKeyGenParameters(
92 const blink::WebCryptoRsaHashedKeyGenParams
* params
,
93 unsigned int* public_exponent
,
94 unsigned int* modulus_length_bits
);
96 } // namespace webcrypto
98 } // namespace content
100 #endif // CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_