1 /* 2 * Copyright (C) 2014 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 WebCryptoKeyAlgorithmParams_h 32 #define WebCryptoKeyAlgorithmParams_h 33 34 #include "WebCommon.h" 35 #include "WebCryptoAlgorithm.h" 36 #include "WebVector.h" 37 38 namespace blink { 39 40 enum WebCryptoKeyAlgorithmParamsType { 41 WebCryptoKeyAlgorithmParamsTypeNone, 42 WebCryptoKeyAlgorithmParamsTypeHmac, 43 WebCryptoKeyAlgorithmParamsTypeAes, 44 WebCryptoKeyAlgorithmParamsTypeRsaHashed 45 }; 46 47 class WebCryptoKeyAlgorithmParams { 48 public: 49 virtual ~WebCryptoKeyAlgorithmParams() { } 50 virtual WebCryptoKeyAlgorithmParamsType type() const 51 { 52 return WebCryptoKeyAlgorithmParamsTypeNone; 53 } 54 }; 55 56 class WebCryptoAesKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams { 57 public: 58 explicit WebCryptoAesKeyAlgorithmParams(unsigned short lengthBits) 59 : m_lengthBits(lengthBits) 60 { 61 } 62 63 unsigned short lengthBits() const 64 { 65 return m_lengthBits; 66 } 67 68 virtual WebCryptoKeyAlgorithmParamsType type() const 69 { 70 return WebCryptoKeyAlgorithmParamsTypeAes; 71 } 72 73 private: 74 unsigned short m_lengthBits; 75 }; 76 77 class WebCryptoHmacKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams { 78 public: 79 WebCryptoHmacKeyAlgorithmParams(const WebCryptoAlgorithm& hash, unsigned lengthBits) 80 : m_hash(hash) 81 , m_lengthBits(lengthBits) 82 { 83 } 84 85 const WebCryptoAlgorithm& hash() const 86 { 87 return m_hash; 88 } 89 90 unsigned lengthBits() const 91 { 92 return m_lengthBits; 93 } 94 95 virtual WebCryptoKeyAlgorithmParamsType type() const 96 { 97 return WebCryptoKeyAlgorithmParamsTypeHmac; 98 } 99 100 private: 101 WebCryptoAlgorithm m_hash; 102 unsigned m_lengthBits; 103 }; 104 105 class WebCryptoRsaHashedKeyAlgorithmParams : public WebCryptoKeyAlgorithmParams { 106 public: 107 WebCryptoRsaHashedKeyAlgorithmParams(unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize, const WebCryptoAlgorithm& hash) 108 : m_modulusLengthBits(modulusLengthBits) 109 , m_publicExponent(publicExponent, publicExponentSize) 110 , m_hash(hash) 111 { 112 } 113 114 unsigned modulusLengthBits() const 115 { 116 return m_modulusLengthBits; 117 } 118 119 const WebVector<unsigned char>& publicExponent() const 120 { 121 return m_publicExponent; 122 } 123 124 const WebCryptoAlgorithm& hash() const 125 { 126 return m_hash; 127 } 128 129 virtual WebCryptoKeyAlgorithmParamsType type() const 130 { 131 return WebCryptoKeyAlgorithmParamsTypeRsaHashed; 132 } 133 134 private: 135 unsigned m_modulusLengthBits; 136 WebVector<unsigned char> m_publicExponent; 137 WebCryptoAlgorithm m_hash; 138 }; 139 140 } // namespace blink 141 142 #endif 143