Changeset 5261 for pjproject/trunk/third_party/srtp/crypto/cipher/cipher.c
- Timestamp:
- Mar 15, 2016 3:57:39 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/third_party/srtp/crypto/cipher/cipher.c
r2660 r5261 11 11 /* 12 12 * 13 * Copyright (c) 2001-2006, Cisco Systems, Inc.13 * Copyright (c) 2001-2006,2013 Cisco Systems, Inc. 14 14 * All rights reserved. 15 15 * … … 45 45 */ 46 46 47 #ifdef HAVE_CONFIG_H 48 #include <config.h> 49 #endif 50 47 51 #include "cipher.h" 52 #include "crypto_types.h" 48 53 #include "rand_source.h" /* used in invertibiltiy tests */ 49 54 #include "alloc.h" /* for crypto_alloc(), crypto_free() */ … … 72 77 73 78 /* 74 * cipher_type_ self_test(ct) tests a cipher of type ct against test cases75 * provided in an array of values of key, salt, xtd_seq_num_t,79 * cipher_type_test(ct, test_data) tests a cipher of type ct against 80 * test cases provided in a list test_data of values of key, salt, iv, 76 81 * plaintext, and ciphertext that is known to be good 77 82 */ … … 82 87 83 88 err_status_t 84 cipher_type_ self_test(const cipher_type_t *ct) {85 const cipher_test_case_t *test_case = ct->test_data;89 cipher_type_test(const cipher_type_t *ct, const cipher_test_case_t *test_data) { 90 const cipher_test_case_t *test_case = test_data; 86 91 cipher_t *c; 87 92 err_status_t status; 88 93 uint8_t buffer[SELF_TEST_BUF_OCTETS]; 89 94 uint8_t buffer2[SELF_TEST_BUF_OCTETS]; 95 int tag_len; 90 96 unsigned int len; 91 97 int i, j, case_num = 0; … … 106 112 */ 107 113 while (test_case != NULL) { 108 109 114 /* allocate cipher */ 110 status = cipher_type_alloc(ct, &c, test_case->key_length_octets );115 status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->tag_length_octets); 111 116 if (status) 112 117 return status; … … 118 123 119 124 /* initialize cipher */ 120 status = cipher_init(c, test_case->key , direction_encrypt);125 status = cipher_init(c, test_case->key); 121 126 if (status) { 122 127 cipher_dealloc(c); … … 137 142 138 143 /* set the initialization vector */ 139 status = cipher_set_iv(c, test_case->idx );144 status = cipher_set_iv(c, test_case->idx, direction_encrypt); 140 145 if (status) { 141 146 cipher_dealloc(c); … … 143 148 } 144 149 150 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 151 debug_print(mod_cipher, "IV: %s", 152 octet_string_hex_string(test_case->idx, 12)); 153 154 /* 155 * Set the AAD 156 */ 157 status = cipher_set_aad(c, test_case->aad, 158 test_case->aad_length_octets); 159 if (status) { 160 cipher_dealloc(c); 161 return status; 162 } 163 debug_print(mod_cipher, "AAD: %s", 164 octet_string_hex_string(test_case->aad, 165 test_case->aad_length_octets)); 166 } 167 145 168 /* encrypt */ 146 169 len = test_case->plaintext_length_octets; … … 151 174 } 152 175 176 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 177 /* 178 * Get the GCM tag 179 */ 180 status = cipher_get_tag(c, buffer + len, &tag_len); 181 if (status) { 182 cipher_dealloc(c); 183 return status; 184 } 185 len += tag_len; 186 } 187 153 188 debug_print(mod_cipher, "ciphertext: %s", 154 189 octet_string_hex_string(buffer, … … 185 220 186 221 /* re-initialize cipher for decryption */ 187 status = cipher_init(c, test_case->key , direction_decrypt);222 status = cipher_init(c, test_case->key); 188 223 if (status) { 189 224 cipher_dealloc(c); … … 204 239 205 240 /* set the initialization vector */ 206 status = cipher_set_iv(c, test_case->idx );241 status = cipher_set_iv(c, test_case->idx, direction_decrypt); 207 242 if (status) { 208 243 cipher_dealloc(c); … … 210 245 } 211 246 247 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 248 /* 249 * Set the AAD 250 */ 251 status = cipher_set_aad(c, test_case->aad, 252 test_case->aad_length_octets); 253 if (status) { 254 cipher_dealloc(c); 255 return status; 256 } 257 debug_print(mod_cipher, "AAD: %s", 258 octet_string_hex_string(test_case->aad, 259 test_case->aad_length_octets)); 260 } 261 212 262 /* decrypt */ 213 263 len = test_case->ciphertext_length_octets; … … 261 311 262 312 /* allocate cipher, using paramaters from the first test case */ 263 test_case = ct->test_data;264 status = cipher_type_alloc(ct, &c, test_case->key_length_octets );313 test_case = test_data; 314 status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->tag_length_octets); 265 315 if (status) 266 316 return status; … … 270 320 for (j=0; j < NUM_RAND_TESTS; j++) { 271 321 unsigned length; 272 unsignedplaintext_len;322 int plaintext_len; 273 323 uint8_t key[MAX_KEY_LEN]; 274 324 uint8_t iv[MAX_KEY_LEN]; … … 298 348 299 349 /* initialize cipher */ 300 status = cipher_init(c, key , direction_encrypt);350 status = cipher_init(c, key); 301 351 if (status) { 302 352 cipher_dealloc(c); … … 305 355 306 356 /* set initialization vector */ 307 status = cipher_set_iv(c, test_case->idx );357 status = cipher_set_iv(c, test_case->idx, direction_encrypt); 308 358 if (status) { 309 359 cipher_dealloc(c); 310 360 return status; 311 361 } 362 363 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 364 /* 365 * Set the AAD 366 */ 367 status = cipher_set_aad(c, test_case->aad, 368 test_case->aad_length_octets); 369 if (status) { 370 cipher_dealloc(c); 371 return status; 372 } 373 debug_print(mod_cipher, "AAD: %s", 374 octet_string_hex_string(test_case->aad, 375 test_case->aad_length_octets)); 376 } 312 377 313 378 /* encrypt buffer with cipher */ … … 317 382 cipher_dealloc(c); 318 383 return status; 384 } 385 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 386 /* 387 * Get the GCM tag 388 */ 389 status = cipher_get_tag(c, buffer + length, &tag_len); 390 if (status) { 391 cipher_dealloc(c); 392 return status; 393 } 394 length += tag_len; 319 395 } 320 396 debug_print(mod_cipher, "ciphertext: %s", … … 325 401 * decrypt the ciphertext 326 402 */ 327 status = cipher_init(c, key , direction_decrypt);328 if (status) { 329 cipher_dealloc(c); 330 return status; 331 } 332 status = cipher_set_iv(c, test_case->idx );403 status = cipher_init(c, key); 404 if (status) { 405 cipher_dealloc(c); 406 return status; 407 } 408 status = cipher_set_iv(c, test_case->idx, direction_decrypt); 333 409 if (status) { 334 410 cipher_dealloc(c); 335 411 return status; 336 412 } 413 if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 414 /* 415 * Set the AAD 416 */ 417 status = cipher_set_aad(c, test_case->aad, 418 test_case->aad_length_octets); 419 if (status) { 420 cipher_dealloc(c); 421 return status; 422 } 423 debug_print(mod_cipher, "AAD: %s", 424 octet_string_hex_string(test_case->aad, 425 test_case->aad_length_octets)); 426 } 337 427 status = cipher_decrypt(c, buffer, &length); 338 428 if (status) { … … 345 435 346 436 /* compare the resulting plaintext with the original one */ 347 if (length != plaintext_len) 437 if (length != plaintext_len) { 348 438 return err_status_algo_fail; 439 } 349 440 status = err_status_ok; 350 441 for (i=0; i < plaintext_len; i++) … … 361 452 } 362 453 363 cipher_dealloc(c); 454 status = cipher_dealloc(c); 455 if (status) 456 return status; 364 457 365 458 return err_status_ok; 366 459 } 367 460 461 462 /* 463 * cipher_type_self_test(ct) performs cipher_type_test on ct's internal 464 * list of test data. 465 */ 466 467 err_status_t 468 cipher_type_self_test(const cipher_type_t *ct) { 469 return cipher_type_test(ct, ct->test_data); 470 } 368 471 369 472 /* … … 394 497 timer = clock(); 395 498 for(i=0; i < num_trials; i++, nonce.v32[3] = i) { 396 cipher_set_iv(c, &nonce );499 cipher_set_iv(c, &nonce, direction_encrypt); 397 500 cipher_encrypt(c, enc_buf, &len); 398 501 }
Note: See TracChangeset
for help on using the changeset viewer.