OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 // | 44 // |
45 // For the most part, the parameters in the spec have the same name, | 45 // For the most part, the parameters in the spec have the same name, |
46 // except that in the blink code: | 46 // except that in the blink code: |
47 // | 47 // |
48 // - Structure names are prefixed by "WebCrypto" | 48 // - Structure names are prefixed by "WebCrypto" |
49 // - Optional fields are prefixed by "optional" | 49 // - Optional fields are prefixed by "optional" |
50 // - Data length properties are suffixed by either "Bits" or "Bytes" | 50 // - Data length properties are suffixed by either "Bits" or "Bytes" |
51 | 51 |
52 class WebCryptoAlgorithmParams { | 52 class WebCryptoAlgorithmParams { |
53 public: | 53 public: |
54 explicit WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsType type) | 54 WebCryptoAlgorithmParams() { } |
55 : m_type(type) | |
56 { | |
57 } | |
58 | |
59 virtual ~WebCryptoAlgorithmParams() { } | 55 virtual ~WebCryptoAlgorithmParams() { } |
60 | 56 virtual WebCryptoAlgorithmParamsType type() const = 0; |
61 WebCryptoAlgorithmParamsType type() const { return m_type; } | |
62 | |
63 private: | |
64 const WebCryptoAlgorithmParamsType m_type; | |
65 }; | 57 }; |
66 | 58 |
67 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams { | 59 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams { |
68 public: | 60 public: |
69 WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize) | 61 WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize) |
70 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCbcParams) | 62 : m_iv(iv, ivSize) |
71 , m_iv(iv, ivSize) | |
72 { | 63 { |
73 } | 64 } |
74 | 65 |
| 66 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeAesCbcParams; } |
| 67 |
75 const WebVector<unsigned char>& iv() const { return m_iv; } | 68 const WebVector<unsigned char>& iv() const { return m_iv; } |
76 | 69 |
77 private: | 70 private: |
78 const WebVector<unsigned char> m_iv; | 71 const WebVector<unsigned char> m_iv; |
79 }; | 72 }; |
80 | 73 |
| 74 class WebCryptoAlgorithmParamsWithHash : public WebCryptoAlgorithmParams { |
| 75 public: |
| 76 explicit WebCryptoAlgorithmParamsWithHash(const WebCryptoAlgorithm& hash) |
| 77 : m_hash(hash) |
| 78 { |
| 79 BLINK_ASSERT(!hash.isNull()); |
| 80 } |
| 81 |
| 82 const WebCryptoAlgorithm& hash() const { return m_hash; } |
| 83 |
| 84 private: |
| 85 const WebCryptoAlgorithm m_hash; |
| 86 }; |
| 87 |
81 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams { | 88 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams { |
82 public: | 89 public: |
83 WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter
, unsigned counterSize) | 90 WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter
, unsigned counterSize) |
84 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCtrParams) | 91 : WebCryptoAlgorithmParams() |
85 , m_counter(counter, counterSize) | 92 , m_counter(counter, counterSize) |
86 , m_lengthBits(lengthBits) | 93 , m_lengthBits(lengthBits) |
87 { | 94 { |
88 } | 95 } |
89 | 96 |
| 97 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeAesCtrParams; } |
| 98 |
90 const WebVector<unsigned char>& counter() const { return m_counter; } | 99 const WebVector<unsigned char>& counter() const { return m_counter; } |
91 unsigned char lengthBits() const { return m_lengthBits; } | 100 unsigned char lengthBits() const { return m_lengthBits; } |
92 | 101 |
93 private: | 102 private: |
94 const WebVector<unsigned char> m_counter; | 103 const WebVector<unsigned char> m_counter; |
95 const unsigned char m_lengthBits; | 104 const unsigned char m_lengthBits; |
96 }; | 105 }; |
97 | 106 |
98 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams { | 107 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams { |
99 public: | 108 public: |
100 explicit WebCryptoAesKeyGenParams(unsigned short lengthBits) | 109 explicit WebCryptoAesKeyGenParams(unsigned short lengthBits) |
101 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesKeyGenParams) | 110 : m_lengthBits(lengthBits) |
102 , m_lengthBits(lengthBits) | |
103 { | 111 { |
104 } | 112 } |
105 | 113 |
| 114 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeAesKeyGenParams; } |
| 115 |
106 unsigned short lengthBits() const { return m_lengthBits; } | 116 unsigned short lengthBits() const { return m_lengthBits; } |
107 | 117 |
108 private: | 118 private: |
109 const unsigned short m_lengthBits; | 119 const unsigned short m_lengthBits; |
110 }; | 120 }; |
111 | 121 |
112 class WebCryptoHmacParams : public WebCryptoAlgorithmParams { | 122 class WebCryptoHmacImportParams : public WebCryptoAlgorithmParamsWithHash { |
113 public: | 123 public: |
114 explicit WebCryptoHmacParams(const WebCryptoAlgorithm& hash) | 124 explicit WebCryptoHmacImportParams(const WebCryptoAlgorithm& hash) |
115 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacParams) | 125 : WebCryptoAlgorithmParamsWithHash(hash) |
116 , m_hash(hash) | |
117 { | 126 { |
118 BLINK_ASSERT(!hash.isNull()); | |
119 } | 127 } |
120 | 128 |
121 const WebCryptoAlgorithm& hash() const { return m_hash; } | 129 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeHmacImportParams; } |
122 | |
123 private: | |
124 const WebCryptoAlgorithm m_hash; | |
125 }; | 130 }; |
126 | 131 |
127 class WebCryptoHmacKeyParams : public WebCryptoAlgorithmParams { | 132 class WebCryptoHmacKeyGenParams : public WebCryptoAlgorithmParamsWithHash { |
128 public: | 133 public: |
129 WebCryptoHmacKeyParams(const WebCryptoAlgorithm& hash, bool hasLengthBytes,
unsigned lengthBytes) | 134 WebCryptoHmacKeyGenParams(const WebCryptoAlgorithm& hash, bool hasLengthByte
s, unsigned lengthBytes) |
130 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacKeyParams) | 135 : WebCryptoAlgorithmParamsWithHash(hash) |
131 , m_hash(hash) | |
132 , m_hasLengthBytes(hasLengthBytes) | 136 , m_hasLengthBytes(hasLengthBytes) |
133 , m_optionalLengthBytes(lengthBytes) | 137 , m_optionalLengthBytes(lengthBytes) |
134 { | 138 { |
135 BLINK_ASSERT(!hash.isNull()); | |
136 BLINK_ASSERT(hasLengthBytes || !lengthBytes); | 139 BLINK_ASSERT(hasLengthBytes || !lengthBytes); |
137 } | 140 } |
138 | 141 |
139 const WebCryptoAlgorithm& hash() const { return m_hash; } | 142 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeHmacKeyGenParams; } |
140 | 143 |
141 bool hasLengthBytes() const { return m_hasLengthBytes; } | 144 bool hasLengthBytes() const { return m_hasLengthBytes; } |
142 | 145 |
143 unsigned optionalLengthBytes() const { return m_optionalLengthBytes; } | 146 unsigned optionalLengthBytes() const { return m_optionalLengthBytes; } |
144 | 147 |
145 private: | 148 private: |
146 const WebCryptoAlgorithm m_hash; | |
147 const bool m_hasLengthBytes; | 149 const bool m_hasLengthBytes; |
148 const unsigned m_optionalLengthBytes; | 150 const unsigned m_optionalLengthBytes; |
149 }; | 151 }; |
150 | 152 |
151 class WebCryptoRsaSsaParams : public WebCryptoAlgorithmParams { | |
152 public: | |
153 explicit WebCryptoRsaSsaParams(const WebCryptoAlgorithm& hash) | |
154 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaSsaParams) | |
155 , m_hash(hash) | |
156 { | |
157 BLINK_ASSERT(!hash.isNull()); | |
158 } | |
159 | |
160 const WebCryptoAlgorithm& hash() const { return m_hash; } | |
161 | |
162 private: | |
163 const WebCryptoAlgorithm m_hash; | |
164 }; | |
165 | |
166 class WebCryptoRsaKeyGenParams : public WebCryptoAlgorithmParams { | 153 class WebCryptoRsaKeyGenParams : public WebCryptoAlgorithmParams { |
167 public: | 154 public: |
168 WebCryptoRsaKeyGenParams(unsigned modulusLengthBits, const unsigned char* pu
blicExponent, unsigned publicExponentSize) | 155 WebCryptoRsaKeyGenParams(unsigned modulusLengthBits, const unsigned char* pu
blicExponent, unsigned publicExponentSize) |
169 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaKeyGenParams) | 156 : m_modulusLengthBits(modulusLengthBits) |
170 , m_modulusLengthBits(modulusLengthBits) | |
171 , m_publicExponent(publicExponent, publicExponentSize) | 157 , m_publicExponent(publicExponent, publicExponentSize) |
172 { | 158 { |
173 } | 159 } |
174 | 160 |
| 161 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeRsaKeyGenParams; } |
| 162 |
175 unsigned modulusLengthBits() const { return m_modulusLengthBits; } | 163 unsigned modulusLengthBits() const { return m_modulusLengthBits; } |
176 const WebVector<unsigned char>& publicExponent() const { return m_publicExpo
nent; } | 164 const WebVector<unsigned char>& publicExponent() const { return m_publicExpo
nent; } |
177 | 165 |
178 private: | 166 private: |
179 const unsigned m_modulusLengthBits; | 167 const unsigned m_modulusLengthBits; |
180 const WebVector<unsigned char> m_publicExponent; | 168 const WebVector<unsigned char> m_publicExponent; |
181 }; | 169 }; |
182 | 170 |
183 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams { | 171 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams { |
184 public: | 172 public: |
185 WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAddi
tionalData, const unsigned char* additionalData, unsigned additionalDataSize, bo
ol hasTagLengthBits, unsigned char tagLengthBits) | 173 WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAddi
tionalData, const unsigned char* additionalData, unsigned additionalDataSize, bo
ol hasTagLengthBits, unsigned char tagLengthBits) |
186 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesGcmParams) | 174 : m_iv(iv, ivSize) |
187 , m_iv(iv, ivSize) | |
188 , m_hasAdditionalData(hasAdditionalData) | 175 , m_hasAdditionalData(hasAdditionalData) |
189 , m_optionalAdditionalData(additionalData, additionalDataSize) | 176 , m_optionalAdditionalData(additionalData, additionalDataSize) |
190 , m_hasTagLengthBits(hasTagLengthBits) | 177 , m_hasTagLengthBits(hasTagLengthBits) |
191 , m_optionalTagLengthBits(tagLengthBits) | 178 , m_optionalTagLengthBits(tagLengthBits) |
192 { | 179 { |
193 BLINK_ASSERT(hasAdditionalData || !additionalDataSize); | 180 BLINK_ASSERT(hasAdditionalData || !additionalDataSize); |
194 BLINK_ASSERT(hasTagLengthBits || !tagLengthBits); | 181 BLINK_ASSERT(hasTagLengthBits || !tagLengthBits); |
195 } | 182 } |
196 | 183 |
| 184 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeAesGcmParams; } |
| 185 |
197 const WebVector<unsigned char>& iv() const { return m_iv; } | 186 const WebVector<unsigned char>& iv() const { return m_iv; } |
198 | 187 |
199 bool hasAdditionalData() const { return m_hasAdditionalData; } | 188 bool hasAdditionalData() const { return m_hasAdditionalData; } |
200 const WebVector<unsigned char>& optionalAdditionalData() const { return m_op
tionalAdditionalData; } | 189 const WebVector<unsigned char>& optionalAdditionalData() const { return m_op
tionalAdditionalData; } |
201 | 190 |
202 bool hasTagLengthBits() const { return m_hasTagLengthBits; } | 191 bool hasTagLengthBits() const { return m_hasTagLengthBits; } |
203 unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; } | 192 unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; } |
204 | 193 |
205 private: | 194 private: |
206 const WebVector<unsigned char> m_iv; | 195 const WebVector<unsigned char> m_iv; |
207 const bool m_hasAdditionalData; | 196 const bool m_hasAdditionalData; |
208 const WebVector<unsigned char> m_optionalAdditionalData; | 197 const WebVector<unsigned char> m_optionalAdditionalData; |
209 const bool m_hasTagLengthBits; | 198 const bool m_hasTagLengthBits; |
210 const unsigned char m_optionalTagLengthBits; | 199 const unsigned char m_optionalTagLengthBits; |
211 }; | 200 }; |
212 | 201 |
| 202 class WebCryptoRsaHashedImportParams : public WebCryptoAlgorithmParamsWithHash { |
| 203 public: |
| 204 explicit WebCryptoRsaHashedImportParams(const WebCryptoAlgorithm& hash) |
| 205 : WebCryptoAlgorithmParamsWithHash(hash) |
| 206 { |
| 207 } |
| 208 |
| 209 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeRsaHashedImportParams; } |
| 210 }; |
| 211 |
| 212 class WebCryptoRsaHashedKeyGenParams : public WebCryptoRsaKeyGenParams { |
| 213 public: |
| 214 explicit WebCryptoRsaHashedKeyGenParams(const WebCryptoAlgorithm& hash, unsi
gned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExpo
nentSize) |
| 215 : WebCryptoRsaKeyGenParams(modulusLengthBits, publicExponent, publicExpo
nentSize) |
| 216 , m_hash(hash) |
| 217 { |
| 218 BLINK_ASSERT(!hash.isNull()); |
| 219 } |
| 220 |
| 221 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeRsaHashedKeyGenParams; } |
| 222 |
| 223 const WebCryptoAlgorithm& hash() const { return m_hash; } |
| 224 |
| 225 private: |
| 226 const WebCryptoAlgorithm m_hash; |
| 227 }; |
| 228 |
213 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams { | 229 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams { |
214 public: | 230 public: |
215 WebCryptoRsaOaepParams(const WebCryptoAlgorithm& hash, bool hasLabel, const
unsigned char* label, unsigned labelSize) | 231 WebCryptoRsaOaepParams(bool hasLabel, const unsigned char* label, unsigned l
abelSize) |
216 : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaOaepParams) | 232 : m_hasLabel(hasLabel) |
217 , m_hash(hash) | |
218 , m_hasLabel(hasLabel) | |
219 , m_optionalLabel(label, labelSize) | 233 , m_optionalLabel(label, labelSize) |
220 { | 234 { |
221 BLINK_ASSERT(!hash.isNull()); | |
222 BLINK_ASSERT(hasLabel || !labelSize); | 235 BLINK_ASSERT(hasLabel || !labelSize); |
223 } | 236 } |
224 | 237 |
225 const WebCryptoAlgorithm& hash() const { return m_hash; } | 238 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorith
mParamsTypeRsaOaepParams; } |
226 | 239 |
227 bool hasLabel() const { return m_hasLabel; } | 240 bool hasLabel() const { return m_hasLabel; } |
228 const WebVector<unsigned char>& optionalLabel() const { return m_optionalLab
el; } | 241 const WebVector<unsigned char>& optionalLabel() const { return m_optionalLab
el; } |
229 | 242 |
230 private: | 243 private: |
231 const WebCryptoAlgorithm m_hash; | |
232 const bool m_hasLabel; | 244 const bool m_hasLabel; |
233 const WebVector<unsigned char> m_optionalLabel; | 245 const WebVector<unsigned char> m_optionalLabel; |
234 }; | 246 }; |
235 | 247 |
236 } // namespace blink | 248 } // namespace blink |
237 | 249 |
238 #endif | 250 #endif |
OLD | NEW |