| 6 |
| * David A. McGrew |
| 7 | * Cisco Systems, Inc. |
| 8 | * |
| 9 | */ |
| 10 |
| 11 | /* |
| 12 | * |
| 13 | * Copyright (c) 2001-2017 Cisco Systems, Inc. |
| 14 | * All rights reserved. |
| 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * |
| 20 | * Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * |
| 23 | * Redistributions in binary form must reproduce the above |
| 24 | * copyright notice, this list of conditions and the following |
| 25 | * disclaimer in the documentation and/or other materials provided |
| 26 | * with the distribution. |
| 27 | * |
| 28 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
| 29 | * contributors may be used to endorse or promote products derived |
| 30 | * from this software without specific prior written permission. |
| 31 | * |
| 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 33 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 35 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 36 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 37 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 38 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 39 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 40 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 41 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 42 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 43 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 | * |
| 45 | */ |
| 46 |
| 47 | #include "config.h" |
| 48 |
| 49 | #include "cipher.h" |
| 50 | #include "crypto_types.h" |
| 51 | #include "err.h" /* for srtp_debug */ |
| 52 | #include "alloc.h" /* for crypto_alloc(), crypto_free() */ |
| 53 |
| 54 | srtp_debug_module_t srtp_mod_cipher = { |
| 55 | 0, /* debugging is off by default */ |
| 56 | "cipher" /* printable module name */ |
| 57 | }; |
| 58 |
| 59 | srtp_err_status_t srtp_cipher_type_alloc(const srtp_cipher_type_t *ct, |
| 60 | srtp_cipher_t **c, |
| 61 | int key_len, |
| 62 | int tlen) |
| 63 | { |
| 64 | if (!ct || !ct->alloc) { |
| 65 | return (srtp_err_status_bad_param); |
| 66 | } |
| 67 | return ((ct)->alloc((c), (key_len), (tlen))); |
| 68 | } |
| 69 |
| 70 | srtp_err_status_t srtp_cipher_dealloc(srtp_cipher_t *c) |
| 71 | { |
| 72 | if (!c || !c->type) { |
| 73 | return (srtp_err_status_bad_param); |
| 74 | } |
| 75 | return (((c)->type)->dealloc(c)); |
| 76 | } |
| 77 |
| 78 | srtp_err_status_t srtp_cipher_init(srtp_cipher_t *c, const uint8_t *key) |
| 79 | { |
| 80 | if (!c || !c->type || !c->state) { |
| 81 | return (srtp_err_status_bad_param); |
| 82 | } |
| 83 | return (((c)->type)->init(((c)->state), (key))); |
| 84 | } |
| 85 |
| 86 | srtp_err_status_t srtp_cipher_set_iv(srtp_cipher_t *c, |
| 87 | uint8_t *iv, |
| 88 | int direction) |
| 89 | { |
| 90 | if (!c || !c->type || !c->state) { |
| 91 | return (srtp_err_status_bad_param); |
| 92 | } |
| 93 |
| 94 | return (((c)->type)->set_iv(((c)->state), iv, direction)); |
| 95 | } |
| 96 |
| 97 | srtp_err_status_t srtp_cipher_output(srtp_cipher_t *c, |
| 98 | uint8_t *buffer, |
| 99 | uint32_t *num_octets_to_output) |
| 100 | { |
| 101 | /* zeroize the buffer */ |
| 102 | octet_string_set_to_zero(buffer, *num_octets_to_output); |
| 103 |
| 104 | /* exor keystream into buffer */ |
| 105 | return (((c)->type)->encrypt(((c)->state), buffer, num_octets_to_output)); |
| 106 | } |
| 107 |
| 108 | srtp_err_status_t srtp_cipher_encrypt(srtp_cipher_t *c, |
| 109 | uint8_t *buffer, |
| 110 | uint32_t *num_octets_to_output) |
| 111 | { |
| 112 | if (!c || !c->type || !c->state) { |
| 113 | return (srtp_err_status_bad_param); |
| 114 | } |
| 115 |
| 116 | return (((c)->type)->encrypt(((c)->state), buffer, num_octets_to_output)); |
| 117 | } |
| 118 |
| 119 | srtp_err_status_t srtp_cipher_decrypt(srtp_cipher_t *c, |
| 120 | uint8_t *buffer, |
| 121 | uint32_t *num_octets_to_output) |
| 122 | { |
| 123 | if (!c || !c->type || !c->state) { |
| 124 | return (srtp_err_status_bad_param); |
| 125 | } |
| 126 |
| 127 | return (((c)->type)->decrypt(((c)->state), buffer, num_octets_to_output)); |
| 128 | } |
| 129 |
| 130 | srtp_err_status_t srtp_cipher_get_tag(srtp_cipher_t *c, |
| 131 | uint8_t *buffer, |
| 132 | uint32_t *tag_len) |
| 133 | { |
| 134 | if (!c || !c->type || !c->state) { |
| 135 | return (srtp_err_status_bad_param); |
| 136 | } |
| 137 | if (!((c)->type)->get_tag) { |
| 138 | return (srtp_err_status_no_such_op); |
| 139 | } |
| 140 |
| 141 | return (((c)->type)->get_tag(((c)->state), buffer, tag_len)); |
| 142 | } |
| 143 |
| 144 | srtp_err_status_t srtp_cipher_set_aad(srtp_cipher_t *c, |
| 145 | const uint8_t *aad, |
| 146 | uint32_t aad_len) |
| 147 | { |
| 148 | if (!c || !c->type || !c->state) { |
| 149 | return (srtp_err_status_bad_param); |
| 150 | } |
| 151 | if (!((c)->type)->set_aad) { |
| 152 | return (srtp_err_status_no_such_op); |
| 153 | } |
| 154 |
| 155 | return (((c)->type)->set_aad(((c)->state), aad, aad_len)); |
| 156 | } |
| 157 |
| 158 | /* some bookkeeping functions */ |
| 159 |
| 160 | int srtp_cipher_get_key_length(const srtp_cipher_t *c) |
| 161 | { |
| 162 | return c->key_len; |
| 163 | } |
| 164 |
| 165 | /* |
| 166 | * A trivial platform independent random source. The random |
| 167 | * data is used for some of the cipher self-tests. |
| 168 | */ |
| 169 | static srtp_err_status_t srtp_cipher_rand(void *dest, uint32_t len) |
| 170 | { |
| 171 | #if defined(HAVE_RAND_S) |
| 172 | uint8_t *dst = (uint8_t *)dest; |
| 173 | while (len) { |
| 174 | unsigned int val; |
| 175 | errno_t err = rand_s(&val); |
| 176 |
| 177 | if (err != 0) |
| 178 | return srtp_err_status_fail; |
| 179 |
| 180 | *dst++ = val & 0xff; |
| 181 | len--; |
| 182 | } |
| 183 | #else |
| 184 | /* Generic C-library (rand()) version */ |
| 185 | /* This is a random source of last resort */ |
| 186 | uint8_t *dst = (uint8_t *)dest; |
| 187 | while (len) { |
| 188 | int val = rand(); |
| 189 | /* rand() returns 0-32767 (ugh) */ |
| 190 | /* Is this a good enough way to get random bytes? |
| 191 | It is if it passes FIPS-140... */ |
| 192 | *dst++ = val & 0xff; |
| 193 | len--; |
| 194 | } |
| 195 | #endif |
| 196 | return srtp_err_status_ok; |
| 197 | } |
| 198 |
| 199 | #define SELF_TEST_BUF_OCTETS 128 |
| 200 | #define NUM_RAND_TESTS 128 |
| 201 | #define MAX_KEY_LEN 64 |
| 202 | /* |
| 203 | * srtp_cipher_type_test(ct, test_data) tests a cipher of type ct against |
| 204 | * test cases provided in a list test_data of values of key, salt, iv, |
| 205 | * plaintext, and ciphertext that is known to be good |
| 206 | */ |
| 207 | srtp_err_status_t srtp_cipher_type_test( |
| 208 | const srtp_cipher_type_t *ct, |
| 209 | const srtp_cipher_test_case_t *test_data) |
| 210 | { |
| 211 | const srtp_cipher_test_case_t *test_case = test_data; |
| 212 | srtp_cipher_t *c; |
| 213 | srtp_err_status_t status; |
| 214 | uint8_t buffer[SELF_TEST_BUF_OCTETS]; |
| 215 | uint8_t buffer2[SELF_TEST_BUF_OCTETS]; |
| 216 | uint32_t tag_len; |
| 217 | unsigned int len; |
| 218 | int i, j, case_num = 0; |
| 219 | unsigned k = 0; |
| 220 |
| 221 | debug_print(srtp_mod_cipher, "running self-test for cipher %s", |
| 222 | ct->description); |
| 223 |
| 224 | /* |
| 225 | * check to make sure that we have at least one test case, and |
| 226 | * return an error if we don't - we need to be paranoid here |
| 227 | */ |
| 228 | if (test_case == NULL) { |
| 229 | return srtp_err_status_cant_check; |
| 230 | } |
| 231 |
| 232 | /* |
| 233 | * loop over all test cases, perform known-answer tests of both the |
| 234 | * encryption and decryption functions |
| 235 | */ |
| 236 | while (test_case != NULL) { |
| 237 | /* allocate cipher */ |
| 238 | status = srtp_cipher_type_alloc(ct, &c, test_case->key_length_octets, |
| 239 | test_case->tag_length_octets); |
| 240 | if (status) { |
| 241 | return status; |
| 242 | } |
| 243 |
| 244 | /* |
| 245 | * test the encrypt function |
| 246 | */ |
| 247 | debug_print(srtp_mod_cipher, "testing encryption", NULL); |
| 248 |
| 249 | /* initialize cipher */ |
| 250 | status = srtp_cipher_init(c, test_case->key); |
| 251 | if (status) { |
| 252 | srtp_cipher_dealloc(c); |
| 253 | return status; |
| 254 | } |
| 255 |
| 256 | /* copy plaintext into test buffer */ |
| 257 | if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) { |
| 258 | srtp_cipher_dealloc(c); |
| 259 | return srtp_err_status_bad_param; |
| 260 | } |
| 261 | for (k = 0; k < test_case->plaintext_length_octets; k++) { |
| 262 | buffer[k] = test_case->plaintext[k]; |
| 263 | } |
| 264 |
| 265 | debug_print(srtp_mod_cipher, "plaintext: %s", |
| 266 | srtp_octet_string_hex_string( |
| 267 | buffer, test_case->plaintext_length_octets)); |
| 268 |
| 269 | /* set the initialization vector */ |
| 270 | status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx, |
| 271 | srtp_direction_encrypt); |
| 272 | if (status) { |
| 273 | srtp_cipher_dealloc(c); |
| 274 | return status; |
| 275 | } |
| 276 |
| 277 | if (c->algorithm == SRTP_AES_GCM_128 || |
| 278 | c->algorithm == SRTP_AES_GCM_256) { |
| 279 | debug_print(srtp_mod_cipher, "IV: %s", |
| 280 | srtp_octet_string_hex_string(test_case->idx, 12)); |
| 281 |
| 282 | /* |
| 283 | * Set the AAD |
| 284 | */ |
| 285 | status = srtp_cipher_set_aad(c, test_case->aad, |
| 286 | test_case->aad_length_octets); |
| 287 | if (status) { |
| 288 | srtp_cipher_dealloc(c); |
| 289 | return status; |
| 290 | } |
| 291 | debug_print(srtp_mod_cipher, "AAD: %s", |
| 292 | srtp_octet_string_hex_string( |
| 293 | test_case->aad, test_case->aad_length_octets)); |
| 294 | } |
| 295 |
| 296 | /* encrypt */ |
| 297 | len = test_case->plaintext_length_octets; |
| 298 | status = srtp_cipher_encrypt(c, buffer, &len); |
| 299 | if (status) { |
| 300 | srtp_cipher_dealloc(c); |
| 301 | return status; |
| 302 | } |
| 303 |
| 304 | if (c->algorithm == SRTP_AES_GCM_128 || |
| 305 | c->algorithm == SRTP_AES_GCM_256) { |
| 306 | /* |
| 307 | * Get the GCM tag |
| 308 | */ |
| 309 | status = srtp_cipher_get_tag(c, buffer + len, &tag_len); |
| 310 | if (status) { |
| 311 | srtp_cipher_dealloc(c); |
| 312 | return status; |
| 313 | } |
| 314 | len += tag_len; |
| 315 | } |
| 316 |
| 317 | debug_print(srtp_mod_cipher, "ciphertext: %s", |
| 318 | srtp_octet_string_hex_string( |
| 319 | buffer, test_case->ciphertext_length_octets)); |
| 320 |
| 321 | /* compare the resulting ciphertext with that in the test case */ |
| 322 | if (len != test_case->ciphertext_length_octets) { |
| 323 | srtp_cipher_dealloc(c); |
| 324 | return srtp_err_status_algo_fail; |
| 325 | } |
| 326 | status = srtp_err_status_ok; |
| 327 | for (k = 0; k < test_case->ciphertext_length_octets; k++) { |
| 328 | if (buffer[k] != test_case->ciphertext[k]) { |
| 329 | status = srtp_err_status_algo_fail; |
| 330 | debug_print(srtp_mod_cipher, "test case %d failed", case_num); |
| 331 | debug_print(srtp_mod_cipher, "(failure at byte %u)", k); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | if (status) { |
| 336 | debug_print(srtp_mod_cipher, "c computed: %s", |
| 337 | srtp_octet_string_hex_string( |
| 338 | buffer, 2 * test_case->plaintext_length_octets)); |
| 339 | debug_print(srtp_mod_cipher, "c expected: %s", |
| 340 | srtp_octet_string_hex_string( |
| 341 | test_case->ciphertext, |
| 342 | 2 * test_case->plaintext_length_octets)); |
| 343 |
| 344 | srtp_cipher_dealloc(c); |
| 345 | return srtp_err_status_algo_fail; |
| 346 | } |
| 347 |
| 348 | /* |
| 349 | * test the decrypt function |
| 350 | */ |
| 351 | debug_print(srtp_mod_cipher, "testing decryption", NULL); |
| 352 |
| 353 | /* re-initialize cipher for decryption */ |
| 354 | status = srtp_cipher_init(c, test_case->key); |
| 355 | if (status) { |
| 356 | srtp_cipher_dealloc(c); |
| 357 | return status; |
| 358 | } |
| 359 |
| 360 | /* copy ciphertext into test buffer */ |
| 361 | if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) { |
| 362 | srtp_cipher_dealloc(c); |
| 363 | return srtp_err_status_bad_param; |
| 364 | } |
| 365 | for (k = 0; k < test_case->ciphertext_length_octets; k++) { |
| 366 | buffer[k] = test_case->ciphertext[k]; |
| 367 | } |
| 368 |
| 369 | debug_print(srtp_mod_cipher, "ciphertext: %s", |
| 370 | srtp_octet_string_hex_string( |
| 371 | buffer, test_case->plaintext_length_octets)); |
| 372 |
| 373 | /* set the initialization vector */ |
| 374 | status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx, |
| 375 | srtp_direction_decrypt); |
| 376 | if (status) { |
| 377 | srtp_cipher_dealloc(c); |
| 378 | return status; |
| 379 | } |
| 380 |
| 381 | if (c->algorithm == SRTP_AES_GCM_128 || |
| 382 | c->algorithm == SRTP_AES_GCM_256) { |
| 383 | /* |
| 384 | * Set the AAD |
| 385 | */ |
| 386 | status = srtp_cipher_set_aad(c, test_case->aad, |
| 387 | test_case->aad_length_octets); |
| 388 | if (status) { |
| 389 | srtp_cipher_dealloc(c); |
| 390 | return status; |
| 391 | } |
| 392 | debug_print(srtp_mod_cipher, "AAD: %s", |
| 393 | srtp_octet_string_hex_string( |
| 394 | test_case->aad, test_case->aad_length_octets)); |
| 395 | } |
| 396 |
| 397 | /* decrypt */ |
| 398 | len = test_case->ciphertext_length_octets; |
| 399 | status = srtp_cipher_decrypt(c, buffer, &len); |
| 400 | if (status) { |
| 401 | srtp_cipher_dealloc(c); |
| 402 | return status; |
| 403 | } |
| 404 |
| 405 | debug_print(srtp_mod_cipher, "plaintext: %s", |
| 406 | srtp_octet_string_hex_string( |
| 407 | buffer, test_case->plaintext_length_octets)); |
| 408 |
| 409 | /* compare the resulting plaintext with that in the test case */ |
| 410 | if (len != test_case->plaintext_length_octets) { |
| 411 | srtp_cipher_dealloc(c); |
| 412 | return srtp_err_status_algo_fail; |
| 413 | } |
| 414 | status = srtp_err_status_ok; |
| 415 | for (k = 0; k < test_case->plaintext_length_octets; k++) { |
| 416 | if (buffer[k] != test_case->plaintext[k]) { |
| 417 | status = srtp_err_status_algo_fail; |
| 418 | debug_print(srtp_mod_cipher, "test case %d failed", case_num); |
| 419 | debug_print(srtp_mod_cipher, "(failure at byte %u)", k); |
| 420 | } |
| 421 | } |
| 422 | if (status) { |
| 423 | debug_print(srtp_mod_cipher, "p computed: %s", |
| 424 | srtp_octet_string_hex_string( |
| 425 | buffer, 2 * test_case->plaintext_length_octets)); |
| 426 | debug_print(srtp_mod_cipher, "p expected: %s", |
| 427 | srtp_octet_string_hex_string( |
| 428 | test_case->plaintext, |
| 429 | 2 * test_case->plaintext_length_octets)); |
| 430 |
| 431 | srtp_cipher_dealloc(c); |
| 432 | return srtp_err_status_algo_fail; |
| 433 | } |
| 434 |
| 435 | /* deallocate the cipher */ |
| 436 | status = srtp_cipher_dealloc(c); |
| 437 | if (status) { |
| 438 | return status; |
| 439 | } |
| 440 |
| 441 | /* |
| 442 | * the cipher passed the test case, so move on to the next test |
| 443 | * case in the list; if NULL, we'l proceed to the next test |
| 444 | */ |
| 445 | test_case = test_case->next_test_case; |
| 446 | ++case_num; |
| 447 | } |
| 448 |
| 449 | /* now run some random invertibility tests */ |
| 450 |
| 451 | /* allocate cipher, using paramaters from the first test case */ |
| 452 | test_case = test_data; |
| 453 | status = srtp_cipher_type_alloc(ct, &c, test_case->key_length_octets, |
| 454 | test_case->tag_length_octets); |
| 455 | if (status) { |
| 456 | return status; |
| 457 | } |
| 458 |
| 459 | for (j = 0; j < NUM_RAND_TESTS; j++) { |
| 460 | unsigned int length; |
| 461 | unsigned int plaintext_len; |
| 462 | uint8_t key[MAX_KEY_LEN]; |
| 463 | uint8_t iv[MAX_KEY_LEN]; |
| 464 |
| 465 | /* choose a length at random (leaving room for IV and padding) */ |
| 466 | length = rand() % (SELF_TEST_BUF_OCTETS - 64); |
| 467 | debug_print(srtp_mod_cipher, "random plaintext length %d\n", length); |
| 468 | status = srtp_cipher_rand(buffer, length); |
| 469 | if (status) { |
| 470 | srtp_cipher_dealloc(c); |
| 471 | return status; |
| 472 | } |
| 473 |
| 474 | debug_print(srtp_mod_cipher, "plaintext: %s", |
| 475 | srtp_octet_string_hex_string(buffer, length)); |
| 476 |
| 477 | /* copy plaintext into second buffer */ |
| 478 | for (i = 0; (unsigned int)i < length; i++) { |
| 479 | buffer2[i] = buffer[i]; |
| 480 | } |
| 481 |
| 482 | /* choose a key at random */ |
| 483 | if (test_case->key_length_octets > MAX_KEY_LEN) { |
| 484 | srtp_cipher_dealloc(c); |
| 485 | return srtp_err_status_cant_check; |
| 486 | } |
| 487 | status = srtp_cipher_rand(key, test_case->key_length_octets); |
| 488 | if (status) { |
| 489 | srtp_cipher_dealloc(c); |
| 490 | return status; |
| 491 | } |
| 492 |
| 493 | /* chose a random initialization vector */ |
| 494 | status = srtp_cipher_rand(iv, MAX_KEY_LEN); |
| 495 | if (status) { |
| 496 | srtp_cipher_dealloc(c); |
| 497 | return status; |
| 498 | } |
| 499 |
| 500 | /* initialize cipher */ |
| 501 | status = srtp_cipher_init(c, key); |
| 502 | if (status) { |
| 503 | srtp_cipher_dealloc(c); |
| 504 | return status; |
| 505 | } |
| 506 |
| 507 | /* set initialization vector */ |
| 508 | status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx, |
| 509 | srtp_direction_encrypt); |
| 510 | if (status) { |
| 511 | srtp_cipher_dealloc(c); |
| 512 | return status; |
| 513 | } |
| 514 |
| 515 | if (c->algorithm == SRTP_AES_GCM_128 || |
| 516 | c->algorithm == SRTP_AES_GCM_256) { |
| 517 | /* |
| 518 | * Set the AAD |
| 519 | */ |
| 520 | status = srtp_cipher_set_aad(c, test_case->aad, |
| 521 | test_case->aad_length_octets); |
| 522 | if (status) { |
| 523 | srtp_cipher_dealloc(c); |
| 524 | return status; |
| 525 | } |
| 526 | debug_print(srtp_mod_cipher, "AAD: %s", |
| 527 | srtp_octet_string_hex_string( |
| 528 | test_case->aad, test_case->aad_length_octets)); |
| 529 | } |
| 530 |
| 531 | /* encrypt buffer with cipher */ |
| 532 | plaintext_len = length; |
| 533 | status = srtp_cipher_encrypt(c, buffer, &length); |
| 534 | if (status) { |
| 535 | srtp_cipher_dealloc(c); |
| 536 | return status; |
| 537 | } |
| 538 | if (c->algorithm == SRTP_AES_GCM_128 || |
| 539 | c->algorithm == SRTP_AES_GCM_256) { |
| 540 | /* |
| 541 | * Get the GCM tag |
| 542 | */ |
| 543 | status = srtp_cipher_get_tag(c, buffer + length, &tag_len); |
| 544 | if (status) { |
| 545 | srtp_cipher_dealloc(c); |
| 546 | return status; |
| 547 | } |
| 548 | length += tag_len; |
| 549 | } |
| 550 | debug_print(srtp_mod_cipher, "ciphertext: %s", |
| 551 | srtp_octet_string_hex_string(buffer, length)); |
| 552 |
| 553 | /* |
| 554 | * re-initialize cipher for decryption, re-set the iv, then |
| 555 | * decrypt the ciphertext |
| 556 | */ |
| 557 | status = srtp_cipher_init(c, key); |
| 558 | if (status) { |
| 559 | srtp_cipher_dealloc(c); |
| 560 | return status; |
| 561 | } |
| 562 | status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx, |
| 563 | srtp_direction_decrypt); |
| 564 | if (status) { |
| 565 | srtp_cipher_dealloc(c); |
| 566 | return status; |
| 567 | } |
| 568 | if (c->algorithm == SRTP_AES_GCM_128 || |
| 569 | c->algorithm == SRTP_AES_GCM_256) { |
| 570 | /* |
| 571 | * Set the AAD |
| 572 | */ |
| 573 | status = srtp_cipher_set_aad(c, test_case->aad, |
| 574 | test_case->aad_length_octets); |
| 575 | if (status) { |
| 576 | srtp_cipher_dealloc(c); |
| 577 | return status; |
| 578 | } |
| 579 | debug_print(srtp_mod_cipher, "AAD: %s", |
| 580 | srtp_octet_string_hex_string( |
| 581 | test_case->aad, test_case->aad_length_octets)); |
| 582 | } |
| 583 | status = srtp_cipher_decrypt(c, buffer, &length); |
| 584 | if (status) { |
| 585 | srtp_cipher_dealloc(c); |
| 586 | return status; |
| 587 | } |
| 588 |
| 589 | debug_print(srtp_mod_cipher, "plaintext[2]: %s", |
| 590 | srtp_octet_string_hex_string(buffer, length)); |
| 591 |
| 592 | /* compare the resulting plaintext with the original one */ |
| 593 | if (length != plaintext_len) { |
| 594 | srtp_cipher_dealloc(c); |
| 595 | return srtp_err_status_algo_fail; |
| 596 | } |
| 597 | status = srtp_err_status_ok; |
| 598 | for (k = 0; k < plaintext_len; k++) { |
| 599 | if (buffer[k] != buffer2[k]) { |
| 600 | status = srtp_err_status_algo_fail; |
| 601 | debug_print(srtp_mod_cipher, "random test case %d failed", |
| 602 | case_num); |
| 603 | debug_print(srtp_mod_cipher, "(failure at byte %u)", k); |
| 604 | } |
| 605 | } |
| 606 | if (status) { |
| 607 | srtp_cipher_dealloc(c); |
| 608 | return srtp_err_status_algo_fail; |
| 609 | } |
| 610 | } |
| 611 |
| 612 | status = srtp_cipher_dealloc(c); |
| 613 | if (status) { |
| 614 | return status; |
| 615 | } |
| 616 |
| 617 | return srtp_err_status_ok; |
| 618 | } |
| 619 |
| 620 | /* |
| 621 | * srtp_cipher_type_self_test(ct) performs srtp_cipher_type_test on ct's |
| 622 | * internal list of test data. |
| 623 | */ |
| 624 | srtp_err_status_t srtp_cipher_type_self_test(const srtp_cipher_type_t *ct) |
| 625 | { |
| 626 | return srtp_cipher_type_test(ct, ct->test_data); |
| 627 | } |
| 628 |
| 629 | /* |
| 630 | * cipher_bits_per_second(c, l, t) computes (an estimate of) the |
| 631 | * number of bits that a cipher implementation can encrypt in a second |
| 632 | * |
| 633 | * c is a cipher (which MUST be allocated and initialized already), l |
| 634 | * is the length in octets of the test data to be encrypted, and t is |
| 635 | * the number of trials |
| 636 | * |
| 637 | * if an error is encountered, the value 0 is returned |
| 638 | */ |
| 639 | uint64_t srtp_cipher_bits_per_second(srtp_cipher_t *c, |
| 640 | int octets_in_buffer, |
| 641 | int num_trials) |
| 642 | { |
| 643 | int i; |
| 644 | v128_t nonce; |
| 645 | clock_t timer; |
| 646 | unsigned char *enc_buf; |
| 647 | unsigned int len = octets_in_buffer; |
| 648 |
| 649 | enc_buf = (unsigned char *)srtp_crypto_alloc(octets_in_buffer); |
| 650 | if (enc_buf == NULL) { |
| 651 | return 0; /* indicate bad parameters by returning null */ |
| 652 | } |
| 653 | /* time repeated trials */ |
| 654 | v128_set_to_zero(&nonce); |
| 655 | timer = clock(); |
| 656 | for (i = 0; i < num_trials; i++, nonce.v32[3] = i) { |
| 657 | if (srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt) != |
| 658 | srtp_err_status_ok) { |
| 659 | srtp_crypto_free(enc_buf); |
| 660 | return 0; |
| 661 | } |
| 662 | if (srtp_cipher_encrypt(c, enc_buf, &len) != srtp_err_status_ok) { |
| 663 | srtp_crypto_free(enc_buf); |
| 664 | return 0; |
| 665 | } |
| 666 | } |
| 667 | timer = clock() - timer; |
| 668 |
| 669 | srtp_crypto_free(enc_buf); |
| 670 |
| 671 | if (timer == 0) { |
| 672 | /* Too fast! */ |
| 673 | return 0; |
| 674 | } |
| 675 |
| 676 | return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer; |
| 677 | } |
| 678 |