michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsRandomGenerator.h" michael@0: #include "pk11pub.h" michael@0: #include "secerr.h" michael@0: #include "prerror.h" michael@0: #include "nsNSSComponent.h" michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// nsRandomGenerator michael@0: michael@0: NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator) michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// nsIRandomGenerator michael@0: michael@0: /* void generateRandomBytes(in unsigned long aLength, michael@0: [retval, array, size_is(aLength)] out octet aBuffer) */ michael@0: NS_IMETHODIMP michael@0: nsRandomGenerator::GenerateRandomBytes(uint32_t aLength, michael@0: uint8_t **aBuffer) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aBuffer); michael@0: *aBuffer = nullptr; michael@0: michael@0: mozilla::ScopedPK11SlotInfo slot(PK11_GetInternalSlot()); michael@0: if (!slot) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: uint8_t *buf = reinterpret_cast(NS_Alloc(aLength)); michael@0: if (!buf) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: SECStatus srv = PK11_GenerateRandomOnSlot(slot, buf, aLength); michael@0: michael@0: if (SECSuccess != srv) { michael@0: NS_Free(buf); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *aBuffer = buf; michael@0: michael@0: return NS_OK; michael@0: }