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 WebCryptoAlgorithm_h 32 #define WebCryptoAlgorithm_h 33 34 #include "WebCommon.h" 35 #include "WebPrivatePtr.h" 36 37 #if WEBKIT_IMPLEMENTATION 38 #include "wtf/PassOwnPtr.h" 39 #endif 40 41 namespace WebKit { 42 43 enum WebCryptoAlgorithmId { 44 WebCryptoAlgorithmIdAesCbc, 45 WebCryptoAlgorithmIdHmac, 46 WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 47 WebCryptoAlgorithmIdSha1, 48 WebCryptoAlgorithmIdSha224, 49 WebCryptoAlgorithmIdSha256, 50 WebCryptoAlgorithmIdSha384, 51 WebCryptoAlgorithmIdSha512, 52 #if WEBKIT_IMPLEMENTATION 53 NumberOfWebCryptoAlgorithmId, 54 #endif 55 }; 56 57 enum WebCryptoAlgorithmParamsType { 58 WebCryptoAlgorithmParamsTypeNone, 59 WebCryptoAlgorithmParamsTypeAesCbcParams, 60 WebCryptoAlgorithmParamsTypeAesKeyGenParams, 61 WebCryptoAlgorithmParamsTypeHmacParams, 62 WebCryptoAlgorithmParamsTypeRsaSsaParams, 63 WebCryptoAlgorithmParamsTypeRsaKeyGenParams, 64 }; 65 66 class WebCryptoAesCbcParams; 67 class WebCryptoAesKeyGenParams; 68 class WebCryptoHmacParams; 69 class WebCryptoRsaSsaParams; 70 class WebCryptoRsaKeyGenParams; 71 72 class WebCryptoAlgorithmParams; 73 class WebCryptoAlgorithmPrivate; 74 75 // The WebCryptoAlgorithm represents a normalized algorithm and its parameters. 76 // * Immutable 77 // * Threadsafe 78 // * Copiable (cheaply) 79 class WebCryptoAlgorithm { 80 public: 81 #if WEBKIT_IMPLEMENTATION 82 WebCryptoAlgorithm() { } 83 WebCryptoAlgorithm(WebCryptoAlgorithmId, const char* name, PassOwnPtr<WebCryptoAlgorithmParams>); 84 #endif 85 86 WEBKIT_EXPORT static WebCryptoAlgorithm adoptParamsAndCreate(WebCryptoAlgorithmId, const char* name, WebCryptoAlgorithmParams*); 87 88 ~WebCryptoAlgorithm() { reset(); } 89 90 WebCryptoAlgorithm(const WebCryptoAlgorithm& other) { assign(other); } 91 WebCryptoAlgorithm& operator=(const WebCryptoAlgorithm& other) 92 { 93 assign(other); 94 return *this; 95 } 96 97 WEBKIT_EXPORT WebCryptoAlgorithmId id() const; 98 WEBKIT_EXPORT const char* name() const; 99 100 WEBKIT_EXPORT WebCryptoAlgorithmParamsType paramsType() const; 101 102 // Retrieves the type-specific parameters. The algorithm contains at most 1 103 // type of parameters. Retrieving an invalid parameter will return 0. 104 WEBKIT_EXPORT WebCryptoAesCbcParams* aesCbcParams() const; 105 WEBKIT_EXPORT WebCryptoAesKeyGenParams* aesKeyGenParams() const; 106 WEBKIT_EXPORT WebCryptoHmacParams* hmacParams() const; 107 WEBKIT_EXPORT WebCryptoRsaSsaParams* rsaSsaParams() const; 108 WEBKIT_EXPORT WebCryptoRsaKeyGenParams* rsaKeyGenParams() const; 109 110 private: 111 WEBKIT_EXPORT void assign(const WebCryptoAlgorithm& other); 112 WEBKIT_EXPORT void reset(); 113 114 WebPrivatePtr<WebCryptoAlgorithmPrivate> m_private; 115 }; 116 117 } // namespace WebKit 118 119 #endif 120