1 /*
2  * Copyright (C) 2011 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Google, Inc. ("Google") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "third_party/blink/renderer/modules/crypto/crypto.h"
30 
31 #include "crypto/random.h"
32 #include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer_view.h"
33 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
34 
35 namespace blink {
36 
37 namespace {
38 
IsIntegerArray(DOMArrayBufferView * array)39 bool IsIntegerArray(DOMArrayBufferView* array) {
40   DOMArrayBufferView::ViewType type = array->GetType();
41   return type == DOMArrayBufferView::kTypeInt8 ||
42          type == DOMArrayBufferView::kTypeUint8 ||
43          type == DOMArrayBufferView::kTypeUint8Clamped ||
44          type == DOMArrayBufferView::kTypeInt16 ||
45          type == DOMArrayBufferView::kTypeUint16 ||
46          type == DOMArrayBufferView::kTypeInt32 ||
47          type == DOMArrayBufferView::kTypeUint32;
48 }
49 
50 }  // namespace
51 
getRandomValues(NotShared<DOMArrayBufferView> array,ExceptionState & exception_state)52 NotShared<DOMArrayBufferView> Crypto::getRandomValues(
53     NotShared<DOMArrayBufferView> array,
54     ExceptionState& exception_state) {
55   DCHECK(array);
56   if (!IsIntegerArray(array.View())) {
57     exception_state.ThrowDOMException(
58         DOMExceptionCode::kTypeMismatchError,
59         String::Format("The provided ArrayBufferView is of type '%s', which is "
60                        "not an integer array type.",
61                        array.View()->TypeName()));
62     return NotShared<DOMArrayBufferView>(nullptr);
63   }
64   if (array.View()->byteLength() > 65536) {
65     exception_state.ThrowDOMException(
66         DOMExceptionCode::kQuotaExceededError,
67         String::Format("The ArrayBufferView's byte length (%zu) exceeds the "
68                        "number of bytes of entropy available via this API "
69                        "(65536).",
70                        array.View()->byteLength()));
71     return NotShared<DOMArrayBufferView>(nullptr);
72   }
73   crypto::RandBytes(array.View()->BaseAddress(), array.View()->byteLength());
74   return array;
75 }
76 
subtle()77 SubtleCrypto* Crypto::subtle() {
78   if (!subtle_crypto_)
79     subtle_crypto_ = MakeGarbageCollected<SubtleCrypto>();
80   return subtle_crypto_.Get();
81 }
82 
Trace(Visitor * visitor) const83 void Crypto::Trace(Visitor* visitor) const {
84   visitor->Trace(subtle_crypto_);
85   ScriptWrappable::Trace(visitor);
86 }
87 
88 }  // namespace blink
89