Home | History | Annotate | Download | only in platform
      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:
     54     explicit WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsType type)
     55         : m_type(type)
     56     {
     57     }
     58 
     59     virtual ~WebCryptoAlgorithmParams() { }
     60 
     61     WebCryptoAlgorithmParamsType type() const { return m_type; }
     62 
     63 private:
     64     const WebCryptoAlgorithmParamsType m_type;
     65 };
     66 
     67 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams {
     68 public:
     69     WebCryptoAesCbcParams(const unsigned char* iv, unsigned ivSize)
     70         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCbcParams)
     71         , m_iv(iv, ivSize)
     72     {
     73     }
     74 
     75     const WebVector<unsigned char>& iv() const { return m_iv; }
     76 
     77 private:
     78     const WebVector<unsigned char> m_iv;
     79 };
     80 
     81 class WebCryptoAesCtrParams : public WebCryptoAlgorithmParams {
     82 public:
     83     WebCryptoAesCtrParams(unsigned char lengthBits, const unsigned char* counter, unsigned counterSize)
     84         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCtrParams)
     85         , m_counter(counter, counterSize)
     86         , m_lengthBits(lengthBits)
     87     {
     88     }
     89 
     90     const WebVector<unsigned char>& counter() const { return m_counter; }
     91     unsigned char lengthBits() const { return m_lengthBits; }
     92 
     93 private:
     94     const WebVector<unsigned char> m_counter;
     95     const unsigned char m_lengthBits;
     96 };
     97 
     98 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams {
     99 public:
    100     explicit WebCryptoAesKeyGenParams(unsigned short lengthBits)
    101         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesKeyGenParams)
    102         , m_lengthBits(lengthBits)
    103     {
    104     }
    105 
    106     // FIXME: Delete once no longer referenced by chromium.
    107     unsigned short length() const { return m_lengthBits; }
    108 
    109     unsigned short lengthBits() const { return m_lengthBits; }
    110 
    111 private:
    112     const unsigned short m_lengthBits;
    113 };
    114 
    115 class WebCryptoHmacParams : public WebCryptoAlgorithmParams {
    116 public:
    117     explicit WebCryptoHmacParams(const WebCryptoAlgorithm& hash)
    118         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacParams)
    119         , m_hash(hash)
    120     {
    121         BLINK_ASSERT(!hash.isNull());
    122     }
    123 
    124     const WebCryptoAlgorithm& hash() const { return m_hash; }
    125 
    126 private:
    127     const WebCryptoAlgorithm m_hash;
    128 };
    129 
    130 class WebCryptoHmacKeyParams : public WebCryptoAlgorithmParams {
    131 public:
    132     WebCryptoHmacKeyParams(const WebCryptoAlgorithm& hash, bool hasLengthBytes, unsigned lengthBytes)
    133         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacKeyParams)
    134         , m_hash(hash)
    135         , m_hasLengthBytes(hasLengthBytes)
    136         , m_optionalLengthBytes(lengthBytes)
    137     {
    138         BLINK_ASSERT(!hash.isNull());
    139         BLINK_ASSERT(hasLengthBytes || !lengthBytes);
    140     }
    141 
    142     const WebCryptoAlgorithm& hash() const { return m_hash; }
    143 
    144     bool hasLengthBytes() const { return m_hasLengthBytes; }
    145 
    146     // FIXME: Delete once no longer referenced by chromium.
    147     bool getLength(unsigned& length) const
    148     {
    149         if (!m_hasLengthBytes)
    150             return false;
    151         length = m_optionalLengthBytes;
    152         return true;
    153     }
    154 
    155     unsigned optionalLengthBytes() const { return m_optionalLengthBytes; }
    156 
    157 private:
    158     const WebCryptoAlgorithm m_hash;
    159     const bool m_hasLengthBytes;
    160     const unsigned m_optionalLengthBytes;
    161 };
    162 
    163 class WebCryptoRsaSsaParams : public WebCryptoAlgorithmParams {
    164 public:
    165     explicit WebCryptoRsaSsaParams(const WebCryptoAlgorithm& hash)
    166         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaSsaParams)
    167         , m_hash(hash)
    168     {
    169         BLINK_ASSERT(!hash.isNull());
    170     }
    171 
    172     const WebCryptoAlgorithm& hash() const { return m_hash; }
    173 
    174 private:
    175     const WebCryptoAlgorithm m_hash;
    176 };
    177 
    178 class WebCryptoRsaKeyGenParams : public WebCryptoAlgorithmParams {
    179 public:
    180     WebCryptoRsaKeyGenParams(unsigned modulusLengthBits, const unsigned char* publicExponent, unsigned publicExponentSize)
    181         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaKeyGenParams)
    182         , m_modulusLengthBits(modulusLengthBits)
    183         , m_publicExponent(publicExponent, publicExponentSize)
    184     {
    185     }
    186 
    187     // FIXME: Delete once no longer referenced by chromium.
    188     unsigned modulusLength() const { return m_modulusLengthBits; }
    189 
    190     unsigned modulusLengthBits() const { return m_modulusLengthBits; }
    191     const WebVector<unsigned char>& publicExponent() const { return m_publicExponent; }
    192 
    193 private:
    194     const unsigned m_modulusLengthBits;
    195     const WebVector<unsigned char> m_publicExponent;
    196 };
    197 
    198 class WebCryptoAesGcmParams : public WebCryptoAlgorithmParams {
    199 public:
    200     WebCryptoAesGcmParams(const unsigned char* iv, unsigned ivSize, bool hasAdditionalData, const unsigned char* additionalData, unsigned additionalDataSize, bool hasTagLengthBits, unsigned char tagLengthBits)
    201         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesGcmParams)
    202         , m_iv(iv, ivSize)
    203         , m_hasAdditionalData(hasAdditionalData)
    204         , m_optionalAdditionalData(additionalData, additionalDataSize)
    205         , m_hasTagLengthBits(hasTagLengthBits)
    206         , m_optionalTagLengthBits(tagLengthBits)
    207     {
    208         BLINK_ASSERT(hasAdditionalData || !additionalDataSize);
    209         BLINK_ASSERT(hasTagLengthBits || !tagLengthBits);
    210     }
    211 
    212     const WebVector<unsigned char>& iv() const { return m_iv; }
    213 
    214     bool hasAdditionalData() const { return m_hasAdditionalData; }
    215     const WebVector<unsigned char>& optionalAdditionalData() const { return m_optionalAdditionalData; }
    216 
    217     bool hasTagLengthBits() const { return m_hasTagLengthBits; }
    218     unsigned optionalTagLengthBits() const { return m_optionalTagLengthBits; }
    219 
    220 private:
    221     const WebVector<unsigned char> m_iv;
    222     const bool m_hasAdditionalData;
    223     const WebVector<unsigned char> m_optionalAdditionalData;
    224     const bool m_hasTagLengthBits;
    225     const unsigned char m_optionalTagLengthBits;
    226 };
    227 
    228 class WebCryptoRsaOaepParams : public WebCryptoAlgorithmParams {
    229 public:
    230     WebCryptoRsaOaepParams(const WebCryptoAlgorithm& hash, bool hasLabel, const unsigned char* label, unsigned labelSize)
    231         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaOaepParams)
    232         , m_hash(hash)
    233         , m_hasLabel(hasLabel)
    234         , m_optionalLabel(label, labelSize)
    235     {
    236         BLINK_ASSERT(!hash.isNull());
    237         BLINK_ASSERT(hasLabel || !labelSize);
    238     }
    239 
    240     const WebCryptoAlgorithm& hash() const { return m_hash; }
    241 
    242     bool hasLabel() const { return m_hasLabel; }
    243     const WebVector<unsigned char>& optionalLabel() const { return m_optionalLabel; }
    244 
    245 private:
    246     const WebCryptoAlgorithm m_hash;
    247     const bool m_hasLabel;
    248     const WebVector<unsigned char> m_optionalLabel;
    249 };
    250 
    251 } // namespace blink
    252 
    253 #endif
    254