Coverage Report

Created: 2025-09-02 06:46

/src/connectedhomeip/src/crypto/RandUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 *    Copyright (c) 2020 Project CHIP Authors
4
 *    Copyright (c) 2013-2017 Nest Labs, Inc.
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
/**
20
 *    @file
21
 *      This file implements utility functions for deriving random integers.
22
 *
23
 *  @note These utility functions do not generate cryptographically strong
24
 *        random number. To get cryptographically strong random data use
25
 *        chip::Crypto::DRBG_get_bytes().
26
 *
27
 */
28
29
#include "RandUtils.h"
30
31
#include <stdint.h>
32
#include <stdlib.h>
33
34
#include <crypto/CHIPCryptoPAL.h>
35
#include <lib/support/CodeUtils.h>
36
37
namespace chip {
38
namespace Crypto {
39
40
uint64_t GetRandU64()
41
3
{
42
3
    uint64_t tmp = 0;
43
3
    VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp)));
44
3
    return tmp;
45
3
}
46
47
uint32_t GetRandU32()
48
1.54k
{
49
1.54k
    uint32_t tmp = 0;
50
1.54k
    VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp)));
51
1.54k
    return tmp;
52
1.54k
}
53
54
uint16_t GetRandU16()
55
2
{
56
2
    uint16_t tmp = 0;
57
2
    VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp)));
58
2
    return tmp;
59
2
}
60
61
uint8_t GetRandU8()
62
0
{
63
0
    uint8_t tmp = 0;
64
0
    VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(&tmp, sizeof(tmp)));
65
0
    return tmp;
66
0
}
67
68
} // namespace Crypto
69
} // namespace chip