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 WebCryptoOperation { 44 WebCryptoOperationEncrypt, 45 WebCryptoOperationDecrypt, 46 WebCryptoOperationSign, 47 WebCryptoOperationVerify, 48 WebCryptoOperationDigest, 49 WebCryptoOperationGenerateKey, 50 WebCryptoOperationImportKey, 51 WebCryptoOperationDeriveKey, 52 WebCryptoOperationDeriveBits, 53 WebCryptoOperationWrapKey, 54 WebCryptoOperationUnwrapKey, 55 WebCryptoOperationLast = WebCryptoOperationUnwrapKey, 56 }; 57 58 enum WebCryptoAlgorithmId { 59 WebCryptoAlgorithmIdAesCbc, 60 WebCryptoAlgorithmIdHmac, 61 WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 62 WebCryptoAlgorithmIdSha1, 63 WebCryptoAlgorithmIdSha256, 64 WebCryptoAlgorithmIdSha384, 65 WebCryptoAlgorithmIdSha512, 66 WebCryptoAlgorithmIdAesGcm, 67 WebCryptoAlgorithmIdRsaOaep, 68 WebCryptoAlgorithmIdAesCtr, 69 WebCryptoAlgorithmIdAesKw, 70 #if INSIDE_BLINK 71 WebCryptoAlgorithmIdLast = WebCryptoAlgorithmIdAesKw, 72 #endif 73 }; 74 75 enum WebCryptoAlgorithmParamsType { 76 WebCryptoAlgorithmParamsTypeNone, 77 WebCryptoAlgorithmParamsTypeAesCbcParams, 78 WebCryptoAlgorithmParamsTypeAesKeyGenParams, 79 WebCryptoAlgorithmParamsTypeHmacImportParams, 80 WebCryptoAlgorithmParamsTypeHmacKeyGenParams, 81 WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, 82 WebCryptoAlgorithmParamsTypeRsaHashedImportParams, 83 WebCryptoAlgorithmParamsTypeAesGcmParams, 84 WebCryptoAlgorithmParamsTypeRsaOaepParams, 85 WebCryptoAlgorithmParamsTypeAesCtrParams, 86 }; 87 88 struct WebCryptoAlgorithmInfo { 89 typedef char ParamsTypeOrUndefined; 90 static const ParamsTypeOrUndefined Undefined = -1; 91 92 // The canonical (case-sensitive) name for the algorithm as a 93 // null-terminated C-string literal. 94 const char* name; 95 96 // A map from the operation to the expected parameter type of the algorithm. 97 // If an operation is not applicable for the algorithm, set to Undefined. 98 const ParamsTypeOrUndefined operationToParamsType[WebCryptoOperationLast + 1]; 99 }; 100 101 102 class WebCryptoAesCbcParams; 103 class WebCryptoAesKeyGenParams; 104 class WebCryptoHmacImportParams; 105 class WebCryptoHmacKeyGenParams; 106 class WebCryptoAesGcmParams; 107 class WebCryptoRsaOaepParams; 108 class WebCryptoAesCtrParams; 109 class WebCryptoRsaHashedKeyGenParams; 110 class WebCryptoRsaHashedImportParams; 111 112 class WebCryptoAlgorithmParams; 113 class WebCryptoAlgorithmPrivate; 114 115 // The WebCryptoAlgorithm represents a normalized algorithm and its parameters. 116 // * Immutable 117 // * Threadsafe 118 // * Copiable (cheaply) 119 // 120 // If WebCryptoAlgorithm "isNull()" then it is invalid to call any of the other 121 // methods on it (other than destruction, assignment, or isNull()). 122 class WebCryptoAlgorithm { 123 public: 124 #if INSIDE_BLINK 125 WebCryptoAlgorithm() { } 126 BLINK_PLATFORM_EXPORT WebCryptoAlgorithm(WebCryptoAlgorithmId, PassOwnPtr<WebCryptoAlgorithmParams>); 127 #endif 128 129 BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm createNull(); 130 BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm adoptParamsAndCreate(WebCryptoAlgorithmId, WebCryptoAlgorithmParams*); 131 132 // Returns a WebCryptoAlgorithmInfo for the algorithm with the given ID. If 133 // the ID is invalid, return 0. The caller can assume the pointer will be 134 // valid for the program's entire runtime. 135 BLINK_PLATFORM_EXPORT static const WebCryptoAlgorithmInfo* lookupAlgorithmInfo(WebCryptoAlgorithmId); 136 137 ~WebCryptoAlgorithm() { reset(); } 138 139 WebCryptoAlgorithm(const WebCryptoAlgorithm& other) { assign(other); } 140 WebCryptoAlgorithm& operator=(const WebCryptoAlgorithm& other) 141 { 142 assign(other); 143 return *this; 144 } 145 146 BLINK_PLATFORM_EXPORT bool isNull() const; 147 148 BLINK_PLATFORM_EXPORT WebCryptoAlgorithmId id() const; 149 150 BLINK_PLATFORM_EXPORT WebCryptoAlgorithmParamsType paramsType() const; 151 152 // Retrieves the type-specific parameters. The algorithm contains at most 1 153 // type of parameters. Retrieving an invalid parameter will return 0. 154 BLINK_PLATFORM_EXPORT const WebCryptoAesCbcParams* aesCbcParams() const; 155 BLINK_PLATFORM_EXPORT const WebCryptoAesKeyGenParams* aesKeyGenParams() const; 156 BLINK_PLATFORM_EXPORT const WebCryptoHmacImportParams* hmacImportParams() const; 157 BLINK_PLATFORM_EXPORT const WebCryptoHmacKeyGenParams* hmacKeyGenParams() const; 158 BLINK_PLATFORM_EXPORT const WebCryptoAesGcmParams* aesGcmParams() const; 159 BLINK_PLATFORM_EXPORT const WebCryptoRsaOaepParams* rsaOaepParams() const; 160 BLINK_PLATFORM_EXPORT const WebCryptoAesCtrParams* aesCtrParams() const; 161 BLINK_PLATFORM_EXPORT const WebCryptoRsaHashedImportParams* rsaHashedImportParams() const; 162 BLINK_PLATFORM_EXPORT const WebCryptoRsaHashedKeyGenParams* rsaHashedKeyGenParams() const; 163 164 // Returns true if the provided algorithm ID is for a hash (in other words, SHA-*) 165 BLINK_PLATFORM_EXPORT static bool isHash(WebCryptoAlgorithmId); 166 167 private: 168 BLINK_PLATFORM_EXPORT void assign(const WebCryptoAlgorithm& other); 169 BLINK_PLATFORM_EXPORT void reset(); 170 171 WebPrivatePtr<WebCryptoAlgorithmPrivate> m_private; 172 }; 173 174 } // namespace blink 175 176 #endif 177