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 COMPONENTS_WEBCRYPTO_ALGORITHMS_SECRET_KEY_UTIL_
6 #define COMPONENTS_WEBCRYPTO_ALGORITHMS_SECRET_KEY_UTIL_
11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
12 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
14 // This file contains functions shared by multiple symmetric key algorithms.
19 class GenerateKeyResult
;
23 // Generates a random secret key of the given bit length. If the bit length is
24 // not a multiple of 8, then the resulting key will have ceil(keylen_bits / 8)
25 // bytes, and the "unused" bits will be set to zero. This function does not do
26 // any validation checks on the provided parameters.
27 Status
GenerateWebCryptoSecretKey(const blink::WebCryptoKeyAlgorithm
& algorithm
,
29 blink::WebCryptoKeyUsageMask usages
,
30 unsigned int keylen_bits
,
31 GenerateKeyResult
* result
);
33 // Creates a WebCrypto secret key given a the raw data. The provided |key_data|
34 // will be copied into the new key. This function does not do any validation
35 // checks for the provided parameters.
36 Status
CreateWebCryptoSecretKey(const CryptoData
& key_data
,
37 const blink::WebCryptoKeyAlgorithm
& algorithm
,
39 blink::WebCryptoKeyUsageMask usages
,
40 blink::WebCryptoKey
* key
);
42 // Writes a JWK-formatted symmetric key to |jwk_key_data|.
43 // * raw_key_data: The actual key data
44 // * algorithm: The JWK algorithm name (i.e. "alg")
45 // * extractable: The JWK extractability (i.e. "ext")
46 // * usages: The JWK usages (i.e. "key_ops")
47 void WriteSecretKeyJwk(const CryptoData
& raw_key_data
,
48 const std::string
& algorithm
,
50 blink::WebCryptoKeyUsageMask usages
,
51 std::vector
<uint8_t>* jwk_key_data
);
53 // Parses a UTF-8 encoded JWK (key_data), and extracts the key material to
54 // |*raw_key_data|. Returns Status::Success() on success, otherwise an error.
55 // In order for this to succeed:
56 // * expected_extractable must be consistent with the JWK's "ext", if
58 // * expected_usages must be a subset of the JWK's "key_ops" if present.
59 Status
ReadSecretKeyNoExpectedAlgJwk(
60 const CryptoData
& key_data
,
61 bool expected_extractable
,
62 blink::WebCryptoKeyUsageMask expected_usages
,
63 std::vector
<uint8_t>* raw_key_data
,
66 } // namespace webcrypto
68 #endif // COMPONENTS_WEBCRYPTO_ALGORITHMS_SECRET_KEY_UTIL_