Coverage Report

Created: 2026-04-08 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/libfido2/src/random.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2018-2024 Yubico AB. All rights reserved.
3
 * Use of this source code is governed by a BSD-style
4
 * license that can be found in the LICENSE file.
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <sys/types.h>
9
#include <sys/stat.h>
10
#ifdef HAVE_SYS_RANDOM_H
11
#include <sys/random.h>
12
#endif
13
14
#include <fcntl.h>
15
#ifdef HAVE_UNISTD_H
16
#include <unistd.h>
17
#endif
18
19
#if defined(__has_feature)
20
# if  __has_feature(memory_sanitizer)
21
#  include <sanitizer/msan_interface.h>
22
#  define WITH_MSAN     1
23
# endif
24
#endif
25
26
#include "fido.h"
27
28
#ifdef FIDO_FUZZ
29
/* XXX: force consistent harnesses */
30
#undef HAVE_ARC4RANDOM_BUF
31
#undef HAVE_DEV_URANDOM
32
#endif
33
34
#if defined(_WIN32)
35
#include <windows.h>
36
37
#include <winternl.h>
38
#include <winerror.h>
39
#include <stdio.h>
40
#include <bcrypt.h>
41
#include <sal.h>
42
43
int
44
fido_get_random(void *buf, size_t len)
45
{
46
        NTSTATUS status;
47
48
        status = BCryptGenRandom(NULL, buf, (ULONG)len,
49
            BCRYPT_USE_SYSTEM_PREFERRED_RNG);
50
51
        if (!NT_SUCCESS(status))
52
                return (-1);
53
54
        return (0);
55
}
56
#elif defined(HAVE_ARC4RANDOM_BUF)
57
int
58
fido_get_random(void *buf, size_t len)
59
{
60
        arc4random_buf(buf, len);
61
#ifdef WITH_MSAN
62
        __msan_unpoison(buf, len); /* XXX */
63
#endif
64
        return (0);
65
}
66
#elif defined(HAVE_GETRANDOM)
67
int
68
fido_get_random(void *buf, size_t len)
69
2.99M
{
70
2.99M
        ssize_t r;
71
72
2.99M
        if ((r = getrandom(buf, len, 0)) < 0 || (size_t)r != len)
73
7.10k
                return (-1);
74
75
2.98M
        return (0);
76
2.99M
}
77
#elif defined(HAVE_DEV_URANDOM)
78
int
79
fido_get_random(void *buf, size_t len)
80
{
81
        int     fd = -1;
82
        int     ok = -1;
83
        ssize_t r;
84
85
        if ((fd = open(FIDO_RANDOM_DEV, O_RDONLY)) < 0)
86
                goto fail;
87
        if ((r = read(fd, buf, len)) < 0 || (size_t)r != len)
88
                goto fail;
89
90
        ok = 0;
91
fail:
92
        if (fd != -1)
93
                close(fd);
94
95
        return (ok);
96
}
97
#else
98
#error "please provide an implementation of fido_get_random() for your platform"
99
#endif /* _WIN32 */