Bug 1580312 - Refactor ResponsiveUI into its own module. r=mtigley
[gecko.git] / security / manager / ssl / nsRandomGenerator.cpp
blobaee99e25362498856dab1c70a26895f7ccd89e3f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsRandomGenerator.h"
7 #include "ScopedNSSTypes.h"
8 #include "nsNSSComponent.h"
9 #include "pk11pub.h"
10 #include "prerror.h"
11 #include "secerr.h"
13 NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator)
15 NS_IMETHODIMP
16 nsRandomGenerator::GenerateRandomBytes(uint32_t aLength, uint8_t** aBuffer) {
17 NS_ENSURE_ARG_POINTER(aBuffer);
18 *aBuffer = nullptr;
20 mozilla::UniquePK11SlotInfo slot(PK11_GetInternalSlot());
21 if (!slot) {
22 return NS_ERROR_FAILURE;
25 auto buf = static_cast<uint8_t*>(moz_xmalloc(aLength));
27 SECStatus srv = PK11_GenerateRandomOnSlot(slot.get(), buf, aLength);
28 if (srv != SECSuccess) {
29 free(buf);
30 return NS_ERROR_FAILURE;
33 *aBuffer = buf;
35 return NS_OK;