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 WebKit {
     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 //       The parameters in the spec have the same name, minus the "WebCrypto" prefix.
     46 
     47 class WebCryptoAlgorithmParams {
     48 public:
     49     explicit WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsType type)
     50         : m_type(type)
     51     {
     52     }
     53 
     54     virtual ~WebCryptoAlgorithmParams() { }
     55 
     56     WebCryptoAlgorithmParamsType type() const { return m_type; }
     57 
     58 private:
     59     WebCryptoAlgorithmParamsType m_type;
     60 };
     61 
     62 class WebCryptoAesCbcParams : public WebCryptoAlgorithmParams {
     63 public:
     64     WebCryptoAesCbcParams(unsigned char* iv, size_t ivSize)
     65         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesCbcParams)
     66         , m_iv(iv, ivSize)
     67     {
     68     }
     69 
     70     const WebVector<unsigned char>& iv() const { return m_iv; }
     71 
     72 private:
     73     const WebVector<unsigned char> m_iv;
     74 };
     75 
     76 class WebCryptoAesKeyGenParams : public WebCryptoAlgorithmParams {
     77 public:
     78     explicit WebCryptoAesKeyGenParams(unsigned short length)
     79         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeAesKeyGenParams)
     80         , m_length(length)
     81     {
     82     }
     83 
     84     unsigned short length() const { return m_length; }
     85 
     86 private:
     87     const unsigned short m_length;
     88 };
     89 
     90 class WebCryptoHmacParams : public WebCryptoAlgorithmParams {
     91 public:
     92     explicit WebCryptoHmacParams(const WebCryptoAlgorithm& hash)
     93         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeHmacParams)
     94         , m_hash(hash)
     95     {
     96     }
     97 
     98     const WebCryptoAlgorithm& hash() const { return m_hash; }
     99 
    100 private:
    101     WebCryptoAlgorithm m_hash;
    102 };
    103 
    104 class WebCryptoRsaSsaParams : public WebCryptoAlgorithmParams {
    105 public:
    106     explicit WebCryptoRsaSsaParams(const WebCryptoAlgorithm& hash)
    107         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaSsaParams)
    108         , m_hash(hash)
    109     {
    110     }
    111 
    112     const WebCryptoAlgorithm& hash() const { return m_hash; }
    113 
    114 private:
    115     WebCryptoAlgorithm m_hash;
    116 };
    117 
    118 class WebCryptoRsaKeyGenParams : public WebCryptoAlgorithmParams {
    119 public:
    120     WebCryptoRsaKeyGenParams(unsigned modulusLength, const unsigned char* publicExponent, size_t publicExponentSize)
    121         : WebCryptoAlgorithmParams(WebCryptoAlgorithmParamsTypeRsaKeyGenParams)
    122         , m_modulusLength(modulusLength)
    123         , m_publicExponent(publicExponent, publicExponentSize)
    124     {
    125     }
    126 
    127     unsigned modulusLength() const { return m_modulusLength; }
    128     const WebVector<unsigned char>& publicExponent() const { return m_publicExponent; }
    129 
    130 private:
    131     const unsigned m_modulusLength;
    132     const WebVector<unsigned char> m_publicExponent;
    133 };
    134 
    135 } // namespace WebKit
    136 
    137 #endif
    138