/src/openssl/crypto/rand/rand_hash_salt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <string.h> |
11 | | #include <openssl/err.h> |
12 | | #include "crypto/rand.h" |
13 | | #include "crypto/siphash.h" |
14 | | #include "internal/cryptlib.h" |
15 | | #include "internal/time.h" |
16 | | #include "internal/tsan_assist.h" |
17 | | |
18 | | /* |
19 | | * Last resort: condense the current time, the time stamp counter (where the |
20 | | * CPU has one), the process (fork) and thread ids, a call counter and a |
21 | | * couple of addresses (ASLR) into |num| bytes with SipHash-2-4. Within a |
22 | | * process the counter makes the result differ on every call, and the process |
23 | | * id and time keep forked children apart. |
24 | | * |
25 | | * This is not entropy in the cryptographic sense. With a time stamp counter |
26 | | * or address space randomisation it is not practically guessable by a |
27 | | * remote peer, which is all a hash-flooding defense needs. On a platform |
28 | | * with neither, it only rests on the clock, the process and thread ids and |
29 | | * the call count, which a peer may be able to narrow down; nothing better is |
30 | | * left once RAND and the operating system sources have failed. |
31 | | */ |
32 | | static int hash_salt_fallback(unsigned char *buf, size_t num) |
33 | 0 | { |
34 | 0 | static TSAN_QUALIFIER unsigned int counter; |
35 | 0 | static const unsigned char zero_key[SIPHASH_KEY_SIZE] = { 0 }; |
36 | 0 | struct { |
37 | 0 | OSSL_TIME now; |
38 | 0 | uint32_t tsc; |
39 | 0 | unsigned int counter; |
40 | 0 | int pid; |
41 | 0 | CRYPTO_THREAD_ID tid; |
42 | 0 | const void *stack; |
43 | 0 | const void *data; |
44 | 0 | } salt; |
45 | 0 | unsigned char block[SIPHASH_MAX_DIGEST_SIZE]; |
46 | 0 | size_t off, n; |
47 | 0 | uint64_t i; |
48 | 0 | SIPHASH sip; |
49 | 0 | int ret = 0; |
50 | |
|
51 | 0 | memset(&salt, 0, sizeof(salt)); |
52 | 0 | salt.now = ossl_time_now(); |
53 | 0 | salt.tsc = OPENSSL_rdtsc(); |
54 | 0 | salt.counter = tsan_counter(&counter); |
55 | 0 | salt.pid = openssl_get_fork_id(); |
56 | 0 | salt.tid = CRYPTO_THREAD_get_current_id(); |
57 | 0 | salt.stack = &salt; |
58 | 0 | salt.data = &zero_key; |
59 | |
|
60 | 0 | for (i = 0, off = 0; off < num; i++, off += n) { |
61 | 0 | memset(&sip, 0, sizeof(sip)); |
62 | 0 | if (!SipHash_set_hash_size(&sip, SIPHASH_MAX_DIGEST_SIZE) |
63 | 0 | || !SipHash_Init(&sip, zero_key, 0, 0)) |
64 | 0 | goto err; |
65 | 0 | SipHash_Update(&sip, (unsigned char *)&i, sizeof(i)); |
66 | 0 | SipHash_Update(&sip, (unsigned char *)&salt, sizeof(salt)); |
67 | 0 | if (!SipHash_Final(&sip, block, sizeof(block))) |
68 | 0 | goto err; |
69 | 0 | n = num - off < sizeof(block) ? num - off : sizeof(block); |
70 | 0 | memcpy(buf + off, block, n); |
71 | 0 | } |
72 | 0 | ret = 1; |
73 | 0 | err: |
74 | 0 | OPENSSL_cleanse(block, sizeof(block)); |
75 | 0 | return ret; |
76 | 0 | } |
77 | | |
78 | | /* |
79 | | * Fill |buf| with a |num| byte salt for hashing untrusted input, e.g. the |
80 | | * SipHash key of a hash table indexed by attacker controlled data, so that |
81 | | * colliding inputs cannot be precomputed (hash flooding). The salt only has |
82 | | * to be unpredictable, not secret: never use this for cryptographic keys, |
83 | | * IVs, nonces or anything else that must come from a CSPRNG. |
84 | | * |
85 | | * Such tables must keep working in library contexts that have no usable |
86 | | * RAND, e.g. when only the null provider is loaded, so this tries in turn: |
87 | | * |
88 | | * 1. RAND_bytes_ex() on |ctx|, |
89 | | * 2. the operating system entropy sources, unless |ctx| has a strict seed |
90 | | * source configuration, which they must not stand in for, |
91 | | * 3. time, time stamp counter, process and thread ids and addresses, see |
92 | | * hash_salt_fallback(). This is used with strict seeding as well: it |
93 | | * makes a hash salt, not seed material. |
94 | | * |
95 | | * Errors raised by a source that failed are discarded when a later one |
96 | | * succeeds. Returns 1 on success and 0 if every source failed. |
97 | | */ |
98 | | int ossl_rand_hash_salt(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num) |
99 | 0 | { |
100 | 0 | unsigned char *seed = NULL; |
101 | 0 | size_t seed_len; |
102 | 0 | int ok; |
103 | |
|
104 | 0 | if (num == 0) |
105 | 0 | return 1; |
106 | | |
107 | 0 | ERR_set_mark(); |
108 | 0 | if (RAND_bytes_ex(ctx, buf, num, 0) > 0) |
109 | 0 | goto done; |
110 | | |
111 | | /* |
112 | | * Read the operating system sources directly rather than through |
113 | | * ossl_rand_get_user_entropy(), which would consult, and with strict |
114 | | * seeding create, the seed source of |ctx| from within the caller (e.g. |
115 | | * certificate parsing), and whose cleanup cannot reliably tell which of |
116 | | * the two allocated a buffer. ossl_rand_get_entropy() and |
117 | | * ossl_rand_cleanup_entropy() always pair up. |
118 | | */ |
119 | 0 | if (num <= RAND_POOL_MAX_LENGTH && !ossl_rand_seed_source_strict(ctx)) { |
120 | 0 | seed_len = ossl_rand_get_entropy(ctx, &seed, (int)(8 * num), num, num); |
121 | 0 | ok = seed_len == num; |
122 | 0 | if (ok) |
123 | 0 | memcpy(buf, seed, num); |
124 | 0 | ossl_rand_cleanup_entropy(ctx, seed, seed_len); |
125 | 0 | if (ok) |
126 | 0 | goto done; |
127 | 0 | } |
128 | | |
129 | 0 | if (hash_salt_fallback(buf, num)) |
130 | 0 | goto done; |
131 | | |
132 | 0 | ERR_clear_last_mark(); |
133 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_RETRIEVING_ENTROPY); |
134 | 0 | return 0; |
135 | | |
136 | 0 | done: |
137 | 0 | ERR_pop_to_mark(); |
138 | 0 | return 1; |
139 | 0 | } |