Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(875)

Side by Side Diff: components/webcrypto/algorithms/secret_key_util.cc

Issue 1355873002: [refactor] More post-NSS WebCrypto cleanups (utility functions). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address David's comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "components/webcrypto/algorithms/secret_key_util.h"
6
7 #include <openssl/rand.h>
8
9 #include "base/stl_util.h"
10 #include "components/webcrypto/crypto_data.h"
11 #include "components/webcrypto/generate_key_result.h"
12 #include "components/webcrypto/jwk.h"
13 #include "components/webcrypto/key.h"
14 #include "components/webcrypto/status.h"
15 #include "components/webcrypto/webcrypto_util.h"
16 #include "crypto/openssl_util.h"
17
18 namespace webcrypto {
19
20 Status GenerateWebCryptoSecretKey(const blink::WebCryptoKeyAlgorithm& algorithm,
21 bool extractable,
22 blink::WebCryptoKeyUsageMask usages,
23 unsigned int keylen_bits,
24 GenerateKeyResult* result) {
25 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
26
27 unsigned int keylen_bytes = NumBitsToBytes(keylen_bits);
28 std::vector<unsigned char> random_bytes(keylen_bytes, 0);
29
30 if (keylen_bytes > 0) {
31 if (!RAND_bytes(vector_as_array(&random_bytes), keylen_bytes))
32 return Status::OperationError();
33 TruncateToBitLength(keylen_bits, &random_bytes);
34 }
35
36 result->AssignSecretKey(blink::WebCryptoKey::create(
37 CreateSymmetricKeyHandle(CryptoData(random_bytes)),
38 blink::WebCryptoKeyTypeSecret, extractable, algorithm, usages));
39
40 return Status::Success();
41 }
42
43 Status CreateWebCryptoSecretKey(const CryptoData& key_data,
44 const blink::WebCryptoKeyAlgorithm& algorithm,
45 bool extractable,
46 blink::WebCryptoKeyUsageMask usages,
47 blink::WebCryptoKey* key) {
48 *key = blink::WebCryptoKey::create(CreateSymmetricKeyHandle(key_data),
49 blink::WebCryptoKeyTypeSecret, extractable,
50 algorithm, usages);
51 return Status::Success();
52 }
53
54 void WriteSecretKeyJwk(const CryptoData& raw_key_data,
55 const std::string& algorithm,
56 bool extractable,
57 blink::WebCryptoKeyUsageMask usages,
58 std::vector<uint8_t>* jwk_key_data) {
59 JwkWriter writer(algorithm, extractable, usages, "oct");
60 writer.SetBytes("k", raw_key_data);
61 writer.ToJson(jwk_key_data);
62 }
63
64 Status ReadSecretKeyNoExpectedAlgJwk(
65 const CryptoData& key_data,
66 bool expected_extractable,
67 blink::WebCryptoKeyUsageMask expected_usages,
68 std::vector<uint8_t>* raw_key_data,
69 JwkReader* jwk) {
70 Status status = jwk->Init(key_data, expected_extractable, expected_usages,
71 "oct", std::string());
72 if (status.IsError())
73 return status;
74
75 std::string jwk_k_value;
76 status = jwk->GetBytes("k", &jwk_k_value);
77 if (status.IsError())
78 return status;
79 raw_key_data->assign(jwk_k_value.begin(), jwk_k_value.end());
80
81 return Status::Success();
82 }
83
84 } // namespace webcrypto
OLDNEW
« no previous file with comments | « components/webcrypto/algorithms/secret_key_util.h ('k') | components/webcrypto/algorithms/test_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698