| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssh/src/openbsd-compat/arc4random.c |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||||||||
| 2 | - | |||||||||||||||||||
| 3 | - | |||||||||||||||||||
| 4 | - | |||||||||||||||||||
| 5 | - | |||||||||||||||||||
| 6 | - | |||||||||||||||||||
| 7 | - | |||||||||||||||||||
| 8 | - | |||||||||||||||||||
| 9 | static int rs_initialized; | - | ||||||||||||||||||
| 10 | static pid_t rs_stir_pid; | - | ||||||||||||||||||
| 11 | static chacha_ctx rs; | - | ||||||||||||||||||
| 12 | static u_char rs_buf[(16*64)]; | - | ||||||||||||||||||
| 13 | static size_t rs_have; | - | ||||||||||||||||||
| 14 | static size_t rs_count; | - | ||||||||||||||||||
| 15 | - | |||||||||||||||||||
| 16 | static __inline void _rs_rekey(u_char *dat, size_t datlen); | - | ||||||||||||||||||
| 17 | - | |||||||||||||||||||
| 18 | static __inline void | - | ||||||||||||||||||
| 19 | _rs_init(u_char *buf, size_t n) | - | ||||||||||||||||||
| 20 | { | - | ||||||||||||||||||
| 21 | if (n < 32 + 8
| 0-3033 | ||||||||||||||||||
| 22 | return; never executed: return; | 0 | ||||||||||||||||||
| 23 | chacha_keysetup(&rs, buf, 32 * 8, 0); | - | ||||||||||||||||||
| 24 | chacha_ivsetup(&rs, buf + 32); | - | ||||||||||||||||||
| 25 | } executed 3033 times by 4 tests: end of blockExecuted by:
| 3033 | ||||||||||||||||||
| 26 | static void | - | ||||||||||||||||||
| 27 | _rs_stir(void) | - | ||||||||||||||||||
| 28 | { | - | ||||||||||||||||||
| 29 | u_char rnd[32 + 8]; | - | ||||||||||||||||||
| 30 | - | |||||||||||||||||||
| 31 | - | |||||||||||||||||||
| 32 | if (RAND_bytes(rnd, sizeof(rnd)) <= 0
| 0-11 | ||||||||||||||||||
| 33 | fatal("Couldn't obtain random bytes (error 0x%lx)", never executed: fatal("Couldn't obtain random bytes (error 0x%lx)", (unsigned long)ERR_get_error()); | 0 | ||||||||||||||||||
| 34 | (unsigned long)ERR_get_error()); never executed: fatal("Couldn't obtain random bytes (error 0x%lx)", (unsigned long)ERR_get_error()); | 0 | ||||||||||||||||||
| 35 | - | |||||||||||||||||||
| 36 | - | |||||||||||||||||||
| 37 | - | |||||||||||||||||||
| 38 | - | |||||||||||||||||||
| 39 | if (!rs_initialized
| 1-10 | ||||||||||||||||||
| 40 | rs_initialized = 1; | - | ||||||||||||||||||
| 41 | _rs_init(rnd, sizeof(rnd)); | - | ||||||||||||||||||
| 42 | } executed 10 times by 4 tests: elseend of blockExecuted by:
| 10 | ||||||||||||||||||
| 43 | _rs_rekey(rnd, sizeof(rnd)); executed 1 time by 1 test: _rs_rekey(rnd, sizeof(rnd));Executed by:
| 1 | ||||||||||||||||||
| 44 | explicit_bzero(rnd, sizeof(rnd)); | - | ||||||||||||||||||
| 45 | - | |||||||||||||||||||
| 46 | - | |||||||||||||||||||
| 47 | rs_have = 0; | - | ||||||||||||||||||
| 48 | memset(rs_buf, 0, (16*64)); | - | ||||||||||||||||||
| 49 | - | |||||||||||||||||||
| 50 | rs_count = 1600000; | - | ||||||||||||||||||
| 51 | } executed 11 times by 4 tests: end of blockExecuted by:
| 11 | ||||||||||||||||||
| 52 | - | |||||||||||||||||||
| 53 | static __inline void | - | ||||||||||||||||||
| 54 | _rs_stir_if_needed(size_t len) | - | ||||||||||||||||||
| 55 | { | - | ||||||||||||||||||
| 56 | pid_t pid = getpid(); | - | ||||||||||||||||||
| 57 | - | |||||||||||||||||||
| 58 | if (rs_count <= len
| 0-739066 | ||||||||||||||||||
| 59 | rs_stir_pid = pid; | - | ||||||||||||||||||
| 60 | _rs_stir(); | - | ||||||||||||||||||
| 61 | } executed 11 times by 4 tests: elseend of blockExecuted by:
| 11 | ||||||||||||||||||
| 62 | rs_count -= len; executed 739066 times by 4 tests: rs_count -= len;Executed by:
| 739066 | ||||||||||||||||||
| 63 | } | - | ||||||||||||||||||
| 64 | - | |||||||||||||||||||
| 65 | static __inline void | - | ||||||||||||||||||
| 66 | _rs_rekey(u_char *dat, size_t datlen) | - | ||||||||||||||||||
| 67 | { | - | ||||||||||||||||||
| 68 | - | |||||||||||||||||||
| 69 | - | |||||||||||||||||||
| 70 | - | |||||||||||||||||||
| 71 | - | |||||||||||||||||||
| 72 | chacha_encrypt_bytes(&rs, rs_buf, rs_buf, (16*64)); | - | ||||||||||||||||||
| 73 | - | |||||||||||||||||||
| 74 | if (dat
| 1-3022 | ||||||||||||||||||
| 75 | size_t i, m; | - | ||||||||||||||||||
| 76 | - | |||||||||||||||||||
| 77 | m = | - | ||||||||||||||||||
| 78 | (((
| 0-1 | ||||||||||||||||||
| 79 | datlen
| 0-1 | ||||||||||||||||||
| 80 | )<(
| 0-1 | ||||||||||||||||||
| 81 | 32 + 8
| 0-1 | ||||||||||||||||||
| 82 | ))
| 0-1 | ||||||||||||||||||
| 83 | datlen | - | ||||||||||||||||||
| 84 | ):( | - | ||||||||||||||||||
| 85 | 32 + 8 | - | ||||||||||||||||||
| 86 | )) | - | ||||||||||||||||||
| 87 | ; | - | ||||||||||||||||||
| 88 | for (i = 0; i < m
| 1-40 | ||||||||||||||||||
| 89 | rs_buf[i] ^= dat[i]; executed 40 times by 1 test: rs_buf[i] ^= dat[i];Executed by:
| 40 | ||||||||||||||||||
| 90 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 91 | - | |||||||||||||||||||
| 92 | _rs_init(rs_buf, 32 + 8); | - | ||||||||||||||||||
| 93 | memset(rs_buf, 0, 32 + 8); | - | ||||||||||||||||||
| 94 | rs_have = (16*64) - 32 - 8; | - | ||||||||||||||||||
| 95 | } executed 3023 times by 4 tests: end of blockExecuted by:
| 3023 | ||||||||||||||||||
| 96 | - | |||||||||||||||||||
| 97 | static __inline void | - | ||||||||||||||||||
| 98 | _rs_random_buf(void *_buf, size_t n) | - | ||||||||||||||||||
| 99 | { | - | ||||||||||||||||||
| 100 | u_char *buf = (u_char *)_buf; | - | ||||||||||||||||||
| 101 | size_t m; | - | ||||||||||||||||||
| 102 | - | |||||||||||||||||||
| 103 | _rs_stir_if_needed(n); | - | ||||||||||||||||||
| 104 | while (n > 0
| 27008-27142 | ||||||||||||||||||
| 105 | if (rs_have > 0
| 124-27018 | ||||||||||||||||||
| 106 | m = | - | ||||||||||||||||||
| 107 | (((
| 107-26911 | ||||||||||||||||||
| 108 | n
| 107-26911 | ||||||||||||||||||
| 109 | )<(
| 107-26911 | ||||||||||||||||||
| 110 | rs_have
| 107-26911 | ||||||||||||||||||
| 111 | ))
| 107-26911 | ||||||||||||||||||
| 112 | n | - | ||||||||||||||||||
| 113 | ):( | - | ||||||||||||||||||
| 114 | rs_have | - | ||||||||||||||||||
| 115 | )) | - | ||||||||||||||||||
| 116 | ; | - | ||||||||||||||||||
| 117 | memcpy(buf, rs_buf + (16*64) - rs_have, m); | - | ||||||||||||||||||
| 118 | memset(rs_buf + (16*64) - rs_have, 0, m); | - | ||||||||||||||||||
| 119 | buf += m; | - | ||||||||||||||||||
| 120 | n -= m; | - | ||||||||||||||||||
| 121 | rs_have -= m; | - | ||||||||||||||||||
| 122 | } executed 27018 times by 4 tests: end of blockExecuted by:
| 27018 | ||||||||||||||||||
| 123 | if (rs_have == 0
| 231-26911 | ||||||||||||||||||
| 124 | _rs_rekey( executed 231 times by 4 tests: _rs_rekey( ((void *)0) , 0);Executed by:
| 231 | ||||||||||||||||||
| 125 | ((void *)0) executed 231 times by 4 tests: _rs_rekey( ((void *)0) , 0);Executed by:
| 231 | ||||||||||||||||||
| 126 | , 0); executed 231 times by 4 tests: _rs_rekey( ((void *)0) , 0);Executed by:
| 231 | ||||||||||||||||||
| 127 | } executed 27142 times by 4 tests: end of blockExecuted by:
| 27142 | ||||||||||||||||||
| 128 | } executed 27008 times by 4 tests: end of blockExecuted by:
| 27008 | ||||||||||||||||||
| 129 | - | |||||||||||||||||||
| 130 | static __inline void | - | ||||||||||||||||||
| 131 | _rs_random_u32(u_int32_t *val) | - | ||||||||||||||||||
| 132 | { | - | ||||||||||||||||||
| 133 | _rs_stir_if_needed(sizeof(*val)); | - | ||||||||||||||||||
| 134 | if (rs_have < sizeof(*val)
| 2791-709278 | ||||||||||||||||||
| 135 | _rs_rekey( executed 2791 times by 2 tests: _rs_rekey( ((void *)0) , 0);Executed by:
| 2791 | ||||||||||||||||||
| 136 | ((void *)0) executed 2791 times by 2 tests: _rs_rekey( ((void *)0) , 0);Executed by:
| 2791 | ||||||||||||||||||
| 137 | , 0); executed 2791 times by 2 tests: _rs_rekey( ((void *)0) , 0);Executed by:
| 2791 | ||||||||||||||||||
| 138 | memcpy(val, rs_buf + (16*64) - rs_have, sizeof(*val)); | - | ||||||||||||||||||
| 139 | memset(rs_buf + (16*64) - rs_have, 0, sizeof(*val)); | - | ||||||||||||||||||
| 140 | rs_have -= sizeof(*val); | - | ||||||||||||||||||
| 141 | return; executed 712069 times by 2 tests: return;Executed by:
| 712069 | ||||||||||||||||||
| 142 | } | - | ||||||||||||||||||
| 143 | - | |||||||||||||||||||
| 144 | void | - | ||||||||||||||||||
| 145 | arc4random_stir(void) | - | ||||||||||||||||||
| 146 | { | - | ||||||||||||||||||
| 147 | ; | - | ||||||||||||||||||
| 148 | _rs_stir(); | - | ||||||||||||||||||
| 149 | ; | - | ||||||||||||||||||
| 150 | } never executed: end of block | 0 | ||||||||||||||||||
| 151 | - | |||||||||||||||||||
| 152 | void | - | ||||||||||||||||||
| 153 | arc4random_addrandom(u_char *dat, int datlen) | - | ||||||||||||||||||
| 154 | { | - | ||||||||||||||||||
| 155 | int m; | - | ||||||||||||||||||
| 156 | - | |||||||||||||||||||
| 157 | ; | - | ||||||||||||||||||
| 158 | if (!rs_initialized
| 0 | ||||||||||||||||||
| 159 | _rs_stir(); never executed: _rs_stir(); | 0 | ||||||||||||||||||
| 160 | while (datlen > 0
| 0 | ||||||||||||||||||
| 161 | m = | - | ||||||||||||||||||
| 162 | (((
| 0 | ||||||||||||||||||
| 163 | datlen
| 0 | ||||||||||||||||||
| 164 | )<(
| 0 | ||||||||||||||||||
| 165 | 32 + 8
| 0 | ||||||||||||||||||
| 166 | ))
| 0 | ||||||||||||||||||
| 167 | datlen | - | ||||||||||||||||||
| 168 | ):( | - | ||||||||||||||||||
| 169 | 32 + 8 | - | ||||||||||||||||||
| 170 | )) | - | ||||||||||||||||||
| 171 | ; | - | ||||||||||||||||||
| 172 | _rs_rekey(dat, m); | - | ||||||||||||||||||
| 173 | dat += m; | - | ||||||||||||||||||
| 174 | datlen -= m; | - | ||||||||||||||||||
| 175 | } never executed: end of block | 0 | ||||||||||||||||||
| 176 | ; | - | ||||||||||||||||||
| 177 | } never executed: end of block | 0 | ||||||||||||||||||
| 178 | - | |||||||||||||||||||
| 179 | u_int32_t | - | ||||||||||||||||||
| 180 | arc4random(void) | - | ||||||||||||||||||
| 181 | { | - | ||||||||||||||||||
| 182 | u_int32_t val; | - | ||||||||||||||||||
| 183 | - | |||||||||||||||||||
| 184 | ; | - | ||||||||||||||||||
| 185 | _rs_random_u32(&val); | - | ||||||||||||||||||
| 186 | ; | - | ||||||||||||||||||
| 187 | return executed 712069 times by 2 tests: val;return val;Executed by:
executed 712069 times by 2 tests: return val;Executed by:
| 712069 | ||||||||||||||||||
| 188 | } | - | ||||||||||||||||||
| 189 | - | |||||||||||||||||||
| 190 | - | |||||||||||||||||||
| 191 | - | |||||||||||||||||||
| 192 | - | |||||||||||||||||||
| 193 | - | |||||||||||||||||||
| 194 | - | |||||||||||||||||||
| 195 | void | - | ||||||||||||||||||
| 196 | arc4random_buf(void *buf, size_t n) | - | ||||||||||||||||||
| 197 | { | - | ||||||||||||||||||
| 198 | ; | - | ||||||||||||||||||
| 199 | _rs_random_buf(buf, n); | - | ||||||||||||||||||
| 200 | ; | - | ||||||||||||||||||
| 201 | } executed 27008 times by 4 tests: end of blockExecuted by:
| 27008 | ||||||||||||||||||
| 202 | u_int32_t | - | ||||||||||||||||||
| 203 | arc4random_uniform(u_int32_t upper_bound) | - | ||||||||||||||||||
| 204 | { | - | ||||||||||||||||||
| 205 | u_int32_t r, min; | - | ||||||||||||||||||
| 206 | - | |||||||||||||||||||
| 207 | if (upper_bound < 2
| 0-580289 | ||||||||||||||||||
| 208 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 209 | - | |||||||||||||||||||
| 210 | - | |||||||||||||||||||
| 211 | min = -upper_bound % upper_bound; | - | ||||||||||||||||||
| 212 | - | |||||||||||||||||||
| 213 | - | |||||||||||||||||||
| 214 | - | |||||||||||||||||||
| 215 | - | |||||||||||||||||||
| 216 | - | |||||||||||||||||||
| 217 | - | |||||||||||||||||||
| 218 | - | |||||||||||||||||||
| 219 | for (;;) { | - | ||||||||||||||||||
| 220 | r = arc4random(); | - | ||||||||||||||||||
| 221 | if (r >= min
| 0-580289 | ||||||||||||||||||
| 222 | break; executed 580289 times by 1 test: break;Executed by:
| 580289 | ||||||||||||||||||
| 223 | } never executed: end of block | 0 | ||||||||||||||||||
| 224 | - | |||||||||||||||||||
| 225 | return executed 580289 times by 1 test: r % upper_bound;return r % upper_bound;Executed by:
executed 580289 times by 1 test: return r % upper_bound;Executed by:
| 580289 | ||||||||||||||||||
| 226 | } | - | ||||||||||||||||||
| Switch to Source code | Preprocessed file |