Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / webcrypto / algorithms / secret_key_util.h
blob19ebdbb9b3432159d5c04849607ac387efa05031
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_
8 #include <string>
9 #include <vector>
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.
16 namespace webcrypto {
18 class CryptoData;
19 class GenerateKeyResult;
20 class JwkReader;
21 class Status;
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,
28 bool extractable,
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,
38 bool extractable,
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,
49 bool extractable,
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
57 // present.
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,
64 JwkReader* jwk);
66 } // namespace webcrypto
68 #endif // COMPONENTS_WEBCRYPTO_ALGORITHMS_SECRET_KEY_UTIL_