Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / public / platform / WebCryptoKeyAlgorithmParams.h
blob0944adb6117fcc870a816b2858bc62895f6ee5ac
1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WebCryptoKeyAlgorithmParams_h
32 #define WebCryptoKeyAlgorithmParams_h
34 #include "WebCommon.h"
35 #include "WebCryptoAlgorithm.h"
36 #include "WebVector.h"
38 #if INSIDE_BLINK
39 #include "platform/heap/Handle.h"
40 #endif
42 namespace blink {
44 // Interface used for serializing WebCryptoKeyAlgorithmParams to a javascript
45 // dictionary.
46 class WebCryptoKeyAlgorithmDictionary {
47 #if INSIDE_BLINK
48 STACK_ALLOCATED();
49 #endif
50 public:
51 virtual ~WebCryptoKeyAlgorithmDictionary() { }
53 virtual void setString(const char*, const char*) = 0;
54 virtual void setUint(const char*, unsigned) = 0;
55 virtual void setAlgorithm(const char*, const WebCryptoAlgorithm&) = 0;
56 virtual void setUint8Array(const char*, const WebVector<unsigned char>&) = 0;
59 enum WebCryptoKeyAlgorithmParamsType {
60 WebCryptoKeyAlgorithmParamsTypeNone,
61 WebCryptoKeyAlgorithmParamsTypeHmac,
62 WebCryptoKeyAlgorithmParamsTypeAes,
63 WebCryptoKeyAlgorithmParamsTypeRsaHashed,
64 WebCryptoKeyAlgorithmParamsTypeEc,
67 class WebCryptoKeyAlgorithmParams {
68 public:
69 virtual ~WebCryptoKeyAlgorithmParams() { }
70 virtual WebCryptoKeyAlgorithmParamsType type() const
72 return WebCryptoKeyAlgorithmParamsTypeNone;
75 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary*) const = 0;
78 class WebCryptoAesKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams {
79 public:
80 explicit WebCryptoAesKeyAlgorithmParams(unsigned short lengthBits)
81 : m_lengthBits(lengthBits)
85 unsigned short lengthBits() const
87 return m_lengthBits;
90 virtual WebCryptoKeyAlgorithmParamsType type() const
92 return WebCryptoKeyAlgorithmParamsTypeAes;
95 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
97 dict->setUint("length", m_lengthBits);
100 private:
101 unsigned short m_lengthBits;
104 class WebCryptoHmacKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams {
105 public:
106 WebCryptoHmacKeyAlgorithmParams(const WebCryptoAlgorithm& hash, unsigned lengthBits)
107 : m_hash(hash)
108 , m_lengthBits(lengthBits)
112 const WebCryptoAlgorithm& hash() const
114 return m_hash;
117 unsigned lengthBits() const
119 return m_lengthBits;
122 virtual WebCryptoKeyAlgorithmParamsType type() const
124 return WebCryptoKeyAlgorithmParamsTypeHmac;
127 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
129 dict->setAlgorithm("hash", m_hash);
130 dict->setUint("length", m_lengthBits);
133 private:
134 WebCryptoAlgorithm m_hash;
135 unsigned m_lengthBits;
138 class WebCryptoRsaHashedKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams {
139 public:
140 WebCryptoRsaHashedKeyAlgorithmParams(unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize, const WebCryptoAlgorithm& hash)
141 : m_modulusLengthBits(modulusLengthBits)
142 , m_publicExponent(publicExponent, publicExponentSize)
143 , m_hash(hash)
147 unsigned modulusLengthBits() const
149 return m_modulusLengthBits;
152 const WebVector<unsigned char>& publicExponent() const
154 return m_publicExponent;
157 const WebCryptoAlgorithm& hash() const
159 return m_hash;
162 virtual WebCryptoKeyAlgorithmParamsType type() const
164 return WebCryptoKeyAlgorithmParamsTypeRsaHashed;
167 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
169 dict->setAlgorithm("hash", m_hash);
170 dict->setUint("modulusLength", m_modulusLengthBits);
171 dict->setUint8Array("publicExponent", m_publicExponent);
174 private:
175 unsigned m_modulusLengthBits;
176 WebVector<unsigned char> m_publicExponent;
177 WebCryptoAlgorithm m_hash;
180 class WebCryptoEcKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams {
181 public:
182 explicit WebCryptoEcKeyAlgorithmParams(WebCryptoNamedCurve namedCurve)
183 : m_namedCurve(namedCurve)
187 WebCryptoNamedCurve namedCurve() const
189 return m_namedCurve;
192 virtual WebCryptoKeyAlgorithmParamsType type() const
194 return WebCryptoKeyAlgorithmParamsTypeEc;
197 virtual void writeToDictionary(WebCryptoKeyAlgorithmDictionary* dict) const
199 switch (m_namedCurve) {
200 case WebCryptoNamedCurveP256:
201 dict->setString("namedCurve", "P-256");
202 break;
203 case WebCryptoNamedCurveP384:
204 dict->setString("namedCurve", "P-384");
205 break;
206 case WebCryptoNamedCurveP521:
207 dict->setString("namedCurve", "P-521");
208 break;
212 private:
213 const WebCryptoNamedCurve m_namedCurve;
216 } // namespace blink
218 #endif