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"
12 #include "mozilla/UniquePtrExtensions.h"
14 NS_IMPL_ISUPPORTS(nsRandomGenerator
, nsIRandomGenerator
)
17 nsRandomGenerator::GenerateRandomBytes(uint32_t aLength
, uint8_t** aBuffer
) {
18 NS_ENSURE_ARG_POINTER(aBuffer
);
21 mozilla::UniqueFreePtr
<uint8_t> buf(
22 static_cast<uint8_t*>(moz_xmalloc(aLength
)));
23 nsresult rv
= GenerateRandomBytesInto(buf
.get(), aLength
);
24 NS_ENSURE_SUCCESS(rv
, rv
);
26 *aBuffer
= buf
.release();
31 nsRandomGenerator::GenerateRandomBytesInto(uint8_t* aBuffer
, uint32_t aLength
) {
32 NS_ENSURE_ARG_POINTER(aBuffer
);
34 mozilla::UniquePK11SlotInfo
slot(PK11_GetInternalSlot());
36 return NS_ERROR_FAILURE
;
39 SECStatus srv
= PK11_GenerateRandomOnSlot(slot
.get(), aBuffer
, aLength
);
40 return srv
== SECSuccess
? NS_OK
: NS_ERROR_FAILURE
;