• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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 #ifndef CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_
6 #define CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 #include "base/basictypes.h"
12 #include "content/common/content_export.h"
13 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
15 
16 namespace content {
17 
18 namespace webcrypto {
19 
20 // Returns a pointer to the start of |data|, or NULL if it is empty. This is a
21 // convenience function for getting the pointer, and should not be used beyond
22 // the expected lifetime of |data|.
23 CONTENT_EXPORT const uint8* Uint8VectorStart(const std::vector<uint8>& data);
24 
25 // Shrinks a WebArrayBuffer to a new size.
26 // TODO(eroman): This works by re-allocating a new buffer. It would be better if
27 //               the WebArrayBuffer could just be truncated instead.
28 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size);
29 
30 // Creates a WebArrayBuffer from a uint8 byte array
31 blink::WebArrayBuffer CreateArrayBuffer(const uint8* data, unsigned data_size);
32 
33 // This function decodes unpadded 'base64url' encoded data, as described in
34 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5.
35 // In Web Crypto, this type of encoding is only used inside JWK.
36 bool Base64DecodeUrlSafe(const std::string& input, std::string* output);
37 
38 // Returns the "hash" param for an algorithm if it exists, otherwise returns
39 // a null algorithm.
40 blink::WebCryptoAlgorithm GetInnerHashAlgorithm(
41     const blink::WebCryptoAlgorithm& algorithm);
42 
43 // Creates a WebCryptoAlgorithm without any parameters.
44 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateAlgorithm(
45     blink::WebCryptoAlgorithmId id);
46 
47 // Creates an HMAC algorithm whose inner hash algorithm is determined by the
48 // specified hash output length. It is an error to call this method with an
49 // unsupported hash output length.
50 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashOutputLen(
51     unsigned short hash_output_length_bits);
52 
53 // Creates an HMAC algorithm whose inner hash algorithm is determined by the
54 // specified algorithm ID. It is an error to call this method with a hash
55 // algorithm that is not SHA*.
56 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId(
57     blink::WebCryptoAlgorithmId hash_id);
58 
59 // Creates an HMAC algorithm whose parameters struct is compatible with key
60 // generation. It is an error to call this with a hash_id that is not a SHA*.
61 // The key_length_bytes parameter is optional, with zero meaning unspecified.
62 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm(
63     blink::WebCryptoAlgorithmId hash_id,
64     unsigned key_length_bytes);
65 
66 // Creates an RSASSA-PKCS1-v1_5 algorithm. It is an error to call this with a
67 // hash_id that is not a SHA*.
68 blink::WebCryptoAlgorithm CreateRsaSsaAlgorithm(
69     blink::WebCryptoAlgorithmId hash_id);
70 
71 // Creates an RSA-OAEP algorithm. It is an error to call this with a hash_id
72 // that is not a SHA*.
73 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
74     blink::WebCryptoAlgorithmId hash_id);
75 
76 // Creates an RSA algorithm with ID algorithm_id, whose parameters struct is
77 // compatible with key generation.
78 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm(
79     blink::WebCryptoAlgorithmId algorithm_id,
80     unsigned modulus_length,
81     const std::vector<uint8>& public_exponent);
82 
83 // Creates an AES-CBC algorithm.
84 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(
85     const std::vector<uint8>& iv);
86 
87 // Creates and AES-GCM algorithm.
88 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
89     const std::vector<uint8>& iv,
90     const std::vector<uint8>& additional_data,
91     uint8 tag_length_bytes);
92 
93 // Creates an AES-CBC algorithm whose parameters struct is compatible with key
94 // generation.
95 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(
96     unsigned short key_length_bits);
97 
98 // Creates an AES-GCM algorithm whose parameters struct is compatible with key
99 // generation.
100 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(
101     unsigned short key_length_bits);
102 
103 }  // namespace webcrypto
104 
105 }  // namespace content
106 
107 #endif  // CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_
108