Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
rand.c
Go to the documentation of this file.
1/*
2 * Copyright 2015-2018 Yubico AB
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rand.h"
18
19#ifdef _WIN32_BCRYPT
20#include <windows.h>
21#include <bcrypt.h>
22#include <ntstatus.h>
23#else
24#include <openssl/rand.h>
25#endif
26
27bool rand_generate(uint8_t *buf, size_t cb_buf) {
28
29#ifdef _WIN32_BCRYPT
30 NTSTATUS status = STATUS_SUCCESS;
31
32 BCRYPT_ALG_HANDLE hAlg = 0;
33
34 if (!BCRYPT_SUCCESS(
35 status =
36 BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, NULL, 0))) {
37 return false;
38 }
39
40 status = BCryptGenRandom(hAlg, buf, (ULONG) cb_buf, 0);
41 BCryptCloseAlgorithmProvider(hAlg, 0);
42
43 return BCRYPT_SUCCESS(status);
44
45#else
46 return (1 == RAND_bytes(buf, cb_buf));
47
48#endif
49}
bool rand_generate(uint8_t *buf, size_t cb_buf)
Definition rand.c:27
unsigned char uint8_t
Definition stdint.h:124
uint8_t buf[2048]