| 6 |
| * David A. McGrew |
| 7 | * Cisco Systems, Inc. |
| 8 | */ |
| 9 |
| 10 | /* |
| 11 | * |
| 12 | * Copyright (c) 2001-2017, Cisco Systems, Inc. |
| 13 | * All rights reserved. |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * |
| 19 | * Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * |
| 22 | * Redistributions in binary form must reproduce the above |
| 23 | * copyright notice, this list of conditions and the following |
| 24 | * disclaimer in the documentation and/or other materials provided |
| 25 | * with the distribution. |
| 26 | * |
| 27 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
| 28 | * contributors may be used to endorse or promote products derived |
| 29 | * from this software without specific prior written permission. |
| 30 | * |
| 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 34 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 35 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 36 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 37 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 38 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 39 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 40 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 41 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 42 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | * |
| 44 | */ |
| 45 |
| 46 | #include "config.h" |
| 47 |
| 48 | #include "stat.h" |
| 49 |
| 50 | srtp_debug_module_t srtp_mod_stat = { |
| 51 | 0, /* debugging is off by default */ |
| 52 | (char *)"stat test" /* printable module name */ |
| 53 | }; |
| 54 |
| 55 | /* |
| 56 | * each test assumes that 20,000 bits (2500 octets) of data is |
| 57 | * provided as input |
| 58 | */ |
| 59 |
| 60 | #define STAT_TEST_DATA_LEN 2500 |
| 61 |
| 62 | srtp_err_status_t stat_test_monobit(uint8_t *data) |
| 63 | { |
| 64 | uint8_t *data_end = data + STAT_TEST_DATA_LEN; |
| 65 | uint16_t ones_count; |
| 66 |
| 67 | ones_count = 0; |
| 68 | while (data < data_end) { |
| 69 | ones_count += octet_get_weight(*data); |
| 70 | data++; |
| 71 | } |
| 72 |
| 73 | debug_print(srtp_mod_stat, "bit count: %d", ones_count); |
| 74 |
| 75 | if ((ones_count < 9725) || (ones_count > 10275)) |
| 76 | return srtp_err_status_algo_fail; |
| 77 |
| 78 | return srtp_err_status_ok; |
| 79 | } |
| 80 |
| 81 | srtp_err_status_t stat_test_poker(uint8_t *data) |
| 82 | { |
| 83 | int i; |
| 84 | uint8_t *data_end = data + STAT_TEST_DATA_LEN; |
| 85 | double poker; |
| 86 | uint16_t f[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 87 |
| 88 | while (data < data_end) { |
| 89 | f[*data & 0x0f]++; /* increment freq. count for low nibble */ |
| 90 | f[(*data) >> 4]++; /* increment freq. count for high nibble */ |
| 91 | data++; |
| 92 | } |
| 93 |
| 94 | poker = 0.0; |
| 95 | for (i = 0; i < 16; i++) |
| 96 | poker += (double)f[i] * f[i]; |
| 97 |
| 98 | poker *= (16.0 / 5000.0); |
| 99 | poker -= 5000.0; |
| 100 |
| 101 | debug_print(srtp_mod_stat, "poker test: %f\n", poker); |
| 102 |
| 103 | if ((poker < 2.16) || (poker > 46.17)) |
| 104 | return srtp_err_status_algo_fail; |
| 105 |
| 106 | return srtp_err_status_ok; |
| 107 | } |
| 108 |
| 109 | /* |
| 110 | * runs[i] holds the number of runs of size (i-1) |
| 111 | */ |
| 112 |
| 113 | srtp_err_status_t stat_test_runs(uint8_t *data) |
| 114 | { |
| 115 | uint8_t *data_end = data + STAT_TEST_DATA_LEN; |
| 116 | uint16_t runs[6] = { 0, 0, 0, 0, 0, 0 }; |
| 117 | uint16_t gaps[6] = { 0, 0, 0, 0, 0, 0 }; |
| 118 | uint16_t lo_value[6] = { 2315, 1114, 527, 240, 103, 103 }; |
| 119 | uint16_t hi_value[6] = { 2685, 1386, 723, 384, 209, 209 }; |
| 120 | int state = 0; |
| 121 | uint16_t mask; |
| 122 | int i; |
| 123 |
| 124 | /* |
| 125 | * the state variable holds the number of bits in the |
| 126 | * current run (or gap, if negative) |
| 127 | */ |
| 128 |
| 129 | while (data < data_end) { |
| 130 | /* loop over the bits of this byte */ |
| 131 | for (mask = 1; mask < 256; mask <<= 1) { |
| 132 | if (*data & mask) { |
| 133 | /* next bit is a one */ |
| 134 | if (state > 0) { |
| 135 | /* prefix is a run, so increment the run-count */ |
| 136 | state++; |
| 137 |
| 138 | /* check for long runs */ |
| 139 | if (state > 25) { |
| 140 | debug_print(srtp_mod_stat, ">25 runs: %d", state); |
| 141 | return srtp_err_status_algo_fail; |
| 142 | } |
| 143 |
| 144 | } else if (state < 0) { |
| 145 | /* prefix is a gap */ |
| 146 | if (state < -25) { |
| 147 | debug_print(srtp_mod_stat, ">25 gaps: %d", state); |
| 148 | return srtp_err_status_algo_fail; /* long-runs test |
| 149 | failed */ |
| 150 | } |
| 151 | if (state < -6) { |
| 152 | state = -6; /* group together gaps > 5 */ |
| 153 | } |
| 154 | gaps[-1 - state]++; /* increment gap count */ |
| 155 | state = 1; /* set state at one set bit */ |
| 156 | } else { |
| 157 | /* state is zero; this happens only at initialization */ |
| 158 | state = 1; |
| 159 | } |
| 160 | } else { |
| 161 | /* next bit is a zero */ |
| 162 | if (state > 0) { |
| 163 | /* prefix is a run */ |
| 164 | if (state > 25) { |
| 165 | debug_print(srtp_mod_stat, ">25 runs (2): %d", state); |
| 166 | return srtp_err_status_algo_fail; /* long-runs test |
| 167 | failed */ |
| 168 | } |
| 169 | if (state > 6) { |
| 170 | state = 6; /* group together runs > 5 */ |
| 171 | } |
| 172 | runs[state - 1]++; /* increment run count */ |
| 173 | state = -1; /* set state at one zero bit */ |
| 174 | } else if (state < 0) { |
| 175 | /* prefix is a gap, so increment gap-count (decrement state) |
| 176 | */ |
| 177 | state--; |
| 178 |
| 179 | /* check for long gaps */ |
| 180 | if (state < -25) { |
| 181 | debug_print(srtp_mod_stat, ">25 gaps (2): %d", state); |
| 182 | return srtp_err_status_algo_fail; |
| 183 | } |
| 184 |
| 185 | } else { |
| 186 | /* state is zero; this happens only at initialization */ |
| 187 | state = -1; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 |
| 192 | /* move along to next octet */ |
| 193 | data++; |
| 194 | } |
| 195 |
| 196 | if (srtp_mod_stat.on) { |
| 197 | debug_print(srtp_mod_stat, "runs test", NULL); |
| 198 | for (i = 0; i < 6; i++) |
| 199 | debug_print(srtp_mod_stat, " runs[]: %d", runs[i]); |
| 200 | for (i = 0; i < 6; i++) |
| 201 | debug_print(srtp_mod_stat, " gaps[]: %d", gaps[i]); |
| 202 | } |
| 203 |
| 204 | /* check run and gap counts against the fixed limits */ |
| 205 | for (i = 0; i < 6; i++) |
| 206 | if ((runs[i] < lo_value[i]) || (runs[i] > hi_value[i]) || |
| 207 | (gaps[i] < lo_value[i]) || (gaps[i] > hi_value[i])) |
| 208 | return srtp_err_status_algo_fail; |
| 209 |
| 210 | return srtp_err_status_ok; |
| 211 | } |
| 212 |