1591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch/* 2591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * Copyright (C) 2013 Google Inc. All rights reserved. 3591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * 4591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * Redistribution and use in source and binary forms, with or without 5591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * modification, are permitted provided that the following conditions are 6591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * met: 7591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * 8591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * * Redistributions of source code must retain the above copyright 9591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * notice, this list of conditions and the following disclaimer. 10591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * * Redistributions in binary form must reproduce the above 11591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * copyright notice, this list of conditions and the following disclaimer 12591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * in the documentation and/or other materials provided with the 13591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * distribution. 14591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * * Neither the name of Google Inc. nor the names of its 15591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * contributors may be used to endorse or promote products derived from 16591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * this software without specific prior written permission. 17591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * 18591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch */ 30591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 31591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#ifndef WebCryptoAlgorithm_h 32591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#define WebCryptoAlgorithm_h 33591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 34591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "WebCommon.h" 35591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "WebPrivatePtr.h" 36591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 37f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#if INSIDE_BLINK 38591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/PassOwnPtr.h" 39591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#endif 40591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 4151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)namespace blink { 42591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 43f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)enum WebCryptoOperation { 44f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationEncrypt, 45f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationDecrypt, 46f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationSign, 47f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationVerify, 48f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationDigest, 49f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationGenerateKey, 50f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationImportKey, 51f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationDeriveKey, 52f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationDeriveBits, 53f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationWrapKey, 54f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationUnwrapKey, 55f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) WebCryptoOperationLast = WebCryptoOperationUnwrapKey, 56f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)}; 57f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) 58591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochenum WebCryptoAlgorithmId { 59591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmIdAesCbc, 6002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch WebCryptoAlgorithmIdHmac, 61f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 62591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmIdSha1, 63591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmIdSha256, 64591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmIdSha384, 65591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmIdSha512, 661e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) WebCryptoAlgorithmIdAesGcm, 671e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) WebCryptoAlgorithmIdRsaOaep, 6851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) WebCryptoAlgorithmIdAesCtr, 6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) WebCryptoAlgorithmIdAesKw, 70f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#if INSIDE_BLINK 7107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch WebCryptoAlgorithmIdLast = WebCryptoAlgorithmIdAesKw, 72591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#endif 73591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch}; 74591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 75591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochenum WebCryptoAlgorithmParamsType { 76591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmParamsTypeNone, 77591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmParamsTypeAesCbcParams, 78591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithmParamsTypeAesKeyGenParams, 79d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) WebCryptoAlgorithmParamsTypeHmacImportParams, 80d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) WebCryptoAlgorithmParamsTypeHmacKeyGenParams, 81d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams, 82d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) WebCryptoAlgorithmParamsTypeRsaHashedImportParams, 831e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) WebCryptoAlgorithmParamsTypeAesGcmParams, 841e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) WebCryptoAlgorithmParamsTypeRsaOaepParams, 8551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) WebCryptoAlgorithmParamsTypeAesCtrParams, 86591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch}; 87591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 885d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)struct WebCryptoAlgorithmInfo { 895d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) typedef char ParamsTypeOrUndefined; 905d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) static const ParamsTypeOrUndefined Undefined = -1; 915d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) 925d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) // The canonical (case-sensitive) name for the algorithm as a 935d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) // null-terminated C-string literal. 945d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) const char* name; 955d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) 965d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) // A map from the operation to the expected parameter type of the algorithm. 975d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) // If an operation is not applicable for the algorithm, set to Undefined. 985d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) const ParamsTypeOrUndefined operationToParamsType[WebCryptoOperationLast + 1]; 995d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)}; 1005d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) 1015d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) 102591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochclass WebCryptoAesCbcParams; 103591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochclass WebCryptoAesKeyGenParams; 104d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class WebCryptoHmacImportParams; 105d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class WebCryptoHmacKeyGenParams; 1061e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)class WebCryptoAesGcmParams; 1071e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)class WebCryptoRsaOaepParams; 10851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)class WebCryptoAesCtrParams; 109d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class WebCryptoRsaHashedKeyGenParams; 110d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class WebCryptoRsaHashedImportParams; 111591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 112591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochclass WebCryptoAlgorithmParams; 113591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochclass WebCryptoAlgorithmPrivate; 114591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 115591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch// The WebCryptoAlgorithm represents a normalized algorithm and its parameters. 116591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch// * Immutable 117591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch// * Threadsafe 118591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch// * Copiable (cheaply) 119f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)// 120f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)// If WebCryptoAlgorithm "isNull()" then it is invalid to call any of the other 121f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)// methods on it (other than destruction, assignment, or isNull()). 122591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochclass WebCryptoAlgorithm { 123591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochpublic: 124f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#if INSIDE_BLINK 125591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithm() { } 126f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT WebCryptoAlgorithm(WebCryptoAlgorithmId, PassOwnPtr<WebCryptoAlgorithmParams>); 127591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#endif 128591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 129f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm createNull(); 130f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT static WebCryptoAlgorithm adoptParamsAndCreate(WebCryptoAlgorithmId, WebCryptoAlgorithmParams*); 131f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 1325d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) // Returns a WebCryptoAlgorithmInfo for the algorithm with the given ID. If 1335d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) // the ID is invalid, return 0. The caller can assume the pointer will be 1345d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) // valid for the program's entire runtime. 1355d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) BLINK_PLATFORM_EXPORT static const WebCryptoAlgorithmInfo* lookupAlgorithmInfo(WebCryptoAlgorithmId); 1365d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) 137591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch ~WebCryptoAlgorithm() { reset(); } 138591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 139591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithm(const WebCryptoAlgorithm& other) { assign(other); } 140591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebCryptoAlgorithm& operator=(const WebCryptoAlgorithm& other) 141591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch { 142591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch assign(other); 143591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch return *this; 144591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch } 145591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 146f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT bool isNull() const; 147591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 148f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT WebCryptoAlgorithmId id() const; 149f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) 150f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT WebCryptoAlgorithmParamsType paramsType() const; 151591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 152591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch // Retrieves the type-specific parameters. The algorithm contains at most 1 153591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch // type of parameters. Retrieving an invalid parameter will return 0. 154f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoAesCbcParams* aesCbcParams() const; 155f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoAesKeyGenParams* aesKeyGenParams() const; 156d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoHmacImportParams* hmacImportParams() const; 157d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoHmacKeyGenParams* hmacKeyGenParams() const; 158f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoAesGcmParams* aesGcmParams() const; 159f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoRsaOaepParams* rsaOaepParams() const; 16051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoAesCtrParams* aesCtrParams() const; 161d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoRsaHashedImportParams* rsaHashedImportParams() const; 162d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) BLINK_PLATFORM_EXPORT const WebCryptoRsaHashedKeyGenParams* rsaHashedKeyGenParams() const; 163591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 16407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch // Returns true if the provided algorithm ID is for a hash (in other words, SHA-*) 16507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch BLINK_PLATFORM_EXPORT static bool isHash(WebCryptoAlgorithmId); 16607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch 167591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdochprivate: 168f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT void assign(const WebCryptoAlgorithm& other); 169f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) BLINK_PLATFORM_EXPORT void reset(); 170591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 171591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch WebPrivatePtr<WebCryptoAlgorithmPrivate> m_private; 172591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch}; 173591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 17451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)} // namespace blink 175591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 176591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#endif 177