/src/FreeRDP/winpr/libwinpr/crypto/rand.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * |
4 | | * Copyright 2015 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
5 | | * |
6 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | * you may not use this file except in compliance with the License. |
8 | | * You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, software |
13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | * See the License for the specific language governing permissions and |
16 | | * limitations under the License. |
17 | | */ |
18 | | |
19 | | #include <winpr/config.h> |
20 | | |
21 | | #include <winpr/crt.h> |
22 | | |
23 | | #include <winpr/crypto.h> |
24 | | |
25 | | #ifdef WITH_OPENSSL |
26 | | #include <openssl/crypto.h> |
27 | | #include <openssl/rand.h> |
28 | | #endif |
29 | | |
30 | | #ifdef WITH_MBEDTLS |
31 | | #include <mbedtls/md.h> |
32 | | #include <mbedtls/entropy.h> |
33 | | #include <mbedtls/havege.h> |
34 | | #include <mbedtls/hmac_drbg.h> |
35 | | #endif |
36 | | |
37 | | int winpr_RAND(void* output, size_t len) |
38 | 0 | { |
39 | 0 | #if defined(WITH_OPENSSL) |
40 | 0 | if (len > INT_MAX) |
41 | 0 | return -1; |
42 | 0 | if (RAND_bytes(output, (int)len) != 1) |
43 | 0 | return -1; |
44 | | #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_HAVEGE_C) |
45 | | mbedtls_havege_state hs; |
46 | | mbedtls_havege_init(&hs); |
47 | | |
48 | | if (mbedtls_havege_random(&hs, output, len) != 0) |
49 | | return -1; |
50 | | |
51 | | mbedtls_havege_free(&hs); |
52 | | #endif |
53 | 0 | return 0; |
54 | 0 | } |
55 | | |
56 | | int winpr_RAND_pseudo(void* output, size_t len) |
57 | 0 | { |
58 | 0 | return winpr_RAND(output, len); |
59 | 0 | } |