15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2011 Google Inc. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright 9521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer in the 12521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) * documentation and/or other materials provided with the distribution. 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 3. Neither the name of Google, Inc. ("Google") nor the names of 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * its contributors may be used to endorse or promote products derived 15521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) * from this software without specific prior written permission. 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h" 31521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include "modules/crypto/Crypto.h" 325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 33197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ExceptionState.h" 3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/ExceptionCode.h" 35521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include "wtf/ArrayBufferView.h" 36521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include "wtf/CryptographicallyRandomNumber.h" 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 38c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace { 415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool isIntegerArray(ArrayBufferView* array) 435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 4409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) ArrayBufferView::ViewType type = array->type(); 455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return type == ArrayBufferView::TypeInt8 46521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) || type == ArrayBufferView::TypeUint8 47521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) || type == ArrayBufferView::TypeUint8Clamped 48521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) || type == ArrayBufferView::TypeInt16 49521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) || type == ArrayBufferView::TypeUint16 50521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) || type == ArrayBufferView::TypeInt32 51521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) || type == ArrayBufferView::TypeUint32; 525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)Crypto::Crypto() 575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 6051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void Crypto::getRandomValues(ArrayBufferView* array, ExceptionState& exceptionState) 615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 62a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) if (!array) { 63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) exceptionState.throwDOMException(TypeMismatchError, "The provided ArrayBufferView is null."); 64a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return; 65a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 66a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) if (!isIntegerArray(array)) { 67a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) exceptionState.throwDOMException(TypeMismatchError, String::format("The provided ArrayBufferView is of type '%s', which is not an integer array type.", array->typeName())); 685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return; 695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (array->byteLength() > 65536) { 71a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) exceptionState.throwDOMException(QuotaExceededError, String::format("The ArrayBufferView's byte length (%u) exceeds the number of bytes of entropy available via this API (65536).", array->byteLength())); 725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return; 735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) cryptographicallyRandomValues(array->baseAddress(), array->byteLength()); 755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 77521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)SubtleCrypto* Crypto::subtle() 78521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles){ 79521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) if (!m_subtleCrypto) 80521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) m_subtleCrypto = SubtleCrypto::create(); 81521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) return m_subtleCrypto.get(); 82521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)} 83521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) 8409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void Crypto::trace(Visitor* visitor) 8509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){ 8609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) visitor->trace(m_subtleCrypto); 8709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)} 8809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 90