1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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. 17 * 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. 29 */ 30 31 #ifndef WebCryptoAlgorithmParams_h 32 #define WebCryptoAlgorithmParams_h 33 34 #include "WebCommon.h" 35 #include "WebCryptoAlgorithm.h" 36 #include "WebVector.h" 37 38 namespace blink { 39 40 // NOTE: For documentation on the meaning of each of the parameters see the 41 // Web crypto spec: 42 // 43 // http://www.w3.org/TR/WebCryptoAPI 44 // 45 // For the most part, the parameters in the spec have the same name, 46 // except that in the blink code: 47 // 48 // - Structure names are prefixed by "WebCrypto" 49 // - Optional fields are prefixed by "optional" 50 // - Data length properties are suffixed by either "Bits" or "Bytes" 51 52 class WebCryptoAlgorithmParams { 53 public: WebCryptoAlgorithmParams()54 WebCryptoAlgorithmParams() { } ~WebCryptoAlgorithmParams()55 virtual ~WebCryptoAlgorithmParams() { } 56 virtual WebCryptoAlgorithmParamsType type() const = 0; 57 }; 58 59 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams { 60 public: WebCryptoAesCbcParams(const unsigned char * iv,unsigned ivSize)61 WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize) 62 : m_iv(iv, ivSize) 63 { 64 } 65 type()66 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesCbcParams; } 67 iv()68 const WebVector<unsigned char>& iv() const { return m_iv; } 69 70 private: 71 const WebVector<unsigned char> m_iv; 72 }; 73 74 class WebCryptoAlgorithmParamsWithHash : public WebCryptoAlgorithmParams { 75 public: WebCryptoAlgorithmParamsWithHash(const WebCryptoAlgorithm & hash)76 explicit WebCryptoAlgorithmParamsWithHash(const WebCryptoAlgorithm& hash) 77 : m_hash(hash) 78 { 79 BLINK_ASSERT(!hash.isNull()); 80 } 81 hash()82 const WebCryptoAlgorithm& hash() const { return m_hash; } 83 84 private: 85 const WebCryptoAlgorithm m_hash; 86 }; 87 88 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams { 89 public: WebCryptoAesCtrParams(unsigned char lengthBits,const unsigned char * counter,unsigned counterSize)90 WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter, unsigned counterSize) 91 : WebCryptoAlgorithmParams() 92 , m_counter(counter, counterSize) 93 , m_lengthBits(lengthBits) 94 { 95 } 96 type()97 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesCtrParams; } 98 counter()99 const WebVector<unsigned char>& counter() const { return m_counter; } lengthBits()100 unsigned char lengthBits() const { return m_lengthBits; } 101 102 private: 103 const WebVector<unsigned char> m_counter; 104 const unsigned char m_lengthBits; 105 }; 106 107 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams { 108 public: WebCryptoAesKeyGenParams(unsigned short lengthBits)109 explicit WebCryptoAesKeyGenParams(unsigned short lengthBits) 110 : m_lengthBits(lengthBits) 111 { 112 } 113 type()114 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesKeyGenParams; } 115 lengthBits()116 unsigned short lengthBits() const { return m_lengthBits; } 117 118 private: 119 const unsigned short m_lengthBits; 120 }; 121 122 class WebCryptoHmacImportParams : public WebCryptoAlgorithmParamsWithHash { 123 public: WebCryptoHmacImportParams(const WebCryptoAlgorithm & hash)124 explicit WebCryptoHmacImportParams(const WebCryptoAlgorithm& hash) 125 : WebCryptoAlgorithmParamsWithHash(hash) 126 { 127 } 128 type()129 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeHmacImportParams; } 130 }; 131 132 class WebCryptoHmacKeyGenParams : public WebCryptoAlgorithmParamsWithHash { 133 public: WebCryptoHmacKeyGenParams(const WebCryptoAlgorithm & hash,bool hasLengthBits,unsigned lengthBits)134 WebCryptoHmacKeyGenParams(const WebCryptoAlgorithm& hash, bool hasLengthBits, unsigned lengthBits) 135 : WebCryptoAlgorithmParamsWithHash(hash) 136 , m_hasLengthBits(hasLengthBits) 137 , m_optionalLengthBits(lengthBits) 138 { 139 BLINK_ASSERT(hasLengthBits || !lengthBits); 140 } 141 type()142 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeHmacKeyGenParams; } 143 hasLengthBits()144 bool hasLengthBits() const { return m_hasLengthBits; } 145 optionalLengthBits()146 unsigned optionalLengthBits() const { return m_optionalLengthBits; } 147 148 private: 149 const bool m_hasLengthBits; 150 const unsigned m_optionalLengthBits; 151 }; 152 153 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams { 154 public: WebCryptoAesGcmParams(const unsigned char * iv,unsigned ivSize,bool hasAdditionalData,const unsigned char * additionalData,unsigned additionalDataSize,bool hasTagLengthBits,unsigned char tagLengthBits)155 WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAdditionalData, const unsigned char* additionalData, unsigned additionalDataSize, bool hasTagLengthBits, unsigned char tagLengthBits) 156 : m_iv(iv, ivSize) 157 , m_hasAdditionalData(hasAdditionalData) 158 , m_optionalAdditionalData(additionalData, additionalDataSize) 159 , m_hasTagLengthBits(hasTagLengthBits) 160 , m_optionalTagLengthBits(tagLengthBits) 161 { 162 BLINK_ASSERT(hasAdditionalData || !additionalDataSize); 163 BLINK_ASSERT(hasTagLengthBits || !tagLengthBits); 164 } 165 type()166 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeAesGcmParams; } 167 iv()168 const WebVector<unsigned char>& iv() const { return m_iv; } 169 hasAdditionalData()170 bool hasAdditionalData() const { return m_hasAdditionalData; } optionalAdditionalData()171 const WebVector<unsigned char>& optionalAdditionalData() const { return m_optionalAdditionalData; } 172 hasTagLengthBits()173 bool hasTagLengthBits() const { return m_hasTagLengthBits; } optionalTagLengthBits()174 unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; } 175 176 private: 177 const WebVector<unsigned char> m_iv; 178 const bool m_hasAdditionalData; 179 const WebVector<unsigned char> m_optionalAdditionalData; 180 const bool m_hasTagLengthBits; 181 const unsigned char m_optionalTagLengthBits; 182 }; 183 184 class WebCryptoRsaHashedImportParams : public WebCryptoAlgorithmParamsWithHash { 185 public: WebCryptoRsaHashedImportParams(const WebCryptoAlgorithm & hash)186 explicit WebCryptoRsaHashedImportParams(const WebCryptoAlgorithm& hash) 187 : WebCryptoAlgorithmParamsWithHash(hash) 188 { 189 } 190 type()191 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaHashedImportParams; } 192 }; 193 194 class WebCryptoRsaHashedKeyGenParams : public WebCryptoAlgorithmParams { 195 public: WebCryptoRsaHashedKeyGenParams(const WebCryptoAlgorithm & hash,unsigned modulusLengthBits,const unsigned char * publicExponent,unsigned publicExponentSize)196 explicit WebCryptoRsaHashedKeyGenParams(const WebCryptoAlgorithm& hash, unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize) 197 : m_modulusLengthBits(modulusLengthBits) 198 , m_publicExponent(publicExponent, publicExponentSize) 199 , m_hash(hash) 200 { 201 BLINK_ASSERT(!hash.isNull()); 202 } 203 type()204 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams; } 205 modulusLengthBits()206 unsigned modulusLengthBits() const { return m_modulusLengthBits; } publicExponent()207 const WebVector<unsigned char>& publicExponent() const { return m_publicExponent; } hash()208 const WebCryptoAlgorithm& hash() const { return m_hash; } 209 210 private: 211 const unsigned m_modulusLengthBits; 212 const WebVector<unsigned char> m_publicExponent; 213 const WebCryptoAlgorithm m_hash; 214 }; 215 216 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams { 217 public: WebCryptoRsaOaepParams(bool hasLabel,const unsigned char * label,unsigned labelSize)218 WebCryptoRsaOaepParams(bool hasLabel, const unsigned char* label, unsigned labelSize) 219 : m_hasLabel(hasLabel) 220 , m_optionalLabel(label, labelSize) 221 { 222 BLINK_ASSERT(hasLabel || !labelSize); 223 } 224 type()225 virtual WebCryptoAlgorithmParamsType type() const { return WebCryptoAlgorithmParamsTypeRsaOaepParams; } 226 hasLabel()227 bool hasLabel() const { return m_hasLabel; } optionalLabel()228 const WebVector<unsigned char>& optionalLabel() const { return m_optionalLabel; } 229 230 private: 231 const bool m_hasLabel; 232 const WebVector<unsigned char> m_optionalLabel; 233 }; 234 235 } // namespace blink 236 237 #endif 238