Debian Codegraph @

src/crypto/crypto.c

raw symbols SCIP
1 /* librist. Copyright © 2019 SipRadius LLC. All right reserved.
2 * Author: Kuldeep Singh Dhaka <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "crypto-private.h"
9 #include "proto/rist_time.h"
10 #include "udp-private.h"
11 #include "sha256.h"
12 #include "crypto/random.h"
13 #include <string.h>
14 #include <stdlib.h>
15 /*
16 // This is intended for verifying that the peer has the same passphrase
17 // Usecase: "reply attack protection"
18 uint64_t rist_siphash(uint64_t birthtime, uint32_t seq, const char *phrase)
19 {
20 uint8_t tmp[SHA256_BLOCK_SIZE];
21 SHA256_CTX ctx;
22 uint64_t out;
23
24 if (!birthtime) {
25 // This is an expected scenario and
26 // happens until the peer receives the first ping/pong
27 return 0;
28 }
29
30 _librist_SHA256_Init(&ctx);
31 _librist_SHA256_Update(&ctx, (void *) &birthtime, sizeof(birthtime));
32 _librist_SHA256_Update(&ctx, (void *) &seq, sizeof(seq));
33
34 if ((phrase != NULL) && strlen(phrase)) {
35 _librist_SHA256_Update(&ctx, (const void *) phrase, strlen(phrase));
36 }
37
38 _librist_SHA256_Final(&ctx, tmp);
39
40 memcpy(&out, tmp, sizeof(out));
41
42 return out;
43 }
44 */
45
46 /* Non-security caller of the CSPRNG with a wall-clock fallback on failure.
47 * Used by SSRC / flow-id / peer-id generation. Security-critical sites must
48 * use _librist_crypto_random_u32 instead. */
49 uint32_t prand_u32(void) {
50 uint32_t u32 = 0;
51 if (_librist_crypto_ramdom_get_bytes((uint8_t *)&u32, sizeof(u32)) == 0)
52 return u32;
53 uint32_t fallback = (uint32_t)timestampNTP_u64();
54 return fallback ? fallback : 0xa5a5a5a5u;
55 }
56
57 int _librist_crypto_random_u32(uint32_t *out) {
58 uint32_t u32 = 0;
59 int ret = _librist_crypto_ramdom_get_bytes((uint8_t *)&u32, sizeof(u32));
60 if (ret != 0)
61 return ret;
62 *out = u32;
63 return 0;
64 }