| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bn/bn_rand.c |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||||||||
| 2 | typedef enum bnrand_flag_e { | - | ||||||||||||||||||
| 3 | NORMAL, TESTING, PRIVATE | - | ||||||||||||||||||
| 4 | } BNRAND_FLAG; | - | ||||||||||||||||||
| 5 | - | |||||||||||||||||||
| 6 | static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom) | - | ||||||||||||||||||
| 7 | { | - | ||||||||||||||||||
| 8 | unsigned char *buf = | - | ||||||||||||||||||
| 9 | ((void *)0) | - | ||||||||||||||||||
| 10 | ; | - | ||||||||||||||||||
| 11 | int b, ret = 0, bit, bytes, mask; | - | ||||||||||||||||||
| 12 | - | |||||||||||||||||||
| 13 | if (bits == 0
| 2-137416 | ||||||||||||||||||
| 14 | if (top != -1
| 0-2 | ||||||||||||||||||
| 15 | goto executed 2 times by 1 test: toosmall;goto toosmall;Executed by:
executed 2 times by 1 test: goto toosmall;Executed by:
| 2 | ||||||||||||||||||
| 16 | (BN_set_word((rnd),0)); | - | ||||||||||||||||||
| 17 | return never executed: 1;return 1;never executed: return 1; | 0 | ||||||||||||||||||
| 18 | } | - | ||||||||||||||||||
| 19 | if (bits < 0
| 0-137416 | ||||||||||||||||||
| 20 | goto executed 1 time by 1 test: toosmall;goto toosmall;Executed by:
executed 1 time by 1 test: goto toosmall;Executed by:
| 1 | ||||||||||||||||||
| 21 | - | |||||||||||||||||||
| 22 | bytes = (bits + 7) / 8; | - | ||||||||||||||||||
| 23 | bit = (bits - 1) % 8; | - | ||||||||||||||||||
| 24 | mask = 0xff << (bit + 1); | - | ||||||||||||||||||
| 25 | - | |||||||||||||||||||
| 26 | buf = CRYPTO_malloc(bytes, __FILE__, 39); | - | ||||||||||||||||||
| 27 | if (buf ==
| 0-137415 | ||||||||||||||||||
| 28 | ((void *)0)
| 0-137415 | ||||||||||||||||||
| 29 | ) { | - | ||||||||||||||||||
| 30 | ERR_put_error(3,(127),((1|64)),__FILE__,41); | - | ||||||||||||||||||
| 31 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 32 | } | - | ||||||||||||||||||
| 33 | - | |||||||||||||||||||
| 34 | - | |||||||||||||||||||
| 35 | b = flag == NORMAL
| 734-136681 | ||||||||||||||||||
| 36 | if (b <= 0
| 0-137415 | ||||||||||||||||||
| 37 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 38 | - | |||||||||||||||||||
| 39 | if (flag == TESTING
| 1807-135608 | ||||||||||||||||||
| 40 | - | |||||||||||||||||||
| 41 | - | |||||||||||||||||||
| 42 | - | |||||||||||||||||||
| 43 | int i; | - | ||||||||||||||||||
| 44 | unsigned char c; | - | ||||||||||||||||||
| 45 | - | |||||||||||||||||||
| 46 | for (i = 0; i < bytes
| 1807-148234 | ||||||||||||||||||
| 47 | if (RAND_bytes(&c, 1) <= 0
| 0-148234 | ||||||||||||||||||
| 48 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 49 | if (c >= 128
| 864-74246 | ||||||||||||||||||
| 50 | buf[i] = buf[i - 1]; executed 73382 times by 1 test: buf[i] = buf[i - 1];Executed by:
| 73382 | ||||||||||||||||||
| 51 | else if (c < 42
| 24287-50565 | ||||||||||||||||||
| 52 | buf[i] = 0; executed 24287 times by 1 test: buf[i] = 0;Executed by:
| 24287 | ||||||||||||||||||
| 53 | else if (c < 84
| 24281-26284 | ||||||||||||||||||
| 54 | buf[i] = 255; executed 24281 times by 1 test: buf[i] = 255;Executed by:
| 24281 | ||||||||||||||||||
| 55 | } executed 148234 times by 1 test: end of blockExecuted by:
| 148234 | ||||||||||||||||||
| 56 | } executed 1807 times by 1 test: end of blockExecuted by:
| 1807 | ||||||||||||||||||
| 57 | - | |||||||||||||||||||
| 58 | if (top >= 0
| 49342-88073 | ||||||||||||||||||
| 59 | if (top
| 3385-45957 | ||||||||||||||||||
| 60 | if (bit == 0
| 0-3385 | ||||||||||||||||||
| 61 | buf[0] = 1; | - | ||||||||||||||||||
| 62 | buf[1] |= 0x80; | - | ||||||||||||||||||
| 63 | } never executed: else {end of block | 0 | ||||||||||||||||||
| 64 | buf[0] |= (3 << (bit - 1)); | - | ||||||||||||||||||
| 65 | } executed 3385 times by 1 test: end of blockExecuted by:
| 3385 | ||||||||||||||||||
| 66 | } else { | - | ||||||||||||||||||
| 67 | buf[0] |= (1 << bit); | - | ||||||||||||||||||
| 68 | } executed 45957 times by 1 test: end of blockExecuted by:
| 45957 | ||||||||||||||||||
| 69 | } | - | ||||||||||||||||||
| 70 | buf[0] &= ~mask; | - | ||||||||||||||||||
| 71 | if (bottom
| 3598-133817 | ||||||||||||||||||
| 72 | buf[bytes - 1] |= 1; executed 3598 times by 1 test: buf[bytes - 1] |= 1;Executed by:
| 3598 | ||||||||||||||||||
| 73 | if (!BN_bin2bn(buf, bytes, rnd)
| 0-137415 | ||||||||||||||||||
| 74 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 75 | ret = 1; | - | ||||||||||||||||||
| 76 | err: code before this statement executed 137415 times by 2 tests: err:Executed by:
| 137415 | ||||||||||||||||||
| 77 | CRYPTO_clear_free(buf, bytes, __FILE__, 88); | - | ||||||||||||||||||
| 78 | ; | - | ||||||||||||||||||
| 79 | return executed 137415 times by 2 tests: ret;return ret;Executed by:
executed 137415 times by 2 tests: return ret;Executed by:
| 137415 | ||||||||||||||||||
| 80 | - | |||||||||||||||||||
| 81 | toosmall: | - | ||||||||||||||||||
| 82 | ERR_put_error(3,(127),(118),__FILE__,93); | - | ||||||||||||||||||
| 83 | return executed 3 times by 1 test: 0;return 0;Executed by:
executed 3 times by 1 test: return 0;Executed by:
| 3 | ||||||||||||||||||
| 84 | } | - | ||||||||||||||||||
| 85 | - | |||||||||||||||||||
| 86 | int BN_rand(BIGNUM *rnd, int bits, int top, int bottom) | - | ||||||||||||||||||
| 87 | { | - | ||||||||||||||||||
| 88 | return executed 737 times by 1 test: bnrand(NORMAL, rnd, bits, top, bottom);return bnrand(NORMAL, rnd, bits, top, bottom);Executed by:
executed 737 times by 1 test: return bnrand(NORMAL, rnd, bits, top, bottom);Executed by:
| 737 | ||||||||||||||||||
| 89 | } | - | ||||||||||||||||||
| 90 | - | |||||||||||||||||||
| 91 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom) | - | ||||||||||||||||||
| 92 | { | - | ||||||||||||||||||
| 93 | return executed 1807 times by 1 test: bnrand(TESTING, rnd, bits, top, bottom);return bnrand(TESTING, rnd, bits, top, bottom);Executed by:
executed 1807 times by 1 test: return bnrand(TESTING, rnd, bits, top, bottom);Executed by:
| 1807 | ||||||||||||||||||
| 94 | } | - | ||||||||||||||||||
| 95 | - | |||||||||||||||||||
| 96 | int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom) | - | ||||||||||||||||||
| 97 | { | - | ||||||||||||||||||
| 98 | return executed 124517 times by 1 test: bnrand(PRIVATE, rnd, bits, top, bottom);return bnrand(PRIVATE, rnd, bits, top, bottom);Executed by:
executed 124517 times by 1 test: return bnrand(PRIVATE, rnd, bits, top, bottom);Executed by:
| 124517 | ||||||||||||||||||
| 99 | } | - | ||||||||||||||||||
| 100 | - | |||||||||||||||||||
| 101 | - | |||||||||||||||||||
| 102 | static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range) | - | ||||||||||||||||||
| 103 | { | - | ||||||||||||||||||
| 104 | int n; | - | ||||||||||||||||||
| 105 | int count = 100; | - | ||||||||||||||||||
| 106 | - | |||||||||||||||||||
| 107 | if (range->neg
| 0-9242 | ||||||||||||||||||
| 108 | ERR_put_error(3,(138),(115),__FILE__,119); | - | ||||||||||||||||||
| 109 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 110 | } | - | ||||||||||||||||||
| 111 | - | |||||||||||||||||||
| 112 | n = BN_num_bits(range); | - | ||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | - | |||||||||||||||||||
| 115 | - | |||||||||||||||||||
| 116 | if (n == 1
| 0-9242 | ||||||||||||||||||
| 117 | ( never executed: BN_set_word((r),0));(BN_set_word((r),0));never executed: (BN_set_word((r),0)); | 0 | ||||||||||||||||||
| 118 | else if (!BN_is_bit_set(range, n - 2)
| 248-8651 | ||||||||||||||||||
| 119 | - | |||||||||||||||||||
| 120 | - | |||||||||||||||||||
| 121 | - | |||||||||||||||||||
| 122 | - | |||||||||||||||||||
| 123 | do { | - | ||||||||||||||||||
| 124 | if (!bnrand(flag, r, n + 1, -1, 0)
| 0-320 | ||||||||||||||||||
| 125 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 126 | - | |||||||||||||||||||
| 127 | - | |||||||||||||||||||
| 128 | - | |||||||||||||||||||
| 129 | - | |||||||||||||||||||
| 130 | - | |||||||||||||||||||
| 131 | - | |||||||||||||||||||
| 132 | - | |||||||||||||||||||
| 133 | if (BN_cmp(r, range) >= 0
| 83-237 | ||||||||||||||||||
| 134 | if (!BN_sub(r, r, range)
| 0-237 | ||||||||||||||||||
| 135 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 136 | if (BN_cmp(r, range) >= 0
| 97-140 | ||||||||||||||||||
| 137 | if (!BN_sub(r, r, range)
| 0-140 | ||||||||||||||||||
| 138 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 139 | } executed 237 times by 2 tests: end of blockExecuted by:
| 237 | ||||||||||||||||||
| 140 | - | |||||||||||||||||||
| 141 | if (!--count
| 0-320 | ||||||||||||||||||
| 142 | ERR_put_error(3,(138),(113),__FILE__,153); | - | ||||||||||||||||||
| 143 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 144 | } | - | ||||||||||||||||||
| 145 | - | |||||||||||||||||||
| 146 | } executed 320 times by 2 tests: end of blockExecuted by:
| 320 | ||||||||||||||||||
| 147 | while (BN_cmp(r, range) >= 0
| 72-248 | ||||||||||||||||||
| 148 | } executed 248 times by 2 tests: else {end of blockExecuted by:
| 248 | ||||||||||||||||||
| 149 | do { | - | ||||||||||||||||||
| 150 | - | |||||||||||||||||||
| 151 | if (!bnrand(flag, r, n, -1, 0)
| 0-10037 | ||||||||||||||||||
| 152 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 153 | - | |||||||||||||||||||
| 154 | if (!--count
| 0-10037 | ||||||||||||||||||
| 155 | ERR_put_error(3,(138),(113),__FILE__,166); | - | ||||||||||||||||||
| 156 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||||||||
| 157 | } | - | ||||||||||||||||||
| 158 | } executed 10037 times by 1 test: end of blockExecuted by:
| 10037 | ||||||||||||||||||
| 159 | while (BN_cmp(r, range) >= 0
| 1043-8994 | ||||||||||||||||||
| 160 | } executed 8994 times by 1 test: end of blockExecuted by:
| 8994 | ||||||||||||||||||
| 161 | - | |||||||||||||||||||
| 162 | ; | - | ||||||||||||||||||
| 163 | return executed 9242 times by 2 tests: 1;return 1;Executed by:
executed 9242 times by 2 tests: return 1;Executed by:
| 9242 | ||||||||||||||||||
| 164 | } | - | ||||||||||||||||||
| 165 | - | |||||||||||||||||||
| 166 | int BN_rand_range(BIGNUM *r, const BIGNUM *range) | - | ||||||||||||||||||
| 167 | { | - | ||||||||||||||||||
| 168 | return never executed: bnrand_range(NORMAL, r, range);return bnrand_range(NORMAL, r, range);never executed: return bnrand_range(NORMAL, r, range); | 0 | ||||||||||||||||||
| 169 | } | - | ||||||||||||||||||
| 170 | - | |||||||||||||||||||
| 171 | int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range) | - | ||||||||||||||||||
| 172 | { | - | ||||||||||||||||||
| 173 | return executed 9242 times by 2 tests: bnrand_range(PRIVATE, r, range);return bnrand_range(PRIVATE, r, range);Executed by:
executed 9242 times by 2 tests: return bnrand_range(PRIVATE, r, range);Executed by:
| 9242 | ||||||||||||||||||
| 174 | } | - | ||||||||||||||||||
| 175 | - | |||||||||||||||||||
| 176 | int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) | - | ||||||||||||||||||
| 177 | { | - | ||||||||||||||||||
| 178 | return never executed: BN_rand(rnd, bits, top, bottom);return BN_rand(rnd, bits, top, bottom);never executed: return BN_rand(rnd, bits, top, bottom); | 0 | ||||||||||||||||||
| 179 | } | - | ||||||||||||||||||
| 180 | - | |||||||||||||||||||
| 181 | int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range) | - | ||||||||||||||||||
| 182 | { | - | ||||||||||||||||||
| 183 | return never executed: BN_rand_range(r, range);return BN_rand_range(r, range);never executed: return BN_rand_range(r, range); | 0 | ||||||||||||||||||
| 184 | } | - | ||||||||||||||||||
| 185 | int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, | - | ||||||||||||||||||
| 186 | const BIGNUM *priv, const unsigned char *message, | - | ||||||||||||||||||
| 187 | size_t message_len, BN_CTX *ctx) | - | ||||||||||||||||||
| 188 | { | - | ||||||||||||||||||
| 189 | SHA512_CTX sha; | - | ||||||||||||||||||
| 190 | - | |||||||||||||||||||
| 191 | - | |||||||||||||||||||
| 192 | - | |||||||||||||||||||
| 193 | - | |||||||||||||||||||
| 194 | unsigned char random_bytes[64]; | - | ||||||||||||||||||
| 195 | unsigned char digest[64]; | - | ||||||||||||||||||
| 196 | unsigned done, todo; | - | ||||||||||||||||||
| 197 | - | |||||||||||||||||||
| 198 | const unsigned num_k_bytes = ((BN_num_bits(range)+7)/8) + 8; | - | ||||||||||||||||||
| 199 | unsigned char private_bytes[96]; | - | ||||||||||||||||||
| 200 | unsigned char *k_bytes; | - | ||||||||||||||||||
| 201 | int ret = 0; | - | ||||||||||||||||||
| 202 | - | |||||||||||||||||||
| 203 | k_bytes = CRYPTO_malloc(num_k_bytes, __FILE__, 223); | - | ||||||||||||||||||
| 204 | if (k_bytes ==
| 0-427 | ||||||||||||||||||
| 205 | ((void *)0)
| 0-427 | ||||||||||||||||||
| 206 | ) | - | ||||||||||||||||||
| 207 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 208 | - | |||||||||||||||||||
| 209 | - | |||||||||||||||||||
| 210 | todo = sizeof(priv->d[0]) * priv->top; | - | ||||||||||||||||||
| 211 | if (todo > sizeof(private_bytes)
| 0-427 | ||||||||||||||||||
| 212 | - | |||||||||||||||||||
| 213 | - | |||||||||||||||||||
| 214 | - | |||||||||||||||||||
| 215 | - | |||||||||||||||||||
| 216 | - | |||||||||||||||||||
| 217 | ERR_put_error(3,(140),(117),__FILE__,235); | - | ||||||||||||||||||
| 218 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 219 | } | - | ||||||||||||||||||
| 220 | memcpy(private_bytes, priv->d, todo); | - | ||||||||||||||||||
| 221 | memset(private_bytes + todo, 0, sizeof(private_bytes) - todo); | - | ||||||||||||||||||
| 222 | - | |||||||||||||||||||
| 223 | for (done = 0; done < num_k_bytes
| 427-432 | ||||||||||||||||||
| 224 | if (RAND_priv_bytes(random_bytes, sizeof(random_bytes)) != 1
| 0-432 | ||||||||||||||||||
| 225 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 226 | SHA512_Init(&sha); | - | ||||||||||||||||||
| 227 | SHA512_Update(&sha, &done, sizeof(done)); | - | ||||||||||||||||||
| 228 | SHA512_Update(&sha, private_bytes, sizeof(private_bytes)); | - | ||||||||||||||||||
| 229 | SHA512_Update(&sha, message, message_len); | - | ||||||||||||||||||
| 230 | SHA512_Update(&sha, random_bytes, sizeof(random_bytes)); | - | ||||||||||||||||||
| 231 | SHA512_Final(digest, &sha); | - | ||||||||||||||||||
| 232 | - | |||||||||||||||||||
| 233 | todo = num_k_bytes - done; | - | ||||||||||||||||||
| 234 | if (todo > 64
| 5-427 | ||||||||||||||||||
| 235 | todo = 64; executed 5 times by 1 test: todo = 64;Executed by:
| 5 | ||||||||||||||||||
| 236 | memcpy(k_bytes + done, digest, todo); | - | ||||||||||||||||||
| 237 | done += todo; | - | ||||||||||||||||||
| 238 | } executed 432 times by 1 test: end of blockExecuted by:
| 432 | ||||||||||||||||||
| 239 | - | |||||||||||||||||||
| 240 | if (!BN_bin2bn(k_bytes, num_k_bytes, out)
| 0-427 | ||||||||||||||||||
| 241 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 242 | if (BN_div(
| 0-427 | ||||||||||||||||||
| 243 | ((void *)0)
| 0-427 | ||||||||||||||||||
| 244 | ,(out),(out),(range),(ctx)) != 1
| 0-427 | ||||||||||||||||||
| 245 | goto never executed: err;goto err;never executed: goto err; | 0 | ||||||||||||||||||
| 246 | ret = 1; | - | ||||||||||||||||||
| 247 | - | |||||||||||||||||||
| 248 | err: code before this statement executed 427 times by 1 test: err:Executed by:
| 427 | ||||||||||||||||||
| 249 | CRYPTO_free(k_bytes, __FILE__, 265); | - | ||||||||||||||||||
| 250 | OPENSSL_cleanse(private_bytes, sizeof(private_bytes)); | - | ||||||||||||||||||
| 251 | return executed 427 times by 1 test: ret;return ret;Executed by:
executed 427 times by 1 test: return ret;Executed by:
| 427 | ||||||||||||||||||
| 252 | } | - | ||||||||||||||||||
| Switch to Source code | Preprocessed file |