Coverage Report

Created: 2026-10-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/getrandom_crypto.c
Line
Count
Source
1
/*
2
 * This file is part of the SSH Library
3
 *
4
 * Copyright (c) 2009 by Aris Adamantiadis
5
 *
6
 * The SSH Library is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * The SSH Library is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with the SSH Library; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19
 * MA 02111-1307, USA.
20
 */
21
22
#include "config.h"
23
24
#include "libssh/crypto.h"
25
#include <openssl/rand.h>
26
27
/**
28
 * @addtogroup libssh_misc
29
 *
30
 * @{
31
 */
32
33
/**
34
 * @brief Get random bytes
35
 *
36
 * Make sure to always check the return code of this function!
37
 *
38
 * @param[in]  where    The buffer to fill with random bytes
39
 *
40
 * @param[in]  len      The size of the buffer to fill.
41
 *
42
 * @param[in]  strong   Use a strong or private RNG source.
43
 *
44
 * @return 1 on success, 0 on error.
45
 */
46
int
47
ssh_get_random(void *where, int len, int strong)
48
4.42k
{
49
#ifdef HAVE_OPENSSL_RAND_PRIV_BYTES
50
    if (strong) {
51
        /* Returns -1 when not supported, 0 on error, 1 on success */
52
        return !!RAND_priv_bytes(where, len);
53
    }
54
#else
55
4.42k
    (void)strong;
56
4.42k
#endif /* HAVE_RAND_PRIV_BYTES */
57
58
    /* Returns -1 when not supported, 0 on error, 1 on success */
59
4.42k
    return !!RAND_bytes(where, len);
60
4.42k
}
61
62
/**
63
 * @}
64
 */