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 INSIDE_BLINK 38 #include "wtf/PassOwnPtr.h" 39 #endif 40 41 namespace blink { 42 43 enum WebCryptoAlgorithmId { 44 WebCryptoAlgorithmIdAesCbc, 45 WebCryptoAlgorithmIdHmac, 46 WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 47 WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 48 WebCryptoAlgorithmIdSha1, 49 WebCryptoAlgorithmIdSha224, 50 WebCryptoAlgorithmIdSha256, 51 WebCryptoAlgorithmIdSha384, 52 WebCryptoAlgorithmIdSha512, 53 WebCryptoAlgorithmIdAesGcm, 54 WebCryptoAlgorithmIdRsaOaep, 55 WebCryptoAlgorithmIdAesCtr, 56 WebCryptoAlgorithmIdAesKw, 57 #if INSIDE_BLINK 58 NumberOfWebCryptoAlgorithmId, 59 #endif 60 }; 61 62 enum WebCryptoAlgorithmParamsType { 63 WebCryptoAlgorithmParamsTypeNone, 64 WebCryptoAlgorithmParamsTypeAesCbcParams, 65 WebCryptoAlgorithmParamsTypeAesKeyGenParams, 66 WebCryptoAlgorithmParamsTypeHmacParams, 67 WebCryptoAlgorithmParamsTypeHmacKeyParams, 68 WebCryptoAlgorithmParamsTypeRsaSsaParams, 69 WebCryptoAlgorithmParamsTypeRsaKeyGenParams, 70 WebCryptoAlgorithmParamsTypeAesGcmParams, 71 WebCryptoAlgorithmParamsTypeRsaOaepParams, 72 WebCryptoAlgorithmParamsTypeAesCtrParams, 73 }; 74 75 class WebCryptoAesCbcParams; 76 class WebCryptoAesKeyGenParams; 77 class WebCryptoHmacParams; 78 class WebCryptoHmacKeyParams; 79 class WebCryptoRsaSsaParams; 80 class WebCryptoRsaKeyGenParams; 81 class WebCryptoAesGcmParams; 82 class WebCryptoRsaOaepParams; 83 class WebCryptoAesCtrParams; 84 85 class WebCryptoAlgorithmParams; 86 class WebCryptoAlgorithmPrivate; 87 88 // The WebCryptoAlgorithm represents a normalized algorithm and its parameters. 89 // * Immutable 90 // * Threadsafe 91 // * Copiable (cheaply) 92 // 93 // If WebCryptoAlgorithm "isNull()" then it is invalid to call any of the other 94 // methods on it (other than destruction, assignment, or isNull()). 95 class WebCryptoAlgorithm { 96 public: 97 #if INSIDE_BLINK 98 WebCryptoAlgorithm() { } 99 BLINK_PLATFORM_EXPORT WebCryptoAlgorithm(WebCryptoAlgorithmId, PassOwnPtr<WebCryptoAlgorithmParams>); 100 #endif 101 102 BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm createNull(); 103 BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm adoptParamsAndCreate(WebCryptoAlgorithmId, WebCryptoAlgorithmParams*); 104 105 ~WebCryptoAlgorithm() { reset(); } 106 107 WebCryptoAlgorithm(const WebCryptoAlgorithm& other) { assign(other); } 108 WebCryptoAlgorithm& operator=(const WebCryptoAlgorithm& other) 109 { 110 assign(other); 111 return *this; 112 } 113 114 BLINK_PLATFORM_EXPORT bool isNull() const; 115 116 BLINK_PLATFORM_EXPORT WebCryptoAlgorithmId id() const; 117 118 BLINK_PLATFORM_EXPORT WebCryptoAlgorithmParamsType paramsType() const; 119 120 // Retrieves the type-specific parameters. The algorithm contains at most 1 121 // type of parameters. Retrieving an invalid parameter will return 0. 122 BLINK_PLATFORM_EXPORT const WebCryptoAesCbcParams* aesCbcParams() const; 123 BLINK_PLATFORM_EXPORT const WebCryptoAesKeyGenParams* aesKeyGenParams() const; 124 BLINK_PLATFORM_EXPORT const WebCryptoHmacParams* hmacParams() const; 125 BLINK_PLATFORM_EXPORT const WebCryptoHmacKeyParams* hmacKeyParams() const; 126 BLINK_PLATFORM_EXPORT const WebCryptoRsaSsaParams* rsaSsaParams() const; 127 BLINK_PLATFORM_EXPORT const WebCryptoRsaKeyGenParams* rsaKeyGenParams() const; 128 BLINK_PLATFORM_EXPORT const WebCryptoAesGcmParams* aesGcmParams() const; 129 BLINK_PLATFORM_EXPORT const WebCryptoRsaOaepParams* rsaOaepParams() const; 130 BLINK_PLATFORM_EXPORT const WebCryptoAesCtrParams* aesCtrParams() const; 131 132 private: 133 BLINK_PLATFORM_EXPORT void assign(const WebCryptoAlgorithm& other); 134 BLINK_PLATFORM_EXPORT void reset(); 135 136 WebPrivatePtr<WebCryptoAlgorithmPrivate> m_private; 137 }; 138 139 } // namespace blink 140 141 #endif 142