Line data Source code
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 47 : uint64_t GetRandU64()
41 : {
42 47 : uint64_t tmp = 0;
43 47 : VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp)));
44 47 : return tmp;
45 : }
46 :
47 136355 : uint32_t GetRandU32()
48 : {
49 136355 : uint32_t tmp = 0;
50 136355 : VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp)));
51 136355 : return tmp;
52 : }
53 :
54 729 : uint16_t GetRandU16()
55 : {
56 729 : uint16_t tmp = 0;
57 729 : VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(reinterpret_cast<uint8_t *>(&tmp), sizeof(tmp)));
58 729 : return tmp;
59 : }
60 :
61 8228 : uint8_t GetRandU8()
62 : {
63 8228 : uint8_t tmp = 0;
64 8228 : VerifyOrDie(CHIP_NO_ERROR == DRBG_get_bytes(&tmp, sizeof(tmp)));
65 8228 : return tmp;
66 : }
67 :
68 : } // namespace Crypto
69 : } // namespace chip
|