Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * cipher_driver.c |
michael@0 | 3 | * |
michael@0 | 4 | * A driver for the generic cipher type |
michael@0 | 5 | * |
michael@0 | 6 | * David A. McGrew |
michael@0 | 7 | * Cisco Systems, Inc. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | /* |
michael@0 | 11 | * |
michael@0 | 12 | * Copyright (c) 2001-2006, Cisco Systems, Inc. |
michael@0 | 13 | * All rights reserved. |
michael@0 | 14 | * |
michael@0 | 15 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 16 | * modification, are permitted provided that the following conditions |
michael@0 | 17 | * are met: |
michael@0 | 18 | * |
michael@0 | 19 | * Redistributions of source code must retain the above copyright |
michael@0 | 20 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 21 | * |
michael@0 | 22 | * Redistributions in binary form must reproduce the above |
michael@0 | 23 | * copyright notice, this list of conditions and the following |
michael@0 | 24 | * disclaimer in the documentation and/or other materials provided |
michael@0 | 25 | * with the distribution. |
michael@0 | 26 | * |
michael@0 | 27 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
michael@0 | 28 | * contributors may be used to endorse or promote products derived |
michael@0 | 29 | * from this software without specific prior written permission. |
michael@0 | 30 | * |
michael@0 | 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
michael@0 | 34 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
michael@0 | 35 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
michael@0 | 36 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 37 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
michael@0 | 38 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 39 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
michael@0 | 40 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 41 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
michael@0 | 42 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 43 | * |
michael@0 | 44 | */ |
michael@0 | 45 | |
michael@0 | 46 | #include <stdio.h> /* for printf() */ |
michael@0 | 47 | #include <stdlib.h> /* for rand() */ |
michael@0 | 48 | #include <string.h> /* for memset() */ |
michael@0 | 49 | #include <unistd.h> /* for getopt() */ |
michael@0 | 50 | #include "cipher.h" |
michael@0 | 51 | #include "aes_icm.h" |
michael@0 | 52 | #include "null_cipher.h" |
michael@0 | 53 | |
michael@0 | 54 | #define PRINT_DEBUG 0 |
michael@0 | 55 | |
michael@0 | 56 | void |
michael@0 | 57 | cipher_driver_test_throughput(cipher_t *c); |
michael@0 | 58 | |
michael@0 | 59 | err_status_t |
michael@0 | 60 | cipher_driver_self_test(cipher_type_t *ct); |
michael@0 | 61 | |
michael@0 | 62 | |
michael@0 | 63 | /* |
michael@0 | 64 | * cipher_driver_test_buffering(ct) tests the cipher's output |
michael@0 | 65 | * buffering for correctness by checking the consistency of succesive |
michael@0 | 66 | * calls |
michael@0 | 67 | */ |
michael@0 | 68 | |
michael@0 | 69 | err_status_t |
michael@0 | 70 | cipher_driver_test_buffering(cipher_t *c); |
michael@0 | 71 | |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | * functions for testing cipher cache thrash |
michael@0 | 75 | */ |
michael@0 | 76 | err_status_t |
michael@0 | 77 | cipher_driver_test_array_throughput(cipher_type_t *ct, |
michael@0 | 78 | int klen, int num_cipher); |
michael@0 | 79 | |
michael@0 | 80 | void |
michael@0 | 81 | cipher_array_test_throughput(cipher_t *ca[], int num_cipher); |
michael@0 | 82 | |
michael@0 | 83 | uint64_t |
michael@0 | 84 | cipher_array_bits_per_second(cipher_t *cipher_array[], int num_cipher, |
michael@0 | 85 | unsigned octets_in_buffer, int num_trials); |
michael@0 | 86 | |
michael@0 | 87 | err_status_t |
michael@0 | 88 | cipher_array_delete(cipher_t *cipher_array[], int num_cipher); |
michael@0 | 89 | |
michael@0 | 90 | err_status_t |
michael@0 | 91 | cipher_array_alloc_init(cipher_t ***cipher_array, int num_ciphers, |
michael@0 | 92 | cipher_type_t *ctype, int klen); |
michael@0 | 93 | |
michael@0 | 94 | void |
michael@0 | 95 | usage(char *prog_name) { |
michael@0 | 96 | printf("usage: %s [ -t | -v | -a ]\n", prog_name); |
michael@0 | 97 | exit(255); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | void |
michael@0 | 101 | check_status(err_status_t s) { |
michael@0 | 102 | if (s) { |
michael@0 | 103 | printf("error (code %d)\n", s); |
michael@0 | 104 | exit(s); |
michael@0 | 105 | } |
michael@0 | 106 | return; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | /* |
michael@0 | 110 | * null_cipher, aes_icm, and aes_cbc are the cipher meta-objects |
michael@0 | 111 | * defined in the files in crypto/cipher subdirectory. these are |
michael@0 | 112 | * declared external so that we can use these cipher types here |
michael@0 | 113 | */ |
michael@0 | 114 | |
michael@0 | 115 | extern cipher_type_t null_cipher; |
michael@0 | 116 | extern cipher_type_t aes_icm; |
michael@0 | 117 | extern cipher_type_t aes_cbc; |
michael@0 | 118 | |
michael@0 | 119 | int |
michael@0 | 120 | main(int argc, char *argv[]) { |
michael@0 | 121 | cipher_t *c = NULL; |
michael@0 | 122 | err_status_t status; |
michael@0 | 123 | unsigned char test_key[48] = { |
michael@0 | 124 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
michael@0 | 125 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
michael@0 | 126 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
michael@0 | 127 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
michael@0 | 128 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
michael@0 | 129 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
michael@0 | 130 | }; |
michael@0 | 131 | int q; |
michael@0 | 132 | unsigned do_timing_test = 0; |
michael@0 | 133 | unsigned do_validation = 0; |
michael@0 | 134 | unsigned do_array_timing_test = 0; |
michael@0 | 135 | |
michael@0 | 136 | /* process input arguments */ |
michael@0 | 137 | while (1) { |
michael@0 | 138 | q = getopt(argc, argv, "tva"); |
michael@0 | 139 | if (q == -1) |
michael@0 | 140 | break; |
michael@0 | 141 | switch (q) { |
michael@0 | 142 | case 't': |
michael@0 | 143 | do_timing_test = 1; |
michael@0 | 144 | break; |
michael@0 | 145 | case 'v': |
michael@0 | 146 | do_validation = 1; |
michael@0 | 147 | break; |
michael@0 | 148 | case 'a': |
michael@0 | 149 | do_array_timing_test = 1; |
michael@0 | 150 | break; |
michael@0 | 151 | default: |
michael@0 | 152 | usage(argv[0]); |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | printf("cipher test driver\n" |
michael@0 | 157 | "David A. McGrew\n" |
michael@0 | 158 | "Cisco Systems, Inc.\n"); |
michael@0 | 159 | |
michael@0 | 160 | if (!do_validation && !do_timing_test && !do_array_timing_test) |
michael@0 | 161 | usage(argv[0]); |
michael@0 | 162 | |
michael@0 | 163 | /* arry timing (cache thrash) test */ |
michael@0 | 164 | if (do_array_timing_test) { |
michael@0 | 165 | int max_num_cipher = 1 << 16; /* number of ciphers in cipher_array */ |
michael@0 | 166 | int num_cipher; |
michael@0 | 167 | |
michael@0 | 168 | for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8) |
michael@0 | 169 | cipher_driver_test_array_throughput(&null_cipher, 0, num_cipher); |
michael@0 | 170 | |
michael@0 | 171 | for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8) |
michael@0 | 172 | cipher_driver_test_array_throughput(&aes_icm, 30, num_cipher); |
michael@0 | 173 | |
michael@0 | 174 | for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8) |
michael@0 | 175 | cipher_driver_test_array_throughput(&aes_icm, 46, num_cipher); |
michael@0 | 176 | |
michael@0 | 177 | for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8) |
michael@0 | 178 | cipher_driver_test_array_throughput(&aes_cbc, 16, num_cipher); |
michael@0 | 179 | |
michael@0 | 180 | for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8) |
michael@0 | 181 | cipher_driver_test_array_throughput(&aes_cbc, 32, num_cipher); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | if (do_validation) { |
michael@0 | 185 | cipher_driver_self_test(&null_cipher); |
michael@0 | 186 | cipher_driver_self_test(&aes_icm); |
michael@0 | 187 | cipher_driver_self_test(&aes_cbc); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | /* do timing and/or buffer_test on null_cipher */ |
michael@0 | 191 | status = cipher_type_alloc(&null_cipher, &c, 0); |
michael@0 | 192 | check_status(status); |
michael@0 | 193 | |
michael@0 | 194 | status = cipher_init(c, NULL, direction_encrypt); |
michael@0 | 195 | check_status(status); |
michael@0 | 196 | |
michael@0 | 197 | if (do_timing_test) |
michael@0 | 198 | cipher_driver_test_throughput(c); |
michael@0 | 199 | if (do_validation) { |
michael@0 | 200 | status = cipher_driver_test_buffering(c); |
michael@0 | 201 | check_status(status); |
michael@0 | 202 | } |
michael@0 | 203 | status = cipher_dealloc(c); |
michael@0 | 204 | check_status(status); |
michael@0 | 205 | |
michael@0 | 206 | |
michael@0 | 207 | /* run the throughput test on the aes_icm cipher (128-bit key) */ |
michael@0 | 208 | status = cipher_type_alloc(&aes_icm, &c, 30); |
michael@0 | 209 | if (status) { |
michael@0 | 210 | fprintf(stderr, "error: can't allocate cipher\n"); |
michael@0 | 211 | exit(status); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | status = cipher_init(c, test_key, direction_encrypt); |
michael@0 | 215 | check_status(status); |
michael@0 | 216 | |
michael@0 | 217 | if (do_timing_test) |
michael@0 | 218 | cipher_driver_test_throughput(c); |
michael@0 | 219 | |
michael@0 | 220 | if (do_validation) { |
michael@0 | 221 | status = cipher_driver_test_buffering(c); |
michael@0 | 222 | check_status(status); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | status = cipher_dealloc(c); |
michael@0 | 226 | check_status(status); |
michael@0 | 227 | |
michael@0 | 228 | /* repeat the tests with 256-bit keys */ |
michael@0 | 229 | status = cipher_type_alloc(&aes_icm, &c, 46); |
michael@0 | 230 | if (status) { |
michael@0 | 231 | fprintf(stderr, "error: can't allocate cipher\n"); |
michael@0 | 232 | exit(status); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | status = cipher_init(c, test_key, direction_encrypt); |
michael@0 | 236 | check_status(status); |
michael@0 | 237 | |
michael@0 | 238 | if (do_timing_test) |
michael@0 | 239 | cipher_driver_test_throughput(c); |
michael@0 | 240 | |
michael@0 | 241 | if (do_validation) { |
michael@0 | 242 | status = cipher_driver_test_buffering(c); |
michael@0 | 243 | check_status(status); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | status = cipher_dealloc(c); |
michael@0 | 247 | check_status(status); |
michael@0 | 248 | |
michael@0 | 249 | return 0; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | void |
michael@0 | 253 | cipher_driver_test_throughput(cipher_t *c) { |
michael@0 | 254 | int i; |
michael@0 | 255 | int min_enc_len = 32; |
michael@0 | 256 | int max_enc_len = 2048; /* should be a power of two */ |
michael@0 | 257 | int num_trials = 1000000; |
michael@0 | 258 | |
michael@0 | 259 | printf("timing %s throughput, key length %d:\n", c->type->description, c->key_len); |
michael@0 | 260 | fflush(stdout); |
michael@0 | 261 | for (i=min_enc_len; i <= max_enc_len; i = i * 2) |
michael@0 | 262 | printf("msg len: %d\tgigabits per second: %f\n", |
michael@0 | 263 | i, cipher_bits_per_second(c, i, num_trials) / 1e9); |
michael@0 | 264 | |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | err_status_t |
michael@0 | 268 | cipher_driver_self_test(cipher_type_t *ct) { |
michael@0 | 269 | err_status_t status; |
michael@0 | 270 | |
michael@0 | 271 | printf("running cipher self-test for %s...", ct->description); |
michael@0 | 272 | status = cipher_type_self_test(ct); |
michael@0 | 273 | if (status) { |
michael@0 | 274 | printf("failed with error code %d\n", status); |
michael@0 | 275 | exit(status); |
michael@0 | 276 | } |
michael@0 | 277 | printf("passed\n"); |
michael@0 | 278 | |
michael@0 | 279 | return err_status_ok; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | /* |
michael@0 | 283 | * cipher_driver_test_buffering(ct) tests the cipher's output |
michael@0 | 284 | * buffering for correctness by checking the consistency of succesive |
michael@0 | 285 | * calls |
michael@0 | 286 | */ |
michael@0 | 287 | |
michael@0 | 288 | err_status_t |
michael@0 | 289 | cipher_driver_test_buffering(cipher_t *c) { |
michael@0 | 290 | int i, j, num_trials = 1000; |
michael@0 | 291 | unsigned len, buflen = 1024; |
michael@0 | 292 | uint8_t buffer0[buflen], buffer1[buflen], *current, *end; |
michael@0 | 293 | uint8_t idx[16] = { |
michael@0 | 294 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
michael@0 | 295 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34 |
michael@0 | 296 | }; |
michael@0 | 297 | err_status_t status; |
michael@0 | 298 | |
michael@0 | 299 | printf("testing output buffering for cipher %s...", |
michael@0 | 300 | c->type->description); |
michael@0 | 301 | |
michael@0 | 302 | for (i=0; i < num_trials; i++) { |
michael@0 | 303 | |
michael@0 | 304 | /* set buffers to zero */ |
michael@0 | 305 | for (j=0; j < buflen; j++) |
michael@0 | 306 | buffer0[j] = buffer1[j] = 0; |
michael@0 | 307 | |
michael@0 | 308 | /* initialize cipher */ |
michael@0 | 309 | status = cipher_set_iv(c, idx); |
michael@0 | 310 | if (status) |
michael@0 | 311 | return status; |
michael@0 | 312 | |
michael@0 | 313 | /* generate 'reference' value by encrypting all at once */ |
michael@0 | 314 | status = cipher_encrypt(c, buffer0, &buflen); |
michael@0 | 315 | if (status) |
michael@0 | 316 | return status; |
michael@0 | 317 | |
michael@0 | 318 | /* re-initialize cipher */ |
michael@0 | 319 | status = cipher_set_iv(c, idx); |
michael@0 | 320 | if (status) |
michael@0 | 321 | return status; |
michael@0 | 322 | |
michael@0 | 323 | /* now loop over short lengths until buffer1 is encrypted */ |
michael@0 | 324 | current = buffer1; |
michael@0 | 325 | end = buffer1 + buflen; |
michael@0 | 326 | while (current < end) { |
michael@0 | 327 | |
michael@0 | 328 | /* choose a short length */ |
michael@0 | 329 | len = rand() & 0x01f; |
michael@0 | 330 | |
michael@0 | 331 | /* make sure that len doesn't cause us to overreach the buffer */ |
michael@0 | 332 | if (current + len > end) |
michael@0 | 333 | len = end - current; |
michael@0 | 334 | |
michael@0 | 335 | status = cipher_encrypt(c, current, &len); |
michael@0 | 336 | if (status) |
michael@0 | 337 | return status; |
michael@0 | 338 | |
michael@0 | 339 | /* advance pointer into buffer1 to reflect encryption */ |
michael@0 | 340 | current += len; |
michael@0 | 341 | |
michael@0 | 342 | /* if buffer1 is all encrypted, break out of loop */ |
michael@0 | 343 | if (current == end) |
michael@0 | 344 | break; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | /* compare buffers */ |
michael@0 | 348 | for (j=0; j < buflen; j++) |
michael@0 | 349 | if (buffer0[j] != buffer1[j]) { |
michael@0 | 350 | #if PRINT_DEBUG |
michael@0 | 351 | printf("test case %d failed at byte %d\n", i, j); |
michael@0 | 352 | printf("computed: %s\n", octet_string_hex_string(buffer1, buflen)); |
michael@0 | 353 | printf("expected: %s\n", octet_string_hex_string(buffer0, buflen)); |
michael@0 | 354 | #endif |
michael@0 | 355 | return err_status_algo_fail; |
michael@0 | 356 | } |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | printf("passed\n"); |
michael@0 | 360 | |
michael@0 | 361 | return err_status_ok; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | |
michael@0 | 365 | /* |
michael@0 | 366 | * The function cipher_test_throughput_array() tests the effect of CPU |
michael@0 | 367 | * cache thrash on cipher throughput. |
michael@0 | 368 | * |
michael@0 | 369 | * cipher_array_alloc_init(ctype, array, num_ciphers) creates an array |
michael@0 | 370 | * of cipher_t of type ctype |
michael@0 | 371 | */ |
michael@0 | 372 | |
michael@0 | 373 | err_status_t |
michael@0 | 374 | cipher_array_alloc_init(cipher_t ***ca, int num_ciphers, |
michael@0 | 375 | cipher_type_t *ctype, int klen) { |
michael@0 | 376 | int i, j; |
michael@0 | 377 | err_status_t status; |
michael@0 | 378 | uint8_t *key; |
michael@0 | 379 | cipher_t **cipher_array; |
michael@0 | 380 | /* pad klen allocation, to handle aes_icm reading 16 bytes for the |
michael@0 | 381 | 14-byte salt */ |
michael@0 | 382 | int klen_pad = ((klen + 15) >> 4) << 4; |
michael@0 | 383 | |
michael@0 | 384 | /* allocate array of pointers to ciphers */ |
michael@0 | 385 | cipher_array = (cipher_t **) malloc(sizeof(cipher_t *) * num_ciphers); |
michael@0 | 386 | if (cipher_array == NULL) |
michael@0 | 387 | return err_status_alloc_fail; |
michael@0 | 388 | |
michael@0 | 389 | /* set ca to location of cipher_array */ |
michael@0 | 390 | *ca = cipher_array; |
michael@0 | 391 | |
michael@0 | 392 | /* allocate key */ |
michael@0 | 393 | key = crypto_alloc(klen_pad); |
michael@0 | 394 | if (key == NULL) { |
michael@0 | 395 | free(cipher_array); |
michael@0 | 396 | return err_status_alloc_fail; |
michael@0 | 397 | } |
michael@0 | 398 | |
michael@0 | 399 | /* allocate and initialize an array of ciphers */ |
michael@0 | 400 | for (i=0; i < num_ciphers; i++) { |
michael@0 | 401 | |
michael@0 | 402 | /* allocate cipher */ |
michael@0 | 403 | status = cipher_type_alloc(ctype, cipher_array, klen); |
michael@0 | 404 | if (status) |
michael@0 | 405 | return status; |
michael@0 | 406 | |
michael@0 | 407 | /* generate random key and initialize cipher */ |
michael@0 | 408 | for (j=0; j < klen; j++) |
michael@0 | 409 | key[j] = (uint8_t) rand(); |
michael@0 | 410 | for (; j < klen_pad; j++) |
michael@0 | 411 | key[j] = 0; |
michael@0 | 412 | status = cipher_init(*cipher_array, key, direction_encrypt); |
michael@0 | 413 | if (status) |
michael@0 | 414 | return status; |
michael@0 | 415 | |
michael@0 | 416 | /* printf("%dth cipher is at %p\n", i, *cipher_array); */ |
michael@0 | 417 | /* printf("%dth cipher description: %s\n", i, */ |
michael@0 | 418 | /* (*cipher_array)->type->description); */ |
michael@0 | 419 | |
michael@0 | 420 | /* advance cipher array pointer */ |
michael@0 | 421 | cipher_array++; |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | crypto_free(key); |
michael@0 | 425 | |
michael@0 | 426 | return err_status_ok; |
michael@0 | 427 | } |
michael@0 | 428 | |
michael@0 | 429 | err_status_t |
michael@0 | 430 | cipher_array_delete(cipher_t *cipher_array[], int num_cipher) { |
michael@0 | 431 | int i; |
michael@0 | 432 | |
michael@0 | 433 | for (i=0; i < num_cipher; i++) { |
michael@0 | 434 | cipher_dealloc(cipher_array[i]); |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | free(cipher_array); |
michael@0 | 438 | |
michael@0 | 439 | return err_status_ok; |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | |
michael@0 | 443 | /* |
michael@0 | 444 | * cipher_array_bits_per_second(c, l, t) computes (an estimate of) the |
michael@0 | 445 | * number of bits that a cipher implementation can encrypt in a second |
michael@0 | 446 | * when distinct keys are used to encrypt distinct messages |
michael@0 | 447 | * |
michael@0 | 448 | * c is a cipher (which MUST be allocated an initialized already), l |
michael@0 | 449 | * is the length in octets of the test data to be encrypted, and t is |
michael@0 | 450 | * the number of trials |
michael@0 | 451 | * |
michael@0 | 452 | * if an error is encountered, the value 0 is returned |
michael@0 | 453 | */ |
michael@0 | 454 | |
michael@0 | 455 | uint64_t |
michael@0 | 456 | cipher_array_bits_per_second(cipher_t *cipher_array[], int num_cipher, |
michael@0 | 457 | unsigned octets_in_buffer, int num_trials) { |
michael@0 | 458 | int i; |
michael@0 | 459 | v128_t nonce; |
michael@0 | 460 | clock_t timer; |
michael@0 | 461 | unsigned char *enc_buf; |
michael@0 | 462 | int cipher_index = rand() % num_cipher; |
michael@0 | 463 | |
michael@0 | 464 | /* Over-alloc, for NIST CBC padding */ |
michael@0 | 465 | enc_buf = crypto_alloc(octets_in_buffer+17); |
michael@0 | 466 | if (enc_buf == NULL) |
michael@0 | 467 | return 0; /* indicate bad parameters by returning null */ |
michael@0 | 468 | memset(enc_buf, 0, octets_in_buffer); |
michael@0 | 469 | |
michael@0 | 470 | /* time repeated trials */ |
michael@0 | 471 | v128_set_to_zero(&nonce); |
michael@0 | 472 | timer = clock(); |
michael@0 | 473 | for(i=0; i < num_trials; i++, nonce.v32[3] = i) { |
michael@0 | 474 | /* length parameter to cipher_encrypt is in/out -- out is total, padded |
michael@0 | 475 | * length -- so reset it each time. */ |
michael@0 | 476 | unsigned octets_to_encrypt = octets_in_buffer; |
michael@0 | 477 | |
michael@0 | 478 | /* encrypt buffer with cipher */ |
michael@0 | 479 | cipher_set_iv(cipher_array[cipher_index], &nonce); |
michael@0 | 480 | cipher_encrypt(cipher_array[cipher_index], enc_buf, &octets_to_encrypt); |
michael@0 | 481 | |
michael@0 | 482 | /* choose a cipher at random from the array*/ |
michael@0 | 483 | cipher_index = (*((uint32_t *)enc_buf)) % num_cipher; |
michael@0 | 484 | } |
michael@0 | 485 | timer = clock() - timer; |
michael@0 | 486 | |
michael@0 | 487 | free(enc_buf); |
michael@0 | 488 | |
michael@0 | 489 | if (timer == 0) { |
michael@0 | 490 | /* Too fast! */ |
michael@0 | 491 | return 0; |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer; |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | void |
michael@0 | 498 | cipher_array_test_throughput(cipher_t *ca[], int num_cipher) { |
michael@0 | 499 | int i; |
michael@0 | 500 | int min_enc_len = 16; |
michael@0 | 501 | int max_enc_len = 2048; /* should be a power of two */ |
michael@0 | 502 | int num_trials = 1000000; |
michael@0 | 503 | |
michael@0 | 504 | printf("timing %s throughput with key length %d, array size %d:\n", |
michael@0 | 505 | (ca[0])->type->description, (ca[0])->key_len, num_cipher); |
michael@0 | 506 | fflush(stdout); |
michael@0 | 507 | for (i=min_enc_len; i <= max_enc_len; i = i * 4) |
michael@0 | 508 | printf("msg len: %d\tgigabits per second: %f\n", i, |
michael@0 | 509 | cipher_array_bits_per_second(ca, num_cipher, i, num_trials) / 1e9); |
michael@0 | 510 | |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | err_status_t |
michael@0 | 514 | cipher_driver_test_array_throughput(cipher_type_t *ct, |
michael@0 | 515 | int klen, int num_cipher) { |
michael@0 | 516 | cipher_t **ca = NULL; |
michael@0 | 517 | err_status_t status; |
michael@0 | 518 | |
michael@0 | 519 | status = cipher_array_alloc_init(&ca, num_cipher, ct, klen); |
michael@0 | 520 | if (status) { |
michael@0 | 521 | printf("error: cipher_array_alloc_init() failed with error code %d\n", |
michael@0 | 522 | status); |
michael@0 | 523 | return status; |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | cipher_array_test_throughput(ca, num_cipher); |
michael@0 | 527 | |
michael@0 | 528 | cipher_array_delete(ca, num_cipher); |
michael@0 | 529 | |
michael@0 | 530 | return err_status_ok; |
michael@0 | 531 | } |